re PR fortran/49397 ([F03] ICE with proc pointer assignment)
[official-gcc.git] / gcc / real.c
blob86f88cec4b9cac2f753c17b9e45c49a6e58d2a5c
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 HOST_WIDE_INT low, high;
1381 int exp;
1383 switch (r->cl)
1385 case rvc_zero:
1386 underflow:
1387 low = high = 0;
1388 break;
1390 case rvc_inf:
1391 case rvc_nan:
1392 overflow:
1393 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1394 if (r->sign)
1395 low = 0;
1396 else
1398 high--;
1399 low = -1;
1401 break;
1403 case rvc_normal:
1404 if (r->decimal)
1406 decimal_real_to_integer2 (plow, phigh, r);
1407 return;
1410 exp = REAL_EXP (r);
1411 if (exp <= 0)
1412 goto underflow;
1413 /* Only force overflow for unsigned overflow. Signed overflow is
1414 undefined, so it doesn't matter what we return, and some callers
1415 expect to be able to use this routine for both signed and
1416 unsigned conversions. */
1417 if (exp > HOST_BITS_PER_DOUBLE_INT)
1418 goto overflow;
1420 rshift_significand (&t, r, HOST_BITS_PER_DOUBLE_INT - exp);
1421 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1423 high = t.sig[SIGSZ-1];
1424 low = t.sig[SIGSZ-2];
1426 else
1428 gcc_assert (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG);
1429 high = t.sig[SIGSZ-1];
1430 high = high << (HOST_BITS_PER_LONG - 1) << 1;
1431 high |= t.sig[SIGSZ-2];
1433 low = t.sig[SIGSZ-3];
1434 low = low << (HOST_BITS_PER_LONG - 1) << 1;
1435 low |= t.sig[SIGSZ-4];
1438 if (r->sign)
1440 if (low == 0)
1441 high = -high;
1442 else
1443 low = -low, high = ~high;
1445 break;
1447 default:
1448 gcc_unreachable ();
1451 *plow = low;
1452 *phigh = high;
1455 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1456 of NUM / DEN. Return the quotient and place the remainder in NUM.
1457 It is expected that NUM / DEN are close enough that the quotient is
1458 small. */
1460 static unsigned long
1461 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1463 unsigned long q, msb;
1464 int expn = REAL_EXP (num), expd = REAL_EXP (den);
1466 if (expn < expd)
1467 return 0;
1469 q = msb = 0;
1470 goto start;
1473 msb = num->sig[SIGSZ-1] & SIG_MSB;
1474 q <<= 1;
1475 lshift_significand_1 (num, num);
1476 start:
1477 if (msb || cmp_significands (num, den) >= 0)
1479 sub_significands (num, num, den, 0);
1480 q |= 1;
1483 while (--expn >= expd);
1485 SET_REAL_EXP (num, expd);
1486 normalize (num);
1488 return q;
1491 /* Render R as a decimal floating point constant. Emit DIGITS significant
1492 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1493 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1494 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1495 to a string that, when parsed back in mode MODE, yields the same value. */
1497 #define M_LOG10_2 0.30102999566398119521
1499 void
1500 real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig,
1501 size_t buf_size, size_t digits,
1502 int crop_trailing_zeros, enum machine_mode mode)
1504 const struct real_format *fmt = NULL;
1505 const REAL_VALUE_TYPE *one, *ten;
1506 REAL_VALUE_TYPE r, pten, u, v;
1507 int dec_exp, cmp_one, digit;
1508 size_t max_digits;
1509 char *p, *first, *last;
1510 bool sign;
1511 bool round_up;
1513 if (mode != VOIDmode)
1515 fmt = REAL_MODE_FORMAT (mode);
1516 gcc_assert (fmt);
1519 r = *r_orig;
1520 switch (r.cl)
1522 case rvc_zero:
1523 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1524 return;
1525 case rvc_normal:
1526 break;
1527 case rvc_inf:
1528 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1529 return;
1530 case rvc_nan:
1531 /* ??? Print the significand as well, if not canonical? */
1532 sprintf (str, "%c%cNaN", (r_orig->sign ? '-' : '+'),
1533 (r_orig->signalling ? 'S' : 'Q'));
1534 return;
1535 default:
1536 gcc_unreachable ();
1539 if (r.decimal)
1541 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1542 return;
1545 /* Bound the number of digits printed by the size of the representation. */
1546 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1547 if (digits == 0 || digits > max_digits)
1548 digits = max_digits;
1550 /* Estimate the decimal exponent, and compute the length of the string it
1551 will print as. Be conservative and add one to account for possible
1552 overflow or rounding error. */
1553 dec_exp = REAL_EXP (&r) * M_LOG10_2;
1554 for (max_digits = 1; dec_exp ; max_digits++)
1555 dec_exp /= 10;
1557 /* Bound the number of digits printed by the size of the output buffer. */
1558 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1559 gcc_assert (max_digits <= buf_size);
1560 if (digits > max_digits)
1561 digits = max_digits;
1563 one = real_digit (1);
1564 ten = ten_to_ptwo (0);
1566 sign = r.sign;
1567 r.sign = 0;
1569 dec_exp = 0;
1570 pten = *one;
1572 cmp_one = do_compare (&r, one, 0);
1573 if (cmp_one > 0)
1575 int m;
1577 /* Number is greater than one. Convert significand to an integer
1578 and strip trailing decimal zeros. */
1580 u = r;
1581 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1583 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1584 m = floor_log2 (max_digits);
1586 /* Iterate over the bits of the possible powers of 10 that might
1587 be present in U and eliminate them. That is, if we find that
1588 10**2**M divides U evenly, keep the division and increase
1589 DEC_EXP by 2**M. */
1592 REAL_VALUE_TYPE t;
1594 do_divide (&t, &u, ten_to_ptwo (m));
1595 do_fix_trunc (&v, &t);
1596 if (cmp_significands (&v, &t) == 0)
1598 u = t;
1599 dec_exp += 1 << m;
1602 while (--m >= 0);
1604 /* Revert the scaling to integer that we performed earlier. */
1605 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1606 - (SIGNIFICAND_BITS - 1));
1607 r = u;
1609 /* Find power of 10. Do this by dividing out 10**2**M when
1610 this is larger than the current remainder. Fill PTEN with
1611 the power of 10 that we compute. */
1612 if (REAL_EXP (&r) > 0)
1614 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1617 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1618 if (do_compare (&u, ptentwo, 0) >= 0)
1620 do_divide (&u, &u, ptentwo);
1621 do_multiply (&pten, &pten, ptentwo);
1622 dec_exp += 1 << m;
1625 while (--m >= 0);
1627 else
1628 /* We managed to divide off enough tens in the above reduction
1629 loop that we've now got a negative exponent. Fall into the
1630 less-than-one code to compute the proper value for PTEN. */
1631 cmp_one = -1;
1633 if (cmp_one < 0)
1635 int m;
1637 /* Number is less than one. Pad significand with leading
1638 decimal zeros. */
1640 v = r;
1641 while (1)
1643 /* Stop if we'd shift bits off the bottom. */
1644 if (v.sig[0] & 7)
1645 break;
1647 do_multiply (&u, &v, ten);
1649 /* Stop if we're now >= 1. */
1650 if (REAL_EXP (&u) > 0)
1651 break;
1653 v = u;
1654 dec_exp -= 1;
1656 r = v;
1658 /* Find power of 10. Do this by multiplying in P=10**2**M when
1659 the current remainder is smaller than 1/P. Fill PTEN with the
1660 power of 10 that we compute. */
1661 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1664 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1665 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1667 if (do_compare (&v, ptenmtwo, 0) <= 0)
1669 do_multiply (&v, &v, ptentwo);
1670 do_multiply (&pten, &pten, ptentwo);
1671 dec_exp -= 1 << m;
1674 while (--m >= 0);
1676 /* Invert the positive power of 10 that we've collected so far. */
1677 do_divide (&pten, one, &pten);
1680 p = str;
1681 if (sign)
1682 *p++ = '-';
1683 first = p++;
1685 /* At this point, PTEN should contain the nearest power of 10 smaller
1686 than R, such that this division produces the first digit.
1688 Using a divide-step primitive that returns the complete integral
1689 remainder avoids the rounding error that would be produced if
1690 we were to use do_divide here and then simply multiply by 10 for
1691 each subsequent digit. */
1693 digit = rtd_divmod (&r, &pten);
1695 /* Be prepared for error in that division via underflow ... */
1696 if (digit == 0 && cmp_significand_0 (&r))
1698 /* Multiply by 10 and try again. */
1699 do_multiply (&r, &r, ten);
1700 digit = rtd_divmod (&r, &pten);
1701 dec_exp -= 1;
1702 gcc_assert (digit != 0);
1705 /* ... or overflow. */
1706 if (digit == 10)
1708 *p++ = '1';
1709 if (--digits > 0)
1710 *p++ = '0';
1711 dec_exp += 1;
1713 else
1715 gcc_assert (digit <= 10);
1716 *p++ = digit + '0';
1719 /* Generate subsequent digits. */
1720 while (--digits > 0)
1722 do_multiply (&r, &r, ten);
1723 digit = rtd_divmod (&r, &pten);
1724 *p++ = digit + '0';
1726 last = p;
1728 /* Generate one more digit with which to do rounding. */
1729 do_multiply (&r, &r, ten);
1730 digit = rtd_divmod (&r, &pten);
1732 /* Round the result. */
1733 if (fmt && fmt->round_towards_zero)
1735 /* If the format uses round towards zero when parsing the string
1736 back in, we need to always round away from zero here. */
1737 if (cmp_significand_0 (&r))
1738 digit++;
1739 round_up = digit > 0;
1741 else
1743 if (digit == 5)
1745 /* Round to nearest. If R is nonzero there are additional
1746 nonzero digits to be extracted. */
1747 if (cmp_significand_0 (&r))
1748 digit++;
1749 /* Round to even. */
1750 else if ((p[-1] - '0') & 1)
1751 digit++;
1754 round_up = digit > 5;
1757 if (round_up)
1759 while (p > first)
1761 digit = *--p;
1762 if (digit == '9')
1763 *p = '0';
1764 else
1766 *p = digit + 1;
1767 break;
1771 /* Carry out of the first digit. This means we had all 9's and
1772 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1773 if (p == first)
1775 first[1] = '1';
1776 dec_exp++;
1780 /* Insert the decimal point. */
1781 first[0] = first[1];
1782 first[1] = '.';
1784 /* If requested, drop trailing zeros. Never crop past "1.0". */
1785 if (crop_trailing_zeros)
1786 while (last > first + 3 && last[-1] == '0')
1787 last--;
1789 /* Append the exponent. */
1790 sprintf (last, "e%+d", dec_exp);
1792 #ifdef ENABLE_CHECKING
1793 /* Verify that we can read the original value back in. */
1794 if (mode != VOIDmode)
1796 real_from_string (&r, str);
1797 real_convert (&r, mode, &r);
1798 gcc_assert (real_identical (&r, r_orig));
1800 #endif
1803 /* Likewise, except always uses round-to-nearest. */
1805 void
1806 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1807 size_t digits, int crop_trailing_zeros)
1809 real_to_decimal_for_mode (str, r_orig, buf_size,
1810 digits, crop_trailing_zeros, VOIDmode);
1813 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1814 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1815 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1816 strip trailing zeros. */
1818 void
1819 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1820 size_t digits, int crop_trailing_zeros)
1822 int i, j, exp = REAL_EXP (r);
1823 char *p, *first;
1824 char exp_buf[16];
1825 size_t max_digits;
1827 switch (r->cl)
1829 case rvc_zero:
1830 exp = 0;
1831 break;
1832 case rvc_normal:
1833 break;
1834 case rvc_inf:
1835 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1836 return;
1837 case rvc_nan:
1838 /* ??? Print the significand as well, if not canonical? */
1839 sprintf (str, "%c%cNaN", (r->sign ? '-' : '+'),
1840 (r->signalling ? 'S' : 'Q'));
1841 return;
1842 default:
1843 gcc_unreachable ();
1846 if (r->decimal)
1848 /* Hexadecimal format for decimal floats is not interesting. */
1849 strcpy (str, "N/A");
1850 return;
1853 if (digits == 0)
1854 digits = SIGNIFICAND_BITS / 4;
1856 /* Bound the number of digits printed by the size of the output buffer. */
1858 sprintf (exp_buf, "p%+d", exp);
1859 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1860 gcc_assert (max_digits <= buf_size);
1861 if (digits > max_digits)
1862 digits = max_digits;
1864 p = str;
1865 if (r->sign)
1866 *p++ = '-';
1867 *p++ = '0';
1868 *p++ = 'x';
1869 *p++ = '0';
1870 *p++ = '.';
1871 first = p;
1873 for (i = SIGSZ - 1; i >= 0; --i)
1874 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1876 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1877 if (--digits == 0)
1878 goto out;
1881 out:
1882 if (crop_trailing_zeros)
1883 while (p > first + 1 && p[-1] == '0')
1884 p--;
1886 sprintf (p, "p%+d", exp);
1889 /* Initialize R from a decimal or hexadecimal string. The string is
1890 assumed to have been syntax checked already. Return -1 if the
1891 value underflows, +1 if overflows, and 0 otherwise. */
1894 real_from_string (REAL_VALUE_TYPE *r, const char *str)
1896 int exp = 0;
1897 bool sign = false;
1899 get_zero (r, 0);
1901 if (*str == '-')
1903 sign = true;
1904 str++;
1906 else if (*str == '+')
1907 str++;
1909 if (!strncmp (str, "QNaN", 4))
1911 get_canonical_qnan (r, sign);
1912 return 0;
1914 else if (!strncmp (str, "SNaN", 4))
1916 get_canonical_snan (r, sign);
1917 return 0;
1919 else if (!strncmp (str, "Inf", 3))
1921 get_inf (r, sign);
1922 return 0;
1925 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1927 /* Hexadecimal floating point. */
1928 int pos = SIGNIFICAND_BITS - 4, d;
1930 str += 2;
1932 while (*str == '0')
1933 str++;
1934 while (1)
1936 d = hex_value (*str);
1937 if (d == _hex_bad)
1938 break;
1939 if (pos >= 0)
1941 r->sig[pos / HOST_BITS_PER_LONG]
1942 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1943 pos -= 4;
1945 else if (d)
1946 /* Ensure correct rounding by setting last bit if there is
1947 a subsequent nonzero digit. */
1948 r->sig[0] |= 1;
1949 exp += 4;
1950 str++;
1952 if (*str == '.')
1954 str++;
1955 if (pos == SIGNIFICAND_BITS - 4)
1957 while (*str == '0')
1958 str++, exp -= 4;
1960 while (1)
1962 d = hex_value (*str);
1963 if (d == _hex_bad)
1964 break;
1965 if (pos >= 0)
1967 r->sig[pos / HOST_BITS_PER_LONG]
1968 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1969 pos -= 4;
1971 else if (d)
1972 /* Ensure correct rounding by setting last bit if there is
1973 a subsequent nonzero digit. */
1974 r->sig[0] |= 1;
1975 str++;
1979 /* If the mantissa is zero, ignore the exponent. */
1980 if (!cmp_significand_0 (r))
1981 goto is_a_zero;
1983 if (*str == 'p' || *str == 'P')
1985 bool exp_neg = false;
1987 str++;
1988 if (*str == '-')
1990 exp_neg = true;
1991 str++;
1993 else if (*str == '+')
1994 str++;
1996 d = 0;
1997 while (ISDIGIT (*str))
1999 d *= 10;
2000 d += *str - '0';
2001 if (d > MAX_EXP)
2003 /* Overflowed the exponent. */
2004 if (exp_neg)
2005 goto underflow;
2006 else
2007 goto overflow;
2009 str++;
2011 if (exp_neg)
2012 d = -d;
2014 exp += d;
2017 r->cl = rvc_normal;
2018 SET_REAL_EXP (r, exp);
2020 normalize (r);
2022 else
2024 /* Decimal floating point. */
2025 const char *cstr = str;
2026 mpfr_t m;
2027 bool inexact;
2029 while (*cstr == '0')
2030 cstr++;
2031 if (*cstr == '.')
2033 cstr++;
2034 while (*cstr == '0')
2035 cstr++;
2038 /* If the mantissa is zero, ignore the exponent. */
2039 if (!ISDIGIT (*cstr))
2040 goto is_a_zero;
2042 /* Nonzero value, possibly overflowing or underflowing. */
2043 mpfr_init2 (m, SIGNIFICAND_BITS);
2044 inexact = mpfr_strtofr (m, str, NULL, 10, GMP_RNDZ);
2045 /* The result should never be a NaN, and because the rounding is
2046 toward zero should never be an infinity. */
2047 gcc_assert (!mpfr_nan_p (m) && !mpfr_inf_p (m));
2048 if (mpfr_zero_p (m) || mpfr_get_exp (m) < -MAX_EXP + 4)
2050 mpfr_clear (m);
2051 goto underflow;
2053 else if (mpfr_get_exp (m) > MAX_EXP - 4)
2055 mpfr_clear (m);
2056 goto overflow;
2058 else
2060 real_from_mpfr (r, m, NULL_TREE, GMP_RNDZ);
2061 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2062 because the hex digits used in real_from_mpfr did not
2063 start with a digit 8 to f, but the exponent bounds above
2064 should have avoided underflow or overflow. */
2065 gcc_assert (r->cl = rvc_normal);
2066 /* Set a sticky bit if mpfr_strtofr was inexact. */
2067 r->sig[0] |= inexact;
2071 r->sign = sign;
2072 return 0;
2074 is_a_zero:
2075 get_zero (r, sign);
2076 return 0;
2078 underflow:
2079 get_zero (r, sign);
2080 return -1;
2082 overflow:
2083 get_inf (r, sign);
2084 return 1;
2087 /* Legacy. Similar, but return the result directly. */
2089 REAL_VALUE_TYPE
2090 real_from_string2 (const char *s, enum machine_mode mode)
2092 REAL_VALUE_TYPE r;
2094 real_from_string (&r, s);
2095 if (mode != VOIDmode)
2096 real_convert (&r, mode, &r);
2098 return r;
2101 /* Initialize R from string S and desired MODE. */
2103 void
2104 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, enum machine_mode mode)
2106 if (DECIMAL_FLOAT_MODE_P (mode))
2107 decimal_real_from_string (r, s);
2108 else
2109 real_from_string (r, s);
2111 if (mode != VOIDmode)
2112 real_convert (r, mode, r);
2115 /* Initialize R from the integer pair HIGH+LOW. */
2117 void
2118 real_from_integer (REAL_VALUE_TYPE *r, enum machine_mode mode,
2119 unsigned HOST_WIDE_INT low, HOST_WIDE_INT high,
2120 int unsigned_p)
2122 if (low == 0 && high == 0)
2123 get_zero (r, 0);
2124 else
2126 memset (r, 0, sizeof (*r));
2127 r->cl = rvc_normal;
2128 r->sign = high < 0 && !unsigned_p;
2129 SET_REAL_EXP (r, HOST_BITS_PER_DOUBLE_INT);
2131 if (r->sign)
2133 high = ~high;
2134 if (low == 0)
2135 high += 1;
2136 else
2137 low = -low;
2140 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2142 r->sig[SIGSZ-1] = high;
2143 r->sig[SIGSZ-2] = low;
2145 else
2147 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2148 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
2149 r->sig[SIGSZ-2] = high;
2150 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
2151 r->sig[SIGSZ-4] = low;
2154 normalize (r);
2157 if (DECIMAL_FLOAT_MODE_P (mode))
2158 decimal_from_integer (r);
2159 else if (mode != VOIDmode)
2160 real_convert (r, mode, r);
2163 /* Render R, an integral value, as a floating point constant with no
2164 specified exponent. */
2166 static void
2167 decimal_integer_string (char *str, const REAL_VALUE_TYPE *r_orig,
2168 size_t buf_size)
2170 int dec_exp, digit, digits;
2171 REAL_VALUE_TYPE r, pten;
2172 char *p;
2173 bool sign;
2175 r = *r_orig;
2177 if (r.cl == rvc_zero)
2179 strcpy (str, "0.");
2180 return;
2183 sign = r.sign;
2184 r.sign = 0;
2186 dec_exp = REAL_EXP (&r) * M_LOG10_2;
2187 digits = dec_exp + 1;
2188 gcc_assert ((digits + 2) < (int)buf_size);
2190 pten = *real_digit (1);
2191 times_pten (&pten, dec_exp);
2193 p = str;
2194 if (sign)
2195 *p++ = '-';
2197 digit = rtd_divmod (&r, &pten);
2198 gcc_assert (digit >= 0 && digit <= 9);
2199 *p++ = digit + '0';
2200 while (--digits > 0)
2202 times_pten (&r, 1);
2203 digit = rtd_divmod (&r, &pten);
2204 *p++ = digit + '0';
2206 *p++ = '.';
2207 *p++ = '\0';
2210 /* Convert a real with an integral value to decimal float. */
2212 static void
2213 decimal_from_integer (REAL_VALUE_TYPE *r)
2215 char str[256];
2217 decimal_integer_string (str, r, sizeof (str) - 1);
2218 decimal_real_from_string (r, str);
2221 /* Returns 10**2**N. */
2223 static const REAL_VALUE_TYPE *
2224 ten_to_ptwo (int n)
2226 static REAL_VALUE_TYPE tens[EXP_BITS];
2228 gcc_assert (n >= 0);
2229 gcc_assert (n < EXP_BITS);
2231 if (tens[n].cl == rvc_zero)
2233 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2235 HOST_WIDE_INT t = 10;
2236 int i;
2238 for (i = 0; i < n; ++i)
2239 t *= t;
2241 real_from_integer (&tens[n], VOIDmode, t, 0, 1);
2243 else
2245 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2246 do_multiply (&tens[n], t, t);
2250 return &tens[n];
2253 /* Returns 10**(-2**N). */
2255 static const REAL_VALUE_TYPE *
2256 ten_to_mptwo (int n)
2258 static REAL_VALUE_TYPE tens[EXP_BITS];
2260 gcc_assert (n >= 0);
2261 gcc_assert (n < EXP_BITS);
2263 if (tens[n].cl == rvc_zero)
2264 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2266 return &tens[n];
2269 /* Returns N. */
2271 static const REAL_VALUE_TYPE *
2272 real_digit (int n)
2274 static REAL_VALUE_TYPE num[10];
2276 gcc_assert (n >= 0);
2277 gcc_assert (n <= 9);
2279 if (n > 0 && num[n].cl == rvc_zero)
2280 real_from_integer (&num[n], VOIDmode, n, 0, 1);
2282 return &num[n];
2285 /* Multiply R by 10**EXP. */
2287 static void
2288 times_pten (REAL_VALUE_TYPE *r, int exp)
2290 REAL_VALUE_TYPE pten, *rr;
2291 bool negative = (exp < 0);
2292 int i;
2294 if (negative)
2296 exp = -exp;
2297 pten = *real_digit (1);
2298 rr = &pten;
2300 else
2301 rr = r;
2303 for (i = 0; exp > 0; ++i, exp >>= 1)
2304 if (exp & 1)
2305 do_multiply (rr, rr, ten_to_ptwo (i));
2307 if (negative)
2308 do_divide (r, r, &pten);
2311 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2313 const REAL_VALUE_TYPE *
2314 dconst_e_ptr (void)
2316 static REAL_VALUE_TYPE value;
2318 /* Initialize mathematical constants for constant folding builtins.
2319 These constants need to be given to at least 160 bits precision. */
2320 if (value.cl == rvc_zero)
2322 mpfr_t m;
2323 mpfr_init2 (m, SIGNIFICAND_BITS);
2324 mpfr_set_ui (m, 1, GMP_RNDN);
2325 mpfr_exp (m, m, GMP_RNDN);
2326 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2327 mpfr_clear (m);
2330 return &value;
2333 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2335 const REAL_VALUE_TYPE *
2336 dconst_third_ptr (void)
2338 static REAL_VALUE_TYPE value;
2340 /* Initialize mathematical constants for constant folding builtins.
2341 These constants need to be given to at least 160 bits precision. */
2342 if (value.cl == rvc_zero)
2344 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (3));
2346 return &value;
2349 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2351 const REAL_VALUE_TYPE *
2352 dconst_sqrt2_ptr (void)
2354 static REAL_VALUE_TYPE value;
2356 /* Initialize mathematical constants for constant folding builtins.
2357 These constants need to be given to at least 160 bits precision. */
2358 if (value.cl == rvc_zero)
2360 mpfr_t m;
2361 mpfr_init2 (m, SIGNIFICAND_BITS);
2362 mpfr_sqrt_ui (m, 2, GMP_RNDN);
2363 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2364 mpfr_clear (m);
2366 return &value;
2369 /* Fills R with +Inf. */
2371 void
2372 real_inf (REAL_VALUE_TYPE *r)
2374 get_inf (r, 0);
2377 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2378 we force a QNaN, else we force an SNaN. The string, if not empty,
2379 is parsed as a number and placed in the significand. Return true
2380 if the string was successfully parsed. */
2382 bool
2383 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2384 enum machine_mode mode)
2386 const struct real_format *fmt;
2388 fmt = REAL_MODE_FORMAT (mode);
2389 gcc_assert (fmt);
2391 if (*str == 0)
2393 if (quiet)
2394 get_canonical_qnan (r, 0);
2395 else
2396 get_canonical_snan (r, 0);
2398 else
2400 int base = 10, d;
2402 memset (r, 0, sizeof (*r));
2403 r->cl = rvc_nan;
2405 /* Parse akin to strtol into the significand of R. */
2407 while (ISSPACE (*str))
2408 str++;
2409 if (*str == '-')
2410 str++;
2411 else if (*str == '+')
2412 str++;
2413 if (*str == '0')
2415 str++;
2416 if (*str == 'x' || *str == 'X')
2418 base = 16;
2419 str++;
2421 else
2422 base = 8;
2425 while ((d = hex_value (*str)) < base)
2427 REAL_VALUE_TYPE u;
2429 switch (base)
2431 case 8:
2432 lshift_significand (r, r, 3);
2433 break;
2434 case 16:
2435 lshift_significand (r, r, 4);
2436 break;
2437 case 10:
2438 lshift_significand_1 (&u, r);
2439 lshift_significand (r, r, 3);
2440 add_significands (r, r, &u);
2441 break;
2442 default:
2443 gcc_unreachable ();
2446 get_zero (&u, 0);
2447 u.sig[0] = d;
2448 add_significands (r, r, &u);
2450 str++;
2453 /* Must have consumed the entire string for success. */
2454 if (*str != 0)
2455 return false;
2457 /* Shift the significand into place such that the bits
2458 are in the most significant bits for the format. */
2459 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2461 /* Our MSB is always unset for NaNs. */
2462 r->sig[SIGSZ-1] &= ~SIG_MSB;
2464 /* Force quiet or signalling NaN. */
2465 r->signalling = !quiet;
2468 return true;
2471 /* Fills R with the largest finite value representable in mode MODE.
2472 If SIGN is nonzero, R is set to the most negative finite value. */
2474 void
2475 real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
2477 const struct real_format *fmt;
2478 int np2;
2480 fmt = REAL_MODE_FORMAT (mode);
2481 gcc_assert (fmt);
2482 memset (r, 0, sizeof (*r));
2484 if (fmt->b == 10)
2485 decimal_real_maxval (r, sign, mode);
2486 else
2488 r->cl = rvc_normal;
2489 r->sign = sign;
2490 SET_REAL_EXP (r, fmt->emax);
2492 np2 = SIGNIFICAND_BITS - fmt->p;
2493 memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2494 clear_significand_below (r, np2);
2496 if (fmt->pnan < fmt->p)
2497 /* This is an IBM extended double format made up of two IEEE
2498 doubles. The value of the long double is the sum of the
2499 values of the two parts. The most significant part is
2500 required to be the value of the long double rounded to the
2501 nearest double. Rounding means we need a slightly smaller
2502 value for LDBL_MAX. */
2503 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1);
2507 /* Fills R with 2**N. */
2509 void
2510 real_2expN (REAL_VALUE_TYPE *r, int n, enum machine_mode fmode)
2512 memset (r, 0, sizeof (*r));
2514 n++;
2515 if (n > MAX_EXP)
2516 r->cl = rvc_inf;
2517 else if (n < -MAX_EXP)
2519 else
2521 r->cl = rvc_normal;
2522 SET_REAL_EXP (r, n);
2523 r->sig[SIGSZ-1] = SIG_MSB;
2525 if (DECIMAL_FLOAT_MODE_P (fmode))
2526 decimal_real_convert (r, fmode, r);
2530 static void
2531 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2533 int p2, np2, i, w;
2534 int emin2m1, emax2;
2535 bool round_up = false;
2537 if (r->decimal)
2539 if (fmt->b == 10)
2541 decimal_round_for_format (fmt, r);
2542 return;
2544 /* FIXME. We can come here via fp_easy_constant
2545 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2546 investigated whether this convert needs to be here, or
2547 something else is missing. */
2548 decimal_real_convert (r, DFmode, r);
2551 p2 = fmt->p;
2552 emin2m1 = fmt->emin - 1;
2553 emax2 = fmt->emax;
2555 np2 = SIGNIFICAND_BITS - p2;
2556 switch (r->cl)
2558 underflow:
2559 get_zero (r, r->sign);
2560 case rvc_zero:
2561 if (!fmt->has_signed_zero)
2562 r->sign = 0;
2563 return;
2565 overflow:
2566 get_inf (r, r->sign);
2567 case rvc_inf:
2568 return;
2570 case rvc_nan:
2571 clear_significand_below (r, np2);
2572 return;
2574 case rvc_normal:
2575 break;
2577 default:
2578 gcc_unreachable ();
2581 /* Check the range of the exponent. If we're out of range,
2582 either underflow or overflow. */
2583 if (REAL_EXP (r) > emax2)
2584 goto overflow;
2585 else if (REAL_EXP (r) <= emin2m1)
2587 int diff;
2589 if (!fmt->has_denorm)
2591 /* Don't underflow completely until we've had a chance to round. */
2592 if (REAL_EXP (r) < emin2m1)
2593 goto underflow;
2595 else
2597 diff = emin2m1 - REAL_EXP (r) + 1;
2598 if (diff > p2)
2599 goto underflow;
2601 /* De-normalize the significand. */
2602 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2603 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2607 if (!fmt->round_towards_zero)
2609 /* There are P2 true significand bits, followed by one guard bit,
2610 followed by one sticky bit, followed by stuff. Fold nonzero
2611 stuff into the sticky bit. */
2612 unsigned long sticky;
2613 bool guard, lsb;
2615 sticky = 0;
2616 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2617 sticky |= r->sig[i];
2618 sticky |= r->sig[w]
2619 & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2621 guard = test_significand_bit (r, np2 - 1);
2622 lsb = test_significand_bit (r, np2);
2624 /* Round to even. */
2625 round_up = guard && (sticky || lsb);
2628 if (round_up)
2630 REAL_VALUE_TYPE u;
2631 get_zero (&u, 0);
2632 set_significand_bit (&u, np2);
2634 if (add_significands (r, r, &u))
2636 /* Overflow. Means the significand had been all ones, and
2637 is now all zeros. Need to increase the exponent, and
2638 possibly re-normalize it. */
2639 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2640 if (REAL_EXP (r) > emax2)
2641 goto overflow;
2642 r->sig[SIGSZ-1] = SIG_MSB;
2646 /* Catch underflow that we deferred until after rounding. */
2647 if (REAL_EXP (r) <= emin2m1)
2648 goto underflow;
2650 /* Clear out trailing garbage. */
2651 clear_significand_below (r, np2);
2654 /* Extend or truncate to a new mode. */
2656 void
2657 real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
2658 const REAL_VALUE_TYPE *a)
2660 const struct real_format *fmt;
2662 fmt = REAL_MODE_FORMAT (mode);
2663 gcc_assert (fmt);
2665 *r = *a;
2667 if (a->decimal || fmt->b == 10)
2668 decimal_real_convert (r, mode, a);
2670 round_for_format (fmt, r);
2672 /* round_for_format de-normalizes denormals. Undo just that part. */
2673 if (r->cl == rvc_normal)
2674 normalize (r);
2677 /* Legacy. Likewise, except return the struct directly. */
2679 REAL_VALUE_TYPE
2680 real_value_truncate (enum machine_mode mode, REAL_VALUE_TYPE a)
2682 REAL_VALUE_TYPE r;
2683 real_convert (&r, mode, &a);
2684 return r;
2687 /* Return true if truncating to MODE is exact. */
2689 bool
2690 exact_real_truncate (enum machine_mode mode, const REAL_VALUE_TYPE *a)
2692 const struct real_format *fmt;
2693 REAL_VALUE_TYPE t;
2694 int emin2m1;
2696 fmt = REAL_MODE_FORMAT (mode);
2697 gcc_assert (fmt);
2699 /* Don't allow conversion to denormals. */
2700 emin2m1 = fmt->emin - 1;
2701 if (REAL_EXP (a) <= emin2m1)
2702 return false;
2704 /* After conversion to the new mode, the value must be identical. */
2705 real_convert (&t, mode, a);
2706 return real_identical (&t, a);
2709 /* Write R to the given target format. Place the words of the result
2710 in target word order in BUF. There are always 32 bits in each
2711 long, no matter the size of the host long.
2713 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2715 long
2716 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
2717 const struct real_format *fmt)
2719 REAL_VALUE_TYPE r;
2720 long buf1;
2722 r = *r_orig;
2723 round_for_format (fmt, &r);
2725 if (!buf)
2726 buf = &buf1;
2727 (*fmt->encode) (fmt, buf, &r);
2729 return *buf;
2732 /* Similar, but look up the format from MODE. */
2734 long
2735 real_to_target (long *buf, const REAL_VALUE_TYPE *r, enum machine_mode mode)
2737 const struct real_format *fmt;
2739 fmt = REAL_MODE_FORMAT (mode);
2740 gcc_assert (fmt);
2742 return real_to_target_fmt (buf, r, fmt);
2745 /* Read R from the given target format. Read the words of the result
2746 in target word order in BUF. There are always 32 bits in each
2747 long, no matter the size of the host long. */
2749 void
2750 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
2751 const struct real_format *fmt)
2753 (*fmt->decode) (fmt, r, buf);
2756 /* Similar, but look up the format from MODE. */
2758 void
2759 real_from_target (REAL_VALUE_TYPE *r, const long *buf, enum machine_mode mode)
2761 const struct real_format *fmt;
2763 fmt = REAL_MODE_FORMAT (mode);
2764 gcc_assert (fmt);
2766 (*fmt->decode) (fmt, r, buf);
2769 /* Return the number of bits of the largest binary value that the
2770 significand of MODE will hold. */
2771 /* ??? Legacy. Should get access to real_format directly. */
2774 significand_size (enum machine_mode mode)
2776 const struct real_format *fmt;
2778 fmt = REAL_MODE_FORMAT (mode);
2779 if (fmt == NULL)
2780 return 0;
2782 if (fmt->b == 10)
2784 /* Return the size in bits of the largest binary value that can be
2785 held by the decimal coefficient for this mode. This is one more
2786 than the number of bits required to hold the largest coefficient
2787 of this mode. */
2788 double log2_10 = 3.3219281;
2789 return fmt->p * log2_10;
2791 return fmt->p;
2794 /* Return a hash value for the given real value. */
2795 /* ??? The "unsigned int" return value is intended to be hashval_t,
2796 but I didn't want to pull hashtab.h into real.h. */
2798 unsigned int
2799 real_hash (const REAL_VALUE_TYPE *r)
2801 unsigned int h;
2802 size_t i;
2804 h = r->cl | (r->sign << 2);
2805 switch (r->cl)
2807 case rvc_zero:
2808 case rvc_inf:
2809 return h;
2811 case rvc_normal:
2812 h |= REAL_EXP (r) << 3;
2813 break;
2815 case rvc_nan:
2816 if (r->signalling)
2817 h ^= (unsigned int)-1;
2818 if (r->canonical)
2819 return h;
2820 break;
2822 default:
2823 gcc_unreachable ();
2826 if (sizeof (unsigned long) > sizeof (unsigned int))
2827 for (i = 0; i < SIGSZ; ++i)
2829 unsigned long s = r->sig[i];
2830 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2832 else
2833 for (i = 0; i < SIGSZ; ++i)
2834 h ^= r->sig[i];
2836 return h;
2839 /* IEEE single-precision format. */
2841 static void encode_ieee_single (const struct real_format *fmt,
2842 long *, const REAL_VALUE_TYPE *);
2843 static void decode_ieee_single (const struct real_format *,
2844 REAL_VALUE_TYPE *, const long *);
2846 static void
2847 encode_ieee_single (const struct real_format *fmt, long *buf,
2848 const REAL_VALUE_TYPE *r)
2850 unsigned long image, sig, exp;
2851 unsigned long sign = r->sign;
2852 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2854 image = sign << 31;
2855 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2857 switch (r->cl)
2859 case rvc_zero:
2860 break;
2862 case rvc_inf:
2863 if (fmt->has_inf)
2864 image |= 255 << 23;
2865 else
2866 image |= 0x7fffffff;
2867 break;
2869 case rvc_nan:
2870 if (fmt->has_nans)
2872 if (r->canonical)
2873 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
2874 if (r->signalling == fmt->qnan_msb_set)
2875 sig &= ~(1 << 22);
2876 else
2877 sig |= 1 << 22;
2878 if (sig == 0)
2879 sig = 1 << 21;
2881 image |= 255 << 23;
2882 image |= sig;
2884 else
2885 image |= 0x7fffffff;
2886 break;
2888 case rvc_normal:
2889 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2890 whereas the intermediate representation is 0.F x 2**exp.
2891 Which means we're off by one. */
2892 if (denormal)
2893 exp = 0;
2894 else
2895 exp = REAL_EXP (r) + 127 - 1;
2896 image |= exp << 23;
2897 image |= sig;
2898 break;
2900 default:
2901 gcc_unreachable ();
2904 buf[0] = image;
2907 static void
2908 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2909 const long *buf)
2911 unsigned long image = buf[0] & 0xffffffff;
2912 bool sign = (image >> 31) & 1;
2913 int exp = (image >> 23) & 0xff;
2915 memset (r, 0, sizeof (*r));
2916 image <<= HOST_BITS_PER_LONG - 24;
2917 image &= ~SIG_MSB;
2919 if (exp == 0)
2921 if (image && fmt->has_denorm)
2923 r->cl = rvc_normal;
2924 r->sign = sign;
2925 SET_REAL_EXP (r, -126);
2926 r->sig[SIGSZ-1] = image << 1;
2927 normalize (r);
2929 else if (fmt->has_signed_zero)
2930 r->sign = sign;
2932 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2934 if (image)
2936 r->cl = rvc_nan;
2937 r->sign = sign;
2938 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
2939 ^ fmt->qnan_msb_set);
2940 r->sig[SIGSZ-1] = image;
2942 else
2944 r->cl = rvc_inf;
2945 r->sign = sign;
2948 else
2950 r->cl = rvc_normal;
2951 r->sign = sign;
2952 SET_REAL_EXP (r, exp - 127 + 1);
2953 r->sig[SIGSZ-1] = image | SIG_MSB;
2957 const struct real_format ieee_single_format =
2959 encode_ieee_single,
2960 decode_ieee_single,
2964 -125,
2965 128,
2968 false,
2969 true,
2970 true,
2971 true,
2972 true,
2973 true,
2974 true,
2975 false
2978 const struct real_format mips_single_format =
2980 encode_ieee_single,
2981 decode_ieee_single,
2985 -125,
2986 128,
2989 false,
2990 true,
2991 true,
2992 true,
2993 true,
2994 true,
2995 false,
2996 true
2999 const struct real_format motorola_single_format =
3001 encode_ieee_single,
3002 decode_ieee_single,
3006 -125,
3007 128,
3010 false,
3011 true,
3012 true,
3013 true,
3014 true,
3015 true,
3016 true,
3017 true
3020 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3021 single precision with the following differences:
3022 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3023 are generated.
3024 - NaNs are not supported.
3025 - The range of non-zero numbers in binary is
3026 (001)[1.]000...000 to (255)[1.]111...111.
3027 - Denormals can be represented, but are treated as +0.0 when
3028 used as an operand and are never generated as a result.
3029 - -0.0 can be represented, but a zero result is always +0.0.
3030 - the only supported rounding mode is trunction (towards zero). */
3031 const struct real_format spu_single_format =
3033 encode_ieee_single,
3034 decode_ieee_single,
3038 -125,
3039 129,
3042 true,
3043 false,
3044 false,
3045 false,
3046 true,
3047 true,
3048 false,
3049 false
3052 /* IEEE double-precision format. */
3054 static void encode_ieee_double (const struct real_format *fmt,
3055 long *, const REAL_VALUE_TYPE *);
3056 static void decode_ieee_double (const struct real_format *,
3057 REAL_VALUE_TYPE *, const long *);
3059 static void
3060 encode_ieee_double (const struct real_format *fmt, long *buf,
3061 const REAL_VALUE_TYPE *r)
3063 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
3064 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3066 image_hi = r->sign << 31;
3067 image_lo = 0;
3069 if (HOST_BITS_PER_LONG == 64)
3071 sig_hi = r->sig[SIGSZ-1];
3072 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
3073 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
3075 else
3077 sig_hi = r->sig[SIGSZ-1];
3078 sig_lo = r->sig[SIGSZ-2];
3079 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
3080 sig_hi = (sig_hi >> 11) & 0xfffff;
3083 switch (r->cl)
3085 case rvc_zero:
3086 break;
3088 case rvc_inf:
3089 if (fmt->has_inf)
3090 image_hi |= 2047 << 20;
3091 else
3093 image_hi |= 0x7fffffff;
3094 image_lo = 0xffffffff;
3096 break;
3098 case rvc_nan:
3099 if (fmt->has_nans)
3101 if (r->canonical)
3103 if (fmt->canonical_nan_lsbs_set)
3105 sig_hi = (1 << 19) - 1;
3106 sig_lo = 0xffffffff;
3108 else
3110 sig_hi = 0;
3111 sig_lo = 0;
3114 if (r->signalling == fmt->qnan_msb_set)
3115 sig_hi &= ~(1 << 19);
3116 else
3117 sig_hi |= 1 << 19;
3118 if (sig_hi == 0 && sig_lo == 0)
3119 sig_hi = 1 << 18;
3121 image_hi |= 2047 << 20;
3122 image_hi |= sig_hi;
3123 image_lo = sig_lo;
3125 else
3127 image_hi |= 0x7fffffff;
3128 image_lo = 0xffffffff;
3130 break;
3132 case rvc_normal:
3133 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3134 whereas the intermediate representation is 0.F x 2**exp.
3135 Which means we're off by one. */
3136 if (denormal)
3137 exp = 0;
3138 else
3139 exp = REAL_EXP (r) + 1023 - 1;
3140 image_hi |= exp << 20;
3141 image_hi |= sig_hi;
3142 image_lo = sig_lo;
3143 break;
3145 default:
3146 gcc_unreachable ();
3149 if (FLOAT_WORDS_BIG_ENDIAN)
3150 buf[0] = image_hi, buf[1] = image_lo;
3151 else
3152 buf[0] = image_lo, buf[1] = image_hi;
3155 static void
3156 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3157 const long *buf)
3159 unsigned long image_hi, image_lo;
3160 bool sign;
3161 int exp;
3163 if (FLOAT_WORDS_BIG_ENDIAN)
3164 image_hi = buf[0], image_lo = buf[1];
3165 else
3166 image_lo = buf[0], image_hi = buf[1];
3167 image_lo &= 0xffffffff;
3168 image_hi &= 0xffffffff;
3170 sign = (image_hi >> 31) & 1;
3171 exp = (image_hi >> 20) & 0x7ff;
3173 memset (r, 0, sizeof (*r));
3175 image_hi <<= 32 - 21;
3176 image_hi |= image_lo >> 21;
3177 image_hi &= 0x7fffffff;
3178 image_lo <<= 32 - 21;
3180 if (exp == 0)
3182 if ((image_hi || image_lo) && fmt->has_denorm)
3184 r->cl = rvc_normal;
3185 r->sign = sign;
3186 SET_REAL_EXP (r, -1022);
3187 if (HOST_BITS_PER_LONG == 32)
3189 image_hi = (image_hi << 1) | (image_lo >> 31);
3190 image_lo <<= 1;
3191 r->sig[SIGSZ-1] = image_hi;
3192 r->sig[SIGSZ-2] = image_lo;
3194 else
3196 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
3197 r->sig[SIGSZ-1] = image_hi;
3199 normalize (r);
3201 else if (fmt->has_signed_zero)
3202 r->sign = sign;
3204 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
3206 if (image_hi || image_lo)
3208 r->cl = rvc_nan;
3209 r->sign = sign;
3210 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3211 if (HOST_BITS_PER_LONG == 32)
3213 r->sig[SIGSZ-1] = image_hi;
3214 r->sig[SIGSZ-2] = image_lo;
3216 else
3217 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3219 else
3221 r->cl = rvc_inf;
3222 r->sign = sign;
3225 else
3227 r->cl = rvc_normal;
3228 r->sign = sign;
3229 SET_REAL_EXP (r, exp - 1023 + 1);
3230 if (HOST_BITS_PER_LONG == 32)
3232 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3233 r->sig[SIGSZ-2] = image_lo;
3235 else
3236 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3240 const struct real_format ieee_double_format =
3242 encode_ieee_double,
3243 decode_ieee_double,
3247 -1021,
3248 1024,
3251 false,
3252 true,
3253 true,
3254 true,
3255 true,
3256 true,
3257 true,
3258 false
3261 const struct real_format mips_double_format =
3263 encode_ieee_double,
3264 decode_ieee_double,
3268 -1021,
3269 1024,
3272 false,
3273 true,
3274 true,
3275 true,
3276 true,
3277 true,
3278 false,
3279 true
3282 const struct real_format motorola_double_format =
3284 encode_ieee_double,
3285 decode_ieee_double,
3289 -1021,
3290 1024,
3293 false,
3294 true,
3295 true,
3296 true,
3297 true,
3298 true,
3299 true,
3300 true
3303 /* IEEE extended real format. This comes in three flavors: Intel's as
3304 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3305 12- and 16-byte images may be big- or little endian; Motorola's is
3306 always big endian. */
3308 /* Helper subroutine which converts from the internal format to the
3309 12-byte little-endian Intel format. Functions below adjust this
3310 for the other possible formats. */
3311 static void
3312 encode_ieee_extended (const struct real_format *fmt, long *buf,
3313 const REAL_VALUE_TYPE *r)
3315 unsigned long image_hi, sig_hi, sig_lo;
3316 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3318 image_hi = r->sign << 15;
3319 sig_hi = sig_lo = 0;
3321 switch (r->cl)
3323 case rvc_zero:
3324 break;
3326 case rvc_inf:
3327 if (fmt->has_inf)
3329 image_hi |= 32767;
3331 /* Intel requires the explicit integer bit to be set, otherwise
3332 it considers the value a "pseudo-infinity". Motorola docs
3333 say it doesn't care. */
3334 sig_hi = 0x80000000;
3336 else
3338 image_hi |= 32767;
3339 sig_lo = sig_hi = 0xffffffff;
3341 break;
3343 case rvc_nan:
3344 if (fmt->has_nans)
3346 image_hi |= 32767;
3347 if (r->canonical)
3349 if (fmt->canonical_nan_lsbs_set)
3351 sig_hi = (1 << 30) - 1;
3352 sig_lo = 0xffffffff;
3355 else if (HOST_BITS_PER_LONG == 32)
3357 sig_hi = r->sig[SIGSZ-1];
3358 sig_lo = r->sig[SIGSZ-2];
3360 else
3362 sig_lo = r->sig[SIGSZ-1];
3363 sig_hi = sig_lo >> 31 >> 1;
3364 sig_lo &= 0xffffffff;
3366 if (r->signalling == fmt->qnan_msb_set)
3367 sig_hi &= ~(1 << 30);
3368 else
3369 sig_hi |= 1 << 30;
3370 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3371 sig_hi = 1 << 29;
3373 /* Intel requires the explicit integer bit to be set, otherwise
3374 it considers the value a "pseudo-nan". Motorola docs say it
3375 doesn't care. */
3376 sig_hi |= 0x80000000;
3378 else
3380 image_hi |= 32767;
3381 sig_lo = sig_hi = 0xffffffff;
3383 break;
3385 case rvc_normal:
3387 int exp = REAL_EXP (r);
3389 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3390 whereas the intermediate representation is 0.F x 2**exp.
3391 Which means we're off by one.
3393 Except for Motorola, which consider exp=0 and explicit
3394 integer bit set to continue to be normalized. In theory
3395 this discrepancy has been taken care of by the difference
3396 in fmt->emin in round_for_format. */
3398 if (denormal)
3399 exp = 0;
3400 else
3402 exp += 16383 - 1;
3403 gcc_assert (exp >= 0);
3405 image_hi |= exp;
3407 if (HOST_BITS_PER_LONG == 32)
3409 sig_hi = r->sig[SIGSZ-1];
3410 sig_lo = r->sig[SIGSZ-2];
3412 else
3414 sig_lo = r->sig[SIGSZ-1];
3415 sig_hi = sig_lo >> 31 >> 1;
3416 sig_lo &= 0xffffffff;
3419 break;
3421 default:
3422 gcc_unreachable ();
3425 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3428 /* Convert from the internal format to the 12-byte Motorola format
3429 for an IEEE extended real. */
3430 static void
3431 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3432 const REAL_VALUE_TYPE *r)
3434 long intermed[3];
3435 encode_ieee_extended (fmt, intermed, r);
3437 /* Motorola chips are assumed always to be big-endian. Also, the
3438 padding in a Motorola extended real goes between the exponent and
3439 the mantissa. At this point the mantissa is entirely within
3440 elements 0 and 1 of intermed, and the exponent entirely within
3441 element 2, so all we have to do is swap the order around, and
3442 shift element 2 left 16 bits. */
3443 buf[0] = intermed[2] << 16;
3444 buf[1] = intermed[1];
3445 buf[2] = intermed[0];
3448 /* Convert from the internal format to the 12-byte Intel format for
3449 an IEEE extended real. */
3450 static void
3451 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3452 const REAL_VALUE_TYPE *r)
3454 if (FLOAT_WORDS_BIG_ENDIAN)
3456 /* All the padding in an Intel-format extended real goes at the high
3457 end, which in this case is after the mantissa, not the exponent.
3458 Therefore we must shift everything down 16 bits. */
3459 long intermed[3];
3460 encode_ieee_extended (fmt, intermed, r);
3461 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3462 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3463 buf[2] = (intermed[0] << 16);
3465 else
3466 /* encode_ieee_extended produces what we want directly. */
3467 encode_ieee_extended (fmt, buf, r);
3470 /* Convert from the internal format to the 16-byte Intel format for
3471 an IEEE extended real. */
3472 static void
3473 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3474 const REAL_VALUE_TYPE *r)
3476 /* All the padding in an Intel-format extended real goes at the high end. */
3477 encode_ieee_extended_intel_96 (fmt, buf, r);
3478 buf[3] = 0;
3481 /* As above, we have a helper function which converts from 12-byte
3482 little-endian Intel format to internal format. Functions below
3483 adjust for the other possible formats. */
3484 static void
3485 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3486 const long *buf)
3488 unsigned long image_hi, sig_hi, sig_lo;
3489 bool sign;
3490 int exp;
3492 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3493 sig_lo &= 0xffffffff;
3494 sig_hi &= 0xffffffff;
3495 image_hi &= 0xffffffff;
3497 sign = (image_hi >> 15) & 1;
3498 exp = image_hi & 0x7fff;
3500 memset (r, 0, sizeof (*r));
3502 if (exp == 0)
3504 if ((sig_hi || sig_lo) && fmt->has_denorm)
3506 r->cl = rvc_normal;
3507 r->sign = sign;
3509 /* When the IEEE format contains a hidden bit, we know that
3510 it's zero at this point, and so shift up the significand
3511 and decrease the exponent to match. In this case, Motorola
3512 defines the explicit integer bit to be valid, so we don't
3513 know whether the msb is set or not. */
3514 SET_REAL_EXP (r, fmt->emin);
3515 if (HOST_BITS_PER_LONG == 32)
3517 r->sig[SIGSZ-1] = sig_hi;
3518 r->sig[SIGSZ-2] = sig_lo;
3520 else
3521 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3523 normalize (r);
3525 else if (fmt->has_signed_zero)
3526 r->sign = sign;
3528 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3530 /* See above re "pseudo-infinities" and "pseudo-nans".
3531 Short summary is that the MSB will likely always be
3532 set, and that we don't care about it. */
3533 sig_hi &= 0x7fffffff;
3535 if (sig_hi || sig_lo)
3537 r->cl = rvc_nan;
3538 r->sign = sign;
3539 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3540 if (HOST_BITS_PER_LONG == 32)
3542 r->sig[SIGSZ-1] = sig_hi;
3543 r->sig[SIGSZ-2] = sig_lo;
3545 else
3546 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3548 else
3550 r->cl = rvc_inf;
3551 r->sign = sign;
3554 else
3556 r->cl = rvc_normal;
3557 r->sign = sign;
3558 SET_REAL_EXP (r, exp - 16383 + 1);
3559 if (HOST_BITS_PER_LONG == 32)
3561 r->sig[SIGSZ-1] = sig_hi;
3562 r->sig[SIGSZ-2] = sig_lo;
3564 else
3565 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3569 /* Convert from the internal format to the 12-byte Motorola format
3570 for an IEEE extended real. */
3571 static void
3572 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3573 const long *buf)
3575 long intermed[3];
3577 /* Motorola chips are assumed always to be big-endian. Also, the
3578 padding in a Motorola extended real goes between the exponent and
3579 the mantissa; remove it. */
3580 intermed[0] = buf[2];
3581 intermed[1] = buf[1];
3582 intermed[2] = (unsigned long)buf[0] >> 16;
3584 decode_ieee_extended (fmt, r, intermed);
3587 /* Convert from the internal format to the 12-byte Intel format for
3588 an IEEE extended real. */
3589 static void
3590 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3591 const long *buf)
3593 if (FLOAT_WORDS_BIG_ENDIAN)
3595 /* All the padding in an Intel-format extended real goes at the high
3596 end, which in this case is after the mantissa, not the exponent.
3597 Therefore we must shift everything up 16 bits. */
3598 long intermed[3];
3600 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3601 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3602 intermed[2] = ((unsigned long)buf[0] >> 16);
3604 decode_ieee_extended (fmt, r, intermed);
3606 else
3607 /* decode_ieee_extended produces what we want directly. */
3608 decode_ieee_extended (fmt, r, buf);
3611 /* Convert from the internal format to the 16-byte Intel format for
3612 an IEEE extended real. */
3613 static void
3614 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3615 const long *buf)
3617 /* All the padding in an Intel-format extended real goes at the high end. */
3618 decode_ieee_extended_intel_96 (fmt, r, buf);
3621 const struct real_format ieee_extended_motorola_format =
3623 encode_ieee_extended_motorola,
3624 decode_ieee_extended_motorola,
3628 -16382,
3629 16384,
3632 false,
3633 true,
3634 true,
3635 true,
3636 true,
3637 true,
3638 true,
3639 true
3642 const struct real_format ieee_extended_intel_96_format =
3644 encode_ieee_extended_intel_96,
3645 decode_ieee_extended_intel_96,
3649 -16381,
3650 16384,
3653 false,
3654 true,
3655 true,
3656 true,
3657 true,
3658 true,
3659 true,
3660 false
3663 const struct real_format ieee_extended_intel_128_format =
3665 encode_ieee_extended_intel_128,
3666 decode_ieee_extended_intel_128,
3670 -16381,
3671 16384,
3674 false,
3675 true,
3676 true,
3677 true,
3678 true,
3679 true,
3680 true,
3681 false
3684 /* The following caters to i386 systems that set the rounding precision
3685 to 53 bits instead of 64, e.g. FreeBSD. */
3686 const struct real_format ieee_extended_intel_96_round_53_format =
3688 encode_ieee_extended_intel_96,
3689 decode_ieee_extended_intel_96,
3693 -16381,
3694 16384,
3697 false,
3698 true,
3699 true,
3700 true,
3701 true,
3702 true,
3703 true,
3704 false
3707 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3708 numbers whose sum is equal to the extended precision value. The number
3709 with greater magnitude is first. This format has the same magnitude
3710 range as an IEEE double precision value, but effectively 106 bits of
3711 significand precision. Infinity and NaN are represented by their IEEE
3712 double precision value stored in the first number, the second number is
3713 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3715 static void encode_ibm_extended (const struct real_format *fmt,
3716 long *, const REAL_VALUE_TYPE *);
3717 static void decode_ibm_extended (const struct real_format *,
3718 REAL_VALUE_TYPE *, const long *);
3720 static void
3721 encode_ibm_extended (const struct real_format *fmt, long *buf,
3722 const REAL_VALUE_TYPE *r)
3724 REAL_VALUE_TYPE u, normr, v;
3725 const struct real_format *base_fmt;
3727 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3729 /* Renormalize R before doing any arithmetic on it. */
3730 normr = *r;
3731 if (normr.cl == rvc_normal)
3732 normalize (&normr);
3734 /* u = IEEE double precision portion of significand. */
3735 u = normr;
3736 round_for_format (base_fmt, &u);
3737 encode_ieee_double (base_fmt, &buf[0], &u);
3739 if (u.cl == rvc_normal)
3741 do_add (&v, &normr, &u, 1);
3742 /* Call round_for_format since we might need to denormalize. */
3743 round_for_format (base_fmt, &v);
3744 encode_ieee_double (base_fmt, &buf[2], &v);
3746 else
3748 /* Inf, NaN, 0 are all representable as doubles, so the
3749 least-significant part can be 0.0. */
3750 buf[2] = 0;
3751 buf[3] = 0;
3755 static void
3756 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3757 const long *buf)
3759 REAL_VALUE_TYPE u, v;
3760 const struct real_format *base_fmt;
3762 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3763 decode_ieee_double (base_fmt, &u, &buf[0]);
3765 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3767 decode_ieee_double (base_fmt, &v, &buf[2]);
3768 do_add (r, &u, &v, 0);
3770 else
3771 *r = u;
3774 const struct real_format ibm_extended_format =
3776 encode_ibm_extended,
3777 decode_ibm_extended,
3779 53 + 53,
3781 -1021 + 53,
3782 1024,
3783 127,
3785 false,
3786 true,
3787 true,
3788 true,
3789 true,
3790 true,
3791 true,
3792 false
3795 const struct real_format mips_extended_format =
3797 encode_ibm_extended,
3798 decode_ibm_extended,
3800 53 + 53,
3802 -1021 + 53,
3803 1024,
3804 127,
3806 false,
3807 true,
3808 true,
3809 true,
3810 true,
3811 true,
3812 false,
3813 true
3817 /* IEEE quad precision format. */
3819 static void encode_ieee_quad (const struct real_format *fmt,
3820 long *, const REAL_VALUE_TYPE *);
3821 static void decode_ieee_quad (const struct real_format *,
3822 REAL_VALUE_TYPE *, const long *);
3824 static void
3825 encode_ieee_quad (const struct real_format *fmt, long *buf,
3826 const REAL_VALUE_TYPE *r)
3828 unsigned long image3, image2, image1, image0, exp;
3829 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3830 REAL_VALUE_TYPE u;
3832 image3 = r->sign << 31;
3833 image2 = 0;
3834 image1 = 0;
3835 image0 = 0;
3837 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3839 switch (r->cl)
3841 case rvc_zero:
3842 break;
3844 case rvc_inf:
3845 if (fmt->has_inf)
3846 image3 |= 32767 << 16;
3847 else
3849 image3 |= 0x7fffffff;
3850 image2 = 0xffffffff;
3851 image1 = 0xffffffff;
3852 image0 = 0xffffffff;
3854 break;
3856 case rvc_nan:
3857 if (fmt->has_nans)
3859 image3 |= 32767 << 16;
3861 if (r->canonical)
3863 if (fmt->canonical_nan_lsbs_set)
3865 image3 |= 0x7fff;
3866 image2 = image1 = image0 = 0xffffffff;
3869 else if (HOST_BITS_PER_LONG == 32)
3871 image0 = u.sig[0];
3872 image1 = u.sig[1];
3873 image2 = u.sig[2];
3874 image3 |= u.sig[3] & 0xffff;
3876 else
3878 image0 = u.sig[0];
3879 image1 = image0 >> 31 >> 1;
3880 image2 = u.sig[1];
3881 image3 |= (image2 >> 31 >> 1) & 0xffff;
3882 image0 &= 0xffffffff;
3883 image2 &= 0xffffffff;
3885 if (r->signalling == fmt->qnan_msb_set)
3886 image3 &= ~0x8000;
3887 else
3888 image3 |= 0x8000;
3889 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3890 image3 |= 0x4000;
3892 else
3894 image3 |= 0x7fffffff;
3895 image2 = 0xffffffff;
3896 image1 = 0xffffffff;
3897 image0 = 0xffffffff;
3899 break;
3901 case rvc_normal:
3902 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3903 whereas the intermediate representation is 0.F x 2**exp.
3904 Which means we're off by one. */
3905 if (denormal)
3906 exp = 0;
3907 else
3908 exp = REAL_EXP (r) + 16383 - 1;
3909 image3 |= exp << 16;
3911 if (HOST_BITS_PER_LONG == 32)
3913 image0 = u.sig[0];
3914 image1 = u.sig[1];
3915 image2 = u.sig[2];
3916 image3 |= u.sig[3] & 0xffff;
3918 else
3920 image0 = u.sig[0];
3921 image1 = image0 >> 31 >> 1;
3922 image2 = u.sig[1];
3923 image3 |= (image2 >> 31 >> 1) & 0xffff;
3924 image0 &= 0xffffffff;
3925 image2 &= 0xffffffff;
3927 break;
3929 default:
3930 gcc_unreachable ();
3933 if (FLOAT_WORDS_BIG_ENDIAN)
3935 buf[0] = image3;
3936 buf[1] = image2;
3937 buf[2] = image1;
3938 buf[3] = image0;
3940 else
3942 buf[0] = image0;
3943 buf[1] = image1;
3944 buf[2] = image2;
3945 buf[3] = image3;
3949 static void
3950 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3951 const long *buf)
3953 unsigned long image3, image2, image1, image0;
3954 bool sign;
3955 int exp;
3957 if (FLOAT_WORDS_BIG_ENDIAN)
3959 image3 = buf[0];
3960 image2 = buf[1];
3961 image1 = buf[2];
3962 image0 = buf[3];
3964 else
3966 image0 = buf[0];
3967 image1 = buf[1];
3968 image2 = buf[2];
3969 image3 = buf[3];
3971 image0 &= 0xffffffff;
3972 image1 &= 0xffffffff;
3973 image2 &= 0xffffffff;
3975 sign = (image3 >> 31) & 1;
3976 exp = (image3 >> 16) & 0x7fff;
3977 image3 &= 0xffff;
3979 memset (r, 0, sizeof (*r));
3981 if (exp == 0)
3983 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3985 r->cl = rvc_normal;
3986 r->sign = sign;
3988 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
3989 if (HOST_BITS_PER_LONG == 32)
3991 r->sig[0] = image0;
3992 r->sig[1] = image1;
3993 r->sig[2] = image2;
3994 r->sig[3] = image3;
3996 else
3998 r->sig[0] = (image1 << 31 << 1) | image0;
3999 r->sig[1] = (image3 << 31 << 1) | image2;
4002 normalize (r);
4004 else if (fmt->has_signed_zero)
4005 r->sign = sign;
4007 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
4009 if (image3 | image2 | image1 | image0)
4011 r->cl = rvc_nan;
4012 r->sign = sign;
4013 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
4015 if (HOST_BITS_PER_LONG == 32)
4017 r->sig[0] = image0;
4018 r->sig[1] = image1;
4019 r->sig[2] = image2;
4020 r->sig[3] = image3;
4022 else
4024 r->sig[0] = (image1 << 31 << 1) | image0;
4025 r->sig[1] = (image3 << 31 << 1) | image2;
4027 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4029 else
4031 r->cl = rvc_inf;
4032 r->sign = sign;
4035 else
4037 r->cl = rvc_normal;
4038 r->sign = sign;
4039 SET_REAL_EXP (r, exp - 16383 + 1);
4041 if (HOST_BITS_PER_LONG == 32)
4043 r->sig[0] = image0;
4044 r->sig[1] = image1;
4045 r->sig[2] = image2;
4046 r->sig[3] = image3;
4048 else
4050 r->sig[0] = (image1 << 31 << 1) | image0;
4051 r->sig[1] = (image3 << 31 << 1) | image2;
4053 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4054 r->sig[SIGSZ-1] |= SIG_MSB;
4058 const struct real_format ieee_quad_format =
4060 encode_ieee_quad,
4061 decode_ieee_quad,
4063 113,
4064 113,
4065 -16381,
4066 16384,
4067 127,
4068 127,
4069 false,
4070 true,
4071 true,
4072 true,
4073 true,
4074 true,
4075 true,
4076 false
4079 const struct real_format mips_quad_format =
4081 encode_ieee_quad,
4082 decode_ieee_quad,
4084 113,
4085 113,
4086 -16381,
4087 16384,
4088 127,
4089 127,
4090 false,
4091 true,
4092 true,
4093 true,
4094 true,
4095 true,
4096 false,
4097 true
4100 /* Descriptions of VAX floating point formats can be found beginning at
4102 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4104 The thing to remember is that they're almost IEEE, except for word
4105 order, exponent bias, and the lack of infinities, nans, and denormals.
4107 We don't implement the H_floating format here, simply because neither
4108 the VAX or Alpha ports use it. */
4110 static void encode_vax_f (const struct real_format *fmt,
4111 long *, const REAL_VALUE_TYPE *);
4112 static void decode_vax_f (const struct real_format *,
4113 REAL_VALUE_TYPE *, const long *);
4114 static void encode_vax_d (const struct real_format *fmt,
4115 long *, const REAL_VALUE_TYPE *);
4116 static void decode_vax_d (const struct real_format *,
4117 REAL_VALUE_TYPE *, const long *);
4118 static void encode_vax_g (const struct real_format *fmt,
4119 long *, const REAL_VALUE_TYPE *);
4120 static void decode_vax_g (const struct real_format *,
4121 REAL_VALUE_TYPE *, const long *);
4123 static void
4124 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4125 const REAL_VALUE_TYPE *r)
4127 unsigned long sign, exp, sig, image;
4129 sign = r->sign << 15;
4131 switch (r->cl)
4133 case rvc_zero:
4134 image = 0;
4135 break;
4137 case rvc_inf:
4138 case rvc_nan:
4139 image = 0xffff7fff | sign;
4140 break;
4142 case rvc_normal:
4143 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4144 exp = REAL_EXP (r) + 128;
4146 image = (sig << 16) & 0xffff0000;
4147 image |= sign;
4148 image |= exp << 7;
4149 image |= sig >> 16;
4150 break;
4152 default:
4153 gcc_unreachable ();
4156 buf[0] = image;
4159 static void
4160 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
4161 REAL_VALUE_TYPE *r, const long *buf)
4163 unsigned long image = buf[0] & 0xffffffff;
4164 int exp = (image >> 7) & 0xff;
4166 memset (r, 0, sizeof (*r));
4168 if (exp != 0)
4170 r->cl = rvc_normal;
4171 r->sign = (image >> 15) & 1;
4172 SET_REAL_EXP (r, exp - 128);
4174 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
4175 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4179 static void
4180 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4181 const REAL_VALUE_TYPE *r)
4183 unsigned long image0, image1, sign = r->sign << 15;
4185 switch (r->cl)
4187 case rvc_zero:
4188 image0 = image1 = 0;
4189 break;
4191 case rvc_inf:
4192 case rvc_nan:
4193 image0 = 0xffff7fff | sign;
4194 image1 = 0xffffffff;
4195 break;
4197 case rvc_normal:
4198 /* Extract the significand into straight hi:lo. */
4199 if (HOST_BITS_PER_LONG == 64)
4201 image0 = r->sig[SIGSZ-1];
4202 image1 = (image0 >> (64 - 56)) & 0xffffffff;
4203 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
4205 else
4207 image0 = r->sig[SIGSZ-1];
4208 image1 = r->sig[SIGSZ-2];
4209 image1 = (image0 << 24) | (image1 >> 8);
4210 image0 = (image0 >> 8) & 0xffffff;
4213 /* Rearrange the half-words of the significand to match the
4214 external format. */
4215 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
4216 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4218 /* Add the sign and exponent. */
4219 image0 |= sign;
4220 image0 |= (REAL_EXP (r) + 128) << 7;
4221 break;
4223 default:
4224 gcc_unreachable ();
4227 if (FLOAT_WORDS_BIG_ENDIAN)
4228 buf[0] = image1, buf[1] = image0;
4229 else
4230 buf[0] = image0, buf[1] = image1;
4233 static void
4234 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
4235 REAL_VALUE_TYPE *r, const long *buf)
4237 unsigned long image0, image1;
4238 int exp;
4240 if (FLOAT_WORDS_BIG_ENDIAN)
4241 image1 = buf[0], image0 = buf[1];
4242 else
4243 image0 = buf[0], image1 = buf[1];
4244 image0 &= 0xffffffff;
4245 image1 &= 0xffffffff;
4247 exp = (image0 >> 7) & 0xff;
4249 memset (r, 0, sizeof (*r));
4251 if (exp != 0)
4253 r->cl = rvc_normal;
4254 r->sign = (image0 >> 15) & 1;
4255 SET_REAL_EXP (r, exp - 128);
4257 /* Rearrange the half-words of the external format into
4258 proper ascending order. */
4259 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4260 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4262 if (HOST_BITS_PER_LONG == 64)
4264 image0 = (image0 << 31 << 1) | image1;
4265 image0 <<= 64 - 56;
4266 image0 |= SIG_MSB;
4267 r->sig[SIGSZ-1] = image0;
4269 else
4271 r->sig[SIGSZ-1] = image0;
4272 r->sig[SIGSZ-2] = image1;
4273 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4274 r->sig[SIGSZ-1] |= SIG_MSB;
4279 static void
4280 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4281 const REAL_VALUE_TYPE *r)
4283 unsigned long image0, image1, sign = r->sign << 15;
4285 switch (r->cl)
4287 case rvc_zero:
4288 image0 = image1 = 0;
4289 break;
4291 case rvc_inf:
4292 case rvc_nan:
4293 image0 = 0xffff7fff | sign;
4294 image1 = 0xffffffff;
4295 break;
4297 case rvc_normal:
4298 /* Extract the significand into straight hi:lo. */
4299 if (HOST_BITS_PER_LONG == 64)
4301 image0 = r->sig[SIGSZ-1];
4302 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4303 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4305 else
4307 image0 = r->sig[SIGSZ-1];
4308 image1 = r->sig[SIGSZ-2];
4309 image1 = (image0 << 21) | (image1 >> 11);
4310 image0 = (image0 >> 11) & 0xfffff;
4313 /* Rearrange the half-words of the significand to match the
4314 external format. */
4315 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4316 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4318 /* Add the sign and exponent. */
4319 image0 |= sign;
4320 image0 |= (REAL_EXP (r) + 1024) << 4;
4321 break;
4323 default:
4324 gcc_unreachable ();
4327 if (FLOAT_WORDS_BIG_ENDIAN)
4328 buf[0] = image1, buf[1] = image0;
4329 else
4330 buf[0] = image0, buf[1] = image1;
4333 static void
4334 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4335 REAL_VALUE_TYPE *r, const long *buf)
4337 unsigned long image0, image1;
4338 int exp;
4340 if (FLOAT_WORDS_BIG_ENDIAN)
4341 image1 = buf[0], image0 = buf[1];
4342 else
4343 image0 = buf[0], image1 = buf[1];
4344 image0 &= 0xffffffff;
4345 image1 &= 0xffffffff;
4347 exp = (image0 >> 4) & 0x7ff;
4349 memset (r, 0, sizeof (*r));
4351 if (exp != 0)
4353 r->cl = rvc_normal;
4354 r->sign = (image0 >> 15) & 1;
4355 SET_REAL_EXP (r, exp - 1024);
4357 /* Rearrange the half-words of the external format into
4358 proper ascending order. */
4359 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4360 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4362 if (HOST_BITS_PER_LONG == 64)
4364 image0 = (image0 << 31 << 1) | image1;
4365 image0 <<= 64 - 53;
4366 image0 |= SIG_MSB;
4367 r->sig[SIGSZ-1] = image0;
4369 else
4371 r->sig[SIGSZ-1] = image0;
4372 r->sig[SIGSZ-2] = image1;
4373 lshift_significand (r, r, 64 - 53);
4374 r->sig[SIGSZ-1] |= SIG_MSB;
4379 const struct real_format vax_f_format =
4381 encode_vax_f,
4382 decode_vax_f,
4386 -127,
4387 127,
4390 false,
4391 false,
4392 false,
4393 false,
4394 false,
4395 false,
4396 false,
4397 false
4400 const struct real_format vax_d_format =
4402 encode_vax_d,
4403 decode_vax_d,
4407 -127,
4408 127,
4411 false,
4412 false,
4413 false,
4414 false,
4415 false,
4416 false,
4417 false,
4418 false
4421 const struct real_format vax_g_format =
4423 encode_vax_g,
4424 decode_vax_g,
4428 -1023,
4429 1023,
4432 false,
4433 false,
4434 false,
4435 false,
4436 false,
4437 false,
4438 false,
4439 false
4442 /* Encode real R into a single precision DFP value in BUF. */
4443 static void
4444 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4445 long *buf ATTRIBUTE_UNUSED,
4446 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4448 encode_decimal32 (fmt, buf, r);
4451 /* Decode a single precision DFP value in BUF into a real R. */
4452 static void
4453 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4454 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4455 const long *buf ATTRIBUTE_UNUSED)
4457 decode_decimal32 (fmt, r, buf);
4460 /* Encode real R into a double precision DFP value in BUF. */
4461 static void
4462 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4463 long *buf ATTRIBUTE_UNUSED,
4464 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4466 encode_decimal64 (fmt, buf, r);
4469 /* Decode a double precision DFP value in BUF into a real R. */
4470 static void
4471 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4472 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4473 const long *buf ATTRIBUTE_UNUSED)
4475 decode_decimal64 (fmt, r, buf);
4478 /* Encode real R into a quad precision DFP value in BUF. */
4479 static void
4480 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4481 long *buf ATTRIBUTE_UNUSED,
4482 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4484 encode_decimal128 (fmt, buf, r);
4487 /* Decode a quad precision DFP value in BUF into a real R. */
4488 static void
4489 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4490 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4491 const long *buf ATTRIBUTE_UNUSED)
4493 decode_decimal128 (fmt, r, buf);
4496 /* Single precision decimal floating point (IEEE 754). */
4497 const struct real_format decimal_single_format =
4499 encode_decimal_single,
4500 decode_decimal_single,
4504 -94,
4508 false,
4509 true,
4510 true,
4511 true,
4512 true,
4513 true,
4514 true,
4515 false
4518 /* Double precision decimal floating point (IEEE 754). */
4519 const struct real_format decimal_double_format =
4521 encode_decimal_double,
4522 decode_decimal_double,
4526 -382,
4527 385,
4530 false,
4531 true,
4532 true,
4533 true,
4534 true,
4535 true,
4536 true,
4537 false
4540 /* Quad precision decimal floating point (IEEE 754). */
4541 const struct real_format decimal_quad_format =
4543 encode_decimal_quad,
4544 decode_decimal_quad,
4548 -6142,
4549 6145,
4550 127,
4551 127,
4552 false,
4553 true,
4554 true,
4555 true,
4556 true,
4557 true,
4558 true,
4559 false
4562 /* Encode half-precision floats. This routine is used both for the IEEE
4563 ARM alternative encodings. */
4564 static void
4565 encode_ieee_half (const struct real_format *fmt, long *buf,
4566 const REAL_VALUE_TYPE *r)
4568 unsigned long image, sig, exp;
4569 unsigned long sign = r->sign;
4570 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
4572 image = sign << 15;
4573 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 11)) & 0x3ff;
4575 switch (r->cl)
4577 case rvc_zero:
4578 break;
4580 case rvc_inf:
4581 if (fmt->has_inf)
4582 image |= 31 << 10;
4583 else
4584 image |= 0x7fff;
4585 break;
4587 case rvc_nan:
4588 if (fmt->has_nans)
4590 if (r->canonical)
4591 sig = (fmt->canonical_nan_lsbs_set ? (1 << 9) - 1 : 0);
4592 if (r->signalling == fmt->qnan_msb_set)
4593 sig &= ~(1 << 9);
4594 else
4595 sig |= 1 << 9;
4596 if (sig == 0)
4597 sig = 1 << 8;
4599 image |= 31 << 10;
4600 image |= sig;
4602 else
4603 image |= 0x3ff;
4604 break;
4606 case rvc_normal:
4607 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4608 whereas the intermediate representation is 0.F x 2**exp.
4609 Which means we're off by one. */
4610 if (denormal)
4611 exp = 0;
4612 else
4613 exp = REAL_EXP (r) + 15 - 1;
4614 image |= exp << 10;
4615 image |= sig;
4616 break;
4618 default:
4619 gcc_unreachable ();
4622 buf[0] = image;
4625 /* Decode half-precision floats. This routine is used both for the IEEE
4626 ARM alternative encodings. */
4627 static void
4628 decode_ieee_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4629 const long *buf)
4631 unsigned long image = buf[0] & 0xffff;
4632 bool sign = (image >> 15) & 1;
4633 int exp = (image >> 10) & 0x1f;
4635 memset (r, 0, sizeof (*r));
4636 image <<= HOST_BITS_PER_LONG - 11;
4637 image &= ~SIG_MSB;
4639 if (exp == 0)
4641 if (image && fmt->has_denorm)
4643 r->cl = rvc_normal;
4644 r->sign = sign;
4645 SET_REAL_EXP (r, -14);
4646 r->sig[SIGSZ-1] = image << 1;
4647 normalize (r);
4649 else if (fmt->has_signed_zero)
4650 r->sign = sign;
4652 else if (exp == 31 && (fmt->has_nans || fmt->has_inf))
4654 if (image)
4656 r->cl = rvc_nan;
4657 r->sign = sign;
4658 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4659 ^ fmt->qnan_msb_set);
4660 r->sig[SIGSZ-1] = image;
4662 else
4664 r->cl = rvc_inf;
4665 r->sign = sign;
4668 else
4670 r->cl = rvc_normal;
4671 r->sign = sign;
4672 SET_REAL_EXP (r, exp - 15 + 1);
4673 r->sig[SIGSZ-1] = image | SIG_MSB;
4677 /* Half-precision format, as specified in IEEE 754R. */
4678 const struct real_format ieee_half_format =
4680 encode_ieee_half,
4681 decode_ieee_half,
4685 -13,
4689 false,
4690 true,
4691 true,
4692 true,
4693 true,
4694 true,
4695 true,
4696 false
4699 /* ARM's alternative half-precision format, similar to IEEE but with
4700 no reserved exponent value for NaNs and infinities; rather, it just
4701 extends the range of exponents by one. */
4702 const struct real_format arm_half_format =
4704 encode_ieee_half,
4705 decode_ieee_half,
4709 -13,
4713 false,
4714 true,
4715 false,
4716 false,
4717 true,
4718 true,
4719 false,
4720 false
4723 /* A synthetic "format" for internal arithmetic. It's the size of the
4724 internal significand minus the two bits needed for proper rounding.
4725 The encode and decode routines exist only to satisfy our paranoia
4726 harness. */
4728 static void encode_internal (const struct real_format *fmt,
4729 long *, const REAL_VALUE_TYPE *);
4730 static void decode_internal (const struct real_format *,
4731 REAL_VALUE_TYPE *, const long *);
4733 static void
4734 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4735 const REAL_VALUE_TYPE *r)
4737 memcpy (buf, r, sizeof (*r));
4740 static void
4741 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
4742 REAL_VALUE_TYPE *r, const long *buf)
4744 memcpy (r, buf, sizeof (*r));
4747 const struct real_format real_internal_format =
4749 encode_internal,
4750 decode_internal,
4752 SIGNIFICAND_BITS - 2,
4753 SIGNIFICAND_BITS - 2,
4754 -MAX_EXP,
4755 MAX_EXP,
4758 false,
4759 false,
4760 true,
4761 true,
4762 false,
4763 true,
4764 true,
4765 false
4768 /* Calculate X raised to the integer exponent N in mode MODE and store
4769 the result in R. Return true if the result may be inexact due to
4770 loss of precision. The algorithm is the classic "left-to-right binary
4771 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4772 Algorithms", "The Art of Computer Programming", Volume 2. */
4774 bool
4775 real_powi (REAL_VALUE_TYPE *r, enum machine_mode mode,
4776 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4778 unsigned HOST_WIDE_INT bit;
4779 REAL_VALUE_TYPE t;
4780 bool inexact = false;
4781 bool init = false;
4782 bool neg;
4783 int i;
4785 if (n == 0)
4787 *r = dconst1;
4788 return false;
4790 else if (n < 0)
4792 /* Don't worry about overflow, from now on n is unsigned. */
4793 neg = true;
4794 n = -n;
4796 else
4797 neg = false;
4799 t = *x;
4800 bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
4801 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4803 if (init)
4805 inexact |= do_multiply (&t, &t, &t);
4806 if (n & bit)
4807 inexact |= do_multiply (&t, &t, x);
4809 else if (n & bit)
4810 init = true;
4811 bit >>= 1;
4814 if (neg)
4815 inexact |= do_divide (&t, &dconst1, &t);
4817 real_convert (r, mode, &t);
4818 return inexact;
4821 /* Round X to the nearest integer not larger in absolute value, i.e.
4822 towards zero, placing the result in R in mode MODE. */
4824 void
4825 real_trunc (REAL_VALUE_TYPE *r, enum machine_mode mode,
4826 const REAL_VALUE_TYPE *x)
4828 do_fix_trunc (r, x);
4829 if (mode != VOIDmode)
4830 real_convert (r, mode, r);
4833 /* Round X to the largest integer not greater in value, i.e. round
4834 down, placing the result in R in mode MODE. */
4836 void
4837 real_floor (REAL_VALUE_TYPE *r, enum machine_mode mode,
4838 const REAL_VALUE_TYPE *x)
4840 REAL_VALUE_TYPE t;
4842 do_fix_trunc (&t, x);
4843 if (! real_identical (&t, x) && x->sign)
4844 do_add (&t, &t, &dconstm1, 0);
4845 if (mode != VOIDmode)
4846 real_convert (r, mode, &t);
4847 else
4848 *r = t;
4851 /* Round X to the smallest integer not less then argument, i.e. round
4852 up, placing the result in R in mode MODE. */
4854 void
4855 real_ceil (REAL_VALUE_TYPE *r, enum machine_mode mode,
4856 const REAL_VALUE_TYPE *x)
4858 REAL_VALUE_TYPE t;
4860 do_fix_trunc (&t, x);
4861 if (! real_identical (&t, x) && ! x->sign)
4862 do_add (&t, &t, &dconst1, 0);
4863 if (mode != VOIDmode)
4864 real_convert (r, mode, &t);
4865 else
4866 *r = t;
4869 /* Round X to the nearest integer, but round halfway cases away from
4870 zero. */
4872 void
4873 real_round (REAL_VALUE_TYPE *r, enum machine_mode mode,
4874 const REAL_VALUE_TYPE *x)
4876 do_add (r, x, &dconsthalf, x->sign);
4877 do_fix_trunc (r, r);
4878 if (mode != VOIDmode)
4879 real_convert (r, mode, r);
4882 /* Set the sign of R to the sign of X. */
4884 void
4885 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4887 r->sign = x->sign;
4890 /* Check whether the real constant value given is an integer. */
4892 bool
4893 real_isinteger (const REAL_VALUE_TYPE *c, enum machine_mode mode)
4895 REAL_VALUE_TYPE cint;
4897 real_trunc (&cint, mode, c);
4898 return real_identical (c, &cint);
4901 /* Write into BUF the maximum representable finite floating-point
4902 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4903 float string. LEN is the size of BUF, and the buffer must be large
4904 enough to contain the resulting string. */
4906 void
4907 get_max_float (const struct real_format *fmt, char *buf, size_t len)
4909 int i, n;
4910 char *p;
4912 strcpy (buf, "0x0.");
4913 n = fmt->p;
4914 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
4915 *p++ = 'f';
4916 if (i < n)
4917 *p++ = "08ce"[n - i];
4918 sprintf (p, "p%d", fmt->emax);
4919 if (fmt->pnan < fmt->p)
4921 /* This is an IBM extended double format made up of two IEEE
4922 doubles. The value of the long double is the sum of the
4923 values of the two parts. The most significant part is
4924 required to be the value of the long double rounded to the
4925 nearest double. Rounding means we need a slightly smaller
4926 value for LDBL_MAX. */
4927 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
4930 gcc_assert (strlen (buf) < len);