init.c (sort_mem_initializers): Rename "field_type" to "ctx".
[official-gcc.git] / gcc / real.c
blobc4b9b9e4517f617a974609400589e76138f0c76a
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 Free Software Foundation, Inc.
4 Contributed by Stephen L. Moshier (moshier@world.std.com).
5 Re-written by Richard Henderson <rth@redhat.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "diagnostic-core.h"
29 #include "toplev.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 return do_add (r, op0, op1, 0);
1015 case MINUS_EXPR:
1016 return do_add (r, op0, op1, 1);
1018 case MULT_EXPR:
1019 return do_multiply (r, op0, op1);
1021 case RDIV_EXPR:
1022 return do_divide (r, op0, op1);
1024 case MIN_EXPR:
1025 if (op1->cl == rvc_nan)
1026 *r = *op1;
1027 else if (do_compare (op0, op1, -1) < 0)
1028 *r = *op0;
1029 else
1030 *r = *op1;
1031 break;
1033 case MAX_EXPR:
1034 if (op1->cl == rvc_nan)
1035 *r = *op1;
1036 else if (do_compare (op0, op1, 1) < 0)
1037 *r = *op1;
1038 else
1039 *r = *op0;
1040 break;
1042 case NEGATE_EXPR:
1043 *r = *op0;
1044 r->sign ^= 1;
1045 break;
1047 case ABS_EXPR:
1048 *r = *op0;
1049 r->sign = 0;
1050 break;
1052 case FIX_TRUNC_EXPR:
1053 do_fix_trunc (r, op0);
1054 break;
1056 default:
1057 gcc_unreachable ();
1059 return false;
1062 REAL_VALUE_TYPE
1063 real_value_negate (const REAL_VALUE_TYPE *op0)
1065 REAL_VALUE_TYPE r;
1066 real_arithmetic (&r, NEGATE_EXPR, op0, NULL);
1067 return r;
1070 REAL_VALUE_TYPE
1071 real_value_abs (const REAL_VALUE_TYPE *op0)
1073 REAL_VALUE_TYPE r;
1074 real_arithmetic (&r, ABS_EXPR, op0, NULL);
1075 return r;
1078 bool
1079 real_compare (int icode, const REAL_VALUE_TYPE *op0,
1080 const REAL_VALUE_TYPE *op1)
1082 enum tree_code code = (enum tree_code) icode;
1084 switch (code)
1086 case LT_EXPR:
1087 return do_compare (op0, op1, 1) < 0;
1088 case LE_EXPR:
1089 return do_compare (op0, op1, 1) <= 0;
1090 case GT_EXPR:
1091 return do_compare (op0, op1, -1) > 0;
1092 case GE_EXPR:
1093 return do_compare (op0, op1, -1) >= 0;
1094 case EQ_EXPR:
1095 return do_compare (op0, op1, -1) == 0;
1096 case NE_EXPR:
1097 return do_compare (op0, op1, -1) != 0;
1098 case UNORDERED_EXPR:
1099 return op0->cl == rvc_nan || op1->cl == rvc_nan;
1100 case ORDERED_EXPR:
1101 return op0->cl != rvc_nan && op1->cl != rvc_nan;
1102 case UNLT_EXPR:
1103 return do_compare (op0, op1, -1) < 0;
1104 case UNLE_EXPR:
1105 return do_compare (op0, op1, -1) <= 0;
1106 case UNGT_EXPR:
1107 return do_compare (op0, op1, 1) > 0;
1108 case UNGE_EXPR:
1109 return do_compare (op0, op1, 1) >= 0;
1110 case UNEQ_EXPR:
1111 return do_compare (op0, op1, 0) == 0;
1112 case LTGT_EXPR:
1113 return do_compare (op0, op1, 0) != 0;
1115 default:
1116 gcc_unreachable ();
1120 /* Return floor log2(R). */
1123 real_exponent (const REAL_VALUE_TYPE *r)
1125 switch (r->cl)
1127 case rvc_zero:
1128 return 0;
1129 case rvc_inf:
1130 case rvc_nan:
1131 return (unsigned int)-1 >> 1;
1132 case rvc_normal:
1133 return REAL_EXP (r);
1134 default:
1135 gcc_unreachable ();
1139 /* R = OP0 * 2**EXP. */
1141 void
1142 real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1144 *r = *op0;
1145 switch (r->cl)
1147 case rvc_zero:
1148 case rvc_inf:
1149 case rvc_nan:
1150 break;
1152 case rvc_normal:
1153 exp += REAL_EXP (op0);
1154 if (exp > MAX_EXP)
1155 get_inf (r, r->sign);
1156 else if (exp < -MAX_EXP)
1157 get_zero (r, r->sign);
1158 else
1159 SET_REAL_EXP (r, exp);
1160 break;
1162 default:
1163 gcc_unreachable ();
1167 /* Determine whether a floating-point value X is infinite. */
1169 bool
1170 real_isinf (const REAL_VALUE_TYPE *r)
1172 return (r->cl == rvc_inf);
1175 /* Determine whether a floating-point value X is a NaN. */
1177 bool
1178 real_isnan (const REAL_VALUE_TYPE *r)
1180 return (r->cl == rvc_nan);
1183 /* Determine whether a floating-point value X is finite. */
1185 bool
1186 real_isfinite (const REAL_VALUE_TYPE *r)
1188 return (r->cl != rvc_nan) && (r->cl != rvc_inf);
1191 /* Determine whether a floating-point value X is negative. */
1193 bool
1194 real_isneg (const REAL_VALUE_TYPE *r)
1196 return r->sign;
1199 /* Determine whether a floating-point value X is minus zero. */
1201 bool
1202 real_isnegzero (const REAL_VALUE_TYPE *r)
1204 return r->sign && r->cl == rvc_zero;
1207 /* Compare two floating-point objects for bitwise identity. */
1209 bool
1210 real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1212 int i;
1214 if (a->cl != b->cl)
1215 return false;
1216 if (a->sign != b->sign)
1217 return false;
1219 switch (a->cl)
1221 case rvc_zero:
1222 case rvc_inf:
1223 return true;
1225 case rvc_normal:
1226 if (a->decimal != b->decimal)
1227 return false;
1228 if (REAL_EXP (a) != REAL_EXP (b))
1229 return false;
1230 break;
1232 case rvc_nan:
1233 if (a->signalling != b->signalling)
1234 return false;
1235 /* The significand is ignored for canonical NaNs. */
1236 if (a->canonical || b->canonical)
1237 return a->canonical == b->canonical;
1238 break;
1240 default:
1241 gcc_unreachable ();
1244 for (i = 0; i < SIGSZ; ++i)
1245 if (a->sig[i] != b->sig[i])
1246 return false;
1248 return true;
1251 /* Try to change R into its exact multiplicative inverse in machine
1252 mode MODE. Return true if successful. */
1254 bool
1255 exact_real_inverse (enum machine_mode mode, REAL_VALUE_TYPE *r)
1257 const REAL_VALUE_TYPE *one = real_digit (1);
1258 REAL_VALUE_TYPE u;
1259 int i;
1261 if (r->cl != rvc_normal)
1262 return false;
1264 /* Check for a power of two: all significand bits zero except the MSB. */
1265 for (i = 0; i < SIGSZ-1; ++i)
1266 if (r->sig[i] != 0)
1267 return false;
1268 if (r->sig[SIGSZ-1] != SIG_MSB)
1269 return false;
1271 /* Find the inverse and truncate to the required mode. */
1272 do_divide (&u, one, r);
1273 real_convert (&u, mode, &u);
1275 /* The rounding may have overflowed. */
1276 if (u.cl != rvc_normal)
1277 return false;
1278 for (i = 0; i < SIGSZ-1; ++i)
1279 if (u.sig[i] != 0)
1280 return false;
1281 if (u.sig[SIGSZ-1] != SIG_MSB)
1282 return false;
1284 *r = u;
1285 return true;
1288 /* Return true if arithmetic on values in IMODE that were promoted
1289 from values in TMODE is equivalent to direct arithmetic on values
1290 in TMODE. */
1292 bool
1293 real_can_shorten_arithmetic (enum machine_mode imode, enum machine_mode tmode)
1295 const struct real_format *tfmt, *ifmt;
1296 tfmt = REAL_MODE_FORMAT (tmode);
1297 ifmt = REAL_MODE_FORMAT (imode);
1298 /* These conditions are conservative rather than trying to catch the
1299 exact boundary conditions; the main case to allow is IEEE float
1300 and double. */
1301 return (ifmt->b == tfmt->b
1302 && ifmt->p > 2 * tfmt->p
1303 && ifmt->emin < 2 * tfmt->emin - tfmt->p - 2
1304 && ifmt->emin < tfmt->emin - tfmt->emax - tfmt->p - 2
1305 && ifmt->emax > 2 * tfmt->emax + 2
1306 && ifmt->emax > tfmt->emax - tfmt->emin + tfmt->p + 2
1307 && ifmt->round_towards_zero == tfmt->round_towards_zero
1308 && (ifmt->has_sign_dependent_rounding
1309 == tfmt->has_sign_dependent_rounding)
1310 && ifmt->has_nans >= tfmt->has_nans
1311 && ifmt->has_inf >= tfmt->has_inf
1312 && ifmt->has_signed_zero >= tfmt->has_signed_zero
1313 && !MODE_COMPOSITE_P (tmode)
1314 && !MODE_COMPOSITE_P (imode));
1317 /* Render R as an integer. */
1319 HOST_WIDE_INT
1320 real_to_integer (const REAL_VALUE_TYPE *r)
1322 unsigned HOST_WIDE_INT i;
1324 switch (r->cl)
1326 case rvc_zero:
1327 underflow:
1328 return 0;
1330 case rvc_inf:
1331 case rvc_nan:
1332 overflow:
1333 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1334 if (!r->sign)
1335 i--;
1336 return i;
1338 case rvc_normal:
1339 if (r->decimal)
1340 return decimal_real_to_integer (r);
1342 if (REAL_EXP (r) <= 0)
1343 goto underflow;
1344 /* Only force overflow for unsigned overflow. Signed overflow is
1345 undefined, so it doesn't matter what we return, and some callers
1346 expect to be able to use this routine for both signed and
1347 unsigned conversions. */
1348 if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1349 goto overflow;
1351 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1352 i = r->sig[SIGSZ-1];
1353 else
1355 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1356 i = r->sig[SIGSZ-1];
1357 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1358 i |= r->sig[SIGSZ-2];
1361 i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1363 if (r->sign)
1364 i = -i;
1365 return i;
1367 default:
1368 gcc_unreachable ();
1372 /* Likewise, but to an integer pair, HI+LOW. */
1374 void
1375 real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
1376 const REAL_VALUE_TYPE *r)
1378 REAL_VALUE_TYPE t;
1379 HOST_WIDE_INT low, high;
1380 int exp;
1382 switch (r->cl)
1384 case rvc_zero:
1385 underflow:
1386 low = high = 0;
1387 break;
1389 case rvc_inf:
1390 case rvc_nan:
1391 overflow:
1392 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1393 if (r->sign)
1394 low = 0;
1395 else
1397 high--;
1398 low = -1;
1400 break;
1402 case rvc_normal:
1403 if (r->decimal)
1405 decimal_real_to_integer2 (plow, phigh, r);
1406 return;
1409 exp = REAL_EXP (r);
1410 if (exp <= 0)
1411 goto underflow;
1412 /* Only force overflow for unsigned overflow. Signed overflow is
1413 undefined, so it doesn't matter what we return, and some callers
1414 expect to be able to use this routine for both signed and
1415 unsigned conversions. */
1416 if (exp > 2*HOST_BITS_PER_WIDE_INT)
1417 goto overflow;
1419 rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
1420 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1422 high = t.sig[SIGSZ-1];
1423 low = t.sig[SIGSZ-2];
1425 else
1427 gcc_assert (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG);
1428 high = t.sig[SIGSZ-1];
1429 high = high << (HOST_BITS_PER_LONG - 1) << 1;
1430 high |= t.sig[SIGSZ-2];
1432 low = t.sig[SIGSZ-3];
1433 low = low << (HOST_BITS_PER_LONG - 1) << 1;
1434 low |= t.sig[SIGSZ-4];
1437 if (r->sign)
1439 if (low == 0)
1440 high = -high;
1441 else
1442 low = -low, high = ~high;
1444 break;
1446 default:
1447 gcc_unreachable ();
1450 *plow = low;
1451 *phigh = high;
1454 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1455 of NUM / DEN. Return the quotient and place the remainder in NUM.
1456 It is expected that NUM / DEN are close enough that the quotient is
1457 small. */
1459 static unsigned long
1460 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1462 unsigned long q, msb;
1463 int expn = REAL_EXP (num), expd = REAL_EXP (den);
1465 if (expn < expd)
1466 return 0;
1468 q = msb = 0;
1469 goto start;
1472 msb = num->sig[SIGSZ-1] & SIG_MSB;
1473 q <<= 1;
1474 lshift_significand_1 (num, num);
1475 start:
1476 if (msb || cmp_significands (num, den) >= 0)
1478 sub_significands (num, num, den, 0);
1479 q |= 1;
1482 while (--expn >= expd);
1484 SET_REAL_EXP (num, expd);
1485 normalize (num);
1487 return q;
1490 /* Render R as a decimal floating point constant. Emit DIGITS significant
1491 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1492 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1493 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1494 to a string that, when parsed back in mode MODE, yields the same value. */
1496 #define M_LOG10_2 0.30102999566398119521
1498 void
1499 real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig,
1500 size_t buf_size, size_t digits,
1501 int crop_trailing_zeros, enum machine_mode mode)
1503 const struct real_format *fmt = NULL;
1504 const REAL_VALUE_TYPE *one, *ten;
1505 REAL_VALUE_TYPE r, pten, u, v;
1506 int dec_exp, cmp_one, digit;
1507 size_t max_digits;
1508 char *p, *first, *last;
1509 bool sign;
1510 bool round_up;
1512 if (mode != VOIDmode)
1514 fmt = REAL_MODE_FORMAT (mode);
1515 gcc_assert (fmt);
1518 r = *r_orig;
1519 switch (r.cl)
1521 case rvc_zero:
1522 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1523 return;
1524 case rvc_normal:
1525 break;
1526 case rvc_inf:
1527 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1528 return;
1529 case rvc_nan:
1530 /* ??? Print the significand as well, if not canonical? */
1531 sprintf (str, "%c%cNaN", (r_orig->sign ? '-' : '+'),
1532 (r_orig->signalling ? 'S' : 'Q'));
1533 return;
1534 default:
1535 gcc_unreachable ();
1538 if (r.decimal)
1540 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1541 return;
1544 /* Bound the number of digits printed by the size of the representation. */
1545 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1546 if (digits == 0 || digits > max_digits)
1547 digits = max_digits;
1549 /* Estimate the decimal exponent, and compute the length of the string it
1550 will print as. Be conservative and add one to account for possible
1551 overflow or rounding error. */
1552 dec_exp = REAL_EXP (&r) * M_LOG10_2;
1553 for (max_digits = 1; dec_exp ; max_digits++)
1554 dec_exp /= 10;
1556 /* Bound the number of digits printed by the size of the output buffer. */
1557 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1558 gcc_assert (max_digits <= buf_size);
1559 if (digits > max_digits)
1560 digits = max_digits;
1562 one = real_digit (1);
1563 ten = ten_to_ptwo (0);
1565 sign = r.sign;
1566 r.sign = 0;
1568 dec_exp = 0;
1569 pten = *one;
1571 cmp_one = do_compare (&r, one, 0);
1572 if (cmp_one > 0)
1574 int m;
1576 /* Number is greater than one. Convert significand to an integer
1577 and strip trailing decimal zeros. */
1579 u = r;
1580 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1582 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1583 m = floor_log2 (max_digits);
1585 /* Iterate over the bits of the possible powers of 10 that might
1586 be present in U and eliminate them. That is, if we find that
1587 10**2**M divides U evenly, keep the division and increase
1588 DEC_EXP by 2**M. */
1591 REAL_VALUE_TYPE t;
1593 do_divide (&t, &u, ten_to_ptwo (m));
1594 do_fix_trunc (&v, &t);
1595 if (cmp_significands (&v, &t) == 0)
1597 u = t;
1598 dec_exp += 1 << m;
1601 while (--m >= 0);
1603 /* Revert the scaling to integer that we performed earlier. */
1604 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1605 - (SIGNIFICAND_BITS - 1));
1606 r = u;
1608 /* Find power of 10. Do this by dividing out 10**2**M when
1609 this is larger than the current remainder. Fill PTEN with
1610 the power of 10 that we compute. */
1611 if (REAL_EXP (&r) > 0)
1613 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1616 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1617 if (do_compare (&u, ptentwo, 0) >= 0)
1619 do_divide (&u, &u, ptentwo);
1620 do_multiply (&pten, &pten, ptentwo);
1621 dec_exp += 1 << m;
1624 while (--m >= 0);
1626 else
1627 /* We managed to divide off enough tens in the above reduction
1628 loop that we've now got a negative exponent. Fall into the
1629 less-than-one code to compute the proper value for PTEN. */
1630 cmp_one = -1;
1632 if (cmp_one < 0)
1634 int m;
1636 /* Number is less than one. Pad significand with leading
1637 decimal zeros. */
1639 v = r;
1640 while (1)
1642 /* Stop if we'd shift bits off the bottom. */
1643 if (v.sig[0] & 7)
1644 break;
1646 do_multiply (&u, &v, ten);
1648 /* Stop if we're now >= 1. */
1649 if (REAL_EXP (&u) > 0)
1650 break;
1652 v = u;
1653 dec_exp -= 1;
1655 r = v;
1657 /* Find power of 10. Do this by multiplying in P=10**2**M when
1658 the current remainder is smaller than 1/P. Fill PTEN with the
1659 power of 10 that we compute. */
1660 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1663 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1664 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1666 if (do_compare (&v, ptenmtwo, 0) <= 0)
1668 do_multiply (&v, &v, ptentwo);
1669 do_multiply (&pten, &pten, ptentwo);
1670 dec_exp -= 1 << m;
1673 while (--m >= 0);
1675 /* Invert the positive power of 10 that we've collected so far. */
1676 do_divide (&pten, one, &pten);
1679 p = str;
1680 if (sign)
1681 *p++ = '-';
1682 first = p++;
1684 /* At this point, PTEN should contain the nearest power of 10 smaller
1685 than R, such that this division produces the first digit.
1687 Using a divide-step primitive that returns the complete integral
1688 remainder avoids the rounding error that would be produced if
1689 we were to use do_divide here and then simply multiply by 10 for
1690 each subsequent digit. */
1692 digit = rtd_divmod (&r, &pten);
1694 /* Be prepared for error in that division via underflow ... */
1695 if (digit == 0 && cmp_significand_0 (&r))
1697 /* Multiply by 10 and try again. */
1698 do_multiply (&r, &r, ten);
1699 digit = rtd_divmod (&r, &pten);
1700 dec_exp -= 1;
1701 gcc_assert (digit != 0);
1704 /* ... or overflow. */
1705 if (digit == 10)
1707 *p++ = '1';
1708 if (--digits > 0)
1709 *p++ = '0';
1710 dec_exp += 1;
1712 else
1714 gcc_assert (digit <= 10);
1715 *p++ = digit + '0';
1718 /* Generate subsequent digits. */
1719 while (--digits > 0)
1721 do_multiply (&r, &r, ten);
1722 digit = rtd_divmod (&r, &pten);
1723 *p++ = digit + '0';
1725 last = p;
1727 /* Generate one more digit with which to do rounding. */
1728 do_multiply (&r, &r, ten);
1729 digit = rtd_divmod (&r, &pten);
1731 /* Round the result. */
1732 if (fmt && fmt->round_towards_zero)
1734 /* If the format uses round towards zero when parsing the string
1735 back in, we need to always round away from zero here. */
1736 if (cmp_significand_0 (&r))
1737 digit++;
1738 round_up = digit > 0;
1740 else
1742 if (digit == 5)
1744 /* Round to nearest. If R is nonzero there are additional
1745 nonzero digits to be extracted. */
1746 if (cmp_significand_0 (&r))
1747 digit++;
1748 /* Round to even. */
1749 else if ((p[-1] - '0') & 1)
1750 digit++;
1753 round_up = digit > 5;
1756 if (round_up)
1758 while (p > first)
1760 digit = *--p;
1761 if (digit == '9')
1762 *p = '0';
1763 else
1765 *p = digit + 1;
1766 break;
1770 /* Carry out of the first digit. This means we had all 9's and
1771 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1772 if (p == first)
1774 first[1] = '1';
1775 dec_exp++;
1779 /* Insert the decimal point. */
1780 first[0] = first[1];
1781 first[1] = '.';
1783 /* If requested, drop trailing zeros. Never crop past "1.0". */
1784 if (crop_trailing_zeros)
1785 while (last > first + 3 && last[-1] == '0')
1786 last--;
1788 /* Append the exponent. */
1789 sprintf (last, "e%+d", dec_exp);
1791 #ifdef ENABLE_CHECKING
1792 /* Verify that we can read the original value back in. */
1793 if (mode != VOIDmode)
1795 real_from_string (&r, str);
1796 real_convert (&r, mode, &r);
1797 gcc_assert (real_identical (&r, r_orig));
1799 #endif
1802 /* Likewise, except always uses round-to-nearest. */
1804 void
1805 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1806 size_t digits, int crop_trailing_zeros)
1808 real_to_decimal_for_mode (str, r_orig, buf_size,
1809 digits, crop_trailing_zeros, VOIDmode);
1812 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1813 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1814 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1815 strip trailing zeros. */
1817 void
1818 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1819 size_t digits, int crop_trailing_zeros)
1821 int i, j, exp = REAL_EXP (r);
1822 char *p, *first;
1823 char exp_buf[16];
1824 size_t max_digits;
1826 switch (r->cl)
1828 case rvc_zero:
1829 exp = 0;
1830 break;
1831 case rvc_normal:
1832 break;
1833 case rvc_inf:
1834 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1835 return;
1836 case rvc_nan:
1837 /* ??? Print the significand as well, if not canonical? */
1838 sprintf (str, "%c%cNaN", (r->sign ? '-' : '+'),
1839 (r->signalling ? 'S' : 'Q'));
1840 return;
1841 default:
1842 gcc_unreachable ();
1845 if (r->decimal)
1847 /* Hexadecimal format for decimal floats is not interesting. */
1848 strcpy (str, "N/A");
1849 return;
1852 if (digits == 0)
1853 digits = SIGNIFICAND_BITS / 4;
1855 /* Bound the number of digits printed by the size of the output buffer. */
1857 sprintf (exp_buf, "p%+d", exp);
1858 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1859 gcc_assert (max_digits <= buf_size);
1860 if (digits > max_digits)
1861 digits = max_digits;
1863 p = str;
1864 if (r->sign)
1865 *p++ = '-';
1866 *p++ = '0';
1867 *p++ = 'x';
1868 *p++ = '0';
1869 *p++ = '.';
1870 first = p;
1872 for (i = SIGSZ - 1; i >= 0; --i)
1873 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1875 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1876 if (--digits == 0)
1877 goto out;
1880 out:
1881 if (crop_trailing_zeros)
1882 while (p > first + 1 && p[-1] == '0')
1883 p--;
1885 sprintf (p, "p%+d", exp);
1888 /* Initialize R from a decimal or hexadecimal string. The string is
1889 assumed to have been syntax checked already. Return -1 if the
1890 value underflows, +1 if overflows, and 0 otherwise. */
1893 real_from_string (REAL_VALUE_TYPE *r, const char *str)
1895 int exp = 0;
1896 bool sign = false;
1898 get_zero (r, 0);
1900 if (*str == '-')
1902 sign = true;
1903 str++;
1905 else if (*str == '+')
1906 str++;
1908 if (!strncmp (str, "QNaN", 4))
1910 get_canonical_qnan (r, sign);
1911 return 0;
1913 else if (!strncmp (str, "SNaN", 4))
1915 get_canonical_snan (r, sign);
1916 return 0;
1918 else if (!strncmp (str, "Inf", 3))
1920 get_inf (r, sign);
1921 return 0;
1924 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1926 /* Hexadecimal floating point. */
1927 int pos = SIGNIFICAND_BITS - 4, d;
1929 str += 2;
1931 while (*str == '0')
1932 str++;
1933 while (1)
1935 d = hex_value (*str);
1936 if (d == _hex_bad)
1937 break;
1938 if (pos >= 0)
1940 r->sig[pos / HOST_BITS_PER_LONG]
1941 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1942 pos -= 4;
1944 else if (d)
1945 /* Ensure correct rounding by setting last bit if there is
1946 a subsequent nonzero digit. */
1947 r->sig[0] |= 1;
1948 exp += 4;
1949 str++;
1951 if (*str == '.')
1953 str++;
1954 if (pos == SIGNIFICAND_BITS - 4)
1956 while (*str == '0')
1957 str++, exp -= 4;
1959 while (1)
1961 d = hex_value (*str);
1962 if (d == _hex_bad)
1963 break;
1964 if (pos >= 0)
1966 r->sig[pos / HOST_BITS_PER_LONG]
1967 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1968 pos -= 4;
1970 else if (d)
1971 /* Ensure correct rounding by setting last bit if there is
1972 a subsequent nonzero digit. */
1973 r->sig[0] |= 1;
1974 str++;
1978 /* If the mantissa is zero, ignore the exponent. */
1979 if (!cmp_significand_0 (r))
1980 goto is_a_zero;
1982 if (*str == 'p' || *str == 'P')
1984 bool exp_neg = false;
1986 str++;
1987 if (*str == '-')
1989 exp_neg = true;
1990 str++;
1992 else if (*str == '+')
1993 str++;
1995 d = 0;
1996 while (ISDIGIT (*str))
1998 d *= 10;
1999 d += *str - '0';
2000 if (d > MAX_EXP)
2002 /* Overflowed the exponent. */
2003 if (exp_neg)
2004 goto underflow;
2005 else
2006 goto overflow;
2008 str++;
2010 if (exp_neg)
2011 d = -d;
2013 exp += d;
2016 r->cl = rvc_normal;
2017 SET_REAL_EXP (r, exp);
2019 normalize (r);
2021 else
2023 /* Decimal floating point. */
2024 const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
2025 int d;
2027 while (*str == '0')
2028 str++;
2029 while (ISDIGIT (*str))
2031 d = *str++ - '0';
2032 do_multiply (r, r, ten);
2033 if (d)
2034 do_add (r, r, real_digit (d), 0);
2036 if (*str == '.')
2038 str++;
2039 if (r->cl == rvc_zero)
2041 while (*str == '0')
2042 str++, exp--;
2044 while (ISDIGIT (*str))
2046 d = *str++ - '0';
2047 do_multiply (r, r, ten);
2048 if (d)
2049 do_add (r, r, real_digit (d), 0);
2050 exp--;
2054 /* If the mantissa is zero, ignore the exponent. */
2055 if (r->cl == rvc_zero)
2056 goto is_a_zero;
2058 if (*str == 'e' || *str == 'E')
2060 bool exp_neg = false;
2062 str++;
2063 if (*str == '-')
2065 exp_neg = true;
2066 str++;
2068 else if (*str == '+')
2069 str++;
2071 d = 0;
2072 while (ISDIGIT (*str))
2074 d *= 10;
2075 d += *str - '0';
2076 if (d > MAX_EXP)
2078 /* Overflowed the exponent. */
2079 if (exp_neg)
2080 goto underflow;
2081 else
2082 goto overflow;
2084 str++;
2086 if (exp_neg)
2087 d = -d;
2088 exp += d;
2091 if (exp)
2092 times_pten (r, exp);
2095 r->sign = sign;
2096 return 0;
2098 is_a_zero:
2099 get_zero (r, sign);
2100 return 0;
2102 underflow:
2103 get_zero (r, sign);
2104 return -1;
2106 overflow:
2107 get_inf (r, sign);
2108 return 1;
2111 /* Legacy. Similar, but return the result directly. */
2113 REAL_VALUE_TYPE
2114 real_from_string2 (const char *s, enum machine_mode mode)
2116 REAL_VALUE_TYPE r;
2118 real_from_string (&r, s);
2119 if (mode != VOIDmode)
2120 real_convert (&r, mode, &r);
2122 return r;
2125 /* Initialize R from string S and desired MODE. */
2127 void
2128 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, enum machine_mode mode)
2130 if (DECIMAL_FLOAT_MODE_P (mode))
2131 decimal_real_from_string (r, s);
2132 else
2133 real_from_string (r, s);
2135 if (mode != VOIDmode)
2136 real_convert (r, mode, r);
2139 /* Initialize R from the integer pair HIGH+LOW. */
2141 void
2142 real_from_integer (REAL_VALUE_TYPE *r, enum machine_mode mode,
2143 unsigned HOST_WIDE_INT low, HOST_WIDE_INT high,
2144 int unsigned_p)
2146 if (low == 0 && high == 0)
2147 get_zero (r, 0);
2148 else
2150 memset (r, 0, sizeof (*r));
2151 r->cl = rvc_normal;
2152 r->sign = high < 0 && !unsigned_p;
2153 SET_REAL_EXP (r, 2 * HOST_BITS_PER_WIDE_INT);
2155 if (r->sign)
2157 high = ~high;
2158 if (low == 0)
2159 high += 1;
2160 else
2161 low = -low;
2164 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2166 r->sig[SIGSZ-1] = high;
2167 r->sig[SIGSZ-2] = low;
2169 else
2171 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2172 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
2173 r->sig[SIGSZ-2] = high;
2174 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
2175 r->sig[SIGSZ-4] = low;
2178 normalize (r);
2181 if (DECIMAL_FLOAT_MODE_P (mode))
2182 decimal_from_integer (r);
2183 else if (mode != VOIDmode)
2184 real_convert (r, mode, r);
2187 /* Render R, an integral value, as a floating point constant with no
2188 specified exponent. */
2190 static void
2191 decimal_integer_string (char *str, const REAL_VALUE_TYPE *r_orig,
2192 size_t buf_size)
2194 int dec_exp, digit, digits;
2195 REAL_VALUE_TYPE r, pten;
2196 char *p;
2197 bool sign;
2199 r = *r_orig;
2201 if (r.cl == rvc_zero)
2203 strcpy (str, "0.");
2204 return;
2207 sign = r.sign;
2208 r.sign = 0;
2210 dec_exp = REAL_EXP (&r) * M_LOG10_2;
2211 digits = dec_exp + 1;
2212 gcc_assert ((digits + 2) < (int)buf_size);
2214 pten = *real_digit (1);
2215 times_pten (&pten, dec_exp);
2217 p = str;
2218 if (sign)
2219 *p++ = '-';
2221 digit = rtd_divmod (&r, &pten);
2222 gcc_assert (digit >= 0 && digit <= 9);
2223 *p++ = digit + '0';
2224 while (--digits > 0)
2226 times_pten (&r, 1);
2227 digit = rtd_divmod (&r, &pten);
2228 *p++ = digit + '0';
2230 *p++ = '.';
2231 *p++ = '\0';
2234 /* Convert a real with an integral value to decimal float. */
2236 static void
2237 decimal_from_integer (REAL_VALUE_TYPE *r)
2239 char str[256];
2241 decimal_integer_string (str, r, sizeof (str) - 1);
2242 decimal_real_from_string (r, str);
2245 /* Returns 10**2**N. */
2247 static const REAL_VALUE_TYPE *
2248 ten_to_ptwo (int n)
2250 static REAL_VALUE_TYPE tens[EXP_BITS];
2252 gcc_assert (n >= 0);
2253 gcc_assert (n < EXP_BITS);
2255 if (tens[n].cl == rvc_zero)
2257 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2259 HOST_WIDE_INT t = 10;
2260 int i;
2262 for (i = 0; i < n; ++i)
2263 t *= t;
2265 real_from_integer (&tens[n], VOIDmode, t, 0, 1);
2267 else
2269 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2270 do_multiply (&tens[n], t, t);
2274 return &tens[n];
2277 /* Returns 10**(-2**N). */
2279 static const REAL_VALUE_TYPE *
2280 ten_to_mptwo (int n)
2282 static REAL_VALUE_TYPE tens[EXP_BITS];
2284 gcc_assert (n >= 0);
2285 gcc_assert (n < EXP_BITS);
2287 if (tens[n].cl == rvc_zero)
2288 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2290 return &tens[n];
2293 /* Returns N. */
2295 static const REAL_VALUE_TYPE *
2296 real_digit (int n)
2298 static REAL_VALUE_TYPE num[10];
2300 gcc_assert (n >= 0);
2301 gcc_assert (n <= 9);
2303 if (n > 0 && num[n].cl == rvc_zero)
2304 real_from_integer (&num[n], VOIDmode, n, 0, 1);
2306 return &num[n];
2309 /* Multiply R by 10**EXP. */
2311 static void
2312 times_pten (REAL_VALUE_TYPE *r, int exp)
2314 REAL_VALUE_TYPE pten, *rr;
2315 bool negative = (exp < 0);
2316 int i;
2318 if (negative)
2320 exp = -exp;
2321 pten = *real_digit (1);
2322 rr = &pten;
2324 else
2325 rr = r;
2327 for (i = 0; exp > 0; ++i, exp >>= 1)
2328 if (exp & 1)
2329 do_multiply (rr, rr, ten_to_ptwo (i));
2331 if (negative)
2332 do_divide (r, r, &pten);
2335 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2337 const REAL_VALUE_TYPE *
2338 dconst_e_ptr (void)
2340 static REAL_VALUE_TYPE value;
2342 /* Initialize mathematical constants for constant folding builtins.
2343 These constants need to be given to at least 160 bits precision. */
2344 if (value.cl == rvc_zero)
2346 mpfr_t m;
2347 mpfr_init2 (m, SIGNIFICAND_BITS);
2348 mpfr_set_ui (m, 1, GMP_RNDN);
2349 mpfr_exp (m, m, GMP_RNDN);
2350 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2351 mpfr_clear (m);
2354 return &value;
2357 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2359 const REAL_VALUE_TYPE *
2360 dconst_third_ptr (void)
2362 static REAL_VALUE_TYPE value;
2364 /* Initialize mathematical constants for constant folding builtins.
2365 These constants need to be given to at least 160 bits precision. */
2366 if (value.cl == rvc_zero)
2368 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (3));
2370 return &value;
2373 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2375 const REAL_VALUE_TYPE *
2376 dconst_sqrt2_ptr (void)
2378 static REAL_VALUE_TYPE value;
2380 /* Initialize mathematical constants for constant folding builtins.
2381 These constants need to be given to at least 160 bits precision. */
2382 if (value.cl == rvc_zero)
2384 mpfr_t m;
2385 mpfr_init2 (m, SIGNIFICAND_BITS);
2386 mpfr_sqrt_ui (m, 2, GMP_RNDN);
2387 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2388 mpfr_clear (m);
2390 return &value;
2393 /* Fills R with +Inf. */
2395 void
2396 real_inf (REAL_VALUE_TYPE *r)
2398 get_inf (r, 0);
2401 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2402 we force a QNaN, else we force an SNaN. The string, if not empty,
2403 is parsed as a number and placed in the significand. Return true
2404 if the string was successfully parsed. */
2406 bool
2407 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2408 enum machine_mode mode)
2410 const struct real_format *fmt;
2412 fmt = REAL_MODE_FORMAT (mode);
2413 gcc_assert (fmt);
2415 if (*str == 0)
2417 if (quiet)
2418 get_canonical_qnan (r, 0);
2419 else
2420 get_canonical_snan (r, 0);
2422 else
2424 int base = 10, d;
2426 memset (r, 0, sizeof (*r));
2427 r->cl = rvc_nan;
2429 /* Parse akin to strtol into the significand of R. */
2431 while (ISSPACE (*str))
2432 str++;
2433 if (*str == '-')
2434 str++;
2435 else if (*str == '+')
2436 str++;
2437 if (*str == '0')
2439 str++;
2440 if (*str == 'x' || *str == 'X')
2442 base = 16;
2443 str++;
2445 else
2446 base = 8;
2449 while ((d = hex_value (*str)) < base)
2451 REAL_VALUE_TYPE u;
2453 switch (base)
2455 case 8:
2456 lshift_significand (r, r, 3);
2457 break;
2458 case 16:
2459 lshift_significand (r, r, 4);
2460 break;
2461 case 10:
2462 lshift_significand_1 (&u, r);
2463 lshift_significand (r, r, 3);
2464 add_significands (r, r, &u);
2465 break;
2466 default:
2467 gcc_unreachable ();
2470 get_zero (&u, 0);
2471 u.sig[0] = d;
2472 add_significands (r, r, &u);
2474 str++;
2477 /* Must have consumed the entire string for success. */
2478 if (*str != 0)
2479 return false;
2481 /* Shift the significand into place such that the bits
2482 are in the most significant bits for the format. */
2483 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2485 /* Our MSB is always unset for NaNs. */
2486 r->sig[SIGSZ-1] &= ~SIG_MSB;
2488 /* Force quiet or signalling NaN. */
2489 r->signalling = !quiet;
2492 return true;
2495 /* Fills R with the largest finite value representable in mode MODE.
2496 If SIGN is nonzero, R is set to the most negative finite value. */
2498 void
2499 real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
2501 const struct real_format *fmt;
2502 int np2;
2504 fmt = REAL_MODE_FORMAT (mode);
2505 gcc_assert (fmt);
2506 memset (r, 0, sizeof (*r));
2508 if (fmt->b == 10)
2509 decimal_real_maxval (r, sign, mode);
2510 else
2512 r->cl = rvc_normal;
2513 r->sign = sign;
2514 SET_REAL_EXP (r, fmt->emax);
2516 np2 = SIGNIFICAND_BITS - fmt->p;
2517 memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2518 clear_significand_below (r, np2);
2520 if (fmt->pnan < fmt->p)
2521 /* This is an IBM extended double format made up of two IEEE
2522 doubles. The value of the long double is the sum of the
2523 values of the two parts. The most significant part is
2524 required to be the value of the long double rounded to the
2525 nearest double. Rounding means we need a slightly smaller
2526 value for LDBL_MAX. */
2527 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1);
2531 /* Fills R with 2**N. */
2533 void
2534 real_2expN (REAL_VALUE_TYPE *r, int n, enum machine_mode fmode)
2536 memset (r, 0, sizeof (*r));
2538 n++;
2539 if (n > MAX_EXP)
2540 r->cl = rvc_inf;
2541 else if (n < -MAX_EXP)
2543 else
2545 r->cl = rvc_normal;
2546 SET_REAL_EXP (r, n);
2547 r->sig[SIGSZ-1] = SIG_MSB;
2549 if (DECIMAL_FLOAT_MODE_P (fmode))
2550 decimal_real_convert (r, fmode, r);
2554 static void
2555 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2557 int p2, np2, i, w;
2558 int emin2m1, emax2;
2559 bool round_up = false;
2561 if (r->decimal)
2563 if (fmt->b == 10)
2565 decimal_round_for_format (fmt, r);
2566 return;
2568 /* FIXME. We can come here via fp_easy_constant
2569 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2570 investigated whether this convert needs to be here, or
2571 something else is missing. */
2572 decimal_real_convert (r, DFmode, r);
2575 p2 = fmt->p;
2576 emin2m1 = fmt->emin - 1;
2577 emax2 = fmt->emax;
2579 np2 = SIGNIFICAND_BITS - p2;
2580 switch (r->cl)
2582 underflow:
2583 get_zero (r, r->sign);
2584 case rvc_zero:
2585 if (!fmt->has_signed_zero)
2586 r->sign = 0;
2587 return;
2589 overflow:
2590 get_inf (r, r->sign);
2591 case rvc_inf:
2592 return;
2594 case rvc_nan:
2595 clear_significand_below (r, np2);
2596 return;
2598 case rvc_normal:
2599 break;
2601 default:
2602 gcc_unreachable ();
2605 /* Check the range of the exponent. If we're out of range,
2606 either underflow or overflow. */
2607 if (REAL_EXP (r) > emax2)
2608 goto overflow;
2609 else if (REAL_EXP (r) <= emin2m1)
2611 int diff;
2613 if (!fmt->has_denorm)
2615 /* Don't underflow completely until we've had a chance to round. */
2616 if (REAL_EXP (r) < emin2m1)
2617 goto underflow;
2619 else
2621 diff = emin2m1 - REAL_EXP (r) + 1;
2622 if (diff > p2)
2623 goto underflow;
2625 /* De-normalize the significand. */
2626 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2627 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2631 if (!fmt->round_towards_zero)
2633 /* There are P2 true significand bits, followed by one guard bit,
2634 followed by one sticky bit, followed by stuff. Fold nonzero
2635 stuff into the sticky bit. */
2636 unsigned long sticky;
2637 bool guard, lsb;
2639 sticky = 0;
2640 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2641 sticky |= r->sig[i];
2642 sticky |= r->sig[w]
2643 & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2645 guard = test_significand_bit (r, np2 - 1);
2646 lsb = test_significand_bit (r, np2);
2648 /* Round to even. */
2649 round_up = guard && (sticky || lsb);
2652 if (round_up)
2654 REAL_VALUE_TYPE u;
2655 get_zero (&u, 0);
2656 set_significand_bit (&u, np2);
2658 if (add_significands (r, r, &u))
2660 /* Overflow. Means the significand had been all ones, and
2661 is now all zeros. Need to increase the exponent, and
2662 possibly re-normalize it. */
2663 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2664 if (REAL_EXP (r) > emax2)
2665 goto overflow;
2666 r->sig[SIGSZ-1] = SIG_MSB;
2670 /* Catch underflow that we deferred until after rounding. */
2671 if (REAL_EXP (r) <= emin2m1)
2672 goto underflow;
2674 /* Clear out trailing garbage. */
2675 clear_significand_below (r, np2);
2678 /* Extend or truncate to a new mode. */
2680 void
2681 real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
2682 const REAL_VALUE_TYPE *a)
2684 const struct real_format *fmt;
2686 fmt = REAL_MODE_FORMAT (mode);
2687 gcc_assert (fmt);
2689 *r = *a;
2691 if (a->decimal || fmt->b == 10)
2692 decimal_real_convert (r, mode, a);
2694 round_for_format (fmt, r);
2696 /* round_for_format de-normalizes denormals. Undo just that part. */
2697 if (r->cl == rvc_normal)
2698 normalize (r);
2701 /* Legacy. Likewise, except return the struct directly. */
2703 REAL_VALUE_TYPE
2704 real_value_truncate (enum machine_mode mode, REAL_VALUE_TYPE a)
2706 REAL_VALUE_TYPE r;
2707 real_convert (&r, mode, &a);
2708 return r;
2711 /* Return true if truncating to MODE is exact. */
2713 bool
2714 exact_real_truncate (enum machine_mode mode, const REAL_VALUE_TYPE *a)
2716 const struct real_format *fmt;
2717 REAL_VALUE_TYPE t;
2718 int emin2m1;
2720 fmt = REAL_MODE_FORMAT (mode);
2721 gcc_assert (fmt);
2723 /* Don't allow conversion to denormals. */
2724 emin2m1 = fmt->emin - 1;
2725 if (REAL_EXP (a) <= emin2m1)
2726 return false;
2728 /* After conversion to the new mode, the value must be identical. */
2729 real_convert (&t, mode, a);
2730 return real_identical (&t, a);
2733 /* Write R to the given target format. Place the words of the result
2734 in target word order in BUF. There are always 32 bits in each
2735 long, no matter the size of the host long.
2737 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2739 long
2740 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
2741 const struct real_format *fmt)
2743 REAL_VALUE_TYPE r;
2744 long buf1;
2746 r = *r_orig;
2747 round_for_format (fmt, &r);
2749 if (!buf)
2750 buf = &buf1;
2751 (*fmt->encode) (fmt, buf, &r);
2753 return *buf;
2756 /* Similar, but look up the format from MODE. */
2758 long
2759 real_to_target (long *buf, const REAL_VALUE_TYPE *r, enum machine_mode mode)
2761 const struct real_format *fmt;
2763 fmt = REAL_MODE_FORMAT (mode);
2764 gcc_assert (fmt);
2766 return real_to_target_fmt (buf, r, fmt);
2769 /* Read R from the given target format. Read the words of the result
2770 in target word order in BUF. There are always 32 bits in each
2771 long, no matter the size of the host long. */
2773 void
2774 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
2775 const struct real_format *fmt)
2777 (*fmt->decode) (fmt, r, buf);
2780 /* Similar, but look up the format from MODE. */
2782 void
2783 real_from_target (REAL_VALUE_TYPE *r, const long *buf, enum machine_mode mode)
2785 const struct real_format *fmt;
2787 fmt = REAL_MODE_FORMAT (mode);
2788 gcc_assert (fmt);
2790 (*fmt->decode) (fmt, r, buf);
2793 /* Return the number of bits of the largest binary value that the
2794 significand of MODE will hold. */
2795 /* ??? Legacy. Should get access to real_format directly. */
2798 significand_size (enum machine_mode mode)
2800 const struct real_format *fmt;
2802 fmt = REAL_MODE_FORMAT (mode);
2803 if (fmt == NULL)
2804 return 0;
2806 if (fmt->b == 10)
2808 /* Return the size in bits of the largest binary value that can be
2809 held by the decimal coefficient for this mode. This is one more
2810 than the number of bits required to hold the largest coefficient
2811 of this mode. */
2812 double log2_10 = 3.3219281;
2813 return fmt->p * log2_10;
2815 return fmt->p;
2818 /* Return a hash value for the given real value. */
2819 /* ??? The "unsigned int" return value is intended to be hashval_t,
2820 but I didn't want to pull hashtab.h into real.h. */
2822 unsigned int
2823 real_hash (const REAL_VALUE_TYPE *r)
2825 unsigned int h;
2826 size_t i;
2828 h = r->cl | (r->sign << 2);
2829 switch (r->cl)
2831 case rvc_zero:
2832 case rvc_inf:
2833 return h;
2835 case rvc_normal:
2836 h |= REAL_EXP (r) << 3;
2837 break;
2839 case rvc_nan:
2840 if (r->signalling)
2841 h ^= (unsigned int)-1;
2842 if (r->canonical)
2843 return h;
2844 break;
2846 default:
2847 gcc_unreachable ();
2850 if (sizeof(unsigned long) > sizeof(unsigned int))
2851 for (i = 0; i < SIGSZ; ++i)
2853 unsigned long s = r->sig[i];
2854 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2856 else
2857 for (i = 0; i < SIGSZ; ++i)
2858 h ^= r->sig[i];
2860 return h;
2863 /* IEEE single-precision format. */
2865 static void encode_ieee_single (const struct real_format *fmt,
2866 long *, const REAL_VALUE_TYPE *);
2867 static void decode_ieee_single (const struct real_format *,
2868 REAL_VALUE_TYPE *, const long *);
2870 static void
2871 encode_ieee_single (const struct real_format *fmt, long *buf,
2872 const REAL_VALUE_TYPE *r)
2874 unsigned long image, sig, exp;
2875 unsigned long sign = r->sign;
2876 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2878 image = sign << 31;
2879 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2881 switch (r->cl)
2883 case rvc_zero:
2884 break;
2886 case rvc_inf:
2887 if (fmt->has_inf)
2888 image |= 255 << 23;
2889 else
2890 image |= 0x7fffffff;
2891 break;
2893 case rvc_nan:
2894 if (fmt->has_nans)
2896 if (r->canonical)
2897 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
2898 if (r->signalling == fmt->qnan_msb_set)
2899 sig &= ~(1 << 22);
2900 else
2901 sig |= 1 << 22;
2902 if (sig == 0)
2903 sig = 1 << 21;
2905 image |= 255 << 23;
2906 image |= sig;
2908 else
2909 image |= 0x7fffffff;
2910 break;
2912 case rvc_normal:
2913 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2914 whereas the intermediate representation is 0.F x 2**exp.
2915 Which means we're off by one. */
2916 if (denormal)
2917 exp = 0;
2918 else
2919 exp = REAL_EXP (r) + 127 - 1;
2920 image |= exp << 23;
2921 image |= sig;
2922 break;
2924 default:
2925 gcc_unreachable ();
2928 buf[0] = image;
2931 static void
2932 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2933 const long *buf)
2935 unsigned long image = buf[0] & 0xffffffff;
2936 bool sign = (image >> 31) & 1;
2937 int exp = (image >> 23) & 0xff;
2939 memset (r, 0, sizeof (*r));
2940 image <<= HOST_BITS_PER_LONG - 24;
2941 image &= ~SIG_MSB;
2943 if (exp == 0)
2945 if (image && fmt->has_denorm)
2947 r->cl = rvc_normal;
2948 r->sign = sign;
2949 SET_REAL_EXP (r, -126);
2950 r->sig[SIGSZ-1] = image << 1;
2951 normalize (r);
2953 else if (fmt->has_signed_zero)
2954 r->sign = sign;
2956 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2958 if (image)
2960 r->cl = rvc_nan;
2961 r->sign = sign;
2962 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
2963 ^ fmt->qnan_msb_set);
2964 r->sig[SIGSZ-1] = image;
2966 else
2968 r->cl = rvc_inf;
2969 r->sign = sign;
2972 else
2974 r->cl = rvc_normal;
2975 r->sign = sign;
2976 SET_REAL_EXP (r, exp - 127 + 1);
2977 r->sig[SIGSZ-1] = image | SIG_MSB;
2981 const struct real_format ieee_single_format =
2983 encode_ieee_single,
2984 decode_ieee_single,
2988 -125,
2989 128,
2992 false,
2993 true,
2994 true,
2995 true,
2996 true,
2997 true,
2998 true,
2999 false
3002 const struct real_format mips_single_format =
3004 encode_ieee_single,
3005 decode_ieee_single,
3009 -125,
3010 128,
3013 false,
3014 true,
3015 true,
3016 true,
3017 true,
3018 true,
3019 false,
3020 true
3023 const struct real_format motorola_single_format =
3025 encode_ieee_single,
3026 decode_ieee_single,
3030 -125,
3031 128,
3034 false,
3035 true,
3036 true,
3037 true,
3038 true,
3039 true,
3040 true,
3041 true
3044 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3045 single precision with the following differences:
3046 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3047 are generated.
3048 - NaNs are not supported.
3049 - The range of non-zero numbers in binary is
3050 (001)[1.]000...000 to (255)[1.]111...111.
3051 - Denormals can be represented, but are treated as +0.0 when
3052 used as an operand and are never generated as a result.
3053 - -0.0 can be represented, but a zero result is always +0.0.
3054 - the only supported rounding mode is trunction (towards zero). */
3055 const struct real_format spu_single_format =
3057 encode_ieee_single,
3058 decode_ieee_single,
3062 -125,
3063 129,
3066 true,
3067 false,
3068 false,
3069 false,
3070 true,
3071 true,
3072 false,
3073 false
3076 /* IEEE double-precision format. */
3078 static void encode_ieee_double (const struct real_format *fmt,
3079 long *, const REAL_VALUE_TYPE *);
3080 static void decode_ieee_double (const struct real_format *,
3081 REAL_VALUE_TYPE *, const long *);
3083 static void
3084 encode_ieee_double (const struct real_format *fmt, long *buf,
3085 const REAL_VALUE_TYPE *r)
3087 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
3088 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3090 image_hi = r->sign << 31;
3091 image_lo = 0;
3093 if (HOST_BITS_PER_LONG == 64)
3095 sig_hi = r->sig[SIGSZ-1];
3096 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
3097 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
3099 else
3101 sig_hi = r->sig[SIGSZ-1];
3102 sig_lo = r->sig[SIGSZ-2];
3103 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
3104 sig_hi = (sig_hi >> 11) & 0xfffff;
3107 switch (r->cl)
3109 case rvc_zero:
3110 break;
3112 case rvc_inf:
3113 if (fmt->has_inf)
3114 image_hi |= 2047 << 20;
3115 else
3117 image_hi |= 0x7fffffff;
3118 image_lo = 0xffffffff;
3120 break;
3122 case rvc_nan:
3123 if (fmt->has_nans)
3125 if (r->canonical)
3127 if (fmt->canonical_nan_lsbs_set)
3129 sig_hi = (1 << 19) - 1;
3130 sig_lo = 0xffffffff;
3132 else
3134 sig_hi = 0;
3135 sig_lo = 0;
3138 if (r->signalling == fmt->qnan_msb_set)
3139 sig_hi &= ~(1 << 19);
3140 else
3141 sig_hi |= 1 << 19;
3142 if (sig_hi == 0 && sig_lo == 0)
3143 sig_hi = 1 << 18;
3145 image_hi |= 2047 << 20;
3146 image_hi |= sig_hi;
3147 image_lo = sig_lo;
3149 else
3151 image_hi |= 0x7fffffff;
3152 image_lo = 0xffffffff;
3154 break;
3156 case rvc_normal:
3157 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3158 whereas the intermediate representation is 0.F x 2**exp.
3159 Which means we're off by one. */
3160 if (denormal)
3161 exp = 0;
3162 else
3163 exp = REAL_EXP (r) + 1023 - 1;
3164 image_hi |= exp << 20;
3165 image_hi |= sig_hi;
3166 image_lo = sig_lo;
3167 break;
3169 default:
3170 gcc_unreachable ();
3173 if (FLOAT_WORDS_BIG_ENDIAN)
3174 buf[0] = image_hi, buf[1] = image_lo;
3175 else
3176 buf[0] = image_lo, buf[1] = image_hi;
3179 static void
3180 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3181 const long *buf)
3183 unsigned long image_hi, image_lo;
3184 bool sign;
3185 int exp;
3187 if (FLOAT_WORDS_BIG_ENDIAN)
3188 image_hi = buf[0], image_lo = buf[1];
3189 else
3190 image_lo = buf[0], image_hi = buf[1];
3191 image_lo &= 0xffffffff;
3192 image_hi &= 0xffffffff;
3194 sign = (image_hi >> 31) & 1;
3195 exp = (image_hi >> 20) & 0x7ff;
3197 memset (r, 0, sizeof (*r));
3199 image_hi <<= 32 - 21;
3200 image_hi |= image_lo >> 21;
3201 image_hi &= 0x7fffffff;
3202 image_lo <<= 32 - 21;
3204 if (exp == 0)
3206 if ((image_hi || image_lo) && fmt->has_denorm)
3208 r->cl = rvc_normal;
3209 r->sign = sign;
3210 SET_REAL_EXP (r, -1022);
3211 if (HOST_BITS_PER_LONG == 32)
3213 image_hi = (image_hi << 1) | (image_lo >> 31);
3214 image_lo <<= 1;
3215 r->sig[SIGSZ-1] = image_hi;
3216 r->sig[SIGSZ-2] = image_lo;
3218 else
3220 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
3221 r->sig[SIGSZ-1] = image_hi;
3223 normalize (r);
3225 else if (fmt->has_signed_zero)
3226 r->sign = sign;
3228 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
3230 if (image_hi || image_lo)
3232 r->cl = rvc_nan;
3233 r->sign = sign;
3234 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3235 if (HOST_BITS_PER_LONG == 32)
3237 r->sig[SIGSZ-1] = image_hi;
3238 r->sig[SIGSZ-2] = image_lo;
3240 else
3241 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3243 else
3245 r->cl = rvc_inf;
3246 r->sign = sign;
3249 else
3251 r->cl = rvc_normal;
3252 r->sign = sign;
3253 SET_REAL_EXP (r, exp - 1023 + 1);
3254 if (HOST_BITS_PER_LONG == 32)
3256 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3257 r->sig[SIGSZ-2] = image_lo;
3259 else
3260 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3264 const struct real_format ieee_double_format =
3266 encode_ieee_double,
3267 decode_ieee_double,
3271 -1021,
3272 1024,
3275 false,
3276 true,
3277 true,
3278 true,
3279 true,
3280 true,
3281 true,
3282 false
3285 const struct real_format mips_double_format =
3287 encode_ieee_double,
3288 decode_ieee_double,
3292 -1021,
3293 1024,
3296 false,
3297 true,
3298 true,
3299 true,
3300 true,
3301 true,
3302 false,
3303 true
3306 const struct real_format motorola_double_format =
3308 encode_ieee_double,
3309 decode_ieee_double,
3313 -1021,
3314 1024,
3317 false,
3318 true,
3319 true,
3320 true,
3321 true,
3322 true,
3323 true,
3324 true
3327 /* IEEE extended real format. This comes in three flavors: Intel's as
3328 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3329 12- and 16-byte images may be big- or little endian; Motorola's is
3330 always big endian. */
3332 /* Helper subroutine which converts from the internal format to the
3333 12-byte little-endian Intel format. Functions below adjust this
3334 for the other possible formats. */
3335 static void
3336 encode_ieee_extended (const struct real_format *fmt, long *buf,
3337 const REAL_VALUE_TYPE *r)
3339 unsigned long image_hi, sig_hi, sig_lo;
3340 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3342 image_hi = r->sign << 15;
3343 sig_hi = sig_lo = 0;
3345 switch (r->cl)
3347 case rvc_zero:
3348 break;
3350 case rvc_inf:
3351 if (fmt->has_inf)
3353 image_hi |= 32767;
3355 /* Intel requires the explicit integer bit to be set, otherwise
3356 it considers the value a "pseudo-infinity". Motorola docs
3357 say it doesn't care. */
3358 sig_hi = 0x80000000;
3360 else
3362 image_hi |= 32767;
3363 sig_lo = sig_hi = 0xffffffff;
3365 break;
3367 case rvc_nan:
3368 if (fmt->has_nans)
3370 image_hi |= 32767;
3371 if (r->canonical)
3373 if (fmt->canonical_nan_lsbs_set)
3375 sig_hi = (1 << 30) - 1;
3376 sig_lo = 0xffffffff;
3379 else if (HOST_BITS_PER_LONG == 32)
3381 sig_hi = r->sig[SIGSZ-1];
3382 sig_lo = r->sig[SIGSZ-2];
3384 else
3386 sig_lo = r->sig[SIGSZ-1];
3387 sig_hi = sig_lo >> 31 >> 1;
3388 sig_lo &= 0xffffffff;
3390 if (r->signalling == fmt->qnan_msb_set)
3391 sig_hi &= ~(1 << 30);
3392 else
3393 sig_hi |= 1 << 30;
3394 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3395 sig_hi = 1 << 29;
3397 /* Intel requires the explicit integer bit to be set, otherwise
3398 it considers the value a "pseudo-nan". Motorola docs say it
3399 doesn't care. */
3400 sig_hi |= 0x80000000;
3402 else
3404 image_hi |= 32767;
3405 sig_lo = sig_hi = 0xffffffff;
3407 break;
3409 case rvc_normal:
3411 int exp = REAL_EXP (r);
3413 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3414 whereas the intermediate representation is 0.F x 2**exp.
3415 Which means we're off by one.
3417 Except for Motorola, which consider exp=0 and explicit
3418 integer bit set to continue to be normalized. In theory
3419 this discrepancy has been taken care of by the difference
3420 in fmt->emin in round_for_format. */
3422 if (denormal)
3423 exp = 0;
3424 else
3426 exp += 16383 - 1;
3427 gcc_assert (exp >= 0);
3429 image_hi |= exp;
3431 if (HOST_BITS_PER_LONG == 32)
3433 sig_hi = r->sig[SIGSZ-1];
3434 sig_lo = r->sig[SIGSZ-2];
3436 else
3438 sig_lo = r->sig[SIGSZ-1];
3439 sig_hi = sig_lo >> 31 >> 1;
3440 sig_lo &= 0xffffffff;
3443 break;
3445 default:
3446 gcc_unreachable ();
3449 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3452 /* Convert from the internal format to the 12-byte Motorola format
3453 for an IEEE extended real. */
3454 static void
3455 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3456 const REAL_VALUE_TYPE *r)
3458 long intermed[3];
3459 encode_ieee_extended (fmt, intermed, r);
3461 /* Motorola chips are assumed always to be big-endian. Also, the
3462 padding in a Motorola extended real goes between the exponent and
3463 the mantissa. At this point the mantissa is entirely within
3464 elements 0 and 1 of intermed, and the exponent entirely within
3465 element 2, so all we have to do is swap the order around, and
3466 shift element 2 left 16 bits. */
3467 buf[0] = intermed[2] << 16;
3468 buf[1] = intermed[1];
3469 buf[2] = intermed[0];
3472 /* Convert from the internal format to the 12-byte Intel format for
3473 an IEEE extended real. */
3474 static void
3475 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3476 const REAL_VALUE_TYPE *r)
3478 if (FLOAT_WORDS_BIG_ENDIAN)
3480 /* All the padding in an Intel-format extended real goes at the high
3481 end, which in this case is after the mantissa, not the exponent.
3482 Therefore we must shift everything down 16 bits. */
3483 long intermed[3];
3484 encode_ieee_extended (fmt, intermed, r);
3485 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3486 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3487 buf[2] = (intermed[0] << 16);
3489 else
3490 /* encode_ieee_extended produces what we want directly. */
3491 encode_ieee_extended (fmt, buf, r);
3494 /* Convert from the internal format to the 16-byte Intel format for
3495 an IEEE extended real. */
3496 static void
3497 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3498 const REAL_VALUE_TYPE *r)
3500 /* All the padding in an Intel-format extended real goes at the high end. */
3501 encode_ieee_extended_intel_96 (fmt, buf, r);
3502 buf[3] = 0;
3505 /* As above, we have a helper function which converts from 12-byte
3506 little-endian Intel format to internal format. Functions below
3507 adjust for the other possible formats. */
3508 static void
3509 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3510 const long *buf)
3512 unsigned long image_hi, sig_hi, sig_lo;
3513 bool sign;
3514 int exp;
3516 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3517 sig_lo &= 0xffffffff;
3518 sig_hi &= 0xffffffff;
3519 image_hi &= 0xffffffff;
3521 sign = (image_hi >> 15) & 1;
3522 exp = image_hi & 0x7fff;
3524 memset (r, 0, sizeof (*r));
3526 if (exp == 0)
3528 if ((sig_hi || sig_lo) && fmt->has_denorm)
3530 r->cl = rvc_normal;
3531 r->sign = sign;
3533 /* When the IEEE format contains a hidden bit, we know that
3534 it's zero at this point, and so shift up the significand
3535 and decrease the exponent to match. In this case, Motorola
3536 defines the explicit integer bit to be valid, so we don't
3537 know whether the msb is set or not. */
3538 SET_REAL_EXP (r, fmt->emin);
3539 if (HOST_BITS_PER_LONG == 32)
3541 r->sig[SIGSZ-1] = sig_hi;
3542 r->sig[SIGSZ-2] = sig_lo;
3544 else
3545 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3547 normalize (r);
3549 else if (fmt->has_signed_zero)
3550 r->sign = sign;
3552 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3554 /* See above re "pseudo-infinities" and "pseudo-nans".
3555 Short summary is that the MSB will likely always be
3556 set, and that we don't care about it. */
3557 sig_hi &= 0x7fffffff;
3559 if (sig_hi || sig_lo)
3561 r->cl = rvc_nan;
3562 r->sign = sign;
3563 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3564 if (HOST_BITS_PER_LONG == 32)
3566 r->sig[SIGSZ-1] = sig_hi;
3567 r->sig[SIGSZ-2] = sig_lo;
3569 else
3570 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3572 else
3574 r->cl = rvc_inf;
3575 r->sign = sign;
3578 else
3580 r->cl = rvc_normal;
3581 r->sign = sign;
3582 SET_REAL_EXP (r, exp - 16383 + 1);
3583 if (HOST_BITS_PER_LONG == 32)
3585 r->sig[SIGSZ-1] = sig_hi;
3586 r->sig[SIGSZ-2] = sig_lo;
3588 else
3589 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3593 /* Convert from the internal format to the 12-byte Motorola format
3594 for an IEEE extended real. */
3595 static void
3596 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3597 const long *buf)
3599 long intermed[3];
3601 /* Motorola chips are assumed always to be big-endian. Also, the
3602 padding in a Motorola extended real goes between the exponent and
3603 the mantissa; remove it. */
3604 intermed[0] = buf[2];
3605 intermed[1] = buf[1];
3606 intermed[2] = (unsigned long)buf[0] >> 16;
3608 decode_ieee_extended (fmt, r, intermed);
3611 /* Convert from the internal format to the 12-byte Intel format for
3612 an IEEE extended real. */
3613 static void
3614 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3615 const long *buf)
3617 if (FLOAT_WORDS_BIG_ENDIAN)
3619 /* All the padding in an Intel-format extended real goes at the high
3620 end, which in this case is after the mantissa, not the exponent.
3621 Therefore we must shift everything up 16 bits. */
3622 long intermed[3];
3624 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3625 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3626 intermed[2] = ((unsigned long)buf[0] >> 16);
3628 decode_ieee_extended (fmt, r, intermed);
3630 else
3631 /* decode_ieee_extended produces what we want directly. */
3632 decode_ieee_extended (fmt, r, buf);
3635 /* Convert from the internal format to the 16-byte Intel format for
3636 an IEEE extended real. */
3637 static void
3638 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3639 const long *buf)
3641 /* All the padding in an Intel-format extended real goes at the high end. */
3642 decode_ieee_extended_intel_96 (fmt, r, buf);
3645 const struct real_format ieee_extended_motorola_format =
3647 encode_ieee_extended_motorola,
3648 decode_ieee_extended_motorola,
3652 -16382,
3653 16384,
3656 false,
3657 true,
3658 true,
3659 true,
3660 true,
3661 true,
3662 true,
3663 true
3666 const struct real_format ieee_extended_intel_96_format =
3668 encode_ieee_extended_intel_96,
3669 decode_ieee_extended_intel_96,
3673 -16381,
3674 16384,
3677 false,
3678 true,
3679 true,
3680 true,
3681 true,
3682 true,
3683 true,
3684 false
3687 const struct real_format ieee_extended_intel_128_format =
3689 encode_ieee_extended_intel_128,
3690 decode_ieee_extended_intel_128,
3694 -16381,
3695 16384,
3698 false,
3699 true,
3700 true,
3701 true,
3702 true,
3703 true,
3704 true,
3705 false
3708 /* The following caters to i386 systems that set the rounding precision
3709 to 53 bits instead of 64, e.g. FreeBSD. */
3710 const struct real_format ieee_extended_intel_96_round_53_format =
3712 encode_ieee_extended_intel_96,
3713 decode_ieee_extended_intel_96,
3717 -16381,
3718 16384,
3721 false,
3722 true,
3723 true,
3724 true,
3725 true,
3726 true,
3727 true,
3728 false
3731 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3732 numbers whose sum is equal to the extended precision value. The number
3733 with greater magnitude is first. This format has the same magnitude
3734 range as an IEEE double precision value, but effectively 106 bits of
3735 significand precision. Infinity and NaN are represented by their IEEE
3736 double precision value stored in the first number, the second number is
3737 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3739 static void encode_ibm_extended (const struct real_format *fmt,
3740 long *, const REAL_VALUE_TYPE *);
3741 static void decode_ibm_extended (const struct real_format *,
3742 REAL_VALUE_TYPE *, const long *);
3744 static void
3745 encode_ibm_extended (const struct real_format *fmt, long *buf,
3746 const REAL_VALUE_TYPE *r)
3748 REAL_VALUE_TYPE u, normr, v;
3749 const struct real_format *base_fmt;
3751 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3753 /* Renormalize R before doing any arithmetic on it. */
3754 normr = *r;
3755 if (normr.cl == rvc_normal)
3756 normalize (&normr);
3758 /* u = IEEE double precision portion of significand. */
3759 u = normr;
3760 round_for_format (base_fmt, &u);
3761 encode_ieee_double (base_fmt, &buf[0], &u);
3763 if (u.cl == rvc_normal)
3765 do_add (&v, &normr, &u, 1);
3766 /* Call round_for_format since we might need to denormalize. */
3767 round_for_format (base_fmt, &v);
3768 encode_ieee_double (base_fmt, &buf[2], &v);
3770 else
3772 /* Inf, NaN, 0 are all representable as doubles, so the
3773 least-significant part can be 0.0. */
3774 buf[2] = 0;
3775 buf[3] = 0;
3779 static void
3780 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3781 const long *buf)
3783 REAL_VALUE_TYPE u, v;
3784 const struct real_format *base_fmt;
3786 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3787 decode_ieee_double (base_fmt, &u, &buf[0]);
3789 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3791 decode_ieee_double (base_fmt, &v, &buf[2]);
3792 do_add (r, &u, &v, 0);
3794 else
3795 *r = u;
3798 const struct real_format ibm_extended_format =
3800 encode_ibm_extended,
3801 decode_ibm_extended,
3803 53 + 53,
3805 -1021 + 53,
3806 1024,
3807 127,
3809 false,
3810 true,
3811 true,
3812 true,
3813 true,
3814 true,
3815 true,
3816 false
3819 const struct real_format mips_extended_format =
3821 encode_ibm_extended,
3822 decode_ibm_extended,
3824 53 + 53,
3826 -1021 + 53,
3827 1024,
3828 127,
3830 false,
3831 true,
3832 true,
3833 true,
3834 true,
3835 true,
3836 false,
3837 true
3841 /* IEEE quad precision format. */
3843 static void encode_ieee_quad (const struct real_format *fmt,
3844 long *, const REAL_VALUE_TYPE *);
3845 static void decode_ieee_quad (const struct real_format *,
3846 REAL_VALUE_TYPE *, const long *);
3848 static void
3849 encode_ieee_quad (const struct real_format *fmt, long *buf,
3850 const REAL_VALUE_TYPE *r)
3852 unsigned long image3, image2, image1, image0, exp;
3853 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3854 REAL_VALUE_TYPE u;
3856 image3 = r->sign << 31;
3857 image2 = 0;
3858 image1 = 0;
3859 image0 = 0;
3861 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3863 switch (r->cl)
3865 case rvc_zero:
3866 break;
3868 case rvc_inf:
3869 if (fmt->has_inf)
3870 image3 |= 32767 << 16;
3871 else
3873 image3 |= 0x7fffffff;
3874 image2 = 0xffffffff;
3875 image1 = 0xffffffff;
3876 image0 = 0xffffffff;
3878 break;
3880 case rvc_nan:
3881 if (fmt->has_nans)
3883 image3 |= 32767 << 16;
3885 if (r->canonical)
3887 if (fmt->canonical_nan_lsbs_set)
3889 image3 |= 0x7fff;
3890 image2 = image1 = image0 = 0xffffffff;
3893 else if (HOST_BITS_PER_LONG == 32)
3895 image0 = u.sig[0];
3896 image1 = u.sig[1];
3897 image2 = u.sig[2];
3898 image3 |= u.sig[3] & 0xffff;
3900 else
3902 image0 = u.sig[0];
3903 image1 = image0 >> 31 >> 1;
3904 image2 = u.sig[1];
3905 image3 |= (image2 >> 31 >> 1) & 0xffff;
3906 image0 &= 0xffffffff;
3907 image2 &= 0xffffffff;
3909 if (r->signalling == fmt->qnan_msb_set)
3910 image3 &= ~0x8000;
3911 else
3912 image3 |= 0x8000;
3913 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3914 image3 |= 0x4000;
3916 else
3918 image3 |= 0x7fffffff;
3919 image2 = 0xffffffff;
3920 image1 = 0xffffffff;
3921 image0 = 0xffffffff;
3923 break;
3925 case rvc_normal:
3926 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3927 whereas the intermediate representation is 0.F x 2**exp.
3928 Which means we're off by one. */
3929 if (denormal)
3930 exp = 0;
3931 else
3932 exp = REAL_EXP (r) + 16383 - 1;
3933 image3 |= exp << 16;
3935 if (HOST_BITS_PER_LONG == 32)
3937 image0 = u.sig[0];
3938 image1 = u.sig[1];
3939 image2 = u.sig[2];
3940 image3 |= u.sig[3] & 0xffff;
3942 else
3944 image0 = u.sig[0];
3945 image1 = image0 >> 31 >> 1;
3946 image2 = u.sig[1];
3947 image3 |= (image2 >> 31 >> 1) & 0xffff;
3948 image0 &= 0xffffffff;
3949 image2 &= 0xffffffff;
3951 break;
3953 default:
3954 gcc_unreachable ();
3957 if (FLOAT_WORDS_BIG_ENDIAN)
3959 buf[0] = image3;
3960 buf[1] = image2;
3961 buf[2] = image1;
3962 buf[3] = image0;
3964 else
3966 buf[0] = image0;
3967 buf[1] = image1;
3968 buf[2] = image2;
3969 buf[3] = image3;
3973 static void
3974 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3975 const long *buf)
3977 unsigned long image3, image2, image1, image0;
3978 bool sign;
3979 int exp;
3981 if (FLOAT_WORDS_BIG_ENDIAN)
3983 image3 = buf[0];
3984 image2 = buf[1];
3985 image1 = buf[2];
3986 image0 = buf[3];
3988 else
3990 image0 = buf[0];
3991 image1 = buf[1];
3992 image2 = buf[2];
3993 image3 = buf[3];
3995 image0 &= 0xffffffff;
3996 image1 &= 0xffffffff;
3997 image2 &= 0xffffffff;
3999 sign = (image3 >> 31) & 1;
4000 exp = (image3 >> 16) & 0x7fff;
4001 image3 &= 0xffff;
4003 memset (r, 0, sizeof (*r));
4005 if (exp == 0)
4007 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
4009 r->cl = rvc_normal;
4010 r->sign = sign;
4012 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
4013 if (HOST_BITS_PER_LONG == 32)
4015 r->sig[0] = image0;
4016 r->sig[1] = image1;
4017 r->sig[2] = image2;
4018 r->sig[3] = image3;
4020 else
4022 r->sig[0] = (image1 << 31 << 1) | image0;
4023 r->sig[1] = (image3 << 31 << 1) | image2;
4026 normalize (r);
4028 else if (fmt->has_signed_zero)
4029 r->sign = sign;
4031 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
4033 if (image3 | image2 | image1 | image0)
4035 r->cl = rvc_nan;
4036 r->sign = sign;
4037 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
4039 if (HOST_BITS_PER_LONG == 32)
4041 r->sig[0] = image0;
4042 r->sig[1] = image1;
4043 r->sig[2] = image2;
4044 r->sig[3] = image3;
4046 else
4048 r->sig[0] = (image1 << 31 << 1) | image0;
4049 r->sig[1] = (image3 << 31 << 1) | image2;
4051 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4053 else
4055 r->cl = rvc_inf;
4056 r->sign = sign;
4059 else
4061 r->cl = rvc_normal;
4062 r->sign = sign;
4063 SET_REAL_EXP (r, exp - 16383 + 1);
4065 if (HOST_BITS_PER_LONG == 32)
4067 r->sig[0] = image0;
4068 r->sig[1] = image1;
4069 r->sig[2] = image2;
4070 r->sig[3] = image3;
4072 else
4074 r->sig[0] = (image1 << 31 << 1) | image0;
4075 r->sig[1] = (image3 << 31 << 1) | image2;
4077 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4078 r->sig[SIGSZ-1] |= SIG_MSB;
4082 const struct real_format ieee_quad_format =
4084 encode_ieee_quad,
4085 decode_ieee_quad,
4087 113,
4088 113,
4089 -16381,
4090 16384,
4091 127,
4092 127,
4093 false,
4094 true,
4095 true,
4096 true,
4097 true,
4098 true,
4099 true,
4100 false
4103 const struct real_format mips_quad_format =
4105 encode_ieee_quad,
4106 decode_ieee_quad,
4108 113,
4109 113,
4110 -16381,
4111 16384,
4112 127,
4113 127,
4114 false,
4115 true,
4116 true,
4117 true,
4118 true,
4119 true,
4120 false,
4121 true
4124 /* Descriptions of VAX floating point formats can be found beginning at
4126 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4128 The thing to remember is that they're almost IEEE, except for word
4129 order, exponent bias, and the lack of infinities, nans, and denormals.
4131 We don't implement the H_floating format here, simply because neither
4132 the VAX or Alpha ports use it. */
4134 static void encode_vax_f (const struct real_format *fmt,
4135 long *, const REAL_VALUE_TYPE *);
4136 static void decode_vax_f (const struct real_format *,
4137 REAL_VALUE_TYPE *, const long *);
4138 static void encode_vax_d (const struct real_format *fmt,
4139 long *, const REAL_VALUE_TYPE *);
4140 static void decode_vax_d (const struct real_format *,
4141 REAL_VALUE_TYPE *, const long *);
4142 static void encode_vax_g (const struct real_format *fmt,
4143 long *, const REAL_VALUE_TYPE *);
4144 static void decode_vax_g (const struct real_format *,
4145 REAL_VALUE_TYPE *, const long *);
4147 static void
4148 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4149 const REAL_VALUE_TYPE *r)
4151 unsigned long sign, exp, sig, image;
4153 sign = r->sign << 15;
4155 switch (r->cl)
4157 case rvc_zero:
4158 image = 0;
4159 break;
4161 case rvc_inf:
4162 case rvc_nan:
4163 image = 0xffff7fff | sign;
4164 break;
4166 case rvc_normal:
4167 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4168 exp = REAL_EXP (r) + 128;
4170 image = (sig << 16) & 0xffff0000;
4171 image |= sign;
4172 image |= exp << 7;
4173 image |= sig >> 16;
4174 break;
4176 default:
4177 gcc_unreachable ();
4180 buf[0] = image;
4183 static void
4184 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
4185 REAL_VALUE_TYPE *r, const long *buf)
4187 unsigned long image = buf[0] & 0xffffffff;
4188 int exp = (image >> 7) & 0xff;
4190 memset (r, 0, sizeof (*r));
4192 if (exp != 0)
4194 r->cl = rvc_normal;
4195 r->sign = (image >> 15) & 1;
4196 SET_REAL_EXP (r, exp - 128);
4198 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
4199 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4203 static void
4204 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4205 const REAL_VALUE_TYPE *r)
4207 unsigned long image0, image1, sign = r->sign << 15;
4209 switch (r->cl)
4211 case rvc_zero:
4212 image0 = image1 = 0;
4213 break;
4215 case rvc_inf:
4216 case rvc_nan:
4217 image0 = 0xffff7fff | sign;
4218 image1 = 0xffffffff;
4219 break;
4221 case rvc_normal:
4222 /* Extract the significand into straight hi:lo. */
4223 if (HOST_BITS_PER_LONG == 64)
4225 image0 = r->sig[SIGSZ-1];
4226 image1 = (image0 >> (64 - 56)) & 0xffffffff;
4227 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
4229 else
4231 image0 = r->sig[SIGSZ-1];
4232 image1 = r->sig[SIGSZ-2];
4233 image1 = (image0 << 24) | (image1 >> 8);
4234 image0 = (image0 >> 8) & 0xffffff;
4237 /* Rearrange the half-words of the significand to match the
4238 external format. */
4239 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
4240 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4242 /* Add the sign and exponent. */
4243 image0 |= sign;
4244 image0 |= (REAL_EXP (r) + 128) << 7;
4245 break;
4247 default:
4248 gcc_unreachable ();
4251 if (FLOAT_WORDS_BIG_ENDIAN)
4252 buf[0] = image1, buf[1] = image0;
4253 else
4254 buf[0] = image0, buf[1] = image1;
4257 static void
4258 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
4259 REAL_VALUE_TYPE *r, const long *buf)
4261 unsigned long image0, image1;
4262 int exp;
4264 if (FLOAT_WORDS_BIG_ENDIAN)
4265 image1 = buf[0], image0 = buf[1];
4266 else
4267 image0 = buf[0], image1 = buf[1];
4268 image0 &= 0xffffffff;
4269 image1 &= 0xffffffff;
4271 exp = (image0 >> 7) & 0xff;
4273 memset (r, 0, sizeof (*r));
4275 if (exp != 0)
4277 r->cl = rvc_normal;
4278 r->sign = (image0 >> 15) & 1;
4279 SET_REAL_EXP (r, exp - 128);
4281 /* Rearrange the half-words of the external format into
4282 proper ascending order. */
4283 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4284 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4286 if (HOST_BITS_PER_LONG == 64)
4288 image0 = (image0 << 31 << 1) | image1;
4289 image0 <<= 64 - 56;
4290 image0 |= SIG_MSB;
4291 r->sig[SIGSZ-1] = image0;
4293 else
4295 r->sig[SIGSZ-1] = image0;
4296 r->sig[SIGSZ-2] = image1;
4297 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4298 r->sig[SIGSZ-1] |= SIG_MSB;
4303 static void
4304 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4305 const REAL_VALUE_TYPE *r)
4307 unsigned long image0, image1, sign = r->sign << 15;
4309 switch (r->cl)
4311 case rvc_zero:
4312 image0 = image1 = 0;
4313 break;
4315 case rvc_inf:
4316 case rvc_nan:
4317 image0 = 0xffff7fff | sign;
4318 image1 = 0xffffffff;
4319 break;
4321 case rvc_normal:
4322 /* Extract the significand into straight hi:lo. */
4323 if (HOST_BITS_PER_LONG == 64)
4325 image0 = r->sig[SIGSZ-1];
4326 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4327 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4329 else
4331 image0 = r->sig[SIGSZ-1];
4332 image1 = r->sig[SIGSZ-2];
4333 image1 = (image0 << 21) | (image1 >> 11);
4334 image0 = (image0 >> 11) & 0xfffff;
4337 /* Rearrange the half-words of the significand to match the
4338 external format. */
4339 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4340 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4342 /* Add the sign and exponent. */
4343 image0 |= sign;
4344 image0 |= (REAL_EXP (r) + 1024) << 4;
4345 break;
4347 default:
4348 gcc_unreachable ();
4351 if (FLOAT_WORDS_BIG_ENDIAN)
4352 buf[0] = image1, buf[1] = image0;
4353 else
4354 buf[0] = image0, buf[1] = image1;
4357 static void
4358 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4359 REAL_VALUE_TYPE *r, const long *buf)
4361 unsigned long image0, image1;
4362 int exp;
4364 if (FLOAT_WORDS_BIG_ENDIAN)
4365 image1 = buf[0], image0 = buf[1];
4366 else
4367 image0 = buf[0], image1 = buf[1];
4368 image0 &= 0xffffffff;
4369 image1 &= 0xffffffff;
4371 exp = (image0 >> 4) & 0x7ff;
4373 memset (r, 0, sizeof (*r));
4375 if (exp != 0)
4377 r->cl = rvc_normal;
4378 r->sign = (image0 >> 15) & 1;
4379 SET_REAL_EXP (r, exp - 1024);
4381 /* Rearrange the half-words of the external format into
4382 proper ascending order. */
4383 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4384 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4386 if (HOST_BITS_PER_LONG == 64)
4388 image0 = (image0 << 31 << 1) | image1;
4389 image0 <<= 64 - 53;
4390 image0 |= SIG_MSB;
4391 r->sig[SIGSZ-1] = image0;
4393 else
4395 r->sig[SIGSZ-1] = image0;
4396 r->sig[SIGSZ-2] = image1;
4397 lshift_significand (r, r, 64 - 53);
4398 r->sig[SIGSZ-1] |= SIG_MSB;
4403 const struct real_format vax_f_format =
4405 encode_vax_f,
4406 decode_vax_f,
4410 -127,
4411 127,
4414 false,
4415 false,
4416 false,
4417 false,
4418 false,
4419 false,
4420 false,
4421 false
4424 const struct real_format vax_d_format =
4426 encode_vax_d,
4427 decode_vax_d,
4431 -127,
4432 127,
4435 false,
4436 false,
4437 false,
4438 false,
4439 false,
4440 false,
4441 false,
4442 false
4445 const struct real_format vax_g_format =
4447 encode_vax_g,
4448 decode_vax_g,
4452 -1023,
4453 1023,
4456 false,
4457 false,
4458 false,
4459 false,
4460 false,
4461 false,
4462 false,
4463 false
4466 /* Encode real R into a single precision DFP value in BUF. */
4467 static void
4468 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4469 long *buf ATTRIBUTE_UNUSED,
4470 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4472 encode_decimal32 (fmt, buf, r);
4475 /* Decode a single precision DFP value in BUF into a real R. */
4476 static void
4477 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4478 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4479 const long *buf ATTRIBUTE_UNUSED)
4481 decode_decimal32 (fmt, r, buf);
4484 /* Encode real R into a double precision DFP value in BUF. */
4485 static void
4486 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4487 long *buf ATTRIBUTE_UNUSED,
4488 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4490 encode_decimal64 (fmt, buf, r);
4493 /* Decode a double precision DFP value in BUF into a real R. */
4494 static void
4495 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4496 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4497 const long *buf ATTRIBUTE_UNUSED)
4499 decode_decimal64 (fmt, r, buf);
4502 /* Encode real R into a quad precision DFP value in BUF. */
4503 static void
4504 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4505 long *buf ATTRIBUTE_UNUSED,
4506 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4508 encode_decimal128 (fmt, buf, r);
4511 /* Decode a quad precision DFP value in BUF into a real R. */
4512 static void
4513 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4514 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4515 const long *buf ATTRIBUTE_UNUSED)
4517 decode_decimal128 (fmt, r, buf);
4520 /* Single precision decimal floating point (IEEE 754). */
4521 const struct real_format decimal_single_format =
4523 encode_decimal_single,
4524 decode_decimal_single,
4528 -94,
4532 false,
4533 true,
4534 true,
4535 true,
4536 true,
4537 true,
4538 true,
4539 false
4542 /* Double precision decimal floating point (IEEE 754). */
4543 const struct real_format decimal_double_format =
4545 encode_decimal_double,
4546 decode_decimal_double,
4550 -382,
4551 385,
4554 false,
4555 true,
4556 true,
4557 true,
4558 true,
4559 true,
4560 true,
4561 false
4564 /* Quad precision decimal floating point (IEEE 754). */
4565 const struct real_format decimal_quad_format =
4567 encode_decimal_quad,
4568 decode_decimal_quad,
4572 -6142,
4573 6145,
4574 127,
4575 127,
4576 false,
4577 true,
4578 true,
4579 true,
4580 true,
4581 true,
4582 true,
4583 false
4586 /* Encode half-precision floats. This routine is used both for the IEEE
4587 ARM alternative encodings. */
4588 static void
4589 encode_ieee_half (const struct real_format *fmt, long *buf,
4590 const REAL_VALUE_TYPE *r)
4592 unsigned long image, sig, exp;
4593 unsigned long sign = r->sign;
4594 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
4596 image = sign << 15;
4597 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 11)) & 0x3ff;
4599 switch (r->cl)
4601 case rvc_zero:
4602 break;
4604 case rvc_inf:
4605 if (fmt->has_inf)
4606 image |= 31 << 10;
4607 else
4608 image |= 0x7fff;
4609 break;
4611 case rvc_nan:
4612 if (fmt->has_nans)
4614 if (r->canonical)
4615 sig = (fmt->canonical_nan_lsbs_set ? (1 << 9) - 1 : 0);
4616 if (r->signalling == fmt->qnan_msb_set)
4617 sig &= ~(1 << 9);
4618 else
4619 sig |= 1 << 9;
4620 if (sig == 0)
4621 sig = 1 << 8;
4623 image |= 31 << 10;
4624 image |= sig;
4626 else
4627 image |= 0x3ff;
4628 break;
4630 case rvc_normal:
4631 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4632 whereas the intermediate representation is 0.F x 2**exp.
4633 Which means we're off by one. */
4634 if (denormal)
4635 exp = 0;
4636 else
4637 exp = REAL_EXP (r) + 15 - 1;
4638 image |= exp << 10;
4639 image |= sig;
4640 break;
4642 default:
4643 gcc_unreachable ();
4646 buf[0] = image;
4649 /* Decode half-precision floats. This routine is used both for the IEEE
4650 ARM alternative encodings. */
4651 static void
4652 decode_ieee_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4653 const long *buf)
4655 unsigned long image = buf[0] & 0xffff;
4656 bool sign = (image >> 15) & 1;
4657 int exp = (image >> 10) & 0x1f;
4659 memset (r, 0, sizeof (*r));
4660 image <<= HOST_BITS_PER_LONG - 11;
4661 image &= ~SIG_MSB;
4663 if (exp == 0)
4665 if (image && fmt->has_denorm)
4667 r->cl = rvc_normal;
4668 r->sign = sign;
4669 SET_REAL_EXP (r, -14);
4670 r->sig[SIGSZ-1] = image << 1;
4671 normalize (r);
4673 else if (fmt->has_signed_zero)
4674 r->sign = sign;
4676 else if (exp == 31 && (fmt->has_nans || fmt->has_inf))
4678 if (image)
4680 r->cl = rvc_nan;
4681 r->sign = sign;
4682 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4683 ^ fmt->qnan_msb_set);
4684 r->sig[SIGSZ-1] = image;
4686 else
4688 r->cl = rvc_inf;
4689 r->sign = sign;
4692 else
4694 r->cl = rvc_normal;
4695 r->sign = sign;
4696 SET_REAL_EXP (r, exp - 15 + 1);
4697 r->sig[SIGSZ-1] = image | SIG_MSB;
4701 /* Half-precision format, as specified in IEEE 754R. */
4702 const struct real_format ieee_half_format =
4704 encode_ieee_half,
4705 decode_ieee_half,
4709 -13,
4713 false,
4714 true,
4715 true,
4716 true,
4717 true,
4718 true,
4719 true,
4720 false
4723 /* ARM's alternative half-precision format, similar to IEEE but with
4724 no reserved exponent value for NaNs and infinities; rather, it just
4725 extends the range of exponents by one. */
4726 const struct real_format arm_half_format =
4728 encode_ieee_half,
4729 decode_ieee_half,
4733 -13,
4737 false,
4738 true,
4739 false,
4740 false,
4741 true,
4742 true,
4743 false,
4744 false
4747 /* A synthetic "format" for internal arithmetic. It's the size of the
4748 internal significand minus the two bits needed for proper rounding.
4749 The encode and decode routines exist only to satisfy our paranoia
4750 harness. */
4752 static void encode_internal (const struct real_format *fmt,
4753 long *, const REAL_VALUE_TYPE *);
4754 static void decode_internal (const struct real_format *,
4755 REAL_VALUE_TYPE *, const long *);
4757 static void
4758 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4759 const REAL_VALUE_TYPE *r)
4761 memcpy (buf, r, sizeof (*r));
4764 static void
4765 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
4766 REAL_VALUE_TYPE *r, const long *buf)
4768 memcpy (r, buf, sizeof (*r));
4771 const struct real_format real_internal_format =
4773 encode_internal,
4774 decode_internal,
4776 SIGNIFICAND_BITS - 2,
4777 SIGNIFICAND_BITS - 2,
4778 -MAX_EXP,
4779 MAX_EXP,
4782 false,
4783 false,
4784 true,
4785 true,
4786 false,
4787 true,
4788 true,
4789 false
4792 /* Calculate the square root of X in mode MODE, and store the result
4793 in R. Return TRUE if the operation does not raise an exception.
4794 For details see "High Precision Division and Square Root",
4795 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4796 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4798 bool
4799 real_sqrt (REAL_VALUE_TYPE *r, enum machine_mode mode,
4800 const REAL_VALUE_TYPE *x)
4802 static REAL_VALUE_TYPE halfthree;
4803 static bool init = false;
4804 REAL_VALUE_TYPE h, t, i;
4805 int iter, exp;
4807 /* sqrt(-0.0) is -0.0. */
4808 if (real_isnegzero (x))
4810 *r = *x;
4811 return false;
4814 /* Negative arguments return NaN. */
4815 if (real_isneg (x))
4817 get_canonical_qnan (r, 0);
4818 return false;
4821 /* Infinity and NaN return themselves. */
4822 if (!real_isfinite (x))
4824 *r = *x;
4825 return false;
4828 if (!init)
4830 do_add (&halfthree, &dconst1, &dconsthalf, 0);
4831 init = true;
4834 /* Initial guess for reciprocal sqrt, i. */
4835 exp = real_exponent (x);
4836 real_ldexp (&i, &dconst1, -exp/2);
4838 /* Newton's iteration for reciprocal sqrt, i. */
4839 for (iter = 0; iter < 16; iter++)
4841 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4842 do_multiply (&t, x, &i);
4843 do_multiply (&h, &t, &i);
4844 do_multiply (&t, &h, &dconsthalf);
4845 do_add (&h, &halfthree, &t, 1);
4846 do_multiply (&t, &i, &h);
4848 /* Check for early convergence. */
4849 if (iter >= 6 && real_identical (&i, &t))
4850 break;
4852 /* ??? Unroll loop to avoid copying. */
4853 i = t;
4856 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4857 do_multiply (&t, x, &i);
4858 do_multiply (&h, &t, &i);
4859 do_add (&i, &dconst1, &h, 1);
4860 do_multiply (&h, &t, &i);
4861 do_multiply (&i, &dconsthalf, &h);
4862 do_add (&h, &t, &i, 0);
4864 /* ??? We need a Tuckerman test to get the last bit. */
4866 real_convert (r, mode, &h);
4867 return true;
4870 /* Calculate X raised to the integer exponent N in mode MODE and store
4871 the result in R. Return true if the result may be inexact due to
4872 loss of precision. The algorithm is the classic "left-to-right binary
4873 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4874 Algorithms", "The Art of Computer Programming", Volume 2. */
4876 bool
4877 real_powi (REAL_VALUE_TYPE *r, enum machine_mode mode,
4878 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4880 unsigned HOST_WIDE_INT bit;
4881 REAL_VALUE_TYPE t;
4882 bool inexact = false;
4883 bool init = false;
4884 bool neg;
4885 int i;
4887 if (n == 0)
4889 *r = dconst1;
4890 return false;
4892 else if (n < 0)
4894 /* Don't worry about overflow, from now on n is unsigned. */
4895 neg = true;
4896 n = -n;
4898 else
4899 neg = false;
4901 t = *x;
4902 bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
4903 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4905 if (init)
4907 inexact |= do_multiply (&t, &t, &t);
4908 if (n & bit)
4909 inexact |= do_multiply (&t, &t, x);
4911 else if (n & bit)
4912 init = true;
4913 bit >>= 1;
4916 if (neg)
4917 inexact |= do_divide (&t, &dconst1, &t);
4919 real_convert (r, mode, &t);
4920 return inexact;
4923 /* Round X to the nearest integer not larger in absolute value, i.e.
4924 towards zero, placing the result in R in mode MODE. */
4926 void
4927 real_trunc (REAL_VALUE_TYPE *r, enum machine_mode mode,
4928 const REAL_VALUE_TYPE *x)
4930 do_fix_trunc (r, x);
4931 if (mode != VOIDmode)
4932 real_convert (r, mode, r);
4935 /* Round X to the largest integer not greater in value, i.e. round
4936 down, placing the result in R in mode MODE. */
4938 void
4939 real_floor (REAL_VALUE_TYPE *r, enum machine_mode mode,
4940 const REAL_VALUE_TYPE *x)
4942 REAL_VALUE_TYPE t;
4944 do_fix_trunc (&t, x);
4945 if (! real_identical (&t, x) && x->sign)
4946 do_add (&t, &t, &dconstm1, 0);
4947 if (mode != VOIDmode)
4948 real_convert (r, mode, &t);
4949 else
4950 *r = t;
4953 /* Round X to the smallest integer not less then argument, i.e. round
4954 up, placing the result in R in mode MODE. */
4956 void
4957 real_ceil (REAL_VALUE_TYPE *r, enum machine_mode mode,
4958 const REAL_VALUE_TYPE *x)
4960 REAL_VALUE_TYPE t;
4962 do_fix_trunc (&t, x);
4963 if (! real_identical (&t, x) && ! x->sign)
4964 do_add (&t, &t, &dconst1, 0);
4965 if (mode != VOIDmode)
4966 real_convert (r, mode, &t);
4967 else
4968 *r = t;
4971 /* Round X to the nearest integer, but round halfway cases away from
4972 zero. */
4974 void
4975 real_round (REAL_VALUE_TYPE *r, enum machine_mode mode,
4976 const REAL_VALUE_TYPE *x)
4978 do_add (r, x, &dconsthalf, x->sign);
4979 do_fix_trunc (r, r);
4980 if (mode != VOIDmode)
4981 real_convert (r, mode, r);
4984 /* Set the sign of R to the sign of X. */
4986 void
4987 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4989 r->sign = x->sign;
4992 /* Check whether the real constant value given is an integer. */
4994 bool
4995 real_isinteger (const REAL_VALUE_TYPE *c, enum machine_mode mode)
4997 REAL_VALUE_TYPE cint;
4999 real_trunc (&cint, mode, c);
5000 return real_identical (c, &cint);
5003 /* Write into BUF the maximum representable finite floating-point
5004 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5005 float string. LEN is the size of BUF, and the buffer must be large
5006 enough to contain the resulting string. */
5008 void
5009 get_max_float (const struct real_format *fmt, char *buf, size_t len)
5011 int i, n;
5012 char *p;
5014 strcpy (buf, "0x0.");
5015 n = fmt->p;
5016 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
5017 *p++ = 'f';
5018 if (i < n)
5019 *p++ = "08ce"[n - i];
5020 sprintf (p, "p%d", fmt->emax);
5021 if (fmt->pnan < fmt->p)
5023 /* This is an IBM extended double format made up of two IEEE
5024 doubles. The value of the long double is the sum of the
5025 values of the two parts. The most significant part is
5026 required to be the value of the long double rounded to the
5027 nearest double. Rounding means we need a slightly smaller
5028 value for LDBL_MAX. */
5029 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
5032 gcc_assert (strlen (buf) < len);