Daily bump.
[official-gcc.git] / gcc / real.c
blob6d3b4356d237475f7ddc5943c4227ed0ff7dad12
1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002,
3 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Stephen L. Moshier (moshier@world.std.com).
6 Re-written by Richard Henderson <rth@redhat.com>
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "diagnostic-core.h"
30 #include "real.h"
31 #include "realmpfr.h"
32 #include "tm_p.h"
33 #include "dfp.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.
64 Note that the decimal string conversion routines are sensitive to
65 rounding errors. Since the raw arithmetic routines do not themselves
66 have guard digits or rounding, the computation of 10**exp can
67 accumulate more than a few digits of error. The previous incarnation
68 of real.c successfully used a 144-bit fraction; given the current
69 layout of REAL_VALUE_TYPE we're forced to expand to at least 160 bits. */
72 /* Used to classify two numbers simultaneously. */
73 #define CLASS2(A, B) ((A) << 2 | (B))
75 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
76 #error "Some constant folding done by hand to avoid shift count warnings"
77 #endif
79 static void get_zero (REAL_VALUE_TYPE *, int);
80 static void get_canonical_qnan (REAL_VALUE_TYPE *, int);
81 static void get_canonical_snan (REAL_VALUE_TYPE *, int);
82 static void get_inf (REAL_VALUE_TYPE *, int);
83 static bool sticky_rshift_significand (REAL_VALUE_TYPE *,
84 const REAL_VALUE_TYPE *, unsigned int);
85 static void rshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
86 unsigned int);
87 static void lshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
88 unsigned int);
89 static void lshift_significand_1 (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
90 static bool add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *,
91 const REAL_VALUE_TYPE *);
92 static bool sub_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
93 const REAL_VALUE_TYPE *, int);
94 static void neg_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
95 static int cmp_significands (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
96 static int cmp_significand_0 (const REAL_VALUE_TYPE *);
97 static void set_significand_bit (REAL_VALUE_TYPE *, unsigned int);
98 static void clear_significand_bit (REAL_VALUE_TYPE *, unsigned int);
99 static bool test_significand_bit (REAL_VALUE_TYPE *, unsigned int);
100 static void clear_significand_below (REAL_VALUE_TYPE *, unsigned int);
101 static bool div_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
102 const REAL_VALUE_TYPE *);
103 static void normalize (REAL_VALUE_TYPE *);
105 static bool do_add (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
106 const REAL_VALUE_TYPE *, int);
107 static bool do_multiply (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
108 const REAL_VALUE_TYPE *);
109 static bool do_divide (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
110 const REAL_VALUE_TYPE *);
111 static int do_compare (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int);
112 static void do_fix_trunc (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
114 static unsigned long rtd_divmod (REAL_VALUE_TYPE *, REAL_VALUE_TYPE *);
115 static void decimal_from_integer (REAL_VALUE_TYPE *);
116 static void decimal_integer_string (char *, const REAL_VALUE_TYPE *,
117 size_t);
119 static const REAL_VALUE_TYPE * ten_to_ptwo (int);
120 static const REAL_VALUE_TYPE * ten_to_mptwo (int);
121 static const REAL_VALUE_TYPE * real_digit (int);
122 static void times_pten (REAL_VALUE_TYPE *, int);
124 static void round_for_format (const struct real_format *, REAL_VALUE_TYPE *);
126 /* Initialize R with a positive zero. */
128 static inline void
129 get_zero (REAL_VALUE_TYPE *r, int sign)
131 memset (r, 0, sizeof (*r));
132 r->sign = sign;
135 /* Initialize R with the canonical quiet NaN. */
137 static inline void
138 get_canonical_qnan (REAL_VALUE_TYPE *r, int sign)
140 memset (r, 0, sizeof (*r));
141 r->cl = rvc_nan;
142 r->sign = sign;
143 r->canonical = 1;
146 static inline void
147 get_canonical_snan (REAL_VALUE_TYPE *r, int sign)
149 memset (r, 0, sizeof (*r));
150 r->cl = rvc_nan;
151 r->sign = sign;
152 r->signalling = 1;
153 r->canonical = 1;
156 static inline void
157 get_inf (REAL_VALUE_TYPE *r, int sign)
159 memset (r, 0, sizeof (*r));
160 r->cl = rvc_inf;
161 r->sign = sign;
165 /* Right-shift the significand of A by N bits; put the result in the
166 significand of R. If any one bits are shifted out, return true. */
168 static bool
169 sticky_rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
170 unsigned int n)
172 unsigned long sticky = 0;
173 unsigned int i, ofs = 0;
175 if (n >= HOST_BITS_PER_LONG)
177 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
178 sticky |= a->sig[i];
179 n &= HOST_BITS_PER_LONG - 1;
182 if (n != 0)
184 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
185 for (i = 0; i < SIGSZ; ++i)
187 r->sig[i]
188 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
189 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
190 << (HOST_BITS_PER_LONG - n)));
193 else
195 for (i = 0; ofs + i < SIGSZ; ++i)
196 r->sig[i] = a->sig[ofs + i];
197 for (; i < SIGSZ; ++i)
198 r->sig[i] = 0;
201 return sticky != 0;
204 /* Right-shift the significand of A by N bits; put the result in the
205 significand of R. */
207 static void
208 rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
209 unsigned int n)
211 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
213 n &= HOST_BITS_PER_LONG - 1;
214 if (n != 0)
216 for (i = 0; i < SIGSZ; ++i)
218 r->sig[i]
219 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
220 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
221 << (HOST_BITS_PER_LONG - n)));
224 else
226 for (i = 0; ofs + i < SIGSZ; ++i)
227 r->sig[i] = a->sig[ofs + i];
228 for (; i < SIGSZ; ++i)
229 r->sig[i] = 0;
233 /* Left-shift the significand of A by N bits; put the result in the
234 significand of R. */
236 static void
237 lshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
238 unsigned int n)
240 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
242 n &= HOST_BITS_PER_LONG - 1;
243 if (n == 0)
245 for (i = 0; ofs + i < SIGSZ; ++i)
246 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
247 for (; i < SIGSZ; ++i)
248 r->sig[SIGSZ-1-i] = 0;
250 else
251 for (i = 0; i < SIGSZ; ++i)
253 r->sig[SIGSZ-1-i]
254 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
255 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
256 >> (HOST_BITS_PER_LONG - n)));
260 /* Likewise, but N is specialized to 1. */
262 static inline void
263 lshift_significand_1 (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
265 unsigned int i;
267 for (i = SIGSZ - 1; i > 0; --i)
268 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
269 r->sig[0] = a->sig[0] << 1;
272 /* Add the significands of A and B, placing the result in R. Return
273 true if there was carry out of the most significant word. */
275 static inline bool
276 add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
277 const REAL_VALUE_TYPE *b)
279 bool carry = false;
280 int i;
282 for (i = 0; i < SIGSZ; ++i)
284 unsigned long ai = a->sig[i];
285 unsigned long ri = ai + b->sig[i];
287 if (carry)
289 carry = ri < ai;
290 carry |= ++ri == 0;
292 else
293 carry = ri < ai;
295 r->sig[i] = ri;
298 return carry;
301 /* Subtract the significands of A and B, placing the result in R. CARRY is
302 true if there's a borrow incoming to the least significant word.
303 Return true if there was borrow out of the most significant word. */
305 static inline bool
306 sub_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
307 const REAL_VALUE_TYPE *b, int carry)
309 int i;
311 for (i = 0; i < SIGSZ; ++i)
313 unsigned long ai = a->sig[i];
314 unsigned long ri = ai - b->sig[i];
316 if (carry)
318 carry = ri > ai;
319 carry |= ~--ri == 0;
321 else
322 carry = ri > ai;
324 r->sig[i] = ri;
327 return carry;
330 /* Negate the significand A, placing the result in R. */
332 static inline void
333 neg_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
335 bool carry = true;
336 int i;
338 for (i = 0; i < SIGSZ; ++i)
340 unsigned long ri, ai = a->sig[i];
342 if (carry)
344 if (ai)
346 ri = -ai;
347 carry = false;
349 else
350 ri = ai;
352 else
353 ri = ~ai;
355 r->sig[i] = ri;
359 /* Compare significands. Return tri-state vs zero. */
361 static inline int
362 cmp_significands (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
364 int i;
366 for (i = SIGSZ - 1; i >= 0; --i)
368 unsigned long ai = a->sig[i];
369 unsigned long bi = b->sig[i];
371 if (ai > bi)
372 return 1;
373 if (ai < bi)
374 return -1;
377 return 0;
380 /* Return true if A is nonzero. */
382 static inline int
383 cmp_significand_0 (const REAL_VALUE_TYPE *a)
385 int i;
387 for (i = SIGSZ - 1; i >= 0; --i)
388 if (a->sig[i])
389 return 1;
391 return 0;
394 /* Set bit N of the significand of R. */
396 static inline void
397 set_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
399 r->sig[n / HOST_BITS_PER_LONG]
400 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
403 /* Clear bit N of the significand of R. */
405 static inline void
406 clear_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
408 r->sig[n / HOST_BITS_PER_LONG]
409 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
412 /* Test bit N of the significand of R. */
414 static inline bool
415 test_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
417 /* ??? Compiler bug here if we return this expression directly.
418 The conversion to bool strips the "&1" and we wind up testing
419 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
420 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
421 return t;
424 /* Clear bits 0..N-1 of the significand of R. */
426 static void
427 clear_significand_below (REAL_VALUE_TYPE *r, unsigned int n)
429 int i, w = n / HOST_BITS_PER_LONG;
431 for (i = 0; i < w; ++i)
432 r->sig[i] = 0;
434 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
437 /* Divide the significands of A and B, placing the result in R. Return
438 true if the division was inexact. */
440 static inline bool
441 div_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
442 const REAL_VALUE_TYPE *b)
444 REAL_VALUE_TYPE u;
445 int i, bit = SIGNIFICAND_BITS - 1;
446 unsigned long msb, inexact;
448 u = *a;
449 memset (r->sig, 0, sizeof (r->sig));
451 msb = 0;
452 goto start;
455 msb = u.sig[SIGSZ-1] & SIG_MSB;
456 lshift_significand_1 (&u, &u);
457 start:
458 if (msb || cmp_significands (&u, b) >= 0)
460 sub_significands (&u, &u, b, 0);
461 set_significand_bit (r, bit);
464 while (--bit >= 0);
466 for (i = 0, inexact = 0; i < SIGSZ; i++)
467 inexact |= u.sig[i];
469 return inexact != 0;
472 /* Adjust the exponent and significand of R such that the most
473 significant bit is set. We underflow to zero and overflow to
474 infinity here, without denormals. (The intermediate representation
475 exponent is large enough to handle target denormals normalized.) */
477 static void
478 normalize (REAL_VALUE_TYPE *r)
480 int shift = 0, exp;
481 int i, j;
483 if (r->decimal)
484 return;
486 /* Find the first word that is nonzero. */
487 for (i = SIGSZ - 1; i >= 0; i--)
488 if (r->sig[i] == 0)
489 shift += HOST_BITS_PER_LONG;
490 else
491 break;
493 /* Zero significand flushes to zero. */
494 if (i < 0)
496 r->cl = rvc_zero;
497 SET_REAL_EXP (r, 0);
498 return;
501 /* Find the first bit that is nonzero. */
502 for (j = 0; ; j++)
503 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
504 break;
505 shift += j;
507 if (shift > 0)
509 exp = REAL_EXP (r) - shift;
510 if (exp > MAX_EXP)
511 get_inf (r, r->sign);
512 else if (exp < -MAX_EXP)
513 get_zero (r, r->sign);
514 else
516 SET_REAL_EXP (r, exp);
517 lshift_significand (r, r, shift);
522 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
523 result may be inexact due to a loss of precision. */
525 static bool
526 do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
527 const REAL_VALUE_TYPE *b, int subtract_p)
529 int dexp, sign, exp;
530 REAL_VALUE_TYPE t;
531 bool inexact = false;
533 /* Determine if we need to add or subtract. */
534 sign = a->sign;
535 subtract_p = (sign ^ b->sign) ^ subtract_p;
537 switch (CLASS2 (a->cl, b->cl))
539 case CLASS2 (rvc_zero, rvc_zero):
540 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
541 get_zero (r, sign & !subtract_p);
542 return false;
544 case CLASS2 (rvc_zero, rvc_normal):
545 case CLASS2 (rvc_zero, rvc_inf):
546 case CLASS2 (rvc_zero, rvc_nan):
547 /* 0 + ANY = ANY. */
548 case CLASS2 (rvc_normal, rvc_nan):
549 case CLASS2 (rvc_inf, rvc_nan):
550 case CLASS2 (rvc_nan, rvc_nan):
551 /* ANY + NaN = NaN. */
552 case CLASS2 (rvc_normal, rvc_inf):
553 /* R + Inf = Inf. */
554 *r = *b;
555 r->sign = sign ^ subtract_p;
556 return false;
558 case CLASS2 (rvc_normal, rvc_zero):
559 case CLASS2 (rvc_inf, rvc_zero):
560 case CLASS2 (rvc_nan, rvc_zero):
561 /* ANY + 0 = ANY. */
562 case CLASS2 (rvc_nan, rvc_normal):
563 case CLASS2 (rvc_nan, rvc_inf):
564 /* NaN + ANY = NaN. */
565 case CLASS2 (rvc_inf, rvc_normal):
566 /* Inf + R = Inf. */
567 *r = *a;
568 return false;
570 case CLASS2 (rvc_inf, rvc_inf):
571 if (subtract_p)
572 /* Inf - Inf = NaN. */
573 get_canonical_qnan (r, 0);
574 else
575 /* Inf + Inf = Inf. */
576 *r = *a;
577 return false;
579 case CLASS2 (rvc_normal, rvc_normal):
580 break;
582 default:
583 gcc_unreachable ();
586 /* Swap the arguments such that A has the larger exponent. */
587 dexp = REAL_EXP (a) - REAL_EXP (b);
588 if (dexp < 0)
590 const REAL_VALUE_TYPE *t;
591 t = a, a = b, b = t;
592 dexp = -dexp;
593 sign ^= subtract_p;
595 exp = REAL_EXP (a);
597 /* If the exponents are not identical, we need to shift the
598 significand of B down. */
599 if (dexp > 0)
601 /* If the exponents are too far apart, the significands
602 do not overlap, which makes the subtraction a noop. */
603 if (dexp >= SIGNIFICAND_BITS)
605 *r = *a;
606 r->sign = sign;
607 return true;
610 inexact |= sticky_rshift_significand (&t, b, dexp);
611 b = &t;
614 if (subtract_p)
616 if (sub_significands (r, a, b, inexact))
618 /* We got a borrow out of the subtraction. That means that
619 A and B had the same exponent, and B had the larger
620 significand. We need to swap the sign and negate the
621 significand. */
622 sign ^= 1;
623 neg_significand (r, r);
626 else
628 if (add_significands (r, a, b))
630 /* We got carry out of the addition. This means we need to
631 shift the significand back down one bit and increase the
632 exponent. */
633 inexact |= sticky_rshift_significand (r, r, 1);
634 r->sig[SIGSZ-1] |= SIG_MSB;
635 if (++exp > MAX_EXP)
637 get_inf (r, sign);
638 return true;
643 r->cl = rvc_normal;
644 r->sign = sign;
645 SET_REAL_EXP (r, exp);
646 /* Zero out the remaining fields. */
647 r->signalling = 0;
648 r->canonical = 0;
649 r->decimal = 0;
651 /* Re-normalize the result. */
652 normalize (r);
654 /* Special case: if the subtraction results in zero, the result
655 is positive. */
656 if (r->cl == rvc_zero)
657 r->sign = 0;
658 else
659 r->sig[0] |= inexact;
661 return inexact;
664 /* Calculate R = A * B. Return true if the result may be inexact. */
666 static bool
667 do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
668 const REAL_VALUE_TYPE *b)
670 REAL_VALUE_TYPE u, t, *rr;
671 unsigned int i, j, k;
672 int sign = a->sign ^ b->sign;
673 bool inexact = false;
675 switch (CLASS2 (a->cl, b->cl))
677 case CLASS2 (rvc_zero, rvc_zero):
678 case CLASS2 (rvc_zero, rvc_normal):
679 case CLASS2 (rvc_normal, rvc_zero):
680 /* +-0 * ANY = 0 with appropriate sign. */
681 get_zero (r, sign);
682 return false;
684 case CLASS2 (rvc_zero, rvc_nan):
685 case CLASS2 (rvc_normal, rvc_nan):
686 case CLASS2 (rvc_inf, rvc_nan):
687 case CLASS2 (rvc_nan, rvc_nan):
688 /* ANY * NaN = NaN. */
689 *r = *b;
690 r->sign = sign;
691 return false;
693 case CLASS2 (rvc_nan, rvc_zero):
694 case CLASS2 (rvc_nan, rvc_normal):
695 case CLASS2 (rvc_nan, rvc_inf):
696 /* NaN * ANY = NaN. */
697 *r = *a;
698 r->sign = sign;
699 return false;
701 case CLASS2 (rvc_zero, rvc_inf):
702 case CLASS2 (rvc_inf, rvc_zero):
703 /* 0 * Inf = NaN */
704 get_canonical_qnan (r, sign);
705 return false;
707 case CLASS2 (rvc_inf, rvc_inf):
708 case CLASS2 (rvc_normal, rvc_inf):
709 case CLASS2 (rvc_inf, rvc_normal):
710 /* Inf * Inf = Inf, R * Inf = Inf */
711 get_inf (r, sign);
712 return false;
714 case CLASS2 (rvc_normal, rvc_normal):
715 break;
717 default:
718 gcc_unreachable ();
721 if (r == a || r == b)
722 rr = &t;
723 else
724 rr = r;
725 get_zero (rr, 0);
727 /* Collect all the partial products. Since we don't have sure access
728 to a widening multiply, we split each long into two half-words.
730 Consider the long-hand form of a four half-word multiplication:
732 A B C D
733 * E F G H
734 --------------
735 DE DF DG DH
736 CE CF CG CH
737 BE BF BG BH
738 AE AF AG AH
740 We construct partial products of the widened half-word products
741 that are known to not overlap, e.g. DF+DH. Each such partial
742 product is given its proper exponent, which allows us to sum them
743 and obtain the finished product. */
745 for (i = 0; i < SIGSZ * 2; ++i)
747 unsigned long ai = a->sig[i / 2];
748 if (i & 1)
749 ai >>= HOST_BITS_PER_LONG / 2;
750 else
751 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
753 if (ai == 0)
754 continue;
756 for (j = 0; j < 2; ++j)
758 int exp = (REAL_EXP (a) - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
759 + (REAL_EXP (b) - (1-j)*(HOST_BITS_PER_LONG/2)));
761 if (exp > MAX_EXP)
763 get_inf (r, sign);
764 return true;
766 if (exp < -MAX_EXP)
768 /* Would underflow to zero, which we shouldn't bother adding. */
769 inexact = true;
770 continue;
773 memset (&u, 0, sizeof (u));
774 u.cl = rvc_normal;
775 SET_REAL_EXP (&u, exp);
777 for (k = j; k < SIGSZ * 2; k += 2)
779 unsigned long bi = b->sig[k / 2];
780 if (k & 1)
781 bi >>= HOST_BITS_PER_LONG / 2;
782 else
783 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
785 u.sig[k / 2] = ai * bi;
788 normalize (&u);
789 inexact |= do_add (rr, rr, &u, 0);
793 rr->sign = sign;
794 if (rr != r)
795 *r = t;
797 return inexact;
800 /* Calculate R = A / B. Return true if the result may be inexact. */
802 static bool
803 do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
804 const REAL_VALUE_TYPE *b)
806 int exp, sign = a->sign ^ b->sign;
807 REAL_VALUE_TYPE t, *rr;
808 bool inexact;
810 switch (CLASS2 (a->cl, b->cl))
812 case CLASS2 (rvc_zero, rvc_zero):
813 /* 0 / 0 = NaN. */
814 case CLASS2 (rvc_inf, rvc_inf):
815 /* Inf / Inf = NaN. */
816 get_canonical_qnan (r, sign);
817 return false;
819 case CLASS2 (rvc_zero, rvc_normal):
820 case CLASS2 (rvc_zero, rvc_inf):
821 /* 0 / ANY = 0. */
822 case CLASS2 (rvc_normal, rvc_inf):
823 /* R / Inf = 0. */
824 get_zero (r, sign);
825 return false;
827 case CLASS2 (rvc_normal, rvc_zero):
828 /* R / 0 = Inf. */
829 case CLASS2 (rvc_inf, rvc_zero):
830 /* Inf / 0 = Inf. */
831 get_inf (r, sign);
832 return false;
834 case CLASS2 (rvc_zero, rvc_nan):
835 case CLASS2 (rvc_normal, rvc_nan):
836 case CLASS2 (rvc_inf, rvc_nan):
837 case CLASS2 (rvc_nan, rvc_nan):
838 /* ANY / NaN = NaN. */
839 *r = *b;
840 r->sign = sign;
841 return false;
843 case CLASS2 (rvc_nan, rvc_zero):
844 case CLASS2 (rvc_nan, rvc_normal):
845 case CLASS2 (rvc_nan, rvc_inf):
846 /* NaN / ANY = NaN. */
847 *r = *a;
848 r->sign = sign;
849 return false;
851 case CLASS2 (rvc_inf, rvc_normal):
852 /* Inf / R = Inf. */
853 get_inf (r, sign);
854 return false;
856 case CLASS2 (rvc_normal, rvc_normal):
857 break;
859 default:
860 gcc_unreachable ();
863 if (r == a || r == b)
864 rr = &t;
865 else
866 rr = r;
868 /* Make sure all fields in the result are initialized. */
869 get_zero (rr, 0);
870 rr->cl = rvc_normal;
871 rr->sign = sign;
873 exp = REAL_EXP (a) - REAL_EXP (b) + 1;
874 if (exp > MAX_EXP)
876 get_inf (r, sign);
877 return true;
879 if (exp < -MAX_EXP)
881 get_zero (r, sign);
882 return true;
884 SET_REAL_EXP (rr, exp);
886 inexact = div_significands (rr, a, b);
888 /* Re-normalize the result. */
889 normalize (rr);
890 rr->sig[0] |= inexact;
892 if (rr != r)
893 *r = t;
895 return inexact;
898 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
899 one of the two operands is a NaN. */
901 static int
902 do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
903 int nan_result)
905 int ret;
907 switch (CLASS2 (a->cl, b->cl))
909 case CLASS2 (rvc_zero, rvc_zero):
910 /* Sign of zero doesn't matter for compares. */
911 return 0;
913 case CLASS2 (rvc_normal, rvc_zero):
914 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
915 if (a->decimal)
916 return decimal_do_compare (a, b, nan_result);
917 /* Fall through. */
918 case CLASS2 (rvc_inf, rvc_zero):
919 case CLASS2 (rvc_inf, rvc_normal):
920 return (a->sign ? -1 : 1);
922 case CLASS2 (rvc_inf, rvc_inf):
923 return -a->sign - -b->sign;
925 case CLASS2 (rvc_zero, rvc_normal):
926 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
927 if (b->decimal)
928 return decimal_do_compare (a, b, nan_result);
929 /* Fall through. */
930 case CLASS2 (rvc_zero, rvc_inf):
931 case CLASS2 (rvc_normal, rvc_inf):
932 return (b->sign ? 1 : -1);
934 case CLASS2 (rvc_zero, rvc_nan):
935 case CLASS2 (rvc_normal, rvc_nan):
936 case CLASS2 (rvc_inf, rvc_nan):
937 case CLASS2 (rvc_nan, rvc_nan):
938 case CLASS2 (rvc_nan, rvc_zero):
939 case CLASS2 (rvc_nan, rvc_normal):
940 case CLASS2 (rvc_nan, rvc_inf):
941 return nan_result;
943 case CLASS2 (rvc_normal, rvc_normal):
944 break;
946 default:
947 gcc_unreachable ();
950 if (a->sign != b->sign)
951 return -a->sign - -b->sign;
953 if (a->decimal || b->decimal)
954 return decimal_do_compare (a, b, nan_result);
956 if (REAL_EXP (a) > REAL_EXP (b))
957 ret = 1;
958 else if (REAL_EXP (a) < REAL_EXP (b))
959 ret = -1;
960 else
961 ret = cmp_significands (a, b);
963 return (a->sign ? -ret : ret);
966 /* Return A truncated to an integral value toward zero. */
968 static void
969 do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
971 *r = *a;
973 switch (r->cl)
975 case rvc_zero:
976 case rvc_inf:
977 case rvc_nan:
978 break;
980 case rvc_normal:
981 if (r->decimal)
983 decimal_do_fix_trunc (r, a);
984 return;
986 if (REAL_EXP (r) <= 0)
987 get_zero (r, r->sign);
988 else if (REAL_EXP (r) < SIGNIFICAND_BITS)
989 clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r));
990 break;
992 default:
993 gcc_unreachable ();
997 /* Perform the binary or unary operation described by CODE.
998 For a unary operation, leave OP1 NULL. This function returns
999 true if the result may be inexact due to loss of precision. */
1001 bool
1002 real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0,
1003 const REAL_VALUE_TYPE *op1)
1005 enum tree_code code = (enum tree_code) icode;
1007 if (op0->decimal || (op1 && op1->decimal))
1008 return decimal_real_arithmetic (r, code, op0, op1);
1010 switch (code)
1012 case PLUS_EXPR:
1013 /* Clear any padding areas in *r if it isn't equal to one of the
1014 operands so that we can later do bitwise comparisons later on. */
1015 if (r != op0 && r != op1)
1016 memset (r, '\0', sizeof (*r));
1017 return do_add (r, op0, op1, 0);
1019 case MINUS_EXPR:
1020 if (r != op0 && r != op1)
1021 memset (r, '\0', sizeof (*r));
1022 return do_add (r, op0, op1, 1);
1024 case MULT_EXPR:
1025 if (r != op0 && r != op1)
1026 memset (r, '\0', sizeof (*r));
1027 return do_multiply (r, op0, op1);
1029 case RDIV_EXPR:
1030 if (r != op0 && r != op1)
1031 memset (r, '\0', sizeof (*r));
1032 return do_divide (r, op0, op1);
1034 case MIN_EXPR:
1035 if (op1->cl == rvc_nan)
1036 *r = *op1;
1037 else if (do_compare (op0, op1, -1) < 0)
1038 *r = *op0;
1039 else
1040 *r = *op1;
1041 break;
1043 case MAX_EXPR:
1044 if (op1->cl == rvc_nan)
1045 *r = *op1;
1046 else if (do_compare (op0, op1, 1) < 0)
1047 *r = *op1;
1048 else
1049 *r = *op0;
1050 break;
1052 case NEGATE_EXPR:
1053 *r = *op0;
1054 r->sign ^= 1;
1055 break;
1057 case ABS_EXPR:
1058 *r = *op0;
1059 r->sign = 0;
1060 break;
1062 case FIX_TRUNC_EXPR:
1063 do_fix_trunc (r, op0);
1064 break;
1066 default:
1067 gcc_unreachable ();
1069 return false;
1072 REAL_VALUE_TYPE
1073 real_value_negate (const REAL_VALUE_TYPE *op0)
1075 REAL_VALUE_TYPE r;
1076 real_arithmetic (&r, NEGATE_EXPR, op0, NULL);
1077 return r;
1080 REAL_VALUE_TYPE
1081 real_value_abs (const REAL_VALUE_TYPE *op0)
1083 REAL_VALUE_TYPE r;
1084 real_arithmetic (&r, ABS_EXPR, op0, NULL);
1085 return r;
1088 bool
1089 real_compare (int icode, const REAL_VALUE_TYPE *op0,
1090 const REAL_VALUE_TYPE *op1)
1092 enum tree_code code = (enum tree_code) icode;
1094 switch (code)
1096 case LT_EXPR:
1097 return do_compare (op0, op1, 1) < 0;
1098 case LE_EXPR:
1099 return do_compare (op0, op1, 1) <= 0;
1100 case GT_EXPR:
1101 return do_compare (op0, op1, -1) > 0;
1102 case GE_EXPR:
1103 return do_compare (op0, op1, -1) >= 0;
1104 case EQ_EXPR:
1105 return do_compare (op0, op1, -1) == 0;
1106 case NE_EXPR:
1107 return do_compare (op0, op1, -1) != 0;
1108 case UNORDERED_EXPR:
1109 return op0->cl == rvc_nan || op1->cl == rvc_nan;
1110 case ORDERED_EXPR:
1111 return op0->cl != rvc_nan && op1->cl != rvc_nan;
1112 case UNLT_EXPR:
1113 return do_compare (op0, op1, -1) < 0;
1114 case UNLE_EXPR:
1115 return do_compare (op0, op1, -1) <= 0;
1116 case UNGT_EXPR:
1117 return do_compare (op0, op1, 1) > 0;
1118 case UNGE_EXPR:
1119 return do_compare (op0, op1, 1) >= 0;
1120 case UNEQ_EXPR:
1121 return do_compare (op0, op1, 0) == 0;
1122 case LTGT_EXPR:
1123 return do_compare (op0, op1, 0) != 0;
1125 default:
1126 gcc_unreachable ();
1130 /* Return floor log2(R). */
1133 real_exponent (const REAL_VALUE_TYPE *r)
1135 switch (r->cl)
1137 case rvc_zero:
1138 return 0;
1139 case rvc_inf:
1140 case rvc_nan:
1141 return (unsigned int)-1 >> 1;
1142 case rvc_normal:
1143 return REAL_EXP (r);
1144 default:
1145 gcc_unreachable ();
1149 /* R = OP0 * 2**EXP. */
1151 void
1152 real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1154 *r = *op0;
1155 switch (r->cl)
1157 case rvc_zero:
1158 case rvc_inf:
1159 case rvc_nan:
1160 break;
1162 case rvc_normal:
1163 exp += REAL_EXP (op0);
1164 if (exp > MAX_EXP)
1165 get_inf (r, r->sign);
1166 else if (exp < -MAX_EXP)
1167 get_zero (r, r->sign);
1168 else
1169 SET_REAL_EXP (r, exp);
1170 break;
1172 default:
1173 gcc_unreachable ();
1177 /* Determine whether a floating-point value X is infinite. */
1179 bool
1180 real_isinf (const REAL_VALUE_TYPE *r)
1182 return (r->cl == rvc_inf);
1185 /* Determine whether a floating-point value X is a NaN. */
1187 bool
1188 real_isnan (const REAL_VALUE_TYPE *r)
1190 return (r->cl == rvc_nan);
1193 /* Determine whether a floating-point value X is finite. */
1195 bool
1196 real_isfinite (const REAL_VALUE_TYPE *r)
1198 return (r->cl != rvc_nan) && (r->cl != rvc_inf);
1201 /* Determine whether a floating-point value X is negative. */
1203 bool
1204 real_isneg (const REAL_VALUE_TYPE *r)
1206 return r->sign;
1209 /* Determine whether a floating-point value X is minus zero. */
1211 bool
1212 real_isnegzero (const REAL_VALUE_TYPE *r)
1214 return r->sign && r->cl == rvc_zero;
1217 /* Compare two floating-point objects for bitwise identity. */
1219 bool
1220 real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1222 int i;
1224 if (a->cl != b->cl)
1225 return false;
1226 if (a->sign != b->sign)
1227 return false;
1229 switch (a->cl)
1231 case rvc_zero:
1232 case rvc_inf:
1233 return true;
1235 case rvc_normal:
1236 if (a->decimal != b->decimal)
1237 return false;
1238 if (REAL_EXP (a) != REAL_EXP (b))
1239 return false;
1240 break;
1242 case rvc_nan:
1243 if (a->signalling != b->signalling)
1244 return false;
1245 /* The significand is ignored for canonical NaNs. */
1246 if (a->canonical || b->canonical)
1247 return a->canonical == b->canonical;
1248 break;
1250 default:
1251 gcc_unreachable ();
1254 for (i = 0; i < SIGSZ; ++i)
1255 if (a->sig[i] != b->sig[i])
1256 return false;
1258 return true;
1261 /* Try to change R into its exact multiplicative inverse in machine
1262 mode MODE. Return true if successful. */
1264 bool
1265 exact_real_inverse (enum machine_mode mode, REAL_VALUE_TYPE *r)
1267 const REAL_VALUE_TYPE *one = real_digit (1);
1268 REAL_VALUE_TYPE u;
1269 int i;
1271 if (r->cl != rvc_normal)
1272 return false;
1274 /* Check for a power of two: all significand bits zero except the MSB. */
1275 for (i = 0; i < SIGSZ-1; ++i)
1276 if (r->sig[i] != 0)
1277 return false;
1278 if (r->sig[SIGSZ-1] != SIG_MSB)
1279 return false;
1281 /* Find the inverse and truncate to the required mode. */
1282 do_divide (&u, one, r);
1283 real_convert (&u, mode, &u);
1285 /* The rounding may have overflowed. */
1286 if (u.cl != rvc_normal)
1287 return false;
1288 for (i = 0; i < SIGSZ-1; ++i)
1289 if (u.sig[i] != 0)
1290 return false;
1291 if (u.sig[SIGSZ-1] != SIG_MSB)
1292 return false;
1294 *r = u;
1295 return true;
1298 /* Return true if arithmetic on values in IMODE that were promoted
1299 from values in TMODE is equivalent to direct arithmetic on values
1300 in TMODE. */
1302 bool
1303 real_can_shorten_arithmetic (enum machine_mode imode, enum machine_mode tmode)
1305 const struct real_format *tfmt, *ifmt;
1306 tfmt = REAL_MODE_FORMAT (tmode);
1307 ifmt = REAL_MODE_FORMAT (imode);
1308 /* These conditions are conservative rather than trying to catch the
1309 exact boundary conditions; the main case to allow is IEEE float
1310 and double. */
1311 return (ifmt->b == tfmt->b
1312 && ifmt->p > 2 * tfmt->p
1313 && ifmt->emin < 2 * tfmt->emin - tfmt->p - 2
1314 && ifmt->emin < tfmt->emin - tfmt->emax - tfmt->p - 2
1315 && ifmt->emax > 2 * tfmt->emax + 2
1316 && ifmt->emax > tfmt->emax - tfmt->emin + tfmt->p + 2
1317 && ifmt->round_towards_zero == tfmt->round_towards_zero
1318 && (ifmt->has_sign_dependent_rounding
1319 == tfmt->has_sign_dependent_rounding)
1320 && ifmt->has_nans >= tfmt->has_nans
1321 && ifmt->has_inf >= tfmt->has_inf
1322 && ifmt->has_signed_zero >= tfmt->has_signed_zero
1323 && !MODE_COMPOSITE_P (tmode)
1324 && !MODE_COMPOSITE_P (imode));
1327 /* Render R as an integer. */
1329 HOST_WIDE_INT
1330 real_to_integer (const REAL_VALUE_TYPE *r)
1332 unsigned HOST_WIDE_INT i;
1334 switch (r->cl)
1336 case rvc_zero:
1337 underflow:
1338 return 0;
1340 case rvc_inf:
1341 case rvc_nan:
1342 overflow:
1343 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1344 if (!r->sign)
1345 i--;
1346 return i;
1348 case rvc_normal:
1349 if (r->decimal)
1350 return decimal_real_to_integer (r);
1352 if (REAL_EXP (r) <= 0)
1353 goto underflow;
1354 /* Only force overflow for unsigned overflow. Signed overflow is
1355 undefined, so it doesn't matter what we return, and some callers
1356 expect to be able to use this routine for both signed and
1357 unsigned conversions. */
1358 if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1359 goto overflow;
1361 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1362 i = r->sig[SIGSZ-1];
1363 else
1365 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1366 i = r->sig[SIGSZ-1];
1367 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1368 i |= r->sig[SIGSZ-2];
1371 i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1373 if (r->sign)
1374 i = -i;
1375 return i;
1377 default:
1378 gcc_unreachable ();
1382 /* Likewise, but to an integer pair, HI+LOW. */
1384 void
1385 real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
1386 const REAL_VALUE_TYPE *r)
1388 REAL_VALUE_TYPE t;
1389 HOST_WIDE_INT low, high;
1390 int exp;
1392 switch (r->cl)
1394 case rvc_zero:
1395 underflow:
1396 low = high = 0;
1397 break;
1399 case rvc_inf:
1400 case rvc_nan:
1401 overflow:
1402 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1403 if (r->sign)
1404 low = 0;
1405 else
1407 high--;
1408 low = -1;
1410 break;
1412 case rvc_normal:
1413 if (r->decimal)
1415 decimal_real_to_integer2 (plow, phigh, r);
1416 return;
1419 exp = REAL_EXP (r);
1420 if (exp <= 0)
1421 goto underflow;
1422 /* Only force overflow for unsigned overflow. Signed overflow is
1423 undefined, so it doesn't matter what we return, and some callers
1424 expect to be able to use this routine for both signed and
1425 unsigned conversions. */
1426 if (exp > 2*HOST_BITS_PER_WIDE_INT)
1427 goto overflow;
1429 rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
1430 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1432 high = t.sig[SIGSZ-1];
1433 low = t.sig[SIGSZ-2];
1435 else
1437 gcc_assert (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG);
1438 high = t.sig[SIGSZ-1];
1439 high = high << (HOST_BITS_PER_LONG - 1) << 1;
1440 high |= t.sig[SIGSZ-2];
1442 low = t.sig[SIGSZ-3];
1443 low = low << (HOST_BITS_PER_LONG - 1) << 1;
1444 low |= t.sig[SIGSZ-4];
1447 if (r->sign)
1449 if (low == 0)
1450 high = -high;
1451 else
1452 low = -low, high = ~high;
1454 break;
1456 default:
1457 gcc_unreachable ();
1460 *plow = low;
1461 *phigh = high;
1464 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1465 of NUM / DEN. Return the quotient and place the remainder in NUM.
1466 It is expected that NUM / DEN are close enough that the quotient is
1467 small. */
1469 static unsigned long
1470 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1472 unsigned long q, msb;
1473 int expn = REAL_EXP (num), expd = REAL_EXP (den);
1475 if (expn < expd)
1476 return 0;
1478 q = msb = 0;
1479 goto start;
1482 msb = num->sig[SIGSZ-1] & SIG_MSB;
1483 q <<= 1;
1484 lshift_significand_1 (num, num);
1485 start:
1486 if (msb || cmp_significands (num, den) >= 0)
1488 sub_significands (num, num, den, 0);
1489 q |= 1;
1492 while (--expn >= expd);
1494 SET_REAL_EXP (num, expd);
1495 normalize (num);
1497 return q;
1500 /* Render R as a decimal floating point constant. Emit DIGITS significant
1501 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1502 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1503 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1504 to a string that, when parsed back in mode MODE, yields the same value. */
1506 #define M_LOG10_2 0.30102999566398119521
1508 void
1509 real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig,
1510 size_t buf_size, size_t digits,
1511 int crop_trailing_zeros, enum machine_mode mode)
1513 const struct real_format *fmt = NULL;
1514 const REAL_VALUE_TYPE *one, *ten;
1515 REAL_VALUE_TYPE r, pten, u, v;
1516 int dec_exp, cmp_one, digit;
1517 size_t max_digits;
1518 char *p, *first, *last;
1519 bool sign;
1520 bool round_up;
1522 if (mode != VOIDmode)
1524 fmt = REAL_MODE_FORMAT (mode);
1525 gcc_assert (fmt);
1528 r = *r_orig;
1529 switch (r.cl)
1531 case rvc_zero:
1532 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1533 return;
1534 case rvc_normal:
1535 break;
1536 case rvc_inf:
1537 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1538 return;
1539 case rvc_nan:
1540 /* ??? Print the significand as well, if not canonical? */
1541 sprintf (str, "%c%cNaN", (r_orig->sign ? '-' : '+'),
1542 (r_orig->signalling ? 'S' : 'Q'));
1543 return;
1544 default:
1545 gcc_unreachable ();
1548 if (r.decimal)
1550 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1551 return;
1554 /* Bound the number of digits printed by the size of the representation. */
1555 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1556 if (digits == 0 || digits > max_digits)
1557 digits = max_digits;
1559 /* Estimate the decimal exponent, and compute the length of the string it
1560 will print as. Be conservative and add one to account for possible
1561 overflow or rounding error. */
1562 dec_exp = REAL_EXP (&r) * M_LOG10_2;
1563 for (max_digits = 1; dec_exp ; max_digits++)
1564 dec_exp /= 10;
1566 /* Bound the number of digits printed by the size of the output buffer. */
1567 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1568 gcc_assert (max_digits <= buf_size);
1569 if (digits > max_digits)
1570 digits = max_digits;
1572 one = real_digit (1);
1573 ten = ten_to_ptwo (0);
1575 sign = r.sign;
1576 r.sign = 0;
1578 dec_exp = 0;
1579 pten = *one;
1581 cmp_one = do_compare (&r, one, 0);
1582 if (cmp_one > 0)
1584 int m;
1586 /* Number is greater than one. Convert significand to an integer
1587 and strip trailing decimal zeros. */
1589 u = r;
1590 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1592 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1593 m = floor_log2 (max_digits);
1595 /* Iterate over the bits of the possible powers of 10 that might
1596 be present in U and eliminate them. That is, if we find that
1597 10**2**M divides U evenly, keep the division and increase
1598 DEC_EXP by 2**M. */
1601 REAL_VALUE_TYPE t;
1603 do_divide (&t, &u, ten_to_ptwo (m));
1604 do_fix_trunc (&v, &t);
1605 if (cmp_significands (&v, &t) == 0)
1607 u = t;
1608 dec_exp += 1 << m;
1611 while (--m >= 0);
1613 /* Revert the scaling to integer that we performed earlier. */
1614 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1615 - (SIGNIFICAND_BITS - 1));
1616 r = u;
1618 /* Find power of 10. Do this by dividing out 10**2**M when
1619 this is larger than the current remainder. Fill PTEN with
1620 the power of 10 that we compute. */
1621 if (REAL_EXP (&r) > 0)
1623 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1626 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1627 if (do_compare (&u, ptentwo, 0) >= 0)
1629 do_divide (&u, &u, ptentwo);
1630 do_multiply (&pten, &pten, ptentwo);
1631 dec_exp += 1 << m;
1634 while (--m >= 0);
1636 else
1637 /* We managed to divide off enough tens in the above reduction
1638 loop that we've now got a negative exponent. Fall into the
1639 less-than-one code to compute the proper value for PTEN. */
1640 cmp_one = -1;
1642 if (cmp_one < 0)
1644 int m;
1646 /* Number is less than one. Pad significand with leading
1647 decimal zeros. */
1649 v = r;
1650 while (1)
1652 /* Stop if we'd shift bits off the bottom. */
1653 if (v.sig[0] & 7)
1654 break;
1656 do_multiply (&u, &v, ten);
1658 /* Stop if we're now >= 1. */
1659 if (REAL_EXP (&u) > 0)
1660 break;
1662 v = u;
1663 dec_exp -= 1;
1665 r = v;
1667 /* Find power of 10. Do this by multiplying in P=10**2**M when
1668 the current remainder is smaller than 1/P. Fill PTEN with the
1669 power of 10 that we compute. */
1670 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1673 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1674 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1676 if (do_compare (&v, ptenmtwo, 0) <= 0)
1678 do_multiply (&v, &v, ptentwo);
1679 do_multiply (&pten, &pten, ptentwo);
1680 dec_exp -= 1 << m;
1683 while (--m >= 0);
1685 /* Invert the positive power of 10 that we've collected so far. */
1686 do_divide (&pten, one, &pten);
1689 p = str;
1690 if (sign)
1691 *p++ = '-';
1692 first = p++;
1694 /* At this point, PTEN should contain the nearest power of 10 smaller
1695 than R, such that this division produces the first digit.
1697 Using a divide-step primitive that returns the complete integral
1698 remainder avoids the rounding error that would be produced if
1699 we were to use do_divide here and then simply multiply by 10 for
1700 each subsequent digit. */
1702 digit = rtd_divmod (&r, &pten);
1704 /* Be prepared for error in that division via underflow ... */
1705 if (digit == 0 && cmp_significand_0 (&r))
1707 /* Multiply by 10 and try again. */
1708 do_multiply (&r, &r, ten);
1709 digit = rtd_divmod (&r, &pten);
1710 dec_exp -= 1;
1711 gcc_assert (digit != 0);
1714 /* ... or overflow. */
1715 if (digit == 10)
1717 *p++ = '1';
1718 if (--digits > 0)
1719 *p++ = '0';
1720 dec_exp += 1;
1722 else
1724 gcc_assert (digit <= 10);
1725 *p++ = digit + '0';
1728 /* Generate subsequent digits. */
1729 while (--digits > 0)
1731 do_multiply (&r, &r, ten);
1732 digit = rtd_divmod (&r, &pten);
1733 *p++ = digit + '0';
1735 last = p;
1737 /* Generate one more digit with which to do rounding. */
1738 do_multiply (&r, &r, ten);
1739 digit = rtd_divmod (&r, &pten);
1741 /* Round the result. */
1742 if (fmt && fmt->round_towards_zero)
1744 /* If the format uses round towards zero when parsing the string
1745 back in, we need to always round away from zero here. */
1746 if (cmp_significand_0 (&r))
1747 digit++;
1748 round_up = digit > 0;
1750 else
1752 if (digit == 5)
1754 /* Round to nearest. If R is nonzero there are additional
1755 nonzero digits to be extracted. */
1756 if (cmp_significand_0 (&r))
1757 digit++;
1758 /* Round to even. */
1759 else if ((p[-1] - '0') & 1)
1760 digit++;
1763 round_up = digit > 5;
1766 if (round_up)
1768 while (p > first)
1770 digit = *--p;
1771 if (digit == '9')
1772 *p = '0';
1773 else
1775 *p = digit + 1;
1776 break;
1780 /* Carry out of the first digit. This means we had all 9's and
1781 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1782 if (p == first)
1784 first[1] = '1';
1785 dec_exp++;
1789 /* Insert the decimal point. */
1790 first[0] = first[1];
1791 first[1] = '.';
1793 /* If requested, drop trailing zeros. Never crop past "1.0". */
1794 if (crop_trailing_zeros)
1795 while (last > first + 3 && last[-1] == '0')
1796 last--;
1798 /* Append the exponent. */
1799 sprintf (last, "e%+d", dec_exp);
1801 #ifdef ENABLE_CHECKING
1802 /* Verify that we can read the original value back in. */
1803 if (mode != VOIDmode)
1805 real_from_string (&r, str);
1806 real_convert (&r, mode, &r);
1807 gcc_assert (real_identical (&r, r_orig));
1809 #endif
1812 /* Likewise, except always uses round-to-nearest. */
1814 void
1815 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1816 size_t digits, int crop_trailing_zeros)
1818 real_to_decimal_for_mode (str, r_orig, buf_size,
1819 digits, crop_trailing_zeros, VOIDmode);
1822 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1823 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1824 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1825 strip trailing zeros. */
1827 void
1828 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1829 size_t digits, int crop_trailing_zeros)
1831 int i, j, exp = REAL_EXP (r);
1832 char *p, *first;
1833 char exp_buf[16];
1834 size_t max_digits;
1836 switch (r->cl)
1838 case rvc_zero:
1839 exp = 0;
1840 break;
1841 case rvc_normal:
1842 break;
1843 case rvc_inf:
1844 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1845 return;
1846 case rvc_nan:
1847 /* ??? Print the significand as well, if not canonical? */
1848 sprintf (str, "%c%cNaN", (r->sign ? '-' : '+'),
1849 (r->signalling ? 'S' : 'Q'));
1850 return;
1851 default:
1852 gcc_unreachable ();
1855 if (r->decimal)
1857 /* Hexadecimal format for decimal floats is not interesting. */
1858 strcpy (str, "N/A");
1859 return;
1862 if (digits == 0)
1863 digits = SIGNIFICAND_BITS / 4;
1865 /* Bound the number of digits printed by the size of the output buffer. */
1867 sprintf (exp_buf, "p%+d", exp);
1868 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1869 gcc_assert (max_digits <= buf_size);
1870 if (digits > max_digits)
1871 digits = max_digits;
1873 p = str;
1874 if (r->sign)
1875 *p++ = '-';
1876 *p++ = '0';
1877 *p++ = 'x';
1878 *p++ = '0';
1879 *p++ = '.';
1880 first = p;
1882 for (i = SIGSZ - 1; i >= 0; --i)
1883 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1885 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1886 if (--digits == 0)
1887 goto out;
1890 out:
1891 if (crop_trailing_zeros)
1892 while (p > first + 1 && p[-1] == '0')
1893 p--;
1895 sprintf (p, "p%+d", exp);
1898 /* Initialize R from a decimal or hexadecimal string. The string is
1899 assumed to have been syntax checked already. Return -1 if the
1900 value underflows, +1 if overflows, and 0 otherwise. */
1903 real_from_string (REAL_VALUE_TYPE *r, const char *str)
1905 int exp = 0;
1906 bool sign = false;
1908 get_zero (r, 0);
1910 if (*str == '-')
1912 sign = true;
1913 str++;
1915 else if (*str == '+')
1916 str++;
1918 if (!strncmp (str, "QNaN", 4))
1920 get_canonical_qnan (r, sign);
1921 return 0;
1923 else if (!strncmp (str, "SNaN", 4))
1925 get_canonical_snan (r, sign);
1926 return 0;
1928 else if (!strncmp (str, "Inf", 3))
1930 get_inf (r, sign);
1931 return 0;
1934 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1936 /* Hexadecimal floating point. */
1937 int pos = SIGNIFICAND_BITS - 4, d;
1939 str += 2;
1941 while (*str == '0')
1942 str++;
1943 while (1)
1945 d = hex_value (*str);
1946 if (d == _hex_bad)
1947 break;
1948 if (pos >= 0)
1950 r->sig[pos / HOST_BITS_PER_LONG]
1951 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1952 pos -= 4;
1954 else if (d)
1955 /* Ensure correct rounding by setting last bit if there is
1956 a subsequent nonzero digit. */
1957 r->sig[0] |= 1;
1958 exp += 4;
1959 str++;
1961 if (*str == '.')
1963 str++;
1964 if (pos == SIGNIFICAND_BITS - 4)
1966 while (*str == '0')
1967 str++, exp -= 4;
1969 while (1)
1971 d = hex_value (*str);
1972 if (d == _hex_bad)
1973 break;
1974 if (pos >= 0)
1976 r->sig[pos / HOST_BITS_PER_LONG]
1977 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1978 pos -= 4;
1980 else if (d)
1981 /* Ensure correct rounding by setting last bit if there is
1982 a subsequent nonzero digit. */
1983 r->sig[0] |= 1;
1984 str++;
1988 /* If the mantissa is zero, ignore the exponent. */
1989 if (!cmp_significand_0 (r))
1990 goto is_a_zero;
1992 if (*str == 'p' || *str == 'P')
1994 bool exp_neg = false;
1996 str++;
1997 if (*str == '-')
1999 exp_neg = true;
2000 str++;
2002 else if (*str == '+')
2003 str++;
2005 d = 0;
2006 while (ISDIGIT (*str))
2008 d *= 10;
2009 d += *str - '0';
2010 if (d > MAX_EXP)
2012 /* Overflowed the exponent. */
2013 if (exp_neg)
2014 goto underflow;
2015 else
2016 goto overflow;
2018 str++;
2020 if (exp_neg)
2021 d = -d;
2023 exp += d;
2026 r->cl = rvc_normal;
2027 SET_REAL_EXP (r, exp);
2029 normalize (r);
2031 else
2033 /* Decimal floating point. */
2034 const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
2035 int d;
2037 while (*str == '0')
2038 str++;
2039 while (ISDIGIT (*str))
2041 d = *str++ - '0';
2042 do_multiply (r, r, ten);
2043 if (d)
2044 do_add (r, r, real_digit (d), 0);
2046 if (*str == '.')
2048 str++;
2049 if (r->cl == rvc_zero)
2051 while (*str == '0')
2052 str++, exp--;
2054 while (ISDIGIT (*str))
2056 d = *str++ - '0';
2057 do_multiply (r, r, ten);
2058 if (d)
2059 do_add (r, r, real_digit (d), 0);
2060 exp--;
2064 /* If the mantissa is zero, ignore the exponent. */
2065 if (r->cl == rvc_zero)
2066 goto is_a_zero;
2068 if (*str == 'e' || *str == 'E')
2070 bool exp_neg = false;
2072 str++;
2073 if (*str == '-')
2075 exp_neg = true;
2076 str++;
2078 else if (*str == '+')
2079 str++;
2081 d = 0;
2082 while (ISDIGIT (*str))
2084 d *= 10;
2085 d += *str - '0';
2086 if (d > MAX_EXP)
2088 /* Overflowed the exponent. */
2089 if (exp_neg)
2090 goto underflow;
2091 else
2092 goto overflow;
2094 str++;
2096 if (exp_neg)
2097 d = -d;
2098 exp += d;
2101 if (exp)
2102 times_pten (r, exp);
2105 r->sign = sign;
2106 return 0;
2108 is_a_zero:
2109 get_zero (r, sign);
2110 return 0;
2112 underflow:
2113 get_zero (r, sign);
2114 return -1;
2116 overflow:
2117 get_inf (r, sign);
2118 return 1;
2121 /* Legacy. Similar, but return the result directly. */
2123 REAL_VALUE_TYPE
2124 real_from_string2 (const char *s, enum machine_mode mode)
2126 REAL_VALUE_TYPE r;
2128 real_from_string (&r, s);
2129 if (mode != VOIDmode)
2130 real_convert (&r, mode, &r);
2132 return r;
2135 /* Initialize R from string S and desired MODE. */
2137 void
2138 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, enum machine_mode mode)
2140 if (DECIMAL_FLOAT_MODE_P (mode))
2141 decimal_real_from_string (r, s);
2142 else
2143 real_from_string (r, s);
2145 if (mode != VOIDmode)
2146 real_convert (r, mode, r);
2149 /* Initialize R from the integer pair HIGH+LOW. */
2151 void
2152 real_from_integer (REAL_VALUE_TYPE *r, enum machine_mode mode,
2153 unsigned HOST_WIDE_INT low, HOST_WIDE_INT high,
2154 int unsigned_p)
2156 if (low == 0 && high == 0)
2157 get_zero (r, 0);
2158 else
2160 memset (r, 0, sizeof (*r));
2161 r->cl = rvc_normal;
2162 r->sign = high < 0 && !unsigned_p;
2163 SET_REAL_EXP (r, 2 * HOST_BITS_PER_WIDE_INT);
2165 if (r->sign)
2167 high = ~high;
2168 if (low == 0)
2169 high += 1;
2170 else
2171 low = -low;
2174 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2176 r->sig[SIGSZ-1] = high;
2177 r->sig[SIGSZ-2] = low;
2179 else
2181 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2182 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
2183 r->sig[SIGSZ-2] = high;
2184 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
2185 r->sig[SIGSZ-4] = low;
2188 normalize (r);
2191 if (DECIMAL_FLOAT_MODE_P (mode))
2192 decimal_from_integer (r);
2193 else if (mode != VOIDmode)
2194 real_convert (r, mode, r);
2197 /* Render R, an integral value, as a floating point constant with no
2198 specified exponent. */
2200 static void
2201 decimal_integer_string (char *str, const REAL_VALUE_TYPE *r_orig,
2202 size_t buf_size)
2204 int dec_exp, digit, digits;
2205 REAL_VALUE_TYPE r, pten;
2206 char *p;
2207 bool sign;
2209 r = *r_orig;
2211 if (r.cl == rvc_zero)
2213 strcpy (str, "0.");
2214 return;
2217 sign = r.sign;
2218 r.sign = 0;
2220 dec_exp = REAL_EXP (&r) * M_LOG10_2;
2221 digits = dec_exp + 1;
2222 gcc_assert ((digits + 2) < (int)buf_size);
2224 pten = *real_digit (1);
2225 times_pten (&pten, dec_exp);
2227 p = str;
2228 if (sign)
2229 *p++ = '-';
2231 digit = rtd_divmod (&r, &pten);
2232 gcc_assert (digit >= 0 && digit <= 9);
2233 *p++ = digit + '0';
2234 while (--digits > 0)
2236 times_pten (&r, 1);
2237 digit = rtd_divmod (&r, &pten);
2238 *p++ = digit + '0';
2240 *p++ = '.';
2241 *p++ = '\0';
2244 /* Convert a real with an integral value to decimal float. */
2246 static void
2247 decimal_from_integer (REAL_VALUE_TYPE *r)
2249 char str[256];
2251 decimal_integer_string (str, r, sizeof (str) - 1);
2252 decimal_real_from_string (r, str);
2255 /* Returns 10**2**N. */
2257 static const REAL_VALUE_TYPE *
2258 ten_to_ptwo (int n)
2260 static REAL_VALUE_TYPE tens[EXP_BITS];
2262 gcc_assert (n >= 0);
2263 gcc_assert (n < EXP_BITS);
2265 if (tens[n].cl == rvc_zero)
2267 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2269 HOST_WIDE_INT t = 10;
2270 int i;
2272 for (i = 0; i < n; ++i)
2273 t *= t;
2275 real_from_integer (&tens[n], VOIDmode, t, 0, 1);
2277 else
2279 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2280 do_multiply (&tens[n], t, t);
2284 return &tens[n];
2287 /* Returns 10**(-2**N). */
2289 static const REAL_VALUE_TYPE *
2290 ten_to_mptwo (int n)
2292 static REAL_VALUE_TYPE tens[EXP_BITS];
2294 gcc_assert (n >= 0);
2295 gcc_assert (n < EXP_BITS);
2297 if (tens[n].cl == rvc_zero)
2298 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2300 return &tens[n];
2303 /* Returns N. */
2305 static const REAL_VALUE_TYPE *
2306 real_digit (int n)
2308 static REAL_VALUE_TYPE num[10];
2310 gcc_assert (n >= 0);
2311 gcc_assert (n <= 9);
2313 if (n > 0 && num[n].cl == rvc_zero)
2314 real_from_integer (&num[n], VOIDmode, n, 0, 1);
2316 return &num[n];
2319 /* Multiply R by 10**EXP. */
2321 static void
2322 times_pten (REAL_VALUE_TYPE *r, int exp)
2324 REAL_VALUE_TYPE pten, *rr;
2325 bool negative = (exp < 0);
2326 int i;
2328 if (negative)
2330 exp = -exp;
2331 pten = *real_digit (1);
2332 rr = &pten;
2334 else
2335 rr = r;
2337 for (i = 0; exp > 0; ++i, exp >>= 1)
2338 if (exp & 1)
2339 do_multiply (rr, rr, ten_to_ptwo (i));
2341 if (negative)
2342 do_divide (r, r, &pten);
2345 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2347 const REAL_VALUE_TYPE *
2348 dconst_e_ptr (void)
2350 static REAL_VALUE_TYPE value;
2352 /* Initialize mathematical constants for constant folding builtins.
2353 These constants need to be given to at least 160 bits precision. */
2354 if (value.cl == rvc_zero)
2356 mpfr_t m;
2357 mpfr_init2 (m, SIGNIFICAND_BITS);
2358 mpfr_set_ui (m, 1, GMP_RNDN);
2359 mpfr_exp (m, m, GMP_RNDN);
2360 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2361 mpfr_clear (m);
2364 return &value;
2367 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2369 const REAL_VALUE_TYPE *
2370 dconst_third_ptr (void)
2372 static REAL_VALUE_TYPE value;
2374 /* Initialize mathematical constants for constant folding builtins.
2375 These constants need to be given to at least 160 bits precision. */
2376 if (value.cl == rvc_zero)
2378 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (3));
2380 return &value;
2383 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2385 const REAL_VALUE_TYPE *
2386 dconst_sqrt2_ptr (void)
2388 static REAL_VALUE_TYPE value;
2390 /* Initialize mathematical constants for constant folding builtins.
2391 These constants need to be given to at least 160 bits precision. */
2392 if (value.cl == rvc_zero)
2394 mpfr_t m;
2395 mpfr_init2 (m, SIGNIFICAND_BITS);
2396 mpfr_sqrt_ui (m, 2, GMP_RNDN);
2397 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2398 mpfr_clear (m);
2400 return &value;
2403 /* Fills R with +Inf. */
2405 void
2406 real_inf (REAL_VALUE_TYPE *r)
2408 get_inf (r, 0);
2411 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2412 we force a QNaN, else we force an SNaN. The string, if not empty,
2413 is parsed as a number and placed in the significand. Return true
2414 if the string was successfully parsed. */
2416 bool
2417 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2418 enum machine_mode mode)
2420 const struct real_format *fmt;
2422 fmt = REAL_MODE_FORMAT (mode);
2423 gcc_assert (fmt);
2425 if (*str == 0)
2427 if (quiet)
2428 get_canonical_qnan (r, 0);
2429 else
2430 get_canonical_snan (r, 0);
2432 else
2434 int base = 10, d;
2436 memset (r, 0, sizeof (*r));
2437 r->cl = rvc_nan;
2439 /* Parse akin to strtol into the significand of R. */
2441 while (ISSPACE (*str))
2442 str++;
2443 if (*str == '-')
2444 str++;
2445 else if (*str == '+')
2446 str++;
2447 if (*str == '0')
2449 str++;
2450 if (*str == 'x' || *str == 'X')
2452 base = 16;
2453 str++;
2455 else
2456 base = 8;
2459 while ((d = hex_value (*str)) < base)
2461 REAL_VALUE_TYPE u;
2463 switch (base)
2465 case 8:
2466 lshift_significand (r, r, 3);
2467 break;
2468 case 16:
2469 lshift_significand (r, r, 4);
2470 break;
2471 case 10:
2472 lshift_significand_1 (&u, r);
2473 lshift_significand (r, r, 3);
2474 add_significands (r, r, &u);
2475 break;
2476 default:
2477 gcc_unreachable ();
2480 get_zero (&u, 0);
2481 u.sig[0] = d;
2482 add_significands (r, r, &u);
2484 str++;
2487 /* Must have consumed the entire string for success. */
2488 if (*str != 0)
2489 return false;
2491 /* Shift the significand into place such that the bits
2492 are in the most significant bits for the format. */
2493 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2495 /* Our MSB is always unset for NaNs. */
2496 r->sig[SIGSZ-1] &= ~SIG_MSB;
2498 /* Force quiet or signalling NaN. */
2499 r->signalling = !quiet;
2502 return true;
2505 /* Fills R with the largest finite value representable in mode MODE.
2506 If SIGN is nonzero, R is set to the most negative finite value. */
2508 void
2509 real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
2511 const struct real_format *fmt;
2512 int np2;
2514 fmt = REAL_MODE_FORMAT (mode);
2515 gcc_assert (fmt);
2516 memset (r, 0, sizeof (*r));
2518 if (fmt->b == 10)
2519 decimal_real_maxval (r, sign, mode);
2520 else
2522 r->cl = rvc_normal;
2523 r->sign = sign;
2524 SET_REAL_EXP (r, fmt->emax);
2526 np2 = SIGNIFICAND_BITS - fmt->p;
2527 memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2528 clear_significand_below (r, np2);
2530 if (fmt->pnan < fmt->p)
2531 /* This is an IBM extended double format made up of two IEEE
2532 doubles. The value of the long double is the sum of the
2533 values of the two parts. The most significant part is
2534 required to be the value of the long double rounded to the
2535 nearest double. Rounding means we need a slightly smaller
2536 value for LDBL_MAX. */
2537 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1);
2541 /* Fills R with 2**N. */
2543 void
2544 real_2expN (REAL_VALUE_TYPE *r, int n, enum machine_mode fmode)
2546 memset (r, 0, sizeof (*r));
2548 n++;
2549 if (n > MAX_EXP)
2550 r->cl = rvc_inf;
2551 else if (n < -MAX_EXP)
2553 else
2555 r->cl = rvc_normal;
2556 SET_REAL_EXP (r, n);
2557 r->sig[SIGSZ-1] = SIG_MSB;
2559 if (DECIMAL_FLOAT_MODE_P (fmode))
2560 decimal_real_convert (r, fmode, r);
2564 static void
2565 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2567 int p2, np2, i, w;
2568 int emin2m1, emax2;
2569 bool round_up = false;
2571 if (r->decimal)
2573 if (fmt->b == 10)
2575 decimal_round_for_format (fmt, r);
2576 return;
2578 /* FIXME. We can come here via fp_easy_constant
2579 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2580 investigated whether this convert needs to be here, or
2581 something else is missing. */
2582 decimal_real_convert (r, DFmode, r);
2585 p2 = fmt->p;
2586 emin2m1 = fmt->emin - 1;
2587 emax2 = fmt->emax;
2589 np2 = SIGNIFICAND_BITS - p2;
2590 switch (r->cl)
2592 underflow:
2593 get_zero (r, r->sign);
2594 case rvc_zero:
2595 if (!fmt->has_signed_zero)
2596 r->sign = 0;
2597 return;
2599 overflow:
2600 get_inf (r, r->sign);
2601 case rvc_inf:
2602 return;
2604 case rvc_nan:
2605 clear_significand_below (r, np2);
2606 return;
2608 case rvc_normal:
2609 break;
2611 default:
2612 gcc_unreachable ();
2615 /* Check the range of the exponent. If we're out of range,
2616 either underflow or overflow. */
2617 if (REAL_EXP (r) > emax2)
2618 goto overflow;
2619 else if (REAL_EXP (r) <= emin2m1)
2621 int diff;
2623 if (!fmt->has_denorm)
2625 /* Don't underflow completely until we've had a chance to round. */
2626 if (REAL_EXP (r) < emin2m1)
2627 goto underflow;
2629 else
2631 diff = emin2m1 - REAL_EXP (r) + 1;
2632 if (diff > p2)
2633 goto underflow;
2635 /* De-normalize the significand. */
2636 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2637 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2641 if (!fmt->round_towards_zero)
2643 /* There are P2 true significand bits, followed by one guard bit,
2644 followed by one sticky bit, followed by stuff. Fold nonzero
2645 stuff into the sticky bit. */
2646 unsigned long sticky;
2647 bool guard, lsb;
2649 sticky = 0;
2650 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2651 sticky |= r->sig[i];
2652 sticky |= r->sig[w]
2653 & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2655 guard = test_significand_bit (r, np2 - 1);
2656 lsb = test_significand_bit (r, np2);
2658 /* Round to even. */
2659 round_up = guard && (sticky || lsb);
2662 if (round_up)
2664 REAL_VALUE_TYPE u;
2665 get_zero (&u, 0);
2666 set_significand_bit (&u, np2);
2668 if (add_significands (r, r, &u))
2670 /* Overflow. Means the significand had been all ones, and
2671 is now all zeros. Need to increase the exponent, and
2672 possibly re-normalize it. */
2673 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2674 if (REAL_EXP (r) > emax2)
2675 goto overflow;
2676 r->sig[SIGSZ-1] = SIG_MSB;
2680 /* Catch underflow that we deferred until after rounding. */
2681 if (REAL_EXP (r) <= emin2m1)
2682 goto underflow;
2684 /* Clear out trailing garbage. */
2685 clear_significand_below (r, np2);
2688 /* Extend or truncate to a new mode. */
2690 void
2691 real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
2692 const REAL_VALUE_TYPE *a)
2694 const struct real_format *fmt;
2696 fmt = REAL_MODE_FORMAT (mode);
2697 gcc_assert (fmt);
2699 *r = *a;
2701 if (a->decimal || fmt->b == 10)
2702 decimal_real_convert (r, mode, a);
2704 round_for_format (fmt, r);
2706 /* round_for_format de-normalizes denormals. Undo just that part. */
2707 if (r->cl == rvc_normal)
2708 normalize (r);
2711 /* Legacy. Likewise, except return the struct directly. */
2713 REAL_VALUE_TYPE
2714 real_value_truncate (enum machine_mode mode, REAL_VALUE_TYPE a)
2716 REAL_VALUE_TYPE r;
2717 real_convert (&r, mode, &a);
2718 return r;
2721 /* Return true if truncating to MODE is exact. */
2723 bool
2724 exact_real_truncate (enum machine_mode mode, const REAL_VALUE_TYPE *a)
2726 const struct real_format *fmt;
2727 REAL_VALUE_TYPE t;
2728 int emin2m1;
2730 fmt = REAL_MODE_FORMAT (mode);
2731 gcc_assert (fmt);
2733 /* Don't allow conversion to denormals. */
2734 emin2m1 = fmt->emin - 1;
2735 if (REAL_EXP (a) <= emin2m1)
2736 return false;
2738 /* After conversion to the new mode, the value must be identical. */
2739 real_convert (&t, mode, a);
2740 return real_identical (&t, a);
2743 /* Write R to the given target format. Place the words of the result
2744 in target word order in BUF. There are always 32 bits in each
2745 long, no matter the size of the host long.
2747 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2749 long
2750 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
2751 const struct real_format *fmt)
2753 REAL_VALUE_TYPE r;
2754 long buf1;
2756 r = *r_orig;
2757 round_for_format (fmt, &r);
2759 if (!buf)
2760 buf = &buf1;
2761 (*fmt->encode) (fmt, buf, &r);
2763 return *buf;
2766 /* Similar, but look up the format from MODE. */
2768 long
2769 real_to_target (long *buf, const REAL_VALUE_TYPE *r, enum machine_mode mode)
2771 const struct real_format *fmt;
2773 fmt = REAL_MODE_FORMAT (mode);
2774 gcc_assert (fmt);
2776 return real_to_target_fmt (buf, r, fmt);
2779 /* Read R from the given target format. Read the words of the result
2780 in target word order in BUF. There are always 32 bits in each
2781 long, no matter the size of the host long. */
2783 void
2784 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
2785 const struct real_format *fmt)
2787 (*fmt->decode) (fmt, r, buf);
2790 /* Similar, but look up the format from MODE. */
2792 void
2793 real_from_target (REAL_VALUE_TYPE *r, const long *buf, enum machine_mode mode)
2795 const struct real_format *fmt;
2797 fmt = REAL_MODE_FORMAT (mode);
2798 gcc_assert (fmt);
2800 (*fmt->decode) (fmt, r, buf);
2803 /* Return the number of bits of the largest binary value that the
2804 significand of MODE will hold. */
2805 /* ??? Legacy. Should get access to real_format directly. */
2808 significand_size (enum machine_mode mode)
2810 const struct real_format *fmt;
2812 fmt = REAL_MODE_FORMAT (mode);
2813 if (fmt == NULL)
2814 return 0;
2816 if (fmt->b == 10)
2818 /* Return the size in bits of the largest binary value that can be
2819 held by the decimal coefficient for this mode. This is one more
2820 than the number of bits required to hold the largest coefficient
2821 of this mode. */
2822 double log2_10 = 3.3219281;
2823 return fmt->p * log2_10;
2825 return fmt->p;
2828 /* Return a hash value for the given real value. */
2829 /* ??? The "unsigned int" return value is intended to be hashval_t,
2830 but I didn't want to pull hashtab.h into real.h. */
2832 unsigned int
2833 real_hash (const REAL_VALUE_TYPE *r)
2835 unsigned int h;
2836 size_t i;
2838 h = r->cl | (r->sign << 2);
2839 switch (r->cl)
2841 case rvc_zero:
2842 case rvc_inf:
2843 return h;
2845 case rvc_normal:
2846 h |= REAL_EXP (r) << 3;
2847 break;
2849 case rvc_nan:
2850 if (r->signalling)
2851 h ^= (unsigned int)-1;
2852 if (r->canonical)
2853 return h;
2854 break;
2856 default:
2857 gcc_unreachable ();
2860 if (sizeof(unsigned long) > sizeof(unsigned int))
2861 for (i = 0; i < SIGSZ; ++i)
2863 unsigned long s = r->sig[i];
2864 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2866 else
2867 for (i = 0; i < SIGSZ; ++i)
2868 h ^= r->sig[i];
2870 return h;
2873 /* IEEE single-precision format. */
2875 static void encode_ieee_single (const struct real_format *fmt,
2876 long *, const REAL_VALUE_TYPE *);
2877 static void decode_ieee_single (const struct real_format *,
2878 REAL_VALUE_TYPE *, const long *);
2880 static void
2881 encode_ieee_single (const struct real_format *fmt, long *buf,
2882 const REAL_VALUE_TYPE *r)
2884 unsigned long image, sig, exp;
2885 unsigned long sign = r->sign;
2886 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2888 image = sign << 31;
2889 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2891 switch (r->cl)
2893 case rvc_zero:
2894 break;
2896 case rvc_inf:
2897 if (fmt->has_inf)
2898 image |= 255 << 23;
2899 else
2900 image |= 0x7fffffff;
2901 break;
2903 case rvc_nan:
2904 if (fmt->has_nans)
2906 if (r->canonical)
2907 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
2908 if (r->signalling == fmt->qnan_msb_set)
2909 sig &= ~(1 << 22);
2910 else
2911 sig |= 1 << 22;
2912 if (sig == 0)
2913 sig = 1 << 21;
2915 image |= 255 << 23;
2916 image |= sig;
2918 else
2919 image |= 0x7fffffff;
2920 break;
2922 case rvc_normal:
2923 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2924 whereas the intermediate representation is 0.F x 2**exp.
2925 Which means we're off by one. */
2926 if (denormal)
2927 exp = 0;
2928 else
2929 exp = REAL_EXP (r) + 127 - 1;
2930 image |= exp << 23;
2931 image |= sig;
2932 break;
2934 default:
2935 gcc_unreachable ();
2938 buf[0] = image;
2941 static void
2942 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2943 const long *buf)
2945 unsigned long image = buf[0] & 0xffffffff;
2946 bool sign = (image >> 31) & 1;
2947 int exp = (image >> 23) & 0xff;
2949 memset (r, 0, sizeof (*r));
2950 image <<= HOST_BITS_PER_LONG - 24;
2951 image &= ~SIG_MSB;
2953 if (exp == 0)
2955 if (image && fmt->has_denorm)
2957 r->cl = rvc_normal;
2958 r->sign = sign;
2959 SET_REAL_EXP (r, -126);
2960 r->sig[SIGSZ-1] = image << 1;
2961 normalize (r);
2963 else if (fmt->has_signed_zero)
2964 r->sign = sign;
2966 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2968 if (image)
2970 r->cl = rvc_nan;
2971 r->sign = sign;
2972 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
2973 ^ fmt->qnan_msb_set);
2974 r->sig[SIGSZ-1] = image;
2976 else
2978 r->cl = rvc_inf;
2979 r->sign = sign;
2982 else
2984 r->cl = rvc_normal;
2985 r->sign = sign;
2986 SET_REAL_EXP (r, exp - 127 + 1);
2987 r->sig[SIGSZ-1] = image | SIG_MSB;
2991 const struct real_format ieee_single_format =
2993 encode_ieee_single,
2994 decode_ieee_single,
2998 -125,
2999 128,
3002 false,
3003 true,
3004 true,
3005 true,
3006 true,
3007 true,
3008 true,
3009 false
3012 const struct real_format mips_single_format =
3014 encode_ieee_single,
3015 decode_ieee_single,
3019 -125,
3020 128,
3023 false,
3024 true,
3025 true,
3026 true,
3027 true,
3028 true,
3029 false,
3030 true
3033 const struct real_format motorola_single_format =
3035 encode_ieee_single,
3036 decode_ieee_single,
3040 -125,
3041 128,
3044 false,
3045 true,
3046 true,
3047 true,
3048 true,
3049 true,
3050 true,
3051 true
3054 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3055 single precision with the following differences:
3056 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3057 are generated.
3058 - NaNs are not supported.
3059 - The range of non-zero numbers in binary is
3060 (001)[1.]000...000 to (255)[1.]111...111.
3061 - Denormals can be represented, but are treated as +0.0 when
3062 used as an operand and are never generated as a result.
3063 - -0.0 can be represented, but a zero result is always +0.0.
3064 - the only supported rounding mode is trunction (towards zero). */
3065 const struct real_format spu_single_format =
3067 encode_ieee_single,
3068 decode_ieee_single,
3072 -125,
3073 129,
3076 true,
3077 false,
3078 false,
3079 false,
3080 true,
3081 true,
3082 false,
3083 false
3086 /* IEEE double-precision format. */
3088 static void encode_ieee_double (const struct real_format *fmt,
3089 long *, const REAL_VALUE_TYPE *);
3090 static void decode_ieee_double (const struct real_format *,
3091 REAL_VALUE_TYPE *, const long *);
3093 static void
3094 encode_ieee_double (const struct real_format *fmt, long *buf,
3095 const REAL_VALUE_TYPE *r)
3097 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
3098 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3100 image_hi = r->sign << 31;
3101 image_lo = 0;
3103 if (HOST_BITS_PER_LONG == 64)
3105 sig_hi = r->sig[SIGSZ-1];
3106 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
3107 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
3109 else
3111 sig_hi = r->sig[SIGSZ-1];
3112 sig_lo = r->sig[SIGSZ-2];
3113 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
3114 sig_hi = (sig_hi >> 11) & 0xfffff;
3117 switch (r->cl)
3119 case rvc_zero:
3120 break;
3122 case rvc_inf:
3123 if (fmt->has_inf)
3124 image_hi |= 2047 << 20;
3125 else
3127 image_hi |= 0x7fffffff;
3128 image_lo = 0xffffffff;
3130 break;
3132 case rvc_nan:
3133 if (fmt->has_nans)
3135 if (r->canonical)
3137 if (fmt->canonical_nan_lsbs_set)
3139 sig_hi = (1 << 19) - 1;
3140 sig_lo = 0xffffffff;
3142 else
3144 sig_hi = 0;
3145 sig_lo = 0;
3148 if (r->signalling == fmt->qnan_msb_set)
3149 sig_hi &= ~(1 << 19);
3150 else
3151 sig_hi |= 1 << 19;
3152 if (sig_hi == 0 && sig_lo == 0)
3153 sig_hi = 1 << 18;
3155 image_hi |= 2047 << 20;
3156 image_hi |= sig_hi;
3157 image_lo = sig_lo;
3159 else
3161 image_hi |= 0x7fffffff;
3162 image_lo = 0xffffffff;
3164 break;
3166 case rvc_normal:
3167 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3168 whereas the intermediate representation is 0.F x 2**exp.
3169 Which means we're off by one. */
3170 if (denormal)
3171 exp = 0;
3172 else
3173 exp = REAL_EXP (r) + 1023 - 1;
3174 image_hi |= exp << 20;
3175 image_hi |= sig_hi;
3176 image_lo = sig_lo;
3177 break;
3179 default:
3180 gcc_unreachable ();
3183 if (FLOAT_WORDS_BIG_ENDIAN)
3184 buf[0] = image_hi, buf[1] = image_lo;
3185 else
3186 buf[0] = image_lo, buf[1] = image_hi;
3189 static void
3190 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3191 const long *buf)
3193 unsigned long image_hi, image_lo;
3194 bool sign;
3195 int exp;
3197 if (FLOAT_WORDS_BIG_ENDIAN)
3198 image_hi = buf[0], image_lo = buf[1];
3199 else
3200 image_lo = buf[0], image_hi = buf[1];
3201 image_lo &= 0xffffffff;
3202 image_hi &= 0xffffffff;
3204 sign = (image_hi >> 31) & 1;
3205 exp = (image_hi >> 20) & 0x7ff;
3207 memset (r, 0, sizeof (*r));
3209 image_hi <<= 32 - 21;
3210 image_hi |= image_lo >> 21;
3211 image_hi &= 0x7fffffff;
3212 image_lo <<= 32 - 21;
3214 if (exp == 0)
3216 if ((image_hi || image_lo) && fmt->has_denorm)
3218 r->cl = rvc_normal;
3219 r->sign = sign;
3220 SET_REAL_EXP (r, -1022);
3221 if (HOST_BITS_PER_LONG == 32)
3223 image_hi = (image_hi << 1) | (image_lo >> 31);
3224 image_lo <<= 1;
3225 r->sig[SIGSZ-1] = image_hi;
3226 r->sig[SIGSZ-2] = image_lo;
3228 else
3230 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
3231 r->sig[SIGSZ-1] = image_hi;
3233 normalize (r);
3235 else if (fmt->has_signed_zero)
3236 r->sign = sign;
3238 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
3240 if (image_hi || image_lo)
3242 r->cl = rvc_nan;
3243 r->sign = sign;
3244 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3245 if (HOST_BITS_PER_LONG == 32)
3247 r->sig[SIGSZ-1] = image_hi;
3248 r->sig[SIGSZ-2] = image_lo;
3250 else
3251 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3253 else
3255 r->cl = rvc_inf;
3256 r->sign = sign;
3259 else
3261 r->cl = rvc_normal;
3262 r->sign = sign;
3263 SET_REAL_EXP (r, exp - 1023 + 1);
3264 if (HOST_BITS_PER_LONG == 32)
3266 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3267 r->sig[SIGSZ-2] = image_lo;
3269 else
3270 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3274 const struct real_format ieee_double_format =
3276 encode_ieee_double,
3277 decode_ieee_double,
3281 -1021,
3282 1024,
3285 false,
3286 true,
3287 true,
3288 true,
3289 true,
3290 true,
3291 true,
3292 false
3295 const struct real_format mips_double_format =
3297 encode_ieee_double,
3298 decode_ieee_double,
3302 -1021,
3303 1024,
3306 false,
3307 true,
3308 true,
3309 true,
3310 true,
3311 true,
3312 false,
3313 true
3316 const struct real_format motorola_double_format =
3318 encode_ieee_double,
3319 decode_ieee_double,
3323 -1021,
3324 1024,
3327 false,
3328 true,
3329 true,
3330 true,
3331 true,
3332 true,
3333 true,
3334 true
3337 /* IEEE extended real format. This comes in three flavors: Intel's as
3338 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3339 12- and 16-byte images may be big- or little endian; Motorola's is
3340 always big endian. */
3342 /* Helper subroutine which converts from the internal format to the
3343 12-byte little-endian Intel format. Functions below adjust this
3344 for the other possible formats. */
3345 static void
3346 encode_ieee_extended (const struct real_format *fmt, long *buf,
3347 const REAL_VALUE_TYPE *r)
3349 unsigned long image_hi, sig_hi, sig_lo;
3350 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3352 image_hi = r->sign << 15;
3353 sig_hi = sig_lo = 0;
3355 switch (r->cl)
3357 case rvc_zero:
3358 break;
3360 case rvc_inf:
3361 if (fmt->has_inf)
3363 image_hi |= 32767;
3365 /* Intel requires the explicit integer bit to be set, otherwise
3366 it considers the value a "pseudo-infinity". Motorola docs
3367 say it doesn't care. */
3368 sig_hi = 0x80000000;
3370 else
3372 image_hi |= 32767;
3373 sig_lo = sig_hi = 0xffffffff;
3375 break;
3377 case rvc_nan:
3378 if (fmt->has_nans)
3380 image_hi |= 32767;
3381 if (r->canonical)
3383 if (fmt->canonical_nan_lsbs_set)
3385 sig_hi = (1 << 30) - 1;
3386 sig_lo = 0xffffffff;
3389 else if (HOST_BITS_PER_LONG == 32)
3391 sig_hi = r->sig[SIGSZ-1];
3392 sig_lo = r->sig[SIGSZ-2];
3394 else
3396 sig_lo = r->sig[SIGSZ-1];
3397 sig_hi = sig_lo >> 31 >> 1;
3398 sig_lo &= 0xffffffff;
3400 if (r->signalling == fmt->qnan_msb_set)
3401 sig_hi &= ~(1 << 30);
3402 else
3403 sig_hi |= 1 << 30;
3404 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3405 sig_hi = 1 << 29;
3407 /* Intel requires the explicit integer bit to be set, otherwise
3408 it considers the value a "pseudo-nan". Motorola docs say it
3409 doesn't care. */
3410 sig_hi |= 0x80000000;
3412 else
3414 image_hi |= 32767;
3415 sig_lo = sig_hi = 0xffffffff;
3417 break;
3419 case rvc_normal:
3421 int exp = REAL_EXP (r);
3423 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3424 whereas the intermediate representation is 0.F x 2**exp.
3425 Which means we're off by one.
3427 Except for Motorola, which consider exp=0 and explicit
3428 integer bit set to continue to be normalized. In theory
3429 this discrepancy has been taken care of by the difference
3430 in fmt->emin in round_for_format. */
3432 if (denormal)
3433 exp = 0;
3434 else
3436 exp += 16383 - 1;
3437 gcc_assert (exp >= 0);
3439 image_hi |= exp;
3441 if (HOST_BITS_PER_LONG == 32)
3443 sig_hi = r->sig[SIGSZ-1];
3444 sig_lo = r->sig[SIGSZ-2];
3446 else
3448 sig_lo = r->sig[SIGSZ-1];
3449 sig_hi = sig_lo >> 31 >> 1;
3450 sig_lo &= 0xffffffff;
3453 break;
3455 default:
3456 gcc_unreachable ();
3459 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3462 /* Convert from the internal format to the 12-byte Motorola format
3463 for an IEEE extended real. */
3464 static void
3465 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3466 const REAL_VALUE_TYPE *r)
3468 long intermed[3];
3469 encode_ieee_extended (fmt, intermed, r);
3471 /* Motorola chips are assumed always to be big-endian. Also, the
3472 padding in a Motorola extended real goes between the exponent and
3473 the mantissa. At this point the mantissa is entirely within
3474 elements 0 and 1 of intermed, and the exponent entirely within
3475 element 2, so all we have to do is swap the order around, and
3476 shift element 2 left 16 bits. */
3477 buf[0] = intermed[2] << 16;
3478 buf[1] = intermed[1];
3479 buf[2] = intermed[0];
3482 /* Convert from the internal format to the 12-byte Intel format for
3483 an IEEE extended real. */
3484 static void
3485 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3486 const REAL_VALUE_TYPE *r)
3488 if (FLOAT_WORDS_BIG_ENDIAN)
3490 /* All the padding in an Intel-format extended real goes at the high
3491 end, which in this case is after the mantissa, not the exponent.
3492 Therefore we must shift everything down 16 bits. */
3493 long intermed[3];
3494 encode_ieee_extended (fmt, intermed, r);
3495 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3496 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3497 buf[2] = (intermed[0] << 16);
3499 else
3500 /* encode_ieee_extended produces what we want directly. */
3501 encode_ieee_extended (fmt, buf, r);
3504 /* Convert from the internal format to the 16-byte Intel format for
3505 an IEEE extended real. */
3506 static void
3507 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3508 const REAL_VALUE_TYPE *r)
3510 /* All the padding in an Intel-format extended real goes at the high end. */
3511 encode_ieee_extended_intel_96 (fmt, buf, r);
3512 buf[3] = 0;
3515 /* As above, we have a helper function which converts from 12-byte
3516 little-endian Intel format to internal format. Functions below
3517 adjust for the other possible formats. */
3518 static void
3519 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3520 const long *buf)
3522 unsigned long image_hi, sig_hi, sig_lo;
3523 bool sign;
3524 int exp;
3526 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3527 sig_lo &= 0xffffffff;
3528 sig_hi &= 0xffffffff;
3529 image_hi &= 0xffffffff;
3531 sign = (image_hi >> 15) & 1;
3532 exp = image_hi & 0x7fff;
3534 memset (r, 0, sizeof (*r));
3536 if (exp == 0)
3538 if ((sig_hi || sig_lo) && fmt->has_denorm)
3540 r->cl = rvc_normal;
3541 r->sign = sign;
3543 /* When the IEEE format contains a hidden bit, we know that
3544 it's zero at this point, and so shift up the significand
3545 and decrease the exponent to match. In this case, Motorola
3546 defines the explicit integer bit to be valid, so we don't
3547 know whether the msb is set or not. */
3548 SET_REAL_EXP (r, fmt->emin);
3549 if (HOST_BITS_PER_LONG == 32)
3551 r->sig[SIGSZ-1] = sig_hi;
3552 r->sig[SIGSZ-2] = sig_lo;
3554 else
3555 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3557 normalize (r);
3559 else if (fmt->has_signed_zero)
3560 r->sign = sign;
3562 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3564 /* See above re "pseudo-infinities" and "pseudo-nans".
3565 Short summary is that the MSB will likely always be
3566 set, and that we don't care about it. */
3567 sig_hi &= 0x7fffffff;
3569 if (sig_hi || sig_lo)
3571 r->cl = rvc_nan;
3572 r->sign = sign;
3573 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3574 if (HOST_BITS_PER_LONG == 32)
3576 r->sig[SIGSZ-1] = sig_hi;
3577 r->sig[SIGSZ-2] = sig_lo;
3579 else
3580 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3582 else
3584 r->cl = rvc_inf;
3585 r->sign = sign;
3588 else
3590 r->cl = rvc_normal;
3591 r->sign = sign;
3592 SET_REAL_EXP (r, exp - 16383 + 1);
3593 if (HOST_BITS_PER_LONG == 32)
3595 r->sig[SIGSZ-1] = sig_hi;
3596 r->sig[SIGSZ-2] = sig_lo;
3598 else
3599 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3603 /* Convert from the internal format to the 12-byte Motorola format
3604 for an IEEE extended real. */
3605 static void
3606 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3607 const long *buf)
3609 long intermed[3];
3611 /* Motorola chips are assumed always to be big-endian. Also, the
3612 padding in a Motorola extended real goes between the exponent and
3613 the mantissa; remove it. */
3614 intermed[0] = buf[2];
3615 intermed[1] = buf[1];
3616 intermed[2] = (unsigned long)buf[0] >> 16;
3618 decode_ieee_extended (fmt, r, intermed);
3621 /* Convert from the internal format to the 12-byte Intel format for
3622 an IEEE extended real. */
3623 static void
3624 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3625 const long *buf)
3627 if (FLOAT_WORDS_BIG_ENDIAN)
3629 /* All the padding in an Intel-format extended real goes at the high
3630 end, which in this case is after the mantissa, not the exponent.
3631 Therefore we must shift everything up 16 bits. */
3632 long intermed[3];
3634 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3635 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3636 intermed[2] = ((unsigned long)buf[0] >> 16);
3638 decode_ieee_extended (fmt, r, intermed);
3640 else
3641 /* decode_ieee_extended produces what we want directly. */
3642 decode_ieee_extended (fmt, r, buf);
3645 /* Convert from the internal format to the 16-byte Intel format for
3646 an IEEE extended real. */
3647 static void
3648 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3649 const long *buf)
3651 /* All the padding in an Intel-format extended real goes at the high end. */
3652 decode_ieee_extended_intel_96 (fmt, r, buf);
3655 const struct real_format ieee_extended_motorola_format =
3657 encode_ieee_extended_motorola,
3658 decode_ieee_extended_motorola,
3662 -16382,
3663 16384,
3666 false,
3667 true,
3668 true,
3669 true,
3670 true,
3671 true,
3672 true,
3673 true
3676 const struct real_format ieee_extended_intel_96_format =
3678 encode_ieee_extended_intel_96,
3679 decode_ieee_extended_intel_96,
3683 -16381,
3684 16384,
3687 false,
3688 true,
3689 true,
3690 true,
3691 true,
3692 true,
3693 true,
3694 false
3697 const struct real_format ieee_extended_intel_128_format =
3699 encode_ieee_extended_intel_128,
3700 decode_ieee_extended_intel_128,
3704 -16381,
3705 16384,
3708 false,
3709 true,
3710 true,
3711 true,
3712 true,
3713 true,
3714 true,
3715 false
3718 /* The following caters to i386 systems that set the rounding precision
3719 to 53 bits instead of 64, e.g. FreeBSD. */
3720 const struct real_format ieee_extended_intel_96_round_53_format =
3722 encode_ieee_extended_intel_96,
3723 decode_ieee_extended_intel_96,
3727 -16381,
3728 16384,
3731 false,
3732 true,
3733 true,
3734 true,
3735 true,
3736 true,
3737 true,
3738 false
3741 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3742 numbers whose sum is equal to the extended precision value. The number
3743 with greater magnitude is first. This format has the same magnitude
3744 range as an IEEE double precision value, but effectively 106 bits of
3745 significand precision. Infinity and NaN are represented by their IEEE
3746 double precision value stored in the first number, the second number is
3747 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3749 static void encode_ibm_extended (const struct real_format *fmt,
3750 long *, const REAL_VALUE_TYPE *);
3751 static void decode_ibm_extended (const struct real_format *,
3752 REAL_VALUE_TYPE *, const long *);
3754 static void
3755 encode_ibm_extended (const struct real_format *fmt, long *buf,
3756 const REAL_VALUE_TYPE *r)
3758 REAL_VALUE_TYPE u, normr, v;
3759 const struct real_format *base_fmt;
3761 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3763 /* Renormalize R before doing any arithmetic on it. */
3764 normr = *r;
3765 if (normr.cl == rvc_normal)
3766 normalize (&normr);
3768 /* u = IEEE double precision portion of significand. */
3769 u = normr;
3770 round_for_format (base_fmt, &u);
3771 encode_ieee_double (base_fmt, &buf[0], &u);
3773 if (u.cl == rvc_normal)
3775 do_add (&v, &normr, &u, 1);
3776 /* Call round_for_format since we might need to denormalize. */
3777 round_for_format (base_fmt, &v);
3778 encode_ieee_double (base_fmt, &buf[2], &v);
3780 else
3782 /* Inf, NaN, 0 are all representable as doubles, so the
3783 least-significant part can be 0.0. */
3784 buf[2] = 0;
3785 buf[3] = 0;
3789 static void
3790 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3791 const long *buf)
3793 REAL_VALUE_TYPE u, v;
3794 const struct real_format *base_fmt;
3796 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3797 decode_ieee_double (base_fmt, &u, &buf[0]);
3799 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3801 decode_ieee_double (base_fmt, &v, &buf[2]);
3802 do_add (r, &u, &v, 0);
3804 else
3805 *r = u;
3808 const struct real_format ibm_extended_format =
3810 encode_ibm_extended,
3811 decode_ibm_extended,
3813 53 + 53,
3815 -1021 + 53,
3816 1024,
3817 127,
3819 false,
3820 true,
3821 true,
3822 true,
3823 true,
3824 true,
3825 true,
3826 false
3829 const struct real_format mips_extended_format =
3831 encode_ibm_extended,
3832 decode_ibm_extended,
3834 53 + 53,
3836 -1021 + 53,
3837 1024,
3838 127,
3840 false,
3841 true,
3842 true,
3843 true,
3844 true,
3845 true,
3846 false,
3847 true
3851 /* IEEE quad precision format. */
3853 static void encode_ieee_quad (const struct real_format *fmt,
3854 long *, const REAL_VALUE_TYPE *);
3855 static void decode_ieee_quad (const struct real_format *,
3856 REAL_VALUE_TYPE *, const long *);
3858 static void
3859 encode_ieee_quad (const struct real_format *fmt, long *buf,
3860 const REAL_VALUE_TYPE *r)
3862 unsigned long image3, image2, image1, image0, exp;
3863 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3864 REAL_VALUE_TYPE u;
3866 image3 = r->sign << 31;
3867 image2 = 0;
3868 image1 = 0;
3869 image0 = 0;
3871 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3873 switch (r->cl)
3875 case rvc_zero:
3876 break;
3878 case rvc_inf:
3879 if (fmt->has_inf)
3880 image3 |= 32767 << 16;
3881 else
3883 image3 |= 0x7fffffff;
3884 image2 = 0xffffffff;
3885 image1 = 0xffffffff;
3886 image0 = 0xffffffff;
3888 break;
3890 case rvc_nan:
3891 if (fmt->has_nans)
3893 image3 |= 32767 << 16;
3895 if (r->canonical)
3897 if (fmt->canonical_nan_lsbs_set)
3899 image3 |= 0x7fff;
3900 image2 = image1 = image0 = 0xffffffff;
3903 else if (HOST_BITS_PER_LONG == 32)
3905 image0 = u.sig[0];
3906 image1 = u.sig[1];
3907 image2 = u.sig[2];
3908 image3 |= u.sig[3] & 0xffff;
3910 else
3912 image0 = u.sig[0];
3913 image1 = image0 >> 31 >> 1;
3914 image2 = u.sig[1];
3915 image3 |= (image2 >> 31 >> 1) & 0xffff;
3916 image0 &= 0xffffffff;
3917 image2 &= 0xffffffff;
3919 if (r->signalling == fmt->qnan_msb_set)
3920 image3 &= ~0x8000;
3921 else
3922 image3 |= 0x8000;
3923 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3924 image3 |= 0x4000;
3926 else
3928 image3 |= 0x7fffffff;
3929 image2 = 0xffffffff;
3930 image1 = 0xffffffff;
3931 image0 = 0xffffffff;
3933 break;
3935 case rvc_normal:
3936 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3937 whereas the intermediate representation is 0.F x 2**exp.
3938 Which means we're off by one. */
3939 if (denormal)
3940 exp = 0;
3941 else
3942 exp = REAL_EXP (r) + 16383 - 1;
3943 image3 |= exp << 16;
3945 if (HOST_BITS_PER_LONG == 32)
3947 image0 = u.sig[0];
3948 image1 = u.sig[1];
3949 image2 = u.sig[2];
3950 image3 |= u.sig[3] & 0xffff;
3952 else
3954 image0 = u.sig[0];
3955 image1 = image0 >> 31 >> 1;
3956 image2 = u.sig[1];
3957 image3 |= (image2 >> 31 >> 1) & 0xffff;
3958 image0 &= 0xffffffff;
3959 image2 &= 0xffffffff;
3961 break;
3963 default:
3964 gcc_unreachable ();
3967 if (FLOAT_WORDS_BIG_ENDIAN)
3969 buf[0] = image3;
3970 buf[1] = image2;
3971 buf[2] = image1;
3972 buf[3] = image0;
3974 else
3976 buf[0] = image0;
3977 buf[1] = image1;
3978 buf[2] = image2;
3979 buf[3] = image3;
3983 static void
3984 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3985 const long *buf)
3987 unsigned long image3, image2, image1, image0;
3988 bool sign;
3989 int exp;
3991 if (FLOAT_WORDS_BIG_ENDIAN)
3993 image3 = buf[0];
3994 image2 = buf[1];
3995 image1 = buf[2];
3996 image0 = buf[3];
3998 else
4000 image0 = buf[0];
4001 image1 = buf[1];
4002 image2 = buf[2];
4003 image3 = buf[3];
4005 image0 &= 0xffffffff;
4006 image1 &= 0xffffffff;
4007 image2 &= 0xffffffff;
4009 sign = (image3 >> 31) & 1;
4010 exp = (image3 >> 16) & 0x7fff;
4011 image3 &= 0xffff;
4013 memset (r, 0, sizeof (*r));
4015 if (exp == 0)
4017 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
4019 r->cl = rvc_normal;
4020 r->sign = sign;
4022 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
4023 if (HOST_BITS_PER_LONG == 32)
4025 r->sig[0] = image0;
4026 r->sig[1] = image1;
4027 r->sig[2] = image2;
4028 r->sig[3] = image3;
4030 else
4032 r->sig[0] = (image1 << 31 << 1) | image0;
4033 r->sig[1] = (image3 << 31 << 1) | image2;
4036 normalize (r);
4038 else if (fmt->has_signed_zero)
4039 r->sign = sign;
4041 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
4043 if (image3 | image2 | image1 | image0)
4045 r->cl = rvc_nan;
4046 r->sign = sign;
4047 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
4049 if (HOST_BITS_PER_LONG == 32)
4051 r->sig[0] = image0;
4052 r->sig[1] = image1;
4053 r->sig[2] = image2;
4054 r->sig[3] = image3;
4056 else
4058 r->sig[0] = (image1 << 31 << 1) | image0;
4059 r->sig[1] = (image3 << 31 << 1) | image2;
4061 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4063 else
4065 r->cl = rvc_inf;
4066 r->sign = sign;
4069 else
4071 r->cl = rvc_normal;
4072 r->sign = sign;
4073 SET_REAL_EXP (r, exp - 16383 + 1);
4075 if (HOST_BITS_PER_LONG == 32)
4077 r->sig[0] = image0;
4078 r->sig[1] = image1;
4079 r->sig[2] = image2;
4080 r->sig[3] = image3;
4082 else
4084 r->sig[0] = (image1 << 31 << 1) | image0;
4085 r->sig[1] = (image3 << 31 << 1) | image2;
4087 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4088 r->sig[SIGSZ-1] |= SIG_MSB;
4092 const struct real_format ieee_quad_format =
4094 encode_ieee_quad,
4095 decode_ieee_quad,
4097 113,
4098 113,
4099 -16381,
4100 16384,
4101 127,
4102 127,
4103 false,
4104 true,
4105 true,
4106 true,
4107 true,
4108 true,
4109 true,
4110 false
4113 const struct real_format mips_quad_format =
4115 encode_ieee_quad,
4116 decode_ieee_quad,
4118 113,
4119 113,
4120 -16381,
4121 16384,
4122 127,
4123 127,
4124 false,
4125 true,
4126 true,
4127 true,
4128 true,
4129 true,
4130 false,
4131 true
4134 /* Descriptions of VAX floating point formats can be found beginning at
4136 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4138 The thing to remember is that they're almost IEEE, except for word
4139 order, exponent bias, and the lack of infinities, nans, and denormals.
4141 We don't implement the H_floating format here, simply because neither
4142 the VAX or Alpha ports use it. */
4144 static void encode_vax_f (const struct real_format *fmt,
4145 long *, const REAL_VALUE_TYPE *);
4146 static void decode_vax_f (const struct real_format *,
4147 REAL_VALUE_TYPE *, const long *);
4148 static void encode_vax_d (const struct real_format *fmt,
4149 long *, const REAL_VALUE_TYPE *);
4150 static void decode_vax_d (const struct real_format *,
4151 REAL_VALUE_TYPE *, const long *);
4152 static void encode_vax_g (const struct real_format *fmt,
4153 long *, const REAL_VALUE_TYPE *);
4154 static void decode_vax_g (const struct real_format *,
4155 REAL_VALUE_TYPE *, const long *);
4157 static void
4158 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4159 const REAL_VALUE_TYPE *r)
4161 unsigned long sign, exp, sig, image;
4163 sign = r->sign << 15;
4165 switch (r->cl)
4167 case rvc_zero:
4168 image = 0;
4169 break;
4171 case rvc_inf:
4172 case rvc_nan:
4173 image = 0xffff7fff | sign;
4174 break;
4176 case rvc_normal:
4177 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4178 exp = REAL_EXP (r) + 128;
4180 image = (sig << 16) & 0xffff0000;
4181 image |= sign;
4182 image |= exp << 7;
4183 image |= sig >> 16;
4184 break;
4186 default:
4187 gcc_unreachable ();
4190 buf[0] = image;
4193 static void
4194 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
4195 REAL_VALUE_TYPE *r, const long *buf)
4197 unsigned long image = buf[0] & 0xffffffff;
4198 int exp = (image >> 7) & 0xff;
4200 memset (r, 0, sizeof (*r));
4202 if (exp != 0)
4204 r->cl = rvc_normal;
4205 r->sign = (image >> 15) & 1;
4206 SET_REAL_EXP (r, exp - 128);
4208 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
4209 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4213 static void
4214 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4215 const REAL_VALUE_TYPE *r)
4217 unsigned long image0, image1, sign = r->sign << 15;
4219 switch (r->cl)
4221 case rvc_zero:
4222 image0 = image1 = 0;
4223 break;
4225 case rvc_inf:
4226 case rvc_nan:
4227 image0 = 0xffff7fff | sign;
4228 image1 = 0xffffffff;
4229 break;
4231 case rvc_normal:
4232 /* Extract the significand into straight hi:lo. */
4233 if (HOST_BITS_PER_LONG == 64)
4235 image0 = r->sig[SIGSZ-1];
4236 image1 = (image0 >> (64 - 56)) & 0xffffffff;
4237 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
4239 else
4241 image0 = r->sig[SIGSZ-1];
4242 image1 = r->sig[SIGSZ-2];
4243 image1 = (image0 << 24) | (image1 >> 8);
4244 image0 = (image0 >> 8) & 0xffffff;
4247 /* Rearrange the half-words of the significand to match the
4248 external format. */
4249 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
4250 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4252 /* Add the sign and exponent. */
4253 image0 |= sign;
4254 image0 |= (REAL_EXP (r) + 128) << 7;
4255 break;
4257 default:
4258 gcc_unreachable ();
4261 if (FLOAT_WORDS_BIG_ENDIAN)
4262 buf[0] = image1, buf[1] = image0;
4263 else
4264 buf[0] = image0, buf[1] = image1;
4267 static void
4268 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
4269 REAL_VALUE_TYPE *r, const long *buf)
4271 unsigned long image0, image1;
4272 int exp;
4274 if (FLOAT_WORDS_BIG_ENDIAN)
4275 image1 = buf[0], image0 = buf[1];
4276 else
4277 image0 = buf[0], image1 = buf[1];
4278 image0 &= 0xffffffff;
4279 image1 &= 0xffffffff;
4281 exp = (image0 >> 7) & 0xff;
4283 memset (r, 0, sizeof (*r));
4285 if (exp != 0)
4287 r->cl = rvc_normal;
4288 r->sign = (image0 >> 15) & 1;
4289 SET_REAL_EXP (r, exp - 128);
4291 /* Rearrange the half-words of the external format into
4292 proper ascending order. */
4293 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4294 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4296 if (HOST_BITS_PER_LONG == 64)
4298 image0 = (image0 << 31 << 1) | image1;
4299 image0 <<= 64 - 56;
4300 image0 |= SIG_MSB;
4301 r->sig[SIGSZ-1] = image0;
4303 else
4305 r->sig[SIGSZ-1] = image0;
4306 r->sig[SIGSZ-2] = image1;
4307 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4308 r->sig[SIGSZ-1] |= SIG_MSB;
4313 static void
4314 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4315 const REAL_VALUE_TYPE *r)
4317 unsigned long image0, image1, sign = r->sign << 15;
4319 switch (r->cl)
4321 case rvc_zero:
4322 image0 = image1 = 0;
4323 break;
4325 case rvc_inf:
4326 case rvc_nan:
4327 image0 = 0xffff7fff | sign;
4328 image1 = 0xffffffff;
4329 break;
4331 case rvc_normal:
4332 /* Extract the significand into straight hi:lo. */
4333 if (HOST_BITS_PER_LONG == 64)
4335 image0 = r->sig[SIGSZ-1];
4336 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4337 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4339 else
4341 image0 = r->sig[SIGSZ-1];
4342 image1 = r->sig[SIGSZ-2];
4343 image1 = (image0 << 21) | (image1 >> 11);
4344 image0 = (image0 >> 11) & 0xfffff;
4347 /* Rearrange the half-words of the significand to match the
4348 external format. */
4349 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4350 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4352 /* Add the sign and exponent. */
4353 image0 |= sign;
4354 image0 |= (REAL_EXP (r) + 1024) << 4;
4355 break;
4357 default:
4358 gcc_unreachable ();
4361 if (FLOAT_WORDS_BIG_ENDIAN)
4362 buf[0] = image1, buf[1] = image0;
4363 else
4364 buf[0] = image0, buf[1] = image1;
4367 static void
4368 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4369 REAL_VALUE_TYPE *r, const long *buf)
4371 unsigned long image0, image1;
4372 int exp;
4374 if (FLOAT_WORDS_BIG_ENDIAN)
4375 image1 = buf[0], image0 = buf[1];
4376 else
4377 image0 = buf[0], image1 = buf[1];
4378 image0 &= 0xffffffff;
4379 image1 &= 0xffffffff;
4381 exp = (image0 >> 4) & 0x7ff;
4383 memset (r, 0, sizeof (*r));
4385 if (exp != 0)
4387 r->cl = rvc_normal;
4388 r->sign = (image0 >> 15) & 1;
4389 SET_REAL_EXP (r, exp - 1024);
4391 /* Rearrange the half-words of the external format into
4392 proper ascending order. */
4393 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4394 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4396 if (HOST_BITS_PER_LONG == 64)
4398 image0 = (image0 << 31 << 1) | image1;
4399 image0 <<= 64 - 53;
4400 image0 |= SIG_MSB;
4401 r->sig[SIGSZ-1] = image0;
4403 else
4405 r->sig[SIGSZ-1] = image0;
4406 r->sig[SIGSZ-2] = image1;
4407 lshift_significand (r, r, 64 - 53);
4408 r->sig[SIGSZ-1] |= SIG_MSB;
4413 const struct real_format vax_f_format =
4415 encode_vax_f,
4416 decode_vax_f,
4420 -127,
4421 127,
4424 false,
4425 false,
4426 false,
4427 false,
4428 false,
4429 false,
4430 false,
4431 false
4434 const struct real_format vax_d_format =
4436 encode_vax_d,
4437 decode_vax_d,
4441 -127,
4442 127,
4445 false,
4446 false,
4447 false,
4448 false,
4449 false,
4450 false,
4451 false,
4452 false
4455 const struct real_format vax_g_format =
4457 encode_vax_g,
4458 decode_vax_g,
4462 -1023,
4463 1023,
4466 false,
4467 false,
4468 false,
4469 false,
4470 false,
4471 false,
4472 false,
4473 false
4476 /* Encode real R into a single precision DFP value in BUF. */
4477 static void
4478 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4479 long *buf ATTRIBUTE_UNUSED,
4480 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4482 encode_decimal32 (fmt, buf, r);
4485 /* Decode a single precision DFP value in BUF into a real R. */
4486 static void
4487 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4488 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4489 const long *buf ATTRIBUTE_UNUSED)
4491 decode_decimal32 (fmt, r, buf);
4494 /* Encode real R into a double precision DFP value in BUF. */
4495 static void
4496 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4497 long *buf ATTRIBUTE_UNUSED,
4498 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4500 encode_decimal64 (fmt, buf, r);
4503 /* Decode a double precision DFP value in BUF into a real R. */
4504 static void
4505 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4506 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4507 const long *buf ATTRIBUTE_UNUSED)
4509 decode_decimal64 (fmt, r, buf);
4512 /* Encode real R into a quad precision DFP value in BUF. */
4513 static void
4514 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4515 long *buf ATTRIBUTE_UNUSED,
4516 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4518 encode_decimal128 (fmt, buf, r);
4521 /* Decode a quad precision DFP value in BUF into a real R. */
4522 static void
4523 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4524 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4525 const long *buf ATTRIBUTE_UNUSED)
4527 decode_decimal128 (fmt, r, buf);
4530 /* Single precision decimal floating point (IEEE 754). */
4531 const struct real_format decimal_single_format =
4533 encode_decimal_single,
4534 decode_decimal_single,
4538 -94,
4542 false,
4543 true,
4544 true,
4545 true,
4546 true,
4547 true,
4548 true,
4549 false
4552 /* Double precision decimal floating point (IEEE 754). */
4553 const struct real_format decimal_double_format =
4555 encode_decimal_double,
4556 decode_decimal_double,
4560 -382,
4561 385,
4564 false,
4565 true,
4566 true,
4567 true,
4568 true,
4569 true,
4570 true,
4571 false
4574 /* Quad precision decimal floating point (IEEE 754). */
4575 const struct real_format decimal_quad_format =
4577 encode_decimal_quad,
4578 decode_decimal_quad,
4582 -6142,
4583 6145,
4584 127,
4585 127,
4586 false,
4587 true,
4588 true,
4589 true,
4590 true,
4591 true,
4592 true,
4593 false
4596 /* Encode half-precision floats. This routine is used both for the IEEE
4597 ARM alternative encodings. */
4598 static void
4599 encode_ieee_half (const struct real_format *fmt, long *buf,
4600 const REAL_VALUE_TYPE *r)
4602 unsigned long image, sig, exp;
4603 unsigned long sign = r->sign;
4604 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
4606 image = sign << 15;
4607 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 11)) & 0x3ff;
4609 switch (r->cl)
4611 case rvc_zero:
4612 break;
4614 case rvc_inf:
4615 if (fmt->has_inf)
4616 image |= 31 << 10;
4617 else
4618 image |= 0x7fff;
4619 break;
4621 case rvc_nan:
4622 if (fmt->has_nans)
4624 if (r->canonical)
4625 sig = (fmt->canonical_nan_lsbs_set ? (1 << 9) - 1 : 0);
4626 if (r->signalling == fmt->qnan_msb_set)
4627 sig &= ~(1 << 9);
4628 else
4629 sig |= 1 << 9;
4630 if (sig == 0)
4631 sig = 1 << 8;
4633 image |= 31 << 10;
4634 image |= sig;
4636 else
4637 image |= 0x3ff;
4638 break;
4640 case rvc_normal:
4641 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4642 whereas the intermediate representation is 0.F x 2**exp.
4643 Which means we're off by one. */
4644 if (denormal)
4645 exp = 0;
4646 else
4647 exp = REAL_EXP (r) + 15 - 1;
4648 image |= exp << 10;
4649 image |= sig;
4650 break;
4652 default:
4653 gcc_unreachable ();
4656 buf[0] = image;
4659 /* Decode half-precision floats. This routine is used both for the IEEE
4660 ARM alternative encodings. */
4661 static void
4662 decode_ieee_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4663 const long *buf)
4665 unsigned long image = buf[0] & 0xffff;
4666 bool sign = (image >> 15) & 1;
4667 int exp = (image >> 10) & 0x1f;
4669 memset (r, 0, sizeof (*r));
4670 image <<= HOST_BITS_PER_LONG - 11;
4671 image &= ~SIG_MSB;
4673 if (exp == 0)
4675 if (image && fmt->has_denorm)
4677 r->cl = rvc_normal;
4678 r->sign = sign;
4679 SET_REAL_EXP (r, -14);
4680 r->sig[SIGSZ-1] = image << 1;
4681 normalize (r);
4683 else if (fmt->has_signed_zero)
4684 r->sign = sign;
4686 else if (exp == 31 && (fmt->has_nans || fmt->has_inf))
4688 if (image)
4690 r->cl = rvc_nan;
4691 r->sign = sign;
4692 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4693 ^ fmt->qnan_msb_set);
4694 r->sig[SIGSZ-1] = image;
4696 else
4698 r->cl = rvc_inf;
4699 r->sign = sign;
4702 else
4704 r->cl = rvc_normal;
4705 r->sign = sign;
4706 SET_REAL_EXP (r, exp - 15 + 1);
4707 r->sig[SIGSZ-1] = image | SIG_MSB;
4711 /* Half-precision format, as specified in IEEE 754R. */
4712 const struct real_format ieee_half_format =
4714 encode_ieee_half,
4715 decode_ieee_half,
4719 -13,
4723 false,
4724 true,
4725 true,
4726 true,
4727 true,
4728 true,
4729 true,
4730 false
4733 /* ARM's alternative half-precision format, similar to IEEE but with
4734 no reserved exponent value for NaNs and infinities; rather, it just
4735 extends the range of exponents by one. */
4736 const struct real_format arm_half_format =
4738 encode_ieee_half,
4739 decode_ieee_half,
4743 -13,
4747 false,
4748 true,
4749 false,
4750 false,
4751 true,
4752 true,
4753 false,
4754 false
4757 /* A synthetic "format" for internal arithmetic. It's the size of the
4758 internal significand minus the two bits needed for proper rounding.
4759 The encode and decode routines exist only to satisfy our paranoia
4760 harness. */
4762 static void encode_internal (const struct real_format *fmt,
4763 long *, const REAL_VALUE_TYPE *);
4764 static void decode_internal (const struct real_format *,
4765 REAL_VALUE_TYPE *, const long *);
4767 static void
4768 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4769 const REAL_VALUE_TYPE *r)
4771 memcpy (buf, r, sizeof (*r));
4774 static void
4775 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
4776 REAL_VALUE_TYPE *r, const long *buf)
4778 memcpy (r, buf, sizeof (*r));
4781 const struct real_format real_internal_format =
4783 encode_internal,
4784 decode_internal,
4786 SIGNIFICAND_BITS - 2,
4787 SIGNIFICAND_BITS - 2,
4788 -MAX_EXP,
4789 MAX_EXP,
4792 false,
4793 false,
4794 true,
4795 true,
4796 false,
4797 true,
4798 true,
4799 false
4802 /* Calculate the square root of X in mode MODE, and store the result
4803 in R. Return TRUE if the operation does not raise an exception.
4804 For details see "High Precision Division and Square Root",
4805 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4806 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4808 bool
4809 real_sqrt (REAL_VALUE_TYPE *r, enum machine_mode mode,
4810 const REAL_VALUE_TYPE *x)
4812 static REAL_VALUE_TYPE halfthree;
4813 static bool init = false;
4814 REAL_VALUE_TYPE h, t, i;
4815 int iter, exp;
4817 /* sqrt(-0.0) is -0.0. */
4818 if (real_isnegzero (x))
4820 *r = *x;
4821 return false;
4824 /* Negative arguments return NaN. */
4825 if (real_isneg (x))
4827 get_canonical_qnan (r, 0);
4828 return false;
4831 /* Infinity and NaN return themselves. */
4832 if (!real_isfinite (x))
4834 *r = *x;
4835 return false;
4838 if (!init)
4840 do_add (&halfthree, &dconst1, &dconsthalf, 0);
4841 init = true;
4844 /* Initial guess for reciprocal sqrt, i. */
4845 exp = real_exponent (x);
4846 real_ldexp (&i, &dconst1, -exp/2);
4848 /* Newton's iteration for reciprocal sqrt, i. */
4849 for (iter = 0; iter < 16; iter++)
4851 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4852 do_multiply (&t, x, &i);
4853 do_multiply (&h, &t, &i);
4854 do_multiply (&t, &h, &dconsthalf);
4855 do_add (&h, &halfthree, &t, 1);
4856 do_multiply (&t, &i, &h);
4858 /* Check for early convergence. */
4859 if (iter >= 6 && real_identical (&i, &t))
4860 break;
4862 /* ??? Unroll loop to avoid copying. */
4863 i = t;
4866 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4867 do_multiply (&t, x, &i);
4868 do_multiply (&h, &t, &i);
4869 do_add (&i, &dconst1, &h, 1);
4870 do_multiply (&h, &t, &i);
4871 do_multiply (&i, &dconsthalf, &h);
4872 do_add (&h, &t, &i, 0);
4874 /* ??? We need a Tuckerman test to get the last bit. */
4876 real_convert (r, mode, &h);
4877 return true;
4880 /* Calculate X raised to the integer exponent N in mode MODE and store
4881 the result in R. Return true if the result may be inexact due to
4882 loss of precision. The algorithm is the classic "left-to-right binary
4883 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4884 Algorithms", "The Art of Computer Programming", Volume 2. */
4886 bool
4887 real_powi (REAL_VALUE_TYPE *r, enum machine_mode mode,
4888 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4890 unsigned HOST_WIDE_INT bit;
4891 REAL_VALUE_TYPE t;
4892 bool inexact = false;
4893 bool init = false;
4894 bool neg;
4895 int i;
4897 if (n == 0)
4899 *r = dconst1;
4900 return false;
4902 else if (n < 0)
4904 /* Don't worry about overflow, from now on n is unsigned. */
4905 neg = true;
4906 n = -n;
4908 else
4909 neg = false;
4911 t = *x;
4912 bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
4913 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4915 if (init)
4917 inexact |= do_multiply (&t, &t, &t);
4918 if (n & bit)
4919 inexact |= do_multiply (&t, &t, x);
4921 else if (n & bit)
4922 init = true;
4923 bit >>= 1;
4926 if (neg)
4927 inexact |= do_divide (&t, &dconst1, &t);
4929 real_convert (r, mode, &t);
4930 return inexact;
4933 /* Round X to the nearest integer not larger in absolute value, i.e.
4934 towards zero, placing the result in R in mode MODE. */
4936 void
4937 real_trunc (REAL_VALUE_TYPE *r, enum machine_mode mode,
4938 const REAL_VALUE_TYPE *x)
4940 do_fix_trunc (r, x);
4941 if (mode != VOIDmode)
4942 real_convert (r, mode, r);
4945 /* Round X to the largest integer not greater in value, i.e. round
4946 down, placing the result in R in mode MODE. */
4948 void
4949 real_floor (REAL_VALUE_TYPE *r, enum machine_mode mode,
4950 const REAL_VALUE_TYPE *x)
4952 REAL_VALUE_TYPE t;
4954 do_fix_trunc (&t, x);
4955 if (! real_identical (&t, x) && x->sign)
4956 do_add (&t, &t, &dconstm1, 0);
4957 if (mode != VOIDmode)
4958 real_convert (r, mode, &t);
4959 else
4960 *r = t;
4963 /* Round X to the smallest integer not less then argument, i.e. round
4964 up, placing the result in R in mode MODE. */
4966 void
4967 real_ceil (REAL_VALUE_TYPE *r, enum machine_mode mode,
4968 const REAL_VALUE_TYPE *x)
4970 REAL_VALUE_TYPE t;
4972 do_fix_trunc (&t, x);
4973 if (! real_identical (&t, x) && ! x->sign)
4974 do_add (&t, &t, &dconst1, 0);
4975 if (mode != VOIDmode)
4976 real_convert (r, mode, &t);
4977 else
4978 *r = t;
4981 /* Round X to the nearest integer, but round halfway cases away from
4982 zero. */
4984 void
4985 real_round (REAL_VALUE_TYPE *r, enum machine_mode mode,
4986 const REAL_VALUE_TYPE *x)
4988 do_add (r, x, &dconsthalf, x->sign);
4989 do_fix_trunc (r, r);
4990 if (mode != VOIDmode)
4991 real_convert (r, mode, r);
4994 /* Set the sign of R to the sign of X. */
4996 void
4997 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4999 r->sign = x->sign;
5002 /* Check whether the real constant value given is an integer. */
5004 bool
5005 real_isinteger (const REAL_VALUE_TYPE *c, enum machine_mode mode)
5007 REAL_VALUE_TYPE cint;
5009 real_trunc (&cint, mode, c);
5010 return real_identical (c, &cint);
5013 /* Write into BUF the maximum representable finite floating-point
5014 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5015 float string. LEN is the size of BUF, and the buffer must be large
5016 enough to contain the resulting string. */
5018 void
5019 get_max_float (const struct real_format *fmt, char *buf, size_t len)
5021 int i, n;
5022 char *p;
5024 strcpy (buf, "0x0.");
5025 n = fmt->p;
5026 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
5027 *p++ = 'f';
5028 if (i < n)
5029 *p++ = "08ce"[n - i];
5030 sprintf (p, "p%d", fmt->emax);
5031 if (fmt->pnan < fmt->p)
5033 /* This is an IBM extended double format made up of two IEEE
5034 doubles. The value of the long double is the sum of the
5035 values of the two parts. The most significant part is
5036 required to be the value of the long double rounded to the
5037 nearest double. Rounding means we need a slightly smaller
5038 value for LDBL_MAX. */
5039 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
5042 gcc_assert (strlen (buf) < len);