avr.md (*sbrx_branch, [...]): Add mode to zero_extract.
[official-gcc.git] / gcc / real.c
blobac3b7dc02ecde8adc526ac257eb6cc5f20054008
1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2002, 2003, 2004, 2005, 2007, 2008 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 27.
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 *);
114 static const REAL_VALUE_TYPE * ten_to_ptwo (int);
115 static const REAL_VALUE_TYPE * ten_to_mptwo (int);
116 static const REAL_VALUE_TYPE * real_digit (int);
117 static void times_pten (REAL_VALUE_TYPE *, int);
119 static void round_for_format (const struct real_format *, REAL_VALUE_TYPE *);
121 /* Initialize R with a positive zero. */
123 static inline void
124 get_zero (REAL_VALUE_TYPE *r, int sign)
126 memset (r, 0, sizeof (*r));
127 r->sign = sign;
130 /* Initialize R with the canonical quiet NaN. */
132 static inline void
133 get_canonical_qnan (REAL_VALUE_TYPE *r, int sign)
135 memset (r, 0, sizeof (*r));
136 r->cl = rvc_nan;
137 r->sign = sign;
138 r->canonical = 1;
141 static inline void
142 get_canonical_snan (REAL_VALUE_TYPE *r, int sign)
144 memset (r, 0, sizeof (*r));
145 r->cl = rvc_nan;
146 r->sign = sign;
147 r->signalling = 1;
148 r->canonical = 1;
151 static inline void
152 get_inf (REAL_VALUE_TYPE *r, int sign)
154 memset (r, 0, sizeof (*r));
155 r->cl = rvc_inf;
156 r->sign = sign;
160 /* Right-shift the significand of A by N bits; put the result in the
161 significand of R. If any one bits are shifted out, return true. */
163 static bool
164 sticky_rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
165 unsigned int n)
167 unsigned long sticky = 0;
168 unsigned int i, ofs = 0;
170 if (n >= HOST_BITS_PER_LONG)
172 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
173 sticky |= a->sig[i];
174 n &= HOST_BITS_PER_LONG - 1;
177 if (n != 0)
179 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
180 for (i = 0; i < SIGSZ; ++i)
182 r->sig[i]
183 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
184 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
185 << (HOST_BITS_PER_LONG - n)));
188 else
190 for (i = 0; ofs + i < SIGSZ; ++i)
191 r->sig[i] = a->sig[ofs + i];
192 for (; i < SIGSZ; ++i)
193 r->sig[i] = 0;
196 return sticky != 0;
199 /* Right-shift the significand of A by N bits; put the result in the
200 significand of R. */
202 static void
203 rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
204 unsigned int n)
206 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
208 n &= HOST_BITS_PER_LONG - 1;
209 if (n != 0)
211 for (i = 0; i < SIGSZ; ++i)
213 r->sig[i]
214 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
215 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
216 << (HOST_BITS_PER_LONG - n)));
219 else
221 for (i = 0; ofs + i < SIGSZ; ++i)
222 r->sig[i] = a->sig[ofs + i];
223 for (; i < SIGSZ; ++i)
224 r->sig[i] = 0;
228 /* Left-shift the significand of A by N bits; put the result in the
229 significand of R. */
231 static void
232 lshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
233 unsigned int n)
235 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
237 n &= HOST_BITS_PER_LONG - 1;
238 if (n == 0)
240 for (i = 0; ofs + i < SIGSZ; ++i)
241 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
242 for (; i < SIGSZ; ++i)
243 r->sig[SIGSZ-1-i] = 0;
245 else
246 for (i = 0; i < SIGSZ; ++i)
248 r->sig[SIGSZ-1-i]
249 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
250 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
251 >> (HOST_BITS_PER_LONG - n)));
255 /* Likewise, but N is specialized to 1. */
257 static inline void
258 lshift_significand_1 (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
260 unsigned int i;
262 for (i = SIGSZ - 1; i > 0; --i)
263 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
264 r->sig[0] = a->sig[0] << 1;
267 /* Add the significands of A and B, placing the result in R. Return
268 true if there was carry out of the most significant word. */
270 static inline bool
271 add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
272 const REAL_VALUE_TYPE *b)
274 bool carry = false;
275 int i;
277 for (i = 0; i < SIGSZ; ++i)
279 unsigned long ai = a->sig[i];
280 unsigned long ri = ai + b->sig[i];
282 if (carry)
284 carry = ri < ai;
285 carry |= ++ri == 0;
287 else
288 carry = ri < ai;
290 r->sig[i] = ri;
293 return carry;
296 /* Subtract the significands of A and B, placing the result in R. CARRY is
297 true if there's a borrow incoming to the least significant word.
298 Return true if there was borrow out of the most significant word. */
300 static inline bool
301 sub_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
302 const REAL_VALUE_TYPE *b, int carry)
304 int i;
306 for (i = 0; i < SIGSZ; ++i)
308 unsigned long ai = a->sig[i];
309 unsigned long ri = ai - b->sig[i];
311 if (carry)
313 carry = ri > ai;
314 carry |= ~--ri == 0;
316 else
317 carry = ri > ai;
319 r->sig[i] = ri;
322 return carry;
325 /* Negate the significand A, placing the result in R. */
327 static inline void
328 neg_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
330 bool carry = true;
331 int i;
333 for (i = 0; i < SIGSZ; ++i)
335 unsigned long ri, ai = a->sig[i];
337 if (carry)
339 if (ai)
341 ri = -ai;
342 carry = false;
344 else
345 ri = ai;
347 else
348 ri = ~ai;
350 r->sig[i] = ri;
354 /* Compare significands. Return tri-state vs zero. */
356 static inline int
357 cmp_significands (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
359 int i;
361 for (i = SIGSZ - 1; i >= 0; --i)
363 unsigned long ai = a->sig[i];
364 unsigned long bi = b->sig[i];
366 if (ai > bi)
367 return 1;
368 if (ai < bi)
369 return -1;
372 return 0;
375 /* Return true if A is nonzero. */
377 static inline int
378 cmp_significand_0 (const REAL_VALUE_TYPE *a)
380 int i;
382 for (i = SIGSZ - 1; i >= 0; --i)
383 if (a->sig[i])
384 return 1;
386 return 0;
389 /* Set bit N of the significand of R. */
391 static inline void
392 set_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
394 r->sig[n / HOST_BITS_PER_LONG]
395 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
398 /* Clear bit N of the significand of R. */
400 static inline void
401 clear_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
403 r->sig[n / HOST_BITS_PER_LONG]
404 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
407 /* Test bit N of the significand of R. */
409 static inline bool
410 test_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
412 /* ??? Compiler bug here if we return this expression directly.
413 The conversion to bool strips the "&1" and we wind up testing
414 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
415 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
416 return t;
419 /* Clear bits 0..N-1 of the significand of R. */
421 static void
422 clear_significand_below (REAL_VALUE_TYPE *r, unsigned int n)
424 int i, w = n / HOST_BITS_PER_LONG;
426 for (i = 0; i < w; ++i)
427 r->sig[i] = 0;
429 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
432 /* Divide the significands of A and B, placing the result in R. Return
433 true if the division was inexact. */
435 static inline bool
436 div_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
437 const REAL_VALUE_TYPE *b)
439 REAL_VALUE_TYPE u;
440 int i, bit = SIGNIFICAND_BITS - 1;
441 unsigned long msb, inexact;
443 u = *a;
444 memset (r->sig, 0, sizeof (r->sig));
446 msb = 0;
447 goto start;
450 msb = u.sig[SIGSZ-1] & SIG_MSB;
451 lshift_significand_1 (&u, &u);
452 start:
453 if (msb || cmp_significands (&u, b) >= 0)
455 sub_significands (&u, &u, b, 0);
456 set_significand_bit (r, bit);
459 while (--bit >= 0);
461 for (i = 0, inexact = 0; i < SIGSZ; i++)
462 inexact |= u.sig[i];
464 return inexact != 0;
467 /* Adjust the exponent and significand of R such that the most
468 significant bit is set. We underflow to zero and overflow to
469 infinity here, without denormals. (The intermediate representation
470 exponent is large enough to handle target denormals normalized.) */
472 static void
473 normalize (REAL_VALUE_TYPE *r)
475 int shift = 0, exp;
476 int i, j;
478 if (r->decimal)
479 return;
481 /* Find the first word that is nonzero. */
482 for (i = SIGSZ - 1; i >= 0; i--)
483 if (r->sig[i] == 0)
484 shift += HOST_BITS_PER_LONG;
485 else
486 break;
488 /* Zero significand flushes to zero. */
489 if (i < 0)
491 r->cl = rvc_zero;
492 SET_REAL_EXP (r, 0);
493 return;
496 /* Find the first bit that is nonzero. */
497 for (j = 0; ; j++)
498 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
499 break;
500 shift += j;
502 if (shift > 0)
504 exp = REAL_EXP (r) - shift;
505 if (exp > MAX_EXP)
506 get_inf (r, r->sign);
507 else if (exp < -MAX_EXP)
508 get_zero (r, r->sign);
509 else
511 SET_REAL_EXP (r, exp);
512 lshift_significand (r, r, shift);
517 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
518 result may be inexact due to a loss of precision. */
520 static bool
521 do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
522 const REAL_VALUE_TYPE *b, int subtract_p)
524 int dexp, sign, exp;
525 REAL_VALUE_TYPE t;
526 bool inexact = false;
528 /* Determine if we need to add or subtract. */
529 sign = a->sign;
530 subtract_p = (sign ^ b->sign) ^ subtract_p;
532 switch (CLASS2 (a->cl, b->cl))
534 case CLASS2 (rvc_zero, rvc_zero):
535 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
536 get_zero (r, sign & !subtract_p);
537 return false;
539 case CLASS2 (rvc_zero, rvc_normal):
540 case CLASS2 (rvc_zero, rvc_inf):
541 case CLASS2 (rvc_zero, rvc_nan):
542 /* 0 + ANY = ANY. */
543 case CLASS2 (rvc_normal, rvc_nan):
544 case CLASS2 (rvc_inf, rvc_nan):
545 case CLASS2 (rvc_nan, rvc_nan):
546 /* ANY + NaN = NaN. */
547 case CLASS2 (rvc_normal, rvc_inf):
548 /* R + Inf = Inf. */
549 *r = *b;
550 r->sign = sign ^ subtract_p;
551 return false;
553 case CLASS2 (rvc_normal, rvc_zero):
554 case CLASS2 (rvc_inf, rvc_zero):
555 case CLASS2 (rvc_nan, rvc_zero):
556 /* ANY + 0 = ANY. */
557 case CLASS2 (rvc_nan, rvc_normal):
558 case CLASS2 (rvc_nan, rvc_inf):
559 /* NaN + ANY = NaN. */
560 case CLASS2 (rvc_inf, rvc_normal):
561 /* Inf + R = Inf. */
562 *r = *a;
563 return false;
565 case CLASS2 (rvc_inf, rvc_inf):
566 if (subtract_p)
567 /* Inf - Inf = NaN. */
568 get_canonical_qnan (r, 0);
569 else
570 /* Inf + Inf = Inf. */
571 *r = *a;
572 return false;
574 case CLASS2 (rvc_normal, rvc_normal):
575 break;
577 default:
578 gcc_unreachable ();
581 /* Swap the arguments such that A has the larger exponent. */
582 dexp = REAL_EXP (a) - REAL_EXP (b);
583 if (dexp < 0)
585 const REAL_VALUE_TYPE *t;
586 t = a, a = b, b = t;
587 dexp = -dexp;
588 sign ^= subtract_p;
590 exp = REAL_EXP (a);
592 /* If the exponents are not identical, we need to shift the
593 significand of B down. */
594 if (dexp > 0)
596 /* If the exponents are too far apart, the significands
597 do not overlap, which makes the subtraction a noop. */
598 if (dexp >= SIGNIFICAND_BITS)
600 *r = *a;
601 r->sign = sign;
602 return true;
605 inexact |= sticky_rshift_significand (&t, b, dexp);
606 b = &t;
609 if (subtract_p)
611 if (sub_significands (r, a, b, inexact))
613 /* We got a borrow out of the subtraction. That means that
614 A and B had the same exponent, and B had the larger
615 significand. We need to swap the sign and negate the
616 significand. */
617 sign ^= 1;
618 neg_significand (r, r);
621 else
623 if (add_significands (r, a, b))
625 /* We got carry out of the addition. This means we need to
626 shift the significand back down one bit and increase the
627 exponent. */
628 inexact |= sticky_rshift_significand (r, r, 1);
629 r->sig[SIGSZ-1] |= SIG_MSB;
630 if (++exp > MAX_EXP)
632 get_inf (r, sign);
633 return true;
638 r->cl = rvc_normal;
639 r->sign = sign;
640 SET_REAL_EXP (r, exp);
641 /* Zero out the remaining fields. */
642 r->signalling = 0;
643 r->canonical = 0;
644 r->decimal = 0;
646 /* Re-normalize the result. */
647 normalize (r);
649 /* Special case: if the subtraction results in zero, the result
650 is positive. */
651 if (r->cl == rvc_zero)
652 r->sign = 0;
653 else
654 r->sig[0] |= inexact;
656 return inexact;
659 /* Calculate R = A * B. Return true if the result may be inexact. */
661 static bool
662 do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
663 const REAL_VALUE_TYPE *b)
665 REAL_VALUE_TYPE u, t, *rr;
666 unsigned int i, j, k;
667 int sign = a->sign ^ b->sign;
668 bool inexact = false;
670 switch (CLASS2 (a->cl, b->cl))
672 case CLASS2 (rvc_zero, rvc_zero):
673 case CLASS2 (rvc_zero, rvc_normal):
674 case CLASS2 (rvc_normal, rvc_zero):
675 /* +-0 * ANY = 0 with appropriate sign. */
676 get_zero (r, sign);
677 return false;
679 case CLASS2 (rvc_zero, rvc_nan):
680 case CLASS2 (rvc_normal, rvc_nan):
681 case CLASS2 (rvc_inf, rvc_nan):
682 case CLASS2 (rvc_nan, rvc_nan):
683 /* ANY * NaN = NaN. */
684 *r = *b;
685 r->sign = sign;
686 return false;
688 case CLASS2 (rvc_nan, rvc_zero):
689 case CLASS2 (rvc_nan, rvc_normal):
690 case CLASS2 (rvc_nan, rvc_inf):
691 /* NaN * ANY = NaN. */
692 *r = *a;
693 r->sign = sign;
694 return false;
696 case CLASS2 (rvc_zero, rvc_inf):
697 case CLASS2 (rvc_inf, rvc_zero):
698 /* 0 * Inf = NaN */
699 get_canonical_qnan (r, sign);
700 return false;
702 case CLASS2 (rvc_inf, rvc_inf):
703 case CLASS2 (rvc_normal, rvc_inf):
704 case CLASS2 (rvc_inf, rvc_normal):
705 /* Inf * Inf = Inf, R * Inf = Inf */
706 get_inf (r, sign);
707 return false;
709 case CLASS2 (rvc_normal, rvc_normal):
710 break;
712 default:
713 gcc_unreachable ();
716 if (r == a || r == b)
717 rr = &t;
718 else
719 rr = r;
720 get_zero (rr, 0);
722 /* Collect all the partial products. Since we don't have sure access
723 to a widening multiply, we split each long into two half-words.
725 Consider the long-hand form of a four half-word multiplication:
727 A B C D
728 * E F G H
729 --------------
730 DE DF DG DH
731 CE CF CG CH
732 BE BF BG BH
733 AE AF AG AH
735 We construct partial products of the widened half-word products
736 that are known to not overlap, e.g. DF+DH. Each such partial
737 product is given its proper exponent, which allows us to sum them
738 and obtain the finished product. */
740 for (i = 0; i < SIGSZ * 2; ++i)
742 unsigned long ai = a->sig[i / 2];
743 if (i & 1)
744 ai >>= HOST_BITS_PER_LONG / 2;
745 else
746 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
748 if (ai == 0)
749 continue;
751 for (j = 0; j < 2; ++j)
753 int exp = (REAL_EXP (a) - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
754 + (REAL_EXP (b) - (1-j)*(HOST_BITS_PER_LONG/2)));
756 if (exp > MAX_EXP)
758 get_inf (r, sign);
759 return true;
761 if (exp < -MAX_EXP)
763 /* Would underflow to zero, which we shouldn't bother adding. */
764 inexact = true;
765 continue;
768 memset (&u, 0, sizeof (u));
769 u.cl = rvc_normal;
770 SET_REAL_EXP (&u, exp);
772 for (k = j; k < SIGSZ * 2; k += 2)
774 unsigned long bi = b->sig[k / 2];
775 if (k & 1)
776 bi >>= HOST_BITS_PER_LONG / 2;
777 else
778 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
780 u.sig[k / 2] = ai * bi;
783 normalize (&u);
784 inexact |= do_add (rr, rr, &u, 0);
788 rr->sign = sign;
789 if (rr != r)
790 *r = t;
792 return inexact;
795 /* Calculate R = A / B. Return true if the result may be inexact. */
797 static bool
798 do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
799 const REAL_VALUE_TYPE *b)
801 int exp, sign = a->sign ^ b->sign;
802 REAL_VALUE_TYPE t, *rr;
803 bool inexact;
805 switch (CLASS2 (a->cl, b->cl))
807 case CLASS2 (rvc_zero, rvc_zero):
808 /* 0 / 0 = NaN. */
809 case CLASS2 (rvc_inf, rvc_inf):
810 /* Inf / Inf = NaN. */
811 get_canonical_qnan (r, sign);
812 return false;
814 case CLASS2 (rvc_zero, rvc_normal):
815 case CLASS2 (rvc_zero, rvc_inf):
816 /* 0 / ANY = 0. */
817 case CLASS2 (rvc_normal, rvc_inf):
818 /* R / Inf = 0. */
819 get_zero (r, sign);
820 return false;
822 case CLASS2 (rvc_normal, rvc_zero):
823 /* R / 0 = Inf. */
824 case CLASS2 (rvc_inf, rvc_zero):
825 /* Inf / 0 = Inf. */
826 get_inf (r, sign);
827 return false;
829 case CLASS2 (rvc_zero, rvc_nan):
830 case CLASS2 (rvc_normal, rvc_nan):
831 case CLASS2 (rvc_inf, rvc_nan):
832 case CLASS2 (rvc_nan, rvc_nan):
833 /* ANY / NaN = NaN. */
834 *r = *b;
835 r->sign = sign;
836 return false;
838 case CLASS2 (rvc_nan, rvc_zero):
839 case CLASS2 (rvc_nan, rvc_normal):
840 case CLASS2 (rvc_nan, rvc_inf):
841 /* NaN / ANY = NaN. */
842 *r = *a;
843 r->sign = sign;
844 return false;
846 case CLASS2 (rvc_inf, rvc_normal):
847 /* Inf / R = Inf. */
848 get_inf (r, sign);
849 return false;
851 case CLASS2 (rvc_normal, rvc_normal):
852 break;
854 default:
855 gcc_unreachable ();
858 if (r == a || r == b)
859 rr = &t;
860 else
861 rr = r;
863 /* Make sure all fields in the result are initialized. */
864 get_zero (rr, 0);
865 rr->cl = rvc_normal;
866 rr->sign = sign;
868 exp = REAL_EXP (a) - REAL_EXP (b) + 1;
869 if (exp > MAX_EXP)
871 get_inf (r, sign);
872 return true;
874 if (exp < -MAX_EXP)
876 get_zero (r, sign);
877 return true;
879 SET_REAL_EXP (rr, exp);
881 inexact = div_significands (rr, a, b);
883 /* Re-normalize the result. */
884 normalize (rr);
885 rr->sig[0] |= inexact;
887 if (rr != r)
888 *r = t;
890 return inexact;
893 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
894 one of the two operands is a NaN. */
896 static int
897 do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
898 int nan_result)
900 int ret;
902 switch (CLASS2 (a->cl, b->cl))
904 case CLASS2 (rvc_zero, rvc_zero):
905 /* Sign of zero doesn't matter for compares. */
906 return 0;
908 case CLASS2 (rvc_inf, rvc_zero):
909 case CLASS2 (rvc_inf, rvc_normal):
910 case CLASS2 (rvc_normal, rvc_zero):
911 return (a->sign ? -1 : 1);
913 case CLASS2 (rvc_inf, rvc_inf):
914 return -a->sign - -b->sign;
916 case CLASS2 (rvc_zero, rvc_normal):
917 case CLASS2 (rvc_zero, rvc_inf):
918 case CLASS2 (rvc_normal, rvc_inf):
919 return (b->sign ? 1 : -1);
921 case CLASS2 (rvc_zero, rvc_nan):
922 case CLASS2 (rvc_normal, rvc_nan):
923 case CLASS2 (rvc_inf, rvc_nan):
924 case CLASS2 (rvc_nan, rvc_nan):
925 case CLASS2 (rvc_nan, rvc_zero):
926 case CLASS2 (rvc_nan, rvc_normal):
927 case CLASS2 (rvc_nan, rvc_inf):
928 return nan_result;
930 case CLASS2 (rvc_normal, rvc_normal):
931 break;
933 default:
934 gcc_unreachable ();
937 if (a->sign != b->sign)
938 return -a->sign - -b->sign;
940 if (a->decimal || b->decimal)
941 return decimal_do_compare (a, b, nan_result);
943 if (REAL_EXP (a) > REAL_EXP (b))
944 ret = 1;
945 else if (REAL_EXP (a) < REAL_EXP (b))
946 ret = -1;
947 else
948 ret = cmp_significands (a, b);
950 return (a->sign ? -ret : ret);
953 /* Return A truncated to an integral value toward zero. */
955 static void
956 do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
958 *r = *a;
960 switch (r->cl)
962 case rvc_zero:
963 case rvc_inf:
964 case rvc_nan:
965 break;
967 case rvc_normal:
968 if (r->decimal)
970 decimal_do_fix_trunc (r, a);
971 return;
973 if (REAL_EXP (r) <= 0)
974 get_zero (r, r->sign);
975 else if (REAL_EXP (r) < SIGNIFICAND_BITS)
976 clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r));
977 break;
979 default:
980 gcc_unreachable ();
984 /* Perform the binary or unary operation described by CODE.
985 For a unary operation, leave OP1 NULL. This function returns
986 true if the result may be inexact due to loss of precision. */
988 bool
989 real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0,
990 const REAL_VALUE_TYPE *op1)
992 enum tree_code code = icode;
994 if (op0->decimal || (op1 && op1->decimal))
995 return decimal_real_arithmetic (r, icode, op0, op1);
997 switch (code)
999 case PLUS_EXPR:
1000 return do_add (r, op0, op1, 0);
1002 case MINUS_EXPR:
1003 return do_add (r, op0, op1, 1);
1005 case MULT_EXPR:
1006 return do_multiply (r, op0, op1);
1008 case RDIV_EXPR:
1009 return do_divide (r, op0, op1);
1011 case MIN_EXPR:
1012 if (op1->cl == rvc_nan)
1013 *r = *op1;
1014 else if (do_compare (op0, op1, -1) < 0)
1015 *r = *op0;
1016 else
1017 *r = *op1;
1018 break;
1020 case MAX_EXPR:
1021 if (op1->cl == rvc_nan)
1022 *r = *op1;
1023 else if (do_compare (op0, op1, 1) < 0)
1024 *r = *op1;
1025 else
1026 *r = *op0;
1027 break;
1029 case NEGATE_EXPR:
1030 *r = *op0;
1031 r->sign ^= 1;
1032 break;
1034 case ABS_EXPR:
1035 *r = *op0;
1036 r->sign = 0;
1037 break;
1039 case FIX_TRUNC_EXPR:
1040 do_fix_trunc (r, op0);
1041 break;
1043 default:
1044 gcc_unreachable ();
1046 return false;
1049 /* Legacy. Similar, but return the result directly. */
1051 REAL_VALUE_TYPE
1052 real_arithmetic2 (int icode, const REAL_VALUE_TYPE *op0,
1053 const REAL_VALUE_TYPE *op1)
1055 REAL_VALUE_TYPE r;
1056 real_arithmetic (&r, icode, op0, op1);
1057 return r;
1060 bool
1061 real_compare (int icode, const REAL_VALUE_TYPE *op0,
1062 const REAL_VALUE_TYPE *op1)
1064 enum tree_code code = icode;
1066 switch (code)
1068 case LT_EXPR:
1069 return do_compare (op0, op1, 1) < 0;
1070 case LE_EXPR:
1071 return do_compare (op0, op1, 1) <= 0;
1072 case GT_EXPR:
1073 return do_compare (op0, op1, -1) > 0;
1074 case GE_EXPR:
1075 return do_compare (op0, op1, -1) >= 0;
1076 case EQ_EXPR:
1077 return do_compare (op0, op1, -1) == 0;
1078 case NE_EXPR:
1079 return do_compare (op0, op1, -1) != 0;
1080 case UNORDERED_EXPR:
1081 return op0->cl == rvc_nan || op1->cl == rvc_nan;
1082 case ORDERED_EXPR:
1083 return op0->cl != rvc_nan && op1->cl != rvc_nan;
1084 case UNLT_EXPR:
1085 return do_compare (op0, op1, -1) < 0;
1086 case UNLE_EXPR:
1087 return do_compare (op0, op1, -1) <= 0;
1088 case UNGT_EXPR:
1089 return do_compare (op0, op1, 1) > 0;
1090 case UNGE_EXPR:
1091 return do_compare (op0, op1, 1) >= 0;
1092 case UNEQ_EXPR:
1093 return do_compare (op0, op1, 0) == 0;
1094 case LTGT_EXPR:
1095 return do_compare (op0, op1, 0) != 0;
1097 default:
1098 gcc_unreachable ();
1102 /* Return floor log2(R). */
1105 real_exponent (const REAL_VALUE_TYPE *r)
1107 switch (r->cl)
1109 case rvc_zero:
1110 return 0;
1111 case rvc_inf:
1112 case rvc_nan:
1113 return (unsigned int)-1 >> 1;
1114 case rvc_normal:
1115 return REAL_EXP (r);
1116 default:
1117 gcc_unreachable ();
1121 /* R = OP0 * 2**EXP. */
1123 void
1124 real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1126 *r = *op0;
1127 switch (r->cl)
1129 case rvc_zero:
1130 case rvc_inf:
1131 case rvc_nan:
1132 break;
1134 case rvc_normal:
1135 exp += REAL_EXP (op0);
1136 if (exp > MAX_EXP)
1137 get_inf (r, r->sign);
1138 else if (exp < -MAX_EXP)
1139 get_zero (r, r->sign);
1140 else
1141 SET_REAL_EXP (r, exp);
1142 break;
1144 default:
1145 gcc_unreachable ();
1149 /* Determine whether a floating-point value X is infinite. */
1151 bool
1152 real_isinf (const REAL_VALUE_TYPE *r)
1154 return (r->cl == rvc_inf);
1157 /* Determine whether a floating-point value X is a NaN. */
1159 bool
1160 real_isnan (const REAL_VALUE_TYPE *r)
1162 return (r->cl == rvc_nan);
1165 /* Determine whether a floating-point value X is finite. */
1167 bool
1168 real_isfinite (const REAL_VALUE_TYPE *r)
1170 return (r->cl != rvc_nan) && (r->cl != rvc_inf);
1173 /* Determine whether a floating-point value X is negative. */
1175 bool
1176 real_isneg (const REAL_VALUE_TYPE *r)
1178 return r->sign;
1181 /* Determine whether a floating-point value X is minus zero. */
1183 bool
1184 real_isnegzero (const REAL_VALUE_TYPE *r)
1186 return r->sign && r->cl == rvc_zero;
1189 /* Compare two floating-point objects for bitwise identity. */
1191 bool
1192 real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1194 int i;
1196 if (a->cl != b->cl)
1197 return false;
1198 if (a->sign != b->sign)
1199 return false;
1201 switch (a->cl)
1203 case rvc_zero:
1204 case rvc_inf:
1205 return true;
1207 case rvc_normal:
1208 if (a->decimal != b->decimal)
1209 return false;
1210 if (REAL_EXP (a) != REAL_EXP (b))
1211 return false;
1212 break;
1214 case rvc_nan:
1215 if (a->signalling != b->signalling)
1216 return false;
1217 /* The significand is ignored for canonical NaNs. */
1218 if (a->canonical || b->canonical)
1219 return a->canonical == b->canonical;
1220 break;
1222 default:
1223 gcc_unreachable ();
1226 for (i = 0; i < SIGSZ; ++i)
1227 if (a->sig[i] != b->sig[i])
1228 return false;
1230 return true;
1233 /* Try to change R into its exact multiplicative inverse in machine
1234 mode MODE. Return true if successful. */
1236 bool
1237 exact_real_inverse (enum machine_mode mode, REAL_VALUE_TYPE *r)
1239 const REAL_VALUE_TYPE *one = real_digit (1);
1240 REAL_VALUE_TYPE u;
1241 int i;
1243 if (r->cl != rvc_normal)
1244 return false;
1246 /* Check for a power of two: all significand bits zero except the MSB. */
1247 for (i = 0; i < SIGSZ-1; ++i)
1248 if (r->sig[i] != 0)
1249 return false;
1250 if (r->sig[SIGSZ-1] != SIG_MSB)
1251 return false;
1253 /* Find the inverse and truncate to the required mode. */
1254 do_divide (&u, one, r);
1255 real_convert (&u, mode, &u);
1257 /* The rounding may have overflowed. */
1258 if (u.cl != rvc_normal)
1259 return false;
1260 for (i = 0; i < SIGSZ-1; ++i)
1261 if (u.sig[i] != 0)
1262 return false;
1263 if (u.sig[SIGSZ-1] != SIG_MSB)
1264 return false;
1266 *r = u;
1267 return true;
1270 /* Render R as an integer. */
1272 HOST_WIDE_INT
1273 real_to_integer (const REAL_VALUE_TYPE *r)
1275 unsigned HOST_WIDE_INT i;
1277 switch (r->cl)
1279 case rvc_zero:
1280 underflow:
1281 return 0;
1283 case rvc_inf:
1284 case rvc_nan:
1285 overflow:
1286 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1287 if (!r->sign)
1288 i--;
1289 return i;
1291 case rvc_normal:
1292 if (r->decimal)
1293 return decimal_real_to_integer (r);
1295 if (REAL_EXP (r) <= 0)
1296 goto underflow;
1297 /* Only force overflow for unsigned overflow. Signed overflow is
1298 undefined, so it doesn't matter what we return, and some callers
1299 expect to be able to use this routine for both signed and
1300 unsigned conversions. */
1301 if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1302 goto overflow;
1304 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1305 i = r->sig[SIGSZ-1];
1306 else
1308 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1309 i = r->sig[SIGSZ-1];
1310 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1311 i |= r->sig[SIGSZ-2];
1314 i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1316 if (r->sign)
1317 i = -i;
1318 return i;
1320 default:
1321 gcc_unreachable ();
1325 /* Likewise, but to an integer pair, HI+LOW. */
1327 void
1328 real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
1329 const REAL_VALUE_TYPE *r)
1331 REAL_VALUE_TYPE t;
1332 HOST_WIDE_INT low, high;
1333 int exp;
1335 switch (r->cl)
1337 case rvc_zero:
1338 underflow:
1339 low = high = 0;
1340 break;
1342 case rvc_inf:
1343 case rvc_nan:
1344 overflow:
1345 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1346 if (r->sign)
1347 low = 0;
1348 else
1350 high--;
1351 low = -1;
1353 break;
1355 case rvc_normal:
1356 if (r->decimal)
1358 decimal_real_to_integer2 (plow, phigh, r);
1359 return;
1362 exp = REAL_EXP (r);
1363 if (exp <= 0)
1364 goto underflow;
1365 /* Only force overflow for unsigned overflow. Signed overflow is
1366 undefined, so it doesn't matter what we return, and some callers
1367 expect to be able to use this routine for both signed and
1368 unsigned conversions. */
1369 if (exp > 2*HOST_BITS_PER_WIDE_INT)
1370 goto overflow;
1372 rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
1373 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1375 high = t.sig[SIGSZ-1];
1376 low = t.sig[SIGSZ-2];
1378 else
1380 gcc_assert (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG);
1381 high = t.sig[SIGSZ-1];
1382 high = high << (HOST_BITS_PER_LONG - 1) << 1;
1383 high |= t.sig[SIGSZ-2];
1385 low = t.sig[SIGSZ-3];
1386 low = low << (HOST_BITS_PER_LONG - 1) << 1;
1387 low |= t.sig[SIGSZ-4];
1390 if (r->sign)
1392 if (low == 0)
1393 high = -high;
1394 else
1395 low = -low, high = ~high;
1397 break;
1399 default:
1400 gcc_unreachable ();
1403 *plow = low;
1404 *phigh = high;
1407 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1408 of NUM / DEN. Return the quotient and place the remainder in NUM.
1409 It is expected that NUM / DEN are close enough that the quotient is
1410 small. */
1412 static unsigned long
1413 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1415 unsigned long q, msb;
1416 int expn = REAL_EXP (num), expd = REAL_EXP (den);
1418 if (expn < expd)
1419 return 0;
1421 q = msb = 0;
1422 goto start;
1425 msb = num->sig[SIGSZ-1] & SIG_MSB;
1426 q <<= 1;
1427 lshift_significand_1 (num, num);
1428 start:
1429 if (msb || cmp_significands (num, den) >= 0)
1431 sub_significands (num, num, den, 0);
1432 q |= 1;
1435 while (--expn >= expd);
1437 SET_REAL_EXP (num, expd);
1438 normalize (num);
1440 return q;
1443 /* Render R as a decimal floating point constant. Emit DIGITS significant
1444 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1445 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1446 zeros. */
1448 #define M_LOG10_2 0.30102999566398119521
1450 void
1451 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1452 size_t digits, int crop_trailing_zeros)
1454 const REAL_VALUE_TYPE *one, *ten;
1455 REAL_VALUE_TYPE r, pten, u, v;
1456 int dec_exp, cmp_one, digit;
1457 size_t max_digits;
1458 char *p, *first, *last;
1459 bool sign;
1461 r = *r_orig;
1462 switch (r.cl)
1464 case rvc_zero:
1465 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1466 return;
1467 case rvc_normal:
1468 break;
1469 case rvc_inf:
1470 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1471 return;
1472 case rvc_nan:
1473 /* ??? Print the significand as well, if not canonical? */
1474 strcpy (str, (r.sign ? "-NaN" : "+NaN"));
1475 return;
1476 default:
1477 gcc_unreachable ();
1480 if (r.decimal)
1482 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1483 return;
1486 /* Bound the number of digits printed by the size of the representation. */
1487 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1488 if (digits == 0 || digits > max_digits)
1489 digits = max_digits;
1491 /* Estimate the decimal exponent, and compute the length of the string it
1492 will print as. Be conservative and add one to account for possible
1493 overflow or rounding error. */
1494 dec_exp = REAL_EXP (&r) * M_LOG10_2;
1495 for (max_digits = 1; dec_exp ; max_digits++)
1496 dec_exp /= 10;
1498 /* Bound the number of digits printed by the size of the output buffer. */
1499 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1500 gcc_assert (max_digits <= buf_size);
1501 if (digits > max_digits)
1502 digits = max_digits;
1504 one = real_digit (1);
1505 ten = ten_to_ptwo (0);
1507 sign = r.sign;
1508 r.sign = 0;
1510 dec_exp = 0;
1511 pten = *one;
1513 cmp_one = do_compare (&r, one, 0);
1514 if (cmp_one > 0)
1516 int m;
1518 /* Number is greater than one. Convert significand to an integer
1519 and strip trailing decimal zeros. */
1521 u = r;
1522 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1524 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1525 m = floor_log2 (max_digits);
1527 /* Iterate over the bits of the possible powers of 10 that might
1528 be present in U and eliminate them. That is, if we find that
1529 10**2**M divides U evenly, keep the division and increase
1530 DEC_EXP by 2**M. */
1533 REAL_VALUE_TYPE t;
1535 do_divide (&t, &u, ten_to_ptwo (m));
1536 do_fix_trunc (&v, &t);
1537 if (cmp_significands (&v, &t) == 0)
1539 u = t;
1540 dec_exp += 1 << m;
1543 while (--m >= 0);
1545 /* Revert the scaling to integer that we performed earlier. */
1546 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1547 - (SIGNIFICAND_BITS - 1));
1548 r = u;
1550 /* Find power of 10. Do this by dividing out 10**2**M when
1551 this is larger than the current remainder. Fill PTEN with
1552 the power of 10 that we compute. */
1553 if (REAL_EXP (&r) > 0)
1555 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1558 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1559 if (do_compare (&u, ptentwo, 0) >= 0)
1561 do_divide (&u, &u, ptentwo);
1562 do_multiply (&pten, &pten, ptentwo);
1563 dec_exp += 1 << m;
1566 while (--m >= 0);
1568 else
1569 /* We managed to divide off enough tens in the above reduction
1570 loop that we've now got a negative exponent. Fall into the
1571 less-than-one code to compute the proper value for PTEN. */
1572 cmp_one = -1;
1574 if (cmp_one < 0)
1576 int m;
1578 /* Number is less than one. Pad significand with leading
1579 decimal zeros. */
1581 v = r;
1582 while (1)
1584 /* Stop if we'd shift bits off the bottom. */
1585 if (v.sig[0] & 7)
1586 break;
1588 do_multiply (&u, &v, ten);
1590 /* Stop if we're now >= 1. */
1591 if (REAL_EXP (&u) > 0)
1592 break;
1594 v = u;
1595 dec_exp -= 1;
1597 r = v;
1599 /* Find power of 10. Do this by multiplying in P=10**2**M when
1600 the current remainder is smaller than 1/P. Fill PTEN with the
1601 power of 10 that we compute. */
1602 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1605 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1606 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1608 if (do_compare (&v, ptenmtwo, 0) <= 0)
1610 do_multiply (&v, &v, ptentwo);
1611 do_multiply (&pten, &pten, ptentwo);
1612 dec_exp -= 1 << m;
1615 while (--m >= 0);
1617 /* Invert the positive power of 10 that we've collected so far. */
1618 do_divide (&pten, one, &pten);
1621 p = str;
1622 if (sign)
1623 *p++ = '-';
1624 first = p++;
1626 /* At this point, PTEN should contain the nearest power of 10 smaller
1627 than R, such that this division produces the first digit.
1629 Using a divide-step primitive that returns the complete integral
1630 remainder avoids the rounding error that would be produced if
1631 we were to use do_divide here and then simply multiply by 10 for
1632 each subsequent digit. */
1634 digit = rtd_divmod (&r, &pten);
1636 /* Be prepared for error in that division via underflow ... */
1637 if (digit == 0 && cmp_significand_0 (&r))
1639 /* Multiply by 10 and try again. */
1640 do_multiply (&r, &r, ten);
1641 digit = rtd_divmod (&r, &pten);
1642 dec_exp -= 1;
1643 gcc_assert (digit != 0);
1646 /* ... or overflow. */
1647 if (digit == 10)
1649 *p++ = '1';
1650 if (--digits > 0)
1651 *p++ = '0';
1652 dec_exp += 1;
1654 else
1656 gcc_assert (digit <= 10);
1657 *p++ = digit + '0';
1660 /* Generate subsequent digits. */
1661 while (--digits > 0)
1663 do_multiply (&r, &r, ten);
1664 digit = rtd_divmod (&r, &pten);
1665 *p++ = digit + '0';
1667 last = p;
1669 /* Generate one more digit with which to do rounding. */
1670 do_multiply (&r, &r, ten);
1671 digit = rtd_divmod (&r, &pten);
1673 /* Round the result. */
1674 if (digit == 5)
1676 /* Round to nearest. If R is nonzero there are additional
1677 nonzero digits to be extracted. */
1678 if (cmp_significand_0 (&r))
1679 digit++;
1680 /* Round to even. */
1681 else if ((p[-1] - '0') & 1)
1682 digit++;
1684 if (digit > 5)
1686 while (p > first)
1688 digit = *--p;
1689 if (digit == '9')
1690 *p = '0';
1691 else
1693 *p = digit + 1;
1694 break;
1698 /* Carry out of the first digit. This means we had all 9's and
1699 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1700 if (p == first)
1702 first[1] = '1';
1703 dec_exp++;
1707 /* Insert the decimal point. */
1708 first[0] = first[1];
1709 first[1] = '.';
1711 /* If requested, drop trailing zeros. Never crop past "1.0". */
1712 if (crop_trailing_zeros)
1713 while (last > first + 3 && last[-1] == '0')
1714 last--;
1716 /* Append the exponent. */
1717 sprintf (last, "e%+d", dec_exp);
1720 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1721 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1722 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1723 strip trailing zeros. */
1725 void
1726 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1727 size_t digits, int crop_trailing_zeros)
1729 int i, j, exp = REAL_EXP (r);
1730 char *p, *first;
1731 char exp_buf[16];
1732 size_t max_digits;
1734 switch (r->cl)
1736 case rvc_zero:
1737 exp = 0;
1738 break;
1739 case rvc_normal:
1740 break;
1741 case rvc_inf:
1742 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1743 return;
1744 case rvc_nan:
1745 /* ??? Print the significand as well, if not canonical? */
1746 strcpy (str, (r->sign ? "-NaN" : "+NaN"));
1747 return;
1748 default:
1749 gcc_unreachable ();
1752 if (r->decimal)
1754 /* Hexadecimal format for decimal floats is not interesting. */
1755 strcpy (str, "N/A");
1756 return;
1759 if (digits == 0)
1760 digits = SIGNIFICAND_BITS / 4;
1762 /* Bound the number of digits printed by the size of the output buffer. */
1764 sprintf (exp_buf, "p%+d", exp);
1765 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1766 gcc_assert (max_digits <= buf_size);
1767 if (digits > max_digits)
1768 digits = max_digits;
1770 p = str;
1771 if (r->sign)
1772 *p++ = '-';
1773 *p++ = '0';
1774 *p++ = 'x';
1775 *p++ = '0';
1776 *p++ = '.';
1777 first = p;
1779 for (i = SIGSZ - 1; i >= 0; --i)
1780 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1782 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1783 if (--digits == 0)
1784 goto out;
1787 out:
1788 if (crop_trailing_zeros)
1789 while (p > first + 1 && p[-1] == '0')
1790 p--;
1792 sprintf (p, "p%+d", exp);
1795 /* Initialize R from a decimal or hexadecimal string. The string is
1796 assumed to have been syntax checked already. Return -1 if the
1797 value underflows, +1 if overflows, and 0 otherwise. */
1800 real_from_string (REAL_VALUE_TYPE *r, const char *str)
1802 int exp = 0;
1803 bool sign = false;
1805 get_zero (r, 0);
1807 if (*str == '-')
1809 sign = true;
1810 str++;
1812 else if (*str == '+')
1813 str++;
1815 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1817 /* Hexadecimal floating point. */
1818 int pos = SIGNIFICAND_BITS - 4, d;
1820 str += 2;
1822 while (*str == '0')
1823 str++;
1824 while (1)
1826 d = hex_value (*str);
1827 if (d == _hex_bad)
1828 break;
1829 if (pos >= 0)
1831 r->sig[pos / HOST_BITS_PER_LONG]
1832 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1833 pos -= 4;
1835 else if (d)
1836 /* Ensure correct rounding by setting last bit if there is
1837 a subsequent nonzero digit. */
1838 r->sig[0] |= 1;
1839 exp += 4;
1840 str++;
1842 if (*str == '.')
1844 str++;
1845 if (pos == SIGNIFICAND_BITS - 4)
1847 while (*str == '0')
1848 str++, exp -= 4;
1850 while (1)
1852 d = hex_value (*str);
1853 if (d == _hex_bad)
1854 break;
1855 if (pos >= 0)
1857 r->sig[pos / HOST_BITS_PER_LONG]
1858 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1859 pos -= 4;
1861 else if (d)
1862 /* Ensure correct rounding by setting last bit if there is
1863 a subsequent nonzero digit. */
1864 r->sig[0] |= 1;
1865 str++;
1869 /* If the mantissa is zero, ignore the exponent. */
1870 if (!cmp_significand_0 (r))
1871 goto is_a_zero;
1873 if (*str == 'p' || *str == 'P')
1875 bool exp_neg = false;
1877 str++;
1878 if (*str == '-')
1880 exp_neg = true;
1881 str++;
1883 else if (*str == '+')
1884 str++;
1886 d = 0;
1887 while (ISDIGIT (*str))
1889 d *= 10;
1890 d += *str - '0';
1891 if (d > MAX_EXP)
1893 /* Overflowed the exponent. */
1894 if (exp_neg)
1895 goto underflow;
1896 else
1897 goto overflow;
1899 str++;
1901 if (exp_neg)
1902 d = -d;
1904 exp += d;
1907 r->cl = rvc_normal;
1908 SET_REAL_EXP (r, exp);
1910 normalize (r);
1912 else
1914 /* Decimal floating point. */
1915 const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
1916 int d;
1918 while (*str == '0')
1919 str++;
1920 while (ISDIGIT (*str))
1922 d = *str++ - '0';
1923 do_multiply (r, r, ten);
1924 if (d)
1925 do_add (r, r, real_digit (d), 0);
1927 if (*str == '.')
1929 str++;
1930 if (r->cl == rvc_zero)
1932 while (*str == '0')
1933 str++, exp--;
1935 while (ISDIGIT (*str))
1937 d = *str++ - '0';
1938 do_multiply (r, r, ten);
1939 if (d)
1940 do_add (r, r, real_digit (d), 0);
1941 exp--;
1945 /* If the mantissa is zero, ignore the exponent. */
1946 if (r->cl == rvc_zero)
1947 goto is_a_zero;
1949 if (*str == 'e' || *str == 'E')
1951 bool exp_neg = false;
1953 str++;
1954 if (*str == '-')
1956 exp_neg = true;
1957 str++;
1959 else if (*str == '+')
1960 str++;
1962 d = 0;
1963 while (ISDIGIT (*str))
1965 d *= 10;
1966 d += *str - '0';
1967 if (d > MAX_EXP)
1969 /* Overflowed the exponent. */
1970 if (exp_neg)
1971 goto underflow;
1972 else
1973 goto overflow;
1975 str++;
1977 if (exp_neg)
1978 d = -d;
1979 exp += d;
1982 if (exp)
1983 times_pten (r, exp);
1986 r->sign = sign;
1987 return 0;
1989 is_a_zero:
1990 get_zero (r, sign);
1991 return 0;
1993 underflow:
1994 get_zero (r, sign);
1995 return -1;
1997 overflow:
1998 get_inf (r, sign);
1999 return 1;
2002 /* Legacy. Similar, but return the result directly. */
2004 REAL_VALUE_TYPE
2005 real_from_string2 (const char *s, enum machine_mode mode)
2007 REAL_VALUE_TYPE r;
2009 real_from_string (&r, s);
2010 if (mode != VOIDmode)
2011 real_convert (&r, mode, &r);
2013 return r;
2016 /* Initialize R from string S and desired MODE. */
2018 void
2019 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, enum machine_mode mode)
2021 if (DECIMAL_FLOAT_MODE_P (mode))
2022 decimal_real_from_string (r, s);
2023 else
2024 real_from_string (r, s);
2026 if (mode != VOIDmode)
2027 real_convert (r, mode, r);
2030 /* Initialize R from the integer pair HIGH+LOW. */
2032 void
2033 real_from_integer (REAL_VALUE_TYPE *r, enum machine_mode mode,
2034 unsigned HOST_WIDE_INT low, HOST_WIDE_INT high,
2035 int unsigned_p)
2037 if (low == 0 && high == 0)
2038 get_zero (r, 0);
2039 else
2041 memset (r, 0, sizeof (*r));
2042 r->cl = rvc_normal;
2043 r->sign = high < 0 && !unsigned_p;
2044 SET_REAL_EXP (r, 2 * HOST_BITS_PER_WIDE_INT);
2046 if (r->sign)
2048 high = ~high;
2049 if (low == 0)
2050 high += 1;
2051 else
2052 low = -low;
2055 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2057 r->sig[SIGSZ-1] = high;
2058 r->sig[SIGSZ-2] = low;
2060 else
2062 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2063 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
2064 r->sig[SIGSZ-2] = high;
2065 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
2066 r->sig[SIGSZ-4] = low;
2069 normalize (r);
2072 if (mode != VOIDmode)
2073 real_convert (r, mode, r);
2076 /* Returns 10**2**N. */
2078 static const REAL_VALUE_TYPE *
2079 ten_to_ptwo (int n)
2081 static REAL_VALUE_TYPE tens[EXP_BITS];
2083 gcc_assert (n >= 0);
2084 gcc_assert (n < EXP_BITS);
2086 if (tens[n].cl == rvc_zero)
2088 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2090 HOST_WIDE_INT t = 10;
2091 int i;
2093 for (i = 0; i < n; ++i)
2094 t *= t;
2096 real_from_integer (&tens[n], VOIDmode, t, 0, 1);
2098 else
2100 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2101 do_multiply (&tens[n], t, t);
2105 return &tens[n];
2108 /* Returns 10**(-2**N). */
2110 static const REAL_VALUE_TYPE *
2111 ten_to_mptwo (int n)
2113 static REAL_VALUE_TYPE tens[EXP_BITS];
2115 gcc_assert (n >= 0);
2116 gcc_assert (n < EXP_BITS);
2118 if (tens[n].cl == rvc_zero)
2119 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2121 return &tens[n];
2124 /* Returns N. */
2126 static const REAL_VALUE_TYPE *
2127 real_digit (int n)
2129 static REAL_VALUE_TYPE num[10];
2131 gcc_assert (n >= 0);
2132 gcc_assert (n <= 9);
2134 if (n > 0 && num[n].cl == rvc_zero)
2135 real_from_integer (&num[n], VOIDmode, n, 0, 1);
2137 return &num[n];
2140 /* Multiply R by 10**EXP. */
2142 static void
2143 times_pten (REAL_VALUE_TYPE *r, int exp)
2145 REAL_VALUE_TYPE pten, *rr;
2146 bool negative = (exp < 0);
2147 int i;
2149 if (negative)
2151 exp = -exp;
2152 pten = *real_digit (1);
2153 rr = &pten;
2155 else
2156 rr = r;
2158 for (i = 0; exp > 0; ++i, exp >>= 1)
2159 if (exp & 1)
2160 do_multiply (rr, rr, ten_to_ptwo (i));
2162 if (negative)
2163 do_divide (r, r, &pten);
2166 /* Returns the special REAL_VALUE_TYPE enumerated by E. */
2168 const REAL_VALUE_TYPE *
2169 get_real_const (enum real_value_const e)
2171 static REAL_VALUE_TYPE value[rv_max];
2173 gcc_assert (e < rv_max);
2175 /* Initialize mathematical constants for constant folding builtins.
2176 These constants need to be given to at least 160 bits precision. */
2177 if (value[e].cl == rvc_zero)
2178 switch (e)
2180 case rv_e:
2182 mpfr_t m;
2183 mpfr_init2 (m, SIGNIFICAND_BITS);
2184 mpfr_set_ui (m, 1, GMP_RNDN);
2185 mpfr_exp (m, m, GMP_RNDN);
2186 real_from_mpfr (&value[e], m, NULL_TREE, GMP_RNDN);
2187 mpfr_clear (m);
2189 break;
2190 case rv_third:
2191 real_arithmetic (&value[e], RDIV_EXPR, &dconst1, real_digit (3));
2192 break;
2193 case rv_sqrt2:
2195 mpfr_t m;
2196 mpfr_init2 (m, SIGNIFICAND_BITS);
2197 mpfr_sqrt_ui (m, 2, GMP_RNDN);
2198 real_from_mpfr (&value[e], m, NULL_TREE, GMP_RNDN);
2199 mpfr_clear (m);
2201 break;
2202 default:
2203 gcc_unreachable();
2206 return &value[e];
2209 /* Fills R with +Inf. */
2211 void
2212 real_inf (REAL_VALUE_TYPE *r)
2214 get_inf (r, 0);
2217 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2218 we force a QNaN, else we force an SNaN. The string, if not empty,
2219 is parsed as a number and placed in the significand. Return true
2220 if the string was successfully parsed. */
2222 bool
2223 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2224 enum machine_mode mode)
2226 const struct real_format *fmt;
2228 fmt = REAL_MODE_FORMAT (mode);
2229 gcc_assert (fmt);
2231 if (*str == 0)
2233 if (quiet)
2234 get_canonical_qnan (r, 0);
2235 else
2236 get_canonical_snan (r, 0);
2238 else
2240 int base = 10, d;
2242 memset (r, 0, sizeof (*r));
2243 r->cl = rvc_nan;
2245 /* Parse akin to strtol into the significand of R. */
2247 while (ISSPACE (*str))
2248 str++;
2249 if (*str == '-')
2250 str++;
2251 else if (*str == '+')
2252 str++;
2253 if (*str == '0')
2255 str++;
2256 if (*str == 'x' || *str == 'X')
2258 base = 16;
2259 str++;
2261 else
2262 base = 8;
2265 while ((d = hex_value (*str)) < base)
2267 REAL_VALUE_TYPE u;
2269 switch (base)
2271 case 8:
2272 lshift_significand (r, r, 3);
2273 break;
2274 case 16:
2275 lshift_significand (r, r, 4);
2276 break;
2277 case 10:
2278 lshift_significand_1 (&u, r);
2279 lshift_significand (r, r, 3);
2280 add_significands (r, r, &u);
2281 break;
2282 default:
2283 gcc_unreachable ();
2286 get_zero (&u, 0);
2287 u.sig[0] = d;
2288 add_significands (r, r, &u);
2290 str++;
2293 /* Must have consumed the entire string for success. */
2294 if (*str != 0)
2295 return false;
2297 /* Shift the significand into place such that the bits
2298 are in the most significant bits for the format. */
2299 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2301 /* Our MSB is always unset for NaNs. */
2302 r->sig[SIGSZ-1] &= ~SIG_MSB;
2304 /* Force quiet or signalling NaN. */
2305 r->signalling = !quiet;
2308 return true;
2311 /* Fills R with the largest finite value representable in mode MODE.
2312 If SIGN is nonzero, R is set to the most negative finite value. */
2314 void
2315 real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
2317 const struct real_format *fmt;
2318 int np2;
2320 fmt = REAL_MODE_FORMAT (mode);
2321 gcc_assert (fmt);
2322 memset (r, 0, sizeof (*r));
2324 if (fmt->b == 10)
2325 decimal_real_maxval (r, sign, mode);
2326 else
2328 r->cl = rvc_normal;
2329 r->sign = sign;
2330 SET_REAL_EXP (r, fmt->emax);
2332 np2 = SIGNIFICAND_BITS - fmt->p;
2333 memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2334 clear_significand_below (r, np2);
2336 if (fmt->pnan < fmt->p)
2337 /* This is an IBM extended double format made up of two IEEE
2338 doubles. The value of the long double is the sum of the
2339 values of the two parts. The most significant part is
2340 required to be the value of the long double rounded to the
2341 nearest double. Rounding means we need a slightly smaller
2342 value for LDBL_MAX. */
2343 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan);
2347 /* Fills R with 2**N. */
2349 void
2350 real_2expN (REAL_VALUE_TYPE *r, int n, enum machine_mode fmode)
2352 memset (r, 0, sizeof (*r));
2354 n++;
2355 if (n > MAX_EXP)
2356 r->cl = rvc_inf;
2357 else if (n < -MAX_EXP)
2359 else
2361 r->cl = rvc_normal;
2362 SET_REAL_EXP (r, n);
2363 r->sig[SIGSZ-1] = SIG_MSB;
2365 if (DECIMAL_FLOAT_MODE_P (fmode))
2366 decimal_real_convert (r, fmode, r);
2370 static void
2371 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2373 int p2, np2, i, w;
2374 unsigned long sticky;
2375 bool guard, lsb;
2376 int emin2m1, emax2;
2378 if (r->decimal)
2380 if (fmt->b == 10)
2382 decimal_round_for_format (fmt, r);
2383 return;
2385 /* FIXME. We can come here via fp_easy_constant
2386 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2387 investigated whether this convert needs to be here, or
2388 something else is missing. */
2389 decimal_real_convert (r, DFmode, r);
2392 p2 = fmt->p;
2393 emin2m1 = fmt->emin - 1;
2394 emax2 = fmt->emax;
2396 np2 = SIGNIFICAND_BITS - p2;
2397 switch (r->cl)
2399 underflow:
2400 get_zero (r, r->sign);
2401 case rvc_zero:
2402 if (!fmt->has_signed_zero)
2403 r->sign = 0;
2404 return;
2406 overflow:
2407 get_inf (r, r->sign);
2408 case rvc_inf:
2409 return;
2411 case rvc_nan:
2412 clear_significand_below (r, np2);
2413 return;
2415 case rvc_normal:
2416 break;
2418 default:
2419 gcc_unreachable ();
2422 /* Check the range of the exponent. If we're out of range,
2423 either underflow or overflow. */
2424 if (REAL_EXP (r) > emax2)
2425 goto overflow;
2426 else if (REAL_EXP (r) <= emin2m1)
2428 int diff;
2430 if (!fmt->has_denorm)
2432 /* Don't underflow completely until we've had a chance to round. */
2433 if (REAL_EXP (r) < emin2m1)
2434 goto underflow;
2436 else
2438 diff = emin2m1 - REAL_EXP (r) + 1;
2439 if (diff > p2)
2440 goto underflow;
2442 /* De-normalize the significand. */
2443 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2444 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2448 /* There are P2 true significand bits, followed by one guard bit,
2449 followed by one sticky bit, followed by stuff. Fold nonzero
2450 stuff into the sticky bit. */
2452 sticky = 0;
2453 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2454 sticky |= r->sig[i];
2455 sticky |=
2456 r->sig[w] & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2458 guard = test_significand_bit (r, np2 - 1);
2459 lsb = test_significand_bit (r, np2);
2461 /* Round to even. */
2462 if (guard && (sticky || lsb))
2464 REAL_VALUE_TYPE u;
2465 get_zero (&u, 0);
2466 set_significand_bit (&u, np2);
2468 if (add_significands (r, r, &u))
2470 /* Overflow. Means the significand had been all ones, and
2471 is now all zeros. Need to increase the exponent, and
2472 possibly re-normalize it. */
2473 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2474 if (REAL_EXP (r) > emax2)
2475 goto overflow;
2476 r->sig[SIGSZ-1] = SIG_MSB;
2480 /* Catch underflow that we deferred until after rounding. */
2481 if (REAL_EXP (r) <= emin2m1)
2482 goto underflow;
2484 /* Clear out trailing garbage. */
2485 clear_significand_below (r, np2);
2488 /* Extend or truncate to a new mode. */
2490 void
2491 real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
2492 const REAL_VALUE_TYPE *a)
2494 const struct real_format *fmt;
2496 fmt = REAL_MODE_FORMAT (mode);
2497 gcc_assert (fmt);
2499 *r = *a;
2501 if (a->decimal || fmt->b == 10)
2502 decimal_real_convert (r, mode, a);
2504 round_for_format (fmt, r);
2506 /* round_for_format de-normalizes denormals. Undo just that part. */
2507 if (r->cl == rvc_normal)
2508 normalize (r);
2511 /* Legacy. Likewise, except return the struct directly. */
2513 REAL_VALUE_TYPE
2514 real_value_truncate (enum machine_mode mode, REAL_VALUE_TYPE a)
2516 REAL_VALUE_TYPE r;
2517 real_convert (&r, mode, &a);
2518 return r;
2521 /* Return true if truncating to MODE is exact. */
2523 bool
2524 exact_real_truncate (enum machine_mode mode, const REAL_VALUE_TYPE *a)
2526 const struct real_format *fmt;
2527 REAL_VALUE_TYPE t;
2528 int emin2m1;
2530 fmt = REAL_MODE_FORMAT (mode);
2531 gcc_assert (fmt);
2533 /* Don't allow conversion to denormals. */
2534 emin2m1 = fmt->emin - 1;
2535 if (REAL_EXP (a) <= emin2m1)
2536 return false;
2538 /* After conversion to the new mode, the value must be identical. */
2539 real_convert (&t, mode, a);
2540 return real_identical (&t, a);
2543 /* Write R to the given target format. Place the words of the result
2544 in target word order in BUF. There are always 32 bits in each
2545 long, no matter the size of the host long.
2547 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2549 long
2550 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
2551 const struct real_format *fmt)
2553 REAL_VALUE_TYPE r;
2554 long buf1;
2556 r = *r_orig;
2557 round_for_format (fmt, &r);
2559 if (!buf)
2560 buf = &buf1;
2561 (*fmt->encode) (fmt, buf, &r);
2563 return *buf;
2566 /* Similar, but look up the format from MODE. */
2568 long
2569 real_to_target (long *buf, const REAL_VALUE_TYPE *r, enum machine_mode mode)
2571 const struct real_format *fmt;
2573 fmt = REAL_MODE_FORMAT (mode);
2574 gcc_assert (fmt);
2576 return real_to_target_fmt (buf, r, fmt);
2579 /* Read R from the given target format. Read the words of the result
2580 in target word order in BUF. There are always 32 bits in each
2581 long, no matter the size of the host long. */
2583 void
2584 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
2585 const struct real_format *fmt)
2587 (*fmt->decode) (fmt, r, buf);
2590 /* Similar, but look up the format from MODE. */
2592 void
2593 real_from_target (REAL_VALUE_TYPE *r, const long *buf, enum machine_mode mode)
2595 const struct real_format *fmt;
2597 fmt = REAL_MODE_FORMAT (mode);
2598 gcc_assert (fmt);
2600 (*fmt->decode) (fmt, r, buf);
2603 /* Return the number of bits of the largest binary value that the
2604 significand of MODE will hold. */
2605 /* ??? Legacy. Should get access to real_format directly. */
2608 significand_size (enum machine_mode mode)
2610 const struct real_format *fmt;
2612 fmt = REAL_MODE_FORMAT (mode);
2613 if (fmt == NULL)
2614 return 0;
2616 if (fmt->b == 10)
2618 /* Return the size in bits of the largest binary value that can be
2619 held by the decimal coefficient for this mode. This is one more
2620 than the number of bits required to hold the largest coefficient
2621 of this mode. */
2622 double log2_10 = 3.3219281;
2623 return fmt->p * log2_10;
2625 return fmt->p;
2628 /* Return a hash value for the given real value. */
2629 /* ??? The "unsigned int" return value is intended to be hashval_t,
2630 but I didn't want to pull hashtab.h into real.h. */
2632 unsigned int
2633 real_hash (const REAL_VALUE_TYPE *r)
2635 unsigned int h;
2636 size_t i;
2638 h = r->cl | (r->sign << 2);
2639 switch (r->cl)
2641 case rvc_zero:
2642 case rvc_inf:
2643 return h;
2645 case rvc_normal:
2646 h |= REAL_EXP (r) << 3;
2647 break;
2649 case rvc_nan:
2650 if (r->signalling)
2651 h ^= (unsigned int)-1;
2652 if (r->canonical)
2653 return h;
2654 break;
2656 default:
2657 gcc_unreachable ();
2660 if (sizeof(unsigned long) > sizeof(unsigned int))
2661 for (i = 0; i < SIGSZ; ++i)
2663 unsigned long s = r->sig[i];
2664 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2666 else
2667 for (i = 0; i < SIGSZ; ++i)
2668 h ^= r->sig[i];
2670 return h;
2673 /* IEEE single-precision format. */
2675 static void encode_ieee_single (const struct real_format *fmt,
2676 long *, const REAL_VALUE_TYPE *);
2677 static void decode_ieee_single (const struct real_format *,
2678 REAL_VALUE_TYPE *, const long *);
2680 static void
2681 encode_ieee_single (const struct real_format *fmt, long *buf,
2682 const REAL_VALUE_TYPE *r)
2684 unsigned long image, sig, exp;
2685 unsigned long sign = r->sign;
2686 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2688 image = sign << 31;
2689 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2691 switch (r->cl)
2693 case rvc_zero:
2694 break;
2696 case rvc_inf:
2697 if (fmt->has_inf)
2698 image |= 255 << 23;
2699 else
2700 image |= 0x7fffffff;
2701 break;
2703 case rvc_nan:
2704 if (fmt->has_nans)
2706 if (r->canonical)
2707 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
2708 if (r->signalling == fmt->qnan_msb_set)
2709 sig &= ~(1 << 22);
2710 else
2711 sig |= 1 << 22;
2712 if (sig == 0)
2713 sig = 1 << 21;
2715 image |= 255 << 23;
2716 image |= sig;
2718 else
2719 image |= 0x7fffffff;
2720 break;
2722 case rvc_normal:
2723 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2724 whereas the intermediate representation is 0.F x 2**exp.
2725 Which means we're off by one. */
2726 if (denormal)
2727 exp = 0;
2728 else
2729 exp = REAL_EXP (r) + 127 - 1;
2730 image |= exp << 23;
2731 image |= sig;
2732 break;
2734 default:
2735 gcc_unreachable ();
2738 buf[0] = image;
2741 static void
2742 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2743 const long *buf)
2745 unsigned long image = buf[0] & 0xffffffff;
2746 bool sign = (image >> 31) & 1;
2747 int exp = (image >> 23) & 0xff;
2749 memset (r, 0, sizeof (*r));
2750 image <<= HOST_BITS_PER_LONG - 24;
2751 image &= ~SIG_MSB;
2753 if (exp == 0)
2755 if (image && fmt->has_denorm)
2757 r->cl = rvc_normal;
2758 r->sign = sign;
2759 SET_REAL_EXP (r, -126);
2760 r->sig[SIGSZ-1] = image << 1;
2761 normalize (r);
2763 else if (fmt->has_signed_zero)
2764 r->sign = sign;
2766 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2768 if (image)
2770 r->cl = rvc_nan;
2771 r->sign = sign;
2772 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
2773 ^ fmt->qnan_msb_set);
2774 r->sig[SIGSZ-1] = image;
2776 else
2778 r->cl = rvc_inf;
2779 r->sign = sign;
2782 else
2784 r->cl = rvc_normal;
2785 r->sign = sign;
2786 SET_REAL_EXP (r, exp - 127 + 1);
2787 r->sig[SIGSZ-1] = image | SIG_MSB;
2791 const struct real_format ieee_single_format =
2793 encode_ieee_single,
2794 decode_ieee_single,
2798 -125,
2799 128,
2802 true,
2803 true,
2804 true,
2805 true,
2806 true,
2807 false
2810 const struct real_format mips_single_format =
2812 encode_ieee_single,
2813 decode_ieee_single,
2817 -125,
2818 128,
2821 true,
2822 true,
2823 true,
2824 true,
2825 false,
2826 true
2829 const struct real_format motorola_single_format =
2831 encode_ieee_single,
2832 decode_ieee_single,
2836 -125,
2837 128,
2840 true,
2841 true,
2842 true,
2843 true,
2844 true,
2845 true
2848 /* IEEE double-precision format. */
2850 static void encode_ieee_double (const struct real_format *fmt,
2851 long *, const REAL_VALUE_TYPE *);
2852 static void decode_ieee_double (const struct real_format *,
2853 REAL_VALUE_TYPE *, const long *);
2855 static void
2856 encode_ieee_double (const struct real_format *fmt, long *buf,
2857 const REAL_VALUE_TYPE *r)
2859 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
2860 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2862 image_hi = r->sign << 31;
2863 image_lo = 0;
2865 if (HOST_BITS_PER_LONG == 64)
2867 sig_hi = r->sig[SIGSZ-1];
2868 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
2869 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
2871 else
2873 sig_hi = r->sig[SIGSZ-1];
2874 sig_lo = r->sig[SIGSZ-2];
2875 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
2876 sig_hi = (sig_hi >> 11) & 0xfffff;
2879 switch (r->cl)
2881 case rvc_zero:
2882 break;
2884 case rvc_inf:
2885 if (fmt->has_inf)
2886 image_hi |= 2047 << 20;
2887 else
2889 image_hi |= 0x7fffffff;
2890 image_lo = 0xffffffff;
2892 break;
2894 case rvc_nan:
2895 if (fmt->has_nans)
2897 if (r->canonical)
2899 if (fmt->canonical_nan_lsbs_set)
2901 sig_hi = (1 << 19) - 1;
2902 sig_lo = 0xffffffff;
2904 else
2906 sig_hi = 0;
2907 sig_lo = 0;
2910 if (r->signalling == fmt->qnan_msb_set)
2911 sig_hi &= ~(1 << 19);
2912 else
2913 sig_hi |= 1 << 19;
2914 if (sig_hi == 0 && sig_lo == 0)
2915 sig_hi = 1 << 18;
2917 image_hi |= 2047 << 20;
2918 image_hi |= sig_hi;
2919 image_lo = sig_lo;
2921 else
2923 image_hi |= 0x7fffffff;
2924 image_lo = 0xffffffff;
2926 break;
2928 case rvc_normal:
2929 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2930 whereas the intermediate representation is 0.F x 2**exp.
2931 Which means we're off by one. */
2932 if (denormal)
2933 exp = 0;
2934 else
2935 exp = REAL_EXP (r) + 1023 - 1;
2936 image_hi |= exp << 20;
2937 image_hi |= sig_hi;
2938 image_lo = sig_lo;
2939 break;
2941 default:
2942 gcc_unreachable ();
2945 if (FLOAT_WORDS_BIG_ENDIAN)
2946 buf[0] = image_hi, buf[1] = image_lo;
2947 else
2948 buf[0] = image_lo, buf[1] = image_hi;
2951 static void
2952 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2953 const long *buf)
2955 unsigned long image_hi, image_lo;
2956 bool sign;
2957 int exp;
2959 if (FLOAT_WORDS_BIG_ENDIAN)
2960 image_hi = buf[0], image_lo = buf[1];
2961 else
2962 image_lo = buf[0], image_hi = buf[1];
2963 image_lo &= 0xffffffff;
2964 image_hi &= 0xffffffff;
2966 sign = (image_hi >> 31) & 1;
2967 exp = (image_hi >> 20) & 0x7ff;
2969 memset (r, 0, sizeof (*r));
2971 image_hi <<= 32 - 21;
2972 image_hi |= image_lo >> 21;
2973 image_hi &= 0x7fffffff;
2974 image_lo <<= 32 - 21;
2976 if (exp == 0)
2978 if ((image_hi || image_lo) && fmt->has_denorm)
2980 r->cl = rvc_normal;
2981 r->sign = sign;
2982 SET_REAL_EXP (r, -1022);
2983 if (HOST_BITS_PER_LONG == 32)
2985 image_hi = (image_hi << 1) | (image_lo >> 31);
2986 image_lo <<= 1;
2987 r->sig[SIGSZ-1] = image_hi;
2988 r->sig[SIGSZ-2] = image_lo;
2990 else
2992 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
2993 r->sig[SIGSZ-1] = image_hi;
2995 normalize (r);
2997 else if (fmt->has_signed_zero)
2998 r->sign = sign;
3000 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
3002 if (image_hi || image_lo)
3004 r->cl = rvc_nan;
3005 r->sign = sign;
3006 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3007 if (HOST_BITS_PER_LONG == 32)
3009 r->sig[SIGSZ-1] = image_hi;
3010 r->sig[SIGSZ-2] = image_lo;
3012 else
3013 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3015 else
3017 r->cl = rvc_inf;
3018 r->sign = sign;
3021 else
3023 r->cl = rvc_normal;
3024 r->sign = sign;
3025 SET_REAL_EXP (r, exp - 1023 + 1);
3026 if (HOST_BITS_PER_LONG == 32)
3028 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3029 r->sig[SIGSZ-2] = image_lo;
3031 else
3032 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3036 const struct real_format ieee_double_format =
3038 encode_ieee_double,
3039 decode_ieee_double,
3043 -1021,
3044 1024,
3047 true,
3048 true,
3049 true,
3050 true,
3051 true,
3052 false
3055 const struct real_format mips_double_format =
3057 encode_ieee_double,
3058 decode_ieee_double,
3062 -1021,
3063 1024,
3066 true,
3067 true,
3068 true,
3069 true,
3070 false,
3071 true
3074 const struct real_format motorola_double_format =
3076 encode_ieee_double,
3077 decode_ieee_double,
3081 -1021,
3082 1024,
3085 true,
3086 true,
3087 true,
3088 true,
3089 true,
3090 true
3093 /* IEEE extended real format. This comes in three flavors: Intel's as
3094 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3095 12- and 16-byte images may be big- or little endian; Motorola's is
3096 always big endian. */
3098 /* Helper subroutine which converts from the internal format to the
3099 12-byte little-endian Intel format. Functions below adjust this
3100 for the other possible formats. */
3101 static void
3102 encode_ieee_extended (const struct real_format *fmt, long *buf,
3103 const REAL_VALUE_TYPE *r)
3105 unsigned long image_hi, sig_hi, sig_lo;
3106 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3108 image_hi = r->sign << 15;
3109 sig_hi = sig_lo = 0;
3111 switch (r->cl)
3113 case rvc_zero:
3114 break;
3116 case rvc_inf:
3117 if (fmt->has_inf)
3119 image_hi |= 32767;
3121 /* Intel requires the explicit integer bit to be set, otherwise
3122 it considers the value a "pseudo-infinity". Motorola docs
3123 say it doesn't care. */
3124 sig_hi = 0x80000000;
3126 else
3128 image_hi |= 32767;
3129 sig_lo = sig_hi = 0xffffffff;
3131 break;
3133 case rvc_nan:
3134 if (fmt->has_nans)
3136 image_hi |= 32767;
3137 if (r->canonical)
3139 if (fmt->canonical_nan_lsbs_set)
3141 sig_hi = (1 << 30) - 1;
3142 sig_lo = 0xffffffff;
3145 else if (HOST_BITS_PER_LONG == 32)
3147 sig_hi = r->sig[SIGSZ-1];
3148 sig_lo = r->sig[SIGSZ-2];
3150 else
3152 sig_lo = r->sig[SIGSZ-1];
3153 sig_hi = sig_lo >> 31 >> 1;
3154 sig_lo &= 0xffffffff;
3156 if (r->signalling == fmt->qnan_msb_set)
3157 sig_hi &= ~(1 << 30);
3158 else
3159 sig_hi |= 1 << 30;
3160 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3161 sig_hi = 1 << 29;
3163 /* Intel requires the explicit integer bit to be set, otherwise
3164 it considers the value a "pseudo-nan". Motorola docs say it
3165 doesn't care. */
3166 sig_hi |= 0x80000000;
3168 else
3170 image_hi |= 32767;
3171 sig_lo = sig_hi = 0xffffffff;
3173 break;
3175 case rvc_normal:
3177 int exp = REAL_EXP (r);
3179 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3180 whereas the intermediate representation is 0.F x 2**exp.
3181 Which means we're off by one.
3183 Except for Motorola, which consider exp=0 and explicit
3184 integer bit set to continue to be normalized. In theory
3185 this discrepancy has been taken care of by the difference
3186 in fmt->emin in round_for_format. */
3188 if (denormal)
3189 exp = 0;
3190 else
3192 exp += 16383 - 1;
3193 gcc_assert (exp >= 0);
3195 image_hi |= exp;
3197 if (HOST_BITS_PER_LONG == 32)
3199 sig_hi = r->sig[SIGSZ-1];
3200 sig_lo = r->sig[SIGSZ-2];
3202 else
3204 sig_lo = r->sig[SIGSZ-1];
3205 sig_hi = sig_lo >> 31 >> 1;
3206 sig_lo &= 0xffffffff;
3209 break;
3211 default:
3212 gcc_unreachable ();
3215 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3218 /* Convert from the internal format to the 12-byte Motorola format
3219 for an IEEE extended real. */
3220 static void
3221 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3222 const REAL_VALUE_TYPE *r)
3224 long intermed[3];
3225 encode_ieee_extended (fmt, intermed, r);
3227 /* Motorola chips are assumed always to be big-endian. Also, the
3228 padding in a Motorola extended real goes between the exponent and
3229 the mantissa. At this point the mantissa is entirely within
3230 elements 0 and 1 of intermed, and the exponent entirely within
3231 element 2, so all we have to do is swap the order around, and
3232 shift element 2 left 16 bits. */
3233 buf[0] = intermed[2] << 16;
3234 buf[1] = intermed[1];
3235 buf[2] = intermed[0];
3238 /* Convert from the internal format to the 12-byte Intel format for
3239 an IEEE extended real. */
3240 static void
3241 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3242 const REAL_VALUE_TYPE *r)
3244 if (FLOAT_WORDS_BIG_ENDIAN)
3246 /* All the padding in an Intel-format extended real goes at the high
3247 end, which in this case is after the mantissa, not the exponent.
3248 Therefore we must shift everything down 16 bits. */
3249 long intermed[3];
3250 encode_ieee_extended (fmt, intermed, r);
3251 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3252 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3253 buf[2] = (intermed[0] << 16);
3255 else
3256 /* encode_ieee_extended produces what we want directly. */
3257 encode_ieee_extended (fmt, buf, r);
3260 /* Convert from the internal format to the 16-byte Intel format for
3261 an IEEE extended real. */
3262 static void
3263 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3264 const REAL_VALUE_TYPE *r)
3266 /* All the padding in an Intel-format extended real goes at the high end. */
3267 encode_ieee_extended_intel_96 (fmt, buf, r);
3268 buf[3] = 0;
3271 /* As above, we have a helper function which converts from 12-byte
3272 little-endian Intel format to internal format. Functions below
3273 adjust for the other possible formats. */
3274 static void
3275 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3276 const long *buf)
3278 unsigned long image_hi, sig_hi, sig_lo;
3279 bool sign;
3280 int exp;
3282 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3283 sig_lo &= 0xffffffff;
3284 sig_hi &= 0xffffffff;
3285 image_hi &= 0xffffffff;
3287 sign = (image_hi >> 15) & 1;
3288 exp = image_hi & 0x7fff;
3290 memset (r, 0, sizeof (*r));
3292 if (exp == 0)
3294 if ((sig_hi || sig_lo) && fmt->has_denorm)
3296 r->cl = rvc_normal;
3297 r->sign = sign;
3299 /* When the IEEE format contains a hidden bit, we know that
3300 it's zero at this point, and so shift up the significand
3301 and decrease the exponent to match. In this case, Motorola
3302 defines the explicit integer bit to be valid, so we don't
3303 know whether the msb is set or not. */
3304 SET_REAL_EXP (r, fmt->emin);
3305 if (HOST_BITS_PER_LONG == 32)
3307 r->sig[SIGSZ-1] = sig_hi;
3308 r->sig[SIGSZ-2] = sig_lo;
3310 else
3311 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3313 normalize (r);
3315 else if (fmt->has_signed_zero)
3316 r->sign = sign;
3318 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3320 /* See above re "pseudo-infinities" and "pseudo-nans".
3321 Short summary is that the MSB will likely always be
3322 set, and that we don't care about it. */
3323 sig_hi &= 0x7fffffff;
3325 if (sig_hi || sig_lo)
3327 r->cl = rvc_nan;
3328 r->sign = sign;
3329 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3330 if (HOST_BITS_PER_LONG == 32)
3332 r->sig[SIGSZ-1] = sig_hi;
3333 r->sig[SIGSZ-2] = sig_lo;
3335 else
3336 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3338 else
3340 r->cl = rvc_inf;
3341 r->sign = sign;
3344 else
3346 r->cl = rvc_normal;
3347 r->sign = sign;
3348 SET_REAL_EXP (r, exp - 16383 + 1);
3349 if (HOST_BITS_PER_LONG == 32)
3351 r->sig[SIGSZ-1] = sig_hi;
3352 r->sig[SIGSZ-2] = sig_lo;
3354 else
3355 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3359 /* Convert from the internal format to the 12-byte Motorola format
3360 for an IEEE extended real. */
3361 static void
3362 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3363 const long *buf)
3365 long intermed[3];
3367 /* Motorola chips are assumed always to be big-endian. Also, the
3368 padding in a Motorola extended real goes between the exponent and
3369 the mantissa; remove it. */
3370 intermed[0] = buf[2];
3371 intermed[1] = buf[1];
3372 intermed[2] = (unsigned long)buf[0] >> 16;
3374 decode_ieee_extended (fmt, r, intermed);
3377 /* Convert from the internal format to the 12-byte Intel format for
3378 an IEEE extended real. */
3379 static void
3380 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3381 const long *buf)
3383 if (FLOAT_WORDS_BIG_ENDIAN)
3385 /* All the padding in an Intel-format extended real goes at the high
3386 end, which in this case is after the mantissa, not the exponent.
3387 Therefore we must shift everything up 16 bits. */
3388 long intermed[3];
3390 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3391 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3392 intermed[2] = ((unsigned long)buf[0] >> 16);
3394 decode_ieee_extended (fmt, r, intermed);
3396 else
3397 /* decode_ieee_extended produces what we want directly. */
3398 decode_ieee_extended (fmt, r, buf);
3401 /* Convert from the internal format to the 16-byte Intel format for
3402 an IEEE extended real. */
3403 static void
3404 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3405 const long *buf)
3407 /* All the padding in an Intel-format extended real goes at the high end. */
3408 decode_ieee_extended_intel_96 (fmt, r, buf);
3411 const struct real_format ieee_extended_motorola_format =
3413 encode_ieee_extended_motorola,
3414 decode_ieee_extended_motorola,
3418 -16382,
3419 16384,
3422 true,
3423 true,
3424 true,
3425 true,
3426 true,
3427 true
3430 const struct real_format ieee_extended_intel_96_format =
3432 encode_ieee_extended_intel_96,
3433 decode_ieee_extended_intel_96,
3437 -16381,
3438 16384,
3441 true,
3442 true,
3443 true,
3444 true,
3445 true,
3446 false
3449 const struct real_format ieee_extended_intel_128_format =
3451 encode_ieee_extended_intel_128,
3452 decode_ieee_extended_intel_128,
3456 -16381,
3457 16384,
3460 true,
3461 true,
3462 true,
3463 true,
3464 true,
3465 false
3468 /* The following caters to i386 systems that set the rounding precision
3469 to 53 bits instead of 64, e.g. FreeBSD. */
3470 const struct real_format ieee_extended_intel_96_round_53_format =
3472 encode_ieee_extended_intel_96,
3473 decode_ieee_extended_intel_96,
3477 -16381,
3478 16384,
3481 true,
3482 true,
3483 true,
3484 true,
3485 true,
3486 false
3489 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3490 numbers whose sum is equal to the extended precision value. The number
3491 with greater magnitude is first. This format has the same magnitude
3492 range as an IEEE double precision value, but effectively 106 bits of
3493 significand precision. Infinity and NaN are represented by their IEEE
3494 double precision value stored in the first number, the second number is
3495 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3497 static void encode_ibm_extended (const struct real_format *fmt,
3498 long *, const REAL_VALUE_TYPE *);
3499 static void decode_ibm_extended (const struct real_format *,
3500 REAL_VALUE_TYPE *, const long *);
3502 static void
3503 encode_ibm_extended (const struct real_format *fmt, long *buf,
3504 const REAL_VALUE_TYPE *r)
3506 REAL_VALUE_TYPE u, normr, v;
3507 const struct real_format *base_fmt;
3509 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3511 /* Renormlize R before doing any arithmetic on it. */
3512 normr = *r;
3513 if (normr.cl == rvc_normal)
3514 normalize (&normr);
3516 /* u = IEEE double precision portion of significand. */
3517 u = normr;
3518 round_for_format (base_fmt, &u);
3519 encode_ieee_double (base_fmt, &buf[0], &u);
3521 if (u.cl == rvc_normal)
3523 do_add (&v, &normr, &u, 1);
3524 /* Call round_for_format since we might need to denormalize. */
3525 round_for_format (base_fmt, &v);
3526 encode_ieee_double (base_fmt, &buf[2], &v);
3528 else
3530 /* Inf, NaN, 0 are all representable as doubles, so the
3531 least-significant part can be 0.0. */
3532 buf[2] = 0;
3533 buf[3] = 0;
3537 static void
3538 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3539 const long *buf)
3541 REAL_VALUE_TYPE u, v;
3542 const struct real_format *base_fmt;
3544 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3545 decode_ieee_double (base_fmt, &u, &buf[0]);
3547 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3549 decode_ieee_double (base_fmt, &v, &buf[2]);
3550 do_add (r, &u, &v, 0);
3552 else
3553 *r = u;
3556 const struct real_format ibm_extended_format =
3558 encode_ibm_extended,
3559 decode_ibm_extended,
3561 53 + 53,
3563 -1021 + 53,
3564 1024,
3565 127,
3567 true,
3568 true,
3569 true,
3570 true,
3571 true,
3572 false
3575 const struct real_format mips_extended_format =
3577 encode_ibm_extended,
3578 decode_ibm_extended,
3580 53 + 53,
3582 -1021 + 53,
3583 1024,
3584 127,
3586 true,
3587 true,
3588 true,
3589 true,
3590 false,
3591 true
3595 /* IEEE quad precision format. */
3597 static void encode_ieee_quad (const struct real_format *fmt,
3598 long *, const REAL_VALUE_TYPE *);
3599 static void decode_ieee_quad (const struct real_format *,
3600 REAL_VALUE_TYPE *, const long *);
3602 static void
3603 encode_ieee_quad (const struct real_format *fmt, long *buf,
3604 const REAL_VALUE_TYPE *r)
3606 unsigned long image3, image2, image1, image0, exp;
3607 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3608 REAL_VALUE_TYPE u;
3610 image3 = r->sign << 31;
3611 image2 = 0;
3612 image1 = 0;
3613 image0 = 0;
3615 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3617 switch (r->cl)
3619 case rvc_zero:
3620 break;
3622 case rvc_inf:
3623 if (fmt->has_inf)
3624 image3 |= 32767 << 16;
3625 else
3627 image3 |= 0x7fffffff;
3628 image2 = 0xffffffff;
3629 image1 = 0xffffffff;
3630 image0 = 0xffffffff;
3632 break;
3634 case rvc_nan:
3635 if (fmt->has_nans)
3637 image3 |= 32767 << 16;
3639 if (r->canonical)
3641 if (fmt->canonical_nan_lsbs_set)
3643 image3 |= 0x7fff;
3644 image2 = image1 = image0 = 0xffffffff;
3647 else if (HOST_BITS_PER_LONG == 32)
3649 image0 = u.sig[0];
3650 image1 = u.sig[1];
3651 image2 = u.sig[2];
3652 image3 |= u.sig[3] & 0xffff;
3654 else
3656 image0 = u.sig[0];
3657 image1 = image0 >> 31 >> 1;
3658 image2 = u.sig[1];
3659 image3 |= (image2 >> 31 >> 1) & 0xffff;
3660 image0 &= 0xffffffff;
3661 image2 &= 0xffffffff;
3663 if (r->signalling == fmt->qnan_msb_set)
3664 image3 &= ~0x8000;
3665 else
3666 image3 |= 0x8000;
3667 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3668 image3 |= 0x4000;
3670 else
3672 image3 |= 0x7fffffff;
3673 image2 = 0xffffffff;
3674 image1 = 0xffffffff;
3675 image0 = 0xffffffff;
3677 break;
3679 case rvc_normal:
3680 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3681 whereas the intermediate representation is 0.F x 2**exp.
3682 Which means we're off by one. */
3683 if (denormal)
3684 exp = 0;
3685 else
3686 exp = REAL_EXP (r) + 16383 - 1;
3687 image3 |= exp << 16;
3689 if (HOST_BITS_PER_LONG == 32)
3691 image0 = u.sig[0];
3692 image1 = u.sig[1];
3693 image2 = u.sig[2];
3694 image3 |= u.sig[3] & 0xffff;
3696 else
3698 image0 = u.sig[0];
3699 image1 = image0 >> 31 >> 1;
3700 image2 = u.sig[1];
3701 image3 |= (image2 >> 31 >> 1) & 0xffff;
3702 image0 &= 0xffffffff;
3703 image2 &= 0xffffffff;
3705 break;
3707 default:
3708 gcc_unreachable ();
3711 if (FLOAT_WORDS_BIG_ENDIAN)
3713 buf[0] = image3;
3714 buf[1] = image2;
3715 buf[2] = image1;
3716 buf[3] = image0;
3718 else
3720 buf[0] = image0;
3721 buf[1] = image1;
3722 buf[2] = image2;
3723 buf[3] = image3;
3727 static void
3728 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3729 const long *buf)
3731 unsigned long image3, image2, image1, image0;
3732 bool sign;
3733 int exp;
3735 if (FLOAT_WORDS_BIG_ENDIAN)
3737 image3 = buf[0];
3738 image2 = buf[1];
3739 image1 = buf[2];
3740 image0 = buf[3];
3742 else
3744 image0 = buf[0];
3745 image1 = buf[1];
3746 image2 = buf[2];
3747 image3 = buf[3];
3749 image0 &= 0xffffffff;
3750 image1 &= 0xffffffff;
3751 image2 &= 0xffffffff;
3753 sign = (image3 >> 31) & 1;
3754 exp = (image3 >> 16) & 0x7fff;
3755 image3 &= 0xffff;
3757 memset (r, 0, sizeof (*r));
3759 if (exp == 0)
3761 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3763 r->cl = rvc_normal;
3764 r->sign = sign;
3766 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
3767 if (HOST_BITS_PER_LONG == 32)
3769 r->sig[0] = image0;
3770 r->sig[1] = image1;
3771 r->sig[2] = image2;
3772 r->sig[3] = image3;
3774 else
3776 r->sig[0] = (image1 << 31 << 1) | image0;
3777 r->sig[1] = (image3 << 31 << 1) | image2;
3780 normalize (r);
3782 else if (fmt->has_signed_zero)
3783 r->sign = sign;
3785 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3787 if (image3 | image2 | image1 | image0)
3789 r->cl = rvc_nan;
3790 r->sign = sign;
3791 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
3793 if (HOST_BITS_PER_LONG == 32)
3795 r->sig[0] = image0;
3796 r->sig[1] = image1;
3797 r->sig[2] = image2;
3798 r->sig[3] = image3;
3800 else
3802 r->sig[0] = (image1 << 31 << 1) | image0;
3803 r->sig[1] = (image3 << 31 << 1) | image2;
3805 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3807 else
3809 r->cl = rvc_inf;
3810 r->sign = sign;
3813 else
3815 r->cl = rvc_normal;
3816 r->sign = sign;
3817 SET_REAL_EXP (r, exp - 16383 + 1);
3819 if (HOST_BITS_PER_LONG == 32)
3821 r->sig[0] = image0;
3822 r->sig[1] = image1;
3823 r->sig[2] = image2;
3824 r->sig[3] = image3;
3826 else
3828 r->sig[0] = (image1 << 31 << 1) | image0;
3829 r->sig[1] = (image3 << 31 << 1) | image2;
3831 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3832 r->sig[SIGSZ-1] |= SIG_MSB;
3836 const struct real_format ieee_quad_format =
3838 encode_ieee_quad,
3839 decode_ieee_quad,
3841 113,
3842 113,
3843 -16381,
3844 16384,
3845 127,
3846 127,
3847 true,
3848 true,
3849 true,
3850 true,
3851 true,
3852 false
3855 const struct real_format mips_quad_format =
3857 encode_ieee_quad,
3858 decode_ieee_quad,
3860 113,
3861 113,
3862 -16381,
3863 16384,
3864 127,
3865 127,
3866 true,
3867 true,
3868 true,
3869 true,
3870 false,
3871 true
3874 /* Descriptions of VAX floating point formats can be found beginning at
3876 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
3878 The thing to remember is that they're almost IEEE, except for word
3879 order, exponent bias, and the lack of infinities, nans, and denormals.
3881 We don't implement the H_floating format here, simply because neither
3882 the VAX or Alpha ports use it. */
3884 static void encode_vax_f (const struct real_format *fmt,
3885 long *, const REAL_VALUE_TYPE *);
3886 static void decode_vax_f (const struct real_format *,
3887 REAL_VALUE_TYPE *, const long *);
3888 static void encode_vax_d (const struct real_format *fmt,
3889 long *, const REAL_VALUE_TYPE *);
3890 static void decode_vax_d (const struct real_format *,
3891 REAL_VALUE_TYPE *, const long *);
3892 static void encode_vax_g (const struct real_format *fmt,
3893 long *, const REAL_VALUE_TYPE *);
3894 static void decode_vax_g (const struct real_format *,
3895 REAL_VALUE_TYPE *, const long *);
3897 static void
3898 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
3899 const REAL_VALUE_TYPE *r)
3901 unsigned long sign, exp, sig, image;
3903 sign = r->sign << 15;
3905 switch (r->cl)
3907 case rvc_zero:
3908 image = 0;
3909 break;
3911 case rvc_inf:
3912 case rvc_nan:
3913 image = 0xffff7fff | sign;
3914 break;
3916 case rvc_normal:
3917 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3918 exp = REAL_EXP (r) + 128;
3920 image = (sig << 16) & 0xffff0000;
3921 image |= sign;
3922 image |= exp << 7;
3923 image |= sig >> 16;
3924 break;
3926 default:
3927 gcc_unreachable ();
3930 buf[0] = image;
3933 static void
3934 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
3935 REAL_VALUE_TYPE *r, const long *buf)
3937 unsigned long image = buf[0] & 0xffffffff;
3938 int exp = (image >> 7) & 0xff;
3940 memset (r, 0, sizeof (*r));
3942 if (exp != 0)
3944 r->cl = rvc_normal;
3945 r->sign = (image >> 15) & 1;
3946 SET_REAL_EXP (r, exp - 128);
3948 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
3949 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3953 static void
3954 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
3955 const REAL_VALUE_TYPE *r)
3957 unsigned long image0, image1, sign = r->sign << 15;
3959 switch (r->cl)
3961 case rvc_zero:
3962 image0 = image1 = 0;
3963 break;
3965 case rvc_inf:
3966 case rvc_nan:
3967 image0 = 0xffff7fff | sign;
3968 image1 = 0xffffffff;
3969 break;
3971 case rvc_normal:
3972 /* Extract the significand into straight hi:lo. */
3973 if (HOST_BITS_PER_LONG == 64)
3975 image0 = r->sig[SIGSZ-1];
3976 image1 = (image0 >> (64 - 56)) & 0xffffffff;
3977 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
3979 else
3981 image0 = r->sig[SIGSZ-1];
3982 image1 = r->sig[SIGSZ-2];
3983 image1 = (image0 << 24) | (image1 >> 8);
3984 image0 = (image0 >> 8) & 0xffffff;
3987 /* Rearrange the half-words of the significand to match the
3988 external format. */
3989 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
3990 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3992 /* Add the sign and exponent. */
3993 image0 |= sign;
3994 image0 |= (REAL_EXP (r) + 128) << 7;
3995 break;
3997 default:
3998 gcc_unreachable ();
4001 if (FLOAT_WORDS_BIG_ENDIAN)
4002 buf[0] = image1, buf[1] = image0;
4003 else
4004 buf[0] = image0, buf[1] = image1;
4007 static void
4008 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
4009 REAL_VALUE_TYPE *r, const long *buf)
4011 unsigned long image0, image1;
4012 int exp;
4014 if (FLOAT_WORDS_BIG_ENDIAN)
4015 image1 = buf[0], image0 = buf[1];
4016 else
4017 image0 = buf[0], image1 = buf[1];
4018 image0 &= 0xffffffff;
4019 image1 &= 0xffffffff;
4021 exp = (image0 >> 7) & 0xff;
4023 memset (r, 0, sizeof (*r));
4025 if (exp != 0)
4027 r->cl = rvc_normal;
4028 r->sign = (image0 >> 15) & 1;
4029 SET_REAL_EXP (r, exp - 128);
4031 /* Rearrange the half-words of the external format into
4032 proper ascending order. */
4033 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4034 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4036 if (HOST_BITS_PER_LONG == 64)
4038 image0 = (image0 << 31 << 1) | image1;
4039 image0 <<= 64 - 56;
4040 image0 |= SIG_MSB;
4041 r->sig[SIGSZ-1] = image0;
4043 else
4045 r->sig[SIGSZ-1] = image0;
4046 r->sig[SIGSZ-2] = image1;
4047 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4048 r->sig[SIGSZ-1] |= SIG_MSB;
4053 static void
4054 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4055 const REAL_VALUE_TYPE *r)
4057 unsigned long image0, image1, sign = r->sign << 15;
4059 switch (r->cl)
4061 case rvc_zero:
4062 image0 = image1 = 0;
4063 break;
4065 case rvc_inf:
4066 case rvc_nan:
4067 image0 = 0xffff7fff | sign;
4068 image1 = 0xffffffff;
4069 break;
4071 case rvc_normal:
4072 /* Extract the significand into straight hi:lo. */
4073 if (HOST_BITS_PER_LONG == 64)
4075 image0 = r->sig[SIGSZ-1];
4076 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4077 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4079 else
4081 image0 = r->sig[SIGSZ-1];
4082 image1 = r->sig[SIGSZ-2];
4083 image1 = (image0 << 21) | (image1 >> 11);
4084 image0 = (image0 >> 11) & 0xfffff;
4087 /* Rearrange the half-words of the significand to match the
4088 external format. */
4089 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4090 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4092 /* Add the sign and exponent. */
4093 image0 |= sign;
4094 image0 |= (REAL_EXP (r) + 1024) << 4;
4095 break;
4097 default:
4098 gcc_unreachable ();
4101 if (FLOAT_WORDS_BIG_ENDIAN)
4102 buf[0] = image1, buf[1] = image0;
4103 else
4104 buf[0] = image0, buf[1] = image1;
4107 static void
4108 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4109 REAL_VALUE_TYPE *r, const long *buf)
4111 unsigned long image0, image1;
4112 int exp;
4114 if (FLOAT_WORDS_BIG_ENDIAN)
4115 image1 = buf[0], image0 = buf[1];
4116 else
4117 image0 = buf[0], image1 = buf[1];
4118 image0 &= 0xffffffff;
4119 image1 &= 0xffffffff;
4121 exp = (image0 >> 4) & 0x7ff;
4123 memset (r, 0, sizeof (*r));
4125 if (exp != 0)
4127 r->cl = rvc_normal;
4128 r->sign = (image0 >> 15) & 1;
4129 SET_REAL_EXP (r, exp - 1024);
4131 /* Rearrange the half-words of the external format into
4132 proper ascending order. */
4133 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4134 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4136 if (HOST_BITS_PER_LONG == 64)
4138 image0 = (image0 << 31 << 1) | image1;
4139 image0 <<= 64 - 53;
4140 image0 |= SIG_MSB;
4141 r->sig[SIGSZ-1] = image0;
4143 else
4145 r->sig[SIGSZ-1] = image0;
4146 r->sig[SIGSZ-2] = image1;
4147 lshift_significand (r, r, 64 - 53);
4148 r->sig[SIGSZ-1] |= SIG_MSB;
4153 const struct real_format vax_f_format =
4155 encode_vax_f,
4156 decode_vax_f,
4160 -127,
4161 127,
4164 false,
4165 false,
4166 false,
4167 false,
4168 false,
4169 false
4172 const struct real_format vax_d_format =
4174 encode_vax_d,
4175 decode_vax_d,
4179 -127,
4180 127,
4183 false,
4184 false,
4185 false,
4186 false,
4187 false,
4188 false
4191 const struct real_format vax_g_format =
4193 encode_vax_g,
4194 decode_vax_g,
4198 -1023,
4199 1023,
4202 false,
4203 false,
4204 false,
4205 false,
4206 false,
4207 false
4210 /* Encode real R into a single precision DFP value in BUF. */
4211 static void
4212 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4213 long *buf ATTRIBUTE_UNUSED,
4214 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4216 encode_decimal32 (fmt, buf, r);
4219 /* Decode a single precision DFP value in BUF into a real R. */
4220 static void
4221 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4222 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4223 const long *buf ATTRIBUTE_UNUSED)
4225 decode_decimal32 (fmt, r, buf);
4228 /* Encode real R into a double precision DFP value in BUF. */
4229 static void
4230 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4231 long *buf ATTRIBUTE_UNUSED,
4232 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4234 encode_decimal64 (fmt, buf, r);
4237 /* Decode a double precision DFP value in BUF into a real R. */
4238 static void
4239 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4240 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4241 const long *buf ATTRIBUTE_UNUSED)
4243 decode_decimal64 (fmt, r, buf);
4246 /* Encode real R into a quad precision DFP value in BUF. */
4247 static void
4248 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4249 long *buf ATTRIBUTE_UNUSED,
4250 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4252 encode_decimal128 (fmt, buf, r);
4255 /* Decode a quad precision DFP value in BUF into a real R. */
4256 static void
4257 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4258 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4259 const long *buf ATTRIBUTE_UNUSED)
4261 decode_decimal128 (fmt, r, buf);
4264 /* Single precision decimal floating point (IEEE 754R). */
4265 const struct real_format decimal_single_format =
4267 encode_decimal_single,
4268 decode_decimal_single,
4269 10,
4272 -95,
4276 true,
4277 true,
4278 true,
4279 true,
4280 true,
4281 false
4284 /* Double precision decimal floating point (IEEE 754R). */
4285 const struct real_format decimal_double_format =
4287 encode_decimal_double,
4288 decode_decimal_double,
4292 -383,
4293 384,
4296 true,
4297 true,
4298 true,
4299 true,
4300 true,
4301 false
4304 /* Quad precision decimal floating point (IEEE 754R). */
4305 const struct real_format decimal_quad_format =
4307 encode_decimal_quad,
4308 decode_decimal_quad,
4312 -6143,
4313 6144,
4314 127,
4315 127,
4316 true,
4317 true,
4318 true,
4319 true,
4320 true,
4321 false
4324 /* A synthetic "format" for internal arithmetic. It's the size of the
4325 internal significand minus the two bits needed for proper rounding.
4326 The encode and decode routines exist only to satisfy our paranoia
4327 harness. */
4329 static void encode_internal (const struct real_format *fmt,
4330 long *, const REAL_VALUE_TYPE *);
4331 static void decode_internal (const struct real_format *,
4332 REAL_VALUE_TYPE *, const long *);
4334 static void
4335 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4336 const REAL_VALUE_TYPE *r)
4338 memcpy (buf, r, sizeof (*r));
4341 static void
4342 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
4343 REAL_VALUE_TYPE *r, const long *buf)
4345 memcpy (r, buf, sizeof (*r));
4348 const struct real_format real_internal_format =
4350 encode_internal,
4351 decode_internal,
4353 SIGNIFICAND_BITS - 2,
4354 SIGNIFICAND_BITS - 2,
4355 -MAX_EXP,
4356 MAX_EXP,
4359 true,
4360 true,
4361 false,
4362 true,
4363 true,
4364 false
4367 /* Calculate the square root of X in mode MODE, and store the result
4368 in R. Return TRUE if the operation does not raise an exception.
4369 For details see "High Precision Division and Square Root",
4370 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4371 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4373 bool
4374 real_sqrt (REAL_VALUE_TYPE *r, enum machine_mode mode,
4375 const REAL_VALUE_TYPE *x)
4377 static REAL_VALUE_TYPE halfthree;
4378 static bool init = false;
4379 REAL_VALUE_TYPE h, t, i;
4380 int iter, exp;
4382 /* sqrt(-0.0) is -0.0. */
4383 if (real_isnegzero (x))
4385 *r = *x;
4386 return false;
4389 /* Negative arguments return NaN. */
4390 if (real_isneg (x))
4392 get_canonical_qnan (r, 0);
4393 return false;
4396 /* Infinity and NaN return themselves. */
4397 if (!real_isfinite (x))
4399 *r = *x;
4400 return false;
4403 if (!init)
4405 do_add (&halfthree, &dconst1, &dconsthalf, 0);
4406 init = true;
4409 /* Initial guess for reciprocal sqrt, i. */
4410 exp = real_exponent (x);
4411 real_ldexp (&i, &dconst1, -exp/2);
4413 /* Newton's iteration for reciprocal sqrt, i. */
4414 for (iter = 0; iter < 16; iter++)
4416 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4417 do_multiply (&t, x, &i);
4418 do_multiply (&h, &t, &i);
4419 do_multiply (&t, &h, &dconsthalf);
4420 do_add (&h, &halfthree, &t, 1);
4421 do_multiply (&t, &i, &h);
4423 /* Check for early convergence. */
4424 if (iter >= 6 && real_identical (&i, &t))
4425 break;
4427 /* ??? Unroll loop to avoid copying. */
4428 i = t;
4431 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4432 do_multiply (&t, x, &i);
4433 do_multiply (&h, &t, &i);
4434 do_add (&i, &dconst1, &h, 1);
4435 do_multiply (&h, &t, &i);
4436 do_multiply (&i, &dconsthalf, &h);
4437 do_add (&h, &t, &i, 0);
4439 /* ??? We need a Tuckerman test to get the last bit. */
4441 real_convert (r, mode, &h);
4442 return true;
4445 /* Calculate X raised to the integer exponent N in mode MODE and store
4446 the result in R. Return true if the result may be inexact due to
4447 loss of precision. The algorithm is the classic "left-to-right binary
4448 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4449 Algorithms", "The Art of Computer Programming", Volume 2. */
4451 bool
4452 real_powi (REAL_VALUE_TYPE *r, enum machine_mode mode,
4453 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4455 unsigned HOST_WIDE_INT bit;
4456 REAL_VALUE_TYPE t;
4457 bool inexact = false;
4458 bool init = false;
4459 bool neg;
4460 int i;
4462 if (n == 0)
4464 *r = dconst1;
4465 return false;
4467 else if (n < 0)
4469 /* Don't worry about overflow, from now on n is unsigned. */
4470 neg = true;
4471 n = -n;
4473 else
4474 neg = false;
4476 t = *x;
4477 bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
4478 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4480 if (init)
4482 inexact |= do_multiply (&t, &t, &t);
4483 if (n & bit)
4484 inexact |= do_multiply (&t, &t, x);
4486 else if (n & bit)
4487 init = true;
4488 bit >>= 1;
4491 if (neg)
4492 inexact |= do_divide (&t, &dconst1, &t);
4494 real_convert (r, mode, &t);
4495 return inexact;
4498 /* Round X to the nearest integer not larger in absolute value, i.e.
4499 towards zero, placing the result in R in mode MODE. */
4501 void
4502 real_trunc (REAL_VALUE_TYPE *r, enum machine_mode mode,
4503 const REAL_VALUE_TYPE *x)
4505 do_fix_trunc (r, x);
4506 if (mode != VOIDmode)
4507 real_convert (r, mode, r);
4510 /* Round X to the largest integer not greater in value, i.e. round
4511 down, placing the result in R in mode MODE. */
4513 void
4514 real_floor (REAL_VALUE_TYPE *r, enum machine_mode mode,
4515 const REAL_VALUE_TYPE *x)
4517 REAL_VALUE_TYPE t;
4519 do_fix_trunc (&t, x);
4520 if (! real_identical (&t, x) && x->sign)
4521 do_add (&t, &t, &dconstm1, 0);
4522 if (mode != VOIDmode)
4523 real_convert (r, mode, &t);
4524 else
4525 *r = t;
4528 /* Round X to the smallest integer not less then argument, i.e. round
4529 up, placing the result in R in mode MODE. */
4531 void
4532 real_ceil (REAL_VALUE_TYPE *r, enum machine_mode mode,
4533 const REAL_VALUE_TYPE *x)
4535 REAL_VALUE_TYPE t;
4537 do_fix_trunc (&t, x);
4538 if (! real_identical (&t, x) && ! x->sign)
4539 do_add (&t, &t, &dconst1, 0);
4540 if (mode != VOIDmode)
4541 real_convert (r, mode, &t);
4542 else
4543 *r = t;
4546 /* Round X to the nearest integer, but round halfway cases away from
4547 zero. */
4549 void
4550 real_round (REAL_VALUE_TYPE *r, enum machine_mode mode,
4551 const REAL_VALUE_TYPE *x)
4553 do_add (r, x, &dconsthalf, x->sign);
4554 do_fix_trunc (r, r);
4555 if (mode != VOIDmode)
4556 real_convert (r, mode, r);
4559 /* Set the sign of R to the sign of X. */
4561 void
4562 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4564 r->sign = x->sign;
4567 /* Convert from REAL_VALUE_TYPE to MPFR. The caller is responsible
4568 for initializing and clearing the MPFR parameter. */
4570 void
4571 mpfr_from_real (mpfr_ptr m, const REAL_VALUE_TYPE *r, mp_rnd_t rndmode)
4573 /* We use a string as an intermediate type. */
4574 char buf[128];
4575 int ret;
4577 /* Take care of Infinity and NaN. */
4578 if (r->cl == rvc_inf)
4580 mpfr_set_inf (m, r->sign == 1 ? -1 : 1);
4581 return;
4584 if (r->cl == rvc_nan)
4586 mpfr_set_nan (m);
4587 return;
4590 real_to_hexadecimal (buf, r, sizeof (buf), 0, 1);
4591 /* mpfr_set_str() parses hexadecimal floats from strings in the same
4592 format that GCC will output them. Nothing extra is needed. */
4593 ret = mpfr_set_str (m, buf, 16, rndmode);
4594 gcc_assert (ret == 0);
4597 /* Convert from MPFR to REAL_VALUE_TYPE, for a given type TYPE and rounding
4598 mode RNDMODE. TYPE is only relevant if M is a NaN. */
4600 void
4601 real_from_mpfr (REAL_VALUE_TYPE *r, mpfr_srcptr m, tree type, mp_rnd_t rndmode)
4603 /* We use a string as an intermediate type. */
4604 char buf[128], *rstr;
4605 mp_exp_t exp;
4607 /* Take care of Infinity and NaN. */
4608 if (mpfr_inf_p (m))
4610 real_inf (r);
4611 if (mpfr_sgn (m) < 0)
4612 *r = REAL_VALUE_NEGATE (*r);
4613 return;
4616 if (mpfr_nan_p (m))
4618 real_nan (r, "", 1, TYPE_MODE (type));
4619 return;
4622 rstr = mpfr_get_str (NULL, &exp, 16, 0, m, rndmode);
4624 /* The additional 12 chars add space for the sprintf below. This
4625 leaves 6 digits for the exponent which is supposedly enough. */
4626 gcc_assert (rstr != NULL && strlen (rstr) < sizeof (buf) - 12);
4628 /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp,
4629 mpfr_get_str returns the exponent for mantissa * 16**exp, adjust
4630 for that. */
4631 exp *= 4;
4633 if (rstr[0] == '-')
4634 sprintf (buf, "-0x.%sp%d", &rstr[1], (int) exp);
4635 else
4636 sprintf (buf, "0x.%sp%d", rstr, (int) exp);
4638 mpfr_free_str (rstr);
4640 real_from_string (r, buf);
4643 /* Check whether the real constant value given is an integer. */
4645 bool
4646 real_isinteger (const REAL_VALUE_TYPE *c, enum machine_mode mode)
4648 REAL_VALUE_TYPE cint;
4650 real_trunc (&cint, mode, c);
4651 return real_identical (c, &cint);
4654 /* Write into BUF the maximum representable finite floating-point
4655 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4656 float string. LEN is the size of BUF, and the buffer must be large
4657 enough to contain the resulting string. */
4659 void
4660 get_max_float (const struct real_format *fmt, char *buf, size_t len)
4662 int i, n;
4663 char *p;
4665 strcpy (buf, "0x0.");
4666 n = fmt->p;
4667 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
4668 *p++ = 'f';
4669 if (i < n)
4670 *p++ = "08ce"[n - i];
4671 sprintf (p, "p%d", fmt->emax);
4672 if (fmt->pnan < fmt->p)
4674 /* This is an IBM extended double format made up of two IEEE
4675 doubles. The value of the long double is the sum of the
4676 values of the two parts. The most significant part is
4677 required to be the value of the long double rounded to the
4678 nearest double. Rounding means we need a slightly smaller
4679 value for LDBL_MAX. */
4680 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
4683 gcc_assert (strlen (buf) < len);