2015-09-25 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / real.c
blobc1ff78d903f138e027cba19100f43118bca86c88
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 bool
1082 real_compare (int icode, const REAL_VALUE_TYPE *op0,
1083 const REAL_VALUE_TYPE *op1)
1085 enum tree_code code = (enum tree_code) icode;
1087 switch (code)
1089 case LT_EXPR:
1090 return do_compare (op0, op1, 1) < 0;
1091 case LE_EXPR:
1092 return do_compare (op0, op1, 1) <= 0;
1093 case GT_EXPR:
1094 return do_compare (op0, op1, -1) > 0;
1095 case GE_EXPR:
1096 return do_compare (op0, op1, -1) >= 0;
1097 case EQ_EXPR:
1098 return do_compare (op0, op1, -1) == 0;
1099 case NE_EXPR:
1100 return do_compare (op0, op1, -1) != 0;
1101 case UNORDERED_EXPR:
1102 return op0->cl == rvc_nan || op1->cl == rvc_nan;
1103 case ORDERED_EXPR:
1104 return op0->cl != rvc_nan && op1->cl != rvc_nan;
1105 case UNLT_EXPR:
1106 return do_compare (op0, op1, -1) < 0;
1107 case UNLE_EXPR:
1108 return do_compare (op0, op1, -1) <= 0;
1109 case UNGT_EXPR:
1110 return do_compare (op0, op1, 1) > 0;
1111 case UNGE_EXPR:
1112 return do_compare (op0, op1, 1) >= 0;
1113 case UNEQ_EXPR:
1114 return do_compare (op0, op1, 0) == 0;
1115 case LTGT_EXPR:
1116 return do_compare (op0, op1, 0) != 0;
1118 default:
1119 gcc_unreachable ();
1123 /* Return floor log2(R). */
1126 real_exponent (const REAL_VALUE_TYPE *r)
1128 switch (r->cl)
1130 case rvc_zero:
1131 return 0;
1132 case rvc_inf:
1133 case rvc_nan:
1134 return (unsigned int)-1 >> 1;
1135 case rvc_normal:
1136 return REAL_EXP (r);
1137 default:
1138 gcc_unreachable ();
1142 /* R = OP0 * 2**EXP. */
1144 void
1145 real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1147 *r = *op0;
1148 switch (r->cl)
1150 case rvc_zero:
1151 case rvc_inf:
1152 case rvc_nan:
1153 break;
1155 case rvc_normal:
1156 exp += REAL_EXP (op0);
1157 if (exp > MAX_EXP)
1158 get_inf (r, r->sign);
1159 else if (exp < -MAX_EXP)
1160 get_zero (r, r->sign);
1161 else
1162 SET_REAL_EXP (r, exp);
1163 break;
1165 default:
1166 gcc_unreachable ();
1170 /* Determine whether a floating-point value X is infinite. */
1172 bool
1173 real_isinf (const REAL_VALUE_TYPE *r)
1175 return (r->cl == rvc_inf);
1178 /* Determine whether a floating-point value X is a NaN. */
1180 bool
1181 real_isnan (const REAL_VALUE_TYPE *r)
1183 return (r->cl == rvc_nan);
1186 /* Determine whether a floating-point value X is finite. */
1188 bool
1189 real_isfinite (const REAL_VALUE_TYPE *r)
1191 return (r->cl != rvc_nan) && (r->cl != rvc_inf);
1194 /* Determine whether a floating-point value X is negative. */
1196 bool
1197 real_isneg (const REAL_VALUE_TYPE *r)
1199 return r->sign;
1202 /* Determine whether a floating-point value X is minus zero. */
1204 bool
1205 real_isnegzero (const REAL_VALUE_TYPE *r)
1207 return r->sign && r->cl == rvc_zero;
1210 /* Compare two floating-point objects for bitwise identity. */
1212 bool
1213 real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1215 int i;
1217 if (a->cl != b->cl)
1218 return false;
1219 if (a->sign != b->sign)
1220 return false;
1222 switch (a->cl)
1224 case rvc_zero:
1225 case rvc_inf:
1226 return true;
1228 case rvc_normal:
1229 if (a->decimal != b->decimal)
1230 return false;
1231 if (REAL_EXP (a) != REAL_EXP (b))
1232 return false;
1233 break;
1235 case rvc_nan:
1236 if (a->signalling != b->signalling)
1237 return false;
1238 /* The significand is ignored for canonical NaNs. */
1239 if (a->canonical || b->canonical)
1240 return a->canonical == b->canonical;
1241 break;
1243 default:
1244 gcc_unreachable ();
1247 for (i = 0; i < SIGSZ; ++i)
1248 if (a->sig[i] != b->sig[i])
1249 return false;
1251 return true;
1254 /* Try to change R into its exact multiplicative inverse in machine
1255 mode MODE. Return true if successful. */
1257 bool
1258 exact_real_inverse (machine_mode mode, REAL_VALUE_TYPE *r)
1260 const REAL_VALUE_TYPE *one = real_digit (1);
1261 REAL_VALUE_TYPE u;
1262 int i;
1264 if (r->cl != rvc_normal)
1265 return false;
1267 /* Check for a power of two: all significand bits zero except the MSB. */
1268 for (i = 0; i < SIGSZ-1; ++i)
1269 if (r->sig[i] != 0)
1270 return false;
1271 if (r->sig[SIGSZ-1] != SIG_MSB)
1272 return false;
1274 /* Find the inverse and truncate to the required mode. */
1275 do_divide (&u, one, r);
1276 real_convert (&u, mode, &u);
1278 /* The rounding may have overflowed. */
1279 if (u.cl != rvc_normal)
1280 return false;
1281 for (i = 0; i < SIGSZ-1; ++i)
1282 if (u.sig[i] != 0)
1283 return false;
1284 if (u.sig[SIGSZ-1] != SIG_MSB)
1285 return false;
1287 *r = u;
1288 return true;
1291 /* Return true if arithmetic on values in IMODE that were promoted
1292 from values in TMODE is equivalent to direct arithmetic on values
1293 in TMODE. */
1295 bool
1296 real_can_shorten_arithmetic (machine_mode imode, machine_mode tmode)
1298 const struct real_format *tfmt, *ifmt;
1299 tfmt = REAL_MODE_FORMAT (tmode);
1300 ifmt = REAL_MODE_FORMAT (imode);
1301 /* These conditions are conservative rather than trying to catch the
1302 exact boundary conditions; the main case to allow is IEEE float
1303 and double. */
1304 return (ifmt->b == tfmt->b
1305 && ifmt->p > 2 * tfmt->p
1306 && ifmt->emin < 2 * tfmt->emin - tfmt->p - 2
1307 && ifmt->emin < tfmt->emin - tfmt->emax - tfmt->p - 2
1308 && ifmt->emax > 2 * tfmt->emax + 2
1309 && ifmt->emax > tfmt->emax - tfmt->emin + tfmt->p + 2
1310 && ifmt->round_towards_zero == tfmt->round_towards_zero
1311 && (ifmt->has_sign_dependent_rounding
1312 == tfmt->has_sign_dependent_rounding)
1313 && ifmt->has_nans >= tfmt->has_nans
1314 && ifmt->has_inf >= tfmt->has_inf
1315 && ifmt->has_signed_zero >= tfmt->has_signed_zero
1316 && !MODE_COMPOSITE_P (tmode)
1317 && !MODE_COMPOSITE_P (imode));
1320 /* Render R as an integer. */
1322 HOST_WIDE_INT
1323 real_to_integer (const REAL_VALUE_TYPE *r)
1325 unsigned HOST_WIDE_INT i;
1327 switch (r->cl)
1329 case rvc_zero:
1330 underflow:
1331 return 0;
1333 case rvc_inf:
1334 case rvc_nan:
1335 overflow:
1336 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1337 if (!r->sign)
1338 i--;
1339 return i;
1341 case rvc_normal:
1342 if (r->decimal)
1343 return decimal_real_to_integer (r);
1345 if (REAL_EXP (r) <= 0)
1346 goto underflow;
1347 /* Only force overflow for unsigned overflow. Signed overflow is
1348 undefined, so it doesn't matter what we return, and some callers
1349 expect to be able to use this routine for both signed and
1350 unsigned conversions. */
1351 if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1352 goto overflow;
1354 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1355 i = r->sig[SIGSZ-1];
1356 else
1358 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1359 i = r->sig[SIGSZ-1];
1360 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1361 i |= r->sig[SIGSZ-2];
1364 i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1366 if (r->sign)
1367 i = -i;
1368 return i;
1370 default:
1371 gcc_unreachable ();
1375 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1376 be represented in precision, *FAIL is set to TRUE. */
1378 wide_int
1379 real_to_integer (const REAL_VALUE_TYPE *r, bool *fail, int precision)
1381 HOST_WIDE_INT val[2 * WIDE_INT_MAX_ELTS];
1382 int exp;
1383 int words, w;
1384 wide_int result;
1386 switch (r->cl)
1388 case rvc_zero:
1389 underflow:
1390 return wi::zero (precision);
1392 case rvc_inf:
1393 case rvc_nan:
1394 overflow:
1395 *fail = true;
1397 if (r->sign)
1398 return wi::set_bit_in_zero (precision - 1, precision);
1399 else
1400 return ~wi::set_bit_in_zero (precision - 1, precision);
1402 case rvc_normal:
1403 if (r->decimal)
1404 return decimal_real_to_integer (r, fail, precision);
1406 exp = REAL_EXP (r);
1407 if (exp <= 0)
1408 goto underflow;
1409 /* Only force overflow for unsigned overflow. Signed overflow is
1410 undefined, so it doesn't matter what we return, and some callers
1411 expect to be able to use this routine for both signed and
1412 unsigned conversions. */
1413 if (exp > precision)
1414 goto overflow;
1416 /* Put the significand into a wide_int that has precision W, which
1417 is the smallest HWI-multiple that has at least PRECISION bits.
1418 This ensures that the top bit of the significand is in the
1419 top bit of the wide_int. */
1420 words = (precision + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT;
1421 w = words * HOST_BITS_PER_WIDE_INT;
1423 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1424 for (int i = 0; i < words; i++)
1426 int j = SIGSZ - words + i;
1427 val[i] = (j < 0) ? 0 : r->sig[j];
1429 #else
1430 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1431 for (int i = 0; i < words; i++)
1433 int j = SIGSZ - (words * 2) + (i * 2);
1434 if (j < 0)
1435 val[i] = 0;
1436 else
1437 val[i] = r->sig[j];
1438 j += 1;
1439 if (j >= 0)
1440 val[i] |= (unsigned HOST_WIDE_INT) r->sig[j] << HOST_BITS_PER_LONG;
1442 #endif
1443 /* Shift the value into place and truncate to the desired precision. */
1444 result = wide_int::from_array (val, words, w);
1445 result = wi::lrshift (result, w - exp);
1446 result = wide_int::from (result, precision, UNSIGNED);
1448 if (r->sign)
1449 return -result;
1450 else
1451 return result;
1453 default:
1454 gcc_unreachable ();
1458 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1459 of NUM / DEN. Return the quotient and place the remainder in NUM.
1460 It is expected that NUM / DEN are close enough that the quotient is
1461 small. */
1463 static unsigned long
1464 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1466 unsigned long q, msb;
1467 int expn = REAL_EXP (num), expd = REAL_EXP (den);
1469 if (expn < expd)
1470 return 0;
1472 q = msb = 0;
1473 goto start;
1476 msb = num->sig[SIGSZ-1] & SIG_MSB;
1477 q <<= 1;
1478 lshift_significand_1 (num, num);
1479 start:
1480 if (msb || cmp_significands (num, den) >= 0)
1482 sub_significands (num, num, den, 0);
1483 q |= 1;
1486 while (--expn >= expd);
1488 SET_REAL_EXP (num, expd);
1489 normalize (num);
1491 return q;
1494 /* Render R as a decimal floating point constant. Emit DIGITS significant
1495 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1496 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1497 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1498 to a string that, when parsed back in mode MODE, yields the same value. */
1500 #define M_LOG10_2 0.30102999566398119521
1502 void
1503 real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig,
1504 size_t buf_size, size_t digits,
1505 int crop_trailing_zeros, machine_mode mode)
1507 const struct real_format *fmt = NULL;
1508 const REAL_VALUE_TYPE *one, *ten;
1509 REAL_VALUE_TYPE r, pten, u, v;
1510 int dec_exp, cmp_one, digit;
1511 size_t max_digits;
1512 char *p, *first, *last;
1513 bool sign;
1514 bool round_up;
1516 if (mode != VOIDmode)
1518 fmt = REAL_MODE_FORMAT (mode);
1519 gcc_assert (fmt);
1522 r = *r_orig;
1523 switch (r.cl)
1525 case rvc_zero:
1526 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1527 return;
1528 case rvc_normal:
1529 break;
1530 case rvc_inf:
1531 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1532 return;
1533 case rvc_nan:
1534 /* ??? Print the significand as well, if not canonical? */
1535 sprintf (str, "%c%cNaN", (r_orig->sign ? '-' : '+'),
1536 (r_orig->signalling ? 'S' : 'Q'));
1537 return;
1538 default:
1539 gcc_unreachable ();
1542 if (r.decimal)
1544 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1545 return;
1548 /* Bound the number of digits printed by the size of the representation. */
1549 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1550 if (digits == 0 || digits > max_digits)
1551 digits = max_digits;
1553 /* Estimate the decimal exponent, and compute the length of the string it
1554 will print as. Be conservative and add one to account for possible
1555 overflow or rounding error. */
1556 dec_exp = REAL_EXP (&r) * M_LOG10_2;
1557 for (max_digits = 1; dec_exp ; max_digits++)
1558 dec_exp /= 10;
1560 /* Bound the number of digits printed by the size of the output buffer. */
1561 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1562 gcc_assert (max_digits <= buf_size);
1563 if (digits > max_digits)
1564 digits = max_digits;
1566 one = real_digit (1);
1567 ten = ten_to_ptwo (0);
1569 sign = r.sign;
1570 r.sign = 0;
1572 dec_exp = 0;
1573 pten = *one;
1575 cmp_one = do_compare (&r, one, 0);
1576 if (cmp_one > 0)
1578 int m;
1580 /* Number is greater than one. Convert significand to an integer
1581 and strip trailing decimal zeros. */
1583 u = r;
1584 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1586 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1587 m = floor_log2 (max_digits);
1589 /* Iterate over the bits of the possible powers of 10 that might
1590 be present in U and eliminate them. That is, if we find that
1591 10**2**M divides U evenly, keep the division and increase
1592 DEC_EXP by 2**M. */
1595 REAL_VALUE_TYPE t;
1597 do_divide (&t, &u, ten_to_ptwo (m));
1598 do_fix_trunc (&v, &t);
1599 if (cmp_significands (&v, &t) == 0)
1601 u = t;
1602 dec_exp += 1 << m;
1605 while (--m >= 0);
1607 /* Revert the scaling to integer that we performed earlier. */
1608 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1609 - (SIGNIFICAND_BITS - 1));
1610 r = u;
1612 /* Find power of 10. Do this by dividing out 10**2**M when
1613 this is larger than the current remainder. Fill PTEN with
1614 the power of 10 that we compute. */
1615 if (REAL_EXP (&r) > 0)
1617 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1620 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1621 if (do_compare (&u, ptentwo, 0) >= 0)
1623 do_divide (&u, &u, ptentwo);
1624 do_multiply (&pten, &pten, ptentwo);
1625 dec_exp += 1 << m;
1628 while (--m >= 0);
1630 else
1631 /* We managed to divide off enough tens in the above reduction
1632 loop that we've now got a negative exponent. Fall into the
1633 less-than-one code to compute the proper value for PTEN. */
1634 cmp_one = -1;
1636 if (cmp_one < 0)
1638 int m;
1640 /* Number is less than one. Pad significand with leading
1641 decimal zeros. */
1643 v = r;
1644 while (1)
1646 /* Stop if we'd shift bits off the bottom. */
1647 if (v.sig[0] & 7)
1648 break;
1650 do_multiply (&u, &v, ten);
1652 /* Stop if we're now >= 1. */
1653 if (REAL_EXP (&u) > 0)
1654 break;
1656 v = u;
1657 dec_exp -= 1;
1659 r = v;
1661 /* Find power of 10. Do this by multiplying in P=10**2**M when
1662 the current remainder is smaller than 1/P. Fill PTEN with the
1663 power of 10 that we compute. */
1664 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1667 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1668 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1670 if (do_compare (&v, ptenmtwo, 0) <= 0)
1672 do_multiply (&v, &v, ptentwo);
1673 do_multiply (&pten, &pten, ptentwo);
1674 dec_exp -= 1 << m;
1677 while (--m >= 0);
1679 /* Invert the positive power of 10 that we've collected so far. */
1680 do_divide (&pten, one, &pten);
1683 p = str;
1684 if (sign)
1685 *p++ = '-';
1686 first = p++;
1688 /* At this point, PTEN should contain the nearest power of 10 smaller
1689 than R, such that this division produces the first digit.
1691 Using a divide-step primitive that returns the complete integral
1692 remainder avoids the rounding error that would be produced if
1693 we were to use do_divide here and then simply multiply by 10 for
1694 each subsequent digit. */
1696 digit = rtd_divmod (&r, &pten);
1698 /* Be prepared for error in that division via underflow ... */
1699 if (digit == 0 && cmp_significand_0 (&r))
1701 /* Multiply by 10 and try again. */
1702 do_multiply (&r, &r, ten);
1703 digit = rtd_divmod (&r, &pten);
1704 dec_exp -= 1;
1705 gcc_assert (digit != 0);
1708 /* ... or overflow. */
1709 if (digit == 10)
1711 *p++ = '1';
1712 if (--digits > 0)
1713 *p++ = '0';
1714 dec_exp += 1;
1716 else
1718 gcc_assert (digit <= 10);
1719 *p++ = digit + '0';
1722 /* Generate subsequent digits. */
1723 while (--digits > 0)
1725 do_multiply (&r, &r, ten);
1726 digit = rtd_divmod (&r, &pten);
1727 *p++ = digit + '0';
1729 last = p;
1731 /* Generate one more digit with which to do rounding. */
1732 do_multiply (&r, &r, ten);
1733 digit = rtd_divmod (&r, &pten);
1735 /* Round the result. */
1736 if (fmt && fmt->round_towards_zero)
1738 /* If the format uses round towards zero when parsing the string
1739 back in, we need to always round away from zero here. */
1740 if (cmp_significand_0 (&r))
1741 digit++;
1742 round_up = digit > 0;
1744 else
1746 if (digit == 5)
1748 /* Round to nearest. If R is nonzero there are additional
1749 nonzero digits to be extracted. */
1750 if (cmp_significand_0 (&r))
1751 digit++;
1752 /* Round to even. */
1753 else if ((p[-1] - '0') & 1)
1754 digit++;
1757 round_up = digit > 5;
1760 if (round_up)
1762 while (p > first)
1764 digit = *--p;
1765 if (digit == '9')
1766 *p = '0';
1767 else
1769 *p = digit + 1;
1770 break;
1774 /* Carry out of the first digit. This means we had all 9's and
1775 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1776 if (p == first)
1778 first[1] = '1';
1779 dec_exp++;
1783 /* Insert the decimal point. */
1784 first[0] = first[1];
1785 first[1] = '.';
1787 /* If requested, drop trailing zeros. Never crop past "1.0". */
1788 if (crop_trailing_zeros)
1789 while (last > first + 3 && last[-1] == '0')
1790 last--;
1792 /* Append the exponent. */
1793 sprintf (last, "e%+d", dec_exp);
1795 #ifdef ENABLE_CHECKING
1796 /* Verify that we can read the original value back in. */
1797 if (mode != VOIDmode)
1799 real_from_string (&r, str);
1800 real_convert (&r, mode, &r);
1801 gcc_assert (real_identical (&r, r_orig));
1803 #endif
1806 /* Likewise, except always uses round-to-nearest. */
1808 void
1809 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1810 size_t digits, int crop_trailing_zeros)
1812 real_to_decimal_for_mode (str, r_orig, buf_size,
1813 digits, crop_trailing_zeros, VOIDmode);
1816 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1817 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1818 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1819 strip trailing zeros. */
1821 void
1822 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1823 size_t digits, int crop_trailing_zeros)
1825 int i, j, exp = REAL_EXP (r);
1826 char *p, *first;
1827 char exp_buf[16];
1828 size_t max_digits;
1830 switch (r->cl)
1832 case rvc_zero:
1833 exp = 0;
1834 break;
1835 case rvc_normal:
1836 break;
1837 case rvc_inf:
1838 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1839 return;
1840 case rvc_nan:
1841 /* ??? Print the significand as well, if not canonical? */
1842 sprintf (str, "%c%cNaN", (r->sign ? '-' : '+'),
1843 (r->signalling ? 'S' : 'Q'));
1844 return;
1845 default:
1846 gcc_unreachable ();
1849 if (r->decimal)
1851 /* Hexadecimal format for decimal floats is not interesting. */
1852 strcpy (str, "N/A");
1853 return;
1856 if (digits == 0)
1857 digits = SIGNIFICAND_BITS / 4;
1859 /* Bound the number of digits printed by the size of the output buffer. */
1861 sprintf (exp_buf, "p%+d", exp);
1862 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1863 gcc_assert (max_digits <= buf_size);
1864 if (digits > max_digits)
1865 digits = max_digits;
1867 p = str;
1868 if (r->sign)
1869 *p++ = '-';
1870 *p++ = '0';
1871 *p++ = 'x';
1872 *p++ = '0';
1873 *p++ = '.';
1874 first = p;
1876 for (i = SIGSZ - 1; i >= 0; --i)
1877 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1879 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1880 if (--digits == 0)
1881 goto out;
1884 out:
1885 if (crop_trailing_zeros)
1886 while (p > first + 1 && p[-1] == '0')
1887 p--;
1889 sprintf (p, "p%+d", exp);
1892 /* Initialize R from a decimal or hexadecimal string. The string is
1893 assumed to have been syntax checked already. Return -1 if the
1894 value underflows, +1 if overflows, and 0 otherwise. */
1897 real_from_string (REAL_VALUE_TYPE *r, const char *str)
1899 int exp = 0;
1900 bool sign = false;
1902 get_zero (r, 0);
1904 if (*str == '-')
1906 sign = true;
1907 str++;
1909 else if (*str == '+')
1910 str++;
1912 if (!strncmp (str, "QNaN", 4))
1914 get_canonical_qnan (r, sign);
1915 return 0;
1917 else if (!strncmp (str, "SNaN", 4))
1919 get_canonical_snan (r, sign);
1920 return 0;
1922 else if (!strncmp (str, "Inf", 3))
1924 get_inf (r, sign);
1925 return 0;
1928 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1930 /* Hexadecimal floating point. */
1931 int pos = SIGNIFICAND_BITS - 4, d;
1933 str += 2;
1935 while (*str == '0')
1936 str++;
1937 while (1)
1939 d = hex_value (*str);
1940 if (d == _hex_bad)
1941 break;
1942 if (pos >= 0)
1944 r->sig[pos / HOST_BITS_PER_LONG]
1945 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1946 pos -= 4;
1948 else if (d)
1949 /* Ensure correct rounding by setting last bit if there is
1950 a subsequent nonzero digit. */
1951 r->sig[0] |= 1;
1952 exp += 4;
1953 str++;
1955 if (*str == '.')
1957 str++;
1958 if (pos == SIGNIFICAND_BITS - 4)
1960 while (*str == '0')
1961 str++, exp -= 4;
1963 while (1)
1965 d = hex_value (*str);
1966 if (d == _hex_bad)
1967 break;
1968 if (pos >= 0)
1970 r->sig[pos / HOST_BITS_PER_LONG]
1971 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1972 pos -= 4;
1974 else if (d)
1975 /* Ensure correct rounding by setting last bit if there is
1976 a subsequent nonzero digit. */
1977 r->sig[0] |= 1;
1978 str++;
1982 /* If the mantissa is zero, ignore the exponent. */
1983 if (!cmp_significand_0 (r))
1984 goto is_a_zero;
1986 if (*str == 'p' || *str == 'P')
1988 bool exp_neg = false;
1990 str++;
1991 if (*str == '-')
1993 exp_neg = true;
1994 str++;
1996 else if (*str == '+')
1997 str++;
1999 d = 0;
2000 while (ISDIGIT (*str))
2002 d *= 10;
2003 d += *str - '0';
2004 if (d > MAX_EXP)
2006 /* Overflowed the exponent. */
2007 if (exp_neg)
2008 goto underflow;
2009 else
2010 goto overflow;
2012 str++;
2014 if (exp_neg)
2015 d = -d;
2017 exp += d;
2020 r->cl = rvc_normal;
2021 SET_REAL_EXP (r, exp);
2023 normalize (r);
2025 else
2027 /* Decimal floating point. */
2028 const char *cstr = str;
2029 mpfr_t m;
2030 bool inexact;
2032 while (*cstr == '0')
2033 cstr++;
2034 if (*cstr == '.')
2036 cstr++;
2037 while (*cstr == '0')
2038 cstr++;
2041 /* If the mantissa is zero, ignore the exponent. */
2042 if (!ISDIGIT (*cstr))
2043 goto is_a_zero;
2045 /* Nonzero value, possibly overflowing or underflowing. */
2046 mpfr_init2 (m, SIGNIFICAND_BITS);
2047 inexact = mpfr_strtofr (m, str, NULL, 10, GMP_RNDZ);
2048 /* The result should never be a NaN, and because the rounding is
2049 toward zero should never be an infinity. */
2050 gcc_assert (!mpfr_nan_p (m) && !mpfr_inf_p (m));
2051 if (mpfr_zero_p (m) || mpfr_get_exp (m) < -MAX_EXP + 4)
2053 mpfr_clear (m);
2054 goto underflow;
2056 else if (mpfr_get_exp (m) > MAX_EXP - 4)
2058 mpfr_clear (m);
2059 goto overflow;
2061 else
2063 real_from_mpfr (r, m, NULL_TREE, GMP_RNDZ);
2064 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2065 because the hex digits used in real_from_mpfr did not
2066 start with a digit 8 to f, but the exponent bounds above
2067 should have avoided underflow or overflow. */
2068 gcc_assert (r->cl == rvc_normal);
2069 /* Set a sticky bit if mpfr_strtofr was inexact. */
2070 r->sig[0] |= inexact;
2071 mpfr_clear (m);
2075 r->sign = sign;
2076 return 0;
2078 is_a_zero:
2079 get_zero (r, sign);
2080 return 0;
2082 underflow:
2083 get_zero (r, sign);
2084 return -1;
2086 overflow:
2087 get_inf (r, sign);
2088 return 1;
2091 /* Legacy. Similar, but return the result directly. */
2093 REAL_VALUE_TYPE
2094 real_from_string2 (const char *s, machine_mode mode)
2096 REAL_VALUE_TYPE r;
2098 real_from_string (&r, s);
2099 if (mode != VOIDmode)
2100 real_convert (&r, mode, &r);
2102 return r;
2105 /* Initialize R from string S and desired MODE. */
2107 void
2108 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, machine_mode mode)
2110 if (DECIMAL_FLOAT_MODE_P (mode))
2111 decimal_real_from_string (r, s);
2112 else
2113 real_from_string (r, s);
2115 if (mode != VOIDmode)
2116 real_convert (r, mode, r);
2119 /* Initialize R from the wide_int VAL_IN. The MODE is not VOIDmode,*/
2121 void
2122 real_from_integer (REAL_VALUE_TYPE *r, machine_mode mode,
2123 const wide_int_ref &val_in, signop sgn)
2125 if (val_in == 0)
2126 get_zero (r, 0);
2127 else
2129 unsigned int len = val_in.get_precision ();
2130 int i, j, e = 0;
2131 int maxbitlen = MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT;
2132 const unsigned int realmax = (SIGNIFICAND_BITS / HOST_BITS_PER_WIDE_INT
2133 * HOST_BITS_PER_WIDE_INT);
2135 memset (r, 0, sizeof (*r));
2136 r->cl = rvc_normal;
2137 r->sign = wi::neg_p (val_in, sgn);
2139 /* We have to ensure we can negate the largest negative number. */
2140 wide_int val = wide_int::from (val_in, maxbitlen, sgn);
2142 if (r->sign)
2143 val = -val;
2145 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2146 won't work with precisions that are not a multiple of
2147 HOST_BITS_PER_WIDE_INT. */
2148 len += HOST_BITS_PER_WIDE_INT - 1;
2150 /* Ensure we can represent the largest negative number. */
2151 len += 1;
2153 len = len/HOST_BITS_PER_WIDE_INT * HOST_BITS_PER_WIDE_INT;
2155 /* Cap the size to the size allowed by real.h. */
2156 if (len > realmax)
2158 HOST_WIDE_INT cnt_l_z;
2159 cnt_l_z = wi::clz (val);
2161 if (maxbitlen - cnt_l_z > realmax)
2163 e = maxbitlen - cnt_l_z - realmax;
2165 /* This value is too large, we must shift it right to
2166 preserve all the bits we can, and then bump the
2167 exponent up by that amount. */
2168 val = wi::lrshift (val, e);
2170 len = realmax;
2173 /* Clear out top bits so elt will work with precisions that aren't
2174 a multiple of HOST_BITS_PER_WIDE_INT. */
2175 val = wide_int::from (val, len, sgn);
2176 len = len / HOST_BITS_PER_WIDE_INT;
2178 SET_REAL_EXP (r, len * HOST_BITS_PER_WIDE_INT + e);
2180 j = SIGSZ - 1;
2181 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2182 for (i = len - 1; i >= 0; i--)
2184 r->sig[j--] = val.elt (i);
2185 if (j < 0)
2186 break;
2188 else
2190 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2191 for (i = len - 1; i >= 0; i--)
2193 HOST_WIDE_INT e = val.elt (i);
2194 r->sig[j--] = e >> (HOST_BITS_PER_LONG - 1) >> 1;
2195 if (j < 0)
2196 break;
2197 r->sig[j--] = e;
2198 if (j < 0)
2199 break;
2203 normalize (r);
2206 if (DECIMAL_FLOAT_MODE_P (mode))
2207 decimal_from_integer (r);
2208 else if (mode != VOIDmode)
2209 real_convert (r, mode, r);
2212 /* Render R, an integral value, as a floating point constant with no
2213 specified exponent. */
2215 static void
2216 decimal_integer_string (char *str, const REAL_VALUE_TYPE *r_orig,
2217 size_t buf_size)
2219 int dec_exp, digit, digits;
2220 REAL_VALUE_TYPE r, pten;
2221 char *p;
2222 bool sign;
2224 r = *r_orig;
2226 if (r.cl == rvc_zero)
2228 strcpy (str, "0.");
2229 return;
2232 sign = r.sign;
2233 r.sign = 0;
2235 dec_exp = REAL_EXP (&r) * M_LOG10_2;
2236 digits = dec_exp + 1;
2237 gcc_assert ((digits + 2) < (int)buf_size);
2239 pten = *real_digit (1);
2240 times_pten (&pten, dec_exp);
2242 p = str;
2243 if (sign)
2244 *p++ = '-';
2246 digit = rtd_divmod (&r, &pten);
2247 gcc_assert (digit >= 0 && digit <= 9);
2248 *p++ = digit + '0';
2249 while (--digits > 0)
2251 times_pten (&r, 1);
2252 digit = rtd_divmod (&r, &pten);
2253 *p++ = digit + '0';
2255 *p++ = '.';
2256 *p++ = '\0';
2259 /* Convert a real with an integral value to decimal float. */
2261 static void
2262 decimal_from_integer (REAL_VALUE_TYPE *r)
2264 char str[256];
2266 decimal_integer_string (str, r, sizeof (str) - 1);
2267 decimal_real_from_string (r, str);
2270 /* Returns 10**2**N. */
2272 static const REAL_VALUE_TYPE *
2273 ten_to_ptwo (int n)
2275 static REAL_VALUE_TYPE tens[EXP_BITS];
2277 gcc_assert (n >= 0);
2278 gcc_assert (n < EXP_BITS);
2280 if (tens[n].cl == rvc_zero)
2282 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2284 HOST_WIDE_INT t = 10;
2285 int i;
2287 for (i = 0; i < n; ++i)
2288 t *= t;
2290 real_from_integer (&tens[n], VOIDmode, t, UNSIGNED);
2292 else
2294 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2295 do_multiply (&tens[n], t, t);
2299 return &tens[n];
2302 /* Returns 10**(-2**N). */
2304 static const REAL_VALUE_TYPE *
2305 ten_to_mptwo (int n)
2307 static REAL_VALUE_TYPE tens[EXP_BITS];
2309 gcc_assert (n >= 0);
2310 gcc_assert (n < EXP_BITS);
2312 if (tens[n].cl == rvc_zero)
2313 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2315 return &tens[n];
2318 /* Returns N. */
2320 static const REAL_VALUE_TYPE *
2321 real_digit (int n)
2323 static REAL_VALUE_TYPE num[10];
2325 gcc_assert (n >= 0);
2326 gcc_assert (n <= 9);
2328 if (n > 0 && num[n].cl == rvc_zero)
2329 real_from_integer (&num[n], VOIDmode, n, UNSIGNED);
2331 return &num[n];
2334 /* Multiply R by 10**EXP. */
2336 static void
2337 times_pten (REAL_VALUE_TYPE *r, int exp)
2339 REAL_VALUE_TYPE pten, *rr;
2340 bool negative = (exp < 0);
2341 int i;
2343 if (negative)
2345 exp = -exp;
2346 pten = *real_digit (1);
2347 rr = &pten;
2349 else
2350 rr = r;
2352 for (i = 0; exp > 0; ++i, exp >>= 1)
2353 if (exp & 1)
2354 do_multiply (rr, rr, ten_to_ptwo (i));
2356 if (negative)
2357 do_divide (r, r, &pten);
2360 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2362 const REAL_VALUE_TYPE *
2363 dconst_e_ptr (void)
2365 static REAL_VALUE_TYPE value;
2367 /* Initialize mathematical constants for constant folding builtins.
2368 These constants need to be given to at least 160 bits precision. */
2369 if (value.cl == rvc_zero)
2371 mpfr_t m;
2372 mpfr_init2 (m, SIGNIFICAND_BITS);
2373 mpfr_set_ui (m, 1, GMP_RNDN);
2374 mpfr_exp (m, m, GMP_RNDN);
2375 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2376 mpfr_clear (m);
2379 return &value;
2382 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2384 const REAL_VALUE_TYPE *
2385 dconst_third_ptr (void)
2387 static REAL_VALUE_TYPE value;
2389 /* Initialize mathematical constants for constant folding builtins.
2390 These constants need to be given to at least 160 bits precision. */
2391 if (value.cl == rvc_zero)
2393 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (3));
2395 return &value;
2398 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2400 const REAL_VALUE_TYPE *
2401 dconst_sqrt2_ptr (void)
2403 static REAL_VALUE_TYPE value;
2405 /* Initialize mathematical constants for constant folding builtins.
2406 These constants need to be given to at least 160 bits precision. */
2407 if (value.cl == rvc_zero)
2409 mpfr_t m;
2410 mpfr_init2 (m, SIGNIFICAND_BITS);
2411 mpfr_sqrt_ui (m, 2, GMP_RNDN);
2412 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2413 mpfr_clear (m);
2415 return &value;
2418 /* Fills R with +Inf. */
2420 void
2421 real_inf (REAL_VALUE_TYPE *r)
2423 get_inf (r, 0);
2426 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2427 we force a QNaN, else we force an SNaN. The string, if not empty,
2428 is parsed as a number and placed in the significand. Return true
2429 if the string was successfully parsed. */
2431 bool
2432 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2433 machine_mode mode)
2435 const struct real_format *fmt;
2437 fmt = REAL_MODE_FORMAT (mode);
2438 gcc_assert (fmt);
2440 if (*str == 0)
2442 if (quiet)
2443 get_canonical_qnan (r, 0);
2444 else
2445 get_canonical_snan (r, 0);
2447 else
2449 int base = 10, d;
2451 memset (r, 0, sizeof (*r));
2452 r->cl = rvc_nan;
2454 /* Parse akin to strtol into the significand of R. */
2456 while (ISSPACE (*str))
2457 str++;
2458 if (*str == '-')
2459 str++;
2460 else if (*str == '+')
2461 str++;
2462 if (*str == '0')
2464 str++;
2465 if (*str == 'x' || *str == 'X')
2467 base = 16;
2468 str++;
2470 else
2471 base = 8;
2474 while ((d = hex_value (*str)) < base)
2476 REAL_VALUE_TYPE u;
2478 switch (base)
2480 case 8:
2481 lshift_significand (r, r, 3);
2482 break;
2483 case 16:
2484 lshift_significand (r, r, 4);
2485 break;
2486 case 10:
2487 lshift_significand_1 (&u, r);
2488 lshift_significand (r, r, 3);
2489 add_significands (r, r, &u);
2490 break;
2491 default:
2492 gcc_unreachable ();
2495 get_zero (&u, 0);
2496 u.sig[0] = d;
2497 add_significands (r, r, &u);
2499 str++;
2502 /* Must have consumed the entire string for success. */
2503 if (*str != 0)
2504 return false;
2506 /* Shift the significand into place such that the bits
2507 are in the most significant bits for the format. */
2508 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2510 /* Our MSB is always unset for NaNs. */
2511 r->sig[SIGSZ-1] &= ~SIG_MSB;
2513 /* Force quiet or signalling NaN. */
2514 r->signalling = !quiet;
2517 return true;
2520 /* Fills R with the largest finite value representable in mode MODE.
2521 If SIGN is nonzero, R is set to the most negative finite value. */
2523 void
2524 real_maxval (REAL_VALUE_TYPE *r, int sign, machine_mode mode)
2526 const struct real_format *fmt;
2527 int np2;
2529 fmt = REAL_MODE_FORMAT (mode);
2530 gcc_assert (fmt);
2531 memset (r, 0, sizeof (*r));
2533 if (fmt->b == 10)
2534 decimal_real_maxval (r, sign, mode);
2535 else
2537 r->cl = rvc_normal;
2538 r->sign = sign;
2539 SET_REAL_EXP (r, fmt->emax);
2541 np2 = SIGNIFICAND_BITS - fmt->p;
2542 memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2543 clear_significand_below (r, np2);
2545 if (fmt->pnan < fmt->p)
2546 /* This is an IBM extended double format made up of two IEEE
2547 doubles. The value of the long double is the sum of the
2548 values of the two parts. The most significant part is
2549 required to be the value of the long double rounded to the
2550 nearest double. Rounding means we need a slightly smaller
2551 value for LDBL_MAX. */
2552 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1);
2556 /* Fills R with 2**N. */
2558 void
2559 real_2expN (REAL_VALUE_TYPE *r, int n, machine_mode fmode)
2561 memset (r, 0, sizeof (*r));
2563 n++;
2564 if (n > MAX_EXP)
2565 r->cl = rvc_inf;
2566 else if (n < -MAX_EXP)
2568 else
2570 r->cl = rvc_normal;
2571 SET_REAL_EXP (r, n);
2572 r->sig[SIGSZ-1] = SIG_MSB;
2574 if (DECIMAL_FLOAT_MODE_P (fmode))
2575 decimal_real_convert (r, fmode, r);
2579 static void
2580 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2582 int p2, np2, i, w;
2583 int emin2m1, emax2;
2584 bool round_up = false;
2586 if (r->decimal)
2588 if (fmt->b == 10)
2590 decimal_round_for_format (fmt, r);
2591 return;
2593 /* FIXME. We can come here via fp_easy_constant
2594 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2595 investigated whether this convert needs to be here, or
2596 something else is missing. */
2597 decimal_real_convert (r, DFmode, r);
2600 p2 = fmt->p;
2601 emin2m1 = fmt->emin - 1;
2602 emax2 = fmt->emax;
2604 np2 = SIGNIFICAND_BITS - p2;
2605 switch (r->cl)
2607 underflow:
2608 get_zero (r, r->sign);
2609 case rvc_zero:
2610 if (!fmt->has_signed_zero)
2611 r->sign = 0;
2612 return;
2614 overflow:
2615 get_inf (r, r->sign);
2616 case rvc_inf:
2617 return;
2619 case rvc_nan:
2620 clear_significand_below (r, np2);
2621 return;
2623 case rvc_normal:
2624 break;
2626 default:
2627 gcc_unreachable ();
2630 /* Check the range of the exponent. If we're out of range,
2631 either underflow or overflow. */
2632 if (REAL_EXP (r) > emax2)
2633 goto overflow;
2634 else if (REAL_EXP (r) <= emin2m1)
2636 int diff;
2638 if (!fmt->has_denorm)
2640 /* Don't underflow completely until we've had a chance to round. */
2641 if (REAL_EXP (r) < emin2m1)
2642 goto underflow;
2644 else
2646 diff = emin2m1 - REAL_EXP (r) + 1;
2647 if (diff > p2)
2648 goto underflow;
2650 /* De-normalize the significand. */
2651 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2652 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2656 if (!fmt->round_towards_zero)
2658 /* There are P2 true significand bits, followed by one guard bit,
2659 followed by one sticky bit, followed by stuff. Fold nonzero
2660 stuff into the sticky bit. */
2661 unsigned long sticky;
2662 bool guard, lsb;
2664 sticky = 0;
2665 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2666 sticky |= r->sig[i];
2667 sticky |= r->sig[w]
2668 & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2670 guard = test_significand_bit (r, np2 - 1);
2671 lsb = test_significand_bit (r, np2);
2673 /* Round to even. */
2674 round_up = guard && (sticky || lsb);
2677 if (round_up)
2679 REAL_VALUE_TYPE u;
2680 get_zero (&u, 0);
2681 set_significand_bit (&u, np2);
2683 if (add_significands (r, r, &u))
2685 /* Overflow. Means the significand had been all ones, and
2686 is now all zeros. Need to increase the exponent, and
2687 possibly re-normalize it. */
2688 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2689 if (REAL_EXP (r) > emax2)
2690 goto overflow;
2691 r->sig[SIGSZ-1] = SIG_MSB;
2695 /* Catch underflow that we deferred until after rounding. */
2696 if (REAL_EXP (r) <= emin2m1)
2697 goto underflow;
2699 /* Clear out trailing garbage. */
2700 clear_significand_below (r, np2);
2703 /* Extend or truncate to a new mode. */
2705 void
2706 real_convert (REAL_VALUE_TYPE *r, machine_mode mode,
2707 const REAL_VALUE_TYPE *a)
2709 const struct real_format *fmt;
2711 fmt = REAL_MODE_FORMAT (mode);
2712 gcc_assert (fmt);
2714 *r = *a;
2716 if (a->decimal || fmt->b == 10)
2717 decimal_real_convert (r, mode, a);
2719 round_for_format (fmt, r);
2721 /* round_for_format de-normalizes denormals. Undo just that part. */
2722 if (r->cl == rvc_normal)
2723 normalize (r);
2726 /* Legacy. Likewise, except return the struct directly. */
2728 REAL_VALUE_TYPE
2729 real_value_truncate (machine_mode mode, REAL_VALUE_TYPE a)
2731 REAL_VALUE_TYPE r;
2732 real_convert (&r, mode, &a);
2733 return r;
2736 /* Return true if truncating to MODE is exact. */
2738 bool
2739 exact_real_truncate (machine_mode mode, const REAL_VALUE_TYPE *a)
2741 const struct real_format *fmt;
2742 REAL_VALUE_TYPE t;
2743 int emin2m1;
2745 fmt = REAL_MODE_FORMAT (mode);
2746 gcc_assert (fmt);
2748 /* Don't allow conversion to denormals. */
2749 emin2m1 = fmt->emin - 1;
2750 if (REAL_EXP (a) <= emin2m1)
2751 return false;
2753 /* After conversion to the new mode, the value must be identical. */
2754 real_convert (&t, mode, a);
2755 return real_identical (&t, a);
2758 /* Write R to the given target format. Place the words of the result
2759 in target word order in BUF. There are always 32 bits in each
2760 long, no matter the size of the host long.
2762 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2764 long
2765 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
2766 const struct real_format *fmt)
2768 REAL_VALUE_TYPE r;
2769 long buf1;
2771 r = *r_orig;
2772 round_for_format (fmt, &r);
2774 if (!buf)
2775 buf = &buf1;
2776 (*fmt->encode) (fmt, buf, &r);
2778 return *buf;
2781 /* Similar, but look up the format from MODE. */
2783 long
2784 real_to_target (long *buf, const REAL_VALUE_TYPE *r, machine_mode mode)
2786 const struct real_format *fmt;
2788 fmt = REAL_MODE_FORMAT (mode);
2789 gcc_assert (fmt);
2791 return real_to_target_fmt (buf, r, fmt);
2794 /* Read R from the given target format. Read the words of the result
2795 in target word order in BUF. There are always 32 bits in each
2796 long, no matter the size of the host long. */
2798 void
2799 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
2800 const struct real_format *fmt)
2802 (*fmt->decode) (fmt, r, buf);
2805 /* Similar, but look up the format from MODE. */
2807 void
2808 real_from_target (REAL_VALUE_TYPE *r, const long *buf, machine_mode mode)
2810 const struct real_format *fmt;
2812 fmt = REAL_MODE_FORMAT (mode);
2813 gcc_assert (fmt);
2815 (*fmt->decode) (fmt, r, buf);
2818 /* Return the number of bits of the largest binary value that the
2819 significand of MODE will hold. */
2820 /* ??? Legacy. Should get access to real_format directly. */
2823 significand_size (machine_mode mode)
2825 const struct real_format *fmt;
2827 fmt = REAL_MODE_FORMAT (mode);
2828 if (fmt == NULL)
2829 return 0;
2831 if (fmt->b == 10)
2833 /* Return the size in bits of the largest binary value that can be
2834 held by the decimal coefficient for this mode. This is one more
2835 than the number of bits required to hold the largest coefficient
2836 of this mode. */
2837 double log2_10 = 3.3219281;
2838 return fmt->p * log2_10;
2840 return fmt->p;
2843 /* Return a hash value for the given real value. */
2844 /* ??? The "unsigned int" return value is intended to be hashval_t,
2845 but I didn't want to pull hashtab.h into real.h. */
2847 unsigned int
2848 real_hash (const REAL_VALUE_TYPE *r)
2850 unsigned int h;
2851 size_t i;
2853 h = r->cl | (r->sign << 2);
2854 switch (r->cl)
2856 case rvc_zero:
2857 case rvc_inf:
2858 return h;
2860 case rvc_normal:
2861 h |= REAL_EXP (r) << 3;
2862 break;
2864 case rvc_nan:
2865 if (r->signalling)
2866 h ^= (unsigned int)-1;
2867 if (r->canonical)
2868 return h;
2869 break;
2871 default:
2872 gcc_unreachable ();
2875 if (sizeof (unsigned long) > sizeof (unsigned int))
2876 for (i = 0; i < SIGSZ; ++i)
2878 unsigned long s = r->sig[i];
2879 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2881 else
2882 for (i = 0; i < SIGSZ; ++i)
2883 h ^= r->sig[i];
2885 return h;
2888 /* IEEE single-precision format. */
2890 static void encode_ieee_single (const struct real_format *fmt,
2891 long *, const REAL_VALUE_TYPE *);
2892 static void decode_ieee_single (const struct real_format *,
2893 REAL_VALUE_TYPE *, const long *);
2895 static void
2896 encode_ieee_single (const struct real_format *fmt, long *buf,
2897 const REAL_VALUE_TYPE *r)
2899 unsigned long image, sig, exp;
2900 unsigned long sign = r->sign;
2901 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2903 image = sign << 31;
2904 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2906 switch (r->cl)
2908 case rvc_zero:
2909 break;
2911 case rvc_inf:
2912 if (fmt->has_inf)
2913 image |= 255 << 23;
2914 else
2915 image |= 0x7fffffff;
2916 break;
2918 case rvc_nan:
2919 if (fmt->has_nans)
2921 if (r->canonical)
2922 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
2923 if (r->signalling == fmt->qnan_msb_set)
2924 sig &= ~(1 << 22);
2925 else
2926 sig |= 1 << 22;
2927 if (sig == 0)
2928 sig = 1 << 21;
2930 image |= 255 << 23;
2931 image |= sig;
2933 else
2934 image |= 0x7fffffff;
2935 break;
2937 case rvc_normal:
2938 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2939 whereas the intermediate representation is 0.F x 2**exp.
2940 Which means we're off by one. */
2941 if (denormal)
2942 exp = 0;
2943 else
2944 exp = REAL_EXP (r) + 127 - 1;
2945 image |= exp << 23;
2946 image |= sig;
2947 break;
2949 default:
2950 gcc_unreachable ();
2953 buf[0] = image;
2956 static void
2957 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2958 const long *buf)
2960 unsigned long image = buf[0] & 0xffffffff;
2961 bool sign = (image >> 31) & 1;
2962 int exp = (image >> 23) & 0xff;
2964 memset (r, 0, sizeof (*r));
2965 image <<= HOST_BITS_PER_LONG - 24;
2966 image &= ~SIG_MSB;
2968 if (exp == 0)
2970 if (image && fmt->has_denorm)
2972 r->cl = rvc_normal;
2973 r->sign = sign;
2974 SET_REAL_EXP (r, -126);
2975 r->sig[SIGSZ-1] = image << 1;
2976 normalize (r);
2978 else if (fmt->has_signed_zero)
2979 r->sign = sign;
2981 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2983 if (image)
2985 r->cl = rvc_nan;
2986 r->sign = sign;
2987 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
2988 ^ fmt->qnan_msb_set);
2989 r->sig[SIGSZ-1] = image;
2991 else
2993 r->cl = rvc_inf;
2994 r->sign = sign;
2997 else
2999 r->cl = rvc_normal;
3000 r->sign = sign;
3001 SET_REAL_EXP (r, exp - 127 + 1);
3002 r->sig[SIGSZ-1] = image | SIG_MSB;
3006 const struct real_format ieee_single_format =
3008 encode_ieee_single,
3009 decode_ieee_single,
3013 -125,
3014 128,
3017 false,
3018 true,
3019 true,
3020 true,
3021 true,
3022 true,
3023 true,
3024 false,
3025 "ieee_single"
3028 const struct real_format mips_single_format =
3030 encode_ieee_single,
3031 decode_ieee_single,
3035 -125,
3036 128,
3039 false,
3040 true,
3041 true,
3042 true,
3043 true,
3044 true,
3045 false,
3046 true,
3047 "mips_single"
3050 const struct real_format motorola_single_format =
3052 encode_ieee_single,
3053 decode_ieee_single,
3057 -125,
3058 128,
3061 false,
3062 true,
3063 true,
3064 true,
3065 true,
3066 true,
3067 true,
3068 true,
3069 "motorola_single"
3072 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3073 single precision with the following differences:
3074 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3075 are generated.
3076 - NaNs are not supported.
3077 - The range of non-zero numbers in binary is
3078 (001)[1.]000...000 to (255)[1.]111...111.
3079 - Denormals can be represented, but are treated as +0.0 when
3080 used as an operand and are never generated as a result.
3081 - -0.0 can be represented, but a zero result is always +0.0.
3082 - the only supported rounding mode is trunction (towards zero). */
3083 const struct real_format spu_single_format =
3085 encode_ieee_single,
3086 decode_ieee_single,
3090 -125,
3091 129,
3094 true,
3095 false,
3096 false,
3097 false,
3098 true,
3099 true,
3100 false,
3101 false,
3102 "spu_single"
3105 /* IEEE double-precision format. */
3107 static void encode_ieee_double (const struct real_format *fmt,
3108 long *, const REAL_VALUE_TYPE *);
3109 static void decode_ieee_double (const struct real_format *,
3110 REAL_VALUE_TYPE *, const long *);
3112 static void
3113 encode_ieee_double (const struct real_format *fmt, long *buf,
3114 const REAL_VALUE_TYPE *r)
3116 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
3117 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3119 image_hi = r->sign << 31;
3120 image_lo = 0;
3122 if (HOST_BITS_PER_LONG == 64)
3124 sig_hi = r->sig[SIGSZ-1];
3125 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
3126 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
3128 else
3130 sig_hi = r->sig[SIGSZ-1];
3131 sig_lo = r->sig[SIGSZ-2];
3132 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
3133 sig_hi = (sig_hi >> 11) & 0xfffff;
3136 switch (r->cl)
3138 case rvc_zero:
3139 break;
3141 case rvc_inf:
3142 if (fmt->has_inf)
3143 image_hi |= 2047 << 20;
3144 else
3146 image_hi |= 0x7fffffff;
3147 image_lo = 0xffffffff;
3149 break;
3151 case rvc_nan:
3152 if (fmt->has_nans)
3154 if (r->canonical)
3156 if (fmt->canonical_nan_lsbs_set)
3158 sig_hi = (1 << 19) - 1;
3159 sig_lo = 0xffffffff;
3161 else
3163 sig_hi = 0;
3164 sig_lo = 0;
3167 if (r->signalling == fmt->qnan_msb_set)
3168 sig_hi &= ~(1 << 19);
3169 else
3170 sig_hi |= 1 << 19;
3171 if (sig_hi == 0 && sig_lo == 0)
3172 sig_hi = 1 << 18;
3174 image_hi |= 2047 << 20;
3175 image_hi |= sig_hi;
3176 image_lo = sig_lo;
3178 else
3180 image_hi |= 0x7fffffff;
3181 image_lo = 0xffffffff;
3183 break;
3185 case rvc_normal:
3186 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3187 whereas the intermediate representation is 0.F x 2**exp.
3188 Which means we're off by one. */
3189 if (denormal)
3190 exp = 0;
3191 else
3192 exp = REAL_EXP (r) + 1023 - 1;
3193 image_hi |= exp << 20;
3194 image_hi |= sig_hi;
3195 image_lo = sig_lo;
3196 break;
3198 default:
3199 gcc_unreachable ();
3202 if (FLOAT_WORDS_BIG_ENDIAN)
3203 buf[0] = image_hi, buf[1] = image_lo;
3204 else
3205 buf[0] = image_lo, buf[1] = image_hi;
3208 static void
3209 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3210 const long *buf)
3212 unsigned long image_hi, image_lo;
3213 bool sign;
3214 int exp;
3216 if (FLOAT_WORDS_BIG_ENDIAN)
3217 image_hi = buf[0], image_lo = buf[1];
3218 else
3219 image_lo = buf[0], image_hi = buf[1];
3220 image_lo &= 0xffffffff;
3221 image_hi &= 0xffffffff;
3223 sign = (image_hi >> 31) & 1;
3224 exp = (image_hi >> 20) & 0x7ff;
3226 memset (r, 0, sizeof (*r));
3228 image_hi <<= 32 - 21;
3229 image_hi |= image_lo >> 21;
3230 image_hi &= 0x7fffffff;
3231 image_lo <<= 32 - 21;
3233 if (exp == 0)
3235 if ((image_hi || image_lo) && fmt->has_denorm)
3237 r->cl = rvc_normal;
3238 r->sign = sign;
3239 SET_REAL_EXP (r, -1022);
3240 if (HOST_BITS_PER_LONG == 32)
3242 image_hi = (image_hi << 1) | (image_lo >> 31);
3243 image_lo <<= 1;
3244 r->sig[SIGSZ-1] = image_hi;
3245 r->sig[SIGSZ-2] = image_lo;
3247 else
3249 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
3250 r->sig[SIGSZ-1] = image_hi;
3252 normalize (r);
3254 else if (fmt->has_signed_zero)
3255 r->sign = sign;
3257 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
3259 if (image_hi || image_lo)
3261 r->cl = rvc_nan;
3262 r->sign = sign;
3263 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3264 if (HOST_BITS_PER_LONG == 32)
3266 r->sig[SIGSZ-1] = image_hi;
3267 r->sig[SIGSZ-2] = image_lo;
3269 else
3270 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3272 else
3274 r->cl = rvc_inf;
3275 r->sign = sign;
3278 else
3280 r->cl = rvc_normal;
3281 r->sign = sign;
3282 SET_REAL_EXP (r, exp - 1023 + 1);
3283 if (HOST_BITS_PER_LONG == 32)
3285 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3286 r->sig[SIGSZ-2] = image_lo;
3288 else
3289 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3293 const struct real_format ieee_double_format =
3295 encode_ieee_double,
3296 decode_ieee_double,
3300 -1021,
3301 1024,
3304 false,
3305 true,
3306 true,
3307 true,
3308 true,
3309 true,
3310 true,
3311 false,
3312 "ieee_double"
3315 const struct real_format mips_double_format =
3317 encode_ieee_double,
3318 decode_ieee_double,
3322 -1021,
3323 1024,
3326 false,
3327 true,
3328 true,
3329 true,
3330 true,
3331 true,
3332 false,
3333 true,
3334 "mips_double"
3337 const struct real_format motorola_double_format =
3339 encode_ieee_double,
3340 decode_ieee_double,
3344 -1021,
3345 1024,
3348 false,
3349 true,
3350 true,
3351 true,
3352 true,
3353 true,
3354 true,
3355 true,
3356 "motorola_double"
3359 /* IEEE extended real format. This comes in three flavors: Intel's as
3360 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3361 12- and 16-byte images may be big- or little endian; Motorola's is
3362 always big endian. */
3364 /* Helper subroutine which converts from the internal format to the
3365 12-byte little-endian Intel format. Functions below adjust this
3366 for the other possible formats. */
3367 static void
3368 encode_ieee_extended (const struct real_format *fmt, long *buf,
3369 const REAL_VALUE_TYPE *r)
3371 unsigned long image_hi, sig_hi, sig_lo;
3372 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3374 image_hi = r->sign << 15;
3375 sig_hi = sig_lo = 0;
3377 switch (r->cl)
3379 case rvc_zero:
3380 break;
3382 case rvc_inf:
3383 if (fmt->has_inf)
3385 image_hi |= 32767;
3387 /* Intel requires the explicit integer bit to be set, otherwise
3388 it considers the value a "pseudo-infinity". Motorola docs
3389 say it doesn't care. */
3390 sig_hi = 0x80000000;
3392 else
3394 image_hi |= 32767;
3395 sig_lo = sig_hi = 0xffffffff;
3397 break;
3399 case rvc_nan:
3400 if (fmt->has_nans)
3402 image_hi |= 32767;
3403 if (r->canonical)
3405 if (fmt->canonical_nan_lsbs_set)
3407 sig_hi = (1 << 30) - 1;
3408 sig_lo = 0xffffffff;
3411 else if (HOST_BITS_PER_LONG == 32)
3413 sig_hi = r->sig[SIGSZ-1];
3414 sig_lo = r->sig[SIGSZ-2];
3416 else
3418 sig_lo = r->sig[SIGSZ-1];
3419 sig_hi = sig_lo >> 31 >> 1;
3420 sig_lo &= 0xffffffff;
3422 if (r->signalling == fmt->qnan_msb_set)
3423 sig_hi &= ~(1 << 30);
3424 else
3425 sig_hi |= 1 << 30;
3426 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3427 sig_hi = 1 << 29;
3429 /* Intel requires the explicit integer bit to be set, otherwise
3430 it considers the value a "pseudo-nan". Motorola docs say it
3431 doesn't care. */
3432 sig_hi |= 0x80000000;
3434 else
3436 image_hi |= 32767;
3437 sig_lo = sig_hi = 0xffffffff;
3439 break;
3441 case rvc_normal:
3443 int exp = REAL_EXP (r);
3445 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3446 whereas the intermediate representation is 0.F x 2**exp.
3447 Which means we're off by one.
3449 Except for Motorola, which consider exp=0 and explicit
3450 integer bit set to continue to be normalized. In theory
3451 this discrepancy has been taken care of by the difference
3452 in fmt->emin in round_for_format. */
3454 if (denormal)
3455 exp = 0;
3456 else
3458 exp += 16383 - 1;
3459 gcc_assert (exp >= 0);
3461 image_hi |= exp;
3463 if (HOST_BITS_PER_LONG == 32)
3465 sig_hi = r->sig[SIGSZ-1];
3466 sig_lo = r->sig[SIGSZ-2];
3468 else
3470 sig_lo = r->sig[SIGSZ-1];
3471 sig_hi = sig_lo >> 31 >> 1;
3472 sig_lo &= 0xffffffff;
3475 break;
3477 default:
3478 gcc_unreachable ();
3481 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3484 /* Convert from the internal format to the 12-byte Motorola format
3485 for an IEEE extended real. */
3486 static void
3487 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3488 const REAL_VALUE_TYPE *r)
3490 long intermed[3];
3491 encode_ieee_extended (fmt, intermed, r);
3493 if (r->cl == rvc_inf)
3494 /* For infinity clear the explicit integer bit again, so that the
3495 format matches the canonical infinity generated by the FPU. */
3496 intermed[1] = 0;
3498 /* Motorola chips are assumed always to be big-endian. Also, the
3499 padding in a Motorola extended real goes between the exponent and
3500 the mantissa. At this point the mantissa is entirely within
3501 elements 0 and 1 of intermed, and the exponent entirely within
3502 element 2, so all we have to do is swap the order around, and
3503 shift element 2 left 16 bits. */
3504 buf[0] = intermed[2] << 16;
3505 buf[1] = intermed[1];
3506 buf[2] = intermed[0];
3509 /* Convert from the internal format to the 12-byte Intel format for
3510 an IEEE extended real. */
3511 static void
3512 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3513 const REAL_VALUE_TYPE *r)
3515 if (FLOAT_WORDS_BIG_ENDIAN)
3517 /* All the padding in an Intel-format extended real goes at the high
3518 end, which in this case is after the mantissa, not the exponent.
3519 Therefore we must shift everything down 16 bits. */
3520 long intermed[3];
3521 encode_ieee_extended (fmt, intermed, r);
3522 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3523 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3524 buf[2] = (intermed[0] << 16);
3526 else
3527 /* encode_ieee_extended produces what we want directly. */
3528 encode_ieee_extended (fmt, buf, r);
3531 /* Convert from the internal format to the 16-byte Intel format for
3532 an IEEE extended real. */
3533 static void
3534 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3535 const REAL_VALUE_TYPE *r)
3537 /* All the padding in an Intel-format extended real goes at the high end. */
3538 encode_ieee_extended_intel_96 (fmt, buf, r);
3539 buf[3] = 0;
3542 /* As above, we have a helper function which converts from 12-byte
3543 little-endian Intel format to internal format. Functions below
3544 adjust for the other possible formats. */
3545 static void
3546 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3547 const long *buf)
3549 unsigned long image_hi, sig_hi, sig_lo;
3550 bool sign;
3551 int exp;
3553 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3554 sig_lo &= 0xffffffff;
3555 sig_hi &= 0xffffffff;
3556 image_hi &= 0xffffffff;
3558 sign = (image_hi >> 15) & 1;
3559 exp = image_hi & 0x7fff;
3561 memset (r, 0, sizeof (*r));
3563 if (exp == 0)
3565 if ((sig_hi || sig_lo) && fmt->has_denorm)
3567 r->cl = rvc_normal;
3568 r->sign = sign;
3570 /* When the IEEE format contains a hidden bit, we know that
3571 it's zero at this point, and so shift up the significand
3572 and decrease the exponent to match. In this case, Motorola
3573 defines the explicit integer bit to be valid, so we don't
3574 know whether the msb is set or not. */
3575 SET_REAL_EXP (r, fmt->emin);
3576 if (HOST_BITS_PER_LONG == 32)
3578 r->sig[SIGSZ-1] = sig_hi;
3579 r->sig[SIGSZ-2] = sig_lo;
3581 else
3582 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3584 normalize (r);
3586 else if (fmt->has_signed_zero)
3587 r->sign = sign;
3589 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3591 /* See above re "pseudo-infinities" and "pseudo-nans".
3592 Short summary is that the MSB will likely always be
3593 set, and that we don't care about it. */
3594 sig_hi &= 0x7fffffff;
3596 if (sig_hi || sig_lo)
3598 r->cl = rvc_nan;
3599 r->sign = sign;
3600 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3601 if (HOST_BITS_PER_LONG == 32)
3603 r->sig[SIGSZ-1] = sig_hi;
3604 r->sig[SIGSZ-2] = sig_lo;
3606 else
3607 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3609 else
3611 r->cl = rvc_inf;
3612 r->sign = sign;
3615 else
3617 r->cl = rvc_normal;
3618 r->sign = sign;
3619 SET_REAL_EXP (r, exp - 16383 + 1);
3620 if (HOST_BITS_PER_LONG == 32)
3622 r->sig[SIGSZ-1] = sig_hi;
3623 r->sig[SIGSZ-2] = sig_lo;
3625 else
3626 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3630 /* Convert from the internal format to the 12-byte Motorola format
3631 for an IEEE extended real. */
3632 static void
3633 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3634 const long *buf)
3636 long intermed[3];
3638 /* Motorola chips are assumed always to be big-endian. Also, the
3639 padding in a Motorola extended real goes between the exponent and
3640 the mantissa; remove it. */
3641 intermed[0] = buf[2];
3642 intermed[1] = buf[1];
3643 intermed[2] = (unsigned long)buf[0] >> 16;
3645 decode_ieee_extended (fmt, r, intermed);
3648 /* Convert from the internal format to the 12-byte Intel format for
3649 an IEEE extended real. */
3650 static void
3651 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3652 const long *buf)
3654 if (FLOAT_WORDS_BIG_ENDIAN)
3656 /* All the padding in an Intel-format extended real goes at the high
3657 end, which in this case is after the mantissa, not the exponent.
3658 Therefore we must shift everything up 16 bits. */
3659 long intermed[3];
3661 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3662 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3663 intermed[2] = ((unsigned long)buf[0] >> 16);
3665 decode_ieee_extended (fmt, r, intermed);
3667 else
3668 /* decode_ieee_extended produces what we want directly. */
3669 decode_ieee_extended (fmt, r, buf);
3672 /* Convert from the internal format to the 16-byte Intel format for
3673 an IEEE extended real. */
3674 static void
3675 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3676 const long *buf)
3678 /* All the padding in an Intel-format extended real goes at the high end. */
3679 decode_ieee_extended_intel_96 (fmt, r, buf);
3682 const struct real_format ieee_extended_motorola_format =
3684 encode_ieee_extended_motorola,
3685 decode_ieee_extended_motorola,
3689 -16382,
3690 16384,
3693 false,
3694 true,
3695 true,
3696 true,
3697 true,
3698 true,
3699 true,
3700 true,
3701 "ieee_extended_motorola"
3704 const struct real_format ieee_extended_intel_96_format =
3706 encode_ieee_extended_intel_96,
3707 decode_ieee_extended_intel_96,
3711 -16381,
3712 16384,
3715 false,
3716 true,
3717 true,
3718 true,
3719 true,
3720 true,
3721 true,
3722 false,
3723 "ieee_extended_intel_96"
3726 const struct real_format ieee_extended_intel_128_format =
3728 encode_ieee_extended_intel_128,
3729 decode_ieee_extended_intel_128,
3733 -16381,
3734 16384,
3737 false,
3738 true,
3739 true,
3740 true,
3741 true,
3742 true,
3743 true,
3744 false,
3745 "ieee_extended_intel_128"
3748 /* The following caters to i386 systems that set the rounding precision
3749 to 53 bits instead of 64, e.g. FreeBSD. */
3750 const struct real_format ieee_extended_intel_96_round_53_format =
3752 encode_ieee_extended_intel_96,
3753 decode_ieee_extended_intel_96,
3757 -16381,
3758 16384,
3761 false,
3762 true,
3763 true,
3764 true,
3765 true,
3766 true,
3767 true,
3768 false,
3769 "ieee_extended_intel_96_round_53"
3772 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3773 numbers whose sum is equal to the extended precision value. The number
3774 with greater magnitude is first. This format has the same magnitude
3775 range as an IEEE double precision value, but effectively 106 bits of
3776 significand precision. Infinity and NaN are represented by their IEEE
3777 double precision value stored in the first number, the second number is
3778 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3780 static void encode_ibm_extended (const struct real_format *fmt,
3781 long *, const REAL_VALUE_TYPE *);
3782 static void decode_ibm_extended (const struct real_format *,
3783 REAL_VALUE_TYPE *, const long *);
3785 static void
3786 encode_ibm_extended (const struct real_format *fmt, long *buf,
3787 const REAL_VALUE_TYPE *r)
3789 REAL_VALUE_TYPE u, normr, v;
3790 const struct real_format *base_fmt;
3792 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3794 /* Renormalize R before doing any arithmetic on it. */
3795 normr = *r;
3796 if (normr.cl == rvc_normal)
3797 normalize (&normr);
3799 /* u = IEEE double precision portion of significand. */
3800 u = normr;
3801 round_for_format (base_fmt, &u);
3802 encode_ieee_double (base_fmt, &buf[0], &u);
3804 if (u.cl == rvc_normal)
3806 do_add (&v, &normr, &u, 1);
3807 /* Call round_for_format since we might need to denormalize. */
3808 round_for_format (base_fmt, &v);
3809 encode_ieee_double (base_fmt, &buf[2], &v);
3811 else
3813 /* Inf, NaN, 0 are all representable as doubles, so the
3814 least-significant part can be 0.0. */
3815 buf[2] = 0;
3816 buf[3] = 0;
3820 static void
3821 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3822 const long *buf)
3824 REAL_VALUE_TYPE u, v;
3825 const struct real_format *base_fmt;
3827 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3828 decode_ieee_double (base_fmt, &u, &buf[0]);
3830 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3832 decode_ieee_double (base_fmt, &v, &buf[2]);
3833 do_add (r, &u, &v, 0);
3835 else
3836 *r = u;
3839 const struct real_format ibm_extended_format =
3841 encode_ibm_extended,
3842 decode_ibm_extended,
3844 53 + 53,
3846 -1021 + 53,
3847 1024,
3848 127,
3850 false,
3851 true,
3852 true,
3853 true,
3854 true,
3855 true,
3856 true,
3857 false,
3858 "ibm_extended"
3861 const struct real_format mips_extended_format =
3863 encode_ibm_extended,
3864 decode_ibm_extended,
3866 53 + 53,
3868 -1021 + 53,
3869 1024,
3870 127,
3872 false,
3873 true,
3874 true,
3875 true,
3876 true,
3877 true,
3878 false,
3879 true,
3880 "mips_extended"
3884 /* IEEE quad precision format. */
3886 static void encode_ieee_quad (const struct real_format *fmt,
3887 long *, const REAL_VALUE_TYPE *);
3888 static void decode_ieee_quad (const struct real_format *,
3889 REAL_VALUE_TYPE *, const long *);
3891 static void
3892 encode_ieee_quad (const struct real_format *fmt, long *buf,
3893 const REAL_VALUE_TYPE *r)
3895 unsigned long image3, image2, image1, image0, exp;
3896 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3897 REAL_VALUE_TYPE u;
3899 image3 = r->sign << 31;
3900 image2 = 0;
3901 image1 = 0;
3902 image0 = 0;
3904 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3906 switch (r->cl)
3908 case rvc_zero:
3909 break;
3911 case rvc_inf:
3912 if (fmt->has_inf)
3913 image3 |= 32767 << 16;
3914 else
3916 image3 |= 0x7fffffff;
3917 image2 = 0xffffffff;
3918 image1 = 0xffffffff;
3919 image0 = 0xffffffff;
3921 break;
3923 case rvc_nan:
3924 if (fmt->has_nans)
3926 image3 |= 32767 << 16;
3928 if (r->canonical)
3930 if (fmt->canonical_nan_lsbs_set)
3932 image3 |= 0x7fff;
3933 image2 = image1 = image0 = 0xffffffff;
3936 else if (HOST_BITS_PER_LONG == 32)
3938 image0 = u.sig[0];
3939 image1 = u.sig[1];
3940 image2 = u.sig[2];
3941 image3 |= u.sig[3] & 0xffff;
3943 else
3945 image0 = u.sig[0];
3946 image1 = image0 >> 31 >> 1;
3947 image2 = u.sig[1];
3948 image3 |= (image2 >> 31 >> 1) & 0xffff;
3949 image0 &= 0xffffffff;
3950 image2 &= 0xffffffff;
3952 if (r->signalling == fmt->qnan_msb_set)
3953 image3 &= ~0x8000;
3954 else
3955 image3 |= 0x8000;
3956 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3957 image3 |= 0x4000;
3959 else
3961 image3 |= 0x7fffffff;
3962 image2 = 0xffffffff;
3963 image1 = 0xffffffff;
3964 image0 = 0xffffffff;
3966 break;
3968 case rvc_normal:
3969 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3970 whereas the intermediate representation is 0.F x 2**exp.
3971 Which means we're off by one. */
3972 if (denormal)
3973 exp = 0;
3974 else
3975 exp = REAL_EXP (r) + 16383 - 1;
3976 image3 |= exp << 16;
3978 if (HOST_BITS_PER_LONG == 32)
3980 image0 = u.sig[0];
3981 image1 = u.sig[1];
3982 image2 = u.sig[2];
3983 image3 |= u.sig[3] & 0xffff;
3985 else
3987 image0 = u.sig[0];
3988 image1 = image0 >> 31 >> 1;
3989 image2 = u.sig[1];
3990 image3 |= (image2 >> 31 >> 1) & 0xffff;
3991 image0 &= 0xffffffff;
3992 image2 &= 0xffffffff;
3994 break;
3996 default:
3997 gcc_unreachable ();
4000 if (FLOAT_WORDS_BIG_ENDIAN)
4002 buf[0] = image3;
4003 buf[1] = image2;
4004 buf[2] = image1;
4005 buf[3] = image0;
4007 else
4009 buf[0] = image0;
4010 buf[1] = image1;
4011 buf[2] = image2;
4012 buf[3] = image3;
4016 static void
4017 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4018 const long *buf)
4020 unsigned long image3, image2, image1, image0;
4021 bool sign;
4022 int exp;
4024 if (FLOAT_WORDS_BIG_ENDIAN)
4026 image3 = buf[0];
4027 image2 = buf[1];
4028 image1 = buf[2];
4029 image0 = buf[3];
4031 else
4033 image0 = buf[0];
4034 image1 = buf[1];
4035 image2 = buf[2];
4036 image3 = buf[3];
4038 image0 &= 0xffffffff;
4039 image1 &= 0xffffffff;
4040 image2 &= 0xffffffff;
4042 sign = (image3 >> 31) & 1;
4043 exp = (image3 >> 16) & 0x7fff;
4044 image3 &= 0xffff;
4046 memset (r, 0, sizeof (*r));
4048 if (exp == 0)
4050 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
4052 r->cl = rvc_normal;
4053 r->sign = sign;
4055 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
4056 if (HOST_BITS_PER_LONG == 32)
4058 r->sig[0] = image0;
4059 r->sig[1] = image1;
4060 r->sig[2] = image2;
4061 r->sig[3] = image3;
4063 else
4065 r->sig[0] = (image1 << 31 << 1) | image0;
4066 r->sig[1] = (image3 << 31 << 1) | image2;
4069 normalize (r);
4071 else if (fmt->has_signed_zero)
4072 r->sign = sign;
4074 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
4076 if (image3 | image2 | image1 | image0)
4078 r->cl = rvc_nan;
4079 r->sign = sign;
4080 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
4082 if (HOST_BITS_PER_LONG == 32)
4084 r->sig[0] = image0;
4085 r->sig[1] = image1;
4086 r->sig[2] = image2;
4087 r->sig[3] = image3;
4089 else
4091 r->sig[0] = (image1 << 31 << 1) | image0;
4092 r->sig[1] = (image3 << 31 << 1) | image2;
4094 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4096 else
4098 r->cl = rvc_inf;
4099 r->sign = sign;
4102 else
4104 r->cl = rvc_normal;
4105 r->sign = sign;
4106 SET_REAL_EXP (r, exp - 16383 + 1);
4108 if (HOST_BITS_PER_LONG == 32)
4110 r->sig[0] = image0;
4111 r->sig[1] = image1;
4112 r->sig[2] = image2;
4113 r->sig[3] = image3;
4115 else
4117 r->sig[0] = (image1 << 31 << 1) | image0;
4118 r->sig[1] = (image3 << 31 << 1) | image2;
4120 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4121 r->sig[SIGSZ-1] |= SIG_MSB;
4125 const struct real_format ieee_quad_format =
4127 encode_ieee_quad,
4128 decode_ieee_quad,
4130 113,
4131 113,
4132 -16381,
4133 16384,
4134 127,
4135 127,
4136 false,
4137 true,
4138 true,
4139 true,
4140 true,
4141 true,
4142 true,
4143 false,
4144 "ieee_quad"
4147 const struct real_format mips_quad_format =
4149 encode_ieee_quad,
4150 decode_ieee_quad,
4152 113,
4153 113,
4154 -16381,
4155 16384,
4156 127,
4157 127,
4158 false,
4159 true,
4160 true,
4161 true,
4162 true,
4163 true,
4164 false,
4165 true,
4166 "mips_quad"
4169 /* Descriptions of VAX floating point formats can be found beginning at
4171 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4173 The thing to remember is that they're almost IEEE, except for word
4174 order, exponent bias, and the lack of infinities, nans, and denormals.
4176 We don't implement the H_floating format here, simply because neither
4177 the VAX or Alpha ports use it. */
4179 static void encode_vax_f (const struct real_format *fmt,
4180 long *, const REAL_VALUE_TYPE *);
4181 static void decode_vax_f (const struct real_format *,
4182 REAL_VALUE_TYPE *, const long *);
4183 static void encode_vax_d (const struct real_format *fmt,
4184 long *, const REAL_VALUE_TYPE *);
4185 static void decode_vax_d (const struct real_format *,
4186 REAL_VALUE_TYPE *, const long *);
4187 static void encode_vax_g (const struct real_format *fmt,
4188 long *, const REAL_VALUE_TYPE *);
4189 static void decode_vax_g (const struct real_format *,
4190 REAL_VALUE_TYPE *, const long *);
4192 static void
4193 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4194 const REAL_VALUE_TYPE *r)
4196 unsigned long sign, exp, sig, image;
4198 sign = r->sign << 15;
4200 switch (r->cl)
4202 case rvc_zero:
4203 image = 0;
4204 break;
4206 case rvc_inf:
4207 case rvc_nan:
4208 image = 0xffff7fff | sign;
4209 break;
4211 case rvc_normal:
4212 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4213 exp = REAL_EXP (r) + 128;
4215 image = (sig << 16) & 0xffff0000;
4216 image |= sign;
4217 image |= exp << 7;
4218 image |= sig >> 16;
4219 break;
4221 default:
4222 gcc_unreachable ();
4225 buf[0] = image;
4228 static void
4229 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
4230 REAL_VALUE_TYPE *r, const long *buf)
4232 unsigned long image = buf[0] & 0xffffffff;
4233 int exp = (image >> 7) & 0xff;
4235 memset (r, 0, sizeof (*r));
4237 if (exp != 0)
4239 r->cl = rvc_normal;
4240 r->sign = (image >> 15) & 1;
4241 SET_REAL_EXP (r, exp - 128);
4243 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
4244 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4248 static void
4249 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4250 const REAL_VALUE_TYPE *r)
4252 unsigned long image0, image1, sign = r->sign << 15;
4254 switch (r->cl)
4256 case rvc_zero:
4257 image0 = image1 = 0;
4258 break;
4260 case rvc_inf:
4261 case rvc_nan:
4262 image0 = 0xffff7fff | sign;
4263 image1 = 0xffffffff;
4264 break;
4266 case rvc_normal:
4267 /* Extract the significand into straight hi:lo. */
4268 if (HOST_BITS_PER_LONG == 64)
4270 image0 = r->sig[SIGSZ-1];
4271 image1 = (image0 >> (64 - 56)) & 0xffffffff;
4272 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
4274 else
4276 image0 = r->sig[SIGSZ-1];
4277 image1 = r->sig[SIGSZ-2];
4278 image1 = (image0 << 24) | (image1 >> 8);
4279 image0 = (image0 >> 8) & 0xffffff;
4282 /* Rearrange the half-words of the significand to match the
4283 external format. */
4284 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
4285 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4287 /* Add the sign and exponent. */
4288 image0 |= sign;
4289 image0 |= (REAL_EXP (r) + 128) << 7;
4290 break;
4292 default:
4293 gcc_unreachable ();
4296 if (FLOAT_WORDS_BIG_ENDIAN)
4297 buf[0] = image1, buf[1] = image0;
4298 else
4299 buf[0] = image0, buf[1] = image1;
4302 static void
4303 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
4304 REAL_VALUE_TYPE *r, const long *buf)
4306 unsigned long image0, image1;
4307 int exp;
4309 if (FLOAT_WORDS_BIG_ENDIAN)
4310 image1 = buf[0], image0 = buf[1];
4311 else
4312 image0 = buf[0], image1 = buf[1];
4313 image0 &= 0xffffffff;
4314 image1 &= 0xffffffff;
4316 exp = (image0 >> 7) & 0xff;
4318 memset (r, 0, sizeof (*r));
4320 if (exp != 0)
4322 r->cl = rvc_normal;
4323 r->sign = (image0 >> 15) & 1;
4324 SET_REAL_EXP (r, exp - 128);
4326 /* Rearrange the half-words of the external format into
4327 proper ascending order. */
4328 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4329 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4331 if (HOST_BITS_PER_LONG == 64)
4333 image0 = (image0 << 31 << 1) | image1;
4334 image0 <<= 64 - 56;
4335 image0 |= SIG_MSB;
4336 r->sig[SIGSZ-1] = image0;
4338 else
4340 r->sig[SIGSZ-1] = image0;
4341 r->sig[SIGSZ-2] = image1;
4342 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4343 r->sig[SIGSZ-1] |= SIG_MSB;
4348 static void
4349 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4350 const REAL_VALUE_TYPE *r)
4352 unsigned long image0, image1, sign = r->sign << 15;
4354 switch (r->cl)
4356 case rvc_zero:
4357 image0 = image1 = 0;
4358 break;
4360 case rvc_inf:
4361 case rvc_nan:
4362 image0 = 0xffff7fff | sign;
4363 image1 = 0xffffffff;
4364 break;
4366 case rvc_normal:
4367 /* Extract the significand into straight hi:lo. */
4368 if (HOST_BITS_PER_LONG == 64)
4370 image0 = r->sig[SIGSZ-1];
4371 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4372 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4374 else
4376 image0 = r->sig[SIGSZ-1];
4377 image1 = r->sig[SIGSZ-2];
4378 image1 = (image0 << 21) | (image1 >> 11);
4379 image0 = (image0 >> 11) & 0xfffff;
4382 /* Rearrange the half-words of the significand to match the
4383 external format. */
4384 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4385 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4387 /* Add the sign and exponent. */
4388 image0 |= sign;
4389 image0 |= (REAL_EXP (r) + 1024) << 4;
4390 break;
4392 default:
4393 gcc_unreachable ();
4396 if (FLOAT_WORDS_BIG_ENDIAN)
4397 buf[0] = image1, buf[1] = image0;
4398 else
4399 buf[0] = image0, buf[1] = image1;
4402 static void
4403 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4404 REAL_VALUE_TYPE *r, const long *buf)
4406 unsigned long image0, image1;
4407 int exp;
4409 if (FLOAT_WORDS_BIG_ENDIAN)
4410 image1 = buf[0], image0 = buf[1];
4411 else
4412 image0 = buf[0], image1 = buf[1];
4413 image0 &= 0xffffffff;
4414 image1 &= 0xffffffff;
4416 exp = (image0 >> 4) & 0x7ff;
4418 memset (r, 0, sizeof (*r));
4420 if (exp != 0)
4422 r->cl = rvc_normal;
4423 r->sign = (image0 >> 15) & 1;
4424 SET_REAL_EXP (r, exp - 1024);
4426 /* Rearrange the half-words of the external format into
4427 proper ascending order. */
4428 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4429 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4431 if (HOST_BITS_PER_LONG == 64)
4433 image0 = (image0 << 31 << 1) | image1;
4434 image0 <<= 64 - 53;
4435 image0 |= SIG_MSB;
4436 r->sig[SIGSZ-1] = image0;
4438 else
4440 r->sig[SIGSZ-1] = image0;
4441 r->sig[SIGSZ-2] = image1;
4442 lshift_significand (r, r, 64 - 53);
4443 r->sig[SIGSZ-1] |= SIG_MSB;
4448 const struct real_format vax_f_format =
4450 encode_vax_f,
4451 decode_vax_f,
4455 -127,
4456 127,
4459 false,
4460 false,
4461 false,
4462 false,
4463 false,
4464 false,
4465 false,
4466 false,
4467 "vax_f"
4470 const struct real_format vax_d_format =
4472 encode_vax_d,
4473 decode_vax_d,
4477 -127,
4478 127,
4481 false,
4482 false,
4483 false,
4484 false,
4485 false,
4486 false,
4487 false,
4488 false,
4489 "vax_d"
4492 const struct real_format vax_g_format =
4494 encode_vax_g,
4495 decode_vax_g,
4499 -1023,
4500 1023,
4503 false,
4504 false,
4505 false,
4506 false,
4507 false,
4508 false,
4509 false,
4510 false,
4511 "vax_g"
4514 /* Encode real R into a single precision DFP value in BUF. */
4515 static void
4516 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4517 long *buf ATTRIBUTE_UNUSED,
4518 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4520 encode_decimal32 (fmt, buf, r);
4523 /* Decode a single precision DFP value in BUF into a real R. */
4524 static void
4525 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4526 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4527 const long *buf ATTRIBUTE_UNUSED)
4529 decode_decimal32 (fmt, r, buf);
4532 /* Encode real R into a double precision DFP value in BUF. */
4533 static void
4534 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4535 long *buf ATTRIBUTE_UNUSED,
4536 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4538 encode_decimal64 (fmt, buf, r);
4541 /* Decode a double precision DFP value in BUF into a real R. */
4542 static void
4543 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4544 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4545 const long *buf ATTRIBUTE_UNUSED)
4547 decode_decimal64 (fmt, r, buf);
4550 /* Encode real R into a quad precision DFP value in BUF. */
4551 static void
4552 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4553 long *buf ATTRIBUTE_UNUSED,
4554 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4556 encode_decimal128 (fmt, buf, r);
4559 /* Decode a quad precision DFP value in BUF into a real R. */
4560 static void
4561 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4562 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4563 const long *buf ATTRIBUTE_UNUSED)
4565 decode_decimal128 (fmt, r, buf);
4568 /* Single precision decimal floating point (IEEE 754). */
4569 const struct real_format decimal_single_format =
4571 encode_decimal_single,
4572 decode_decimal_single,
4576 -94,
4580 false,
4581 true,
4582 true,
4583 true,
4584 true,
4585 true,
4586 true,
4587 false,
4588 "decimal_single"
4591 /* Double precision decimal floating point (IEEE 754). */
4592 const struct real_format decimal_double_format =
4594 encode_decimal_double,
4595 decode_decimal_double,
4599 -382,
4600 385,
4603 false,
4604 true,
4605 true,
4606 true,
4607 true,
4608 true,
4609 true,
4610 false,
4611 "decimal_double"
4614 /* Quad precision decimal floating point (IEEE 754). */
4615 const struct real_format decimal_quad_format =
4617 encode_decimal_quad,
4618 decode_decimal_quad,
4622 -6142,
4623 6145,
4624 127,
4625 127,
4626 false,
4627 true,
4628 true,
4629 true,
4630 true,
4631 true,
4632 true,
4633 false,
4634 "decimal_quad"
4637 /* Encode half-precision floats. This routine is used both for the IEEE
4638 ARM alternative encodings. */
4639 static void
4640 encode_ieee_half (const struct real_format *fmt, long *buf,
4641 const REAL_VALUE_TYPE *r)
4643 unsigned long image, sig, exp;
4644 unsigned long sign = r->sign;
4645 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
4647 image = sign << 15;
4648 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 11)) & 0x3ff;
4650 switch (r->cl)
4652 case rvc_zero:
4653 break;
4655 case rvc_inf:
4656 if (fmt->has_inf)
4657 image |= 31 << 10;
4658 else
4659 image |= 0x7fff;
4660 break;
4662 case rvc_nan:
4663 if (fmt->has_nans)
4665 if (r->canonical)
4666 sig = (fmt->canonical_nan_lsbs_set ? (1 << 9) - 1 : 0);
4667 if (r->signalling == fmt->qnan_msb_set)
4668 sig &= ~(1 << 9);
4669 else
4670 sig |= 1 << 9;
4671 if (sig == 0)
4672 sig = 1 << 8;
4674 image |= 31 << 10;
4675 image |= sig;
4677 else
4678 image |= 0x3ff;
4679 break;
4681 case rvc_normal:
4682 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4683 whereas the intermediate representation is 0.F x 2**exp.
4684 Which means we're off by one. */
4685 if (denormal)
4686 exp = 0;
4687 else
4688 exp = REAL_EXP (r) + 15 - 1;
4689 image |= exp << 10;
4690 image |= sig;
4691 break;
4693 default:
4694 gcc_unreachable ();
4697 buf[0] = image;
4700 /* Decode half-precision floats. This routine is used both for the IEEE
4701 ARM alternative encodings. */
4702 static void
4703 decode_ieee_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4704 const long *buf)
4706 unsigned long image = buf[0] & 0xffff;
4707 bool sign = (image >> 15) & 1;
4708 int exp = (image >> 10) & 0x1f;
4710 memset (r, 0, sizeof (*r));
4711 image <<= HOST_BITS_PER_LONG - 11;
4712 image &= ~SIG_MSB;
4714 if (exp == 0)
4716 if (image && fmt->has_denorm)
4718 r->cl = rvc_normal;
4719 r->sign = sign;
4720 SET_REAL_EXP (r, -14);
4721 r->sig[SIGSZ-1] = image << 1;
4722 normalize (r);
4724 else if (fmt->has_signed_zero)
4725 r->sign = sign;
4727 else if (exp == 31 && (fmt->has_nans || fmt->has_inf))
4729 if (image)
4731 r->cl = rvc_nan;
4732 r->sign = sign;
4733 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4734 ^ fmt->qnan_msb_set);
4735 r->sig[SIGSZ-1] = image;
4737 else
4739 r->cl = rvc_inf;
4740 r->sign = sign;
4743 else
4745 r->cl = rvc_normal;
4746 r->sign = sign;
4747 SET_REAL_EXP (r, exp - 15 + 1);
4748 r->sig[SIGSZ-1] = image | SIG_MSB;
4752 /* Half-precision format, as specified in IEEE 754R. */
4753 const struct real_format ieee_half_format =
4755 encode_ieee_half,
4756 decode_ieee_half,
4760 -13,
4764 false,
4765 true,
4766 true,
4767 true,
4768 true,
4769 true,
4770 true,
4771 false,
4772 "ieee_half"
4775 /* ARM's alternative half-precision format, similar to IEEE but with
4776 no reserved exponent value for NaNs and infinities; rather, it just
4777 extends the range of exponents by one. */
4778 const struct real_format arm_half_format =
4780 encode_ieee_half,
4781 decode_ieee_half,
4785 -13,
4789 false,
4790 true,
4791 false,
4792 false,
4793 true,
4794 true,
4795 false,
4796 false,
4797 "arm_half"
4800 /* A synthetic "format" for internal arithmetic. It's the size of the
4801 internal significand minus the two bits needed for proper rounding.
4802 The encode and decode routines exist only to satisfy our paranoia
4803 harness. */
4805 static void encode_internal (const struct real_format *fmt,
4806 long *, const REAL_VALUE_TYPE *);
4807 static void decode_internal (const struct real_format *,
4808 REAL_VALUE_TYPE *, const long *);
4810 static void
4811 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4812 const REAL_VALUE_TYPE *r)
4814 memcpy (buf, r, sizeof (*r));
4817 static void
4818 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
4819 REAL_VALUE_TYPE *r, const long *buf)
4821 memcpy (r, buf, sizeof (*r));
4824 const struct real_format real_internal_format =
4826 encode_internal,
4827 decode_internal,
4829 SIGNIFICAND_BITS - 2,
4830 SIGNIFICAND_BITS - 2,
4831 -MAX_EXP,
4832 MAX_EXP,
4835 false,
4836 false,
4837 true,
4838 true,
4839 false,
4840 true,
4841 true,
4842 false,
4843 "real_internal"
4846 /* Calculate X raised to the integer exponent N in mode MODE and store
4847 the result in R. Return true if the result may be inexact due to
4848 loss of precision. The algorithm is the classic "left-to-right binary
4849 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4850 Algorithms", "The Art of Computer Programming", Volume 2. */
4852 bool
4853 real_powi (REAL_VALUE_TYPE *r, machine_mode mode,
4854 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4856 unsigned HOST_WIDE_INT bit;
4857 REAL_VALUE_TYPE t;
4858 bool inexact = false;
4859 bool init = false;
4860 bool neg;
4861 int i;
4863 if (n == 0)
4865 *r = dconst1;
4866 return false;
4868 else if (n < 0)
4870 /* Don't worry about overflow, from now on n is unsigned. */
4871 neg = true;
4872 n = -n;
4874 else
4875 neg = false;
4877 t = *x;
4878 bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
4879 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4881 if (init)
4883 inexact |= do_multiply (&t, &t, &t);
4884 if (n & bit)
4885 inexact |= do_multiply (&t, &t, x);
4887 else if (n & bit)
4888 init = true;
4889 bit >>= 1;
4892 if (neg)
4893 inexact |= do_divide (&t, &dconst1, &t);
4895 real_convert (r, mode, &t);
4896 return inexact;
4899 /* Round X to the nearest integer not larger in absolute value, i.e.
4900 towards zero, placing the result in R in mode MODE. */
4902 void
4903 real_trunc (REAL_VALUE_TYPE *r, machine_mode mode,
4904 const REAL_VALUE_TYPE *x)
4906 do_fix_trunc (r, x);
4907 if (mode != VOIDmode)
4908 real_convert (r, mode, r);
4911 /* Round X to the largest integer not greater in value, i.e. round
4912 down, placing the result in R in mode MODE. */
4914 void
4915 real_floor (REAL_VALUE_TYPE *r, machine_mode mode,
4916 const REAL_VALUE_TYPE *x)
4918 REAL_VALUE_TYPE t;
4920 do_fix_trunc (&t, x);
4921 if (! real_identical (&t, x) && x->sign)
4922 do_add (&t, &t, &dconstm1, 0);
4923 if (mode != VOIDmode)
4924 real_convert (r, mode, &t);
4925 else
4926 *r = t;
4929 /* Round X to the smallest integer not less then argument, i.e. round
4930 up, placing the result in R in mode MODE. */
4932 void
4933 real_ceil (REAL_VALUE_TYPE *r, machine_mode mode,
4934 const REAL_VALUE_TYPE *x)
4936 REAL_VALUE_TYPE t;
4938 do_fix_trunc (&t, x);
4939 if (! real_identical (&t, x) && ! x->sign)
4940 do_add (&t, &t, &dconst1, 0);
4941 if (mode != VOIDmode)
4942 real_convert (r, mode, &t);
4943 else
4944 *r = t;
4947 /* Round X to the nearest integer, but round halfway cases away from
4948 zero. */
4950 void
4951 real_round (REAL_VALUE_TYPE *r, machine_mode mode,
4952 const REAL_VALUE_TYPE *x)
4954 do_add (r, x, &dconsthalf, x->sign);
4955 do_fix_trunc (r, r);
4956 if (mode != VOIDmode)
4957 real_convert (r, mode, r);
4960 /* Set the sign of R to the sign of X. */
4962 void
4963 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4965 r->sign = x->sign;
4968 /* Check whether the real constant value given is an integer. */
4970 bool
4971 real_isinteger (const REAL_VALUE_TYPE *c, machine_mode mode)
4973 REAL_VALUE_TYPE cint;
4975 real_trunc (&cint, mode, c);
4976 return real_identical (c, &cint);
4979 /* Write into BUF the maximum representable finite floating-point
4980 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4981 float string. LEN is the size of BUF, and the buffer must be large
4982 enough to contain the resulting string. */
4984 void
4985 get_max_float (const struct real_format *fmt, char *buf, size_t len)
4987 int i, n;
4988 char *p;
4990 strcpy (buf, "0x0.");
4991 n = fmt->p;
4992 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
4993 *p++ = 'f';
4994 if (i < n)
4995 *p++ = "08ce"[n - i];
4996 sprintf (p, "p%d", fmt->emax);
4997 if (fmt->pnan < fmt->p)
4999 /* This is an IBM extended double format made up of two IEEE
5000 doubles. The value of the long double is the sum of the
5001 values of the two parts. The most significant part is
5002 required to be the value of the long double rounded to the
5003 nearest double. Rounding means we need a slightly smaller
5004 value for LDBL_MAX. */
5005 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
5008 gcc_assert (strlen (buf) < len);
5011 /* True if mode M has a NaN representation and
5012 the treatment of NaN operands is important. */
5014 bool
5015 HONOR_NANS (machine_mode m)
5017 return MODE_HAS_NANS (m) && !flag_finite_math_only;
5020 bool
5021 HONOR_NANS (const_tree t)
5023 return HONOR_NANS (element_mode (t));
5026 bool
5027 HONOR_NANS (const_rtx x)
5029 return HONOR_NANS (GET_MODE (x));
5032 /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5034 bool
5035 HONOR_SNANS (machine_mode m)
5037 return flag_signaling_nans && HONOR_NANS (m);
5040 bool
5041 HONOR_SNANS (const_tree t)
5043 return HONOR_SNANS (element_mode (t));
5046 bool
5047 HONOR_SNANS (const_rtx x)
5049 return HONOR_SNANS (GET_MODE (x));
5052 /* As for HONOR_NANS, but true if the mode can represent infinity and
5053 the treatment of infinite values is important. */
5055 bool
5056 HONOR_INFINITIES (machine_mode m)
5058 return MODE_HAS_INFINITIES (m) && !flag_finite_math_only;
5061 bool
5062 HONOR_INFINITIES (const_tree t)
5064 return HONOR_INFINITIES (element_mode (t));
5067 bool
5068 HONOR_INFINITIES (const_rtx x)
5070 return HONOR_INFINITIES (GET_MODE (x));
5073 /* Like HONOR_NANS, but true if the given mode distinguishes between
5074 positive and negative zero, and the sign of zero is important. */
5076 bool
5077 HONOR_SIGNED_ZEROS (machine_mode m)
5079 return MODE_HAS_SIGNED_ZEROS (m) && flag_signed_zeros;
5082 bool
5083 HONOR_SIGNED_ZEROS (const_tree t)
5085 return HONOR_SIGNED_ZEROS (element_mode (t));
5088 bool
5089 HONOR_SIGNED_ZEROS (const_rtx x)
5091 return HONOR_SIGNED_ZEROS (GET_MODE (x));
5094 /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5095 and the rounding mode is important. */
5097 bool
5098 HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m)
5100 return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m) && flag_rounding_math;
5103 bool
5104 HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t)
5106 return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t));
5109 bool
5110 HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x)
5112 return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x));