2014-04-24 Segher Boessenkool <segher@kernel.crashing.org>
[official-gcc.git] / gcc / real.c
blob5cf2525b90a4cf763fc19e13f0270eb7989035b2
1 /* real.c - software floating point emulation.
2 Copyright (C) 1993-2014 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 "tree.h"
27 #include "diagnostic-core.h"
28 #include "real.h"
29 #include "realmpfr.h"
30 #include "tm_p.h"
31 #include "dfp.h"
33 /* The floating point model used internally is not exactly IEEE 754
34 compliant, and close to the description in the ISO C99 standard,
35 section 5.2.4.2.2 Characteristics of floating types.
37 Specifically
39 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
41 where
42 s = sign (+- 1)
43 b = base or radix, here always 2
44 e = exponent
45 p = precision (the number of base-b digits in the significand)
46 f_k = the digits of the significand.
48 We differ from typical IEEE 754 encodings in that the entire
49 significand is fractional. Normalized significands are in the
50 range [0.5, 1.0).
52 A requirement of the model is that P be larger than the largest
53 supported target floating-point type by at least 2 bits. This gives
54 us proper rounding when we truncate to the target type. In addition,
55 E must be large enough to hold the smallest supported denormal number
56 in a normalized form.
58 Both of these requirements are easily satisfied. The largest target
59 significand is 113 bits; we store at least 160. The smallest
60 denormal number fits in 17 exponent bits; we store 26. */
63 /* Used to classify two numbers simultaneously. */
64 #define CLASS2(A, B) ((A) << 2 | (B))
66 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
67 #error "Some constant folding done by hand to avoid shift count warnings"
68 #endif
70 static void get_zero (REAL_VALUE_TYPE *, int);
71 static void get_canonical_qnan (REAL_VALUE_TYPE *, int);
72 static void get_canonical_snan (REAL_VALUE_TYPE *, int);
73 static void get_inf (REAL_VALUE_TYPE *, int);
74 static bool sticky_rshift_significand (REAL_VALUE_TYPE *,
75 const REAL_VALUE_TYPE *, unsigned int);
76 static void rshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
77 unsigned int);
78 static void lshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
79 unsigned int);
80 static void lshift_significand_1 (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
81 static bool add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *,
82 const REAL_VALUE_TYPE *);
83 static bool sub_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
84 const REAL_VALUE_TYPE *, int);
85 static void neg_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
86 static int cmp_significands (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
87 static int cmp_significand_0 (const REAL_VALUE_TYPE *);
88 static void set_significand_bit (REAL_VALUE_TYPE *, unsigned int);
89 static void clear_significand_bit (REAL_VALUE_TYPE *, unsigned int);
90 static bool test_significand_bit (REAL_VALUE_TYPE *, unsigned int);
91 static void clear_significand_below (REAL_VALUE_TYPE *, unsigned int);
92 static bool div_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
93 const REAL_VALUE_TYPE *);
94 static void normalize (REAL_VALUE_TYPE *);
96 static bool do_add (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
97 const REAL_VALUE_TYPE *, int);
98 static bool do_multiply (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
99 const REAL_VALUE_TYPE *);
100 static bool do_divide (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
101 const REAL_VALUE_TYPE *);
102 static int do_compare (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int);
103 static void do_fix_trunc (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
105 static unsigned long rtd_divmod (REAL_VALUE_TYPE *, REAL_VALUE_TYPE *);
106 static void decimal_from_integer (REAL_VALUE_TYPE *);
107 static void decimal_integer_string (char *, const REAL_VALUE_TYPE *,
108 size_t);
110 static const REAL_VALUE_TYPE * ten_to_ptwo (int);
111 static const REAL_VALUE_TYPE * ten_to_mptwo (int);
112 static const REAL_VALUE_TYPE * real_digit (int);
113 static void times_pten (REAL_VALUE_TYPE *, int);
115 static void round_for_format (const struct real_format *, REAL_VALUE_TYPE *);
117 /* Initialize R with a positive zero. */
119 static inline void
120 get_zero (REAL_VALUE_TYPE *r, int sign)
122 memset (r, 0, sizeof (*r));
123 r->sign = sign;
126 /* Initialize R with the canonical quiet NaN. */
128 static inline void
129 get_canonical_qnan (REAL_VALUE_TYPE *r, int sign)
131 memset (r, 0, sizeof (*r));
132 r->cl = rvc_nan;
133 r->sign = sign;
134 r->canonical = 1;
137 static inline void
138 get_canonical_snan (REAL_VALUE_TYPE *r, int sign)
140 memset (r, 0, sizeof (*r));
141 r->cl = rvc_nan;
142 r->sign = sign;
143 r->signalling = 1;
144 r->canonical = 1;
147 static inline void
148 get_inf (REAL_VALUE_TYPE *r, int sign)
150 memset (r, 0, sizeof (*r));
151 r->cl = rvc_inf;
152 r->sign = sign;
156 /* Right-shift the significand of A by N bits; put the result in the
157 significand of R. If any one bits are shifted out, return true. */
159 static bool
160 sticky_rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
161 unsigned int n)
163 unsigned long sticky = 0;
164 unsigned int i, ofs = 0;
166 if (n >= HOST_BITS_PER_LONG)
168 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
169 sticky |= a->sig[i];
170 n &= HOST_BITS_PER_LONG - 1;
173 if (n != 0)
175 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
176 for (i = 0; i < SIGSZ; ++i)
178 r->sig[i]
179 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
180 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
181 << (HOST_BITS_PER_LONG - n)));
184 else
186 for (i = 0; ofs + i < SIGSZ; ++i)
187 r->sig[i] = a->sig[ofs + i];
188 for (; i < SIGSZ; ++i)
189 r->sig[i] = 0;
192 return sticky != 0;
195 /* Right-shift the significand of A by N bits; put the result in the
196 significand of R. */
198 static void
199 rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
200 unsigned int n)
202 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
204 n &= HOST_BITS_PER_LONG - 1;
205 if (n != 0)
207 for (i = 0; i < SIGSZ; ++i)
209 r->sig[i]
210 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
211 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
212 << (HOST_BITS_PER_LONG - n)));
215 else
217 for (i = 0; ofs + i < SIGSZ; ++i)
218 r->sig[i] = a->sig[ofs + i];
219 for (; i < SIGSZ; ++i)
220 r->sig[i] = 0;
224 /* Left-shift the significand of A by N bits; put the result in the
225 significand of R. */
227 static void
228 lshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
229 unsigned int n)
231 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
233 n &= HOST_BITS_PER_LONG - 1;
234 if (n == 0)
236 for (i = 0; ofs + i < SIGSZ; ++i)
237 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
238 for (; i < SIGSZ; ++i)
239 r->sig[SIGSZ-1-i] = 0;
241 else
242 for (i = 0; i < SIGSZ; ++i)
244 r->sig[SIGSZ-1-i]
245 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
246 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
247 >> (HOST_BITS_PER_LONG - n)));
251 /* Likewise, but N is specialized to 1. */
253 static inline void
254 lshift_significand_1 (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
256 unsigned int i;
258 for (i = SIGSZ - 1; i > 0; --i)
259 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
260 r->sig[0] = a->sig[0] << 1;
263 /* Add the significands of A and B, placing the result in R. Return
264 true if there was carry out of the most significant word. */
266 static inline bool
267 add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
268 const REAL_VALUE_TYPE *b)
270 bool carry = false;
271 int i;
273 for (i = 0; i < SIGSZ; ++i)
275 unsigned long ai = a->sig[i];
276 unsigned long ri = ai + b->sig[i];
278 if (carry)
280 carry = ri < ai;
281 carry |= ++ri == 0;
283 else
284 carry = ri < ai;
286 r->sig[i] = ri;
289 return carry;
292 /* Subtract the significands of A and B, placing the result in R. CARRY is
293 true if there's a borrow incoming to the least significant word.
294 Return true if there was borrow out of the most significant word. */
296 static inline bool
297 sub_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
298 const REAL_VALUE_TYPE *b, int carry)
300 int i;
302 for (i = 0; i < SIGSZ; ++i)
304 unsigned long ai = a->sig[i];
305 unsigned long ri = ai - b->sig[i];
307 if (carry)
309 carry = ri > ai;
310 carry |= ~--ri == 0;
312 else
313 carry = ri > ai;
315 r->sig[i] = ri;
318 return carry;
321 /* Negate the significand A, placing the result in R. */
323 static inline void
324 neg_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
326 bool carry = true;
327 int i;
329 for (i = 0; i < SIGSZ; ++i)
331 unsigned long ri, ai = a->sig[i];
333 if (carry)
335 if (ai)
337 ri = -ai;
338 carry = false;
340 else
341 ri = ai;
343 else
344 ri = ~ai;
346 r->sig[i] = ri;
350 /* Compare significands. Return tri-state vs zero. */
352 static inline int
353 cmp_significands (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
355 int i;
357 for (i = SIGSZ - 1; i >= 0; --i)
359 unsigned long ai = a->sig[i];
360 unsigned long bi = b->sig[i];
362 if (ai > bi)
363 return 1;
364 if (ai < bi)
365 return -1;
368 return 0;
371 /* Return true if A is nonzero. */
373 static inline int
374 cmp_significand_0 (const REAL_VALUE_TYPE *a)
376 int i;
378 for (i = SIGSZ - 1; i >= 0; --i)
379 if (a->sig[i])
380 return 1;
382 return 0;
385 /* Set bit N of the significand of R. */
387 static inline void
388 set_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
390 r->sig[n / HOST_BITS_PER_LONG]
391 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
394 /* Clear bit N of the significand of R. */
396 static inline void
397 clear_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
399 r->sig[n / HOST_BITS_PER_LONG]
400 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
403 /* Test bit N of the significand of R. */
405 static inline bool
406 test_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
408 /* ??? Compiler bug here if we return this expression directly.
409 The conversion to bool strips the "&1" and we wind up testing
410 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
411 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
412 return t;
415 /* Clear bits 0..N-1 of the significand of R. */
417 static void
418 clear_significand_below (REAL_VALUE_TYPE *r, unsigned int n)
420 int i, w = n / HOST_BITS_PER_LONG;
422 for (i = 0; i < w; ++i)
423 r->sig[i] = 0;
425 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
428 /* Divide the significands of A and B, placing the result in R. Return
429 true if the division was inexact. */
431 static inline bool
432 div_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
433 const REAL_VALUE_TYPE *b)
435 REAL_VALUE_TYPE u;
436 int i, bit = SIGNIFICAND_BITS - 1;
437 unsigned long msb, inexact;
439 u = *a;
440 memset (r->sig, 0, sizeof (r->sig));
442 msb = 0;
443 goto start;
446 msb = u.sig[SIGSZ-1] & SIG_MSB;
447 lshift_significand_1 (&u, &u);
448 start:
449 if (msb || cmp_significands (&u, b) >= 0)
451 sub_significands (&u, &u, b, 0);
452 set_significand_bit (r, bit);
455 while (--bit >= 0);
457 for (i = 0, inexact = 0; i < SIGSZ; i++)
458 inexact |= u.sig[i];
460 return inexact != 0;
463 /* Adjust the exponent and significand of R such that the most
464 significant bit is set. We underflow to zero and overflow to
465 infinity here, without denormals. (The intermediate representation
466 exponent is large enough to handle target denormals normalized.) */
468 static void
469 normalize (REAL_VALUE_TYPE *r)
471 int shift = 0, exp;
472 int i, j;
474 if (r->decimal)
475 return;
477 /* Find the first word that is nonzero. */
478 for (i = SIGSZ - 1; i >= 0; i--)
479 if (r->sig[i] == 0)
480 shift += HOST_BITS_PER_LONG;
481 else
482 break;
484 /* Zero significand flushes to zero. */
485 if (i < 0)
487 r->cl = rvc_zero;
488 SET_REAL_EXP (r, 0);
489 return;
492 /* Find the first bit that is nonzero. */
493 for (j = 0; ; j++)
494 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
495 break;
496 shift += j;
498 if (shift > 0)
500 exp = REAL_EXP (r) - shift;
501 if (exp > MAX_EXP)
502 get_inf (r, r->sign);
503 else if (exp < -MAX_EXP)
504 get_zero (r, r->sign);
505 else
507 SET_REAL_EXP (r, exp);
508 lshift_significand (r, r, shift);
513 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
514 result may be inexact due to a loss of precision. */
516 static bool
517 do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
518 const REAL_VALUE_TYPE *b, int subtract_p)
520 int dexp, sign, exp;
521 REAL_VALUE_TYPE t;
522 bool inexact = false;
524 /* Determine if we need to add or subtract. */
525 sign = a->sign;
526 subtract_p = (sign ^ b->sign) ^ subtract_p;
528 switch (CLASS2 (a->cl, b->cl))
530 case CLASS2 (rvc_zero, rvc_zero):
531 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
532 get_zero (r, sign & !subtract_p);
533 return false;
535 case CLASS2 (rvc_zero, rvc_normal):
536 case CLASS2 (rvc_zero, rvc_inf):
537 case CLASS2 (rvc_zero, rvc_nan):
538 /* 0 + ANY = ANY. */
539 case CLASS2 (rvc_normal, rvc_nan):
540 case CLASS2 (rvc_inf, rvc_nan):
541 case CLASS2 (rvc_nan, rvc_nan):
542 /* ANY + NaN = NaN. */
543 case CLASS2 (rvc_normal, rvc_inf):
544 /* R + Inf = Inf. */
545 *r = *b;
546 r->sign = sign ^ subtract_p;
547 return false;
549 case CLASS2 (rvc_normal, rvc_zero):
550 case CLASS2 (rvc_inf, rvc_zero):
551 case CLASS2 (rvc_nan, rvc_zero):
552 /* ANY + 0 = ANY. */
553 case CLASS2 (rvc_nan, rvc_normal):
554 case CLASS2 (rvc_nan, rvc_inf):
555 /* NaN + ANY = NaN. */
556 case CLASS2 (rvc_inf, rvc_normal):
557 /* Inf + R = Inf. */
558 *r = *a;
559 return false;
561 case CLASS2 (rvc_inf, rvc_inf):
562 if (subtract_p)
563 /* Inf - Inf = NaN. */
564 get_canonical_qnan (r, 0);
565 else
566 /* Inf + Inf = Inf. */
567 *r = *a;
568 return false;
570 case CLASS2 (rvc_normal, rvc_normal):
571 break;
573 default:
574 gcc_unreachable ();
577 /* Swap the arguments such that A has the larger exponent. */
578 dexp = REAL_EXP (a) - REAL_EXP (b);
579 if (dexp < 0)
581 const REAL_VALUE_TYPE *t;
582 t = a, a = b, b = t;
583 dexp = -dexp;
584 sign ^= subtract_p;
586 exp = REAL_EXP (a);
588 /* If the exponents are not identical, we need to shift the
589 significand of B down. */
590 if (dexp > 0)
592 /* If the exponents are too far apart, the significands
593 do not overlap, which makes the subtraction a noop. */
594 if (dexp >= SIGNIFICAND_BITS)
596 *r = *a;
597 r->sign = sign;
598 return true;
601 inexact |= sticky_rshift_significand (&t, b, dexp);
602 b = &t;
605 if (subtract_p)
607 if (sub_significands (r, a, b, inexact))
609 /* We got a borrow out of the subtraction. That means that
610 A and B had the same exponent, and B had the larger
611 significand. We need to swap the sign and negate the
612 significand. */
613 sign ^= 1;
614 neg_significand (r, r);
617 else
619 if (add_significands (r, a, b))
621 /* We got carry out of the addition. This means we need to
622 shift the significand back down one bit and increase the
623 exponent. */
624 inexact |= sticky_rshift_significand (r, r, 1);
625 r->sig[SIGSZ-1] |= SIG_MSB;
626 if (++exp > MAX_EXP)
628 get_inf (r, sign);
629 return true;
634 r->cl = rvc_normal;
635 r->sign = sign;
636 SET_REAL_EXP (r, exp);
637 /* Zero out the remaining fields. */
638 r->signalling = 0;
639 r->canonical = 0;
640 r->decimal = 0;
642 /* Re-normalize the result. */
643 normalize (r);
645 /* Special case: if the subtraction results in zero, the result
646 is positive. */
647 if (r->cl == rvc_zero)
648 r->sign = 0;
649 else
650 r->sig[0] |= inexact;
652 return inexact;
655 /* Calculate R = A * B. Return true if the result may be inexact. */
657 static bool
658 do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
659 const REAL_VALUE_TYPE *b)
661 REAL_VALUE_TYPE u, t, *rr;
662 unsigned int i, j, k;
663 int sign = a->sign ^ b->sign;
664 bool inexact = false;
666 switch (CLASS2 (a->cl, b->cl))
668 case CLASS2 (rvc_zero, rvc_zero):
669 case CLASS2 (rvc_zero, rvc_normal):
670 case CLASS2 (rvc_normal, rvc_zero):
671 /* +-0 * ANY = 0 with appropriate sign. */
672 get_zero (r, sign);
673 return false;
675 case CLASS2 (rvc_zero, rvc_nan):
676 case CLASS2 (rvc_normal, rvc_nan):
677 case CLASS2 (rvc_inf, rvc_nan):
678 case CLASS2 (rvc_nan, rvc_nan):
679 /* ANY * NaN = NaN. */
680 *r = *b;
681 r->sign = sign;
682 return false;
684 case CLASS2 (rvc_nan, rvc_zero):
685 case CLASS2 (rvc_nan, rvc_normal):
686 case CLASS2 (rvc_nan, rvc_inf):
687 /* NaN * ANY = NaN. */
688 *r = *a;
689 r->sign = sign;
690 return false;
692 case CLASS2 (rvc_zero, rvc_inf):
693 case CLASS2 (rvc_inf, rvc_zero):
694 /* 0 * Inf = NaN */
695 get_canonical_qnan (r, sign);
696 return false;
698 case CLASS2 (rvc_inf, rvc_inf):
699 case CLASS2 (rvc_normal, rvc_inf):
700 case CLASS2 (rvc_inf, rvc_normal):
701 /* Inf * Inf = Inf, R * Inf = Inf */
702 get_inf (r, sign);
703 return false;
705 case CLASS2 (rvc_normal, rvc_normal):
706 break;
708 default:
709 gcc_unreachable ();
712 if (r == a || r == b)
713 rr = &t;
714 else
715 rr = r;
716 get_zero (rr, 0);
718 /* Collect all the partial products. Since we don't have sure access
719 to a widening multiply, we split each long into two half-words.
721 Consider the long-hand form of a four half-word multiplication:
723 A B C D
724 * E F G H
725 --------------
726 DE DF DG DH
727 CE CF CG CH
728 BE BF BG BH
729 AE AF AG AH
731 We construct partial products of the widened half-word products
732 that are known to not overlap, e.g. DF+DH. Each such partial
733 product is given its proper exponent, which allows us to sum them
734 and obtain the finished product. */
736 for (i = 0; i < SIGSZ * 2; ++i)
738 unsigned long ai = a->sig[i / 2];
739 if (i & 1)
740 ai >>= HOST_BITS_PER_LONG / 2;
741 else
742 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
744 if (ai == 0)
745 continue;
747 for (j = 0; j < 2; ++j)
749 int exp = (REAL_EXP (a) - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
750 + (REAL_EXP (b) - (1-j)*(HOST_BITS_PER_LONG/2)));
752 if (exp > MAX_EXP)
754 get_inf (r, sign);
755 return true;
757 if (exp < -MAX_EXP)
759 /* Would underflow to zero, which we shouldn't bother adding. */
760 inexact = true;
761 continue;
764 memset (&u, 0, sizeof (u));
765 u.cl = rvc_normal;
766 SET_REAL_EXP (&u, exp);
768 for (k = j; k < SIGSZ * 2; k += 2)
770 unsigned long bi = b->sig[k / 2];
771 if (k & 1)
772 bi >>= HOST_BITS_PER_LONG / 2;
773 else
774 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
776 u.sig[k / 2] = ai * bi;
779 normalize (&u);
780 inexact |= do_add (rr, rr, &u, 0);
784 rr->sign = sign;
785 if (rr != r)
786 *r = t;
788 return inexact;
791 /* Calculate R = A / B. Return true if the result may be inexact. */
793 static bool
794 do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
795 const REAL_VALUE_TYPE *b)
797 int exp, sign = a->sign ^ b->sign;
798 REAL_VALUE_TYPE t, *rr;
799 bool inexact;
801 switch (CLASS2 (a->cl, b->cl))
803 case CLASS2 (rvc_zero, rvc_zero):
804 /* 0 / 0 = NaN. */
805 case CLASS2 (rvc_inf, rvc_inf):
806 /* Inf / Inf = NaN. */
807 get_canonical_qnan (r, sign);
808 return false;
810 case CLASS2 (rvc_zero, rvc_normal):
811 case CLASS2 (rvc_zero, rvc_inf):
812 /* 0 / ANY = 0. */
813 case CLASS2 (rvc_normal, rvc_inf):
814 /* R / Inf = 0. */
815 get_zero (r, sign);
816 return false;
818 case CLASS2 (rvc_normal, rvc_zero):
819 /* R / 0 = Inf. */
820 case CLASS2 (rvc_inf, rvc_zero):
821 /* Inf / 0 = Inf. */
822 get_inf (r, sign);
823 return false;
825 case CLASS2 (rvc_zero, rvc_nan):
826 case CLASS2 (rvc_normal, rvc_nan):
827 case CLASS2 (rvc_inf, rvc_nan):
828 case CLASS2 (rvc_nan, rvc_nan):
829 /* ANY / NaN = NaN. */
830 *r = *b;
831 r->sign = sign;
832 return false;
834 case CLASS2 (rvc_nan, rvc_zero):
835 case CLASS2 (rvc_nan, rvc_normal):
836 case CLASS2 (rvc_nan, rvc_inf):
837 /* NaN / ANY = NaN. */
838 *r = *a;
839 r->sign = sign;
840 return false;
842 case CLASS2 (rvc_inf, rvc_normal):
843 /* Inf / R = Inf. */
844 get_inf (r, sign);
845 return false;
847 case CLASS2 (rvc_normal, rvc_normal):
848 break;
850 default:
851 gcc_unreachable ();
854 if (r == a || r == b)
855 rr = &t;
856 else
857 rr = r;
859 /* Make sure all fields in the result are initialized. */
860 get_zero (rr, 0);
861 rr->cl = rvc_normal;
862 rr->sign = sign;
864 exp = REAL_EXP (a) - REAL_EXP (b) + 1;
865 if (exp > MAX_EXP)
867 get_inf (r, sign);
868 return true;
870 if (exp < -MAX_EXP)
872 get_zero (r, sign);
873 return true;
875 SET_REAL_EXP (rr, exp);
877 inexact = div_significands (rr, a, b);
879 /* Re-normalize the result. */
880 normalize (rr);
881 rr->sig[0] |= inexact;
883 if (rr != r)
884 *r = t;
886 return inexact;
889 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
890 one of the two operands is a NaN. */
892 static int
893 do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
894 int nan_result)
896 int ret;
898 switch (CLASS2 (a->cl, b->cl))
900 case CLASS2 (rvc_zero, rvc_zero):
901 /* Sign of zero doesn't matter for compares. */
902 return 0;
904 case CLASS2 (rvc_normal, rvc_zero):
905 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
906 if (a->decimal)
907 return decimal_do_compare (a, b, nan_result);
908 /* Fall through. */
909 case CLASS2 (rvc_inf, rvc_zero):
910 case CLASS2 (rvc_inf, rvc_normal):
911 return (a->sign ? -1 : 1);
913 case CLASS2 (rvc_inf, rvc_inf):
914 return -a->sign - -b->sign;
916 case CLASS2 (rvc_zero, rvc_normal):
917 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
918 if (b->decimal)
919 return decimal_do_compare (a, b, nan_result);
920 /* Fall through. */
921 case CLASS2 (rvc_zero, rvc_inf):
922 case CLASS2 (rvc_normal, rvc_inf):
923 return (b->sign ? 1 : -1);
925 case CLASS2 (rvc_zero, rvc_nan):
926 case CLASS2 (rvc_normal, rvc_nan):
927 case CLASS2 (rvc_inf, rvc_nan):
928 case CLASS2 (rvc_nan, rvc_nan):
929 case CLASS2 (rvc_nan, rvc_zero):
930 case CLASS2 (rvc_nan, rvc_normal):
931 case CLASS2 (rvc_nan, rvc_inf):
932 return nan_result;
934 case CLASS2 (rvc_normal, rvc_normal):
935 break;
937 default:
938 gcc_unreachable ();
941 if (a->sign != b->sign)
942 return -a->sign - -b->sign;
944 if (a->decimal || b->decimal)
945 return decimal_do_compare (a, b, nan_result);
947 if (REAL_EXP (a) > REAL_EXP (b))
948 ret = 1;
949 else if (REAL_EXP (a) < REAL_EXP (b))
950 ret = -1;
951 else
952 ret = cmp_significands (a, b);
954 return (a->sign ? -ret : ret);
957 /* Return A truncated to an integral value toward zero. */
959 static void
960 do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
962 *r = *a;
964 switch (r->cl)
966 case rvc_zero:
967 case rvc_inf:
968 case rvc_nan:
969 break;
971 case rvc_normal:
972 if (r->decimal)
974 decimal_do_fix_trunc (r, a);
975 return;
977 if (REAL_EXP (r) <= 0)
978 get_zero (r, r->sign);
979 else if (REAL_EXP (r) < SIGNIFICAND_BITS)
980 clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r));
981 break;
983 default:
984 gcc_unreachable ();
988 /* Perform the binary or unary operation described by CODE.
989 For a unary operation, leave OP1 NULL. This function returns
990 true if the result may be inexact due to loss of precision. */
992 bool
993 real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0,
994 const REAL_VALUE_TYPE *op1)
996 enum tree_code code = (enum tree_code) icode;
998 if (op0->decimal || (op1 && op1->decimal))
999 return decimal_real_arithmetic (r, code, op0, op1);
1001 switch (code)
1003 case PLUS_EXPR:
1004 /* Clear any padding areas in *r if it isn't equal to one of the
1005 operands so that we can later do bitwise comparisons later on. */
1006 if (r != op0 && r != op1)
1007 memset (r, '\0', sizeof (*r));
1008 return do_add (r, op0, op1, 0);
1010 case MINUS_EXPR:
1011 if (r != op0 && r != op1)
1012 memset (r, '\0', sizeof (*r));
1013 return do_add (r, op0, op1, 1);
1015 case MULT_EXPR:
1016 if (r != op0 && r != op1)
1017 memset (r, '\0', sizeof (*r));
1018 return do_multiply (r, op0, op1);
1020 case RDIV_EXPR:
1021 if (r != op0 && r != op1)
1022 memset (r, '\0', sizeof (*r));
1023 return do_divide (r, op0, op1);
1025 case MIN_EXPR:
1026 if (op1->cl == rvc_nan)
1027 *r = *op1;
1028 else if (do_compare (op0, op1, -1) < 0)
1029 *r = *op0;
1030 else
1031 *r = *op1;
1032 break;
1034 case MAX_EXPR:
1035 if (op1->cl == rvc_nan)
1036 *r = *op1;
1037 else if (do_compare (op0, op1, 1) < 0)
1038 *r = *op1;
1039 else
1040 *r = *op0;
1041 break;
1043 case NEGATE_EXPR:
1044 *r = *op0;
1045 r->sign ^= 1;
1046 break;
1048 case ABS_EXPR:
1049 *r = *op0;
1050 r->sign = 0;
1051 break;
1053 case FIX_TRUNC_EXPR:
1054 do_fix_trunc (r, op0);
1055 break;
1057 default:
1058 gcc_unreachable ();
1060 return false;
1063 REAL_VALUE_TYPE
1064 real_value_negate (const REAL_VALUE_TYPE *op0)
1066 REAL_VALUE_TYPE r;
1067 real_arithmetic (&r, NEGATE_EXPR, op0, NULL);
1068 return r;
1071 REAL_VALUE_TYPE
1072 real_value_abs (const REAL_VALUE_TYPE *op0)
1074 REAL_VALUE_TYPE r;
1075 real_arithmetic (&r, ABS_EXPR, op0, NULL);
1076 return r;
1079 bool
1080 real_compare (int icode, const REAL_VALUE_TYPE *op0,
1081 const REAL_VALUE_TYPE *op1)
1083 enum tree_code code = (enum tree_code) icode;
1085 switch (code)
1087 case LT_EXPR:
1088 return do_compare (op0, op1, 1) < 0;
1089 case LE_EXPR:
1090 return do_compare (op0, op1, 1) <= 0;
1091 case GT_EXPR:
1092 return do_compare (op0, op1, -1) > 0;
1093 case GE_EXPR:
1094 return do_compare (op0, op1, -1) >= 0;
1095 case EQ_EXPR:
1096 return do_compare (op0, op1, -1) == 0;
1097 case NE_EXPR:
1098 return do_compare (op0, op1, -1) != 0;
1099 case UNORDERED_EXPR:
1100 return op0->cl == rvc_nan || op1->cl == rvc_nan;
1101 case ORDERED_EXPR:
1102 return op0->cl != rvc_nan && op1->cl != rvc_nan;
1103 case UNLT_EXPR:
1104 return do_compare (op0, op1, -1) < 0;
1105 case UNLE_EXPR:
1106 return do_compare (op0, op1, -1) <= 0;
1107 case UNGT_EXPR:
1108 return do_compare (op0, op1, 1) > 0;
1109 case UNGE_EXPR:
1110 return do_compare (op0, op1, 1) >= 0;
1111 case UNEQ_EXPR:
1112 return do_compare (op0, op1, 0) == 0;
1113 case LTGT_EXPR:
1114 return do_compare (op0, op1, 0) != 0;
1116 default:
1117 gcc_unreachable ();
1121 /* Return floor log2(R). */
1124 real_exponent (const REAL_VALUE_TYPE *r)
1126 switch (r->cl)
1128 case rvc_zero:
1129 return 0;
1130 case rvc_inf:
1131 case rvc_nan:
1132 return (unsigned int)-1 >> 1;
1133 case rvc_normal:
1134 return REAL_EXP (r);
1135 default:
1136 gcc_unreachable ();
1140 /* R = OP0 * 2**EXP. */
1142 void
1143 real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1145 *r = *op0;
1146 switch (r->cl)
1148 case rvc_zero:
1149 case rvc_inf:
1150 case rvc_nan:
1151 break;
1153 case rvc_normal:
1154 exp += REAL_EXP (op0);
1155 if (exp > MAX_EXP)
1156 get_inf (r, r->sign);
1157 else if (exp < -MAX_EXP)
1158 get_zero (r, r->sign);
1159 else
1160 SET_REAL_EXP (r, exp);
1161 break;
1163 default:
1164 gcc_unreachable ();
1168 /* Determine whether a floating-point value X is infinite. */
1170 bool
1171 real_isinf (const REAL_VALUE_TYPE *r)
1173 return (r->cl == rvc_inf);
1176 /* Determine whether a floating-point value X is a NaN. */
1178 bool
1179 real_isnan (const REAL_VALUE_TYPE *r)
1181 return (r->cl == rvc_nan);
1184 /* Determine whether a floating-point value X is finite. */
1186 bool
1187 real_isfinite (const REAL_VALUE_TYPE *r)
1189 return (r->cl != rvc_nan) && (r->cl != rvc_inf);
1192 /* Determine whether a floating-point value X is negative. */
1194 bool
1195 real_isneg (const REAL_VALUE_TYPE *r)
1197 return r->sign;
1200 /* Determine whether a floating-point value X is minus zero. */
1202 bool
1203 real_isnegzero (const REAL_VALUE_TYPE *r)
1205 return r->sign && r->cl == rvc_zero;
1208 /* Compare two floating-point objects for bitwise identity. */
1210 bool
1211 real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1213 int i;
1215 if (a->cl != b->cl)
1216 return false;
1217 if (a->sign != b->sign)
1218 return false;
1220 switch (a->cl)
1222 case rvc_zero:
1223 case rvc_inf:
1224 return true;
1226 case rvc_normal:
1227 if (a->decimal != b->decimal)
1228 return false;
1229 if (REAL_EXP (a) != REAL_EXP (b))
1230 return false;
1231 break;
1233 case rvc_nan:
1234 if (a->signalling != b->signalling)
1235 return false;
1236 /* The significand is ignored for canonical NaNs. */
1237 if (a->canonical || b->canonical)
1238 return a->canonical == b->canonical;
1239 break;
1241 default:
1242 gcc_unreachable ();
1245 for (i = 0; i < SIGSZ; ++i)
1246 if (a->sig[i] != b->sig[i])
1247 return false;
1249 return true;
1252 /* Try to change R into its exact multiplicative inverse in machine
1253 mode MODE. Return true if successful. */
1255 bool
1256 exact_real_inverse (enum machine_mode mode, REAL_VALUE_TYPE *r)
1258 const REAL_VALUE_TYPE *one = real_digit (1);
1259 REAL_VALUE_TYPE u;
1260 int i;
1262 if (r->cl != rvc_normal)
1263 return false;
1265 /* Check for a power of two: all significand bits zero except the MSB. */
1266 for (i = 0; i < SIGSZ-1; ++i)
1267 if (r->sig[i] != 0)
1268 return false;
1269 if (r->sig[SIGSZ-1] != SIG_MSB)
1270 return false;
1272 /* Find the inverse and truncate to the required mode. */
1273 do_divide (&u, one, r);
1274 real_convert (&u, mode, &u);
1276 /* The rounding may have overflowed. */
1277 if (u.cl != rvc_normal)
1278 return false;
1279 for (i = 0; i < SIGSZ-1; ++i)
1280 if (u.sig[i] != 0)
1281 return false;
1282 if (u.sig[SIGSZ-1] != SIG_MSB)
1283 return false;
1285 *r = u;
1286 return true;
1289 /* Return true if arithmetic on values in IMODE that were promoted
1290 from values in TMODE is equivalent to direct arithmetic on values
1291 in TMODE. */
1293 bool
1294 real_can_shorten_arithmetic (enum machine_mode imode, enum machine_mode tmode)
1296 const struct real_format *tfmt, *ifmt;
1297 tfmt = REAL_MODE_FORMAT (tmode);
1298 ifmt = REAL_MODE_FORMAT (imode);
1299 /* These conditions are conservative rather than trying to catch the
1300 exact boundary conditions; the main case to allow is IEEE float
1301 and double. */
1302 return (ifmt->b == tfmt->b
1303 && ifmt->p > 2 * tfmt->p
1304 && ifmt->emin < 2 * tfmt->emin - tfmt->p - 2
1305 && ifmt->emin < tfmt->emin - tfmt->emax - tfmt->p - 2
1306 && ifmt->emax > 2 * tfmt->emax + 2
1307 && ifmt->emax > tfmt->emax - tfmt->emin + tfmt->p + 2
1308 && ifmt->round_towards_zero == tfmt->round_towards_zero
1309 && (ifmt->has_sign_dependent_rounding
1310 == tfmt->has_sign_dependent_rounding)
1311 && ifmt->has_nans >= tfmt->has_nans
1312 && ifmt->has_inf >= tfmt->has_inf
1313 && ifmt->has_signed_zero >= tfmt->has_signed_zero
1314 && !MODE_COMPOSITE_P (tmode)
1315 && !MODE_COMPOSITE_P (imode));
1318 /* Render R as an integer. */
1320 HOST_WIDE_INT
1321 real_to_integer (const REAL_VALUE_TYPE *r)
1323 unsigned HOST_WIDE_INT i;
1325 switch (r->cl)
1327 case rvc_zero:
1328 underflow:
1329 return 0;
1331 case rvc_inf:
1332 case rvc_nan:
1333 overflow:
1334 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1335 if (!r->sign)
1336 i--;
1337 return i;
1339 case rvc_normal:
1340 if (r->decimal)
1341 return decimal_real_to_integer (r);
1343 if (REAL_EXP (r) <= 0)
1344 goto underflow;
1345 /* Only force overflow for unsigned overflow. Signed overflow is
1346 undefined, so it doesn't matter what we return, and some callers
1347 expect to be able to use this routine for both signed and
1348 unsigned conversions. */
1349 if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1350 goto overflow;
1352 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1353 i = r->sig[SIGSZ-1];
1354 else
1356 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1357 i = r->sig[SIGSZ-1];
1358 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1359 i |= r->sig[SIGSZ-2];
1362 i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1364 if (r->sign)
1365 i = -i;
1366 return i;
1368 default:
1369 gcc_unreachable ();
1373 /* Likewise, but to an integer pair, HI+LOW. */
1375 void
1376 real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
1377 const REAL_VALUE_TYPE *r)
1379 REAL_VALUE_TYPE t;
1380 unsigned HOST_WIDE_INT low;
1381 HOST_WIDE_INT high;
1382 int exp;
1384 switch (r->cl)
1386 case rvc_zero:
1387 underflow:
1388 low = high = 0;
1389 break;
1391 case rvc_inf:
1392 case rvc_nan:
1393 overflow:
1394 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1395 if (r->sign)
1396 low = 0;
1397 else
1399 high--;
1400 low = -1;
1402 break;
1404 case rvc_normal:
1405 if (r->decimal)
1407 decimal_real_to_integer2 (plow, phigh, r);
1408 return;
1411 exp = REAL_EXP (r);
1412 if (exp <= 0)
1413 goto underflow;
1414 /* Only force overflow for unsigned overflow. Signed overflow is
1415 undefined, so it doesn't matter what we return, and some callers
1416 expect to be able to use this routine for both signed and
1417 unsigned conversions. */
1418 if (exp > HOST_BITS_PER_DOUBLE_INT)
1419 goto overflow;
1421 rshift_significand (&t, r, HOST_BITS_PER_DOUBLE_INT - exp);
1422 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1424 high = t.sig[SIGSZ-1];
1425 low = t.sig[SIGSZ-2];
1427 else
1429 gcc_assert (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG);
1430 high = t.sig[SIGSZ-1];
1431 high = high << (HOST_BITS_PER_LONG - 1) << 1;
1432 high |= t.sig[SIGSZ-2];
1434 low = t.sig[SIGSZ-3];
1435 low = low << (HOST_BITS_PER_LONG - 1) << 1;
1436 low |= t.sig[SIGSZ-4];
1439 if (r->sign)
1441 if (low == 0)
1442 high = -high;
1443 else
1444 low = -low, high = ~high;
1446 break;
1448 default:
1449 gcc_unreachable ();
1452 *plow = low;
1453 *phigh = high;
1456 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1457 of NUM / DEN. Return the quotient and place the remainder in NUM.
1458 It is expected that NUM / DEN are close enough that the quotient is
1459 small. */
1461 static unsigned long
1462 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1464 unsigned long q, msb;
1465 int expn = REAL_EXP (num), expd = REAL_EXP (den);
1467 if (expn < expd)
1468 return 0;
1470 q = msb = 0;
1471 goto start;
1474 msb = num->sig[SIGSZ-1] & SIG_MSB;
1475 q <<= 1;
1476 lshift_significand_1 (num, num);
1477 start:
1478 if (msb || cmp_significands (num, den) >= 0)
1480 sub_significands (num, num, den, 0);
1481 q |= 1;
1484 while (--expn >= expd);
1486 SET_REAL_EXP (num, expd);
1487 normalize (num);
1489 return q;
1492 /* Render R as a decimal floating point constant. Emit DIGITS significant
1493 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1494 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1495 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1496 to a string that, when parsed back in mode MODE, yields the same value. */
1498 #define M_LOG10_2 0.30102999566398119521
1500 void
1501 real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig,
1502 size_t buf_size, size_t digits,
1503 int crop_trailing_zeros, enum machine_mode mode)
1505 const struct real_format *fmt = NULL;
1506 const REAL_VALUE_TYPE *one, *ten;
1507 REAL_VALUE_TYPE r, pten, u, v;
1508 int dec_exp, cmp_one, digit;
1509 size_t max_digits;
1510 char *p, *first, *last;
1511 bool sign;
1512 bool round_up;
1514 if (mode != VOIDmode)
1516 fmt = REAL_MODE_FORMAT (mode);
1517 gcc_assert (fmt);
1520 r = *r_orig;
1521 switch (r.cl)
1523 case rvc_zero:
1524 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1525 return;
1526 case rvc_normal:
1527 break;
1528 case rvc_inf:
1529 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1530 return;
1531 case rvc_nan:
1532 /* ??? Print the significand as well, if not canonical? */
1533 sprintf (str, "%c%cNaN", (r_orig->sign ? '-' : '+'),
1534 (r_orig->signalling ? 'S' : 'Q'));
1535 return;
1536 default:
1537 gcc_unreachable ();
1540 if (r.decimal)
1542 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1543 return;
1546 /* Bound the number of digits printed by the size of the representation. */
1547 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1548 if (digits == 0 || digits > max_digits)
1549 digits = max_digits;
1551 /* Estimate the decimal exponent, and compute the length of the string it
1552 will print as. Be conservative and add one to account for possible
1553 overflow or rounding error. */
1554 dec_exp = REAL_EXP (&r) * M_LOG10_2;
1555 for (max_digits = 1; dec_exp ; max_digits++)
1556 dec_exp /= 10;
1558 /* Bound the number of digits printed by the size of the output buffer. */
1559 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1560 gcc_assert (max_digits <= buf_size);
1561 if (digits > max_digits)
1562 digits = max_digits;
1564 one = real_digit (1);
1565 ten = ten_to_ptwo (0);
1567 sign = r.sign;
1568 r.sign = 0;
1570 dec_exp = 0;
1571 pten = *one;
1573 cmp_one = do_compare (&r, one, 0);
1574 if (cmp_one > 0)
1576 int m;
1578 /* Number is greater than one. Convert significand to an integer
1579 and strip trailing decimal zeros. */
1581 u = r;
1582 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1584 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1585 m = floor_log2 (max_digits);
1587 /* Iterate over the bits of the possible powers of 10 that might
1588 be present in U and eliminate them. That is, if we find that
1589 10**2**M divides U evenly, keep the division and increase
1590 DEC_EXP by 2**M. */
1593 REAL_VALUE_TYPE t;
1595 do_divide (&t, &u, ten_to_ptwo (m));
1596 do_fix_trunc (&v, &t);
1597 if (cmp_significands (&v, &t) == 0)
1599 u = t;
1600 dec_exp += 1 << m;
1603 while (--m >= 0);
1605 /* Revert the scaling to integer that we performed earlier. */
1606 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1607 - (SIGNIFICAND_BITS - 1));
1608 r = u;
1610 /* Find power of 10. Do this by dividing out 10**2**M when
1611 this is larger than the current remainder. Fill PTEN with
1612 the power of 10 that we compute. */
1613 if (REAL_EXP (&r) > 0)
1615 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1618 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1619 if (do_compare (&u, ptentwo, 0) >= 0)
1621 do_divide (&u, &u, ptentwo);
1622 do_multiply (&pten, &pten, ptentwo);
1623 dec_exp += 1 << m;
1626 while (--m >= 0);
1628 else
1629 /* We managed to divide off enough tens in the above reduction
1630 loop that we've now got a negative exponent. Fall into the
1631 less-than-one code to compute the proper value for PTEN. */
1632 cmp_one = -1;
1634 if (cmp_one < 0)
1636 int m;
1638 /* Number is less than one. Pad significand with leading
1639 decimal zeros. */
1641 v = r;
1642 while (1)
1644 /* Stop if we'd shift bits off the bottom. */
1645 if (v.sig[0] & 7)
1646 break;
1648 do_multiply (&u, &v, ten);
1650 /* Stop if we're now >= 1. */
1651 if (REAL_EXP (&u) > 0)
1652 break;
1654 v = u;
1655 dec_exp -= 1;
1657 r = v;
1659 /* Find power of 10. Do this by multiplying in P=10**2**M when
1660 the current remainder is smaller than 1/P. Fill PTEN with the
1661 power of 10 that we compute. */
1662 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1665 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1666 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1668 if (do_compare (&v, ptenmtwo, 0) <= 0)
1670 do_multiply (&v, &v, ptentwo);
1671 do_multiply (&pten, &pten, ptentwo);
1672 dec_exp -= 1 << m;
1675 while (--m >= 0);
1677 /* Invert the positive power of 10 that we've collected so far. */
1678 do_divide (&pten, one, &pten);
1681 p = str;
1682 if (sign)
1683 *p++ = '-';
1684 first = p++;
1686 /* At this point, PTEN should contain the nearest power of 10 smaller
1687 than R, such that this division produces the first digit.
1689 Using a divide-step primitive that returns the complete integral
1690 remainder avoids the rounding error that would be produced if
1691 we were to use do_divide here and then simply multiply by 10 for
1692 each subsequent digit. */
1694 digit = rtd_divmod (&r, &pten);
1696 /* Be prepared for error in that division via underflow ... */
1697 if (digit == 0 && cmp_significand_0 (&r))
1699 /* Multiply by 10 and try again. */
1700 do_multiply (&r, &r, ten);
1701 digit = rtd_divmod (&r, &pten);
1702 dec_exp -= 1;
1703 gcc_assert (digit != 0);
1706 /* ... or overflow. */
1707 if (digit == 10)
1709 *p++ = '1';
1710 if (--digits > 0)
1711 *p++ = '0';
1712 dec_exp += 1;
1714 else
1716 gcc_assert (digit <= 10);
1717 *p++ = digit + '0';
1720 /* Generate subsequent digits. */
1721 while (--digits > 0)
1723 do_multiply (&r, &r, ten);
1724 digit = rtd_divmod (&r, &pten);
1725 *p++ = digit + '0';
1727 last = p;
1729 /* Generate one more digit with which to do rounding. */
1730 do_multiply (&r, &r, ten);
1731 digit = rtd_divmod (&r, &pten);
1733 /* Round the result. */
1734 if (fmt && fmt->round_towards_zero)
1736 /* If the format uses round towards zero when parsing the string
1737 back in, we need to always round away from zero here. */
1738 if (cmp_significand_0 (&r))
1739 digit++;
1740 round_up = digit > 0;
1742 else
1744 if (digit == 5)
1746 /* Round to nearest. If R is nonzero there are additional
1747 nonzero digits to be extracted. */
1748 if (cmp_significand_0 (&r))
1749 digit++;
1750 /* Round to even. */
1751 else if ((p[-1] - '0') & 1)
1752 digit++;
1755 round_up = digit > 5;
1758 if (round_up)
1760 while (p > first)
1762 digit = *--p;
1763 if (digit == '9')
1764 *p = '0';
1765 else
1767 *p = digit + 1;
1768 break;
1772 /* Carry out of the first digit. This means we had all 9's and
1773 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1774 if (p == first)
1776 first[1] = '1';
1777 dec_exp++;
1781 /* Insert the decimal point. */
1782 first[0] = first[1];
1783 first[1] = '.';
1785 /* If requested, drop trailing zeros. Never crop past "1.0". */
1786 if (crop_trailing_zeros)
1787 while (last > first + 3 && last[-1] == '0')
1788 last--;
1790 /* Append the exponent. */
1791 sprintf (last, "e%+d", dec_exp);
1793 #ifdef ENABLE_CHECKING
1794 /* Verify that we can read the original value back in. */
1795 if (mode != VOIDmode)
1797 real_from_string (&r, str);
1798 real_convert (&r, mode, &r);
1799 gcc_assert (real_identical (&r, r_orig));
1801 #endif
1804 /* Likewise, except always uses round-to-nearest. */
1806 void
1807 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1808 size_t digits, int crop_trailing_zeros)
1810 real_to_decimal_for_mode (str, r_orig, buf_size,
1811 digits, crop_trailing_zeros, VOIDmode);
1814 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1815 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1816 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1817 strip trailing zeros. */
1819 void
1820 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1821 size_t digits, int crop_trailing_zeros)
1823 int i, j, exp = REAL_EXP (r);
1824 char *p, *first;
1825 char exp_buf[16];
1826 size_t max_digits;
1828 switch (r->cl)
1830 case rvc_zero:
1831 exp = 0;
1832 break;
1833 case rvc_normal:
1834 break;
1835 case rvc_inf:
1836 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1837 return;
1838 case rvc_nan:
1839 /* ??? Print the significand as well, if not canonical? */
1840 sprintf (str, "%c%cNaN", (r->sign ? '-' : '+'),
1841 (r->signalling ? 'S' : 'Q'));
1842 return;
1843 default:
1844 gcc_unreachable ();
1847 if (r->decimal)
1849 /* Hexadecimal format for decimal floats is not interesting. */
1850 strcpy (str, "N/A");
1851 return;
1854 if (digits == 0)
1855 digits = SIGNIFICAND_BITS / 4;
1857 /* Bound the number of digits printed by the size of the output buffer. */
1859 sprintf (exp_buf, "p%+d", exp);
1860 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1861 gcc_assert (max_digits <= buf_size);
1862 if (digits > max_digits)
1863 digits = max_digits;
1865 p = str;
1866 if (r->sign)
1867 *p++ = '-';
1868 *p++ = '0';
1869 *p++ = 'x';
1870 *p++ = '0';
1871 *p++ = '.';
1872 first = p;
1874 for (i = SIGSZ - 1; i >= 0; --i)
1875 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1877 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1878 if (--digits == 0)
1879 goto out;
1882 out:
1883 if (crop_trailing_zeros)
1884 while (p > first + 1 && p[-1] == '0')
1885 p--;
1887 sprintf (p, "p%+d", exp);
1890 /* Initialize R from a decimal or hexadecimal string. The string is
1891 assumed to have been syntax checked already. Return -1 if the
1892 value underflows, +1 if overflows, and 0 otherwise. */
1895 real_from_string (REAL_VALUE_TYPE *r, const char *str)
1897 int exp = 0;
1898 bool sign = false;
1900 get_zero (r, 0);
1902 if (*str == '-')
1904 sign = true;
1905 str++;
1907 else if (*str == '+')
1908 str++;
1910 if (!strncmp (str, "QNaN", 4))
1912 get_canonical_qnan (r, sign);
1913 return 0;
1915 else if (!strncmp (str, "SNaN", 4))
1917 get_canonical_snan (r, sign);
1918 return 0;
1920 else if (!strncmp (str, "Inf", 3))
1922 get_inf (r, sign);
1923 return 0;
1926 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1928 /* Hexadecimal floating point. */
1929 int pos = SIGNIFICAND_BITS - 4, d;
1931 str += 2;
1933 while (*str == '0')
1934 str++;
1935 while (1)
1937 d = hex_value (*str);
1938 if (d == _hex_bad)
1939 break;
1940 if (pos >= 0)
1942 r->sig[pos / HOST_BITS_PER_LONG]
1943 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1944 pos -= 4;
1946 else if (d)
1947 /* Ensure correct rounding by setting last bit if there is
1948 a subsequent nonzero digit. */
1949 r->sig[0] |= 1;
1950 exp += 4;
1951 str++;
1953 if (*str == '.')
1955 str++;
1956 if (pos == SIGNIFICAND_BITS - 4)
1958 while (*str == '0')
1959 str++, exp -= 4;
1961 while (1)
1963 d = hex_value (*str);
1964 if (d == _hex_bad)
1965 break;
1966 if (pos >= 0)
1968 r->sig[pos / HOST_BITS_PER_LONG]
1969 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1970 pos -= 4;
1972 else if (d)
1973 /* Ensure correct rounding by setting last bit if there is
1974 a subsequent nonzero digit. */
1975 r->sig[0] |= 1;
1976 str++;
1980 /* If the mantissa is zero, ignore the exponent. */
1981 if (!cmp_significand_0 (r))
1982 goto is_a_zero;
1984 if (*str == 'p' || *str == 'P')
1986 bool exp_neg = false;
1988 str++;
1989 if (*str == '-')
1991 exp_neg = true;
1992 str++;
1994 else if (*str == '+')
1995 str++;
1997 d = 0;
1998 while (ISDIGIT (*str))
2000 d *= 10;
2001 d += *str - '0';
2002 if (d > MAX_EXP)
2004 /* Overflowed the exponent. */
2005 if (exp_neg)
2006 goto underflow;
2007 else
2008 goto overflow;
2010 str++;
2012 if (exp_neg)
2013 d = -d;
2015 exp += d;
2018 r->cl = rvc_normal;
2019 SET_REAL_EXP (r, exp);
2021 normalize (r);
2023 else
2025 /* Decimal floating point. */
2026 const char *cstr = str;
2027 mpfr_t m;
2028 bool inexact;
2030 while (*cstr == '0')
2031 cstr++;
2032 if (*cstr == '.')
2034 cstr++;
2035 while (*cstr == '0')
2036 cstr++;
2039 /* If the mantissa is zero, ignore the exponent. */
2040 if (!ISDIGIT (*cstr))
2041 goto is_a_zero;
2043 /* Nonzero value, possibly overflowing or underflowing. */
2044 mpfr_init2 (m, SIGNIFICAND_BITS);
2045 inexact = mpfr_strtofr (m, str, NULL, 10, GMP_RNDZ);
2046 /* The result should never be a NaN, and because the rounding is
2047 toward zero should never be an infinity. */
2048 gcc_assert (!mpfr_nan_p (m) && !mpfr_inf_p (m));
2049 if (mpfr_zero_p (m) || mpfr_get_exp (m) < -MAX_EXP + 4)
2051 mpfr_clear (m);
2052 goto underflow;
2054 else if (mpfr_get_exp (m) > MAX_EXP - 4)
2056 mpfr_clear (m);
2057 goto overflow;
2059 else
2061 real_from_mpfr (r, m, NULL_TREE, GMP_RNDZ);
2062 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2063 because the hex digits used in real_from_mpfr did not
2064 start with a digit 8 to f, but the exponent bounds above
2065 should have avoided underflow or overflow. */
2066 gcc_assert (r->cl = rvc_normal);
2067 /* Set a sticky bit if mpfr_strtofr was inexact. */
2068 r->sig[0] |= inexact;
2072 r->sign = sign;
2073 return 0;
2075 is_a_zero:
2076 get_zero (r, sign);
2077 return 0;
2079 underflow:
2080 get_zero (r, sign);
2081 return -1;
2083 overflow:
2084 get_inf (r, sign);
2085 return 1;
2088 /* Legacy. Similar, but return the result directly. */
2090 REAL_VALUE_TYPE
2091 real_from_string2 (const char *s, enum machine_mode mode)
2093 REAL_VALUE_TYPE r;
2095 real_from_string (&r, s);
2096 if (mode != VOIDmode)
2097 real_convert (&r, mode, &r);
2099 return r;
2102 /* Initialize R from string S and desired MODE. */
2104 void
2105 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, enum machine_mode mode)
2107 if (DECIMAL_FLOAT_MODE_P (mode))
2108 decimal_real_from_string (r, s);
2109 else
2110 real_from_string (r, s);
2112 if (mode != VOIDmode)
2113 real_convert (r, mode, r);
2116 /* Initialize R from the integer pair HIGH+LOW. */
2118 void
2119 real_from_integer (REAL_VALUE_TYPE *r, enum machine_mode mode,
2120 unsigned HOST_WIDE_INT low, HOST_WIDE_INT high,
2121 int unsigned_p)
2123 if (low == 0 && high == 0)
2124 get_zero (r, 0);
2125 else
2127 memset (r, 0, sizeof (*r));
2128 r->cl = rvc_normal;
2129 r->sign = high < 0 && !unsigned_p;
2130 SET_REAL_EXP (r, HOST_BITS_PER_DOUBLE_INT);
2132 if (r->sign)
2134 high = ~high;
2135 if (low == 0)
2136 high += 1;
2137 else
2138 low = -low;
2141 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2143 r->sig[SIGSZ-1] = high;
2144 r->sig[SIGSZ-2] = low;
2146 else
2148 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2149 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
2150 r->sig[SIGSZ-2] = high;
2151 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
2152 r->sig[SIGSZ-4] = low;
2155 normalize (r);
2158 if (DECIMAL_FLOAT_MODE_P (mode))
2159 decimal_from_integer (r);
2160 else if (mode != VOIDmode)
2161 real_convert (r, mode, r);
2164 /* Render R, an integral value, as a floating point constant with no
2165 specified exponent. */
2167 static void
2168 decimal_integer_string (char *str, const REAL_VALUE_TYPE *r_orig,
2169 size_t buf_size)
2171 int dec_exp, digit, digits;
2172 REAL_VALUE_TYPE r, pten;
2173 char *p;
2174 bool sign;
2176 r = *r_orig;
2178 if (r.cl == rvc_zero)
2180 strcpy (str, "0.");
2181 return;
2184 sign = r.sign;
2185 r.sign = 0;
2187 dec_exp = REAL_EXP (&r) * M_LOG10_2;
2188 digits = dec_exp + 1;
2189 gcc_assert ((digits + 2) < (int)buf_size);
2191 pten = *real_digit (1);
2192 times_pten (&pten, dec_exp);
2194 p = str;
2195 if (sign)
2196 *p++ = '-';
2198 digit = rtd_divmod (&r, &pten);
2199 gcc_assert (digit >= 0 && digit <= 9);
2200 *p++ = digit + '0';
2201 while (--digits > 0)
2203 times_pten (&r, 1);
2204 digit = rtd_divmod (&r, &pten);
2205 *p++ = digit + '0';
2207 *p++ = '.';
2208 *p++ = '\0';
2211 /* Convert a real with an integral value to decimal float. */
2213 static void
2214 decimal_from_integer (REAL_VALUE_TYPE *r)
2216 char str[256];
2218 decimal_integer_string (str, r, sizeof (str) - 1);
2219 decimal_real_from_string (r, str);
2222 /* Returns 10**2**N. */
2224 static const REAL_VALUE_TYPE *
2225 ten_to_ptwo (int n)
2227 static REAL_VALUE_TYPE tens[EXP_BITS];
2229 gcc_assert (n >= 0);
2230 gcc_assert (n < EXP_BITS);
2232 if (tens[n].cl == rvc_zero)
2234 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2236 HOST_WIDE_INT t = 10;
2237 int i;
2239 for (i = 0; i < n; ++i)
2240 t *= t;
2242 real_from_integer (&tens[n], VOIDmode, t, 0, 1);
2244 else
2246 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2247 do_multiply (&tens[n], t, t);
2251 return &tens[n];
2254 /* Returns 10**(-2**N). */
2256 static const REAL_VALUE_TYPE *
2257 ten_to_mptwo (int n)
2259 static REAL_VALUE_TYPE tens[EXP_BITS];
2261 gcc_assert (n >= 0);
2262 gcc_assert (n < EXP_BITS);
2264 if (tens[n].cl == rvc_zero)
2265 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2267 return &tens[n];
2270 /* Returns N. */
2272 static const REAL_VALUE_TYPE *
2273 real_digit (int n)
2275 static REAL_VALUE_TYPE num[10];
2277 gcc_assert (n >= 0);
2278 gcc_assert (n <= 9);
2280 if (n > 0 && num[n].cl == rvc_zero)
2281 real_from_integer (&num[n], VOIDmode, n, 0, 1);
2283 return &num[n];
2286 /* Multiply R by 10**EXP. */
2288 static void
2289 times_pten (REAL_VALUE_TYPE *r, int exp)
2291 REAL_VALUE_TYPE pten, *rr;
2292 bool negative = (exp < 0);
2293 int i;
2295 if (negative)
2297 exp = -exp;
2298 pten = *real_digit (1);
2299 rr = &pten;
2301 else
2302 rr = r;
2304 for (i = 0; exp > 0; ++i, exp >>= 1)
2305 if (exp & 1)
2306 do_multiply (rr, rr, ten_to_ptwo (i));
2308 if (negative)
2309 do_divide (r, r, &pten);
2312 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2314 const REAL_VALUE_TYPE *
2315 dconst_e_ptr (void)
2317 static REAL_VALUE_TYPE value;
2319 /* Initialize mathematical constants for constant folding builtins.
2320 These constants need to be given to at least 160 bits precision. */
2321 if (value.cl == rvc_zero)
2323 mpfr_t m;
2324 mpfr_init2 (m, SIGNIFICAND_BITS);
2325 mpfr_set_ui (m, 1, GMP_RNDN);
2326 mpfr_exp (m, m, GMP_RNDN);
2327 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2328 mpfr_clear (m);
2331 return &value;
2334 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2336 const REAL_VALUE_TYPE *
2337 dconst_third_ptr (void)
2339 static REAL_VALUE_TYPE value;
2341 /* Initialize mathematical constants for constant folding builtins.
2342 These constants need to be given to at least 160 bits precision. */
2343 if (value.cl == rvc_zero)
2345 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (3));
2347 return &value;
2350 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2352 const REAL_VALUE_TYPE *
2353 dconst_sqrt2_ptr (void)
2355 static REAL_VALUE_TYPE value;
2357 /* Initialize mathematical constants for constant folding builtins.
2358 These constants need to be given to at least 160 bits precision. */
2359 if (value.cl == rvc_zero)
2361 mpfr_t m;
2362 mpfr_init2 (m, SIGNIFICAND_BITS);
2363 mpfr_sqrt_ui (m, 2, GMP_RNDN);
2364 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2365 mpfr_clear (m);
2367 return &value;
2370 /* Fills R with +Inf. */
2372 void
2373 real_inf (REAL_VALUE_TYPE *r)
2375 get_inf (r, 0);
2378 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2379 we force a QNaN, else we force an SNaN. The string, if not empty,
2380 is parsed as a number and placed in the significand. Return true
2381 if the string was successfully parsed. */
2383 bool
2384 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2385 enum machine_mode mode)
2387 const struct real_format *fmt;
2389 fmt = REAL_MODE_FORMAT (mode);
2390 gcc_assert (fmt);
2392 if (*str == 0)
2394 if (quiet)
2395 get_canonical_qnan (r, 0);
2396 else
2397 get_canonical_snan (r, 0);
2399 else
2401 int base = 10, d;
2403 memset (r, 0, sizeof (*r));
2404 r->cl = rvc_nan;
2406 /* Parse akin to strtol into the significand of R. */
2408 while (ISSPACE (*str))
2409 str++;
2410 if (*str == '-')
2411 str++;
2412 else if (*str == '+')
2413 str++;
2414 if (*str == '0')
2416 str++;
2417 if (*str == 'x' || *str == 'X')
2419 base = 16;
2420 str++;
2422 else
2423 base = 8;
2426 while ((d = hex_value (*str)) < base)
2428 REAL_VALUE_TYPE u;
2430 switch (base)
2432 case 8:
2433 lshift_significand (r, r, 3);
2434 break;
2435 case 16:
2436 lshift_significand (r, r, 4);
2437 break;
2438 case 10:
2439 lshift_significand_1 (&u, r);
2440 lshift_significand (r, r, 3);
2441 add_significands (r, r, &u);
2442 break;
2443 default:
2444 gcc_unreachable ();
2447 get_zero (&u, 0);
2448 u.sig[0] = d;
2449 add_significands (r, r, &u);
2451 str++;
2454 /* Must have consumed the entire string for success. */
2455 if (*str != 0)
2456 return false;
2458 /* Shift the significand into place such that the bits
2459 are in the most significant bits for the format. */
2460 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2462 /* Our MSB is always unset for NaNs. */
2463 r->sig[SIGSZ-1] &= ~SIG_MSB;
2465 /* Force quiet or signalling NaN. */
2466 r->signalling = !quiet;
2469 return true;
2472 /* Fills R with the largest finite value representable in mode MODE.
2473 If SIGN is nonzero, R is set to the most negative finite value. */
2475 void
2476 real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
2478 const struct real_format *fmt;
2479 int np2;
2481 fmt = REAL_MODE_FORMAT (mode);
2482 gcc_assert (fmt);
2483 memset (r, 0, sizeof (*r));
2485 if (fmt->b == 10)
2486 decimal_real_maxval (r, sign, mode);
2487 else
2489 r->cl = rvc_normal;
2490 r->sign = sign;
2491 SET_REAL_EXP (r, fmt->emax);
2493 np2 = SIGNIFICAND_BITS - fmt->p;
2494 memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2495 clear_significand_below (r, np2);
2497 if (fmt->pnan < fmt->p)
2498 /* This is an IBM extended double format made up of two IEEE
2499 doubles. The value of the long double is the sum of the
2500 values of the two parts. The most significant part is
2501 required to be the value of the long double rounded to the
2502 nearest double. Rounding means we need a slightly smaller
2503 value for LDBL_MAX. */
2504 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1);
2508 /* Fills R with 2**N. */
2510 void
2511 real_2expN (REAL_VALUE_TYPE *r, int n, enum machine_mode fmode)
2513 memset (r, 0, sizeof (*r));
2515 n++;
2516 if (n > MAX_EXP)
2517 r->cl = rvc_inf;
2518 else if (n < -MAX_EXP)
2520 else
2522 r->cl = rvc_normal;
2523 SET_REAL_EXP (r, n);
2524 r->sig[SIGSZ-1] = SIG_MSB;
2526 if (DECIMAL_FLOAT_MODE_P (fmode))
2527 decimal_real_convert (r, fmode, r);
2531 static void
2532 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2534 int p2, np2, i, w;
2535 int emin2m1, emax2;
2536 bool round_up = false;
2538 if (r->decimal)
2540 if (fmt->b == 10)
2542 decimal_round_for_format (fmt, r);
2543 return;
2545 /* FIXME. We can come here via fp_easy_constant
2546 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2547 investigated whether this convert needs to be here, or
2548 something else is missing. */
2549 decimal_real_convert (r, DFmode, r);
2552 p2 = fmt->p;
2553 emin2m1 = fmt->emin - 1;
2554 emax2 = fmt->emax;
2556 np2 = SIGNIFICAND_BITS - p2;
2557 switch (r->cl)
2559 underflow:
2560 get_zero (r, r->sign);
2561 case rvc_zero:
2562 if (!fmt->has_signed_zero)
2563 r->sign = 0;
2564 return;
2566 overflow:
2567 get_inf (r, r->sign);
2568 case rvc_inf:
2569 return;
2571 case rvc_nan:
2572 clear_significand_below (r, np2);
2573 return;
2575 case rvc_normal:
2576 break;
2578 default:
2579 gcc_unreachable ();
2582 /* Check the range of the exponent. If we're out of range,
2583 either underflow or overflow. */
2584 if (REAL_EXP (r) > emax2)
2585 goto overflow;
2586 else if (REAL_EXP (r) <= emin2m1)
2588 int diff;
2590 if (!fmt->has_denorm)
2592 /* Don't underflow completely until we've had a chance to round. */
2593 if (REAL_EXP (r) < emin2m1)
2594 goto underflow;
2596 else
2598 diff = emin2m1 - REAL_EXP (r) + 1;
2599 if (diff > p2)
2600 goto underflow;
2602 /* De-normalize the significand. */
2603 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2604 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2608 if (!fmt->round_towards_zero)
2610 /* There are P2 true significand bits, followed by one guard bit,
2611 followed by one sticky bit, followed by stuff. Fold nonzero
2612 stuff into the sticky bit. */
2613 unsigned long sticky;
2614 bool guard, lsb;
2616 sticky = 0;
2617 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2618 sticky |= r->sig[i];
2619 sticky |= r->sig[w]
2620 & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2622 guard = test_significand_bit (r, np2 - 1);
2623 lsb = test_significand_bit (r, np2);
2625 /* Round to even. */
2626 round_up = guard && (sticky || lsb);
2629 if (round_up)
2631 REAL_VALUE_TYPE u;
2632 get_zero (&u, 0);
2633 set_significand_bit (&u, np2);
2635 if (add_significands (r, r, &u))
2637 /* Overflow. Means the significand had been all ones, and
2638 is now all zeros. Need to increase the exponent, and
2639 possibly re-normalize it. */
2640 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2641 if (REAL_EXP (r) > emax2)
2642 goto overflow;
2643 r->sig[SIGSZ-1] = SIG_MSB;
2647 /* Catch underflow that we deferred until after rounding. */
2648 if (REAL_EXP (r) <= emin2m1)
2649 goto underflow;
2651 /* Clear out trailing garbage. */
2652 clear_significand_below (r, np2);
2655 /* Extend or truncate to a new mode. */
2657 void
2658 real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
2659 const REAL_VALUE_TYPE *a)
2661 const struct real_format *fmt;
2663 fmt = REAL_MODE_FORMAT (mode);
2664 gcc_assert (fmt);
2666 *r = *a;
2668 if (a->decimal || fmt->b == 10)
2669 decimal_real_convert (r, mode, a);
2671 round_for_format (fmt, r);
2673 /* round_for_format de-normalizes denormals. Undo just that part. */
2674 if (r->cl == rvc_normal)
2675 normalize (r);
2678 /* Legacy. Likewise, except return the struct directly. */
2680 REAL_VALUE_TYPE
2681 real_value_truncate (enum machine_mode mode, REAL_VALUE_TYPE a)
2683 REAL_VALUE_TYPE r;
2684 real_convert (&r, mode, &a);
2685 return r;
2688 /* Return true if truncating to MODE is exact. */
2690 bool
2691 exact_real_truncate (enum machine_mode mode, const REAL_VALUE_TYPE *a)
2693 const struct real_format *fmt;
2694 REAL_VALUE_TYPE t;
2695 int emin2m1;
2697 fmt = REAL_MODE_FORMAT (mode);
2698 gcc_assert (fmt);
2700 /* Don't allow conversion to denormals. */
2701 emin2m1 = fmt->emin - 1;
2702 if (REAL_EXP (a) <= emin2m1)
2703 return false;
2705 /* After conversion to the new mode, the value must be identical. */
2706 real_convert (&t, mode, a);
2707 return real_identical (&t, a);
2710 /* Write R to the given target format. Place the words of the result
2711 in target word order in BUF. There are always 32 bits in each
2712 long, no matter the size of the host long.
2714 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2716 long
2717 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
2718 const struct real_format *fmt)
2720 REAL_VALUE_TYPE r;
2721 long buf1;
2723 r = *r_orig;
2724 round_for_format (fmt, &r);
2726 if (!buf)
2727 buf = &buf1;
2728 (*fmt->encode) (fmt, buf, &r);
2730 return *buf;
2733 /* Similar, but look up the format from MODE. */
2735 long
2736 real_to_target (long *buf, const REAL_VALUE_TYPE *r, enum machine_mode mode)
2738 const struct real_format *fmt;
2740 fmt = REAL_MODE_FORMAT (mode);
2741 gcc_assert (fmt);
2743 return real_to_target_fmt (buf, r, fmt);
2746 /* Read R from the given target format. Read the words of the result
2747 in target word order in BUF. There are always 32 bits in each
2748 long, no matter the size of the host long. */
2750 void
2751 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
2752 const struct real_format *fmt)
2754 (*fmt->decode) (fmt, r, buf);
2757 /* Similar, but look up the format from MODE. */
2759 void
2760 real_from_target (REAL_VALUE_TYPE *r, const long *buf, enum machine_mode mode)
2762 const struct real_format *fmt;
2764 fmt = REAL_MODE_FORMAT (mode);
2765 gcc_assert (fmt);
2767 (*fmt->decode) (fmt, r, buf);
2770 /* Return the number of bits of the largest binary value that the
2771 significand of MODE will hold. */
2772 /* ??? Legacy. Should get access to real_format directly. */
2775 significand_size (enum machine_mode mode)
2777 const struct real_format *fmt;
2779 fmt = REAL_MODE_FORMAT (mode);
2780 if (fmt == NULL)
2781 return 0;
2783 if (fmt->b == 10)
2785 /* Return the size in bits of the largest binary value that can be
2786 held by the decimal coefficient for this mode. This is one more
2787 than the number of bits required to hold the largest coefficient
2788 of this mode. */
2789 double log2_10 = 3.3219281;
2790 return fmt->p * log2_10;
2792 return fmt->p;
2795 /* Return a hash value for the given real value. */
2796 /* ??? The "unsigned int" return value is intended to be hashval_t,
2797 but I didn't want to pull hashtab.h into real.h. */
2799 unsigned int
2800 real_hash (const REAL_VALUE_TYPE *r)
2802 unsigned int h;
2803 size_t i;
2805 h = r->cl | (r->sign << 2);
2806 switch (r->cl)
2808 case rvc_zero:
2809 case rvc_inf:
2810 return h;
2812 case rvc_normal:
2813 h |= REAL_EXP (r) << 3;
2814 break;
2816 case rvc_nan:
2817 if (r->signalling)
2818 h ^= (unsigned int)-1;
2819 if (r->canonical)
2820 return h;
2821 break;
2823 default:
2824 gcc_unreachable ();
2827 if (sizeof (unsigned long) > sizeof (unsigned int))
2828 for (i = 0; i < SIGSZ; ++i)
2830 unsigned long s = r->sig[i];
2831 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2833 else
2834 for (i = 0; i < SIGSZ; ++i)
2835 h ^= r->sig[i];
2837 return h;
2840 /* IEEE single-precision format. */
2842 static void encode_ieee_single (const struct real_format *fmt,
2843 long *, const REAL_VALUE_TYPE *);
2844 static void decode_ieee_single (const struct real_format *,
2845 REAL_VALUE_TYPE *, const long *);
2847 static void
2848 encode_ieee_single (const struct real_format *fmt, long *buf,
2849 const REAL_VALUE_TYPE *r)
2851 unsigned long image, sig, exp;
2852 unsigned long sign = r->sign;
2853 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2855 image = sign << 31;
2856 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2858 switch (r->cl)
2860 case rvc_zero:
2861 break;
2863 case rvc_inf:
2864 if (fmt->has_inf)
2865 image |= 255 << 23;
2866 else
2867 image |= 0x7fffffff;
2868 break;
2870 case rvc_nan:
2871 if (fmt->has_nans)
2873 if (r->canonical)
2874 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
2875 if (r->signalling == fmt->qnan_msb_set)
2876 sig &= ~(1 << 22);
2877 else
2878 sig |= 1 << 22;
2879 if (sig == 0)
2880 sig = 1 << 21;
2882 image |= 255 << 23;
2883 image |= sig;
2885 else
2886 image |= 0x7fffffff;
2887 break;
2889 case rvc_normal:
2890 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2891 whereas the intermediate representation is 0.F x 2**exp.
2892 Which means we're off by one. */
2893 if (denormal)
2894 exp = 0;
2895 else
2896 exp = REAL_EXP (r) + 127 - 1;
2897 image |= exp << 23;
2898 image |= sig;
2899 break;
2901 default:
2902 gcc_unreachable ();
2905 buf[0] = image;
2908 static void
2909 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2910 const long *buf)
2912 unsigned long image = buf[0] & 0xffffffff;
2913 bool sign = (image >> 31) & 1;
2914 int exp = (image >> 23) & 0xff;
2916 memset (r, 0, sizeof (*r));
2917 image <<= HOST_BITS_PER_LONG - 24;
2918 image &= ~SIG_MSB;
2920 if (exp == 0)
2922 if (image && fmt->has_denorm)
2924 r->cl = rvc_normal;
2925 r->sign = sign;
2926 SET_REAL_EXP (r, -126);
2927 r->sig[SIGSZ-1] = image << 1;
2928 normalize (r);
2930 else if (fmt->has_signed_zero)
2931 r->sign = sign;
2933 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2935 if (image)
2937 r->cl = rvc_nan;
2938 r->sign = sign;
2939 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
2940 ^ fmt->qnan_msb_set);
2941 r->sig[SIGSZ-1] = image;
2943 else
2945 r->cl = rvc_inf;
2946 r->sign = sign;
2949 else
2951 r->cl = rvc_normal;
2952 r->sign = sign;
2953 SET_REAL_EXP (r, exp - 127 + 1);
2954 r->sig[SIGSZ-1] = image | SIG_MSB;
2958 const struct real_format ieee_single_format =
2960 encode_ieee_single,
2961 decode_ieee_single,
2965 -125,
2966 128,
2969 false,
2970 true,
2971 true,
2972 true,
2973 true,
2974 true,
2975 true,
2976 false
2979 const struct real_format mips_single_format =
2981 encode_ieee_single,
2982 decode_ieee_single,
2986 -125,
2987 128,
2990 false,
2991 true,
2992 true,
2993 true,
2994 true,
2995 true,
2996 false,
2997 true
3000 const struct real_format motorola_single_format =
3002 encode_ieee_single,
3003 decode_ieee_single,
3007 -125,
3008 128,
3011 false,
3012 true,
3013 true,
3014 true,
3015 true,
3016 true,
3017 true,
3018 true
3021 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3022 single precision with the following differences:
3023 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3024 are generated.
3025 - NaNs are not supported.
3026 - The range of non-zero numbers in binary is
3027 (001)[1.]000...000 to (255)[1.]111...111.
3028 - Denormals can be represented, but are treated as +0.0 when
3029 used as an operand and are never generated as a result.
3030 - -0.0 can be represented, but a zero result is always +0.0.
3031 - the only supported rounding mode is trunction (towards zero). */
3032 const struct real_format spu_single_format =
3034 encode_ieee_single,
3035 decode_ieee_single,
3039 -125,
3040 129,
3043 true,
3044 false,
3045 false,
3046 false,
3047 true,
3048 true,
3049 false,
3050 false
3053 /* IEEE double-precision format. */
3055 static void encode_ieee_double (const struct real_format *fmt,
3056 long *, const REAL_VALUE_TYPE *);
3057 static void decode_ieee_double (const struct real_format *,
3058 REAL_VALUE_TYPE *, const long *);
3060 static void
3061 encode_ieee_double (const struct real_format *fmt, long *buf,
3062 const REAL_VALUE_TYPE *r)
3064 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
3065 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3067 image_hi = r->sign << 31;
3068 image_lo = 0;
3070 if (HOST_BITS_PER_LONG == 64)
3072 sig_hi = r->sig[SIGSZ-1];
3073 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
3074 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
3076 else
3078 sig_hi = r->sig[SIGSZ-1];
3079 sig_lo = r->sig[SIGSZ-2];
3080 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
3081 sig_hi = (sig_hi >> 11) & 0xfffff;
3084 switch (r->cl)
3086 case rvc_zero:
3087 break;
3089 case rvc_inf:
3090 if (fmt->has_inf)
3091 image_hi |= 2047 << 20;
3092 else
3094 image_hi |= 0x7fffffff;
3095 image_lo = 0xffffffff;
3097 break;
3099 case rvc_nan:
3100 if (fmt->has_nans)
3102 if (r->canonical)
3104 if (fmt->canonical_nan_lsbs_set)
3106 sig_hi = (1 << 19) - 1;
3107 sig_lo = 0xffffffff;
3109 else
3111 sig_hi = 0;
3112 sig_lo = 0;
3115 if (r->signalling == fmt->qnan_msb_set)
3116 sig_hi &= ~(1 << 19);
3117 else
3118 sig_hi |= 1 << 19;
3119 if (sig_hi == 0 && sig_lo == 0)
3120 sig_hi = 1 << 18;
3122 image_hi |= 2047 << 20;
3123 image_hi |= sig_hi;
3124 image_lo = sig_lo;
3126 else
3128 image_hi |= 0x7fffffff;
3129 image_lo = 0xffffffff;
3131 break;
3133 case rvc_normal:
3134 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3135 whereas the intermediate representation is 0.F x 2**exp.
3136 Which means we're off by one. */
3137 if (denormal)
3138 exp = 0;
3139 else
3140 exp = REAL_EXP (r) + 1023 - 1;
3141 image_hi |= exp << 20;
3142 image_hi |= sig_hi;
3143 image_lo = sig_lo;
3144 break;
3146 default:
3147 gcc_unreachable ();
3150 if (FLOAT_WORDS_BIG_ENDIAN)
3151 buf[0] = image_hi, buf[1] = image_lo;
3152 else
3153 buf[0] = image_lo, buf[1] = image_hi;
3156 static void
3157 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3158 const long *buf)
3160 unsigned long image_hi, image_lo;
3161 bool sign;
3162 int exp;
3164 if (FLOAT_WORDS_BIG_ENDIAN)
3165 image_hi = buf[0], image_lo = buf[1];
3166 else
3167 image_lo = buf[0], image_hi = buf[1];
3168 image_lo &= 0xffffffff;
3169 image_hi &= 0xffffffff;
3171 sign = (image_hi >> 31) & 1;
3172 exp = (image_hi >> 20) & 0x7ff;
3174 memset (r, 0, sizeof (*r));
3176 image_hi <<= 32 - 21;
3177 image_hi |= image_lo >> 21;
3178 image_hi &= 0x7fffffff;
3179 image_lo <<= 32 - 21;
3181 if (exp == 0)
3183 if ((image_hi || image_lo) && fmt->has_denorm)
3185 r->cl = rvc_normal;
3186 r->sign = sign;
3187 SET_REAL_EXP (r, -1022);
3188 if (HOST_BITS_PER_LONG == 32)
3190 image_hi = (image_hi << 1) | (image_lo >> 31);
3191 image_lo <<= 1;
3192 r->sig[SIGSZ-1] = image_hi;
3193 r->sig[SIGSZ-2] = image_lo;
3195 else
3197 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
3198 r->sig[SIGSZ-1] = image_hi;
3200 normalize (r);
3202 else if (fmt->has_signed_zero)
3203 r->sign = sign;
3205 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
3207 if (image_hi || image_lo)
3209 r->cl = rvc_nan;
3210 r->sign = sign;
3211 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3212 if (HOST_BITS_PER_LONG == 32)
3214 r->sig[SIGSZ-1] = image_hi;
3215 r->sig[SIGSZ-2] = image_lo;
3217 else
3218 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3220 else
3222 r->cl = rvc_inf;
3223 r->sign = sign;
3226 else
3228 r->cl = rvc_normal;
3229 r->sign = sign;
3230 SET_REAL_EXP (r, exp - 1023 + 1);
3231 if (HOST_BITS_PER_LONG == 32)
3233 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3234 r->sig[SIGSZ-2] = image_lo;
3236 else
3237 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3241 const struct real_format ieee_double_format =
3243 encode_ieee_double,
3244 decode_ieee_double,
3248 -1021,
3249 1024,
3252 false,
3253 true,
3254 true,
3255 true,
3256 true,
3257 true,
3258 true,
3259 false
3262 const struct real_format mips_double_format =
3264 encode_ieee_double,
3265 decode_ieee_double,
3269 -1021,
3270 1024,
3273 false,
3274 true,
3275 true,
3276 true,
3277 true,
3278 true,
3279 false,
3280 true
3283 const struct real_format motorola_double_format =
3285 encode_ieee_double,
3286 decode_ieee_double,
3290 -1021,
3291 1024,
3294 false,
3295 true,
3296 true,
3297 true,
3298 true,
3299 true,
3300 true,
3301 true
3304 /* IEEE extended real format. This comes in three flavors: Intel's as
3305 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3306 12- and 16-byte images may be big- or little endian; Motorola's is
3307 always big endian. */
3309 /* Helper subroutine which converts from the internal format to the
3310 12-byte little-endian Intel format. Functions below adjust this
3311 for the other possible formats. */
3312 static void
3313 encode_ieee_extended (const struct real_format *fmt, long *buf,
3314 const REAL_VALUE_TYPE *r)
3316 unsigned long image_hi, sig_hi, sig_lo;
3317 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3319 image_hi = r->sign << 15;
3320 sig_hi = sig_lo = 0;
3322 switch (r->cl)
3324 case rvc_zero:
3325 break;
3327 case rvc_inf:
3328 if (fmt->has_inf)
3330 image_hi |= 32767;
3332 /* Intel requires the explicit integer bit to be set, otherwise
3333 it considers the value a "pseudo-infinity". Motorola docs
3334 say it doesn't care. */
3335 sig_hi = 0x80000000;
3337 else
3339 image_hi |= 32767;
3340 sig_lo = sig_hi = 0xffffffff;
3342 break;
3344 case rvc_nan:
3345 if (fmt->has_nans)
3347 image_hi |= 32767;
3348 if (r->canonical)
3350 if (fmt->canonical_nan_lsbs_set)
3352 sig_hi = (1 << 30) - 1;
3353 sig_lo = 0xffffffff;
3356 else if (HOST_BITS_PER_LONG == 32)
3358 sig_hi = r->sig[SIGSZ-1];
3359 sig_lo = r->sig[SIGSZ-2];
3361 else
3363 sig_lo = r->sig[SIGSZ-1];
3364 sig_hi = sig_lo >> 31 >> 1;
3365 sig_lo &= 0xffffffff;
3367 if (r->signalling == fmt->qnan_msb_set)
3368 sig_hi &= ~(1 << 30);
3369 else
3370 sig_hi |= 1 << 30;
3371 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3372 sig_hi = 1 << 29;
3374 /* Intel requires the explicit integer bit to be set, otherwise
3375 it considers the value a "pseudo-nan". Motorola docs say it
3376 doesn't care. */
3377 sig_hi |= 0x80000000;
3379 else
3381 image_hi |= 32767;
3382 sig_lo = sig_hi = 0xffffffff;
3384 break;
3386 case rvc_normal:
3388 int exp = REAL_EXP (r);
3390 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3391 whereas the intermediate representation is 0.F x 2**exp.
3392 Which means we're off by one.
3394 Except for Motorola, which consider exp=0 and explicit
3395 integer bit set to continue to be normalized. In theory
3396 this discrepancy has been taken care of by the difference
3397 in fmt->emin in round_for_format. */
3399 if (denormal)
3400 exp = 0;
3401 else
3403 exp += 16383 - 1;
3404 gcc_assert (exp >= 0);
3406 image_hi |= exp;
3408 if (HOST_BITS_PER_LONG == 32)
3410 sig_hi = r->sig[SIGSZ-1];
3411 sig_lo = r->sig[SIGSZ-2];
3413 else
3415 sig_lo = r->sig[SIGSZ-1];
3416 sig_hi = sig_lo >> 31 >> 1;
3417 sig_lo &= 0xffffffff;
3420 break;
3422 default:
3423 gcc_unreachable ();
3426 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3429 /* Convert from the internal format to the 12-byte Motorola format
3430 for an IEEE extended real. */
3431 static void
3432 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3433 const REAL_VALUE_TYPE *r)
3435 long intermed[3];
3436 encode_ieee_extended (fmt, intermed, r);
3438 /* Motorola chips are assumed always to be big-endian. Also, the
3439 padding in a Motorola extended real goes between the exponent and
3440 the mantissa. At this point the mantissa is entirely within
3441 elements 0 and 1 of intermed, and the exponent entirely within
3442 element 2, so all we have to do is swap the order around, and
3443 shift element 2 left 16 bits. */
3444 buf[0] = intermed[2] << 16;
3445 buf[1] = intermed[1];
3446 buf[2] = intermed[0];
3449 /* Convert from the internal format to the 12-byte Intel format for
3450 an IEEE extended real. */
3451 static void
3452 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3453 const REAL_VALUE_TYPE *r)
3455 if (FLOAT_WORDS_BIG_ENDIAN)
3457 /* All the padding in an Intel-format extended real goes at the high
3458 end, which in this case is after the mantissa, not the exponent.
3459 Therefore we must shift everything down 16 bits. */
3460 long intermed[3];
3461 encode_ieee_extended (fmt, intermed, r);
3462 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3463 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3464 buf[2] = (intermed[0] << 16);
3466 else
3467 /* encode_ieee_extended produces what we want directly. */
3468 encode_ieee_extended (fmt, buf, r);
3471 /* Convert from the internal format to the 16-byte Intel format for
3472 an IEEE extended real. */
3473 static void
3474 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3475 const REAL_VALUE_TYPE *r)
3477 /* All the padding in an Intel-format extended real goes at the high end. */
3478 encode_ieee_extended_intel_96 (fmt, buf, r);
3479 buf[3] = 0;
3482 /* As above, we have a helper function which converts from 12-byte
3483 little-endian Intel format to internal format. Functions below
3484 adjust for the other possible formats. */
3485 static void
3486 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3487 const long *buf)
3489 unsigned long image_hi, sig_hi, sig_lo;
3490 bool sign;
3491 int exp;
3493 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3494 sig_lo &= 0xffffffff;
3495 sig_hi &= 0xffffffff;
3496 image_hi &= 0xffffffff;
3498 sign = (image_hi >> 15) & 1;
3499 exp = image_hi & 0x7fff;
3501 memset (r, 0, sizeof (*r));
3503 if (exp == 0)
3505 if ((sig_hi || sig_lo) && fmt->has_denorm)
3507 r->cl = rvc_normal;
3508 r->sign = sign;
3510 /* When the IEEE format contains a hidden bit, we know that
3511 it's zero at this point, and so shift up the significand
3512 and decrease the exponent to match. In this case, Motorola
3513 defines the explicit integer bit to be valid, so we don't
3514 know whether the msb is set or not. */
3515 SET_REAL_EXP (r, fmt->emin);
3516 if (HOST_BITS_PER_LONG == 32)
3518 r->sig[SIGSZ-1] = sig_hi;
3519 r->sig[SIGSZ-2] = sig_lo;
3521 else
3522 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3524 normalize (r);
3526 else if (fmt->has_signed_zero)
3527 r->sign = sign;
3529 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3531 /* See above re "pseudo-infinities" and "pseudo-nans".
3532 Short summary is that the MSB will likely always be
3533 set, and that we don't care about it. */
3534 sig_hi &= 0x7fffffff;
3536 if (sig_hi || sig_lo)
3538 r->cl = rvc_nan;
3539 r->sign = sign;
3540 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3541 if (HOST_BITS_PER_LONG == 32)
3543 r->sig[SIGSZ-1] = sig_hi;
3544 r->sig[SIGSZ-2] = sig_lo;
3546 else
3547 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3549 else
3551 r->cl = rvc_inf;
3552 r->sign = sign;
3555 else
3557 r->cl = rvc_normal;
3558 r->sign = sign;
3559 SET_REAL_EXP (r, exp - 16383 + 1);
3560 if (HOST_BITS_PER_LONG == 32)
3562 r->sig[SIGSZ-1] = sig_hi;
3563 r->sig[SIGSZ-2] = sig_lo;
3565 else
3566 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3570 /* Convert from the internal format to the 12-byte Motorola format
3571 for an IEEE extended real. */
3572 static void
3573 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3574 const long *buf)
3576 long intermed[3];
3578 /* Motorola chips are assumed always to be big-endian. Also, the
3579 padding in a Motorola extended real goes between the exponent and
3580 the mantissa; remove it. */
3581 intermed[0] = buf[2];
3582 intermed[1] = buf[1];
3583 intermed[2] = (unsigned long)buf[0] >> 16;
3585 decode_ieee_extended (fmt, r, intermed);
3588 /* Convert from the internal format to the 12-byte Intel format for
3589 an IEEE extended real. */
3590 static void
3591 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3592 const long *buf)
3594 if (FLOAT_WORDS_BIG_ENDIAN)
3596 /* All the padding in an Intel-format extended real goes at the high
3597 end, which in this case is after the mantissa, not the exponent.
3598 Therefore we must shift everything up 16 bits. */
3599 long intermed[3];
3601 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3602 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3603 intermed[2] = ((unsigned long)buf[0] >> 16);
3605 decode_ieee_extended (fmt, r, intermed);
3607 else
3608 /* decode_ieee_extended produces what we want directly. */
3609 decode_ieee_extended (fmt, r, buf);
3612 /* Convert from the internal format to the 16-byte Intel format for
3613 an IEEE extended real. */
3614 static void
3615 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3616 const long *buf)
3618 /* All the padding in an Intel-format extended real goes at the high end. */
3619 decode_ieee_extended_intel_96 (fmt, r, buf);
3622 const struct real_format ieee_extended_motorola_format =
3624 encode_ieee_extended_motorola,
3625 decode_ieee_extended_motorola,
3629 -16382,
3630 16384,
3633 false,
3634 true,
3635 true,
3636 true,
3637 true,
3638 true,
3639 true,
3640 true
3643 const struct real_format ieee_extended_intel_96_format =
3645 encode_ieee_extended_intel_96,
3646 decode_ieee_extended_intel_96,
3650 -16381,
3651 16384,
3654 false,
3655 true,
3656 true,
3657 true,
3658 true,
3659 true,
3660 true,
3661 false
3664 const struct real_format ieee_extended_intel_128_format =
3666 encode_ieee_extended_intel_128,
3667 decode_ieee_extended_intel_128,
3671 -16381,
3672 16384,
3675 false,
3676 true,
3677 true,
3678 true,
3679 true,
3680 true,
3681 true,
3682 false
3685 /* The following caters to i386 systems that set the rounding precision
3686 to 53 bits instead of 64, e.g. FreeBSD. */
3687 const struct real_format ieee_extended_intel_96_round_53_format =
3689 encode_ieee_extended_intel_96,
3690 decode_ieee_extended_intel_96,
3694 -16381,
3695 16384,
3698 false,
3699 true,
3700 true,
3701 true,
3702 true,
3703 true,
3704 true,
3705 false
3708 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3709 numbers whose sum is equal to the extended precision value. The number
3710 with greater magnitude is first. This format has the same magnitude
3711 range as an IEEE double precision value, but effectively 106 bits of
3712 significand precision. Infinity and NaN are represented by their IEEE
3713 double precision value stored in the first number, the second number is
3714 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3716 static void encode_ibm_extended (const struct real_format *fmt,
3717 long *, const REAL_VALUE_TYPE *);
3718 static void decode_ibm_extended (const struct real_format *,
3719 REAL_VALUE_TYPE *, const long *);
3721 static void
3722 encode_ibm_extended (const struct real_format *fmt, long *buf,
3723 const REAL_VALUE_TYPE *r)
3725 REAL_VALUE_TYPE u, normr, v;
3726 const struct real_format *base_fmt;
3728 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3730 /* Renormalize R before doing any arithmetic on it. */
3731 normr = *r;
3732 if (normr.cl == rvc_normal)
3733 normalize (&normr);
3735 /* u = IEEE double precision portion of significand. */
3736 u = normr;
3737 round_for_format (base_fmt, &u);
3738 encode_ieee_double (base_fmt, &buf[0], &u);
3740 if (u.cl == rvc_normal)
3742 do_add (&v, &normr, &u, 1);
3743 /* Call round_for_format since we might need to denormalize. */
3744 round_for_format (base_fmt, &v);
3745 encode_ieee_double (base_fmt, &buf[2], &v);
3747 else
3749 /* Inf, NaN, 0 are all representable as doubles, so the
3750 least-significant part can be 0.0. */
3751 buf[2] = 0;
3752 buf[3] = 0;
3756 static void
3757 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3758 const long *buf)
3760 REAL_VALUE_TYPE u, v;
3761 const struct real_format *base_fmt;
3763 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3764 decode_ieee_double (base_fmt, &u, &buf[0]);
3766 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3768 decode_ieee_double (base_fmt, &v, &buf[2]);
3769 do_add (r, &u, &v, 0);
3771 else
3772 *r = u;
3775 const struct real_format ibm_extended_format =
3777 encode_ibm_extended,
3778 decode_ibm_extended,
3780 53 + 53,
3782 -1021 + 53,
3783 1024,
3784 127,
3786 false,
3787 true,
3788 true,
3789 true,
3790 true,
3791 true,
3792 true,
3793 false
3796 const struct real_format mips_extended_format =
3798 encode_ibm_extended,
3799 decode_ibm_extended,
3801 53 + 53,
3803 -1021 + 53,
3804 1024,
3805 127,
3807 false,
3808 true,
3809 true,
3810 true,
3811 true,
3812 true,
3813 false,
3814 true
3818 /* IEEE quad precision format. */
3820 static void encode_ieee_quad (const struct real_format *fmt,
3821 long *, const REAL_VALUE_TYPE *);
3822 static void decode_ieee_quad (const struct real_format *,
3823 REAL_VALUE_TYPE *, const long *);
3825 static void
3826 encode_ieee_quad (const struct real_format *fmt, long *buf,
3827 const REAL_VALUE_TYPE *r)
3829 unsigned long image3, image2, image1, image0, exp;
3830 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3831 REAL_VALUE_TYPE u;
3833 image3 = r->sign << 31;
3834 image2 = 0;
3835 image1 = 0;
3836 image0 = 0;
3838 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3840 switch (r->cl)
3842 case rvc_zero:
3843 break;
3845 case rvc_inf:
3846 if (fmt->has_inf)
3847 image3 |= 32767 << 16;
3848 else
3850 image3 |= 0x7fffffff;
3851 image2 = 0xffffffff;
3852 image1 = 0xffffffff;
3853 image0 = 0xffffffff;
3855 break;
3857 case rvc_nan:
3858 if (fmt->has_nans)
3860 image3 |= 32767 << 16;
3862 if (r->canonical)
3864 if (fmt->canonical_nan_lsbs_set)
3866 image3 |= 0x7fff;
3867 image2 = image1 = image0 = 0xffffffff;
3870 else if (HOST_BITS_PER_LONG == 32)
3872 image0 = u.sig[0];
3873 image1 = u.sig[1];
3874 image2 = u.sig[2];
3875 image3 |= u.sig[3] & 0xffff;
3877 else
3879 image0 = u.sig[0];
3880 image1 = image0 >> 31 >> 1;
3881 image2 = u.sig[1];
3882 image3 |= (image2 >> 31 >> 1) & 0xffff;
3883 image0 &= 0xffffffff;
3884 image2 &= 0xffffffff;
3886 if (r->signalling == fmt->qnan_msb_set)
3887 image3 &= ~0x8000;
3888 else
3889 image3 |= 0x8000;
3890 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3891 image3 |= 0x4000;
3893 else
3895 image3 |= 0x7fffffff;
3896 image2 = 0xffffffff;
3897 image1 = 0xffffffff;
3898 image0 = 0xffffffff;
3900 break;
3902 case rvc_normal:
3903 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3904 whereas the intermediate representation is 0.F x 2**exp.
3905 Which means we're off by one. */
3906 if (denormal)
3907 exp = 0;
3908 else
3909 exp = REAL_EXP (r) + 16383 - 1;
3910 image3 |= exp << 16;
3912 if (HOST_BITS_PER_LONG == 32)
3914 image0 = u.sig[0];
3915 image1 = u.sig[1];
3916 image2 = u.sig[2];
3917 image3 |= u.sig[3] & 0xffff;
3919 else
3921 image0 = u.sig[0];
3922 image1 = image0 >> 31 >> 1;
3923 image2 = u.sig[1];
3924 image3 |= (image2 >> 31 >> 1) & 0xffff;
3925 image0 &= 0xffffffff;
3926 image2 &= 0xffffffff;
3928 break;
3930 default:
3931 gcc_unreachable ();
3934 if (FLOAT_WORDS_BIG_ENDIAN)
3936 buf[0] = image3;
3937 buf[1] = image2;
3938 buf[2] = image1;
3939 buf[3] = image0;
3941 else
3943 buf[0] = image0;
3944 buf[1] = image1;
3945 buf[2] = image2;
3946 buf[3] = image3;
3950 static void
3951 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3952 const long *buf)
3954 unsigned long image3, image2, image1, image0;
3955 bool sign;
3956 int exp;
3958 if (FLOAT_WORDS_BIG_ENDIAN)
3960 image3 = buf[0];
3961 image2 = buf[1];
3962 image1 = buf[2];
3963 image0 = buf[3];
3965 else
3967 image0 = buf[0];
3968 image1 = buf[1];
3969 image2 = buf[2];
3970 image3 = buf[3];
3972 image0 &= 0xffffffff;
3973 image1 &= 0xffffffff;
3974 image2 &= 0xffffffff;
3976 sign = (image3 >> 31) & 1;
3977 exp = (image3 >> 16) & 0x7fff;
3978 image3 &= 0xffff;
3980 memset (r, 0, sizeof (*r));
3982 if (exp == 0)
3984 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3986 r->cl = rvc_normal;
3987 r->sign = sign;
3989 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
3990 if (HOST_BITS_PER_LONG == 32)
3992 r->sig[0] = image0;
3993 r->sig[1] = image1;
3994 r->sig[2] = image2;
3995 r->sig[3] = image3;
3997 else
3999 r->sig[0] = (image1 << 31 << 1) | image0;
4000 r->sig[1] = (image3 << 31 << 1) | image2;
4003 normalize (r);
4005 else if (fmt->has_signed_zero)
4006 r->sign = sign;
4008 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
4010 if (image3 | image2 | image1 | image0)
4012 r->cl = rvc_nan;
4013 r->sign = sign;
4014 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
4016 if (HOST_BITS_PER_LONG == 32)
4018 r->sig[0] = image0;
4019 r->sig[1] = image1;
4020 r->sig[2] = image2;
4021 r->sig[3] = image3;
4023 else
4025 r->sig[0] = (image1 << 31 << 1) | image0;
4026 r->sig[1] = (image3 << 31 << 1) | image2;
4028 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4030 else
4032 r->cl = rvc_inf;
4033 r->sign = sign;
4036 else
4038 r->cl = rvc_normal;
4039 r->sign = sign;
4040 SET_REAL_EXP (r, exp - 16383 + 1);
4042 if (HOST_BITS_PER_LONG == 32)
4044 r->sig[0] = image0;
4045 r->sig[1] = image1;
4046 r->sig[2] = image2;
4047 r->sig[3] = image3;
4049 else
4051 r->sig[0] = (image1 << 31 << 1) | image0;
4052 r->sig[1] = (image3 << 31 << 1) | image2;
4054 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4055 r->sig[SIGSZ-1] |= SIG_MSB;
4059 const struct real_format ieee_quad_format =
4061 encode_ieee_quad,
4062 decode_ieee_quad,
4064 113,
4065 113,
4066 -16381,
4067 16384,
4068 127,
4069 127,
4070 false,
4071 true,
4072 true,
4073 true,
4074 true,
4075 true,
4076 true,
4077 false
4080 const struct real_format mips_quad_format =
4082 encode_ieee_quad,
4083 decode_ieee_quad,
4085 113,
4086 113,
4087 -16381,
4088 16384,
4089 127,
4090 127,
4091 false,
4092 true,
4093 true,
4094 true,
4095 true,
4096 true,
4097 false,
4098 true
4101 /* Descriptions of VAX floating point formats can be found beginning at
4103 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4105 The thing to remember is that they're almost IEEE, except for word
4106 order, exponent bias, and the lack of infinities, nans, and denormals.
4108 We don't implement the H_floating format here, simply because neither
4109 the VAX or Alpha ports use it. */
4111 static void encode_vax_f (const struct real_format *fmt,
4112 long *, const REAL_VALUE_TYPE *);
4113 static void decode_vax_f (const struct real_format *,
4114 REAL_VALUE_TYPE *, const long *);
4115 static void encode_vax_d (const struct real_format *fmt,
4116 long *, const REAL_VALUE_TYPE *);
4117 static void decode_vax_d (const struct real_format *,
4118 REAL_VALUE_TYPE *, const long *);
4119 static void encode_vax_g (const struct real_format *fmt,
4120 long *, const REAL_VALUE_TYPE *);
4121 static void decode_vax_g (const struct real_format *,
4122 REAL_VALUE_TYPE *, const long *);
4124 static void
4125 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4126 const REAL_VALUE_TYPE *r)
4128 unsigned long sign, exp, sig, image;
4130 sign = r->sign << 15;
4132 switch (r->cl)
4134 case rvc_zero:
4135 image = 0;
4136 break;
4138 case rvc_inf:
4139 case rvc_nan:
4140 image = 0xffff7fff | sign;
4141 break;
4143 case rvc_normal:
4144 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4145 exp = REAL_EXP (r) + 128;
4147 image = (sig << 16) & 0xffff0000;
4148 image |= sign;
4149 image |= exp << 7;
4150 image |= sig >> 16;
4151 break;
4153 default:
4154 gcc_unreachable ();
4157 buf[0] = image;
4160 static void
4161 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
4162 REAL_VALUE_TYPE *r, const long *buf)
4164 unsigned long image = buf[0] & 0xffffffff;
4165 int exp = (image >> 7) & 0xff;
4167 memset (r, 0, sizeof (*r));
4169 if (exp != 0)
4171 r->cl = rvc_normal;
4172 r->sign = (image >> 15) & 1;
4173 SET_REAL_EXP (r, exp - 128);
4175 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
4176 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4180 static void
4181 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4182 const REAL_VALUE_TYPE *r)
4184 unsigned long image0, image1, sign = r->sign << 15;
4186 switch (r->cl)
4188 case rvc_zero:
4189 image0 = image1 = 0;
4190 break;
4192 case rvc_inf:
4193 case rvc_nan:
4194 image0 = 0xffff7fff | sign;
4195 image1 = 0xffffffff;
4196 break;
4198 case rvc_normal:
4199 /* Extract the significand into straight hi:lo. */
4200 if (HOST_BITS_PER_LONG == 64)
4202 image0 = r->sig[SIGSZ-1];
4203 image1 = (image0 >> (64 - 56)) & 0xffffffff;
4204 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
4206 else
4208 image0 = r->sig[SIGSZ-1];
4209 image1 = r->sig[SIGSZ-2];
4210 image1 = (image0 << 24) | (image1 >> 8);
4211 image0 = (image0 >> 8) & 0xffffff;
4214 /* Rearrange the half-words of the significand to match the
4215 external format. */
4216 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
4217 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4219 /* Add the sign and exponent. */
4220 image0 |= sign;
4221 image0 |= (REAL_EXP (r) + 128) << 7;
4222 break;
4224 default:
4225 gcc_unreachable ();
4228 if (FLOAT_WORDS_BIG_ENDIAN)
4229 buf[0] = image1, buf[1] = image0;
4230 else
4231 buf[0] = image0, buf[1] = image1;
4234 static void
4235 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
4236 REAL_VALUE_TYPE *r, const long *buf)
4238 unsigned long image0, image1;
4239 int exp;
4241 if (FLOAT_WORDS_BIG_ENDIAN)
4242 image1 = buf[0], image0 = buf[1];
4243 else
4244 image0 = buf[0], image1 = buf[1];
4245 image0 &= 0xffffffff;
4246 image1 &= 0xffffffff;
4248 exp = (image0 >> 7) & 0xff;
4250 memset (r, 0, sizeof (*r));
4252 if (exp != 0)
4254 r->cl = rvc_normal;
4255 r->sign = (image0 >> 15) & 1;
4256 SET_REAL_EXP (r, exp - 128);
4258 /* Rearrange the half-words of the external format into
4259 proper ascending order. */
4260 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4261 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4263 if (HOST_BITS_PER_LONG == 64)
4265 image0 = (image0 << 31 << 1) | image1;
4266 image0 <<= 64 - 56;
4267 image0 |= SIG_MSB;
4268 r->sig[SIGSZ-1] = image0;
4270 else
4272 r->sig[SIGSZ-1] = image0;
4273 r->sig[SIGSZ-2] = image1;
4274 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4275 r->sig[SIGSZ-1] |= SIG_MSB;
4280 static void
4281 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4282 const REAL_VALUE_TYPE *r)
4284 unsigned long image0, image1, sign = r->sign << 15;
4286 switch (r->cl)
4288 case rvc_zero:
4289 image0 = image1 = 0;
4290 break;
4292 case rvc_inf:
4293 case rvc_nan:
4294 image0 = 0xffff7fff | sign;
4295 image1 = 0xffffffff;
4296 break;
4298 case rvc_normal:
4299 /* Extract the significand into straight hi:lo. */
4300 if (HOST_BITS_PER_LONG == 64)
4302 image0 = r->sig[SIGSZ-1];
4303 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4304 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4306 else
4308 image0 = r->sig[SIGSZ-1];
4309 image1 = r->sig[SIGSZ-2];
4310 image1 = (image0 << 21) | (image1 >> 11);
4311 image0 = (image0 >> 11) & 0xfffff;
4314 /* Rearrange the half-words of the significand to match the
4315 external format. */
4316 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4317 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4319 /* Add the sign and exponent. */
4320 image0 |= sign;
4321 image0 |= (REAL_EXP (r) + 1024) << 4;
4322 break;
4324 default:
4325 gcc_unreachable ();
4328 if (FLOAT_WORDS_BIG_ENDIAN)
4329 buf[0] = image1, buf[1] = image0;
4330 else
4331 buf[0] = image0, buf[1] = image1;
4334 static void
4335 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4336 REAL_VALUE_TYPE *r, const long *buf)
4338 unsigned long image0, image1;
4339 int exp;
4341 if (FLOAT_WORDS_BIG_ENDIAN)
4342 image1 = buf[0], image0 = buf[1];
4343 else
4344 image0 = buf[0], image1 = buf[1];
4345 image0 &= 0xffffffff;
4346 image1 &= 0xffffffff;
4348 exp = (image0 >> 4) & 0x7ff;
4350 memset (r, 0, sizeof (*r));
4352 if (exp != 0)
4354 r->cl = rvc_normal;
4355 r->sign = (image0 >> 15) & 1;
4356 SET_REAL_EXP (r, exp - 1024);
4358 /* Rearrange the half-words of the external format into
4359 proper ascending order. */
4360 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4361 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4363 if (HOST_BITS_PER_LONG == 64)
4365 image0 = (image0 << 31 << 1) | image1;
4366 image0 <<= 64 - 53;
4367 image0 |= SIG_MSB;
4368 r->sig[SIGSZ-1] = image0;
4370 else
4372 r->sig[SIGSZ-1] = image0;
4373 r->sig[SIGSZ-2] = image1;
4374 lshift_significand (r, r, 64 - 53);
4375 r->sig[SIGSZ-1] |= SIG_MSB;
4380 const struct real_format vax_f_format =
4382 encode_vax_f,
4383 decode_vax_f,
4387 -127,
4388 127,
4391 false,
4392 false,
4393 false,
4394 false,
4395 false,
4396 false,
4397 false,
4398 false
4401 const struct real_format vax_d_format =
4403 encode_vax_d,
4404 decode_vax_d,
4408 -127,
4409 127,
4412 false,
4413 false,
4414 false,
4415 false,
4416 false,
4417 false,
4418 false,
4419 false
4422 const struct real_format vax_g_format =
4424 encode_vax_g,
4425 decode_vax_g,
4429 -1023,
4430 1023,
4433 false,
4434 false,
4435 false,
4436 false,
4437 false,
4438 false,
4439 false,
4440 false
4443 /* Encode real R into a single precision DFP value in BUF. */
4444 static void
4445 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4446 long *buf ATTRIBUTE_UNUSED,
4447 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4449 encode_decimal32 (fmt, buf, r);
4452 /* Decode a single precision DFP value in BUF into a real R. */
4453 static void
4454 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4455 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4456 const long *buf ATTRIBUTE_UNUSED)
4458 decode_decimal32 (fmt, r, buf);
4461 /* Encode real R into a double precision DFP value in BUF. */
4462 static void
4463 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4464 long *buf ATTRIBUTE_UNUSED,
4465 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4467 encode_decimal64 (fmt, buf, r);
4470 /* Decode a double precision DFP value in BUF into a real R. */
4471 static void
4472 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4473 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4474 const long *buf ATTRIBUTE_UNUSED)
4476 decode_decimal64 (fmt, r, buf);
4479 /* Encode real R into a quad precision DFP value in BUF. */
4480 static void
4481 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4482 long *buf ATTRIBUTE_UNUSED,
4483 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4485 encode_decimal128 (fmt, buf, r);
4488 /* Decode a quad precision DFP value in BUF into a real R. */
4489 static void
4490 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4491 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4492 const long *buf ATTRIBUTE_UNUSED)
4494 decode_decimal128 (fmt, r, buf);
4497 /* Single precision decimal floating point (IEEE 754). */
4498 const struct real_format decimal_single_format =
4500 encode_decimal_single,
4501 decode_decimal_single,
4505 -94,
4509 false,
4510 true,
4511 true,
4512 true,
4513 true,
4514 true,
4515 true,
4516 false
4519 /* Double precision decimal floating point (IEEE 754). */
4520 const struct real_format decimal_double_format =
4522 encode_decimal_double,
4523 decode_decimal_double,
4527 -382,
4528 385,
4531 false,
4532 true,
4533 true,
4534 true,
4535 true,
4536 true,
4537 true,
4538 false
4541 /* Quad precision decimal floating point (IEEE 754). */
4542 const struct real_format decimal_quad_format =
4544 encode_decimal_quad,
4545 decode_decimal_quad,
4549 -6142,
4550 6145,
4551 127,
4552 127,
4553 false,
4554 true,
4555 true,
4556 true,
4557 true,
4558 true,
4559 true,
4560 false
4563 /* Encode half-precision floats. This routine is used both for the IEEE
4564 ARM alternative encodings. */
4565 static void
4566 encode_ieee_half (const struct real_format *fmt, long *buf,
4567 const REAL_VALUE_TYPE *r)
4569 unsigned long image, sig, exp;
4570 unsigned long sign = r->sign;
4571 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
4573 image = sign << 15;
4574 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 11)) & 0x3ff;
4576 switch (r->cl)
4578 case rvc_zero:
4579 break;
4581 case rvc_inf:
4582 if (fmt->has_inf)
4583 image |= 31 << 10;
4584 else
4585 image |= 0x7fff;
4586 break;
4588 case rvc_nan:
4589 if (fmt->has_nans)
4591 if (r->canonical)
4592 sig = (fmt->canonical_nan_lsbs_set ? (1 << 9) - 1 : 0);
4593 if (r->signalling == fmt->qnan_msb_set)
4594 sig &= ~(1 << 9);
4595 else
4596 sig |= 1 << 9;
4597 if (sig == 0)
4598 sig = 1 << 8;
4600 image |= 31 << 10;
4601 image |= sig;
4603 else
4604 image |= 0x3ff;
4605 break;
4607 case rvc_normal:
4608 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4609 whereas the intermediate representation is 0.F x 2**exp.
4610 Which means we're off by one. */
4611 if (denormal)
4612 exp = 0;
4613 else
4614 exp = REAL_EXP (r) + 15 - 1;
4615 image |= exp << 10;
4616 image |= sig;
4617 break;
4619 default:
4620 gcc_unreachable ();
4623 buf[0] = image;
4626 /* Decode half-precision floats. This routine is used both for the IEEE
4627 ARM alternative encodings. */
4628 static void
4629 decode_ieee_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4630 const long *buf)
4632 unsigned long image = buf[0] & 0xffff;
4633 bool sign = (image >> 15) & 1;
4634 int exp = (image >> 10) & 0x1f;
4636 memset (r, 0, sizeof (*r));
4637 image <<= HOST_BITS_PER_LONG - 11;
4638 image &= ~SIG_MSB;
4640 if (exp == 0)
4642 if (image && fmt->has_denorm)
4644 r->cl = rvc_normal;
4645 r->sign = sign;
4646 SET_REAL_EXP (r, -14);
4647 r->sig[SIGSZ-1] = image << 1;
4648 normalize (r);
4650 else if (fmt->has_signed_zero)
4651 r->sign = sign;
4653 else if (exp == 31 && (fmt->has_nans || fmt->has_inf))
4655 if (image)
4657 r->cl = rvc_nan;
4658 r->sign = sign;
4659 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4660 ^ fmt->qnan_msb_set);
4661 r->sig[SIGSZ-1] = image;
4663 else
4665 r->cl = rvc_inf;
4666 r->sign = sign;
4669 else
4671 r->cl = rvc_normal;
4672 r->sign = sign;
4673 SET_REAL_EXP (r, exp - 15 + 1);
4674 r->sig[SIGSZ-1] = image | SIG_MSB;
4678 /* Half-precision format, as specified in IEEE 754R. */
4679 const struct real_format ieee_half_format =
4681 encode_ieee_half,
4682 decode_ieee_half,
4686 -13,
4690 false,
4691 true,
4692 true,
4693 true,
4694 true,
4695 true,
4696 true,
4697 false
4700 /* ARM's alternative half-precision format, similar to IEEE but with
4701 no reserved exponent value for NaNs and infinities; rather, it just
4702 extends the range of exponents by one. */
4703 const struct real_format arm_half_format =
4705 encode_ieee_half,
4706 decode_ieee_half,
4710 -13,
4714 false,
4715 true,
4716 false,
4717 false,
4718 true,
4719 true,
4720 false,
4721 false
4724 /* A synthetic "format" for internal arithmetic. It's the size of the
4725 internal significand minus the two bits needed for proper rounding.
4726 The encode and decode routines exist only to satisfy our paranoia
4727 harness. */
4729 static void encode_internal (const struct real_format *fmt,
4730 long *, const REAL_VALUE_TYPE *);
4731 static void decode_internal (const struct real_format *,
4732 REAL_VALUE_TYPE *, const long *);
4734 static void
4735 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4736 const REAL_VALUE_TYPE *r)
4738 memcpy (buf, r, sizeof (*r));
4741 static void
4742 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
4743 REAL_VALUE_TYPE *r, const long *buf)
4745 memcpy (r, buf, sizeof (*r));
4748 const struct real_format real_internal_format =
4750 encode_internal,
4751 decode_internal,
4753 SIGNIFICAND_BITS - 2,
4754 SIGNIFICAND_BITS - 2,
4755 -MAX_EXP,
4756 MAX_EXP,
4759 false,
4760 false,
4761 true,
4762 true,
4763 false,
4764 true,
4765 true,
4766 false
4769 /* Calculate X raised to the integer exponent N in mode MODE and store
4770 the result in R. Return true if the result may be inexact due to
4771 loss of precision. The algorithm is the classic "left-to-right binary
4772 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4773 Algorithms", "The Art of Computer Programming", Volume 2. */
4775 bool
4776 real_powi (REAL_VALUE_TYPE *r, enum machine_mode mode,
4777 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4779 unsigned HOST_WIDE_INT bit;
4780 REAL_VALUE_TYPE t;
4781 bool inexact = false;
4782 bool init = false;
4783 bool neg;
4784 int i;
4786 if (n == 0)
4788 *r = dconst1;
4789 return false;
4791 else if (n < 0)
4793 /* Don't worry about overflow, from now on n is unsigned. */
4794 neg = true;
4795 n = -n;
4797 else
4798 neg = false;
4800 t = *x;
4801 bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
4802 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4804 if (init)
4806 inexact |= do_multiply (&t, &t, &t);
4807 if (n & bit)
4808 inexact |= do_multiply (&t, &t, x);
4810 else if (n & bit)
4811 init = true;
4812 bit >>= 1;
4815 if (neg)
4816 inexact |= do_divide (&t, &dconst1, &t);
4818 real_convert (r, mode, &t);
4819 return inexact;
4822 /* Round X to the nearest integer not larger in absolute value, i.e.
4823 towards zero, placing the result in R in mode MODE. */
4825 void
4826 real_trunc (REAL_VALUE_TYPE *r, enum machine_mode mode,
4827 const REAL_VALUE_TYPE *x)
4829 do_fix_trunc (r, x);
4830 if (mode != VOIDmode)
4831 real_convert (r, mode, r);
4834 /* Round X to the largest integer not greater in value, i.e. round
4835 down, placing the result in R in mode MODE. */
4837 void
4838 real_floor (REAL_VALUE_TYPE *r, enum machine_mode mode,
4839 const REAL_VALUE_TYPE *x)
4841 REAL_VALUE_TYPE t;
4843 do_fix_trunc (&t, x);
4844 if (! real_identical (&t, x) && x->sign)
4845 do_add (&t, &t, &dconstm1, 0);
4846 if (mode != VOIDmode)
4847 real_convert (r, mode, &t);
4848 else
4849 *r = t;
4852 /* Round X to the smallest integer not less then argument, i.e. round
4853 up, placing the result in R in mode MODE. */
4855 void
4856 real_ceil (REAL_VALUE_TYPE *r, enum machine_mode mode,
4857 const REAL_VALUE_TYPE *x)
4859 REAL_VALUE_TYPE t;
4861 do_fix_trunc (&t, x);
4862 if (! real_identical (&t, x) && ! x->sign)
4863 do_add (&t, &t, &dconst1, 0);
4864 if (mode != VOIDmode)
4865 real_convert (r, mode, &t);
4866 else
4867 *r = t;
4870 /* Round X to the nearest integer, but round halfway cases away from
4871 zero. */
4873 void
4874 real_round (REAL_VALUE_TYPE *r, enum machine_mode mode,
4875 const REAL_VALUE_TYPE *x)
4877 do_add (r, x, &dconsthalf, x->sign);
4878 do_fix_trunc (r, r);
4879 if (mode != VOIDmode)
4880 real_convert (r, mode, r);
4883 /* Set the sign of R to the sign of X. */
4885 void
4886 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4888 r->sign = x->sign;
4891 /* Check whether the real constant value given is an integer. */
4893 bool
4894 real_isinteger (const REAL_VALUE_TYPE *c, enum machine_mode mode)
4896 REAL_VALUE_TYPE cint;
4898 real_trunc (&cint, mode, c);
4899 return real_identical (c, &cint);
4902 /* Write into BUF the maximum representable finite floating-point
4903 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4904 float string. LEN is the size of BUF, and the buffer must be large
4905 enough to contain the resulting string. */
4907 void
4908 get_max_float (const struct real_format *fmt, char *buf, size_t len)
4910 int i, n;
4911 char *p;
4913 strcpy (buf, "0x0.");
4914 n = fmt->p;
4915 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
4916 *p++ = 'f';
4917 if (i < n)
4918 *p++ = "08ce"[n - i];
4919 sprintf (p, "p%d", fmt->emax);
4920 if (fmt->pnan < fmt->p)
4922 /* This is an IBM extended double format made up of two IEEE
4923 doubles. The value of the long double is the sum of the
4924 values of the two parts. The most significant part is
4925 required to be the value of the long double rounded to the
4926 nearest double. Rounding means we need a slightly smaller
4927 value for LDBL_MAX. */
4928 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
4931 gcc_assert (strlen (buf) < len);