2015-10-18 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / real.c
blob85ac83d3fdca5163d4764fb4065400c4717bb4da
1 /* real.c - software floating point emulation.
2 Copyright (C) 1993-2015 Free Software Foundation, Inc.
3 Contributed by Stephen L. Moshier (moshier@world.std.com).
4 Re-written by Richard Henderson <rth@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "alias.h"
27 #include "tree.h"
28 #include "diagnostic-core.h"
29 #include "realmpfr.h"
30 #include "tm_p.h"
31 #include "dfp.h"
32 #include "rtl.h"
33 #include "options.h"
35 /* The floating point model used internally is not exactly IEEE 754
36 compliant, and close to the description in the ISO C99 standard,
37 section 5.2.4.2.2 Characteristics of floating types.
39 Specifically
41 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
43 where
44 s = sign (+- 1)
45 b = base or radix, here always 2
46 e = exponent
47 p = precision (the number of base-b digits in the significand)
48 f_k = the digits of the significand.
50 We differ from typical IEEE 754 encodings in that the entire
51 significand is fractional. Normalized significands are in the
52 range [0.5, 1.0).
54 A requirement of the model is that P be larger than the largest
55 supported target floating-point type by at least 2 bits. This gives
56 us proper rounding when we truncate to the target type. In addition,
57 E must be large enough to hold the smallest supported denormal number
58 in a normalized form.
60 Both of these requirements are easily satisfied. The largest target
61 significand is 113 bits; we store at least 160. The smallest
62 denormal number fits in 17 exponent bits; we store 26. */
65 /* Used to classify two numbers simultaneously. */
66 #define CLASS2(A, B) ((A) << 2 | (B))
68 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
69 #error "Some constant folding done by hand to avoid shift count warnings"
70 #endif
72 static void get_zero (REAL_VALUE_TYPE *, int);
73 static void get_canonical_qnan (REAL_VALUE_TYPE *, int);
74 static void get_canonical_snan (REAL_VALUE_TYPE *, int);
75 static void get_inf (REAL_VALUE_TYPE *, int);
76 static bool sticky_rshift_significand (REAL_VALUE_TYPE *,
77 const REAL_VALUE_TYPE *, unsigned int);
78 static void rshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
79 unsigned int);
80 static void lshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
81 unsigned int);
82 static void lshift_significand_1 (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
83 static bool add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *,
84 const REAL_VALUE_TYPE *);
85 static bool sub_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
86 const REAL_VALUE_TYPE *, int);
87 static void neg_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
88 static int cmp_significands (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
89 static int cmp_significand_0 (const REAL_VALUE_TYPE *);
90 static void set_significand_bit (REAL_VALUE_TYPE *, unsigned int);
91 static void clear_significand_bit (REAL_VALUE_TYPE *, unsigned int);
92 static bool test_significand_bit (REAL_VALUE_TYPE *, unsigned int);
93 static void clear_significand_below (REAL_VALUE_TYPE *, unsigned int);
94 static bool div_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
95 const REAL_VALUE_TYPE *);
96 static void normalize (REAL_VALUE_TYPE *);
98 static bool do_add (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
99 const REAL_VALUE_TYPE *, int);
100 static bool do_multiply (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
101 const REAL_VALUE_TYPE *);
102 static bool do_divide (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
103 const REAL_VALUE_TYPE *);
104 static int do_compare (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int);
105 static void do_fix_trunc (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
107 static unsigned long rtd_divmod (REAL_VALUE_TYPE *, REAL_VALUE_TYPE *);
108 static void decimal_from_integer (REAL_VALUE_TYPE *);
109 static void decimal_integer_string (char *, const REAL_VALUE_TYPE *,
110 size_t);
112 static const REAL_VALUE_TYPE * ten_to_ptwo (int);
113 static const REAL_VALUE_TYPE * ten_to_mptwo (int);
114 static const REAL_VALUE_TYPE * real_digit (int);
115 static void times_pten (REAL_VALUE_TYPE *, int);
117 static void round_for_format (const struct real_format *, REAL_VALUE_TYPE *);
119 /* Initialize R with a positive zero. */
121 static inline void
122 get_zero (REAL_VALUE_TYPE *r, int sign)
124 memset (r, 0, sizeof (*r));
125 r->sign = sign;
128 /* Initialize R with the canonical quiet NaN. */
130 static inline void
131 get_canonical_qnan (REAL_VALUE_TYPE *r, int sign)
133 memset (r, 0, sizeof (*r));
134 r->cl = rvc_nan;
135 r->sign = sign;
136 r->canonical = 1;
139 static inline void
140 get_canonical_snan (REAL_VALUE_TYPE *r, int sign)
142 memset (r, 0, sizeof (*r));
143 r->cl = rvc_nan;
144 r->sign = sign;
145 r->signalling = 1;
146 r->canonical = 1;
149 static inline void
150 get_inf (REAL_VALUE_TYPE *r, int sign)
152 memset (r, 0, sizeof (*r));
153 r->cl = rvc_inf;
154 r->sign = sign;
158 /* Right-shift the significand of A by N bits; put the result in the
159 significand of R. If any one bits are shifted out, return true. */
161 static bool
162 sticky_rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
163 unsigned int n)
165 unsigned long sticky = 0;
166 unsigned int i, ofs = 0;
168 if (n >= HOST_BITS_PER_LONG)
170 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
171 sticky |= a->sig[i];
172 n &= HOST_BITS_PER_LONG - 1;
175 if (n != 0)
177 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
178 for (i = 0; i < SIGSZ; ++i)
180 r->sig[i]
181 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
182 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
183 << (HOST_BITS_PER_LONG - n)));
186 else
188 for (i = 0; ofs + i < SIGSZ; ++i)
189 r->sig[i] = a->sig[ofs + i];
190 for (; i < SIGSZ; ++i)
191 r->sig[i] = 0;
194 return sticky != 0;
197 /* Right-shift the significand of A by N bits; put the result in the
198 significand of R. */
200 static void
201 rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
202 unsigned int n)
204 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
206 n &= HOST_BITS_PER_LONG - 1;
207 if (n != 0)
209 for (i = 0; i < SIGSZ; ++i)
211 r->sig[i]
212 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
213 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
214 << (HOST_BITS_PER_LONG - n)));
217 else
219 for (i = 0; ofs + i < SIGSZ; ++i)
220 r->sig[i] = a->sig[ofs + i];
221 for (; i < SIGSZ; ++i)
222 r->sig[i] = 0;
226 /* Left-shift the significand of A by N bits; put the result in the
227 significand of R. */
229 static void
230 lshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
231 unsigned int n)
233 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
235 n &= HOST_BITS_PER_LONG - 1;
236 if (n == 0)
238 for (i = 0; ofs + i < SIGSZ; ++i)
239 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
240 for (; i < SIGSZ; ++i)
241 r->sig[SIGSZ-1-i] = 0;
243 else
244 for (i = 0; i < SIGSZ; ++i)
246 r->sig[SIGSZ-1-i]
247 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
248 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
249 >> (HOST_BITS_PER_LONG - n)));
253 /* Likewise, but N is specialized to 1. */
255 static inline void
256 lshift_significand_1 (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
258 unsigned int i;
260 for (i = SIGSZ - 1; i > 0; --i)
261 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
262 r->sig[0] = a->sig[0] << 1;
265 /* Add the significands of A and B, placing the result in R. Return
266 true if there was carry out of the most significant word. */
268 static inline bool
269 add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
270 const REAL_VALUE_TYPE *b)
272 bool carry = false;
273 int i;
275 for (i = 0; i < SIGSZ; ++i)
277 unsigned long ai = a->sig[i];
278 unsigned long ri = ai + b->sig[i];
280 if (carry)
282 carry = ri < ai;
283 carry |= ++ri == 0;
285 else
286 carry = ri < ai;
288 r->sig[i] = ri;
291 return carry;
294 /* Subtract the significands of A and B, placing the result in R. CARRY is
295 true if there's a borrow incoming to the least significant word.
296 Return true if there was borrow out of the most significant word. */
298 static inline bool
299 sub_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
300 const REAL_VALUE_TYPE *b, int carry)
302 int i;
304 for (i = 0; i < SIGSZ; ++i)
306 unsigned long ai = a->sig[i];
307 unsigned long ri = ai - b->sig[i];
309 if (carry)
311 carry = ri > ai;
312 carry |= ~--ri == 0;
314 else
315 carry = ri > ai;
317 r->sig[i] = ri;
320 return carry;
323 /* Negate the significand A, placing the result in R. */
325 static inline void
326 neg_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
328 bool carry = true;
329 int i;
331 for (i = 0; i < SIGSZ; ++i)
333 unsigned long ri, ai = a->sig[i];
335 if (carry)
337 if (ai)
339 ri = -ai;
340 carry = false;
342 else
343 ri = ai;
345 else
346 ri = ~ai;
348 r->sig[i] = ri;
352 /* Compare significands. Return tri-state vs zero. */
354 static inline int
355 cmp_significands (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
357 int i;
359 for (i = SIGSZ - 1; i >= 0; --i)
361 unsigned long ai = a->sig[i];
362 unsigned long bi = b->sig[i];
364 if (ai > bi)
365 return 1;
366 if (ai < bi)
367 return -1;
370 return 0;
373 /* Return true if A is nonzero. */
375 static inline int
376 cmp_significand_0 (const REAL_VALUE_TYPE *a)
378 int i;
380 for (i = SIGSZ - 1; i >= 0; --i)
381 if (a->sig[i])
382 return 1;
384 return 0;
387 /* Set bit N of the significand of R. */
389 static inline void
390 set_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
392 r->sig[n / HOST_BITS_PER_LONG]
393 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
396 /* Clear bit N of the significand of R. */
398 static inline void
399 clear_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
401 r->sig[n / HOST_BITS_PER_LONG]
402 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
405 /* Test bit N of the significand of R. */
407 static inline bool
408 test_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
410 /* ??? Compiler bug here if we return this expression directly.
411 The conversion to bool strips the "&1" and we wind up testing
412 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
413 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
414 return t;
417 /* Clear bits 0..N-1 of the significand of R. */
419 static void
420 clear_significand_below (REAL_VALUE_TYPE *r, unsigned int n)
422 int i, w = n / HOST_BITS_PER_LONG;
424 for (i = 0; i < w; ++i)
425 r->sig[i] = 0;
427 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
430 /* Divide the significands of A and B, placing the result in R. Return
431 true if the division was inexact. */
433 static inline bool
434 div_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
435 const REAL_VALUE_TYPE *b)
437 REAL_VALUE_TYPE u;
438 int i, bit = SIGNIFICAND_BITS - 1;
439 unsigned long msb, inexact;
441 u = *a;
442 memset (r->sig, 0, sizeof (r->sig));
444 msb = 0;
445 goto start;
448 msb = u.sig[SIGSZ-1] & SIG_MSB;
449 lshift_significand_1 (&u, &u);
450 start:
451 if (msb || cmp_significands (&u, b) >= 0)
453 sub_significands (&u, &u, b, 0);
454 set_significand_bit (r, bit);
457 while (--bit >= 0);
459 for (i = 0, inexact = 0; i < SIGSZ; i++)
460 inexact |= u.sig[i];
462 return inexact != 0;
465 /* Adjust the exponent and significand of R such that the most
466 significant bit is set. We underflow to zero and overflow to
467 infinity here, without denormals. (The intermediate representation
468 exponent is large enough to handle target denormals normalized.) */
470 static void
471 normalize (REAL_VALUE_TYPE *r)
473 int shift = 0, exp;
474 int i, j;
476 if (r->decimal)
477 return;
479 /* Find the first word that is nonzero. */
480 for (i = SIGSZ - 1; i >= 0; i--)
481 if (r->sig[i] == 0)
482 shift += HOST_BITS_PER_LONG;
483 else
484 break;
486 /* Zero significand flushes to zero. */
487 if (i < 0)
489 r->cl = rvc_zero;
490 SET_REAL_EXP (r, 0);
491 return;
494 /* Find the first bit that is nonzero. */
495 for (j = 0; ; j++)
496 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
497 break;
498 shift += j;
500 if (shift > 0)
502 exp = REAL_EXP (r) - shift;
503 if (exp > MAX_EXP)
504 get_inf (r, r->sign);
505 else if (exp < -MAX_EXP)
506 get_zero (r, r->sign);
507 else
509 SET_REAL_EXP (r, exp);
510 lshift_significand (r, r, shift);
515 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
516 result may be inexact due to a loss of precision. */
518 static bool
519 do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
520 const REAL_VALUE_TYPE *b, int subtract_p)
522 int dexp, sign, exp;
523 REAL_VALUE_TYPE t;
524 bool inexact = false;
526 /* Determine if we need to add or subtract. */
527 sign = a->sign;
528 subtract_p = (sign ^ b->sign) ^ subtract_p;
530 switch (CLASS2 (a->cl, b->cl))
532 case CLASS2 (rvc_zero, rvc_zero):
533 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
534 get_zero (r, sign & !subtract_p);
535 return false;
537 case CLASS2 (rvc_zero, rvc_normal):
538 case CLASS2 (rvc_zero, rvc_inf):
539 case CLASS2 (rvc_zero, rvc_nan):
540 /* 0 + ANY = ANY. */
541 case CLASS2 (rvc_normal, rvc_nan):
542 case CLASS2 (rvc_inf, rvc_nan):
543 case CLASS2 (rvc_nan, rvc_nan):
544 /* ANY + NaN = NaN. */
545 case CLASS2 (rvc_normal, rvc_inf):
546 /* R + Inf = Inf. */
547 *r = *b;
548 r->sign = sign ^ subtract_p;
549 return false;
551 case CLASS2 (rvc_normal, rvc_zero):
552 case CLASS2 (rvc_inf, rvc_zero):
553 case CLASS2 (rvc_nan, rvc_zero):
554 /* ANY + 0 = ANY. */
555 case CLASS2 (rvc_nan, rvc_normal):
556 case CLASS2 (rvc_nan, rvc_inf):
557 /* NaN + ANY = NaN. */
558 case CLASS2 (rvc_inf, rvc_normal):
559 /* Inf + R = Inf. */
560 *r = *a;
561 return false;
563 case CLASS2 (rvc_inf, rvc_inf):
564 if (subtract_p)
565 /* Inf - Inf = NaN. */
566 get_canonical_qnan (r, 0);
567 else
568 /* Inf + Inf = Inf. */
569 *r = *a;
570 return false;
572 case CLASS2 (rvc_normal, rvc_normal):
573 break;
575 default:
576 gcc_unreachable ();
579 /* Swap the arguments such that A has the larger exponent. */
580 dexp = REAL_EXP (a) - REAL_EXP (b);
581 if (dexp < 0)
583 const REAL_VALUE_TYPE *t;
584 t = a, a = b, b = t;
585 dexp = -dexp;
586 sign ^= subtract_p;
588 exp = REAL_EXP (a);
590 /* If the exponents are not identical, we need to shift the
591 significand of B down. */
592 if (dexp > 0)
594 /* If the exponents are too far apart, the significands
595 do not overlap, which makes the subtraction a noop. */
596 if (dexp >= SIGNIFICAND_BITS)
598 *r = *a;
599 r->sign = sign;
600 return true;
603 inexact |= sticky_rshift_significand (&t, b, dexp);
604 b = &t;
607 if (subtract_p)
609 if (sub_significands (r, a, b, inexact))
611 /* We got a borrow out of the subtraction. That means that
612 A and B had the same exponent, and B had the larger
613 significand. We need to swap the sign and negate the
614 significand. */
615 sign ^= 1;
616 neg_significand (r, r);
619 else
621 if (add_significands (r, a, b))
623 /* We got carry out of the addition. This means we need to
624 shift the significand back down one bit and increase the
625 exponent. */
626 inexact |= sticky_rshift_significand (r, r, 1);
627 r->sig[SIGSZ-1] |= SIG_MSB;
628 if (++exp > MAX_EXP)
630 get_inf (r, sign);
631 return true;
636 r->cl = rvc_normal;
637 r->sign = sign;
638 SET_REAL_EXP (r, exp);
639 /* Zero out the remaining fields. */
640 r->signalling = 0;
641 r->canonical = 0;
642 r->decimal = 0;
644 /* Re-normalize the result. */
645 normalize (r);
647 /* Special case: if the subtraction results in zero, the result
648 is positive. */
649 if (r->cl == rvc_zero)
650 r->sign = 0;
651 else
652 r->sig[0] |= inexact;
654 return inexact;
657 /* Calculate R = A * B. Return true if the result may be inexact. */
659 static bool
660 do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
661 const REAL_VALUE_TYPE *b)
663 REAL_VALUE_TYPE u, t, *rr;
664 unsigned int i, j, k;
665 int sign = a->sign ^ b->sign;
666 bool inexact = false;
668 switch (CLASS2 (a->cl, b->cl))
670 case CLASS2 (rvc_zero, rvc_zero):
671 case CLASS2 (rvc_zero, rvc_normal):
672 case CLASS2 (rvc_normal, rvc_zero):
673 /* +-0 * ANY = 0 with appropriate sign. */
674 get_zero (r, sign);
675 return false;
677 case CLASS2 (rvc_zero, rvc_nan):
678 case CLASS2 (rvc_normal, rvc_nan):
679 case CLASS2 (rvc_inf, rvc_nan):
680 case CLASS2 (rvc_nan, rvc_nan):
681 /* ANY * NaN = NaN. */
682 *r = *b;
683 r->sign = sign;
684 return false;
686 case CLASS2 (rvc_nan, rvc_zero):
687 case CLASS2 (rvc_nan, rvc_normal):
688 case CLASS2 (rvc_nan, rvc_inf):
689 /* NaN * ANY = NaN. */
690 *r = *a;
691 r->sign = sign;
692 return false;
694 case CLASS2 (rvc_zero, rvc_inf):
695 case CLASS2 (rvc_inf, rvc_zero):
696 /* 0 * Inf = NaN */
697 get_canonical_qnan (r, sign);
698 return false;
700 case CLASS2 (rvc_inf, rvc_inf):
701 case CLASS2 (rvc_normal, rvc_inf):
702 case CLASS2 (rvc_inf, rvc_normal):
703 /* Inf * Inf = Inf, R * Inf = Inf */
704 get_inf (r, sign);
705 return false;
707 case CLASS2 (rvc_normal, rvc_normal):
708 break;
710 default:
711 gcc_unreachable ();
714 if (r == a || r == b)
715 rr = &t;
716 else
717 rr = r;
718 get_zero (rr, 0);
720 /* Collect all the partial products. Since we don't have sure access
721 to a widening multiply, we split each long into two half-words.
723 Consider the long-hand form of a four half-word multiplication:
725 A B C D
726 * E F G H
727 --------------
728 DE DF DG DH
729 CE CF CG CH
730 BE BF BG BH
731 AE AF AG AH
733 We construct partial products of the widened half-word products
734 that are known to not overlap, e.g. DF+DH. Each such partial
735 product is given its proper exponent, which allows us to sum them
736 and obtain the finished product. */
738 for (i = 0; i < SIGSZ * 2; ++i)
740 unsigned long ai = a->sig[i / 2];
741 if (i & 1)
742 ai >>= HOST_BITS_PER_LONG / 2;
743 else
744 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
746 if (ai == 0)
747 continue;
749 for (j = 0; j < 2; ++j)
751 int exp = (REAL_EXP (a) - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
752 + (REAL_EXP (b) - (1-j)*(HOST_BITS_PER_LONG/2)));
754 if (exp > MAX_EXP)
756 get_inf (r, sign);
757 return true;
759 if (exp < -MAX_EXP)
761 /* Would underflow to zero, which we shouldn't bother adding. */
762 inexact = true;
763 continue;
766 memset (&u, 0, sizeof (u));
767 u.cl = rvc_normal;
768 SET_REAL_EXP (&u, exp);
770 for (k = j; k < SIGSZ * 2; k += 2)
772 unsigned long bi = b->sig[k / 2];
773 if (k & 1)
774 bi >>= HOST_BITS_PER_LONG / 2;
775 else
776 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
778 u.sig[k / 2] = ai * bi;
781 normalize (&u);
782 inexact |= do_add (rr, rr, &u, 0);
786 rr->sign = sign;
787 if (rr != r)
788 *r = t;
790 return inexact;
793 /* Calculate R = A / B. Return true if the result may be inexact. */
795 static bool
796 do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
797 const REAL_VALUE_TYPE *b)
799 int exp, sign = a->sign ^ b->sign;
800 REAL_VALUE_TYPE t, *rr;
801 bool inexact;
803 switch (CLASS2 (a->cl, b->cl))
805 case CLASS2 (rvc_zero, rvc_zero):
806 /* 0 / 0 = NaN. */
807 case CLASS2 (rvc_inf, rvc_inf):
808 /* Inf / Inf = NaN. */
809 get_canonical_qnan (r, sign);
810 return false;
812 case CLASS2 (rvc_zero, rvc_normal):
813 case CLASS2 (rvc_zero, rvc_inf):
814 /* 0 / ANY = 0. */
815 case CLASS2 (rvc_normal, rvc_inf):
816 /* R / Inf = 0. */
817 get_zero (r, sign);
818 return false;
820 case CLASS2 (rvc_normal, rvc_zero):
821 /* R / 0 = Inf. */
822 case CLASS2 (rvc_inf, rvc_zero):
823 /* Inf / 0 = Inf. */
824 get_inf (r, sign);
825 return false;
827 case CLASS2 (rvc_zero, rvc_nan):
828 case CLASS2 (rvc_normal, rvc_nan):
829 case CLASS2 (rvc_inf, rvc_nan):
830 case CLASS2 (rvc_nan, rvc_nan):
831 /* ANY / NaN = NaN. */
832 *r = *b;
833 r->sign = sign;
834 return false;
836 case CLASS2 (rvc_nan, rvc_zero):
837 case CLASS2 (rvc_nan, rvc_normal):
838 case CLASS2 (rvc_nan, rvc_inf):
839 /* NaN / ANY = NaN. */
840 *r = *a;
841 r->sign = sign;
842 return false;
844 case CLASS2 (rvc_inf, rvc_normal):
845 /* Inf / R = Inf. */
846 get_inf (r, sign);
847 return false;
849 case CLASS2 (rvc_normal, rvc_normal):
850 break;
852 default:
853 gcc_unreachable ();
856 if (r == a || r == b)
857 rr = &t;
858 else
859 rr = r;
861 /* Make sure all fields in the result are initialized. */
862 get_zero (rr, 0);
863 rr->cl = rvc_normal;
864 rr->sign = sign;
866 exp = REAL_EXP (a) - REAL_EXP (b) + 1;
867 if (exp > MAX_EXP)
869 get_inf (r, sign);
870 return true;
872 if (exp < -MAX_EXP)
874 get_zero (r, sign);
875 return true;
877 SET_REAL_EXP (rr, exp);
879 inexact = div_significands (rr, a, b);
881 /* Re-normalize the result. */
882 normalize (rr);
883 rr->sig[0] |= inexact;
885 if (rr != r)
886 *r = t;
888 return inexact;
891 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
892 one of the two operands is a NaN. */
894 static int
895 do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
896 int nan_result)
898 int ret;
900 switch (CLASS2 (a->cl, b->cl))
902 case CLASS2 (rvc_zero, rvc_zero):
903 /* Sign of zero doesn't matter for compares. */
904 return 0;
906 case CLASS2 (rvc_normal, rvc_zero):
907 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
908 if (a->decimal)
909 return decimal_do_compare (a, b, nan_result);
910 /* Fall through. */
911 case CLASS2 (rvc_inf, rvc_zero):
912 case CLASS2 (rvc_inf, rvc_normal):
913 return (a->sign ? -1 : 1);
915 case CLASS2 (rvc_inf, rvc_inf):
916 return -a->sign - -b->sign;
918 case CLASS2 (rvc_zero, rvc_normal):
919 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
920 if (b->decimal)
921 return decimal_do_compare (a, b, nan_result);
922 /* Fall through. */
923 case CLASS2 (rvc_zero, rvc_inf):
924 case CLASS2 (rvc_normal, rvc_inf):
925 return (b->sign ? 1 : -1);
927 case CLASS2 (rvc_zero, rvc_nan):
928 case CLASS2 (rvc_normal, rvc_nan):
929 case CLASS2 (rvc_inf, rvc_nan):
930 case CLASS2 (rvc_nan, rvc_nan):
931 case CLASS2 (rvc_nan, rvc_zero):
932 case CLASS2 (rvc_nan, rvc_normal):
933 case CLASS2 (rvc_nan, rvc_inf):
934 return nan_result;
936 case CLASS2 (rvc_normal, rvc_normal):
937 break;
939 default:
940 gcc_unreachable ();
943 if (a->sign != b->sign)
944 return -a->sign - -b->sign;
946 if (a->decimal || b->decimal)
947 return decimal_do_compare (a, b, nan_result);
949 if (REAL_EXP (a) > REAL_EXP (b))
950 ret = 1;
951 else if (REAL_EXP (a) < REAL_EXP (b))
952 ret = -1;
953 else
954 ret = cmp_significands (a, b);
956 return (a->sign ? -ret : ret);
959 /* Return A truncated to an integral value toward zero. */
961 static void
962 do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
964 *r = *a;
966 switch (r->cl)
968 case rvc_zero:
969 case rvc_inf:
970 case rvc_nan:
971 break;
973 case rvc_normal:
974 if (r->decimal)
976 decimal_do_fix_trunc (r, a);
977 return;
979 if (REAL_EXP (r) <= 0)
980 get_zero (r, r->sign);
981 else if (REAL_EXP (r) < SIGNIFICAND_BITS)
982 clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r));
983 break;
985 default:
986 gcc_unreachable ();
990 /* Perform the binary or unary operation described by CODE.
991 For a unary operation, leave OP1 NULL. This function returns
992 true if the result may be inexact due to loss of precision. */
994 bool
995 real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0,
996 const REAL_VALUE_TYPE *op1)
998 enum tree_code code = (enum tree_code) icode;
1000 if (op0->decimal || (op1 && op1->decimal))
1001 return decimal_real_arithmetic (r, code, op0, op1);
1003 switch (code)
1005 case PLUS_EXPR:
1006 /* Clear any padding areas in *r if it isn't equal to one of the
1007 operands so that we can later do bitwise comparisons later on. */
1008 if (r != op0 && r != op1)
1009 memset (r, '\0', sizeof (*r));
1010 return do_add (r, op0, op1, 0);
1012 case MINUS_EXPR:
1013 if (r != op0 && r != op1)
1014 memset (r, '\0', sizeof (*r));
1015 return do_add (r, op0, op1, 1);
1017 case MULT_EXPR:
1018 if (r != op0 && r != op1)
1019 memset (r, '\0', sizeof (*r));
1020 return do_multiply (r, op0, op1);
1022 case RDIV_EXPR:
1023 if (r != op0 && r != op1)
1024 memset (r, '\0', sizeof (*r));
1025 return do_divide (r, op0, op1);
1027 case MIN_EXPR:
1028 if (op1->cl == rvc_nan)
1029 *r = *op1;
1030 else if (do_compare (op0, op1, -1) < 0)
1031 *r = *op0;
1032 else
1033 *r = *op1;
1034 break;
1036 case MAX_EXPR:
1037 if (op1->cl == rvc_nan)
1038 *r = *op1;
1039 else if (do_compare (op0, op1, 1) < 0)
1040 *r = *op1;
1041 else
1042 *r = *op0;
1043 break;
1045 case NEGATE_EXPR:
1046 *r = *op0;
1047 r->sign ^= 1;
1048 break;
1050 case ABS_EXPR:
1051 *r = *op0;
1052 r->sign = 0;
1053 break;
1055 case FIX_TRUNC_EXPR:
1056 do_fix_trunc (r, op0);
1057 break;
1059 default:
1060 gcc_unreachable ();
1062 return false;
1065 REAL_VALUE_TYPE
1066 real_value_negate (const REAL_VALUE_TYPE *op0)
1068 REAL_VALUE_TYPE r;
1069 real_arithmetic (&r, NEGATE_EXPR, op0, NULL);
1070 return r;
1073 REAL_VALUE_TYPE
1074 real_value_abs (const REAL_VALUE_TYPE *op0)
1076 REAL_VALUE_TYPE r;
1077 real_arithmetic (&r, ABS_EXPR, op0, NULL);
1078 return r;
1081 /* Return whether OP0 == OP1. */
1083 bool
1084 real_equal (const REAL_VALUE_TYPE *op0, const REAL_VALUE_TYPE *op1)
1086 return do_compare (op0, op1, -1) == 0;
1089 /* Return whether OP0 < OP1. */
1091 bool
1092 real_less (const REAL_VALUE_TYPE *op0, const REAL_VALUE_TYPE *op1)
1094 return do_compare (op0, op1, 1) < 0;
1097 bool
1098 real_compare (int icode, const REAL_VALUE_TYPE *op0,
1099 const REAL_VALUE_TYPE *op1)
1101 enum tree_code code = (enum tree_code) icode;
1103 switch (code)
1105 case LT_EXPR:
1106 return real_less (op0, op1);
1107 case LE_EXPR:
1108 return do_compare (op0, op1, 1) <= 0;
1109 case GT_EXPR:
1110 return do_compare (op0, op1, -1) > 0;
1111 case GE_EXPR:
1112 return do_compare (op0, op1, -1) >= 0;
1113 case EQ_EXPR:
1114 return real_equal (op0, op1);
1115 case NE_EXPR:
1116 return do_compare (op0, op1, -1) != 0;
1117 case UNORDERED_EXPR:
1118 return op0->cl == rvc_nan || op1->cl == rvc_nan;
1119 case ORDERED_EXPR:
1120 return op0->cl != rvc_nan && op1->cl != rvc_nan;
1121 case UNLT_EXPR:
1122 return do_compare (op0, op1, -1) < 0;
1123 case UNLE_EXPR:
1124 return do_compare (op0, op1, -1) <= 0;
1125 case UNGT_EXPR:
1126 return do_compare (op0, op1, 1) > 0;
1127 case UNGE_EXPR:
1128 return do_compare (op0, op1, 1) >= 0;
1129 case UNEQ_EXPR:
1130 return do_compare (op0, op1, 0) == 0;
1131 case LTGT_EXPR:
1132 return do_compare (op0, op1, 0) != 0;
1134 default:
1135 gcc_unreachable ();
1139 /* Return floor log2(R). */
1142 real_exponent (const REAL_VALUE_TYPE *r)
1144 switch (r->cl)
1146 case rvc_zero:
1147 return 0;
1148 case rvc_inf:
1149 case rvc_nan:
1150 return (unsigned int)-1 >> 1;
1151 case rvc_normal:
1152 return REAL_EXP (r);
1153 default:
1154 gcc_unreachable ();
1158 /* R = OP0 * 2**EXP. */
1160 void
1161 real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1163 *r = *op0;
1164 switch (r->cl)
1166 case rvc_zero:
1167 case rvc_inf:
1168 case rvc_nan:
1169 break;
1171 case rvc_normal:
1172 exp += REAL_EXP (op0);
1173 if (exp > MAX_EXP)
1174 get_inf (r, r->sign);
1175 else if (exp < -MAX_EXP)
1176 get_zero (r, r->sign);
1177 else
1178 SET_REAL_EXP (r, exp);
1179 break;
1181 default:
1182 gcc_unreachable ();
1186 /* Determine whether a floating-point value X is infinite. */
1188 bool
1189 real_isinf (const REAL_VALUE_TYPE *r)
1191 return (r->cl == rvc_inf);
1194 /* Determine whether a floating-point value X is a NaN. */
1196 bool
1197 real_isnan (const REAL_VALUE_TYPE *r)
1199 return (r->cl == rvc_nan);
1202 /* Determine whether a floating-point value X is finite. */
1204 bool
1205 real_isfinite (const REAL_VALUE_TYPE *r)
1207 return (r->cl != rvc_nan) && (r->cl != rvc_inf);
1210 /* Determine whether a floating-point value X is negative. */
1212 bool
1213 real_isneg (const REAL_VALUE_TYPE *r)
1215 return r->sign;
1218 /* Determine whether a floating-point value X is minus zero. */
1220 bool
1221 real_isnegzero (const REAL_VALUE_TYPE *r)
1223 return r->sign && r->cl == rvc_zero;
1226 /* Compare two floating-point objects for bitwise identity. */
1228 bool
1229 real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1231 int i;
1233 if (a->cl != b->cl)
1234 return false;
1235 if (a->sign != b->sign)
1236 return false;
1238 switch (a->cl)
1240 case rvc_zero:
1241 case rvc_inf:
1242 return true;
1244 case rvc_normal:
1245 if (a->decimal != b->decimal)
1246 return false;
1247 if (REAL_EXP (a) != REAL_EXP (b))
1248 return false;
1249 break;
1251 case rvc_nan:
1252 if (a->signalling != b->signalling)
1253 return false;
1254 /* The significand is ignored for canonical NaNs. */
1255 if (a->canonical || b->canonical)
1256 return a->canonical == b->canonical;
1257 break;
1259 default:
1260 gcc_unreachable ();
1263 for (i = 0; i < SIGSZ; ++i)
1264 if (a->sig[i] != b->sig[i])
1265 return false;
1267 return true;
1270 /* Try to change R into its exact multiplicative inverse in machine
1271 mode MODE. Return true if successful. */
1273 bool
1274 exact_real_inverse (machine_mode mode, REAL_VALUE_TYPE *r)
1276 const REAL_VALUE_TYPE *one = real_digit (1);
1277 REAL_VALUE_TYPE u;
1278 int i;
1280 if (r->cl != rvc_normal)
1281 return false;
1283 /* Check for a power of two: all significand bits zero except the MSB. */
1284 for (i = 0; i < SIGSZ-1; ++i)
1285 if (r->sig[i] != 0)
1286 return false;
1287 if (r->sig[SIGSZ-1] != SIG_MSB)
1288 return false;
1290 /* Find the inverse and truncate to the required mode. */
1291 do_divide (&u, one, r);
1292 real_convert (&u, mode, &u);
1294 /* The rounding may have overflowed. */
1295 if (u.cl != rvc_normal)
1296 return false;
1297 for (i = 0; i < SIGSZ-1; ++i)
1298 if (u.sig[i] != 0)
1299 return false;
1300 if (u.sig[SIGSZ-1] != SIG_MSB)
1301 return false;
1303 *r = u;
1304 return true;
1307 /* Return true if arithmetic on values in IMODE that were promoted
1308 from values in TMODE is equivalent to direct arithmetic on values
1309 in TMODE. */
1311 bool
1312 real_can_shorten_arithmetic (machine_mode imode, machine_mode tmode)
1314 const struct real_format *tfmt, *ifmt;
1315 tfmt = REAL_MODE_FORMAT (tmode);
1316 ifmt = REAL_MODE_FORMAT (imode);
1317 /* These conditions are conservative rather than trying to catch the
1318 exact boundary conditions; the main case to allow is IEEE float
1319 and double. */
1320 return (ifmt->b == tfmt->b
1321 && ifmt->p > 2 * tfmt->p
1322 && ifmt->emin < 2 * tfmt->emin - tfmt->p - 2
1323 && ifmt->emin < tfmt->emin - tfmt->emax - tfmt->p - 2
1324 && ifmt->emax > 2 * tfmt->emax + 2
1325 && ifmt->emax > tfmt->emax - tfmt->emin + tfmt->p + 2
1326 && ifmt->round_towards_zero == tfmt->round_towards_zero
1327 && (ifmt->has_sign_dependent_rounding
1328 == tfmt->has_sign_dependent_rounding)
1329 && ifmt->has_nans >= tfmt->has_nans
1330 && ifmt->has_inf >= tfmt->has_inf
1331 && ifmt->has_signed_zero >= tfmt->has_signed_zero
1332 && !MODE_COMPOSITE_P (tmode)
1333 && !MODE_COMPOSITE_P (imode));
1336 /* Render R as an integer. */
1338 HOST_WIDE_INT
1339 real_to_integer (const REAL_VALUE_TYPE *r)
1341 unsigned HOST_WIDE_INT i;
1343 switch (r->cl)
1345 case rvc_zero:
1346 underflow:
1347 return 0;
1349 case rvc_inf:
1350 case rvc_nan:
1351 overflow:
1352 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1353 if (!r->sign)
1354 i--;
1355 return i;
1357 case rvc_normal:
1358 if (r->decimal)
1359 return decimal_real_to_integer (r);
1361 if (REAL_EXP (r) <= 0)
1362 goto underflow;
1363 /* Only force overflow for unsigned overflow. Signed overflow is
1364 undefined, so it doesn't matter what we return, and some callers
1365 expect to be able to use this routine for both signed and
1366 unsigned conversions. */
1367 if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1368 goto overflow;
1370 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1371 i = r->sig[SIGSZ-1];
1372 else
1374 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1375 i = r->sig[SIGSZ-1];
1376 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1377 i |= r->sig[SIGSZ-2];
1380 i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1382 if (r->sign)
1383 i = -i;
1384 return i;
1386 default:
1387 gcc_unreachable ();
1391 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1392 be represented in precision, *FAIL is set to TRUE. */
1394 wide_int
1395 real_to_integer (const REAL_VALUE_TYPE *r, bool *fail, int precision)
1397 HOST_WIDE_INT val[2 * WIDE_INT_MAX_ELTS];
1398 int exp;
1399 int words, w;
1400 wide_int result;
1402 switch (r->cl)
1404 case rvc_zero:
1405 underflow:
1406 return wi::zero (precision);
1408 case rvc_inf:
1409 case rvc_nan:
1410 overflow:
1411 *fail = true;
1413 if (r->sign)
1414 return wi::set_bit_in_zero (precision - 1, precision);
1415 else
1416 return ~wi::set_bit_in_zero (precision - 1, precision);
1418 case rvc_normal:
1419 if (r->decimal)
1420 return decimal_real_to_integer (r, fail, precision);
1422 exp = REAL_EXP (r);
1423 if (exp <= 0)
1424 goto underflow;
1425 /* Only force overflow for unsigned overflow. Signed overflow is
1426 undefined, so it doesn't matter what we return, and some callers
1427 expect to be able to use this routine for both signed and
1428 unsigned conversions. */
1429 if (exp > precision)
1430 goto overflow;
1432 /* Put the significand into a wide_int that has precision W, which
1433 is the smallest HWI-multiple that has at least PRECISION bits.
1434 This ensures that the top bit of the significand is in the
1435 top bit of the wide_int. */
1436 words = (precision + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT;
1437 w = words * HOST_BITS_PER_WIDE_INT;
1439 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1440 for (int i = 0; i < words; i++)
1442 int j = SIGSZ - words + i;
1443 val[i] = (j < 0) ? 0 : r->sig[j];
1445 #else
1446 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1447 for (int i = 0; i < words; i++)
1449 int j = SIGSZ - (words * 2) + (i * 2);
1450 if (j < 0)
1451 val[i] = 0;
1452 else
1453 val[i] = r->sig[j];
1454 j += 1;
1455 if (j >= 0)
1456 val[i] |= (unsigned HOST_WIDE_INT) r->sig[j] << HOST_BITS_PER_LONG;
1458 #endif
1459 /* Shift the value into place and truncate to the desired precision. */
1460 result = wide_int::from_array (val, words, w);
1461 result = wi::lrshift (result, w - exp);
1462 result = wide_int::from (result, precision, UNSIGNED);
1464 if (r->sign)
1465 return -result;
1466 else
1467 return result;
1469 default:
1470 gcc_unreachable ();
1474 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1475 of NUM / DEN. Return the quotient and place the remainder in NUM.
1476 It is expected that NUM / DEN are close enough that the quotient is
1477 small. */
1479 static unsigned long
1480 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1482 unsigned long q, msb;
1483 int expn = REAL_EXP (num), expd = REAL_EXP (den);
1485 if (expn < expd)
1486 return 0;
1488 q = msb = 0;
1489 goto start;
1492 msb = num->sig[SIGSZ-1] & SIG_MSB;
1493 q <<= 1;
1494 lshift_significand_1 (num, num);
1495 start:
1496 if (msb || cmp_significands (num, den) >= 0)
1498 sub_significands (num, num, den, 0);
1499 q |= 1;
1502 while (--expn >= expd);
1504 SET_REAL_EXP (num, expd);
1505 normalize (num);
1507 return q;
1510 /* Render R as a decimal floating point constant. Emit DIGITS significant
1511 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1512 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1513 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1514 to a string that, when parsed back in mode MODE, yields the same value. */
1516 #define M_LOG10_2 0.30102999566398119521
1518 void
1519 real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig,
1520 size_t buf_size, size_t digits,
1521 int crop_trailing_zeros, machine_mode mode)
1523 const struct real_format *fmt = NULL;
1524 const REAL_VALUE_TYPE *one, *ten;
1525 REAL_VALUE_TYPE r, pten, u, v;
1526 int dec_exp, cmp_one, digit;
1527 size_t max_digits;
1528 char *p, *first, *last;
1529 bool sign;
1530 bool round_up;
1532 if (mode != VOIDmode)
1534 fmt = REAL_MODE_FORMAT (mode);
1535 gcc_assert (fmt);
1538 r = *r_orig;
1539 switch (r.cl)
1541 case rvc_zero:
1542 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1543 return;
1544 case rvc_normal:
1545 break;
1546 case rvc_inf:
1547 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1548 return;
1549 case rvc_nan:
1550 /* ??? Print the significand as well, if not canonical? */
1551 sprintf (str, "%c%cNaN", (r_orig->sign ? '-' : '+'),
1552 (r_orig->signalling ? 'S' : 'Q'));
1553 return;
1554 default:
1555 gcc_unreachable ();
1558 if (r.decimal)
1560 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1561 return;
1564 /* Bound the number of digits printed by the size of the representation. */
1565 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1566 if (digits == 0 || digits > max_digits)
1567 digits = max_digits;
1569 /* Estimate the decimal exponent, and compute the length of the string it
1570 will print as. Be conservative and add one to account for possible
1571 overflow or rounding error. */
1572 dec_exp = REAL_EXP (&r) * M_LOG10_2;
1573 for (max_digits = 1; dec_exp ; max_digits++)
1574 dec_exp /= 10;
1576 /* Bound the number of digits printed by the size of the output buffer. */
1577 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1578 gcc_assert (max_digits <= buf_size);
1579 if (digits > max_digits)
1580 digits = max_digits;
1582 one = real_digit (1);
1583 ten = ten_to_ptwo (0);
1585 sign = r.sign;
1586 r.sign = 0;
1588 dec_exp = 0;
1589 pten = *one;
1591 cmp_one = do_compare (&r, one, 0);
1592 if (cmp_one > 0)
1594 int m;
1596 /* Number is greater than one. Convert significand to an integer
1597 and strip trailing decimal zeros. */
1599 u = r;
1600 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1602 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1603 m = floor_log2 (max_digits);
1605 /* Iterate over the bits of the possible powers of 10 that might
1606 be present in U and eliminate them. That is, if we find that
1607 10**2**M divides U evenly, keep the division and increase
1608 DEC_EXP by 2**M. */
1611 REAL_VALUE_TYPE t;
1613 do_divide (&t, &u, ten_to_ptwo (m));
1614 do_fix_trunc (&v, &t);
1615 if (cmp_significands (&v, &t) == 0)
1617 u = t;
1618 dec_exp += 1 << m;
1621 while (--m >= 0);
1623 /* Revert the scaling to integer that we performed earlier. */
1624 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1625 - (SIGNIFICAND_BITS - 1));
1626 r = u;
1628 /* Find power of 10. Do this by dividing out 10**2**M when
1629 this is larger than the current remainder. Fill PTEN with
1630 the power of 10 that we compute. */
1631 if (REAL_EXP (&r) > 0)
1633 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1636 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1637 if (do_compare (&u, ptentwo, 0) >= 0)
1639 do_divide (&u, &u, ptentwo);
1640 do_multiply (&pten, &pten, ptentwo);
1641 dec_exp += 1 << m;
1644 while (--m >= 0);
1646 else
1647 /* We managed to divide off enough tens in the above reduction
1648 loop that we've now got a negative exponent. Fall into the
1649 less-than-one code to compute the proper value for PTEN. */
1650 cmp_one = -1;
1652 if (cmp_one < 0)
1654 int m;
1656 /* Number is less than one. Pad significand with leading
1657 decimal zeros. */
1659 v = r;
1660 while (1)
1662 /* Stop if we'd shift bits off the bottom. */
1663 if (v.sig[0] & 7)
1664 break;
1666 do_multiply (&u, &v, ten);
1668 /* Stop if we're now >= 1. */
1669 if (REAL_EXP (&u) > 0)
1670 break;
1672 v = u;
1673 dec_exp -= 1;
1675 r = v;
1677 /* Find power of 10. Do this by multiplying in P=10**2**M when
1678 the current remainder is smaller than 1/P. Fill PTEN with the
1679 power of 10 that we compute. */
1680 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1683 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1684 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1686 if (do_compare (&v, ptenmtwo, 0) <= 0)
1688 do_multiply (&v, &v, ptentwo);
1689 do_multiply (&pten, &pten, ptentwo);
1690 dec_exp -= 1 << m;
1693 while (--m >= 0);
1695 /* Invert the positive power of 10 that we've collected so far. */
1696 do_divide (&pten, one, &pten);
1699 p = str;
1700 if (sign)
1701 *p++ = '-';
1702 first = p++;
1704 /* At this point, PTEN should contain the nearest power of 10 smaller
1705 than R, such that this division produces the first digit.
1707 Using a divide-step primitive that returns the complete integral
1708 remainder avoids the rounding error that would be produced if
1709 we were to use do_divide here and then simply multiply by 10 for
1710 each subsequent digit. */
1712 digit = rtd_divmod (&r, &pten);
1714 /* Be prepared for error in that division via underflow ... */
1715 if (digit == 0 && cmp_significand_0 (&r))
1717 /* Multiply by 10 and try again. */
1718 do_multiply (&r, &r, ten);
1719 digit = rtd_divmod (&r, &pten);
1720 dec_exp -= 1;
1721 gcc_assert (digit != 0);
1724 /* ... or overflow. */
1725 if (digit == 10)
1727 *p++ = '1';
1728 if (--digits > 0)
1729 *p++ = '0';
1730 dec_exp += 1;
1732 else
1734 gcc_assert (digit <= 10);
1735 *p++ = digit + '0';
1738 /* Generate subsequent digits. */
1739 while (--digits > 0)
1741 do_multiply (&r, &r, ten);
1742 digit = rtd_divmod (&r, &pten);
1743 *p++ = digit + '0';
1745 last = p;
1747 /* Generate one more digit with which to do rounding. */
1748 do_multiply (&r, &r, ten);
1749 digit = rtd_divmod (&r, &pten);
1751 /* Round the result. */
1752 if (fmt && fmt->round_towards_zero)
1754 /* If the format uses round towards zero when parsing the string
1755 back in, we need to always round away from zero here. */
1756 if (cmp_significand_0 (&r))
1757 digit++;
1758 round_up = digit > 0;
1760 else
1762 if (digit == 5)
1764 /* Round to nearest. If R is nonzero there are additional
1765 nonzero digits to be extracted. */
1766 if (cmp_significand_0 (&r))
1767 digit++;
1768 /* Round to even. */
1769 else if ((p[-1] - '0') & 1)
1770 digit++;
1773 round_up = digit > 5;
1776 if (round_up)
1778 while (p > first)
1780 digit = *--p;
1781 if (digit == '9')
1782 *p = '0';
1783 else
1785 *p = digit + 1;
1786 break;
1790 /* Carry out of the first digit. This means we had all 9's and
1791 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1792 if (p == first)
1794 first[1] = '1';
1795 dec_exp++;
1799 /* Insert the decimal point. */
1800 first[0] = first[1];
1801 first[1] = '.';
1803 /* If requested, drop trailing zeros. Never crop past "1.0". */
1804 if (crop_trailing_zeros)
1805 while (last > first + 3 && last[-1] == '0')
1806 last--;
1808 /* Append the exponent. */
1809 sprintf (last, "e%+d", dec_exp);
1811 #ifdef ENABLE_CHECKING
1812 /* Verify that we can read the original value back in. */
1813 if (mode != VOIDmode)
1815 real_from_string (&r, str);
1816 real_convert (&r, mode, &r);
1817 gcc_assert (real_identical (&r, r_orig));
1819 #endif
1822 /* Likewise, except always uses round-to-nearest. */
1824 void
1825 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1826 size_t digits, int crop_trailing_zeros)
1828 real_to_decimal_for_mode (str, r_orig, buf_size,
1829 digits, crop_trailing_zeros, VOIDmode);
1832 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1833 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1834 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1835 strip trailing zeros. */
1837 void
1838 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1839 size_t digits, int crop_trailing_zeros)
1841 int i, j, exp = REAL_EXP (r);
1842 char *p, *first;
1843 char exp_buf[16];
1844 size_t max_digits;
1846 switch (r->cl)
1848 case rvc_zero:
1849 exp = 0;
1850 break;
1851 case rvc_normal:
1852 break;
1853 case rvc_inf:
1854 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1855 return;
1856 case rvc_nan:
1857 /* ??? Print the significand as well, if not canonical? */
1858 sprintf (str, "%c%cNaN", (r->sign ? '-' : '+'),
1859 (r->signalling ? 'S' : 'Q'));
1860 return;
1861 default:
1862 gcc_unreachable ();
1865 if (r->decimal)
1867 /* Hexadecimal format for decimal floats is not interesting. */
1868 strcpy (str, "N/A");
1869 return;
1872 if (digits == 0)
1873 digits = SIGNIFICAND_BITS / 4;
1875 /* Bound the number of digits printed by the size of the output buffer. */
1877 sprintf (exp_buf, "p%+d", exp);
1878 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1879 gcc_assert (max_digits <= buf_size);
1880 if (digits > max_digits)
1881 digits = max_digits;
1883 p = str;
1884 if (r->sign)
1885 *p++ = '-';
1886 *p++ = '0';
1887 *p++ = 'x';
1888 *p++ = '0';
1889 *p++ = '.';
1890 first = p;
1892 for (i = SIGSZ - 1; i >= 0; --i)
1893 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1895 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1896 if (--digits == 0)
1897 goto out;
1900 out:
1901 if (crop_trailing_zeros)
1902 while (p > first + 1 && p[-1] == '0')
1903 p--;
1905 sprintf (p, "p%+d", exp);
1908 /* Initialize R from a decimal or hexadecimal string. The string is
1909 assumed to have been syntax checked already. Return -1 if the
1910 value underflows, +1 if overflows, and 0 otherwise. */
1913 real_from_string (REAL_VALUE_TYPE *r, const char *str)
1915 int exp = 0;
1916 bool sign = false;
1918 get_zero (r, 0);
1920 if (*str == '-')
1922 sign = true;
1923 str++;
1925 else if (*str == '+')
1926 str++;
1928 if (!strncmp (str, "QNaN", 4))
1930 get_canonical_qnan (r, sign);
1931 return 0;
1933 else if (!strncmp (str, "SNaN", 4))
1935 get_canonical_snan (r, sign);
1936 return 0;
1938 else if (!strncmp (str, "Inf", 3))
1940 get_inf (r, sign);
1941 return 0;
1944 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1946 /* Hexadecimal floating point. */
1947 int pos = SIGNIFICAND_BITS - 4, d;
1949 str += 2;
1951 while (*str == '0')
1952 str++;
1953 while (1)
1955 d = hex_value (*str);
1956 if (d == _hex_bad)
1957 break;
1958 if (pos >= 0)
1960 r->sig[pos / HOST_BITS_PER_LONG]
1961 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1962 pos -= 4;
1964 else if (d)
1965 /* Ensure correct rounding by setting last bit if there is
1966 a subsequent nonzero digit. */
1967 r->sig[0] |= 1;
1968 exp += 4;
1969 str++;
1971 if (*str == '.')
1973 str++;
1974 if (pos == SIGNIFICAND_BITS - 4)
1976 while (*str == '0')
1977 str++, exp -= 4;
1979 while (1)
1981 d = hex_value (*str);
1982 if (d == _hex_bad)
1983 break;
1984 if (pos >= 0)
1986 r->sig[pos / HOST_BITS_PER_LONG]
1987 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1988 pos -= 4;
1990 else if (d)
1991 /* Ensure correct rounding by setting last bit if there is
1992 a subsequent nonzero digit. */
1993 r->sig[0] |= 1;
1994 str++;
1998 /* If the mantissa is zero, ignore the exponent. */
1999 if (!cmp_significand_0 (r))
2000 goto is_a_zero;
2002 if (*str == 'p' || *str == 'P')
2004 bool exp_neg = false;
2006 str++;
2007 if (*str == '-')
2009 exp_neg = true;
2010 str++;
2012 else if (*str == '+')
2013 str++;
2015 d = 0;
2016 while (ISDIGIT (*str))
2018 d *= 10;
2019 d += *str - '0';
2020 if (d > MAX_EXP)
2022 /* Overflowed the exponent. */
2023 if (exp_neg)
2024 goto underflow;
2025 else
2026 goto overflow;
2028 str++;
2030 if (exp_neg)
2031 d = -d;
2033 exp += d;
2036 r->cl = rvc_normal;
2037 SET_REAL_EXP (r, exp);
2039 normalize (r);
2041 else
2043 /* Decimal floating point. */
2044 const char *cstr = str;
2045 mpfr_t m;
2046 bool inexact;
2048 while (*cstr == '0')
2049 cstr++;
2050 if (*cstr == '.')
2052 cstr++;
2053 while (*cstr == '0')
2054 cstr++;
2057 /* If the mantissa is zero, ignore the exponent. */
2058 if (!ISDIGIT (*cstr))
2059 goto is_a_zero;
2061 /* Nonzero value, possibly overflowing or underflowing. */
2062 mpfr_init2 (m, SIGNIFICAND_BITS);
2063 inexact = mpfr_strtofr (m, str, NULL, 10, GMP_RNDZ);
2064 /* The result should never be a NaN, and because the rounding is
2065 toward zero should never be an infinity. */
2066 gcc_assert (!mpfr_nan_p (m) && !mpfr_inf_p (m));
2067 if (mpfr_zero_p (m) || mpfr_get_exp (m) < -MAX_EXP + 4)
2069 mpfr_clear (m);
2070 goto underflow;
2072 else if (mpfr_get_exp (m) > MAX_EXP - 4)
2074 mpfr_clear (m);
2075 goto overflow;
2077 else
2079 real_from_mpfr (r, m, NULL_TREE, GMP_RNDZ);
2080 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2081 because the hex digits used in real_from_mpfr did not
2082 start with a digit 8 to f, but the exponent bounds above
2083 should have avoided underflow or overflow. */
2084 gcc_assert (r->cl == rvc_normal);
2085 /* Set a sticky bit if mpfr_strtofr was inexact. */
2086 r->sig[0] |= inexact;
2087 mpfr_clear (m);
2091 r->sign = sign;
2092 return 0;
2094 is_a_zero:
2095 get_zero (r, sign);
2096 return 0;
2098 underflow:
2099 get_zero (r, sign);
2100 return -1;
2102 overflow:
2103 get_inf (r, sign);
2104 return 1;
2107 /* Legacy. Similar, but return the result directly. */
2109 REAL_VALUE_TYPE
2110 real_from_string2 (const char *s, machine_mode mode)
2112 REAL_VALUE_TYPE r;
2114 real_from_string (&r, s);
2115 if (mode != VOIDmode)
2116 real_convert (&r, mode, &r);
2118 return r;
2121 /* Initialize R from string S and desired MODE. */
2123 void
2124 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, machine_mode mode)
2126 if (DECIMAL_FLOAT_MODE_P (mode))
2127 decimal_real_from_string (r, s);
2128 else
2129 real_from_string (r, s);
2131 if (mode != VOIDmode)
2132 real_convert (r, mode, r);
2135 /* Initialize R from the wide_int VAL_IN. The MODE is not VOIDmode,*/
2137 void
2138 real_from_integer (REAL_VALUE_TYPE *r, machine_mode mode,
2139 const wide_int_ref &val_in, signop sgn)
2141 if (val_in == 0)
2142 get_zero (r, 0);
2143 else
2145 unsigned int len = val_in.get_precision ();
2146 int i, j, e = 0;
2147 int maxbitlen = MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT;
2148 const unsigned int realmax = (SIGNIFICAND_BITS / HOST_BITS_PER_WIDE_INT
2149 * HOST_BITS_PER_WIDE_INT);
2151 memset (r, 0, sizeof (*r));
2152 r->cl = rvc_normal;
2153 r->sign = wi::neg_p (val_in, sgn);
2155 /* We have to ensure we can negate the largest negative number. */
2156 wide_int val = wide_int::from (val_in, maxbitlen, sgn);
2158 if (r->sign)
2159 val = -val;
2161 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2162 won't work with precisions that are not a multiple of
2163 HOST_BITS_PER_WIDE_INT. */
2164 len += HOST_BITS_PER_WIDE_INT - 1;
2166 /* Ensure we can represent the largest negative number. */
2167 len += 1;
2169 len = len/HOST_BITS_PER_WIDE_INT * HOST_BITS_PER_WIDE_INT;
2171 /* Cap the size to the size allowed by real.h. */
2172 if (len > realmax)
2174 HOST_WIDE_INT cnt_l_z;
2175 cnt_l_z = wi::clz (val);
2177 if (maxbitlen - cnt_l_z > realmax)
2179 e = maxbitlen - cnt_l_z - realmax;
2181 /* This value is too large, we must shift it right to
2182 preserve all the bits we can, and then bump the
2183 exponent up by that amount. */
2184 val = wi::lrshift (val, e);
2186 len = realmax;
2189 /* Clear out top bits so elt will work with precisions that aren't
2190 a multiple of HOST_BITS_PER_WIDE_INT. */
2191 val = wide_int::from (val, len, sgn);
2192 len = len / HOST_BITS_PER_WIDE_INT;
2194 SET_REAL_EXP (r, len * HOST_BITS_PER_WIDE_INT + e);
2196 j = SIGSZ - 1;
2197 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2198 for (i = len - 1; i >= 0; i--)
2200 r->sig[j--] = val.elt (i);
2201 if (j < 0)
2202 break;
2204 else
2206 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2207 for (i = len - 1; i >= 0; i--)
2209 HOST_WIDE_INT e = val.elt (i);
2210 r->sig[j--] = e >> (HOST_BITS_PER_LONG - 1) >> 1;
2211 if (j < 0)
2212 break;
2213 r->sig[j--] = e;
2214 if (j < 0)
2215 break;
2219 normalize (r);
2222 if (DECIMAL_FLOAT_MODE_P (mode))
2223 decimal_from_integer (r);
2224 else if (mode != VOIDmode)
2225 real_convert (r, mode, r);
2228 /* Render R, an integral value, as a floating point constant with no
2229 specified exponent. */
2231 static void
2232 decimal_integer_string (char *str, const REAL_VALUE_TYPE *r_orig,
2233 size_t buf_size)
2235 int dec_exp, digit, digits;
2236 REAL_VALUE_TYPE r, pten;
2237 char *p;
2238 bool sign;
2240 r = *r_orig;
2242 if (r.cl == rvc_zero)
2244 strcpy (str, "0.");
2245 return;
2248 sign = r.sign;
2249 r.sign = 0;
2251 dec_exp = REAL_EXP (&r) * M_LOG10_2;
2252 digits = dec_exp + 1;
2253 gcc_assert ((digits + 2) < (int)buf_size);
2255 pten = *real_digit (1);
2256 times_pten (&pten, dec_exp);
2258 p = str;
2259 if (sign)
2260 *p++ = '-';
2262 digit = rtd_divmod (&r, &pten);
2263 gcc_assert (digit >= 0 && digit <= 9);
2264 *p++ = digit + '0';
2265 while (--digits > 0)
2267 times_pten (&r, 1);
2268 digit = rtd_divmod (&r, &pten);
2269 *p++ = digit + '0';
2271 *p++ = '.';
2272 *p++ = '\0';
2275 /* Convert a real with an integral value to decimal float. */
2277 static void
2278 decimal_from_integer (REAL_VALUE_TYPE *r)
2280 char str[256];
2282 decimal_integer_string (str, r, sizeof (str) - 1);
2283 decimal_real_from_string (r, str);
2286 /* Returns 10**2**N. */
2288 static const REAL_VALUE_TYPE *
2289 ten_to_ptwo (int n)
2291 static REAL_VALUE_TYPE tens[EXP_BITS];
2293 gcc_assert (n >= 0);
2294 gcc_assert (n < EXP_BITS);
2296 if (tens[n].cl == rvc_zero)
2298 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2300 HOST_WIDE_INT t = 10;
2301 int i;
2303 for (i = 0; i < n; ++i)
2304 t *= t;
2306 real_from_integer (&tens[n], VOIDmode, t, UNSIGNED);
2308 else
2310 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2311 do_multiply (&tens[n], t, t);
2315 return &tens[n];
2318 /* Returns 10**(-2**N). */
2320 static const REAL_VALUE_TYPE *
2321 ten_to_mptwo (int n)
2323 static REAL_VALUE_TYPE tens[EXP_BITS];
2325 gcc_assert (n >= 0);
2326 gcc_assert (n < EXP_BITS);
2328 if (tens[n].cl == rvc_zero)
2329 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2331 return &tens[n];
2334 /* Returns N. */
2336 static const REAL_VALUE_TYPE *
2337 real_digit (int n)
2339 static REAL_VALUE_TYPE num[10];
2341 gcc_assert (n >= 0);
2342 gcc_assert (n <= 9);
2344 if (n > 0 && num[n].cl == rvc_zero)
2345 real_from_integer (&num[n], VOIDmode, n, UNSIGNED);
2347 return &num[n];
2350 /* Multiply R by 10**EXP. */
2352 static void
2353 times_pten (REAL_VALUE_TYPE *r, int exp)
2355 REAL_VALUE_TYPE pten, *rr;
2356 bool negative = (exp < 0);
2357 int i;
2359 if (negative)
2361 exp = -exp;
2362 pten = *real_digit (1);
2363 rr = &pten;
2365 else
2366 rr = r;
2368 for (i = 0; exp > 0; ++i, exp >>= 1)
2369 if (exp & 1)
2370 do_multiply (rr, rr, ten_to_ptwo (i));
2372 if (negative)
2373 do_divide (r, r, &pten);
2376 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2378 const REAL_VALUE_TYPE *
2379 dconst_e_ptr (void)
2381 static REAL_VALUE_TYPE value;
2383 /* Initialize mathematical constants for constant folding builtins.
2384 These constants need to be given to at least 160 bits precision. */
2385 if (value.cl == rvc_zero)
2387 mpfr_t m;
2388 mpfr_init2 (m, SIGNIFICAND_BITS);
2389 mpfr_set_ui (m, 1, GMP_RNDN);
2390 mpfr_exp (m, m, GMP_RNDN);
2391 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2392 mpfr_clear (m);
2395 return &value;
2398 /* Returns a cached REAL_VALUE_TYPE corresponding to 1/n, for various n. */
2400 #define CACHED_FRACTION(NAME, N) \
2401 const REAL_VALUE_TYPE * \
2402 NAME (void) \
2404 static REAL_VALUE_TYPE value; \
2406 /* Initialize mathematical constants for constant folding builtins. \
2407 These constants need to be given to at least 160 bits \
2408 precision. */ \
2409 if (value.cl == rvc_zero) \
2410 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (N)); \
2411 return &value; \
2414 CACHED_FRACTION (dconst_third_ptr, 3)
2415 CACHED_FRACTION (dconst_quarter_ptr, 4)
2416 CACHED_FRACTION (dconst_sixth_ptr, 6)
2417 CACHED_FRACTION (dconst_ninth_ptr, 9)
2419 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2421 const REAL_VALUE_TYPE *
2422 dconst_sqrt2_ptr (void)
2424 static REAL_VALUE_TYPE value;
2426 /* Initialize mathematical constants for constant folding builtins.
2427 These constants need to be given to at least 160 bits precision. */
2428 if (value.cl == rvc_zero)
2430 mpfr_t m;
2431 mpfr_init2 (m, SIGNIFICAND_BITS);
2432 mpfr_sqrt_ui (m, 2, GMP_RNDN);
2433 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2434 mpfr_clear (m);
2436 return &value;
2439 /* Fills R with +Inf. */
2441 void
2442 real_inf (REAL_VALUE_TYPE *r)
2444 get_inf (r, 0);
2447 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2448 we force a QNaN, else we force an SNaN. The string, if not empty,
2449 is parsed as a number and placed in the significand. Return true
2450 if the string was successfully parsed. */
2452 bool
2453 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2454 machine_mode mode)
2456 const struct real_format *fmt;
2458 fmt = REAL_MODE_FORMAT (mode);
2459 gcc_assert (fmt);
2461 if (*str == 0)
2463 if (quiet)
2464 get_canonical_qnan (r, 0);
2465 else
2466 get_canonical_snan (r, 0);
2468 else
2470 int base = 10, d;
2472 memset (r, 0, sizeof (*r));
2473 r->cl = rvc_nan;
2475 /* Parse akin to strtol into the significand of R. */
2477 while (ISSPACE (*str))
2478 str++;
2479 if (*str == '-')
2480 str++;
2481 else if (*str == '+')
2482 str++;
2483 if (*str == '0')
2485 str++;
2486 if (*str == 'x' || *str == 'X')
2488 base = 16;
2489 str++;
2491 else
2492 base = 8;
2495 while ((d = hex_value (*str)) < base)
2497 REAL_VALUE_TYPE u;
2499 switch (base)
2501 case 8:
2502 lshift_significand (r, r, 3);
2503 break;
2504 case 16:
2505 lshift_significand (r, r, 4);
2506 break;
2507 case 10:
2508 lshift_significand_1 (&u, r);
2509 lshift_significand (r, r, 3);
2510 add_significands (r, r, &u);
2511 break;
2512 default:
2513 gcc_unreachable ();
2516 get_zero (&u, 0);
2517 u.sig[0] = d;
2518 add_significands (r, r, &u);
2520 str++;
2523 /* Must have consumed the entire string for success. */
2524 if (*str != 0)
2525 return false;
2527 /* Shift the significand into place such that the bits
2528 are in the most significant bits for the format. */
2529 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2531 /* Our MSB is always unset for NaNs. */
2532 r->sig[SIGSZ-1] &= ~SIG_MSB;
2534 /* Force quiet or signalling NaN. */
2535 r->signalling = !quiet;
2538 return true;
2541 /* Fills R with the largest finite value representable in mode MODE.
2542 If SIGN is nonzero, R is set to the most negative finite value. */
2544 void
2545 real_maxval (REAL_VALUE_TYPE *r, int sign, machine_mode mode)
2547 const struct real_format *fmt;
2548 int np2;
2550 fmt = REAL_MODE_FORMAT (mode);
2551 gcc_assert (fmt);
2552 memset (r, 0, sizeof (*r));
2554 if (fmt->b == 10)
2555 decimal_real_maxval (r, sign, mode);
2556 else
2558 r->cl = rvc_normal;
2559 r->sign = sign;
2560 SET_REAL_EXP (r, fmt->emax);
2562 np2 = SIGNIFICAND_BITS - fmt->p;
2563 memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2564 clear_significand_below (r, np2);
2566 if (fmt->pnan < fmt->p)
2567 /* This is an IBM extended double format made up of two IEEE
2568 doubles. The value of the long double is the sum of the
2569 values of the two parts. The most significant part is
2570 required to be the value of the long double rounded to the
2571 nearest double. Rounding means we need a slightly smaller
2572 value for LDBL_MAX. */
2573 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1);
2577 /* Fills R with 2**N. */
2579 void
2580 real_2expN (REAL_VALUE_TYPE *r, int n, machine_mode fmode)
2582 memset (r, 0, sizeof (*r));
2584 n++;
2585 if (n > MAX_EXP)
2586 r->cl = rvc_inf;
2587 else if (n < -MAX_EXP)
2589 else
2591 r->cl = rvc_normal;
2592 SET_REAL_EXP (r, n);
2593 r->sig[SIGSZ-1] = SIG_MSB;
2595 if (DECIMAL_FLOAT_MODE_P (fmode))
2596 decimal_real_convert (r, fmode, r);
2600 static void
2601 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2603 int p2, np2, i, w;
2604 int emin2m1, emax2;
2605 bool round_up = false;
2607 if (r->decimal)
2609 if (fmt->b == 10)
2611 decimal_round_for_format (fmt, r);
2612 return;
2614 /* FIXME. We can come here via fp_easy_constant
2615 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2616 investigated whether this convert needs to be here, or
2617 something else is missing. */
2618 decimal_real_convert (r, DFmode, r);
2621 p2 = fmt->p;
2622 emin2m1 = fmt->emin - 1;
2623 emax2 = fmt->emax;
2625 np2 = SIGNIFICAND_BITS - p2;
2626 switch (r->cl)
2628 underflow:
2629 get_zero (r, r->sign);
2630 case rvc_zero:
2631 if (!fmt->has_signed_zero)
2632 r->sign = 0;
2633 return;
2635 overflow:
2636 get_inf (r, r->sign);
2637 case rvc_inf:
2638 return;
2640 case rvc_nan:
2641 clear_significand_below (r, np2);
2642 return;
2644 case rvc_normal:
2645 break;
2647 default:
2648 gcc_unreachable ();
2651 /* Check the range of the exponent. If we're out of range,
2652 either underflow or overflow. */
2653 if (REAL_EXP (r) > emax2)
2654 goto overflow;
2655 else if (REAL_EXP (r) <= emin2m1)
2657 int diff;
2659 if (!fmt->has_denorm)
2661 /* Don't underflow completely until we've had a chance to round. */
2662 if (REAL_EXP (r) < emin2m1)
2663 goto underflow;
2665 else
2667 diff = emin2m1 - REAL_EXP (r) + 1;
2668 if (diff > p2)
2669 goto underflow;
2671 /* De-normalize the significand. */
2672 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2673 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2677 if (!fmt->round_towards_zero)
2679 /* There are P2 true significand bits, followed by one guard bit,
2680 followed by one sticky bit, followed by stuff. Fold nonzero
2681 stuff into the sticky bit. */
2682 unsigned long sticky;
2683 bool guard, lsb;
2685 sticky = 0;
2686 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2687 sticky |= r->sig[i];
2688 sticky |= r->sig[w]
2689 & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2691 guard = test_significand_bit (r, np2 - 1);
2692 lsb = test_significand_bit (r, np2);
2694 /* Round to even. */
2695 round_up = guard && (sticky || lsb);
2698 if (round_up)
2700 REAL_VALUE_TYPE u;
2701 get_zero (&u, 0);
2702 set_significand_bit (&u, np2);
2704 if (add_significands (r, r, &u))
2706 /* Overflow. Means the significand had been all ones, and
2707 is now all zeros. Need to increase the exponent, and
2708 possibly re-normalize it. */
2709 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2710 if (REAL_EXP (r) > emax2)
2711 goto overflow;
2712 r->sig[SIGSZ-1] = SIG_MSB;
2716 /* Catch underflow that we deferred until after rounding. */
2717 if (REAL_EXP (r) <= emin2m1)
2718 goto underflow;
2720 /* Clear out trailing garbage. */
2721 clear_significand_below (r, np2);
2724 /* Extend or truncate to a new mode. */
2726 void
2727 real_convert (REAL_VALUE_TYPE *r, machine_mode mode,
2728 const REAL_VALUE_TYPE *a)
2730 const struct real_format *fmt;
2732 fmt = REAL_MODE_FORMAT (mode);
2733 gcc_assert (fmt);
2735 *r = *a;
2737 if (a->decimal || fmt->b == 10)
2738 decimal_real_convert (r, mode, a);
2740 round_for_format (fmt, r);
2742 /* round_for_format de-normalizes denormals. Undo just that part. */
2743 if (r->cl == rvc_normal)
2744 normalize (r);
2747 /* Legacy. Likewise, except return the struct directly. */
2749 REAL_VALUE_TYPE
2750 real_value_truncate (machine_mode mode, REAL_VALUE_TYPE a)
2752 REAL_VALUE_TYPE r;
2753 real_convert (&r, mode, &a);
2754 return r;
2757 /* Return true if truncating to MODE is exact. */
2759 bool
2760 exact_real_truncate (machine_mode mode, const REAL_VALUE_TYPE *a)
2762 const struct real_format *fmt;
2763 REAL_VALUE_TYPE t;
2764 int emin2m1;
2766 fmt = REAL_MODE_FORMAT (mode);
2767 gcc_assert (fmt);
2769 /* Don't allow conversion to denormals. */
2770 emin2m1 = fmt->emin - 1;
2771 if (REAL_EXP (a) <= emin2m1)
2772 return false;
2774 /* After conversion to the new mode, the value must be identical. */
2775 real_convert (&t, mode, a);
2776 return real_identical (&t, a);
2779 /* Write R to the given target format. Place the words of the result
2780 in target word order in BUF. There are always 32 bits in each
2781 long, no matter the size of the host long.
2783 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2785 long
2786 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
2787 const struct real_format *fmt)
2789 REAL_VALUE_TYPE r;
2790 long buf1;
2792 r = *r_orig;
2793 round_for_format (fmt, &r);
2795 if (!buf)
2796 buf = &buf1;
2797 (*fmt->encode) (fmt, buf, &r);
2799 return *buf;
2802 /* Similar, but look up the format from MODE. */
2804 long
2805 real_to_target (long *buf, const REAL_VALUE_TYPE *r, machine_mode mode)
2807 const struct real_format *fmt;
2809 fmt = REAL_MODE_FORMAT (mode);
2810 gcc_assert (fmt);
2812 return real_to_target_fmt (buf, r, fmt);
2815 /* Read R from the given target format. Read the words of the result
2816 in target word order in BUF. There are always 32 bits in each
2817 long, no matter the size of the host long. */
2819 void
2820 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
2821 const struct real_format *fmt)
2823 (*fmt->decode) (fmt, r, buf);
2826 /* Similar, but look up the format from MODE. */
2828 void
2829 real_from_target (REAL_VALUE_TYPE *r, const long *buf, machine_mode mode)
2831 const struct real_format *fmt;
2833 fmt = REAL_MODE_FORMAT (mode);
2834 gcc_assert (fmt);
2836 (*fmt->decode) (fmt, r, buf);
2839 /* Return the number of bits of the largest binary value that the
2840 significand of MODE will hold. */
2841 /* ??? Legacy. Should get access to real_format directly. */
2844 significand_size (machine_mode mode)
2846 const struct real_format *fmt;
2848 fmt = REAL_MODE_FORMAT (mode);
2849 if (fmt == NULL)
2850 return 0;
2852 if (fmt->b == 10)
2854 /* Return the size in bits of the largest binary value that can be
2855 held by the decimal coefficient for this mode. This is one more
2856 than the number of bits required to hold the largest coefficient
2857 of this mode. */
2858 double log2_10 = 3.3219281;
2859 return fmt->p * log2_10;
2861 return fmt->p;
2864 /* Return a hash value for the given real value. */
2865 /* ??? The "unsigned int" return value is intended to be hashval_t,
2866 but I didn't want to pull hashtab.h into real.h. */
2868 unsigned int
2869 real_hash (const REAL_VALUE_TYPE *r)
2871 unsigned int h;
2872 size_t i;
2874 h = r->cl | (r->sign << 2);
2875 switch (r->cl)
2877 case rvc_zero:
2878 case rvc_inf:
2879 return h;
2881 case rvc_normal:
2882 h |= REAL_EXP (r) << 3;
2883 break;
2885 case rvc_nan:
2886 if (r->signalling)
2887 h ^= (unsigned int)-1;
2888 if (r->canonical)
2889 return h;
2890 break;
2892 default:
2893 gcc_unreachable ();
2896 if (sizeof (unsigned long) > sizeof (unsigned int))
2897 for (i = 0; i < SIGSZ; ++i)
2899 unsigned long s = r->sig[i];
2900 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2902 else
2903 for (i = 0; i < SIGSZ; ++i)
2904 h ^= r->sig[i];
2906 return h;
2909 /* IEEE single-precision format. */
2911 static void encode_ieee_single (const struct real_format *fmt,
2912 long *, const REAL_VALUE_TYPE *);
2913 static void decode_ieee_single (const struct real_format *,
2914 REAL_VALUE_TYPE *, const long *);
2916 static void
2917 encode_ieee_single (const struct real_format *fmt, long *buf,
2918 const REAL_VALUE_TYPE *r)
2920 unsigned long image, sig, exp;
2921 unsigned long sign = r->sign;
2922 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2924 image = sign << 31;
2925 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2927 switch (r->cl)
2929 case rvc_zero:
2930 break;
2932 case rvc_inf:
2933 if (fmt->has_inf)
2934 image |= 255 << 23;
2935 else
2936 image |= 0x7fffffff;
2937 break;
2939 case rvc_nan:
2940 if (fmt->has_nans)
2942 if (r->canonical)
2943 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
2944 if (r->signalling == fmt->qnan_msb_set)
2945 sig &= ~(1 << 22);
2946 else
2947 sig |= 1 << 22;
2948 if (sig == 0)
2949 sig = 1 << 21;
2951 image |= 255 << 23;
2952 image |= sig;
2954 else
2955 image |= 0x7fffffff;
2956 break;
2958 case rvc_normal:
2959 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2960 whereas the intermediate representation is 0.F x 2**exp.
2961 Which means we're off by one. */
2962 if (denormal)
2963 exp = 0;
2964 else
2965 exp = REAL_EXP (r) + 127 - 1;
2966 image |= exp << 23;
2967 image |= sig;
2968 break;
2970 default:
2971 gcc_unreachable ();
2974 buf[0] = image;
2977 static void
2978 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2979 const long *buf)
2981 unsigned long image = buf[0] & 0xffffffff;
2982 bool sign = (image >> 31) & 1;
2983 int exp = (image >> 23) & 0xff;
2985 memset (r, 0, sizeof (*r));
2986 image <<= HOST_BITS_PER_LONG - 24;
2987 image &= ~SIG_MSB;
2989 if (exp == 0)
2991 if (image && fmt->has_denorm)
2993 r->cl = rvc_normal;
2994 r->sign = sign;
2995 SET_REAL_EXP (r, -126);
2996 r->sig[SIGSZ-1] = image << 1;
2997 normalize (r);
2999 else if (fmt->has_signed_zero)
3000 r->sign = sign;
3002 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
3004 if (image)
3006 r->cl = rvc_nan;
3007 r->sign = sign;
3008 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
3009 ^ fmt->qnan_msb_set);
3010 r->sig[SIGSZ-1] = image;
3012 else
3014 r->cl = rvc_inf;
3015 r->sign = sign;
3018 else
3020 r->cl = rvc_normal;
3021 r->sign = sign;
3022 SET_REAL_EXP (r, exp - 127 + 1);
3023 r->sig[SIGSZ-1] = image | SIG_MSB;
3027 const struct real_format ieee_single_format =
3029 encode_ieee_single,
3030 decode_ieee_single,
3034 -125,
3035 128,
3038 false,
3039 true,
3040 true,
3041 true,
3042 true,
3043 true,
3044 true,
3045 false,
3046 "ieee_single"
3049 const struct real_format mips_single_format =
3051 encode_ieee_single,
3052 decode_ieee_single,
3056 -125,
3057 128,
3060 false,
3061 true,
3062 true,
3063 true,
3064 true,
3065 true,
3066 false,
3067 true,
3068 "mips_single"
3071 const struct real_format motorola_single_format =
3073 encode_ieee_single,
3074 decode_ieee_single,
3078 -125,
3079 128,
3082 false,
3083 true,
3084 true,
3085 true,
3086 true,
3087 true,
3088 true,
3089 true,
3090 "motorola_single"
3093 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3094 single precision with the following differences:
3095 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3096 are generated.
3097 - NaNs are not supported.
3098 - The range of non-zero numbers in binary is
3099 (001)[1.]000...000 to (255)[1.]111...111.
3100 - Denormals can be represented, but are treated as +0.0 when
3101 used as an operand and are never generated as a result.
3102 - -0.0 can be represented, but a zero result is always +0.0.
3103 - the only supported rounding mode is trunction (towards zero). */
3104 const struct real_format spu_single_format =
3106 encode_ieee_single,
3107 decode_ieee_single,
3111 -125,
3112 129,
3115 true,
3116 false,
3117 false,
3118 false,
3119 true,
3120 true,
3121 false,
3122 false,
3123 "spu_single"
3126 /* IEEE double-precision format. */
3128 static void encode_ieee_double (const struct real_format *fmt,
3129 long *, const REAL_VALUE_TYPE *);
3130 static void decode_ieee_double (const struct real_format *,
3131 REAL_VALUE_TYPE *, const long *);
3133 static void
3134 encode_ieee_double (const struct real_format *fmt, long *buf,
3135 const REAL_VALUE_TYPE *r)
3137 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
3138 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3140 image_hi = r->sign << 31;
3141 image_lo = 0;
3143 if (HOST_BITS_PER_LONG == 64)
3145 sig_hi = r->sig[SIGSZ-1];
3146 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
3147 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
3149 else
3151 sig_hi = r->sig[SIGSZ-1];
3152 sig_lo = r->sig[SIGSZ-2];
3153 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
3154 sig_hi = (sig_hi >> 11) & 0xfffff;
3157 switch (r->cl)
3159 case rvc_zero:
3160 break;
3162 case rvc_inf:
3163 if (fmt->has_inf)
3164 image_hi |= 2047 << 20;
3165 else
3167 image_hi |= 0x7fffffff;
3168 image_lo = 0xffffffff;
3170 break;
3172 case rvc_nan:
3173 if (fmt->has_nans)
3175 if (r->canonical)
3177 if (fmt->canonical_nan_lsbs_set)
3179 sig_hi = (1 << 19) - 1;
3180 sig_lo = 0xffffffff;
3182 else
3184 sig_hi = 0;
3185 sig_lo = 0;
3188 if (r->signalling == fmt->qnan_msb_set)
3189 sig_hi &= ~(1 << 19);
3190 else
3191 sig_hi |= 1 << 19;
3192 if (sig_hi == 0 && sig_lo == 0)
3193 sig_hi = 1 << 18;
3195 image_hi |= 2047 << 20;
3196 image_hi |= sig_hi;
3197 image_lo = sig_lo;
3199 else
3201 image_hi |= 0x7fffffff;
3202 image_lo = 0xffffffff;
3204 break;
3206 case rvc_normal:
3207 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3208 whereas the intermediate representation is 0.F x 2**exp.
3209 Which means we're off by one. */
3210 if (denormal)
3211 exp = 0;
3212 else
3213 exp = REAL_EXP (r) + 1023 - 1;
3214 image_hi |= exp << 20;
3215 image_hi |= sig_hi;
3216 image_lo = sig_lo;
3217 break;
3219 default:
3220 gcc_unreachable ();
3223 if (FLOAT_WORDS_BIG_ENDIAN)
3224 buf[0] = image_hi, buf[1] = image_lo;
3225 else
3226 buf[0] = image_lo, buf[1] = image_hi;
3229 static void
3230 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3231 const long *buf)
3233 unsigned long image_hi, image_lo;
3234 bool sign;
3235 int exp;
3237 if (FLOAT_WORDS_BIG_ENDIAN)
3238 image_hi = buf[0], image_lo = buf[1];
3239 else
3240 image_lo = buf[0], image_hi = buf[1];
3241 image_lo &= 0xffffffff;
3242 image_hi &= 0xffffffff;
3244 sign = (image_hi >> 31) & 1;
3245 exp = (image_hi >> 20) & 0x7ff;
3247 memset (r, 0, sizeof (*r));
3249 image_hi <<= 32 - 21;
3250 image_hi |= image_lo >> 21;
3251 image_hi &= 0x7fffffff;
3252 image_lo <<= 32 - 21;
3254 if (exp == 0)
3256 if ((image_hi || image_lo) && fmt->has_denorm)
3258 r->cl = rvc_normal;
3259 r->sign = sign;
3260 SET_REAL_EXP (r, -1022);
3261 if (HOST_BITS_PER_LONG == 32)
3263 image_hi = (image_hi << 1) | (image_lo >> 31);
3264 image_lo <<= 1;
3265 r->sig[SIGSZ-1] = image_hi;
3266 r->sig[SIGSZ-2] = image_lo;
3268 else
3270 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
3271 r->sig[SIGSZ-1] = image_hi;
3273 normalize (r);
3275 else if (fmt->has_signed_zero)
3276 r->sign = sign;
3278 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
3280 if (image_hi || image_lo)
3282 r->cl = rvc_nan;
3283 r->sign = sign;
3284 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3285 if (HOST_BITS_PER_LONG == 32)
3287 r->sig[SIGSZ-1] = image_hi;
3288 r->sig[SIGSZ-2] = image_lo;
3290 else
3291 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3293 else
3295 r->cl = rvc_inf;
3296 r->sign = sign;
3299 else
3301 r->cl = rvc_normal;
3302 r->sign = sign;
3303 SET_REAL_EXP (r, exp - 1023 + 1);
3304 if (HOST_BITS_PER_LONG == 32)
3306 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3307 r->sig[SIGSZ-2] = image_lo;
3309 else
3310 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3314 const struct real_format ieee_double_format =
3316 encode_ieee_double,
3317 decode_ieee_double,
3321 -1021,
3322 1024,
3325 false,
3326 true,
3327 true,
3328 true,
3329 true,
3330 true,
3331 true,
3332 false,
3333 "ieee_double"
3336 const struct real_format mips_double_format =
3338 encode_ieee_double,
3339 decode_ieee_double,
3343 -1021,
3344 1024,
3347 false,
3348 true,
3349 true,
3350 true,
3351 true,
3352 true,
3353 false,
3354 true,
3355 "mips_double"
3358 const struct real_format motorola_double_format =
3360 encode_ieee_double,
3361 decode_ieee_double,
3365 -1021,
3366 1024,
3369 false,
3370 true,
3371 true,
3372 true,
3373 true,
3374 true,
3375 true,
3376 true,
3377 "motorola_double"
3380 /* IEEE extended real format. This comes in three flavors: Intel's as
3381 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3382 12- and 16-byte images may be big- or little endian; Motorola's is
3383 always big endian. */
3385 /* Helper subroutine which converts from the internal format to the
3386 12-byte little-endian Intel format. Functions below adjust this
3387 for the other possible formats. */
3388 static void
3389 encode_ieee_extended (const struct real_format *fmt, long *buf,
3390 const REAL_VALUE_TYPE *r)
3392 unsigned long image_hi, sig_hi, sig_lo;
3393 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3395 image_hi = r->sign << 15;
3396 sig_hi = sig_lo = 0;
3398 switch (r->cl)
3400 case rvc_zero:
3401 break;
3403 case rvc_inf:
3404 if (fmt->has_inf)
3406 image_hi |= 32767;
3408 /* Intel requires the explicit integer bit to be set, otherwise
3409 it considers the value a "pseudo-infinity". Motorola docs
3410 say it doesn't care. */
3411 sig_hi = 0x80000000;
3413 else
3415 image_hi |= 32767;
3416 sig_lo = sig_hi = 0xffffffff;
3418 break;
3420 case rvc_nan:
3421 if (fmt->has_nans)
3423 image_hi |= 32767;
3424 if (r->canonical)
3426 if (fmt->canonical_nan_lsbs_set)
3428 sig_hi = (1 << 30) - 1;
3429 sig_lo = 0xffffffff;
3432 else if (HOST_BITS_PER_LONG == 32)
3434 sig_hi = r->sig[SIGSZ-1];
3435 sig_lo = r->sig[SIGSZ-2];
3437 else
3439 sig_lo = r->sig[SIGSZ-1];
3440 sig_hi = sig_lo >> 31 >> 1;
3441 sig_lo &= 0xffffffff;
3443 if (r->signalling == fmt->qnan_msb_set)
3444 sig_hi &= ~(1 << 30);
3445 else
3446 sig_hi |= 1 << 30;
3447 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3448 sig_hi = 1 << 29;
3450 /* Intel requires the explicit integer bit to be set, otherwise
3451 it considers the value a "pseudo-nan". Motorola docs say it
3452 doesn't care. */
3453 sig_hi |= 0x80000000;
3455 else
3457 image_hi |= 32767;
3458 sig_lo = sig_hi = 0xffffffff;
3460 break;
3462 case rvc_normal:
3464 int exp = REAL_EXP (r);
3466 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3467 whereas the intermediate representation is 0.F x 2**exp.
3468 Which means we're off by one.
3470 Except for Motorola, which consider exp=0 and explicit
3471 integer bit set to continue to be normalized. In theory
3472 this discrepancy has been taken care of by the difference
3473 in fmt->emin in round_for_format. */
3475 if (denormal)
3476 exp = 0;
3477 else
3479 exp += 16383 - 1;
3480 gcc_assert (exp >= 0);
3482 image_hi |= exp;
3484 if (HOST_BITS_PER_LONG == 32)
3486 sig_hi = r->sig[SIGSZ-1];
3487 sig_lo = r->sig[SIGSZ-2];
3489 else
3491 sig_lo = r->sig[SIGSZ-1];
3492 sig_hi = sig_lo >> 31 >> 1;
3493 sig_lo &= 0xffffffff;
3496 break;
3498 default:
3499 gcc_unreachable ();
3502 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3505 /* Convert from the internal format to the 12-byte Motorola format
3506 for an IEEE extended real. */
3507 static void
3508 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3509 const REAL_VALUE_TYPE *r)
3511 long intermed[3];
3512 encode_ieee_extended (fmt, intermed, r);
3514 if (r->cl == rvc_inf)
3515 /* For infinity clear the explicit integer bit again, so that the
3516 format matches the canonical infinity generated by the FPU. */
3517 intermed[1] = 0;
3519 /* Motorola chips are assumed always to be big-endian. Also, the
3520 padding in a Motorola extended real goes between the exponent and
3521 the mantissa. At this point the mantissa is entirely within
3522 elements 0 and 1 of intermed, and the exponent entirely within
3523 element 2, so all we have to do is swap the order around, and
3524 shift element 2 left 16 bits. */
3525 buf[0] = intermed[2] << 16;
3526 buf[1] = intermed[1];
3527 buf[2] = intermed[0];
3530 /* Convert from the internal format to the 12-byte Intel format for
3531 an IEEE extended real. */
3532 static void
3533 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3534 const REAL_VALUE_TYPE *r)
3536 if (FLOAT_WORDS_BIG_ENDIAN)
3538 /* All the padding in an Intel-format extended real goes at the high
3539 end, which in this case is after the mantissa, not the exponent.
3540 Therefore we must shift everything down 16 bits. */
3541 long intermed[3];
3542 encode_ieee_extended (fmt, intermed, r);
3543 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3544 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3545 buf[2] = (intermed[0] << 16);
3547 else
3548 /* encode_ieee_extended produces what we want directly. */
3549 encode_ieee_extended (fmt, buf, r);
3552 /* Convert from the internal format to the 16-byte Intel format for
3553 an IEEE extended real. */
3554 static void
3555 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3556 const REAL_VALUE_TYPE *r)
3558 /* All the padding in an Intel-format extended real goes at the high end. */
3559 encode_ieee_extended_intel_96 (fmt, buf, r);
3560 buf[3] = 0;
3563 /* As above, we have a helper function which converts from 12-byte
3564 little-endian Intel format to internal format. Functions below
3565 adjust for the other possible formats. */
3566 static void
3567 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3568 const long *buf)
3570 unsigned long image_hi, sig_hi, sig_lo;
3571 bool sign;
3572 int exp;
3574 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3575 sig_lo &= 0xffffffff;
3576 sig_hi &= 0xffffffff;
3577 image_hi &= 0xffffffff;
3579 sign = (image_hi >> 15) & 1;
3580 exp = image_hi & 0x7fff;
3582 memset (r, 0, sizeof (*r));
3584 if (exp == 0)
3586 if ((sig_hi || sig_lo) && fmt->has_denorm)
3588 r->cl = rvc_normal;
3589 r->sign = sign;
3591 /* When the IEEE format contains a hidden bit, we know that
3592 it's zero at this point, and so shift up the significand
3593 and decrease the exponent to match. In this case, Motorola
3594 defines the explicit integer bit to be valid, so we don't
3595 know whether the msb is set or not. */
3596 SET_REAL_EXP (r, fmt->emin);
3597 if (HOST_BITS_PER_LONG == 32)
3599 r->sig[SIGSZ-1] = sig_hi;
3600 r->sig[SIGSZ-2] = sig_lo;
3602 else
3603 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3605 normalize (r);
3607 else if (fmt->has_signed_zero)
3608 r->sign = sign;
3610 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3612 /* See above re "pseudo-infinities" and "pseudo-nans".
3613 Short summary is that the MSB will likely always be
3614 set, and that we don't care about it. */
3615 sig_hi &= 0x7fffffff;
3617 if (sig_hi || sig_lo)
3619 r->cl = rvc_nan;
3620 r->sign = sign;
3621 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3622 if (HOST_BITS_PER_LONG == 32)
3624 r->sig[SIGSZ-1] = sig_hi;
3625 r->sig[SIGSZ-2] = sig_lo;
3627 else
3628 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3630 else
3632 r->cl = rvc_inf;
3633 r->sign = sign;
3636 else
3638 r->cl = rvc_normal;
3639 r->sign = sign;
3640 SET_REAL_EXP (r, exp - 16383 + 1);
3641 if (HOST_BITS_PER_LONG == 32)
3643 r->sig[SIGSZ-1] = sig_hi;
3644 r->sig[SIGSZ-2] = sig_lo;
3646 else
3647 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3651 /* Convert from the internal format to the 12-byte Motorola format
3652 for an IEEE extended real. */
3653 static void
3654 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3655 const long *buf)
3657 long intermed[3];
3659 /* Motorola chips are assumed always to be big-endian. Also, the
3660 padding in a Motorola extended real goes between the exponent and
3661 the mantissa; remove it. */
3662 intermed[0] = buf[2];
3663 intermed[1] = buf[1];
3664 intermed[2] = (unsigned long)buf[0] >> 16;
3666 decode_ieee_extended (fmt, r, intermed);
3669 /* Convert from the internal format to the 12-byte Intel format for
3670 an IEEE extended real. */
3671 static void
3672 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3673 const long *buf)
3675 if (FLOAT_WORDS_BIG_ENDIAN)
3677 /* All the padding in an Intel-format extended real goes at the high
3678 end, which in this case is after the mantissa, not the exponent.
3679 Therefore we must shift everything up 16 bits. */
3680 long intermed[3];
3682 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3683 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3684 intermed[2] = ((unsigned long)buf[0] >> 16);
3686 decode_ieee_extended (fmt, r, intermed);
3688 else
3689 /* decode_ieee_extended produces what we want directly. */
3690 decode_ieee_extended (fmt, r, buf);
3693 /* Convert from the internal format to the 16-byte Intel format for
3694 an IEEE extended real. */
3695 static void
3696 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3697 const long *buf)
3699 /* All the padding in an Intel-format extended real goes at the high end. */
3700 decode_ieee_extended_intel_96 (fmt, r, buf);
3703 const struct real_format ieee_extended_motorola_format =
3705 encode_ieee_extended_motorola,
3706 decode_ieee_extended_motorola,
3710 -16382,
3711 16384,
3714 false,
3715 true,
3716 true,
3717 true,
3718 true,
3719 true,
3720 true,
3721 true,
3722 "ieee_extended_motorola"
3725 const struct real_format ieee_extended_intel_96_format =
3727 encode_ieee_extended_intel_96,
3728 decode_ieee_extended_intel_96,
3732 -16381,
3733 16384,
3736 false,
3737 true,
3738 true,
3739 true,
3740 true,
3741 true,
3742 true,
3743 false,
3744 "ieee_extended_intel_96"
3747 const struct real_format ieee_extended_intel_128_format =
3749 encode_ieee_extended_intel_128,
3750 decode_ieee_extended_intel_128,
3754 -16381,
3755 16384,
3758 false,
3759 true,
3760 true,
3761 true,
3762 true,
3763 true,
3764 true,
3765 false,
3766 "ieee_extended_intel_128"
3769 /* The following caters to i386 systems that set the rounding precision
3770 to 53 bits instead of 64, e.g. FreeBSD. */
3771 const struct real_format ieee_extended_intel_96_round_53_format =
3773 encode_ieee_extended_intel_96,
3774 decode_ieee_extended_intel_96,
3778 -16381,
3779 16384,
3782 false,
3783 true,
3784 true,
3785 true,
3786 true,
3787 true,
3788 true,
3789 false,
3790 "ieee_extended_intel_96_round_53"
3793 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3794 numbers whose sum is equal to the extended precision value. The number
3795 with greater magnitude is first. This format has the same magnitude
3796 range as an IEEE double precision value, but effectively 106 bits of
3797 significand precision. Infinity and NaN are represented by their IEEE
3798 double precision value stored in the first number, the second number is
3799 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3801 static void encode_ibm_extended (const struct real_format *fmt,
3802 long *, const REAL_VALUE_TYPE *);
3803 static void decode_ibm_extended (const struct real_format *,
3804 REAL_VALUE_TYPE *, const long *);
3806 static void
3807 encode_ibm_extended (const struct real_format *fmt, long *buf,
3808 const REAL_VALUE_TYPE *r)
3810 REAL_VALUE_TYPE u, normr, v;
3811 const struct real_format *base_fmt;
3813 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3815 /* Renormalize R before doing any arithmetic on it. */
3816 normr = *r;
3817 if (normr.cl == rvc_normal)
3818 normalize (&normr);
3820 /* u = IEEE double precision portion of significand. */
3821 u = normr;
3822 round_for_format (base_fmt, &u);
3823 encode_ieee_double (base_fmt, &buf[0], &u);
3825 if (u.cl == rvc_normal)
3827 do_add (&v, &normr, &u, 1);
3828 /* Call round_for_format since we might need to denormalize. */
3829 round_for_format (base_fmt, &v);
3830 encode_ieee_double (base_fmt, &buf[2], &v);
3832 else
3834 /* Inf, NaN, 0 are all representable as doubles, so the
3835 least-significant part can be 0.0. */
3836 buf[2] = 0;
3837 buf[3] = 0;
3841 static void
3842 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3843 const long *buf)
3845 REAL_VALUE_TYPE u, v;
3846 const struct real_format *base_fmt;
3848 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3849 decode_ieee_double (base_fmt, &u, &buf[0]);
3851 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3853 decode_ieee_double (base_fmt, &v, &buf[2]);
3854 do_add (r, &u, &v, 0);
3856 else
3857 *r = u;
3860 const struct real_format ibm_extended_format =
3862 encode_ibm_extended,
3863 decode_ibm_extended,
3865 53 + 53,
3867 -1021 + 53,
3868 1024,
3869 127,
3871 false,
3872 true,
3873 true,
3874 true,
3875 true,
3876 true,
3877 true,
3878 false,
3879 "ibm_extended"
3882 const struct real_format mips_extended_format =
3884 encode_ibm_extended,
3885 decode_ibm_extended,
3887 53 + 53,
3889 -1021 + 53,
3890 1024,
3891 127,
3893 false,
3894 true,
3895 true,
3896 true,
3897 true,
3898 true,
3899 false,
3900 true,
3901 "mips_extended"
3905 /* IEEE quad precision format. */
3907 static void encode_ieee_quad (const struct real_format *fmt,
3908 long *, const REAL_VALUE_TYPE *);
3909 static void decode_ieee_quad (const struct real_format *,
3910 REAL_VALUE_TYPE *, const long *);
3912 static void
3913 encode_ieee_quad (const struct real_format *fmt, long *buf,
3914 const REAL_VALUE_TYPE *r)
3916 unsigned long image3, image2, image1, image0, exp;
3917 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3918 REAL_VALUE_TYPE u;
3920 image3 = r->sign << 31;
3921 image2 = 0;
3922 image1 = 0;
3923 image0 = 0;
3925 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3927 switch (r->cl)
3929 case rvc_zero:
3930 break;
3932 case rvc_inf:
3933 if (fmt->has_inf)
3934 image3 |= 32767 << 16;
3935 else
3937 image3 |= 0x7fffffff;
3938 image2 = 0xffffffff;
3939 image1 = 0xffffffff;
3940 image0 = 0xffffffff;
3942 break;
3944 case rvc_nan:
3945 if (fmt->has_nans)
3947 image3 |= 32767 << 16;
3949 if (r->canonical)
3951 if (fmt->canonical_nan_lsbs_set)
3953 image3 |= 0x7fff;
3954 image2 = image1 = image0 = 0xffffffff;
3957 else if (HOST_BITS_PER_LONG == 32)
3959 image0 = u.sig[0];
3960 image1 = u.sig[1];
3961 image2 = u.sig[2];
3962 image3 |= u.sig[3] & 0xffff;
3964 else
3966 image0 = u.sig[0];
3967 image1 = image0 >> 31 >> 1;
3968 image2 = u.sig[1];
3969 image3 |= (image2 >> 31 >> 1) & 0xffff;
3970 image0 &= 0xffffffff;
3971 image2 &= 0xffffffff;
3973 if (r->signalling == fmt->qnan_msb_set)
3974 image3 &= ~0x8000;
3975 else
3976 image3 |= 0x8000;
3977 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3978 image3 |= 0x4000;
3980 else
3982 image3 |= 0x7fffffff;
3983 image2 = 0xffffffff;
3984 image1 = 0xffffffff;
3985 image0 = 0xffffffff;
3987 break;
3989 case rvc_normal:
3990 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3991 whereas the intermediate representation is 0.F x 2**exp.
3992 Which means we're off by one. */
3993 if (denormal)
3994 exp = 0;
3995 else
3996 exp = REAL_EXP (r) + 16383 - 1;
3997 image3 |= exp << 16;
3999 if (HOST_BITS_PER_LONG == 32)
4001 image0 = u.sig[0];
4002 image1 = u.sig[1];
4003 image2 = u.sig[2];
4004 image3 |= u.sig[3] & 0xffff;
4006 else
4008 image0 = u.sig[0];
4009 image1 = image0 >> 31 >> 1;
4010 image2 = u.sig[1];
4011 image3 |= (image2 >> 31 >> 1) & 0xffff;
4012 image0 &= 0xffffffff;
4013 image2 &= 0xffffffff;
4015 break;
4017 default:
4018 gcc_unreachable ();
4021 if (FLOAT_WORDS_BIG_ENDIAN)
4023 buf[0] = image3;
4024 buf[1] = image2;
4025 buf[2] = image1;
4026 buf[3] = image0;
4028 else
4030 buf[0] = image0;
4031 buf[1] = image1;
4032 buf[2] = image2;
4033 buf[3] = image3;
4037 static void
4038 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4039 const long *buf)
4041 unsigned long image3, image2, image1, image0;
4042 bool sign;
4043 int exp;
4045 if (FLOAT_WORDS_BIG_ENDIAN)
4047 image3 = buf[0];
4048 image2 = buf[1];
4049 image1 = buf[2];
4050 image0 = buf[3];
4052 else
4054 image0 = buf[0];
4055 image1 = buf[1];
4056 image2 = buf[2];
4057 image3 = buf[3];
4059 image0 &= 0xffffffff;
4060 image1 &= 0xffffffff;
4061 image2 &= 0xffffffff;
4063 sign = (image3 >> 31) & 1;
4064 exp = (image3 >> 16) & 0x7fff;
4065 image3 &= 0xffff;
4067 memset (r, 0, sizeof (*r));
4069 if (exp == 0)
4071 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
4073 r->cl = rvc_normal;
4074 r->sign = sign;
4076 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
4077 if (HOST_BITS_PER_LONG == 32)
4079 r->sig[0] = image0;
4080 r->sig[1] = image1;
4081 r->sig[2] = image2;
4082 r->sig[3] = image3;
4084 else
4086 r->sig[0] = (image1 << 31 << 1) | image0;
4087 r->sig[1] = (image3 << 31 << 1) | image2;
4090 normalize (r);
4092 else if (fmt->has_signed_zero)
4093 r->sign = sign;
4095 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
4097 if (image3 | image2 | image1 | image0)
4099 r->cl = rvc_nan;
4100 r->sign = sign;
4101 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
4103 if (HOST_BITS_PER_LONG == 32)
4105 r->sig[0] = image0;
4106 r->sig[1] = image1;
4107 r->sig[2] = image2;
4108 r->sig[3] = image3;
4110 else
4112 r->sig[0] = (image1 << 31 << 1) | image0;
4113 r->sig[1] = (image3 << 31 << 1) | image2;
4115 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4117 else
4119 r->cl = rvc_inf;
4120 r->sign = sign;
4123 else
4125 r->cl = rvc_normal;
4126 r->sign = sign;
4127 SET_REAL_EXP (r, exp - 16383 + 1);
4129 if (HOST_BITS_PER_LONG == 32)
4131 r->sig[0] = image0;
4132 r->sig[1] = image1;
4133 r->sig[2] = image2;
4134 r->sig[3] = image3;
4136 else
4138 r->sig[0] = (image1 << 31 << 1) | image0;
4139 r->sig[1] = (image3 << 31 << 1) | image2;
4141 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4142 r->sig[SIGSZ-1] |= SIG_MSB;
4146 const struct real_format ieee_quad_format =
4148 encode_ieee_quad,
4149 decode_ieee_quad,
4151 113,
4152 113,
4153 -16381,
4154 16384,
4155 127,
4156 127,
4157 false,
4158 true,
4159 true,
4160 true,
4161 true,
4162 true,
4163 true,
4164 false,
4165 "ieee_quad"
4168 const struct real_format mips_quad_format =
4170 encode_ieee_quad,
4171 decode_ieee_quad,
4173 113,
4174 113,
4175 -16381,
4176 16384,
4177 127,
4178 127,
4179 false,
4180 true,
4181 true,
4182 true,
4183 true,
4184 true,
4185 false,
4186 true,
4187 "mips_quad"
4190 /* Descriptions of VAX floating point formats can be found beginning at
4192 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4194 The thing to remember is that they're almost IEEE, except for word
4195 order, exponent bias, and the lack of infinities, nans, and denormals.
4197 We don't implement the H_floating format here, simply because neither
4198 the VAX or Alpha ports use it. */
4200 static void encode_vax_f (const struct real_format *fmt,
4201 long *, const REAL_VALUE_TYPE *);
4202 static void decode_vax_f (const struct real_format *,
4203 REAL_VALUE_TYPE *, const long *);
4204 static void encode_vax_d (const struct real_format *fmt,
4205 long *, const REAL_VALUE_TYPE *);
4206 static void decode_vax_d (const struct real_format *,
4207 REAL_VALUE_TYPE *, const long *);
4208 static void encode_vax_g (const struct real_format *fmt,
4209 long *, const REAL_VALUE_TYPE *);
4210 static void decode_vax_g (const struct real_format *,
4211 REAL_VALUE_TYPE *, const long *);
4213 static void
4214 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4215 const REAL_VALUE_TYPE *r)
4217 unsigned long sign, exp, sig, image;
4219 sign = r->sign << 15;
4221 switch (r->cl)
4223 case rvc_zero:
4224 image = 0;
4225 break;
4227 case rvc_inf:
4228 case rvc_nan:
4229 image = 0xffff7fff | sign;
4230 break;
4232 case rvc_normal:
4233 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4234 exp = REAL_EXP (r) + 128;
4236 image = (sig << 16) & 0xffff0000;
4237 image |= sign;
4238 image |= exp << 7;
4239 image |= sig >> 16;
4240 break;
4242 default:
4243 gcc_unreachable ();
4246 buf[0] = image;
4249 static void
4250 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
4251 REAL_VALUE_TYPE *r, const long *buf)
4253 unsigned long image = buf[0] & 0xffffffff;
4254 int exp = (image >> 7) & 0xff;
4256 memset (r, 0, sizeof (*r));
4258 if (exp != 0)
4260 r->cl = rvc_normal;
4261 r->sign = (image >> 15) & 1;
4262 SET_REAL_EXP (r, exp - 128);
4264 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
4265 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4269 static void
4270 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4271 const REAL_VALUE_TYPE *r)
4273 unsigned long image0, image1, sign = r->sign << 15;
4275 switch (r->cl)
4277 case rvc_zero:
4278 image0 = image1 = 0;
4279 break;
4281 case rvc_inf:
4282 case rvc_nan:
4283 image0 = 0xffff7fff | sign;
4284 image1 = 0xffffffff;
4285 break;
4287 case rvc_normal:
4288 /* Extract the significand into straight hi:lo. */
4289 if (HOST_BITS_PER_LONG == 64)
4291 image0 = r->sig[SIGSZ-1];
4292 image1 = (image0 >> (64 - 56)) & 0xffffffff;
4293 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
4295 else
4297 image0 = r->sig[SIGSZ-1];
4298 image1 = r->sig[SIGSZ-2];
4299 image1 = (image0 << 24) | (image1 >> 8);
4300 image0 = (image0 >> 8) & 0xffffff;
4303 /* Rearrange the half-words of the significand to match the
4304 external format. */
4305 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
4306 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4308 /* Add the sign and exponent. */
4309 image0 |= sign;
4310 image0 |= (REAL_EXP (r) + 128) << 7;
4311 break;
4313 default:
4314 gcc_unreachable ();
4317 if (FLOAT_WORDS_BIG_ENDIAN)
4318 buf[0] = image1, buf[1] = image0;
4319 else
4320 buf[0] = image0, buf[1] = image1;
4323 static void
4324 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
4325 REAL_VALUE_TYPE *r, const long *buf)
4327 unsigned long image0, image1;
4328 int exp;
4330 if (FLOAT_WORDS_BIG_ENDIAN)
4331 image1 = buf[0], image0 = buf[1];
4332 else
4333 image0 = buf[0], image1 = buf[1];
4334 image0 &= 0xffffffff;
4335 image1 &= 0xffffffff;
4337 exp = (image0 >> 7) & 0xff;
4339 memset (r, 0, sizeof (*r));
4341 if (exp != 0)
4343 r->cl = rvc_normal;
4344 r->sign = (image0 >> 15) & 1;
4345 SET_REAL_EXP (r, exp - 128);
4347 /* Rearrange the half-words of the external format into
4348 proper ascending order. */
4349 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4350 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4352 if (HOST_BITS_PER_LONG == 64)
4354 image0 = (image0 << 31 << 1) | image1;
4355 image0 <<= 64 - 56;
4356 image0 |= SIG_MSB;
4357 r->sig[SIGSZ-1] = image0;
4359 else
4361 r->sig[SIGSZ-1] = image0;
4362 r->sig[SIGSZ-2] = image1;
4363 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4364 r->sig[SIGSZ-1] |= SIG_MSB;
4369 static void
4370 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4371 const REAL_VALUE_TYPE *r)
4373 unsigned long image0, image1, sign = r->sign << 15;
4375 switch (r->cl)
4377 case rvc_zero:
4378 image0 = image1 = 0;
4379 break;
4381 case rvc_inf:
4382 case rvc_nan:
4383 image0 = 0xffff7fff | sign;
4384 image1 = 0xffffffff;
4385 break;
4387 case rvc_normal:
4388 /* Extract the significand into straight hi:lo. */
4389 if (HOST_BITS_PER_LONG == 64)
4391 image0 = r->sig[SIGSZ-1];
4392 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4393 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4395 else
4397 image0 = r->sig[SIGSZ-1];
4398 image1 = r->sig[SIGSZ-2];
4399 image1 = (image0 << 21) | (image1 >> 11);
4400 image0 = (image0 >> 11) & 0xfffff;
4403 /* Rearrange the half-words of the significand to match the
4404 external format. */
4405 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4406 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4408 /* Add the sign and exponent. */
4409 image0 |= sign;
4410 image0 |= (REAL_EXP (r) + 1024) << 4;
4411 break;
4413 default:
4414 gcc_unreachable ();
4417 if (FLOAT_WORDS_BIG_ENDIAN)
4418 buf[0] = image1, buf[1] = image0;
4419 else
4420 buf[0] = image0, buf[1] = image1;
4423 static void
4424 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4425 REAL_VALUE_TYPE *r, const long *buf)
4427 unsigned long image0, image1;
4428 int exp;
4430 if (FLOAT_WORDS_BIG_ENDIAN)
4431 image1 = buf[0], image0 = buf[1];
4432 else
4433 image0 = buf[0], image1 = buf[1];
4434 image0 &= 0xffffffff;
4435 image1 &= 0xffffffff;
4437 exp = (image0 >> 4) & 0x7ff;
4439 memset (r, 0, sizeof (*r));
4441 if (exp != 0)
4443 r->cl = rvc_normal;
4444 r->sign = (image0 >> 15) & 1;
4445 SET_REAL_EXP (r, exp - 1024);
4447 /* Rearrange the half-words of the external format into
4448 proper ascending order. */
4449 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4450 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4452 if (HOST_BITS_PER_LONG == 64)
4454 image0 = (image0 << 31 << 1) | image1;
4455 image0 <<= 64 - 53;
4456 image0 |= SIG_MSB;
4457 r->sig[SIGSZ-1] = image0;
4459 else
4461 r->sig[SIGSZ-1] = image0;
4462 r->sig[SIGSZ-2] = image1;
4463 lshift_significand (r, r, 64 - 53);
4464 r->sig[SIGSZ-1] |= SIG_MSB;
4469 const struct real_format vax_f_format =
4471 encode_vax_f,
4472 decode_vax_f,
4476 -127,
4477 127,
4480 false,
4481 false,
4482 false,
4483 false,
4484 false,
4485 false,
4486 false,
4487 false,
4488 "vax_f"
4491 const struct real_format vax_d_format =
4493 encode_vax_d,
4494 decode_vax_d,
4498 -127,
4499 127,
4502 false,
4503 false,
4504 false,
4505 false,
4506 false,
4507 false,
4508 false,
4509 false,
4510 "vax_d"
4513 const struct real_format vax_g_format =
4515 encode_vax_g,
4516 decode_vax_g,
4520 -1023,
4521 1023,
4524 false,
4525 false,
4526 false,
4527 false,
4528 false,
4529 false,
4530 false,
4531 false,
4532 "vax_g"
4535 /* Encode real R into a single precision DFP value in BUF. */
4536 static void
4537 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4538 long *buf ATTRIBUTE_UNUSED,
4539 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4541 encode_decimal32 (fmt, buf, r);
4544 /* Decode a single precision DFP value in BUF into a real R. */
4545 static void
4546 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4547 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4548 const long *buf ATTRIBUTE_UNUSED)
4550 decode_decimal32 (fmt, r, buf);
4553 /* Encode real R into a double precision DFP value in BUF. */
4554 static void
4555 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4556 long *buf ATTRIBUTE_UNUSED,
4557 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4559 encode_decimal64 (fmt, buf, r);
4562 /* Decode a double precision DFP value in BUF into a real R. */
4563 static void
4564 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4565 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4566 const long *buf ATTRIBUTE_UNUSED)
4568 decode_decimal64 (fmt, r, buf);
4571 /* Encode real R into a quad precision DFP value in BUF. */
4572 static void
4573 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4574 long *buf ATTRIBUTE_UNUSED,
4575 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4577 encode_decimal128 (fmt, buf, r);
4580 /* Decode a quad precision DFP value in BUF into a real R. */
4581 static void
4582 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4583 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4584 const long *buf ATTRIBUTE_UNUSED)
4586 decode_decimal128 (fmt, r, buf);
4589 /* Single precision decimal floating point (IEEE 754). */
4590 const struct real_format decimal_single_format =
4592 encode_decimal_single,
4593 decode_decimal_single,
4597 -94,
4601 false,
4602 true,
4603 true,
4604 true,
4605 true,
4606 true,
4607 true,
4608 false,
4609 "decimal_single"
4612 /* Double precision decimal floating point (IEEE 754). */
4613 const struct real_format decimal_double_format =
4615 encode_decimal_double,
4616 decode_decimal_double,
4620 -382,
4621 385,
4624 false,
4625 true,
4626 true,
4627 true,
4628 true,
4629 true,
4630 true,
4631 false,
4632 "decimal_double"
4635 /* Quad precision decimal floating point (IEEE 754). */
4636 const struct real_format decimal_quad_format =
4638 encode_decimal_quad,
4639 decode_decimal_quad,
4643 -6142,
4644 6145,
4645 127,
4646 127,
4647 false,
4648 true,
4649 true,
4650 true,
4651 true,
4652 true,
4653 true,
4654 false,
4655 "decimal_quad"
4658 /* Encode half-precision floats. This routine is used both for the IEEE
4659 ARM alternative encodings. */
4660 static void
4661 encode_ieee_half (const struct real_format *fmt, long *buf,
4662 const REAL_VALUE_TYPE *r)
4664 unsigned long image, sig, exp;
4665 unsigned long sign = r->sign;
4666 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
4668 image = sign << 15;
4669 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 11)) & 0x3ff;
4671 switch (r->cl)
4673 case rvc_zero:
4674 break;
4676 case rvc_inf:
4677 if (fmt->has_inf)
4678 image |= 31 << 10;
4679 else
4680 image |= 0x7fff;
4681 break;
4683 case rvc_nan:
4684 if (fmt->has_nans)
4686 if (r->canonical)
4687 sig = (fmt->canonical_nan_lsbs_set ? (1 << 9) - 1 : 0);
4688 if (r->signalling == fmt->qnan_msb_set)
4689 sig &= ~(1 << 9);
4690 else
4691 sig |= 1 << 9;
4692 if (sig == 0)
4693 sig = 1 << 8;
4695 image |= 31 << 10;
4696 image |= sig;
4698 else
4699 image |= 0x3ff;
4700 break;
4702 case rvc_normal:
4703 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4704 whereas the intermediate representation is 0.F x 2**exp.
4705 Which means we're off by one. */
4706 if (denormal)
4707 exp = 0;
4708 else
4709 exp = REAL_EXP (r) + 15 - 1;
4710 image |= exp << 10;
4711 image |= sig;
4712 break;
4714 default:
4715 gcc_unreachable ();
4718 buf[0] = image;
4721 /* Decode half-precision floats. This routine is used both for the IEEE
4722 ARM alternative encodings. */
4723 static void
4724 decode_ieee_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4725 const long *buf)
4727 unsigned long image = buf[0] & 0xffff;
4728 bool sign = (image >> 15) & 1;
4729 int exp = (image >> 10) & 0x1f;
4731 memset (r, 0, sizeof (*r));
4732 image <<= HOST_BITS_PER_LONG - 11;
4733 image &= ~SIG_MSB;
4735 if (exp == 0)
4737 if (image && fmt->has_denorm)
4739 r->cl = rvc_normal;
4740 r->sign = sign;
4741 SET_REAL_EXP (r, -14);
4742 r->sig[SIGSZ-1] = image << 1;
4743 normalize (r);
4745 else if (fmt->has_signed_zero)
4746 r->sign = sign;
4748 else if (exp == 31 && (fmt->has_nans || fmt->has_inf))
4750 if (image)
4752 r->cl = rvc_nan;
4753 r->sign = sign;
4754 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4755 ^ fmt->qnan_msb_set);
4756 r->sig[SIGSZ-1] = image;
4758 else
4760 r->cl = rvc_inf;
4761 r->sign = sign;
4764 else
4766 r->cl = rvc_normal;
4767 r->sign = sign;
4768 SET_REAL_EXP (r, exp - 15 + 1);
4769 r->sig[SIGSZ-1] = image | SIG_MSB;
4773 /* Half-precision format, as specified in IEEE 754R. */
4774 const struct real_format ieee_half_format =
4776 encode_ieee_half,
4777 decode_ieee_half,
4781 -13,
4785 false,
4786 true,
4787 true,
4788 true,
4789 true,
4790 true,
4791 true,
4792 false,
4793 "ieee_half"
4796 /* ARM's alternative half-precision format, similar to IEEE but with
4797 no reserved exponent value for NaNs and infinities; rather, it just
4798 extends the range of exponents by one. */
4799 const struct real_format arm_half_format =
4801 encode_ieee_half,
4802 decode_ieee_half,
4806 -13,
4810 false,
4811 true,
4812 false,
4813 false,
4814 true,
4815 true,
4816 false,
4817 false,
4818 "arm_half"
4821 /* A synthetic "format" for internal arithmetic. It's the size of the
4822 internal significand minus the two bits needed for proper rounding.
4823 The encode and decode routines exist only to satisfy our paranoia
4824 harness. */
4826 static void encode_internal (const struct real_format *fmt,
4827 long *, const REAL_VALUE_TYPE *);
4828 static void decode_internal (const struct real_format *,
4829 REAL_VALUE_TYPE *, const long *);
4831 static void
4832 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4833 const REAL_VALUE_TYPE *r)
4835 memcpy (buf, r, sizeof (*r));
4838 static void
4839 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
4840 REAL_VALUE_TYPE *r, const long *buf)
4842 memcpy (r, buf, sizeof (*r));
4845 const struct real_format real_internal_format =
4847 encode_internal,
4848 decode_internal,
4850 SIGNIFICAND_BITS - 2,
4851 SIGNIFICAND_BITS - 2,
4852 -MAX_EXP,
4853 MAX_EXP,
4856 false,
4857 false,
4858 true,
4859 true,
4860 false,
4861 true,
4862 true,
4863 false,
4864 "real_internal"
4867 /* Calculate X raised to the integer exponent N in mode MODE and store
4868 the result in R. Return true if the result may be inexact due to
4869 loss of precision. The algorithm is the classic "left-to-right binary
4870 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4871 Algorithms", "The Art of Computer Programming", Volume 2. */
4873 bool
4874 real_powi (REAL_VALUE_TYPE *r, machine_mode mode,
4875 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4877 unsigned HOST_WIDE_INT bit;
4878 REAL_VALUE_TYPE t;
4879 bool inexact = false;
4880 bool init = false;
4881 bool neg;
4882 int i;
4884 if (n == 0)
4886 *r = dconst1;
4887 return false;
4889 else if (n < 0)
4891 /* Don't worry about overflow, from now on n is unsigned. */
4892 neg = true;
4893 n = -n;
4895 else
4896 neg = false;
4898 t = *x;
4899 bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
4900 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4902 if (init)
4904 inexact |= do_multiply (&t, &t, &t);
4905 if (n & bit)
4906 inexact |= do_multiply (&t, &t, x);
4908 else if (n & bit)
4909 init = true;
4910 bit >>= 1;
4913 if (neg)
4914 inexact |= do_divide (&t, &dconst1, &t);
4916 real_convert (r, mode, &t);
4917 return inexact;
4920 /* Round X to the nearest integer not larger in absolute value, i.e.
4921 towards zero, placing the result in R in mode MODE. */
4923 void
4924 real_trunc (REAL_VALUE_TYPE *r, machine_mode mode,
4925 const REAL_VALUE_TYPE *x)
4927 do_fix_trunc (r, x);
4928 if (mode != VOIDmode)
4929 real_convert (r, mode, r);
4932 /* Round X to the largest integer not greater in value, i.e. round
4933 down, placing the result in R in mode MODE. */
4935 void
4936 real_floor (REAL_VALUE_TYPE *r, machine_mode mode,
4937 const REAL_VALUE_TYPE *x)
4939 REAL_VALUE_TYPE t;
4941 do_fix_trunc (&t, x);
4942 if (! real_identical (&t, x) && x->sign)
4943 do_add (&t, &t, &dconstm1, 0);
4944 if (mode != VOIDmode)
4945 real_convert (r, mode, &t);
4946 else
4947 *r = t;
4950 /* Round X to the smallest integer not less then argument, i.e. round
4951 up, placing the result in R in mode MODE. */
4953 void
4954 real_ceil (REAL_VALUE_TYPE *r, machine_mode mode,
4955 const REAL_VALUE_TYPE *x)
4957 REAL_VALUE_TYPE t;
4959 do_fix_trunc (&t, x);
4960 if (! real_identical (&t, x) && ! x->sign)
4961 do_add (&t, &t, &dconst1, 0);
4962 if (mode != VOIDmode)
4963 real_convert (r, mode, &t);
4964 else
4965 *r = t;
4968 /* Round X to the nearest integer, but round halfway cases away from
4969 zero. */
4971 void
4972 real_round (REAL_VALUE_TYPE *r, machine_mode mode,
4973 const REAL_VALUE_TYPE *x)
4975 do_add (r, x, &dconsthalf, x->sign);
4976 do_fix_trunc (r, r);
4977 if (mode != VOIDmode)
4978 real_convert (r, mode, r);
4981 /* Set the sign of R to the sign of X. */
4983 void
4984 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4986 r->sign = x->sign;
4989 /* Check whether the real constant value given is an integer. */
4991 bool
4992 real_isinteger (const REAL_VALUE_TYPE *c, machine_mode mode)
4994 REAL_VALUE_TYPE cint;
4996 real_trunc (&cint, mode, c);
4997 return real_identical (c, &cint);
5000 /* Check whether C is an integer that fits in a HOST_WIDE_INT,
5001 storing it in *INT_OUT if so. */
5003 bool
5004 real_isinteger (const REAL_VALUE_TYPE *c, HOST_WIDE_INT *int_out)
5006 REAL_VALUE_TYPE cint;
5008 HOST_WIDE_INT n = real_to_integer (c);
5009 real_from_integer (&cint, VOIDmode, n, SIGNED);
5010 if (real_identical (c, &cint))
5012 *int_out = n;
5013 return true;
5015 return false;
5018 /* Write into BUF the maximum representable finite floating-point
5019 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5020 float string. LEN is the size of BUF, and the buffer must be large
5021 enough to contain the resulting string. */
5023 void
5024 get_max_float (const struct real_format *fmt, char *buf, size_t len)
5026 int i, n;
5027 char *p;
5029 strcpy (buf, "0x0.");
5030 n = fmt->p;
5031 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
5032 *p++ = 'f';
5033 if (i < n)
5034 *p++ = "08ce"[n - i];
5035 sprintf (p, "p%d", fmt->emax);
5036 if (fmt->pnan < fmt->p)
5038 /* This is an IBM extended double format made up of two IEEE
5039 doubles. The value of the long double is the sum of the
5040 values of the two parts. The most significant part is
5041 required to be the value of the long double rounded to the
5042 nearest double. Rounding means we need a slightly smaller
5043 value for LDBL_MAX. */
5044 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
5047 gcc_assert (strlen (buf) < len);
5050 /* True if mode M has a NaN representation and
5051 the treatment of NaN operands is important. */
5053 bool
5054 HONOR_NANS (machine_mode m)
5056 return MODE_HAS_NANS (m) && !flag_finite_math_only;
5059 bool
5060 HONOR_NANS (const_tree t)
5062 return HONOR_NANS (element_mode (t));
5065 bool
5066 HONOR_NANS (const_rtx x)
5068 return HONOR_NANS (GET_MODE (x));
5071 /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5073 bool
5074 HONOR_SNANS (machine_mode m)
5076 return flag_signaling_nans && HONOR_NANS (m);
5079 bool
5080 HONOR_SNANS (const_tree t)
5082 return HONOR_SNANS (element_mode (t));
5085 bool
5086 HONOR_SNANS (const_rtx x)
5088 return HONOR_SNANS (GET_MODE (x));
5091 /* As for HONOR_NANS, but true if the mode can represent infinity and
5092 the treatment of infinite values is important. */
5094 bool
5095 HONOR_INFINITIES (machine_mode m)
5097 return MODE_HAS_INFINITIES (m) && !flag_finite_math_only;
5100 bool
5101 HONOR_INFINITIES (const_tree t)
5103 return HONOR_INFINITIES (element_mode (t));
5106 bool
5107 HONOR_INFINITIES (const_rtx x)
5109 return HONOR_INFINITIES (GET_MODE (x));
5112 /* Like HONOR_NANS, but true if the given mode distinguishes between
5113 positive and negative zero, and the sign of zero is important. */
5115 bool
5116 HONOR_SIGNED_ZEROS (machine_mode m)
5118 return MODE_HAS_SIGNED_ZEROS (m) && flag_signed_zeros;
5121 bool
5122 HONOR_SIGNED_ZEROS (const_tree t)
5124 return HONOR_SIGNED_ZEROS (element_mode (t));
5127 bool
5128 HONOR_SIGNED_ZEROS (const_rtx x)
5130 return HONOR_SIGNED_ZEROS (GET_MODE (x));
5133 /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5134 and the rounding mode is important. */
5136 bool
5137 HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m)
5139 return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m) && flag_rounding_math;
5142 bool
5143 HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t)
5145 return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t));
5148 bool
5149 HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x)
5151 return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x));