Fix DealII type problems.
[official-gcc/Ramakrishna.git] / gcc / real.c
blob25e599dfe352e4f2d57a0721c87f6d9702c9cc63
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 "toplev.h"
29 #include "real.h"
30 #include "tm_p.h"
31 #include "dfp.h"
33 /* The floating point model used internally is not exactly IEEE 754
34 compliant, and close to the description in the ISO C99 standard,
35 section 5.2.4.2.2 Characteristics of floating types.
37 Specifically
39 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
41 where
42 s = sign (+- 1)
43 b = base or radix, here always 2
44 e = exponent
45 p = precision (the number of base-b digits in the significand)
46 f_k = the digits of the significand.
48 We differ from typical IEEE 754 encodings in that the entire
49 significand is fractional. Normalized significands are in the
50 range [0.5, 1.0).
52 A requirement of the model is that P be larger than the largest
53 supported target floating-point type by at least 2 bits. This gives
54 us proper rounding when we truncate to the target type. In addition,
55 E must be large enough to hold the smallest supported denormal number
56 in a normalized form.
58 Both of these requirements are easily satisfied. The largest target
59 significand is 113 bits; we store at least 160. The smallest
60 denormal number fits in 17 exponent bits; we store 26.
62 Note that the decimal string conversion routines are sensitive to
63 rounding errors. Since the raw arithmetic routines do not themselves
64 have guard digits or rounding, the computation of 10**exp can
65 accumulate more than a few digits of error. The previous incarnation
66 of real.c successfully used a 144-bit fraction; given the current
67 layout of REAL_VALUE_TYPE we're forced to expand to at least 160 bits. */
70 /* Used to classify two numbers simultaneously. */
71 #define CLASS2(A, B) ((A) << 2 | (B))
73 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
74 #error "Some constant folding done by hand to avoid shift count warnings"
75 #endif
77 static void get_zero (REAL_VALUE_TYPE *, int);
78 static void get_canonical_qnan (REAL_VALUE_TYPE *, int);
79 static void get_canonical_snan (REAL_VALUE_TYPE *, int);
80 static void get_inf (REAL_VALUE_TYPE *, int);
81 static bool sticky_rshift_significand (REAL_VALUE_TYPE *,
82 const REAL_VALUE_TYPE *, unsigned int);
83 static void rshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
84 unsigned int);
85 static void lshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
86 unsigned int);
87 static void lshift_significand_1 (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
88 static bool add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *,
89 const REAL_VALUE_TYPE *);
90 static bool sub_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
91 const REAL_VALUE_TYPE *, int);
92 static void neg_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
93 static int cmp_significands (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
94 static int cmp_significand_0 (const REAL_VALUE_TYPE *);
95 static void set_significand_bit (REAL_VALUE_TYPE *, unsigned int);
96 static void clear_significand_bit (REAL_VALUE_TYPE *, unsigned int);
97 static bool test_significand_bit (REAL_VALUE_TYPE *, unsigned int);
98 static void clear_significand_below (REAL_VALUE_TYPE *, unsigned int);
99 static bool div_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
100 const REAL_VALUE_TYPE *);
101 static void normalize (REAL_VALUE_TYPE *);
103 static bool do_add (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
104 const REAL_VALUE_TYPE *, int);
105 static bool do_multiply (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
106 const REAL_VALUE_TYPE *);
107 static bool do_divide (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
108 const REAL_VALUE_TYPE *);
109 static int do_compare (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int);
110 static void do_fix_trunc (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
112 static unsigned long rtd_divmod (REAL_VALUE_TYPE *, REAL_VALUE_TYPE *);
113 static void decimal_from_integer (REAL_VALUE_TYPE *);
114 static void decimal_integer_string (char *, const REAL_VALUE_TYPE *,
115 size_t);
117 static const REAL_VALUE_TYPE * ten_to_ptwo (int);
118 static const REAL_VALUE_TYPE * ten_to_mptwo (int);
119 static const REAL_VALUE_TYPE * real_digit (int);
120 static void times_pten (REAL_VALUE_TYPE *, int);
122 static void round_for_format (const struct real_format *, REAL_VALUE_TYPE *);
124 /* Initialize R with a positive zero. */
126 static inline void
127 get_zero (REAL_VALUE_TYPE *r, int sign)
129 memset (r, 0, sizeof (*r));
130 r->sign = sign;
133 /* Initialize R with the canonical quiet NaN. */
135 static inline void
136 get_canonical_qnan (REAL_VALUE_TYPE *r, int sign)
138 memset (r, 0, sizeof (*r));
139 r->cl = rvc_nan;
140 r->sign = sign;
141 r->canonical = 1;
144 static inline void
145 get_canonical_snan (REAL_VALUE_TYPE *r, int sign)
147 memset (r, 0, sizeof (*r));
148 r->cl = rvc_nan;
149 r->sign = sign;
150 r->signalling = 1;
151 r->canonical = 1;
154 static inline void
155 get_inf (REAL_VALUE_TYPE *r, int sign)
157 memset (r, 0, sizeof (*r));
158 r->cl = rvc_inf;
159 r->sign = sign;
163 /* Right-shift the significand of A by N bits; put the result in the
164 significand of R. If any one bits are shifted out, return true. */
166 static bool
167 sticky_rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
168 unsigned int n)
170 unsigned long sticky = 0;
171 unsigned int i, ofs = 0;
173 if (n >= HOST_BITS_PER_LONG)
175 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
176 sticky |= a->sig[i];
177 n &= HOST_BITS_PER_LONG - 1;
180 if (n != 0)
182 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
183 for (i = 0; i < SIGSZ; ++i)
185 r->sig[i]
186 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
187 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
188 << (HOST_BITS_PER_LONG - n)));
191 else
193 for (i = 0; ofs + i < SIGSZ; ++i)
194 r->sig[i] = a->sig[ofs + i];
195 for (; i < SIGSZ; ++i)
196 r->sig[i] = 0;
199 return sticky != 0;
202 /* Right-shift the significand of A by N bits; put the result in the
203 significand of R. */
205 static void
206 rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
207 unsigned int n)
209 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
211 n &= HOST_BITS_PER_LONG - 1;
212 if (n != 0)
214 for (i = 0; i < SIGSZ; ++i)
216 r->sig[i]
217 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
218 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
219 << (HOST_BITS_PER_LONG - n)));
222 else
224 for (i = 0; ofs + i < SIGSZ; ++i)
225 r->sig[i] = a->sig[ofs + i];
226 for (; i < SIGSZ; ++i)
227 r->sig[i] = 0;
231 /* Left-shift the significand of A by N bits; put the result in the
232 significand of R. */
234 static void
235 lshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
236 unsigned int n)
238 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
240 n &= HOST_BITS_PER_LONG - 1;
241 if (n == 0)
243 for (i = 0; ofs + i < SIGSZ; ++i)
244 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
245 for (; i < SIGSZ; ++i)
246 r->sig[SIGSZ-1-i] = 0;
248 else
249 for (i = 0; i < SIGSZ; ++i)
251 r->sig[SIGSZ-1-i]
252 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
253 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
254 >> (HOST_BITS_PER_LONG - n)));
258 /* Likewise, but N is specialized to 1. */
260 static inline void
261 lshift_significand_1 (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
263 unsigned int i;
265 for (i = SIGSZ - 1; i > 0; --i)
266 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
267 r->sig[0] = a->sig[0] << 1;
270 /* Add the significands of A and B, placing the result in R. Return
271 true if there was carry out of the most significant word. */
273 static inline bool
274 add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
275 const REAL_VALUE_TYPE *b)
277 bool carry = false;
278 int i;
280 for (i = 0; i < SIGSZ; ++i)
282 unsigned long ai = a->sig[i];
283 unsigned long ri = ai + b->sig[i];
285 if (carry)
287 carry = ri < ai;
288 carry |= ++ri == 0;
290 else
291 carry = ri < ai;
293 r->sig[i] = ri;
296 return carry;
299 /* Subtract the significands of A and B, placing the result in R. CARRY is
300 true if there's a borrow incoming to the least significant word.
301 Return true if there was borrow out of the most significant word. */
303 static inline bool
304 sub_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
305 const REAL_VALUE_TYPE *b, int carry)
307 int i;
309 for (i = 0; i < SIGSZ; ++i)
311 unsigned long ai = a->sig[i];
312 unsigned long ri = ai - b->sig[i];
314 if (carry)
316 carry = ri > ai;
317 carry |= ~--ri == 0;
319 else
320 carry = ri > ai;
322 r->sig[i] = ri;
325 return carry;
328 /* Negate the significand A, placing the result in R. */
330 static inline void
331 neg_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
333 bool carry = true;
334 int i;
336 for (i = 0; i < SIGSZ; ++i)
338 unsigned long ri, ai = a->sig[i];
340 if (carry)
342 if (ai)
344 ri = -ai;
345 carry = false;
347 else
348 ri = ai;
350 else
351 ri = ~ai;
353 r->sig[i] = ri;
357 /* Compare significands. Return tri-state vs zero. */
359 static inline int
360 cmp_significands (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
362 int i;
364 for (i = SIGSZ - 1; i >= 0; --i)
366 unsigned long ai = a->sig[i];
367 unsigned long bi = b->sig[i];
369 if (ai > bi)
370 return 1;
371 if (ai < bi)
372 return -1;
375 return 0;
378 /* Return true if A is nonzero. */
380 static inline int
381 cmp_significand_0 (const REAL_VALUE_TYPE *a)
383 int i;
385 for (i = SIGSZ - 1; i >= 0; --i)
386 if (a->sig[i])
387 return 1;
389 return 0;
392 /* Set bit N of the significand of R. */
394 static inline void
395 set_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
397 r->sig[n / HOST_BITS_PER_LONG]
398 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
401 /* Clear bit N of the significand of R. */
403 static inline void
404 clear_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
406 r->sig[n / HOST_BITS_PER_LONG]
407 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
410 /* Test bit N of the significand of R. */
412 static inline bool
413 test_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
415 /* ??? Compiler bug here if we return this expression directly.
416 The conversion to bool strips the "&1" and we wind up testing
417 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
418 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
419 return t;
422 /* Clear bits 0..N-1 of the significand of R. */
424 static void
425 clear_significand_below (REAL_VALUE_TYPE *r, unsigned int n)
427 int i, w = n / HOST_BITS_PER_LONG;
429 for (i = 0; i < w; ++i)
430 r->sig[i] = 0;
432 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
435 /* Divide the significands of A and B, placing the result in R. Return
436 true if the division was inexact. */
438 static inline bool
439 div_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
440 const REAL_VALUE_TYPE *b)
442 REAL_VALUE_TYPE u;
443 int i, bit = SIGNIFICAND_BITS - 1;
444 unsigned long msb, inexact;
446 u = *a;
447 memset (r->sig, 0, sizeof (r->sig));
449 msb = 0;
450 goto start;
453 msb = u.sig[SIGSZ-1] & SIG_MSB;
454 lshift_significand_1 (&u, &u);
455 start:
456 if (msb || cmp_significands (&u, b) >= 0)
458 sub_significands (&u, &u, b, 0);
459 set_significand_bit (r, bit);
462 while (--bit >= 0);
464 for (i = 0, inexact = 0; i < SIGSZ; i++)
465 inexact |= u.sig[i];
467 return inexact != 0;
470 /* Adjust the exponent and significand of R such that the most
471 significant bit is set. We underflow to zero and overflow to
472 infinity here, without denormals. (The intermediate representation
473 exponent is large enough to handle target denormals normalized.) */
475 static void
476 normalize (REAL_VALUE_TYPE *r)
478 int shift = 0, exp;
479 int i, j;
481 if (r->decimal)
482 return;
484 /* Find the first word that is nonzero. */
485 for (i = SIGSZ - 1; i >= 0; i--)
486 if (r->sig[i] == 0)
487 shift += HOST_BITS_PER_LONG;
488 else
489 break;
491 /* Zero significand flushes to zero. */
492 if (i < 0)
494 r->cl = rvc_zero;
495 SET_REAL_EXP (r, 0);
496 return;
499 /* Find the first bit that is nonzero. */
500 for (j = 0; ; j++)
501 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
502 break;
503 shift += j;
505 if (shift > 0)
507 exp = REAL_EXP (r) - shift;
508 if (exp > MAX_EXP)
509 get_inf (r, r->sign);
510 else if (exp < -MAX_EXP)
511 get_zero (r, r->sign);
512 else
514 SET_REAL_EXP (r, exp);
515 lshift_significand (r, r, shift);
520 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
521 result may be inexact due to a loss of precision. */
523 static bool
524 do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
525 const REAL_VALUE_TYPE *b, int subtract_p)
527 int dexp, sign, exp;
528 REAL_VALUE_TYPE t;
529 bool inexact = false;
531 /* Determine if we need to add or subtract. */
532 sign = a->sign;
533 subtract_p = (sign ^ b->sign) ^ subtract_p;
535 switch (CLASS2 (a->cl, b->cl))
537 case CLASS2 (rvc_zero, rvc_zero):
538 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
539 get_zero (r, sign & !subtract_p);
540 return false;
542 case CLASS2 (rvc_zero, rvc_normal):
543 case CLASS2 (rvc_zero, rvc_inf):
544 case CLASS2 (rvc_zero, rvc_nan):
545 /* 0 + ANY = ANY. */
546 case CLASS2 (rvc_normal, rvc_nan):
547 case CLASS2 (rvc_inf, rvc_nan):
548 case CLASS2 (rvc_nan, rvc_nan):
549 /* ANY + NaN = NaN. */
550 case CLASS2 (rvc_normal, rvc_inf):
551 /* R + Inf = Inf. */
552 *r = *b;
553 r->sign = sign ^ subtract_p;
554 return false;
556 case CLASS2 (rvc_normal, rvc_zero):
557 case CLASS2 (rvc_inf, rvc_zero):
558 case CLASS2 (rvc_nan, rvc_zero):
559 /* ANY + 0 = ANY. */
560 case CLASS2 (rvc_nan, rvc_normal):
561 case CLASS2 (rvc_nan, rvc_inf):
562 /* NaN + ANY = NaN. */
563 case CLASS2 (rvc_inf, rvc_normal):
564 /* Inf + R = Inf. */
565 *r = *a;
566 return false;
568 case CLASS2 (rvc_inf, rvc_inf):
569 if (subtract_p)
570 /* Inf - Inf = NaN. */
571 get_canonical_qnan (r, 0);
572 else
573 /* Inf + Inf = Inf. */
574 *r = *a;
575 return false;
577 case CLASS2 (rvc_normal, rvc_normal):
578 break;
580 default:
581 gcc_unreachable ();
584 /* Swap the arguments such that A has the larger exponent. */
585 dexp = REAL_EXP (a) - REAL_EXP (b);
586 if (dexp < 0)
588 const REAL_VALUE_TYPE *t;
589 t = a, a = b, b = t;
590 dexp = -dexp;
591 sign ^= subtract_p;
593 exp = REAL_EXP (a);
595 /* If the exponents are not identical, we need to shift the
596 significand of B down. */
597 if (dexp > 0)
599 /* If the exponents are too far apart, the significands
600 do not overlap, which makes the subtraction a noop. */
601 if (dexp >= SIGNIFICAND_BITS)
603 *r = *a;
604 r->sign = sign;
605 return true;
608 inexact |= sticky_rshift_significand (&t, b, dexp);
609 b = &t;
612 if (subtract_p)
614 if (sub_significands (r, a, b, inexact))
616 /* We got a borrow out of the subtraction. That means that
617 A and B had the same exponent, and B had the larger
618 significand. We need to swap the sign and negate the
619 significand. */
620 sign ^= 1;
621 neg_significand (r, r);
624 else
626 if (add_significands (r, a, b))
628 /* We got carry out of the addition. This means we need to
629 shift the significand back down one bit and increase the
630 exponent. */
631 inexact |= sticky_rshift_significand (r, r, 1);
632 r->sig[SIGSZ-1] |= SIG_MSB;
633 if (++exp > MAX_EXP)
635 get_inf (r, sign);
636 return true;
641 r->cl = rvc_normal;
642 r->sign = sign;
643 SET_REAL_EXP (r, exp);
644 /* Zero out the remaining fields. */
645 r->signalling = 0;
646 r->canonical = 0;
647 r->decimal = 0;
649 /* Re-normalize the result. */
650 normalize (r);
652 /* Special case: if the subtraction results in zero, the result
653 is positive. */
654 if (r->cl == rvc_zero)
655 r->sign = 0;
656 else
657 r->sig[0] |= inexact;
659 return inexact;
662 /* Calculate R = A * B. Return true if the result may be inexact. */
664 static bool
665 do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
666 const REAL_VALUE_TYPE *b)
668 REAL_VALUE_TYPE u, t, *rr;
669 unsigned int i, j, k;
670 int sign = a->sign ^ b->sign;
671 bool inexact = false;
673 switch (CLASS2 (a->cl, b->cl))
675 case CLASS2 (rvc_zero, rvc_zero):
676 case CLASS2 (rvc_zero, rvc_normal):
677 case CLASS2 (rvc_normal, rvc_zero):
678 /* +-0 * ANY = 0 with appropriate sign. */
679 get_zero (r, sign);
680 return false;
682 case CLASS2 (rvc_zero, rvc_nan):
683 case CLASS2 (rvc_normal, rvc_nan):
684 case CLASS2 (rvc_inf, rvc_nan):
685 case CLASS2 (rvc_nan, rvc_nan):
686 /* ANY * NaN = NaN. */
687 *r = *b;
688 r->sign = sign;
689 return false;
691 case CLASS2 (rvc_nan, rvc_zero):
692 case CLASS2 (rvc_nan, rvc_normal):
693 case CLASS2 (rvc_nan, rvc_inf):
694 /* NaN * ANY = NaN. */
695 *r = *a;
696 r->sign = sign;
697 return false;
699 case CLASS2 (rvc_zero, rvc_inf):
700 case CLASS2 (rvc_inf, rvc_zero):
701 /* 0 * Inf = NaN */
702 get_canonical_qnan (r, sign);
703 return false;
705 case CLASS2 (rvc_inf, rvc_inf):
706 case CLASS2 (rvc_normal, rvc_inf):
707 case CLASS2 (rvc_inf, rvc_normal):
708 /* Inf * Inf = Inf, R * Inf = Inf */
709 get_inf (r, sign);
710 return false;
712 case CLASS2 (rvc_normal, rvc_normal):
713 break;
715 default:
716 gcc_unreachable ();
719 if (r == a || r == b)
720 rr = &t;
721 else
722 rr = r;
723 get_zero (rr, 0);
725 /* Collect all the partial products. Since we don't have sure access
726 to a widening multiply, we split each long into two half-words.
728 Consider the long-hand form of a four half-word multiplication:
730 A B C D
731 * E F G H
732 --------------
733 DE DF DG DH
734 CE CF CG CH
735 BE BF BG BH
736 AE AF AG AH
738 We construct partial products of the widened half-word products
739 that are known to not overlap, e.g. DF+DH. Each such partial
740 product is given its proper exponent, which allows us to sum them
741 and obtain the finished product. */
743 for (i = 0; i < SIGSZ * 2; ++i)
745 unsigned long ai = a->sig[i / 2];
746 if (i & 1)
747 ai >>= HOST_BITS_PER_LONG / 2;
748 else
749 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
751 if (ai == 0)
752 continue;
754 for (j = 0; j < 2; ++j)
756 int exp = (REAL_EXP (a) - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
757 + (REAL_EXP (b) - (1-j)*(HOST_BITS_PER_LONG/2)));
759 if (exp > MAX_EXP)
761 get_inf (r, sign);
762 return true;
764 if (exp < -MAX_EXP)
766 /* Would underflow to zero, which we shouldn't bother adding. */
767 inexact = true;
768 continue;
771 memset (&u, 0, sizeof (u));
772 u.cl = rvc_normal;
773 SET_REAL_EXP (&u, exp);
775 for (k = j; k < SIGSZ * 2; k += 2)
777 unsigned long bi = b->sig[k / 2];
778 if (k & 1)
779 bi >>= HOST_BITS_PER_LONG / 2;
780 else
781 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
783 u.sig[k / 2] = ai * bi;
786 normalize (&u);
787 inexact |= do_add (rr, rr, &u, 0);
791 rr->sign = sign;
792 if (rr != r)
793 *r = t;
795 return inexact;
798 /* Calculate R = A / B. Return true if the result may be inexact. */
800 static bool
801 do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
802 const REAL_VALUE_TYPE *b)
804 int exp, sign = a->sign ^ b->sign;
805 REAL_VALUE_TYPE t, *rr;
806 bool inexact;
808 switch (CLASS2 (a->cl, b->cl))
810 case CLASS2 (rvc_zero, rvc_zero):
811 /* 0 / 0 = NaN. */
812 case CLASS2 (rvc_inf, rvc_inf):
813 /* Inf / Inf = NaN. */
814 get_canonical_qnan (r, sign);
815 return false;
817 case CLASS2 (rvc_zero, rvc_normal):
818 case CLASS2 (rvc_zero, rvc_inf):
819 /* 0 / ANY = 0. */
820 case CLASS2 (rvc_normal, rvc_inf):
821 /* R / Inf = 0. */
822 get_zero (r, sign);
823 return false;
825 case CLASS2 (rvc_normal, rvc_zero):
826 /* R / 0 = Inf. */
827 case CLASS2 (rvc_inf, rvc_zero):
828 /* Inf / 0 = Inf. */
829 get_inf (r, sign);
830 return false;
832 case CLASS2 (rvc_zero, rvc_nan):
833 case CLASS2 (rvc_normal, rvc_nan):
834 case CLASS2 (rvc_inf, rvc_nan):
835 case CLASS2 (rvc_nan, rvc_nan):
836 /* ANY / NaN = NaN. */
837 *r = *b;
838 r->sign = sign;
839 return false;
841 case CLASS2 (rvc_nan, rvc_zero):
842 case CLASS2 (rvc_nan, rvc_normal):
843 case CLASS2 (rvc_nan, rvc_inf):
844 /* NaN / ANY = NaN. */
845 *r = *a;
846 r->sign = sign;
847 return false;
849 case CLASS2 (rvc_inf, rvc_normal):
850 /* Inf / R = Inf. */
851 get_inf (r, sign);
852 return false;
854 case CLASS2 (rvc_normal, rvc_normal):
855 break;
857 default:
858 gcc_unreachable ();
861 if (r == a || r == b)
862 rr = &t;
863 else
864 rr = r;
866 /* Make sure all fields in the result are initialized. */
867 get_zero (rr, 0);
868 rr->cl = rvc_normal;
869 rr->sign = sign;
871 exp = REAL_EXP (a) - REAL_EXP (b) + 1;
872 if (exp > MAX_EXP)
874 get_inf (r, sign);
875 return true;
877 if (exp < -MAX_EXP)
879 get_zero (r, sign);
880 return true;
882 SET_REAL_EXP (rr, exp);
884 inexact = div_significands (rr, a, b);
886 /* Re-normalize the result. */
887 normalize (rr);
888 rr->sig[0] |= inexact;
890 if (rr != r)
891 *r = t;
893 return inexact;
896 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
897 one of the two operands is a NaN. */
899 static int
900 do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
901 int nan_result)
903 int ret;
905 switch (CLASS2 (a->cl, b->cl))
907 case CLASS2 (rvc_zero, rvc_zero):
908 /* Sign of zero doesn't matter for compares. */
909 return 0;
911 case CLASS2 (rvc_normal, rvc_zero):
912 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
913 if (a->decimal)
914 return decimal_do_compare (a, b, nan_result);
915 /* Fall through. */
916 case CLASS2 (rvc_inf, rvc_zero):
917 case CLASS2 (rvc_inf, rvc_normal):
918 return (a->sign ? -1 : 1);
920 case CLASS2 (rvc_inf, rvc_inf):
921 return -a->sign - -b->sign;
923 case CLASS2 (rvc_zero, rvc_normal):
924 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
925 if (b->decimal)
926 return decimal_do_compare (a, b, nan_result);
927 /* Fall through. */
928 case CLASS2 (rvc_zero, rvc_inf):
929 case CLASS2 (rvc_normal, rvc_inf):
930 return (b->sign ? 1 : -1);
932 case CLASS2 (rvc_zero, rvc_nan):
933 case CLASS2 (rvc_normal, rvc_nan):
934 case CLASS2 (rvc_inf, rvc_nan):
935 case CLASS2 (rvc_nan, rvc_nan):
936 case CLASS2 (rvc_nan, rvc_zero):
937 case CLASS2 (rvc_nan, rvc_normal):
938 case CLASS2 (rvc_nan, rvc_inf):
939 return nan_result;
941 case CLASS2 (rvc_normal, rvc_normal):
942 break;
944 default:
945 gcc_unreachable ();
948 if (a->sign != b->sign)
949 return -a->sign - -b->sign;
951 if (a->decimal || b->decimal)
952 return decimal_do_compare (a, b, nan_result);
954 if (REAL_EXP (a) > REAL_EXP (b))
955 ret = 1;
956 else if (REAL_EXP (a) < REAL_EXP (b))
957 ret = -1;
958 else
959 ret = cmp_significands (a, b);
961 return (a->sign ? -ret : ret);
964 /* Return A truncated to an integral value toward zero. */
966 static void
967 do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
969 *r = *a;
971 switch (r->cl)
973 case rvc_zero:
974 case rvc_inf:
975 case rvc_nan:
976 break;
978 case rvc_normal:
979 if (r->decimal)
981 decimal_do_fix_trunc (r, a);
982 return;
984 if (REAL_EXP (r) <= 0)
985 get_zero (r, r->sign);
986 else if (REAL_EXP (r) < SIGNIFICAND_BITS)
987 clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r));
988 break;
990 default:
991 gcc_unreachable ();
995 /* Perform the binary or unary operation described by CODE.
996 For a unary operation, leave OP1 NULL. This function returns
997 true if the result may be inexact due to loss of precision. */
999 bool
1000 real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0,
1001 const REAL_VALUE_TYPE *op1)
1003 enum tree_code code = (enum tree_code) icode;
1005 if (op0->decimal || (op1 && op1->decimal))
1006 return decimal_real_arithmetic (r, code, op0, op1);
1008 switch (code)
1010 case PLUS_EXPR:
1011 return do_add (r, op0, op1, 0);
1013 case MINUS_EXPR:
1014 return do_add (r, op0, op1, 1);
1016 case MULT_EXPR:
1017 return do_multiply (r, op0, op1);
1019 case RDIV_EXPR:
1020 return do_divide (r, op0, op1);
1022 case MIN_EXPR:
1023 if (op1->cl == rvc_nan)
1024 *r = *op1;
1025 else if (do_compare (op0, op1, -1) < 0)
1026 *r = *op0;
1027 else
1028 *r = *op1;
1029 break;
1031 case MAX_EXPR:
1032 if (op1->cl == rvc_nan)
1033 *r = *op1;
1034 else if (do_compare (op0, op1, 1) < 0)
1035 *r = *op1;
1036 else
1037 *r = *op0;
1038 break;
1040 case NEGATE_EXPR:
1041 *r = *op0;
1042 r->sign ^= 1;
1043 break;
1045 case ABS_EXPR:
1046 *r = *op0;
1047 r->sign = 0;
1048 break;
1050 case FIX_TRUNC_EXPR:
1051 do_fix_trunc (r, op0);
1052 break;
1054 default:
1055 gcc_unreachable ();
1057 return false;
1060 /* Legacy. Similar, but return the result directly. */
1062 REAL_VALUE_TYPE
1063 real_arithmetic2 (int icode, const REAL_VALUE_TYPE *op0,
1064 const REAL_VALUE_TYPE *op1)
1066 REAL_VALUE_TYPE r;
1067 real_arithmetic (&r, icode, op0, op1);
1068 return r;
1071 bool
1072 real_compare (int icode, const REAL_VALUE_TYPE *op0,
1073 const REAL_VALUE_TYPE *op1)
1075 enum tree_code code = (enum tree_code) icode;
1077 switch (code)
1079 case LT_EXPR:
1080 return do_compare (op0, op1, 1) < 0;
1081 case LE_EXPR:
1082 return do_compare (op0, op1, 1) <= 0;
1083 case GT_EXPR:
1084 return do_compare (op0, op1, -1) > 0;
1085 case GE_EXPR:
1086 return do_compare (op0, op1, -1) >= 0;
1087 case EQ_EXPR:
1088 return do_compare (op0, op1, -1) == 0;
1089 case NE_EXPR:
1090 return do_compare (op0, op1, -1) != 0;
1091 case UNORDERED_EXPR:
1092 return op0->cl == rvc_nan || op1->cl == rvc_nan;
1093 case ORDERED_EXPR:
1094 return op0->cl != rvc_nan && op1->cl != rvc_nan;
1095 case UNLT_EXPR:
1096 return do_compare (op0, op1, -1) < 0;
1097 case UNLE_EXPR:
1098 return do_compare (op0, op1, -1) <= 0;
1099 case UNGT_EXPR:
1100 return do_compare (op0, op1, 1) > 0;
1101 case UNGE_EXPR:
1102 return do_compare (op0, op1, 1) >= 0;
1103 case UNEQ_EXPR:
1104 return do_compare (op0, op1, 0) == 0;
1105 case LTGT_EXPR:
1106 return do_compare (op0, op1, 0) != 0;
1108 default:
1109 gcc_unreachable ();
1113 /* Return floor log2(R). */
1116 real_exponent (const REAL_VALUE_TYPE *r)
1118 switch (r->cl)
1120 case rvc_zero:
1121 return 0;
1122 case rvc_inf:
1123 case rvc_nan:
1124 return (unsigned int)-1 >> 1;
1125 case rvc_normal:
1126 return REAL_EXP (r);
1127 default:
1128 gcc_unreachable ();
1132 /* R = OP0 * 2**EXP. */
1134 void
1135 real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1137 *r = *op0;
1138 switch (r->cl)
1140 case rvc_zero:
1141 case rvc_inf:
1142 case rvc_nan:
1143 break;
1145 case rvc_normal:
1146 exp += REAL_EXP (op0);
1147 if (exp > MAX_EXP)
1148 get_inf (r, r->sign);
1149 else if (exp < -MAX_EXP)
1150 get_zero (r, r->sign);
1151 else
1152 SET_REAL_EXP (r, exp);
1153 break;
1155 default:
1156 gcc_unreachable ();
1160 /* Determine whether a floating-point value X is infinite. */
1162 bool
1163 real_isinf (const REAL_VALUE_TYPE *r)
1165 return (r->cl == rvc_inf);
1168 /* Determine whether a floating-point value X is a NaN. */
1170 bool
1171 real_isnan (const REAL_VALUE_TYPE *r)
1173 return (r->cl == rvc_nan);
1176 /* Determine whether a floating-point value X is finite. */
1178 bool
1179 real_isfinite (const REAL_VALUE_TYPE *r)
1181 return (r->cl != rvc_nan) && (r->cl != rvc_inf);
1184 /* Determine whether a floating-point value X is negative. */
1186 bool
1187 real_isneg (const REAL_VALUE_TYPE *r)
1189 return r->sign;
1192 /* Determine whether a floating-point value X is minus zero. */
1194 bool
1195 real_isnegzero (const REAL_VALUE_TYPE *r)
1197 return r->sign && r->cl == rvc_zero;
1200 /* Compare two floating-point objects for bitwise identity. */
1202 bool
1203 real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1205 int i;
1207 if (a->cl != b->cl)
1208 return false;
1209 if (a->sign != b->sign)
1210 return false;
1212 switch (a->cl)
1214 case rvc_zero:
1215 case rvc_inf:
1216 return true;
1218 case rvc_normal:
1219 if (a->decimal != b->decimal)
1220 return false;
1221 if (REAL_EXP (a) != REAL_EXP (b))
1222 return false;
1223 break;
1225 case rvc_nan:
1226 if (a->signalling != b->signalling)
1227 return false;
1228 /* The significand is ignored for canonical NaNs. */
1229 if (a->canonical || b->canonical)
1230 return a->canonical == b->canonical;
1231 break;
1233 default:
1234 gcc_unreachable ();
1237 for (i = 0; i < SIGSZ; ++i)
1238 if (a->sig[i] != b->sig[i])
1239 return false;
1241 return true;
1244 /* Try to change R into its exact multiplicative inverse in machine
1245 mode MODE. Return true if successful. */
1247 bool
1248 exact_real_inverse (enum machine_mode mode, REAL_VALUE_TYPE *r)
1250 const REAL_VALUE_TYPE *one = real_digit (1);
1251 REAL_VALUE_TYPE u;
1252 int i;
1254 if (r->cl != rvc_normal)
1255 return false;
1257 /* Check for a power of two: all significand bits zero except the MSB. */
1258 for (i = 0; i < SIGSZ-1; ++i)
1259 if (r->sig[i] != 0)
1260 return false;
1261 if (r->sig[SIGSZ-1] != SIG_MSB)
1262 return false;
1264 /* Find the inverse and truncate to the required mode. */
1265 do_divide (&u, one, r);
1266 real_convert (&u, mode, &u);
1268 /* The rounding may have overflowed. */
1269 if (u.cl != rvc_normal)
1270 return false;
1271 for (i = 0; i < SIGSZ-1; ++i)
1272 if (u.sig[i] != 0)
1273 return false;
1274 if (u.sig[SIGSZ-1] != SIG_MSB)
1275 return false;
1277 *r = u;
1278 return true;
1281 /* Return true if arithmetic on values in IMODE that were promoted
1282 from values in TMODE is equivalent to direct arithmetic on values
1283 in TMODE. */
1285 bool
1286 real_can_shorten_arithmetic (enum machine_mode imode, enum machine_mode tmode)
1288 const struct real_format *tfmt, *ifmt;
1289 tfmt = REAL_MODE_FORMAT (tmode);
1290 ifmt = REAL_MODE_FORMAT (imode);
1291 /* These conditions are conservative rather than trying to catch the
1292 exact boundary conditions; the main case to allow is IEEE float
1293 and double. */
1294 return (ifmt->b == tfmt->b
1295 && ifmt->p > 2 * tfmt->p
1296 && ifmt->emin < 2 * tfmt->emin - tfmt->p - 2
1297 && ifmt->emin < tfmt->emin - tfmt->emax - tfmt->p - 2
1298 && ifmt->emax > 2 * tfmt->emax + 2
1299 && ifmt->emax > tfmt->emax - tfmt->emin + tfmt->p + 2
1300 && ifmt->round_towards_zero == tfmt->round_towards_zero
1301 && (ifmt->has_sign_dependent_rounding
1302 == tfmt->has_sign_dependent_rounding)
1303 && ifmt->has_nans >= tfmt->has_nans
1304 && ifmt->has_inf >= tfmt->has_inf
1305 && ifmt->has_signed_zero >= tfmt->has_signed_zero
1306 && !MODE_COMPOSITE_P (tmode)
1307 && !MODE_COMPOSITE_P (imode));
1310 /* Render R as an integer. */
1312 HOST_WIDE_INT
1313 real_to_integer (const REAL_VALUE_TYPE *r)
1315 unsigned HOST_WIDE_INT i;
1317 switch (r->cl)
1319 case rvc_zero:
1320 underflow:
1321 return 0;
1323 case rvc_inf:
1324 case rvc_nan:
1325 overflow:
1326 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1327 if (!r->sign)
1328 i--;
1329 return i;
1331 case rvc_normal:
1332 if (r->decimal)
1333 return decimal_real_to_integer (r);
1335 if (REAL_EXP (r) <= 0)
1336 goto underflow;
1337 /* Only force overflow for unsigned overflow. Signed overflow is
1338 undefined, so it doesn't matter what we return, and some callers
1339 expect to be able to use this routine for both signed and
1340 unsigned conversions. */
1341 if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1342 goto overflow;
1344 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1345 i = r->sig[SIGSZ-1];
1346 else
1348 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1349 i = r->sig[SIGSZ-1];
1350 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1351 i |= r->sig[SIGSZ-2];
1354 i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1356 if (r->sign)
1357 i = -i;
1358 return i;
1360 default:
1361 gcc_unreachable ();
1365 /* Likewise, but to an integer pair, HI+LOW. */
1367 void
1368 real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
1369 const REAL_VALUE_TYPE *r)
1371 REAL_VALUE_TYPE t;
1372 HOST_WIDE_INT low, high;
1373 int exp;
1375 switch (r->cl)
1377 case rvc_zero:
1378 underflow:
1379 low = high = 0;
1380 break;
1382 case rvc_inf:
1383 case rvc_nan:
1384 overflow:
1385 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1386 if (r->sign)
1387 low = 0;
1388 else
1390 high--;
1391 low = -1;
1393 break;
1395 case rvc_normal:
1396 if (r->decimal)
1398 decimal_real_to_integer2 (plow, phigh, r);
1399 return;
1402 exp = REAL_EXP (r);
1403 if (exp <= 0)
1404 goto underflow;
1405 /* Only force overflow for unsigned overflow. Signed overflow is
1406 undefined, so it doesn't matter what we return, and some callers
1407 expect to be able to use this routine for both signed and
1408 unsigned conversions. */
1409 if (exp > 2*HOST_BITS_PER_WIDE_INT)
1410 goto overflow;
1412 rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
1413 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1415 high = t.sig[SIGSZ-1];
1416 low = t.sig[SIGSZ-2];
1418 else
1420 gcc_assert (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG);
1421 high = t.sig[SIGSZ-1];
1422 high = high << (HOST_BITS_PER_LONG - 1) << 1;
1423 high |= t.sig[SIGSZ-2];
1425 low = t.sig[SIGSZ-3];
1426 low = low << (HOST_BITS_PER_LONG - 1) << 1;
1427 low |= t.sig[SIGSZ-4];
1430 if (r->sign)
1432 if (low == 0)
1433 high = -high;
1434 else
1435 low = -low, high = ~high;
1437 break;
1439 default:
1440 gcc_unreachable ();
1443 *plow = low;
1444 *phigh = high;
1447 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1448 of NUM / DEN. Return the quotient and place the remainder in NUM.
1449 It is expected that NUM / DEN are close enough that the quotient is
1450 small. */
1452 static unsigned long
1453 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1455 unsigned long q, msb;
1456 int expn = REAL_EXP (num), expd = REAL_EXP (den);
1458 if (expn < expd)
1459 return 0;
1461 q = msb = 0;
1462 goto start;
1465 msb = num->sig[SIGSZ-1] & SIG_MSB;
1466 q <<= 1;
1467 lshift_significand_1 (num, num);
1468 start:
1469 if (msb || cmp_significands (num, den) >= 0)
1471 sub_significands (num, num, den, 0);
1472 q |= 1;
1475 while (--expn >= expd);
1477 SET_REAL_EXP (num, expd);
1478 normalize (num);
1480 return q;
1483 /* Render R as a decimal floating point constant. Emit DIGITS significant
1484 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1485 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1486 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1487 to a string that, when parsed back in mode MODE, yields the same value. */
1489 #define M_LOG10_2 0.30102999566398119521
1491 void
1492 real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig,
1493 size_t buf_size, size_t digits,
1494 int crop_trailing_zeros, enum machine_mode mode)
1496 const struct real_format *fmt = NULL;
1497 const REAL_VALUE_TYPE *one, *ten;
1498 REAL_VALUE_TYPE r, pten, u, v;
1499 int dec_exp, cmp_one, digit;
1500 size_t max_digits;
1501 char *p, *first, *last;
1502 bool sign;
1503 bool round_up;
1505 if (mode != VOIDmode)
1507 fmt = REAL_MODE_FORMAT (mode);
1508 gcc_assert (fmt);
1511 r = *r_orig;
1512 switch (r.cl)
1514 case rvc_zero:
1515 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1516 return;
1517 case rvc_normal:
1518 break;
1519 case rvc_inf:
1520 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1521 return;
1522 case rvc_nan:
1523 /* ??? Print the significand as well, if not canonical? */
1524 sprintf (str, "%c%cNaN", (r_orig->sign ? '-' : '+'),
1525 (r_orig->signalling ? 'S' : 'Q'));
1526 return;
1527 default:
1528 gcc_unreachable ();
1531 if (r.decimal)
1533 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1534 return;
1537 /* Bound the number of digits printed by the size of the representation. */
1538 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1539 if (digits == 0 || digits > max_digits)
1540 digits = max_digits;
1542 /* Estimate the decimal exponent, and compute the length of the string it
1543 will print as. Be conservative and add one to account for possible
1544 overflow or rounding error. */
1545 dec_exp = REAL_EXP (&r) * M_LOG10_2;
1546 for (max_digits = 1; dec_exp ; max_digits++)
1547 dec_exp /= 10;
1549 /* Bound the number of digits printed by the size of the output buffer. */
1550 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1551 gcc_assert (max_digits <= buf_size);
1552 if (digits > max_digits)
1553 digits = max_digits;
1555 one = real_digit (1);
1556 ten = ten_to_ptwo (0);
1558 sign = r.sign;
1559 r.sign = 0;
1561 dec_exp = 0;
1562 pten = *one;
1564 cmp_one = do_compare (&r, one, 0);
1565 if (cmp_one > 0)
1567 int m;
1569 /* Number is greater than one. Convert significand to an integer
1570 and strip trailing decimal zeros. */
1572 u = r;
1573 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1575 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1576 m = floor_log2 (max_digits);
1578 /* Iterate over the bits of the possible powers of 10 that might
1579 be present in U and eliminate them. That is, if we find that
1580 10**2**M divides U evenly, keep the division and increase
1581 DEC_EXP by 2**M. */
1584 REAL_VALUE_TYPE t;
1586 do_divide (&t, &u, ten_to_ptwo (m));
1587 do_fix_trunc (&v, &t);
1588 if (cmp_significands (&v, &t) == 0)
1590 u = t;
1591 dec_exp += 1 << m;
1594 while (--m >= 0);
1596 /* Revert the scaling to integer that we performed earlier. */
1597 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1598 - (SIGNIFICAND_BITS - 1));
1599 r = u;
1601 /* Find power of 10. Do this by dividing out 10**2**M when
1602 this is larger than the current remainder. Fill PTEN with
1603 the power of 10 that we compute. */
1604 if (REAL_EXP (&r) > 0)
1606 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1609 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1610 if (do_compare (&u, ptentwo, 0) >= 0)
1612 do_divide (&u, &u, ptentwo);
1613 do_multiply (&pten, &pten, ptentwo);
1614 dec_exp += 1 << m;
1617 while (--m >= 0);
1619 else
1620 /* We managed to divide off enough tens in the above reduction
1621 loop that we've now got a negative exponent. Fall into the
1622 less-than-one code to compute the proper value for PTEN. */
1623 cmp_one = -1;
1625 if (cmp_one < 0)
1627 int m;
1629 /* Number is less than one. Pad significand with leading
1630 decimal zeros. */
1632 v = r;
1633 while (1)
1635 /* Stop if we'd shift bits off the bottom. */
1636 if (v.sig[0] & 7)
1637 break;
1639 do_multiply (&u, &v, ten);
1641 /* Stop if we're now >= 1. */
1642 if (REAL_EXP (&u) > 0)
1643 break;
1645 v = u;
1646 dec_exp -= 1;
1648 r = v;
1650 /* Find power of 10. Do this by multiplying in P=10**2**M when
1651 the current remainder is smaller than 1/P. Fill PTEN with the
1652 power of 10 that we compute. */
1653 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1656 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1657 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1659 if (do_compare (&v, ptenmtwo, 0) <= 0)
1661 do_multiply (&v, &v, ptentwo);
1662 do_multiply (&pten, &pten, ptentwo);
1663 dec_exp -= 1 << m;
1666 while (--m >= 0);
1668 /* Invert the positive power of 10 that we've collected so far. */
1669 do_divide (&pten, one, &pten);
1672 p = str;
1673 if (sign)
1674 *p++ = '-';
1675 first = p++;
1677 /* At this point, PTEN should contain the nearest power of 10 smaller
1678 than R, such that this division produces the first digit.
1680 Using a divide-step primitive that returns the complete integral
1681 remainder avoids the rounding error that would be produced if
1682 we were to use do_divide here and then simply multiply by 10 for
1683 each subsequent digit. */
1685 digit = rtd_divmod (&r, &pten);
1687 /* Be prepared for error in that division via underflow ... */
1688 if (digit == 0 && cmp_significand_0 (&r))
1690 /* Multiply by 10 and try again. */
1691 do_multiply (&r, &r, ten);
1692 digit = rtd_divmod (&r, &pten);
1693 dec_exp -= 1;
1694 gcc_assert (digit != 0);
1697 /* ... or overflow. */
1698 if (digit == 10)
1700 *p++ = '1';
1701 if (--digits > 0)
1702 *p++ = '0';
1703 dec_exp += 1;
1705 else
1707 gcc_assert (digit <= 10);
1708 *p++ = digit + '0';
1711 /* Generate subsequent digits. */
1712 while (--digits > 0)
1714 do_multiply (&r, &r, ten);
1715 digit = rtd_divmod (&r, &pten);
1716 *p++ = digit + '0';
1718 last = p;
1720 /* Generate one more digit with which to do rounding. */
1721 do_multiply (&r, &r, ten);
1722 digit = rtd_divmod (&r, &pten);
1724 /* Round the result. */
1725 if (fmt && fmt->round_towards_zero)
1727 /* If the format uses round towards zero when parsing the string
1728 back in, we need to always round away from zero here. */
1729 if (cmp_significand_0 (&r))
1730 digit++;
1731 round_up = digit > 0;
1733 else
1735 if (digit == 5)
1737 /* Round to nearest. If R is nonzero there are additional
1738 nonzero digits to be extracted. */
1739 if (cmp_significand_0 (&r))
1740 digit++;
1741 /* Round to even. */
1742 else if ((p[-1] - '0') & 1)
1743 digit++;
1746 round_up = digit > 5;
1749 if (round_up)
1751 while (p > first)
1753 digit = *--p;
1754 if (digit == '9')
1755 *p = '0';
1756 else
1758 *p = digit + 1;
1759 break;
1763 /* Carry out of the first digit. This means we had all 9's and
1764 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1765 if (p == first)
1767 first[1] = '1';
1768 dec_exp++;
1772 /* Insert the decimal point. */
1773 first[0] = first[1];
1774 first[1] = '.';
1776 /* If requested, drop trailing zeros. Never crop past "1.0". */
1777 if (crop_trailing_zeros)
1778 while (last > first + 3 && last[-1] == '0')
1779 last--;
1781 /* Append the exponent. */
1782 sprintf (last, "e%+d", dec_exp);
1784 #ifdef ENABLE_CHECKING
1785 /* Verify that we can read the original value back in. */
1786 if (mode != VOIDmode)
1788 real_from_string (&r, str);
1789 real_convert (&r, mode, &r);
1790 gcc_assert (real_identical (&r, r_orig));
1792 #endif
1795 /* Likewise, except always uses round-to-nearest. */
1797 void
1798 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1799 size_t digits, int crop_trailing_zeros)
1801 real_to_decimal_for_mode (str, r_orig, buf_size,
1802 digits, crop_trailing_zeros, VOIDmode);
1805 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1806 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1807 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1808 strip trailing zeros. */
1810 void
1811 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1812 size_t digits, int crop_trailing_zeros)
1814 int i, j, exp = REAL_EXP (r);
1815 char *p, *first;
1816 char exp_buf[16];
1817 size_t max_digits;
1819 switch (r->cl)
1821 case rvc_zero:
1822 exp = 0;
1823 break;
1824 case rvc_normal:
1825 break;
1826 case rvc_inf:
1827 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1828 return;
1829 case rvc_nan:
1830 /* ??? Print the significand as well, if not canonical? */
1831 sprintf (str, "%c%cNaN", (r->sign ? '-' : '+'),
1832 (r->signalling ? 'S' : 'Q'));
1833 return;
1834 default:
1835 gcc_unreachable ();
1838 if (r->decimal)
1840 /* Hexadecimal format for decimal floats is not interesting. */
1841 strcpy (str, "N/A");
1842 return;
1845 if (digits == 0)
1846 digits = SIGNIFICAND_BITS / 4;
1848 /* Bound the number of digits printed by the size of the output buffer. */
1850 sprintf (exp_buf, "p%+d", exp);
1851 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1852 gcc_assert (max_digits <= buf_size);
1853 if (digits > max_digits)
1854 digits = max_digits;
1856 p = str;
1857 if (r->sign)
1858 *p++ = '-';
1859 *p++ = '0';
1860 *p++ = 'x';
1861 *p++ = '0';
1862 *p++ = '.';
1863 first = p;
1865 for (i = SIGSZ - 1; i >= 0; --i)
1866 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1868 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1869 if (--digits == 0)
1870 goto out;
1873 out:
1874 if (crop_trailing_zeros)
1875 while (p > first + 1 && p[-1] == '0')
1876 p--;
1878 sprintf (p, "p%+d", exp);
1881 /* Initialize R from a decimal or hexadecimal string. The string is
1882 assumed to have been syntax checked already. Return -1 if the
1883 value underflows, +1 if overflows, and 0 otherwise. */
1886 real_from_string (REAL_VALUE_TYPE *r, const char *str)
1888 int exp = 0;
1889 bool sign = false;
1891 get_zero (r, 0);
1893 if (*str == '-')
1895 sign = true;
1896 str++;
1898 else if (*str == '+')
1899 str++;
1901 if (!strncmp (str, "QNaN", 4))
1903 get_canonical_qnan (r, sign);
1904 return 0;
1906 else if (!strncmp (str, "SNaN", 4))
1908 get_canonical_snan (r, sign);
1909 return 0;
1911 else if (!strncmp (str, "Inf", 3))
1913 get_inf (r, sign);
1914 return 0;
1917 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1919 /* Hexadecimal floating point. */
1920 int pos = SIGNIFICAND_BITS - 4, d;
1922 str += 2;
1924 while (*str == '0')
1925 str++;
1926 while (1)
1928 d = hex_value (*str);
1929 if (d == _hex_bad)
1930 break;
1931 if (pos >= 0)
1933 r->sig[pos / HOST_BITS_PER_LONG]
1934 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1935 pos -= 4;
1937 else if (d)
1938 /* Ensure correct rounding by setting last bit if there is
1939 a subsequent nonzero digit. */
1940 r->sig[0] |= 1;
1941 exp += 4;
1942 str++;
1944 if (*str == '.')
1946 str++;
1947 if (pos == SIGNIFICAND_BITS - 4)
1949 while (*str == '0')
1950 str++, exp -= 4;
1952 while (1)
1954 d = hex_value (*str);
1955 if (d == _hex_bad)
1956 break;
1957 if (pos >= 0)
1959 r->sig[pos / HOST_BITS_PER_LONG]
1960 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1961 pos -= 4;
1963 else if (d)
1964 /* Ensure correct rounding by setting last bit if there is
1965 a subsequent nonzero digit. */
1966 r->sig[0] |= 1;
1967 str++;
1971 /* If the mantissa is zero, ignore the exponent. */
1972 if (!cmp_significand_0 (r))
1973 goto is_a_zero;
1975 if (*str == 'p' || *str == 'P')
1977 bool exp_neg = false;
1979 str++;
1980 if (*str == '-')
1982 exp_neg = true;
1983 str++;
1985 else if (*str == '+')
1986 str++;
1988 d = 0;
1989 while (ISDIGIT (*str))
1991 d *= 10;
1992 d += *str - '0';
1993 if (d > MAX_EXP)
1995 /* Overflowed the exponent. */
1996 if (exp_neg)
1997 goto underflow;
1998 else
1999 goto overflow;
2001 str++;
2003 if (exp_neg)
2004 d = -d;
2006 exp += d;
2009 r->cl = rvc_normal;
2010 SET_REAL_EXP (r, exp);
2012 normalize (r);
2014 else
2016 /* Decimal floating point. */
2017 const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
2018 int d;
2020 while (*str == '0')
2021 str++;
2022 while (ISDIGIT (*str))
2024 d = *str++ - '0';
2025 do_multiply (r, r, ten);
2026 if (d)
2027 do_add (r, r, real_digit (d), 0);
2029 if (*str == '.')
2031 str++;
2032 if (r->cl == rvc_zero)
2034 while (*str == '0')
2035 str++, exp--;
2037 while (ISDIGIT (*str))
2039 d = *str++ - '0';
2040 do_multiply (r, r, ten);
2041 if (d)
2042 do_add (r, r, real_digit (d), 0);
2043 exp--;
2047 /* If the mantissa is zero, ignore the exponent. */
2048 if (r->cl == rvc_zero)
2049 goto is_a_zero;
2051 if (*str == 'e' || *str == 'E')
2053 bool exp_neg = false;
2055 str++;
2056 if (*str == '-')
2058 exp_neg = true;
2059 str++;
2061 else if (*str == '+')
2062 str++;
2064 d = 0;
2065 while (ISDIGIT (*str))
2067 d *= 10;
2068 d += *str - '0';
2069 if (d > MAX_EXP)
2071 /* Overflowed the exponent. */
2072 if (exp_neg)
2073 goto underflow;
2074 else
2075 goto overflow;
2077 str++;
2079 if (exp_neg)
2080 d = -d;
2081 exp += d;
2084 if (exp)
2085 times_pten (r, exp);
2088 r->sign = sign;
2089 return 0;
2091 is_a_zero:
2092 get_zero (r, sign);
2093 return 0;
2095 underflow:
2096 get_zero (r, sign);
2097 return -1;
2099 overflow:
2100 get_inf (r, sign);
2101 return 1;
2104 /* Legacy. Similar, but return the result directly. */
2106 REAL_VALUE_TYPE
2107 real_from_string2 (const char *s, enum machine_mode mode)
2109 REAL_VALUE_TYPE r;
2111 real_from_string (&r, s);
2112 if (mode != VOIDmode)
2113 real_convert (&r, mode, &r);
2115 return r;
2118 /* Initialize R from string S and desired MODE. */
2120 void
2121 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, enum machine_mode mode)
2123 if (DECIMAL_FLOAT_MODE_P (mode))
2124 decimal_real_from_string (r, s);
2125 else
2126 real_from_string (r, s);
2128 if (mode != VOIDmode)
2129 real_convert (r, mode, r);
2132 /* Initialize R from the integer pair HIGH+LOW. */
2134 void
2135 real_from_integer (REAL_VALUE_TYPE *r, enum machine_mode mode,
2136 unsigned HOST_WIDE_INT low, HOST_WIDE_INT high,
2137 int unsigned_p)
2139 if (low == 0 && high == 0)
2140 get_zero (r, 0);
2141 else
2143 memset (r, 0, sizeof (*r));
2144 r->cl = rvc_normal;
2145 r->sign = high < 0 && !unsigned_p;
2146 SET_REAL_EXP (r, 2 * HOST_BITS_PER_WIDE_INT);
2148 if (r->sign)
2150 high = ~high;
2151 if (low == 0)
2152 high += 1;
2153 else
2154 low = -low;
2157 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2159 r->sig[SIGSZ-1] = high;
2160 r->sig[SIGSZ-2] = low;
2162 else
2164 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2165 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
2166 r->sig[SIGSZ-2] = high;
2167 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
2168 r->sig[SIGSZ-4] = low;
2171 normalize (r);
2174 if (DECIMAL_FLOAT_MODE_P (mode))
2175 decimal_from_integer (r);
2176 else if (mode != VOIDmode)
2177 real_convert (r, mode, r);
2180 /* Render R, an integral value, as a floating point constant with no
2181 specified exponent. */
2183 static void
2184 decimal_integer_string (char *str, const REAL_VALUE_TYPE *r_orig,
2185 size_t buf_size)
2187 int dec_exp, digit, digits;
2188 REAL_VALUE_TYPE r, pten;
2189 char *p;
2190 bool sign;
2192 r = *r_orig;
2194 if (r.cl == rvc_zero)
2196 strcpy (str, "0.");
2197 return;
2200 sign = r.sign;
2201 r.sign = 0;
2203 dec_exp = REAL_EXP (&r) * M_LOG10_2;
2204 digits = dec_exp + 1;
2205 gcc_assert ((digits + 2) < (int)buf_size);
2207 pten = *real_digit (1);
2208 times_pten (&pten, dec_exp);
2210 p = str;
2211 if (sign)
2212 *p++ = '-';
2214 digit = rtd_divmod (&r, &pten);
2215 gcc_assert (digit >= 0 && digit <= 9);
2216 *p++ = digit + '0';
2217 while (--digits > 0)
2219 times_pten (&r, 1);
2220 digit = rtd_divmod (&r, &pten);
2221 *p++ = digit + '0';
2223 *p++ = '.';
2224 *p++ = '\0';
2227 /* Convert a real with an integral value to decimal float. */
2229 static void
2230 decimal_from_integer (REAL_VALUE_TYPE *r)
2232 char str[256];
2234 decimal_integer_string (str, r, sizeof (str) - 1);
2235 decimal_real_from_string (r, str);
2238 /* Returns 10**2**N. */
2240 static const REAL_VALUE_TYPE *
2241 ten_to_ptwo (int n)
2243 static REAL_VALUE_TYPE tens[EXP_BITS];
2245 gcc_assert (n >= 0);
2246 gcc_assert (n < EXP_BITS);
2248 if (tens[n].cl == rvc_zero)
2250 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2252 HOST_WIDE_INT t = 10;
2253 int i;
2255 for (i = 0; i < n; ++i)
2256 t *= t;
2258 real_from_integer (&tens[n], VOIDmode, t, 0, 1);
2260 else
2262 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2263 do_multiply (&tens[n], t, t);
2267 return &tens[n];
2270 /* Returns 10**(-2**N). */
2272 static const REAL_VALUE_TYPE *
2273 ten_to_mptwo (int n)
2275 static REAL_VALUE_TYPE tens[EXP_BITS];
2277 gcc_assert (n >= 0);
2278 gcc_assert (n < EXP_BITS);
2280 if (tens[n].cl == rvc_zero)
2281 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2283 return &tens[n];
2286 /* Returns N. */
2288 static const REAL_VALUE_TYPE *
2289 real_digit (int n)
2291 static REAL_VALUE_TYPE num[10];
2293 gcc_assert (n >= 0);
2294 gcc_assert (n <= 9);
2296 if (n > 0 && num[n].cl == rvc_zero)
2297 real_from_integer (&num[n], VOIDmode, n, 0, 1);
2299 return &num[n];
2302 /* Multiply R by 10**EXP. */
2304 static void
2305 times_pten (REAL_VALUE_TYPE *r, int exp)
2307 REAL_VALUE_TYPE pten, *rr;
2308 bool negative = (exp < 0);
2309 int i;
2311 if (negative)
2313 exp = -exp;
2314 pten = *real_digit (1);
2315 rr = &pten;
2317 else
2318 rr = r;
2320 for (i = 0; exp > 0; ++i, exp >>= 1)
2321 if (exp & 1)
2322 do_multiply (rr, rr, ten_to_ptwo (i));
2324 if (negative)
2325 do_divide (r, r, &pten);
2328 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2330 const REAL_VALUE_TYPE *
2331 dconst_e_ptr (void)
2333 static REAL_VALUE_TYPE value;
2335 /* Initialize mathematical constants for constant folding builtins.
2336 These constants need to be given to at least 160 bits precision. */
2337 if (value.cl == rvc_zero)
2339 mpfr_t m;
2340 mpfr_init2 (m, SIGNIFICAND_BITS);
2341 mpfr_set_ui (m, 1, GMP_RNDN);
2342 mpfr_exp (m, m, GMP_RNDN);
2343 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2344 mpfr_clear (m);
2347 return &value;
2350 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2352 const REAL_VALUE_TYPE *
2353 dconst_third_ptr (void)
2355 static REAL_VALUE_TYPE value;
2357 /* Initialize mathematical constants for constant folding builtins.
2358 These constants need to be given to at least 160 bits precision. */
2359 if (value.cl == rvc_zero)
2361 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (3));
2363 return &value;
2366 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2368 const REAL_VALUE_TYPE *
2369 dconst_sqrt2_ptr (void)
2371 static REAL_VALUE_TYPE value;
2373 /* Initialize mathematical constants for constant folding builtins.
2374 These constants need to be given to at least 160 bits precision. */
2375 if (value.cl == rvc_zero)
2377 mpfr_t m;
2378 mpfr_init2 (m, SIGNIFICAND_BITS);
2379 mpfr_sqrt_ui (m, 2, GMP_RNDN);
2380 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2381 mpfr_clear (m);
2383 return &value;
2386 /* Fills R with +Inf. */
2388 void
2389 real_inf (REAL_VALUE_TYPE *r)
2391 get_inf (r, 0);
2394 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2395 we force a QNaN, else we force an SNaN. The string, if not empty,
2396 is parsed as a number and placed in the significand. Return true
2397 if the string was successfully parsed. */
2399 bool
2400 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2401 enum machine_mode mode)
2403 const struct real_format *fmt;
2405 fmt = REAL_MODE_FORMAT (mode);
2406 gcc_assert (fmt);
2408 if (*str == 0)
2410 if (quiet)
2411 get_canonical_qnan (r, 0);
2412 else
2413 get_canonical_snan (r, 0);
2415 else
2417 int base = 10, d;
2419 memset (r, 0, sizeof (*r));
2420 r->cl = rvc_nan;
2422 /* Parse akin to strtol into the significand of R. */
2424 while (ISSPACE (*str))
2425 str++;
2426 if (*str == '-')
2427 str++;
2428 else if (*str == '+')
2429 str++;
2430 if (*str == '0')
2432 str++;
2433 if (*str == 'x' || *str == 'X')
2435 base = 16;
2436 str++;
2438 else
2439 base = 8;
2442 while ((d = hex_value (*str)) < base)
2444 REAL_VALUE_TYPE u;
2446 switch (base)
2448 case 8:
2449 lshift_significand (r, r, 3);
2450 break;
2451 case 16:
2452 lshift_significand (r, r, 4);
2453 break;
2454 case 10:
2455 lshift_significand_1 (&u, r);
2456 lshift_significand (r, r, 3);
2457 add_significands (r, r, &u);
2458 break;
2459 default:
2460 gcc_unreachable ();
2463 get_zero (&u, 0);
2464 u.sig[0] = d;
2465 add_significands (r, r, &u);
2467 str++;
2470 /* Must have consumed the entire string for success. */
2471 if (*str != 0)
2472 return false;
2474 /* Shift the significand into place such that the bits
2475 are in the most significant bits for the format. */
2476 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2478 /* Our MSB is always unset for NaNs. */
2479 r->sig[SIGSZ-1] &= ~SIG_MSB;
2481 /* Force quiet or signalling NaN. */
2482 r->signalling = !quiet;
2485 return true;
2488 /* Fills R with the largest finite value representable in mode MODE.
2489 If SIGN is nonzero, R is set to the most negative finite value. */
2491 void
2492 real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
2494 const struct real_format *fmt;
2495 int np2;
2497 fmt = REAL_MODE_FORMAT (mode);
2498 gcc_assert (fmt);
2499 memset (r, 0, sizeof (*r));
2501 if (fmt->b == 10)
2502 decimal_real_maxval (r, sign, mode);
2503 else
2505 r->cl = rvc_normal;
2506 r->sign = sign;
2507 SET_REAL_EXP (r, fmt->emax);
2509 np2 = SIGNIFICAND_BITS - fmt->p;
2510 memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2511 clear_significand_below (r, np2);
2513 if (fmt->pnan < fmt->p)
2514 /* This is an IBM extended double format made up of two IEEE
2515 doubles. The value of the long double is the sum of the
2516 values of the two parts. The most significant part is
2517 required to be the value of the long double rounded to the
2518 nearest double. Rounding means we need a slightly smaller
2519 value for LDBL_MAX. */
2520 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1);
2524 /* Fills R with 2**N. */
2526 void
2527 real_2expN (REAL_VALUE_TYPE *r, int n, enum machine_mode fmode)
2529 memset (r, 0, sizeof (*r));
2531 n++;
2532 if (n > MAX_EXP)
2533 r->cl = rvc_inf;
2534 else if (n < -MAX_EXP)
2536 else
2538 r->cl = rvc_normal;
2539 SET_REAL_EXP (r, n);
2540 r->sig[SIGSZ-1] = SIG_MSB;
2542 if (DECIMAL_FLOAT_MODE_P (fmode))
2543 decimal_real_convert (r, fmode, r);
2547 static void
2548 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2550 int p2, np2, i, w;
2551 int emin2m1, emax2;
2552 bool round_up = false;
2554 if (r->decimal)
2556 if (fmt->b == 10)
2558 decimal_round_for_format (fmt, r);
2559 return;
2561 /* FIXME. We can come here via fp_easy_constant
2562 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2563 investigated whether this convert needs to be here, or
2564 something else is missing. */
2565 decimal_real_convert (r, DFmode, r);
2568 p2 = fmt->p;
2569 emin2m1 = fmt->emin - 1;
2570 emax2 = fmt->emax;
2572 np2 = SIGNIFICAND_BITS - p2;
2573 switch (r->cl)
2575 underflow:
2576 get_zero (r, r->sign);
2577 case rvc_zero:
2578 if (!fmt->has_signed_zero)
2579 r->sign = 0;
2580 return;
2582 overflow:
2583 get_inf (r, r->sign);
2584 case rvc_inf:
2585 return;
2587 case rvc_nan:
2588 clear_significand_below (r, np2);
2589 return;
2591 case rvc_normal:
2592 break;
2594 default:
2595 gcc_unreachable ();
2598 /* Check the range of the exponent. If we're out of range,
2599 either underflow or overflow. */
2600 if (REAL_EXP (r) > emax2)
2601 goto overflow;
2602 else if (REAL_EXP (r) <= emin2m1)
2604 int diff;
2606 if (!fmt->has_denorm)
2608 /* Don't underflow completely until we've had a chance to round. */
2609 if (REAL_EXP (r) < emin2m1)
2610 goto underflow;
2612 else
2614 diff = emin2m1 - REAL_EXP (r) + 1;
2615 if (diff > p2)
2616 goto underflow;
2618 /* De-normalize the significand. */
2619 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2620 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2624 if (!fmt->round_towards_zero)
2626 /* There are P2 true significand bits, followed by one guard bit,
2627 followed by one sticky bit, followed by stuff. Fold nonzero
2628 stuff into the sticky bit. */
2629 unsigned long sticky;
2630 bool guard, lsb;
2632 sticky = 0;
2633 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2634 sticky |= r->sig[i];
2635 sticky |= r->sig[w]
2636 & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2638 guard = test_significand_bit (r, np2 - 1);
2639 lsb = test_significand_bit (r, np2);
2641 /* Round to even. */
2642 round_up = guard && (sticky || lsb);
2645 if (round_up)
2647 REAL_VALUE_TYPE u;
2648 get_zero (&u, 0);
2649 set_significand_bit (&u, np2);
2651 if (add_significands (r, r, &u))
2653 /* Overflow. Means the significand had been all ones, and
2654 is now all zeros. Need to increase the exponent, and
2655 possibly re-normalize it. */
2656 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2657 if (REAL_EXP (r) > emax2)
2658 goto overflow;
2659 r->sig[SIGSZ-1] = SIG_MSB;
2663 /* Catch underflow that we deferred until after rounding. */
2664 if (REAL_EXP (r) <= emin2m1)
2665 goto underflow;
2667 /* Clear out trailing garbage. */
2668 clear_significand_below (r, np2);
2671 /* Extend or truncate to a new mode. */
2673 void
2674 real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
2675 const REAL_VALUE_TYPE *a)
2677 const struct real_format *fmt;
2679 fmt = REAL_MODE_FORMAT (mode);
2680 gcc_assert (fmt);
2682 *r = *a;
2684 if (a->decimal || fmt->b == 10)
2685 decimal_real_convert (r, mode, a);
2687 round_for_format (fmt, r);
2689 /* round_for_format de-normalizes denormals. Undo just that part. */
2690 if (r->cl == rvc_normal)
2691 normalize (r);
2694 /* Legacy. Likewise, except return the struct directly. */
2696 REAL_VALUE_TYPE
2697 real_value_truncate (enum machine_mode mode, REAL_VALUE_TYPE a)
2699 REAL_VALUE_TYPE r;
2700 real_convert (&r, mode, &a);
2701 return r;
2704 /* Return true if truncating to MODE is exact. */
2706 bool
2707 exact_real_truncate (enum machine_mode mode, const REAL_VALUE_TYPE *a)
2709 const struct real_format *fmt;
2710 REAL_VALUE_TYPE t;
2711 int emin2m1;
2713 fmt = REAL_MODE_FORMAT (mode);
2714 gcc_assert (fmt);
2716 /* Don't allow conversion to denormals. */
2717 emin2m1 = fmt->emin - 1;
2718 if (REAL_EXP (a) <= emin2m1)
2719 return false;
2721 /* After conversion to the new mode, the value must be identical. */
2722 real_convert (&t, mode, a);
2723 return real_identical (&t, a);
2726 /* Write R to the given target format. Place the words of the result
2727 in target word order in BUF. There are always 32 bits in each
2728 long, no matter the size of the host long.
2730 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2732 long
2733 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
2734 const struct real_format *fmt)
2736 REAL_VALUE_TYPE r;
2737 long buf1;
2739 r = *r_orig;
2740 round_for_format (fmt, &r);
2742 if (!buf)
2743 buf = &buf1;
2744 (*fmt->encode) (fmt, buf, &r);
2746 return *buf;
2749 /* Similar, but look up the format from MODE. */
2751 long
2752 real_to_target (long *buf, const REAL_VALUE_TYPE *r, enum machine_mode mode)
2754 const struct real_format *fmt;
2756 fmt = REAL_MODE_FORMAT (mode);
2757 gcc_assert (fmt);
2759 return real_to_target_fmt (buf, r, fmt);
2762 /* Read R from the given target format. Read the words of the result
2763 in target word order in BUF. There are always 32 bits in each
2764 long, no matter the size of the host long. */
2766 void
2767 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
2768 const struct real_format *fmt)
2770 (*fmt->decode) (fmt, r, buf);
2773 /* Similar, but look up the format from MODE. */
2775 void
2776 real_from_target (REAL_VALUE_TYPE *r, const long *buf, enum machine_mode mode)
2778 const struct real_format *fmt;
2780 fmt = REAL_MODE_FORMAT (mode);
2781 gcc_assert (fmt);
2783 (*fmt->decode) (fmt, r, buf);
2786 /* Return the number of bits of the largest binary value that the
2787 significand of MODE will hold. */
2788 /* ??? Legacy. Should get access to real_format directly. */
2791 significand_size (enum machine_mode mode)
2793 const struct real_format *fmt;
2795 fmt = REAL_MODE_FORMAT (mode);
2796 if (fmt == NULL)
2797 return 0;
2799 if (fmt->b == 10)
2801 /* Return the size in bits of the largest binary value that can be
2802 held by the decimal coefficient for this mode. This is one more
2803 than the number of bits required to hold the largest coefficient
2804 of this mode. */
2805 double log2_10 = 3.3219281;
2806 return fmt->p * log2_10;
2808 return fmt->p;
2811 /* Return a hash value for the given real value. */
2812 /* ??? The "unsigned int" return value is intended to be hashval_t,
2813 but I didn't want to pull hashtab.h into real.h. */
2815 unsigned int
2816 real_hash (const REAL_VALUE_TYPE *r)
2818 unsigned int h;
2819 size_t i;
2821 h = r->cl | (r->sign << 2);
2822 switch (r->cl)
2824 case rvc_zero:
2825 case rvc_inf:
2826 return h;
2828 case rvc_normal:
2829 h |= REAL_EXP (r) << 3;
2830 break;
2832 case rvc_nan:
2833 if (r->signalling)
2834 h ^= (unsigned int)-1;
2835 if (r->canonical)
2836 return h;
2837 break;
2839 default:
2840 gcc_unreachable ();
2843 if (sizeof(unsigned long) > sizeof(unsigned int))
2844 for (i = 0; i < SIGSZ; ++i)
2846 unsigned long s = r->sig[i];
2847 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2849 else
2850 for (i = 0; i < SIGSZ; ++i)
2851 h ^= r->sig[i];
2853 return h;
2856 /* IEEE single-precision format. */
2858 static void encode_ieee_single (const struct real_format *fmt,
2859 long *, const REAL_VALUE_TYPE *);
2860 static void decode_ieee_single (const struct real_format *,
2861 REAL_VALUE_TYPE *, const long *);
2863 static void
2864 encode_ieee_single (const struct real_format *fmt, long *buf,
2865 const REAL_VALUE_TYPE *r)
2867 unsigned long image, sig, exp;
2868 unsigned long sign = r->sign;
2869 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2871 image = sign << 31;
2872 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2874 switch (r->cl)
2876 case rvc_zero:
2877 break;
2879 case rvc_inf:
2880 if (fmt->has_inf)
2881 image |= 255 << 23;
2882 else
2883 image |= 0x7fffffff;
2884 break;
2886 case rvc_nan:
2887 if (fmt->has_nans)
2889 if (r->canonical)
2890 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
2891 if (r->signalling == fmt->qnan_msb_set)
2892 sig &= ~(1 << 22);
2893 else
2894 sig |= 1 << 22;
2895 if (sig == 0)
2896 sig = 1 << 21;
2898 image |= 255 << 23;
2899 image |= sig;
2901 else
2902 image |= 0x7fffffff;
2903 break;
2905 case rvc_normal:
2906 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2907 whereas the intermediate representation is 0.F x 2**exp.
2908 Which means we're off by one. */
2909 if (denormal)
2910 exp = 0;
2911 else
2912 exp = REAL_EXP (r) + 127 - 1;
2913 image |= exp << 23;
2914 image |= sig;
2915 break;
2917 default:
2918 gcc_unreachable ();
2921 buf[0] = image;
2924 static void
2925 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2926 const long *buf)
2928 unsigned long image = buf[0] & 0xffffffff;
2929 bool sign = (image >> 31) & 1;
2930 int exp = (image >> 23) & 0xff;
2932 memset (r, 0, sizeof (*r));
2933 image <<= HOST_BITS_PER_LONG - 24;
2934 image &= ~SIG_MSB;
2936 if (exp == 0)
2938 if (image && fmt->has_denorm)
2940 r->cl = rvc_normal;
2941 r->sign = sign;
2942 SET_REAL_EXP (r, -126);
2943 r->sig[SIGSZ-1] = image << 1;
2944 normalize (r);
2946 else if (fmt->has_signed_zero)
2947 r->sign = sign;
2949 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2951 if (image)
2953 r->cl = rvc_nan;
2954 r->sign = sign;
2955 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
2956 ^ fmt->qnan_msb_set);
2957 r->sig[SIGSZ-1] = image;
2959 else
2961 r->cl = rvc_inf;
2962 r->sign = sign;
2965 else
2967 r->cl = rvc_normal;
2968 r->sign = sign;
2969 SET_REAL_EXP (r, exp - 127 + 1);
2970 r->sig[SIGSZ-1] = image | SIG_MSB;
2974 const struct real_format ieee_single_format =
2976 encode_ieee_single,
2977 decode_ieee_single,
2981 -125,
2982 128,
2985 false,
2986 true,
2987 true,
2988 true,
2989 true,
2990 true,
2991 true,
2992 false
2995 const struct real_format mips_single_format =
2997 encode_ieee_single,
2998 decode_ieee_single,
3002 -125,
3003 128,
3006 false,
3007 true,
3008 true,
3009 true,
3010 true,
3011 true,
3012 false,
3013 true
3016 const struct real_format motorola_single_format =
3018 encode_ieee_single,
3019 decode_ieee_single,
3023 -125,
3024 128,
3027 false,
3028 true,
3029 true,
3030 true,
3031 true,
3032 true,
3033 true,
3034 true
3037 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3038 single precision with the following differences:
3039 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3040 are generated.
3041 - NaNs are not supported.
3042 - The range of non-zero numbers in binary is
3043 (001)[1.]000...000 to (255)[1.]111...111.
3044 - Denormals can be represented, but are treated as +0.0 when
3045 used as an operand and are never generated as a result.
3046 - -0.0 can be represented, but a zero result is always +0.0.
3047 - the only supported rounding mode is trunction (towards zero). */
3048 const struct real_format spu_single_format =
3050 encode_ieee_single,
3051 decode_ieee_single,
3055 -125,
3056 129,
3059 true,
3060 false,
3061 false,
3062 false,
3063 true,
3064 true,
3065 false,
3066 false
3069 /* IEEE double-precision format. */
3071 static void encode_ieee_double (const struct real_format *fmt,
3072 long *, const REAL_VALUE_TYPE *);
3073 static void decode_ieee_double (const struct real_format *,
3074 REAL_VALUE_TYPE *, const long *);
3076 static void
3077 encode_ieee_double (const struct real_format *fmt, long *buf,
3078 const REAL_VALUE_TYPE *r)
3080 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
3081 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3083 image_hi = r->sign << 31;
3084 image_lo = 0;
3086 if (HOST_BITS_PER_LONG == 64)
3088 sig_hi = r->sig[SIGSZ-1];
3089 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
3090 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
3092 else
3094 sig_hi = r->sig[SIGSZ-1];
3095 sig_lo = r->sig[SIGSZ-2];
3096 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
3097 sig_hi = (sig_hi >> 11) & 0xfffff;
3100 switch (r->cl)
3102 case rvc_zero:
3103 break;
3105 case rvc_inf:
3106 if (fmt->has_inf)
3107 image_hi |= 2047 << 20;
3108 else
3110 image_hi |= 0x7fffffff;
3111 image_lo = 0xffffffff;
3113 break;
3115 case rvc_nan:
3116 if (fmt->has_nans)
3118 if (r->canonical)
3120 if (fmt->canonical_nan_lsbs_set)
3122 sig_hi = (1 << 19) - 1;
3123 sig_lo = 0xffffffff;
3125 else
3127 sig_hi = 0;
3128 sig_lo = 0;
3131 if (r->signalling == fmt->qnan_msb_set)
3132 sig_hi &= ~(1 << 19);
3133 else
3134 sig_hi |= 1 << 19;
3135 if (sig_hi == 0 && sig_lo == 0)
3136 sig_hi = 1 << 18;
3138 image_hi |= 2047 << 20;
3139 image_hi |= sig_hi;
3140 image_lo = sig_lo;
3142 else
3144 image_hi |= 0x7fffffff;
3145 image_lo = 0xffffffff;
3147 break;
3149 case rvc_normal:
3150 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3151 whereas the intermediate representation is 0.F x 2**exp.
3152 Which means we're off by one. */
3153 if (denormal)
3154 exp = 0;
3155 else
3156 exp = REAL_EXP (r) + 1023 - 1;
3157 image_hi |= exp << 20;
3158 image_hi |= sig_hi;
3159 image_lo = sig_lo;
3160 break;
3162 default:
3163 gcc_unreachable ();
3166 if (FLOAT_WORDS_BIG_ENDIAN)
3167 buf[0] = image_hi, buf[1] = image_lo;
3168 else
3169 buf[0] = image_lo, buf[1] = image_hi;
3172 static void
3173 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3174 const long *buf)
3176 unsigned long image_hi, image_lo;
3177 bool sign;
3178 int exp;
3180 if (FLOAT_WORDS_BIG_ENDIAN)
3181 image_hi = buf[0], image_lo = buf[1];
3182 else
3183 image_lo = buf[0], image_hi = buf[1];
3184 image_lo &= 0xffffffff;
3185 image_hi &= 0xffffffff;
3187 sign = (image_hi >> 31) & 1;
3188 exp = (image_hi >> 20) & 0x7ff;
3190 memset (r, 0, sizeof (*r));
3192 image_hi <<= 32 - 21;
3193 image_hi |= image_lo >> 21;
3194 image_hi &= 0x7fffffff;
3195 image_lo <<= 32 - 21;
3197 if (exp == 0)
3199 if ((image_hi || image_lo) && fmt->has_denorm)
3201 r->cl = rvc_normal;
3202 r->sign = sign;
3203 SET_REAL_EXP (r, -1022);
3204 if (HOST_BITS_PER_LONG == 32)
3206 image_hi = (image_hi << 1) | (image_lo >> 31);
3207 image_lo <<= 1;
3208 r->sig[SIGSZ-1] = image_hi;
3209 r->sig[SIGSZ-2] = image_lo;
3211 else
3213 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
3214 r->sig[SIGSZ-1] = image_hi;
3216 normalize (r);
3218 else if (fmt->has_signed_zero)
3219 r->sign = sign;
3221 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
3223 if (image_hi || image_lo)
3225 r->cl = rvc_nan;
3226 r->sign = sign;
3227 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3228 if (HOST_BITS_PER_LONG == 32)
3230 r->sig[SIGSZ-1] = image_hi;
3231 r->sig[SIGSZ-2] = image_lo;
3233 else
3234 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3236 else
3238 r->cl = rvc_inf;
3239 r->sign = sign;
3242 else
3244 r->cl = rvc_normal;
3245 r->sign = sign;
3246 SET_REAL_EXP (r, exp - 1023 + 1);
3247 if (HOST_BITS_PER_LONG == 32)
3249 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3250 r->sig[SIGSZ-2] = image_lo;
3252 else
3253 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3257 const struct real_format ieee_double_format =
3259 encode_ieee_double,
3260 decode_ieee_double,
3264 -1021,
3265 1024,
3268 false,
3269 true,
3270 true,
3271 true,
3272 true,
3273 true,
3274 true,
3275 false
3278 const struct real_format mips_double_format =
3280 encode_ieee_double,
3281 decode_ieee_double,
3285 -1021,
3286 1024,
3289 false,
3290 true,
3291 true,
3292 true,
3293 true,
3294 true,
3295 false,
3296 true
3299 const struct real_format motorola_double_format =
3301 encode_ieee_double,
3302 decode_ieee_double,
3306 -1021,
3307 1024,
3310 false,
3311 true,
3312 true,
3313 true,
3314 true,
3315 true,
3316 true,
3317 true
3320 /* IEEE extended real format. This comes in three flavors: Intel's as
3321 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3322 12- and 16-byte images may be big- or little endian; Motorola's is
3323 always big endian. */
3325 /* Helper subroutine which converts from the internal format to the
3326 12-byte little-endian Intel format. Functions below adjust this
3327 for the other possible formats. */
3328 static void
3329 encode_ieee_extended (const struct real_format *fmt, long *buf,
3330 const REAL_VALUE_TYPE *r)
3332 unsigned long image_hi, sig_hi, sig_lo;
3333 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3335 image_hi = r->sign << 15;
3336 sig_hi = sig_lo = 0;
3338 switch (r->cl)
3340 case rvc_zero:
3341 break;
3343 case rvc_inf:
3344 if (fmt->has_inf)
3346 image_hi |= 32767;
3348 /* Intel requires the explicit integer bit to be set, otherwise
3349 it considers the value a "pseudo-infinity". Motorola docs
3350 say it doesn't care. */
3351 sig_hi = 0x80000000;
3353 else
3355 image_hi |= 32767;
3356 sig_lo = sig_hi = 0xffffffff;
3358 break;
3360 case rvc_nan:
3361 if (fmt->has_nans)
3363 image_hi |= 32767;
3364 if (r->canonical)
3366 if (fmt->canonical_nan_lsbs_set)
3368 sig_hi = (1 << 30) - 1;
3369 sig_lo = 0xffffffff;
3372 else if (HOST_BITS_PER_LONG == 32)
3374 sig_hi = r->sig[SIGSZ-1];
3375 sig_lo = r->sig[SIGSZ-2];
3377 else
3379 sig_lo = r->sig[SIGSZ-1];
3380 sig_hi = sig_lo >> 31 >> 1;
3381 sig_lo &= 0xffffffff;
3383 if (r->signalling == fmt->qnan_msb_set)
3384 sig_hi &= ~(1 << 30);
3385 else
3386 sig_hi |= 1 << 30;
3387 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3388 sig_hi = 1 << 29;
3390 /* Intel requires the explicit integer bit to be set, otherwise
3391 it considers the value a "pseudo-nan". Motorola docs say it
3392 doesn't care. */
3393 sig_hi |= 0x80000000;
3395 else
3397 image_hi |= 32767;
3398 sig_lo = sig_hi = 0xffffffff;
3400 break;
3402 case rvc_normal:
3404 int exp = REAL_EXP (r);
3406 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3407 whereas the intermediate representation is 0.F x 2**exp.
3408 Which means we're off by one.
3410 Except for Motorola, which consider exp=0 and explicit
3411 integer bit set to continue to be normalized. In theory
3412 this discrepancy has been taken care of by the difference
3413 in fmt->emin in round_for_format. */
3415 if (denormal)
3416 exp = 0;
3417 else
3419 exp += 16383 - 1;
3420 gcc_assert (exp >= 0);
3422 image_hi |= exp;
3424 if (HOST_BITS_PER_LONG == 32)
3426 sig_hi = r->sig[SIGSZ-1];
3427 sig_lo = r->sig[SIGSZ-2];
3429 else
3431 sig_lo = r->sig[SIGSZ-1];
3432 sig_hi = sig_lo >> 31 >> 1;
3433 sig_lo &= 0xffffffff;
3436 break;
3438 default:
3439 gcc_unreachable ();
3442 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3445 /* Convert from the internal format to the 12-byte Motorola format
3446 for an IEEE extended real. */
3447 static void
3448 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3449 const REAL_VALUE_TYPE *r)
3451 long intermed[3];
3452 encode_ieee_extended (fmt, intermed, r);
3454 /* Motorola chips are assumed always to be big-endian. Also, the
3455 padding in a Motorola extended real goes between the exponent and
3456 the mantissa. At this point the mantissa is entirely within
3457 elements 0 and 1 of intermed, and the exponent entirely within
3458 element 2, so all we have to do is swap the order around, and
3459 shift element 2 left 16 bits. */
3460 buf[0] = intermed[2] << 16;
3461 buf[1] = intermed[1];
3462 buf[2] = intermed[0];
3465 /* Convert from the internal format to the 12-byte Intel format for
3466 an IEEE extended real. */
3467 static void
3468 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3469 const REAL_VALUE_TYPE *r)
3471 if (FLOAT_WORDS_BIG_ENDIAN)
3473 /* All the padding in an Intel-format extended real goes at the high
3474 end, which in this case is after the mantissa, not the exponent.
3475 Therefore we must shift everything down 16 bits. */
3476 long intermed[3];
3477 encode_ieee_extended (fmt, intermed, r);
3478 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3479 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3480 buf[2] = (intermed[0] << 16);
3482 else
3483 /* encode_ieee_extended produces what we want directly. */
3484 encode_ieee_extended (fmt, buf, r);
3487 /* Convert from the internal format to the 16-byte Intel format for
3488 an IEEE extended real. */
3489 static void
3490 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3491 const REAL_VALUE_TYPE *r)
3493 /* All the padding in an Intel-format extended real goes at the high end. */
3494 encode_ieee_extended_intel_96 (fmt, buf, r);
3495 buf[3] = 0;
3498 /* As above, we have a helper function which converts from 12-byte
3499 little-endian Intel format to internal format. Functions below
3500 adjust for the other possible formats. */
3501 static void
3502 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3503 const long *buf)
3505 unsigned long image_hi, sig_hi, sig_lo;
3506 bool sign;
3507 int exp;
3509 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3510 sig_lo &= 0xffffffff;
3511 sig_hi &= 0xffffffff;
3512 image_hi &= 0xffffffff;
3514 sign = (image_hi >> 15) & 1;
3515 exp = image_hi & 0x7fff;
3517 memset (r, 0, sizeof (*r));
3519 if (exp == 0)
3521 if ((sig_hi || sig_lo) && fmt->has_denorm)
3523 r->cl = rvc_normal;
3524 r->sign = sign;
3526 /* When the IEEE format contains a hidden bit, we know that
3527 it's zero at this point, and so shift up the significand
3528 and decrease the exponent to match. In this case, Motorola
3529 defines the explicit integer bit to be valid, so we don't
3530 know whether the msb is set or not. */
3531 SET_REAL_EXP (r, fmt->emin);
3532 if (HOST_BITS_PER_LONG == 32)
3534 r->sig[SIGSZ-1] = sig_hi;
3535 r->sig[SIGSZ-2] = sig_lo;
3537 else
3538 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3540 normalize (r);
3542 else if (fmt->has_signed_zero)
3543 r->sign = sign;
3545 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3547 /* See above re "pseudo-infinities" and "pseudo-nans".
3548 Short summary is that the MSB will likely always be
3549 set, and that we don't care about it. */
3550 sig_hi &= 0x7fffffff;
3552 if (sig_hi || sig_lo)
3554 r->cl = rvc_nan;
3555 r->sign = sign;
3556 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3557 if (HOST_BITS_PER_LONG == 32)
3559 r->sig[SIGSZ-1] = sig_hi;
3560 r->sig[SIGSZ-2] = sig_lo;
3562 else
3563 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3565 else
3567 r->cl = rvc_inf;
3568 r->sign = sign;
3571 else
3573 r->cl = rvc_normal;
3574 r->sign = sign;
3575 SET_REAL_EXP (r, exp - 16383 + 1);
3576 if (HOST_BITS_PER_LONG == 32)
3578 r->sig[SIGSZ-1] = sig_hi;
3579 r->sig[SIGSZ-2] = sig_lo;
3581 else
3582 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3586 /* Convert from the internal format to the 12-byte Motorola format
3587 for an IEEE extended real. */
3588 static void
3589 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3590 const long *buf)
3592 long intermed[3];
3594 /* Motorola chips are assumed always to be big-endian. Also, the
3595 padding in a Motorola extended real goes between the exponent and
3596 the mantissa; remove it. */
3597 intermed[0] = buf[2];
3598 intermed[1] = buf[1];
3599 intermed[2] = (unsigned long)buf[0] >> 16;
3601 decode_ieee_extended (fmt, r, intermed);
3604 /* Convert from the internal format to the 12-byte Intel format for
3605 an IEEE extended real. */
3606 static void
3607 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3608 const long *buf)
3610 if (FLOAT_WORDS_BIG_ENDIAN)
3612 /* All the padding in an Intel-format extended real goes at the high
3613 end, which in this case is after the mantissa, not the exponent.
3614 Therefore we must shift everything up 16 bits. */
3615 long intermed[3];
3617 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3618 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3619 intermed[2] = ((unsigned long)buf[0] >> 16);
3621 decode_ieee_extended (fmt, r, intermed);
3623 else
3624 /* decode_ieee_extended produces what we want directly. */
3625 decode_ieee_extended (fmt, r, buf);
3628 /* Convert from the internal format to the 16-byte Intel format for
3629 an IEEE extended real. */
3630 static void
3631 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3632 const long *buf)
3634 /* All the padding in an Intel-format extended real goes at the high end. */
3635 decode_ieee_extended_intel_96 (fmt, r, buf);
3638 const struct real_format ieee_extended_motorola_format =
3640 encode_ieee_extended_motorola,
3641 decode_ieee_extended_motorola,
3645 -16382,
3646 16384,
3649 false,
3650 true,
3651 true,
3652 true,
3653 true,
3654 true,
3655 true,
3656 true
3659 const struct real_format ieee_extended_intel_96_format =
3661 encode_ieee_extended_intel_96,
3662 decode_ieee_extended_intel_96,
3666 -16381,
3667 16384,
3670 false,
3671 true,
3672 true,
3673 true,
3674 true,
3675 true,
3676 true,
3677 false
3680 const struct real_format ieee_extended_intel_128_format =
3682 encode_ieee_extended_intel_128,
3683 decode_ieee_extended_intel_128,
3687 -16381,
3688 16384,
3691 false,
3692 true,
3693 true,
3694 true,
3695 true,
3696 true,
3697 true,
3698 false
3701 /* The following caters to i386 systems that set the rounding precision
3702 to 53 bits instead of 64, e.g. FreeBSD. */
3703 const struct real_format ieee_extended_intel_96_round_53_format =
3705 encode_ieee_extended_intel_96,
3706 decode_ieee_extended_intel_96,
3710 -16381,
3711 16384,
3714 false,
3715 true,
3716 true,
3717 true,
3718 true,
3719 true,
3720 true,
3721 false
3724 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3725 numbers whose sum is equal to the extended precision value. The number
3726 with greater magnitude is first. This format has the same magnitude
3727 range as an IEEE double precision value, but effectively 106 bits of
3728 significand precision. Infinity and NaN are represented by their IEEE
3729 double precision value stored in the first number, the second number is
3730 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3732 static void encode_ibm_extended (const struct real_format *fmt,
3733 long *, const REAL_VALUE_TYPE *);
3734 static void decode_ibm_extended (const struct real_format *,
3735 REAL_VALUE_TYPE *, const long *);
3737 static void
3738 encode_ibm_extended (const struct real_format *fmt, long *buf,
3739 const REAL_VALUE_TYPE *r)
3741 REAL_VALUE_TYPE u, normr, v;
3742 const struct real_format *base_fmt;
3744 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3746 /* Renormalize R before doing any arithmetic on it. */
3747 normr = *r;
3748 if (normr.cl == rvc_normal)
3749 normalize (&normr);
3751 /* u = IEEE double precision portion of significand. */
3752 u = normr;
3753 round_for_format (base_fmt, &u);
3754 encode_ieee_double (base_fmt, &buf[0], &u);
3756 if (u.cl == rvc_normal)
3758 do_add (&v, &normr, &u, 1);
3759 /* Call round_for_format since we might need to denormalize. */
3760 round_for_format (base_fmt, &v);
3761 encode_ieee_double (base_fmt, &buf[2], &v);
3763 else
3765 /* Inf, NaN, 0 are all representable as doubles, so the
3766 least-significant part can be 0.0. */
3767 buf[2] = 0;
3768 buf[3] = 0;
3772 static void
3773 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3774 const long *buf)
3776 REAL_VALUE_TYPE u, v;
3777 const struct real_format *base_fmt;
3779 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3780 decode_ieee_double (base_fmt, &u, &buf[0]);
3782 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3784 decode_ieee_double (base_fmt, &v, &buf[2]);
3785 do_add (r, &u, &v, 0);
3787 else
3788 *r = u;
3791 const struct real_format ibm_extended_format =
3793 encode_ibm_extended,
3794 decode_ibm_extended,
3796 53 + 53,
3798 -1021 + 53,
3799 1024,
3800 127,
3802 false,
3803 true,
3804 true,
3805 true,
3806 true,
3807 true,
3808 true,
3809 false
3812 const struct real_format mips_extended_format =
3814 encode_ibm_extended,
3815 decode_ibm_extended,
3817 53 + 53,
3819 -1021 + 53,
3820 1024,
3821 127,
3823 false,
3824 true,
3825 true,
3826 true,
3827 true,
3828 true,
3829 false,
3830 true
3834 /* IEEE quad precision format. */
3836 static void encode_ieee_quad (const struct real_format *fmt,
3837 long *, const REAL_VALUE_TYPE *);
3838 static void decode_ieee_quad (const struct real_format *,
3839 REAL_VALUE_TYPE *, const long *);
3841 static void
3842 encode_ieee_quad (const struct real_format *fmt, long *buf,
3843 const REAL_VALUE_TYPE *r)
3845 unsigned long image3, image2, image1, image0, exp;
3846 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3847 REAL_VALUE_TYPE u;
3849 image3 = r->sign << 31;
3850 image2 = 0;
3851 image1 = 0;
3852 image0 = 0;
3854 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3856 switch (r->cl)
3858 case rvc_zero:
3859 break;
3861 case rvc_inf:
3862 if (fmt->has_inf)
3863 image3 |= 32767 << 16;
3864 else
3866 image3 |= 0x7fffffff;
3867 image2 = 0xffffffff;
3868 image1 = 0xffffffff;
3869 image0 = 0xffffffff;
3871 break;
3873 case rvc_nan:
3874 if (fmt->has_nans)
3876 image3 |= 32767 << 16;
3878 if (r->canonical)
3880 if (fmt->canonical_nan_lsbs_set)
3882 image3 |= 0x7fff;
3883 image2 = image1 = image0 = 0xffffffff;
3886 else if (HOST_BITS_PER_LONG == 32)
3888 image0 = u.sig[0];
3889 image1 = u.sig[1];
3890 image2 = u.sig[2];
3891 image3 |= u.sig[3] & 0xffff;
3893 else
3895 image0 = u.sig[0];
3896 image1 = image0 >> 31 >> 1;
3897 image2 = u.sig[1];
3898 image3 |= (image2 >> 31 >> 1) & 0xffff;
3899 image0 &= 0xffffffff;
3900 image2 &= 0xffffffff;
3902 if (r->signalling == fmt->qnan_msb_set)
3903 image3 &= ~0x8000;
3904 else
3905 image3 |= 0x8000;
3906 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3907 image3 |= 0x4000;
3909 else
3911 image3 |= 0x7fffffff;
3912 image2 = 0xffffffff;
3913 image1 = 0xffffffff;
3914 image0 = 0xffffffff;
3916 break;
3918 case rvc_normal:
3919 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3920 whereas the intermediate representation is 0.F x 2**exp.
3921 Which means we're off by one. */
3922 if (denormal)
3923 exp = 0;
3924 else
3925 exp = REAL_EXP (r) + 16383 - 1;
3926 image3 |= exp << 16;
3928 if (HOST_BITS_PER_LONG == 32)
3930 image0 = u.sig[0];
3931 image1 = u.sig[1];
3932 image2 = u.sig[2];
3933 image3 |= u.sig[3] & 0xffff;
3935 else
3937 image0 = u.sig[0];
3938 image1 = image0 >> 31 >> 1;
3939 image2 = u.sig[1];
3940 image3 |= (image2 >> 31 >> 1) & 0xffff;
3941 image0 &= 0xffffffff;
3942 image2 &= 0xffffffff;
3944 break;
3946 default:
3947 gcc_unreachable ();
3950 if (FLOAT_WORDS_BIG_ENDIAN)
3952 buf[0] = image3;
3953 buf[1] = image2;
3954 buf[2] = image1;
3955 buf[3] = image0;
3957 else
3959 buf[0] = image0;
3960 buf[1] = image1;
3961 buf[2] = image2;
3962 buf[3] = image3;
3966 static void
3967 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3968 const long *buf)
3970 unsigned long image3, image2, image1, image0;
3971 bool sign;
3972 int exp;
3974 if (FLOAT_WORDS_BIG_ENDIAN)
3976 image3 = buf[0];
3977 image2 = buf[1];
3978 image1 = buf[2];
3979 image0 = buf[3];
3981 else
3983 image0 = buf[0];
3984 image1 = buf[1];
3985 image2 = buf[2];
3986 image3 = buf[3];
3988 image0 &= 0xffffffff;
3989 image1 &= 0xffffffff;
3990 image2 &= 0xffffffff;
3992 sign = (image3 >> 31) & 1;
3993 exp = (image3 >> 16) & 0x7fff;
3994 image3 &= 0xffff;
3996 memset (r, 0, sizeof (*r));
3998 if (exp == 0)
4000 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
4002 r->cl = rvc_normal;
4003 r->sign = sign;
4005 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
4006 if (HOST_BITS_PER_LONG == 32)
4008 r->sig[0] = image0;
4009 r->sig[1] = image1;
4010 r->sig[2] = image2;
4011 r->sig[3] = image3;
4013 else
4015 r->sig[0] = (image1 << 31 << 1) | image0;
4016 r->sig[1] = (image3 << 31 << 1) | image2;
4019 normalize (r);
4021 else if (fmt->has_signed_zero)
4022 r->sign = sign;
4024 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
4026 if (image3 | image2 | image1 | image0)
4028 r->cl = rvc_nan;
4029 r->sign = sign;
4030 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
4032 if (HOST_BITS_PER_LONG == 32)
4034 r->sig[0] = image0;
4035 r->sig[1] = image1;
4036 r->sig[2] = image2;
4037 r->sig[3] = image3;
4039 else
4041 r->sig[0] = (image1 << 31 << 1) | image0;
4042 r->sig[1] = (image3 << 31 << 1) | image2;
4044 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4046 else
4048 r->cl = rvc_inf;
4049 r->sign = sign;
4052 else
4054 r->cl = rvc_normal;
4055 r->sign = sign;
4056 SET_REAL_EXP (r, exp - 16383 + 1);
4058 if (HOST_BITS_PER_LONG == 32)
4060 r->sig[0] = image0;
4061 r->sig[1] = image1;
4062 r->sig[2] = image2;
4063 r->sig[3] = image3;
4065 else
4067 r->sig[0] = (image1 << 31 << 1) | image0;
4068 r->sig[1] = (image3 << 31 << 1) | image2;
4070 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4071 r->sig[SIGSZ-1] |= SIG_MSB;
4075 const struct real_format ieee_quad_format =
4077 encode_ieee_quad,
4078 decode_ieee_quad,
4080 113,
4081 113,
4082 -16381,
4083 16384,
4084 127,
4085 127,
4086 false,
4087 true,
4088 true,
4089 true,
4090 true,
4091 true,
4092 true,
4093 false
4096 const struct real_format mips_quad_format =
4098 encode_ieee_quad,
4099 decode_ieee_quad,
4101 113,
4102 113,
4103 -16381,
4104 16384,
4105 127,
4106 127,
4107 false,
4108 true,
4109 true,
4110 true,
4111 true,
4112 true,
4113 false,
4114 true
4117 /* Descriptions of VAX floating point formats can be found beginning at
4119 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4121 The thing to remember is that they're almost IEEE, except for word
4122 order, exponent bias, and the lack of infinities, nans, and denormals.
4124 We don't implement the H_floating format here, simply because neither
4125 the VAX or Alpha ports use it. */
4127 static void encode_vax_f (const struct real_format *fmt,
4128 long *, const REAL_VALUE_TYPE *);
4129 static void decode_vax_f (const struct real_format *,
4130 REAL_VALUE_TYPE *, const long *);
4131 static void encode_vax_d (const struct real_format *fmt,
4132 long *, const REAL_VALUE_TYPE *);
4133 static void decode_vax_d (const struct real_format *,
4134 REAL_VALUE_TYPE *, const long *);
4135 static void encode_vax_g (const struct real_format *fmt,
4136 long *, const REAL_VALUE_TYPE *);
4137 static void decode_vax_g (const struct real_format *,
4138 REAL_VALUE_TYPE *, const long *);
4140 static void
4141 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4142 const REAL_VALUE_TYPE *r)
4144 unsigned long sign, exp, sig, image;
4146 sign = r->sign << 15;
4148 switch (r->cl)
4150 case rvc_zero:
4151 image = 0;
4152 break;
4154 case rvc_inf:
4155 case rvc_nan:
4156 image = 0xffff7fff | sign;
4157 break;
4159 case rvc_normal:
4160 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4161 exp = REAL_EXP (r) + 128;
4163 image = (sig << 16) & 0xffff0000;
4164 image |= sign;
4165 image |= exp << 7;
4166 image |= sig >> 16;
4167 break;
4169 default:
4170 gcc_unreachable ();
4173 buf[0] = image;
4176 static void
4177 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
4178 REAL_VALUE_TYPE *r, const long *buf)
4180 unsigned long image = buf[0] & 0xffffffff;
4181 int exp = (image >> 7) & 0xff;
4183 memset (r, 0, sizeof (*r));
4185 if (exp != 0)
4187 r->cl = rvc_normal;
4188 r->sign = (image >> 15) & 1;
4189 SET_REAL_EXP (r, exp - 128);
4191 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
4192 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4196 static void
4197 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4198 const REAL_VALUE_TYPE *r)
4200 unsigned long image0, image1, sign = r->sign << 15;
4202 switch (r->cl)
4204 case rvc_zero:
4205 image0 = image1 = 0;
4206 break;
4208 case rvc_inf:
4209 case rvc_nan:
4210 image0 = 0xffff7fff | sign;
4211 image1 = 0xffffffff;
4212 break;
4214 case rvc_normal:
4215 /* Extract the significand into straight hi:lo. */
4216 if (HOST_BITS_PER_LONG == 64)
4218 image0 = r->sig[SIGSZ-1];
4219 image1 = (image0 >> (64 - 56)) & 0xffffffff;
4220 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
4222 else
4224 image0 = r->sig[SIGSZ-1];
4225 image1 = r->sig[SIGSZ-2];
4226 image1 = (image0 << 24) | (image1 >> 8);
4227 image0 = (image0 >> 8) & 0xffffff;
4230 /* Rearrange the half-words of the significand to match the
4231 external format. */
4232 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
4233 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4235 /* Add the sign and exponent. */
4236 image0 |= sign;
4237 image0 |= (REAL_EXP (r) + 128) << 7;
4238 break;
4240 default:
4241 gcc_unreachable ();
4244 if (FLOAT_WORDS_BIG_ENDIAN)
4245 buf[0] = image1, buf[1] = image0;
4246 else
4247 buf[0] = image0, buf[1] = image1;
4250 static void
4251 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
4252 REAL_VALUE_TYPE *r, const long *buf)
4254 unsigned long image0, image1;
4255 int exp;
4257 if (FLOAT_WORDS_BIG_ENDIAN)
4258 image1 = buf[0], image0 = buf[1];
4259 else
4260 image0 = buf[0], image1 = buf[1];
4261 image0 &= 0xffffffff;
4262 image1 &= 0xffffffff;
4264 exp = (image0 >> 7) & 0xff;
4266 memset (r, 0, sizeof (*r));
4268 if (exp != 0)
4270 r->cl = rvc_normal;
4271 r->sign = (image0 >> 15) & 1;
4272 SET_REAL_EXP (r, exp - 128);
4274 /* Rearrange the half-words of the external format into
4275 proper ascending order. */
4276 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4277 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4279 if (HOST_BITS_PER_LONG == 64)
4281 image0 = (image0 << 31 << 1) | image1;
4282 image0 <<= 64 - 56;
4283 image0 |= SIG_MSB;
4284 r->sig[SIGSZ-1] = image0;
4286 else
4288 r->sig[SIGSZ-1] = image0;
4289 r->sig[SIGSZ-2] = image1;
4290 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4291 r->sig[SIGSZ-1] |= SIG_MSB;
4296 static void
4297 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4298 const REAL_VALUE_TYPE *r)
4300 unsigned long image0, image1, sign = r->sign << 15;
4302 switch (r->cl)
4304 case rvc_zero:
4305 image0 = image1 = 0;
4306 break;
4308 case rvc_inf:
4309 case rvc_nan:
4310 image0 = 0xffff7fff | sign;
4311 image1 = 0xffffffff;
4312 break;
4314 case rvc_normal:
4315 /* Extract the significand into straight hi:lo. */
4316 if (HOST_BITS_PER_LONG == 64)
4318 image0 = r->sig[SIGSZ-1];
4319 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4320 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4322 else
4324 image0 = r->sig[SIGSZ-1];
4325 image1 = r->sig[SIGSZ-2];
4326 image1 = (image0 << 21) | (image1 >> 11);
4327 image0 = (image0 >> 11) & 0xfffff;
4330 /* Rearrange the half-words of the significand to match the
4331 external format. */
4332 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4333 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4335 /* Add the sign and exponent. */
4336 image0 |= sign;
4337 image0 |= (REAL_EXP (r) + 1024) << 4;
4338 break;
4340 default:
4341 gcc_unreachable ();
4344 if (FLOAT_WORDS_BIG_ENDIAN)
4345 buf[0] = image1, buf[1] = image0;
4346 else
4347 buf[0] = image0, buf[1] = image1;
4350 static void
4351 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4352 REAL_VALUE_TYPE *r, const long *buf)
4354 unsigned long image0, image1;
4355 int exp;
4357 if (FLOAT_WORDS_BIG_ENDIAN)
4358 image1 = buf[0], image0 = buf[1];
4359 else
4360 image0 = buf[0], image1 = buf[1];
4361 image0 &= 0xffffffff;
4362 image1 &= 0xffffffff;
4364 exp = (image0 >> 4) & 0x7ff;
4366 memset (r, 0, sizeof (*r));
4368 if (exp != 0)
4370 r->cl = rvc_normal;
4371 r->sign = (image0 >> 15) & 1;
4372 SET_REAL_EXP (r, exp - 1024);
4374 /* Rearrange the half-words of the external format into
4375 proper ascending order. */
4376 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4377 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4379 if (HOST_BITS_PER_LONG == 64)
4381 image0 = (image0 << 31 << 1) | image1;
4382 image0 <<= 64 - 53;
4383 image0 |= SIG_MSB;
4384 r->sig[SIGSZ-1] = image0;
4386 else
4388 r->sig[SIGSZ-1] = image0;
4389 r->sig[SIGSZ-2] = image1;
4390 lshift_significand (r, r, 64 - 53);
4391 r->sig[SIGSZ-1] |= SIG_MSB;
4396 const struct real_format vax_f_format =
4398 encode_vax_f,
4399 decode_vax_f,
4403 -127,
4404 127,
4407 false,
4408 false,
4409 false,
4410 false,
4411 false,
4412 false,
4413 false,
4414 false
4417 const struct real_format vax_d_format =
4419 encode_vax_d,
4420 decode_vax_d,
4424 -127,
4425 127,
4428 false,
4429 false,
4430 false,
4431 false,
4432 false,
4433 false,
4434 false,
4435 false
4438 const struct real_format vax_g_format =
4440 encode_vax_g,
4441 decode_vax_g,
4445 -1023,
4446 1023,
4449 false,
4450 false,
4451 false,
4452 false,
4453 false,
4454 false,
4455 false,
4456 false
4459 /* Encode real R into a single precision DFP value in BUF. */
4460 static void
4461 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4462 long *buf ATTRIBUTE_UNUSED,
4463 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4465 encode_decimal32 (fmt, buf, r);
4468 /* Decode a single precision DFP value in BUF into a real R. */
4469 static void
4470 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4471 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4472 const long *buf ATTRIBUTE_UNUSED)
4474 decode_decimal32 (fmt, r, buf);
4477 /* Encode real R into a double precision DFP value in BUF. */
4478 static void
4479 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4480 long *buf ATTRIBUTE_UNUSED,
4481 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4483 encode_decimal64 (fmt, buf, r);
4486 /* Decode a double precision DFP value in BUF into a real R. */
4487 static void
4488 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4489 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4490 const long *buf ATTRIBUTE_UNUSED)
4492 decode_decimal64 (fmt, r, buf);
4495 /* Encode real R into a quad precision DFP value in BUF. */
4496 static void
4497 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4498 long *buf ATTRIBUTE_UNUSED,
4499 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4501 encode_decimal128 (fmt, buf, r);
4504 /* Decode a quad precision DFP value in BUF into a real R. */
4505 static void
4506 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4507 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4508 const long *buf ATTRIBUTE_UNUSED)
4510 decode_decimal128 (fmt, r, buf);
4513 /* Single precision decimal floating point (IEEE 754). */
4514 const struct real_format decimal_single_format =
4516 encode_decimal_single,
4517 decode_decimal_single,
4521 -94,
4525 false,
4526 true,
4527 true,
4528 true,
4529 true,
4530 true,
4531 true,
4532 false
4535 /* Double precision decimal floating point (IEEE 754). */
4536 const struct real_format decimal_double_format =
4538 encode_decimal_double,
4539 decode_decimal_double,
4543 -382,
4544 385,
4547 false,
4548 true,
4549 true,
4550 true,
4551 true,
4552 true,
4553 true,
4554 false
4557 /* Quad precision decimal floating point (IEEE 754). */
4558 const struct real_format decimal_quad_format =
4560 encode_decimal_quad,
4561 decode_decimal_quad,
4565 -6142,
4566 6145,
4567 127,
4568 127,
4569 false,
4570 true,
4571 true,
4572 true,
4573 true,
4574 true,
4575 true,
4576 false
4579 /* Encode half-precision floats. This routine is used both for the IEEE
4580 ARM alternative encodings. */
4581 static void
4582 encode_ieee_half (const struct real_format *fmt, long *buf,
4583 const REAL_VALUE_TYPE *r)
4585 unsigned long image, sig, exp;
4586 unsigned long sign = r->sign;
4587 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
4589 image = sign << 15;
4590 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 11)) & 0x3ff;
4592 switch (r->cl)
4594 case rvc_zero:
4595 break;
4597 case rvc_inf:
4598 if (fmt->has_inf)
4599 image |= 31 << 10;
4600 else
4601 image |= 0x7fff;
4602 break;
4604 case rvc_nan:
4605 if (fmt->has_nans)
4607 if (r->canonical)
4608 sig = (fmt->canonical_nan_lsbs_set ? (1 << 9) - 1 : 0);
4609 if (r->signalling == fmt->qnan_msb_set)
4610 sig &= ~(1 << 9);
4611 else
4612 sig |= 1 << 9;
4613 if (sig == 0)
4614 sig = 1 << 8;
4616 image |= 31 << 10;
4617 image |= sig;
4619 else
4620 image |= 0x3ff;
4621 break;
4623 case rvc_normal:
4624 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4625 whereas the intermediate representation is 0.F x 2**exp.
4626 Which means we're off by one. */
4627 if (denormal)
4628 exp = 0;
4629 else
4630 exp = REAL_EXP (r) + 15 - 1;
4631 image |= exp << 10;
4632 image |= sig;
4633 break;
4635 default:
4636 gcc_unreachable ();
4639 buf[0] = image;
4642 /* Decode half-precision floats. This routine is used both for the IEEE
4643 ARM alternative encodings. */
4644 static void
4645 decode_ieee_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4646 const long *buf)
4648 unsigned long image = buf[0] & 0xffff;
4649 bool sign = (image >> 15) & 1;
4650 int exp = (image >> 10) & 0x1f;
4652 memset (r, 0, sizeof (*r));
4653 image <<= HOST_BITS_PER_LONG - 11;
4654 image &= ~SIG_MSB;
4656 if (exp == 0)
4658 if (image && fmt->has_denorm)
4660 r->cl = rvc_normal;
4661 r->sign = sign;
4662 SET_REAL_EXP (r, -14);
4663 r->sig[SIGSZ-1] = image << 1;
4664 normalize (r);
4666 else if (fmt->has_signed_zero)
4667 r->sign = sign;
4669 else if (exp == 31 && (fmt->has_nans || fmt->has_inf))
4671 if (image)
4673 r->cl = rvc_nan;
4674 r->sign = sign;
4675 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4676 ^ fmt->qnan_msb_set);
4677 r->sig[SIGSZ-1] = image;
4679 else
4681 r->cl = rvc_inf;
4682 r->sign = sign;
4685 else
4687 r->cl = rvc_normal;
4688 r->sign = sign;
4689 SET_REAL_EXP (r, exp - 15 + 1);
4690 r->sig[SIGSZ-1] = image | SIG_MSB;
4694 /* Half-precision format, as specified in IEEE 754R. */
4695 const struct real_format ieee_half_format =
4697 encode_ieee_half,
4698 decode_ieee_half,
4702 -13,
4706 false,
4707 true,
4708 true,
4709 true,
4710 true,
4711 true,
4712 true,
4713 false
4716 /* ARM's alternative half-precision format, similar to IEEE but with
4717 no reserved exponent value for NaNs and infinities; rather, it just
4718 extends the range of exponents by one. */
4719 const struct real_format arm_half_format =
4721 encode_ieee_half,
4722 decode_ieee_half,
4726 -13,
4730 false,
4731 true,
4732 false,
4733 false,
4734 true,
4735 true,
4736 false,
4737 false
4740 /* A synthetic "format" for internal arithmetic. It's the size of the
4741 internal significand minus the two bits needed for proper rounding.
4742 The encode and decode routines exist only to satisfy our paranoia
4743 harness. */
4745 static void encode_internal (const struct real_format *fmt,
4746 long *, const REAL_VALUE_TYPE *);
4747 static void decode_internal (const struct real_format *,
4748 REAL_VALUE_TYPE *, const long *);
4750 static void
4751 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4752 const REAL_VALUE_TYPE *r)
4754 memcpy (buf, r, sizeof (*r));
4757 static void
4758 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
4759 REAL_VALUE_TYPE *r, const long *buf)
4761 memcpy (r, buf, sizeof (*r));
4764 const struct real_format real_internal_format =
4766 encode_internal,
4767 decode_internal,
4769 SIGNIFICAND_BITS - 2,
4770 SIGNIFICAND_BITS - 2,
4771 -MAX_EXP,
4772 MAX_EXP,
4775 false,
4776 false,
4777 true,
4778 true,
4779 false,
4780 true,
4781 true,
4782 false
4785 /* Calculate the square root of X in mode MODE, and store the result
4786 in R. Return TRUE if the operation does not raise an exception.
4787 For details see "High Precision Division and Square Root",
4788 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4789 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4791 bool
4792 real_sqrt (REAL_VALUE_TYPE *r, enum machine_mode mode,
4793 const REAL_VALUE_TYPE *x)
4795 static REAL_VALUE_TYPE halfthree;
4796 static bool init = false;
4797 REAL_VALUE_TYPE h, t, i;
4798 int iter, exp;
4800 /* sqrt(-0.0) is -0.0. */
4801 if (real_isnegzero (x))
4803 *r = *x;
4804 return false;
4807 /* Negative arguments return NaN. */
4808 if (real_isneg (x))
4810 get_canonical_qnan (r, 0);
4811 return false;
4814 /* Infinity and NaN return themselves. */
4815 if (!real_isfinite (x))
4817 *r = *x;
4818 return false;
4821 if (!init)
4823 do_add (&halfthree, &dconst1, &dconsthalf, 0);
4824 init = true;
4827 /* Initial guess for reciprocal sqrt, i. */
4828 exp = real_exponent (x);
4829 real_ldexp (&i, &dconst1, -exp/2);
4831 /* Newton's iteration for reciprocal sqrt, i. */
4832 for (iter = 0; iter < 16; iter++)
4834 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4835 do_multiply (&t, x, &i);
4836 do_multiply (&h, &t, &i);
4837 do_multiply (&t, &h, &dconsthalf);
4838 do_add (&h, &halfthree, &t, 1);
4839 do_multiply (&t, &i, &h);
4841 /* Check for early convergence. */
4842 if (iter >= 6 && real_identical (&i, &t))
4843 break;
4845 /* ??? Unroll loop to avoid copying. */
4846 i = t;
4849 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4850 do_multiply (&t, x, &i);
4851 do_multiply (&h, &t, &i);
4852 do_add (&i, &dconst1, &h, 1);
4853 do_multiply (&h, &t, &i);
4854 do_multiply (&i, &dconsthalf, &h);
4855 do_add (&h, &t, &i, 0);
4857 /* ??? We need a Tuckerman test to get the last bit. */
4859 real_convert (r, mode, &h);
4860 return true;
4863 /* Calculate X raised to the integer exponent N in mode MODE and store
4864 the result in R. Return true if the result may be inexact due to
4865 loss of precision. The algorithm is the classic "left-to-right binary
4866 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4867 Algorithms", "The Art of Computer Programming", Volume 2. */
4869 bool
4870 real_powi (REAL_VALUE_TYPE *r, enum machine_mode mode,
4871 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4873 unsigned HOST_WIDE_INT bit;
4874 REAL_VALUE_TYPE t;
4875 bool inexact = false;
4876 bool init = false;
4877 bool neg;
4878 int i;
4880 if (n == 0)
4882 *r = dconst1;
4883 return false;
4885 else if (n < 0)
4887 /* Don't worry about overflow, from now on n is unsigned. */
4888 neg = true;
4889 n = -n;
4891 else
4892 neg = false;
4894 t = *x;
4895 bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
4896 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4898 if (init)
4900 inexact |= do_multiply (&t, &t, &t);
4901 if (n & bit)
4902 inexact |= do_multiply (&t, &t, x);
4904 else if (n & bit)
4905 init = true;
4906 bit >>= 1;
4909 if (neg)
4910 inexact |= do_divide (&t, &dconst1, &t);
4912 real_convert (r, mode, &t);
4913 return inexact;
4916 /* Round X to the nearest integer not larger in absolute value, i.e.
4917 towards zero, placing the result in R in mode MODE. */
4919 void
4920 real_trunc (REAL_VALUE_TYPE *r, enum machine_mode mode,
4921 const REAL_VALUE_TYPE *x)
4923 do_fix_trunc (r, x);
4924 if (mode != VOIDmode)
4925 real_convert (r, mode, r);
4928 /* Round X to the largest integer not greater in value, i.e. round
4929 down, placing the result in R in mode MODE. */
4931 void
4932 real_floor (REAL_VALUE_TYPE *r, enum machine_mode mode,
4933 const REAL_VALUE_TYPE *x)
4935 REAL_VALUE_TYPE t;
4937 do_fix_trunc (&t, x);
4938 if (! real_identical (&t, x) && x->sign)
4939 do_add (&t, &t, &dconstm1, 0);
4940 if (mode != VOIDmode)
4941 real_convert (r, mode, &t);
4942 else
4943 *r = t;
4946 /* Round X to the smallest integer not less then argument, i.e. round
4947 up, placing the result in R in mode MODE. */
4949 void
4950 real_ceil (REAL_VALUE_TYPE *r, enum machine_mode mode,
4951 const REAL_VALUE_TYPE *x)
4953 REAL_VALUE_TYPE t;
4955 do_fix_trunc (&t, x);
4956 if (! real_identical (&t, x) && ! x->sign)
4957 do_add (&t, &t, &dconst1, 0);
4958 if (mode != VOIDmode)
4959 real_convert (r, mode, &t);
4960 else
4961 *r = t;
4964 /* Round X to the nearest integer, but round halfway cases away from
4965 zero. */
4967 void
4968 real_round (REAL_VALUE_TYPE *r, enum machine_mode mode,
4969 const REAL_VALUE_TYPE *x)
4971 do_add (r, x, &dconsthalf, x->sign);
4972 do_fix_trunc (r, r);
4973 if (mode != VOIDmode)
4974 real_convert (r, mode, r);
4977 /* Set the sign of R to the sign of X. */
4979 void
4980 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4982 r->sign = x->sign;
4985 /* Convert from REAL_VALUE_TYPE to MPFR. The caller is responsible
4986 for initializing and clearing the MPFR parameter. */
4988 void
4989 mpfr_from_real (mpfr_ptr m, const REAL_VALUE_TYPE *r, mp_rnd_t rndmode)
4991 /* We use a string as an intermediate type. */
4992 char buf[128];
4993 int ret;
4995 /* Take care of Infinity and NaN. */
4996 if (r->cl == rvc_inf)
4998 mpfr_set_inf (m, r->sign == 1 ? -1 : 1);
4999 return;
5002 if (r->cl == rvc_nan)
5004 mpfr_set_nan (m);
5005 return;
5008 real_to_hexadecimal (buf, r, sizeof (buf), 0, 1);
5009 /* mpfr_set_str() parses hexadecimal floats from strings in the same
5010 format that GCC will output them. Nothing extra is needed. */
5011 ret = mpfr_set_str (m, buf, 16, rndmode);
5012 gcc_assert (ret == 0);
5015 /* Convert from MPFR to REAL_VALUE_TYPE, for a given type TYPE and rounding
5016 mode RNDMODE. TYPE is only relevant if M is a NaN. */
5018 void
5019 real_from_mpfr (REAL_VALUE_TYPE *r, mpfr_srcptr m, tree type, mp_rnd_t rndmode)
5021 /* We use a string as an intermediate type. */
5022 char buf[128], *rstr;
5023 mp_exp_t exp;
5025 /* Take care of Infinity and NaN. */
5026 if (mpfr_inf_p (m))
5028 real_inf (r);
5029 if (mpfr_sgn (m) < 0)
5030 *r = REAL_VALUE_NEGATE (*r);
5031 return;
5034 if (mpfr_nan_p (m))
5036 real_nan (r, "", 1, TYPE_MODE (type));
5037 return;
5040 rstr = mpfr_get_str (NULL, &exp, 16, 0, m, rndmode);
5042 /* The additional 12 chars add space for the sprintf below. This
5043 leaves 6 digits for the exponent which is supposedly enough. */
5044 gcc_assert (rstr != NULL && strlen (rstr) < sizeof (buf) - 12);
5046 /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp,
5047 mpfr_get_str returns the exponent for mantissa * 16**exp, adjust
5048 for that. */
5049 exp *= 4;
5051 if (rstr[0] == '-')
5052 sprintf (buf, "-0x.%sp%d", &rstr[1], (int) exp);
5053 else
5054 sprintf (buf, "0x.%sp%d", rstr, (int) exp);
5056 mpfr_free_str (rstr);
5058 real_from_string (r, buf);
5061 /* Check whether the real constant value given is an integer. */
5063 bool
5064 real_isinteger (const REAL_VALUE_TYPE *c, enum machine_mode mode)
5066 REAL_VALUE_TYPE cint;
5068 real_trunc (&cint, mode, c);
5069 return real_identical (c, &cint);
5072 /* Write into BUF the maximum representable finite floating-point
5073 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5074 float string. LEN is the size of BUF, and the buffer must be large
5075 enough to contain the resulting string. */
5077 void
5078 get_max_float (const struct real_format *fmt, char *buf, size_t len)
5080 int i, n;
5081 char *p;
5083 strcpy (buf, "0x0.");
5084 n = fmt->p;
5085 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
5086 *p++ = 'f';
5087 if (i < n)
5088 *p++ = "08ce"[n - i];
5089 sprintf (p, "p%d", fmt->emax);
5090 if (fmt->pnan < fmt->p)
5092 /* This is an IBM extended double format made up of two IEEE
5093 doubles. The value of the long double is the sum of the
5094 values of the two parts. The most significant part is
5095 required to be the value of the long double rounded to the
5096 nearest double. Rounding means we need a slightly smaller
5097 value for LDBL_MAX. */
5098 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
5101 gcc_assert (strlen (buf) < len);