extend jump thread for finite state automata
[official-gcc.git] / gcc / real.c
blob5e8050dbbb7d2c58043a1c7b6bc61b88e2a25fe5
1 /* real.c - software floating point emulation.
2 Copyright (C) 1993-2014 Free Software Foundation, Inc.
3 Contributed by Stephen L. Moshier (moshier@world.std.com).
4 Re-written by Richard Henderson <rth@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "diagnostic-core.h"
28 #include "real.h"
29 #include "realmpfr.h"
30 #include "tm_p.h"
31 #include "dfp.h"
32 #include "wide-int.h"
34 /* The floating point model used internally is not exactly IEEE 754
35 compliant, and close to the description in the ISO C99 standard,
36 section 5.2.4.2.2 Characteristics of floating types.
38 Specifically
40 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
42 where
43 s = sign (+- 1)
44 b = base or radix, here always 2
45 e = exponent
46 p = precision (the number of base-b digits in the significand)
47 f_k = the digits of the significand.
49 We differ from typical IEEE 754 encodings in that the entire
50 significand is fractional. Normalized significands are in the
51 range [0.5, 1.0).
53 A requirement of the model is that P be larger than the largest
54 supported target floating-point type by at least 2 bits. This gives
55 us proper rounding when we truncate to the target type. In addition,
56 E must be large enough to hold the smallest supported denormal number
57 in a normalized form.
59 Both of these requirements are easily satisfied. The largest target
60 significand is 113 bits; we store at least 160. The smallest
61 denormal number fits in 17 exponent bits; we store 26. */
64 /* Used to classify two numbers simultaneously. */
65 #define CLASS2(A, B) ((A) << 2 | (B))
67 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
68 #error "Some constant folding done by hand to avoid shift count warnings"
69 #endif
71 static void get_zero (REAL_VALUE_TYPE *, int);
72 static void get_canonical_qnan (REAL_VALUE_TYPE *, int);
73 static void get_canonical_snan (REAL_VALUE_TYPE *, int);
74 static void get_inf (REAL_VALUE_TYPE *, int);
75 static bool sticky_rshift_significand (REAL_VALUE_TYPE *,
76 const REAL_VALUE_TYPE *, unsigned int);
77 static void rshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
78 unsigned int);
79 static void lshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
80 unsigned int);
81 static void lshift_significand_1 (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
82 static bool add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *,
83 const REAL_VALUE_TYPE *);
84 static bool sub_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
85 const REAL_VALUE_TYPE *, int);
86 static void neg_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
87 static int cmp_significands (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
88 static int cmp_significand_0 (const REAL_VALUE_TYPE *);
89 static void set_significand_bit (REAL_VALUE_TYPE *, unsigned int);
90 static void clear_significand_bit (REAL_VALUE_TYPE *, unsigned int);
91 static bool test_significand_bit (REAL_VALUE_TYPE *, unsigned int);
92 static void clear_significand_below (REAL_VALUE_TYPE *, unsigned int);
93 static bool div_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
94 const REAL_VALUE_TYPE *);
95 static void normalize (REAL_VALUE_TYPE *);
97 static bool do_add (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
98 const REAL_VALUE_TYPE *, int);
99 static bool do_multiply (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
100 const REAL_VALUE_TYPE *);
101 static bool do_divide (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
102 const REAL_VALUE_TYPE *);
103 static int do_compare (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int);
104 static void do_fix_trunc (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
106 static unsigned long rtd_divmod (REAL_VALUE_TYPE *, REAL_VALUE_TYPE *);
107 static void decimal_from_integer (REAL_VALUE_TYPE *);
108 static void decimal_integer_string (char *, const REAL_VALUE_TYPE *,
109 size_t);
111 static const REAL_VALUE_TYPE * ten_to_ptwo (int);
112 static const REAL_VALUE_TYPE * ten_to_mptwo (int);
113 static const REAL_VALUE_TYPE * real_digit (int);
114 static void times_pten (REAL_VALUE_TYPE *, int);
116 static void round_for_format (const struct real_format *, REAL_VALUE_TYPE *);
118 /* Initialize R with a positive zero. */
120 static inline void
121 get_zero (REAL_VALUE_TYPE *r, int sign)
123 memset (r, 0, sizeof (*r));
124 r->sign = sign;
127 /* Initialize R with the canonical quiet NaN. */
129 static inline void
130 get_canonical_qnan (REAL_VALUE_TYPE *r, int sign)
132 memset (r, 0, sizeof (*r));
133 r->cl = rvc_nan;
134 r->sign = sign;
135 r->canonical = 1;
138 static inline void
139 get_canonical_snan (REAL_VALUE_TYPE *r, int sign)
141 memset (r, 0, sizeof (*r));
142 r->cl = rvc_nan;
143 r->sign = sign;
144 r->signalling = 1;
145 r->canonical = 1;
148 static inline void
149 get_inf (REAL_VALUE_TYPE *r, int sign)
151 memset (r, 0, sizeof (*r));
152 r->cl = rvc_inf;
153 r->sign = sign;
157 /* Right-shift the significand of A by N bits; put the result in the
158 significand of R. If any one bits are shifted out, return true. */
160 static bool
161 sticky_rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
162 unsigned int n)
164 unsigned long sticky = 0;
165 unsigned int i, ofs = 0;
167 if (n >= HOST_BITS_PER_LONG)
169 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
170 sticky |= a->sig[i];
171 n &= HOST_BITS_PER_LONG - 1;
174 if (n != 0)
176 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
177 for (i = 0; i < SIGSZ; ++i)
179 r->sig[i]
180 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
181 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
182 << (HOST_BITS_PER_LONG - n)));
185 else
187 for (i = 0; ofs + i < SIGSZ; ++i)
188 r->sig[i] = a->sig[ofs + i];
189 for (; i < SIGSZ; ++i)
190 r->sig[i] = 0;
193 return sticky != 0;
196 /* Right-shift the significand of A by N bits; put the result in the
197 significand of R. */
199 static void
200 rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
201 unsigned int n)
203 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
205 n &= HOST_BITS_PER_LONG - 1;
206 if (n != 0)
208 for (i = 0; i < SIGSZ; ++i)
210 r->sig[i]
211 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
212 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
213 << (HOST_BITS_PER_LONG - n)));
216 else
218 for (i = 0; ofs + i < SIGSZ; ++i)
219 r->sig[i] = a->sig[ofs + i];
220 for (; i < SIGSZ; ++i)
221 r->sig[i] = 0;
225 /* Left-shift the significand of A by N bits; put the result in the
226 significand of R. */
228 static void
229 lshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
230 unsigned int n)
232 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
234 n &= HOST_BITS_PER_LONG - 1;
235 if (n == 0)
237 for (i = 0; ofs + i < SIGSZ; ++i)
238 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
239 for (; i < SIGSZ; ++i)
240 r->sig[SIGSZ-1-i] = 0;
242 else
243 for (i = 0; i < SIGSZ; ++i)
245 r->sig[SIGSZ-1-i]
246 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
247 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
248 >> (HOST_BITS_PER_LONG - n)));
252 /* Likewise, but N is specialized to 1. */
254 static inline void
255 lshift_significand_1 (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
257 unsigned int i;
259 for (i = SIGSZ - 1; i > 0; --i)
260 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
261 r->sig[0] = a->sig[0] << 1;
264 /* Add the significands of A and B, placing the result in R. Return
265 true if there was carry out of the most significant word. */
267 static inline bool
268 add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
269 const REAL_VALUE_TYPE *b)
271 bool carry = false;
272 int i;
274 for (i = 0; i < SIGSZ; ++i)
276 unsigned long ai = a->sig[i];
277 unsigned long ri = ai + b->sig[i];
279 if (carry)
281 carry = ri < ai;
282 carry |= ++ri == 0;
284 else
285 carry = ri < ai;
287 r->sig[i] = ri;
290 return carry;
293 /* Subtract the significands of A and B, placing the result in R. CARRY is
294 true if there's a borrow incoming to the least significant word.
295 Return true if there was borrow out of the most significant word. */
297 static inline bool
298 sub_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
299 const REAL_VALUE_TYPE *b, int carry)
301 int i;
303 for (i = 0; i < SIGSZ; ++i)
305 unsigned long ai = a->sig[i];
306 unsigned long ri = ai - b->sig[i];
308 if (carry)
310 carry = ri > ai;
311 carry |= ~--ri == 0;
313 else
314 carry = ri > ai;
316 r->sig[i] = ri;
319 return carry;
322 /* Negate the significand A, placing the result in R. */
324 static inline void
325 neg_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
327 bool carry = true;
328 int i;
330 for (i = 0; i < SIGSZ; ++i)
332 unsigned long ri, ai = a->sig[i];
334 if (carry)
336 if (ai)
338 ri = -ai;
339 carry = false;
341 else
342 ri = ai;
344 else
345 ri = ~ai;
347 r->sig[i] = ri;
351 /* Compare significands. Return tri-state vs zero. */
353 static inline int
354 cmp_significands (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
356 int i;
358 for (i = SIGSZ - 1; i >= 0; --i)
360 unsigned long ai = a->sig[i];
361 unsigned long bi = b->sig[i];
363 if (ai > bi)
364 return 1;
365 if (ai < bi)
366 return -1;
369 return 0;
372 /* Return true if A is nonzero. */
374 static inline int
375 cmp_significand_0 (const REAL_VALUE_TYPE *a)
377 int i;
379 for (i = SIGSZ - 1; i >= 0; --i)
380 if (a->sig[i])
381 return 1;
383 return 0;
386 /* Set bit N of the significand of R. */
388 static inline void
389 set_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
391 r->sig[n / HOST_BITS_PER_LONG]
392 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
395 /* Clear bit N of the significand of R. */
397 static inline void
398 clear_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
400 r->sig[n / HOST_BITS_PER_LONG]
401 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
404 /* Test bit N of the significand of R. */
406 static inline bool
407 test_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
409 /* ??? Compiler bug here if we return this expression directly.
410 The conversion to bool strips the "&1" and we wind up testing
411 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
412 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
413 return t;
416 /* Clear bits 0..N-1 of the significand of R. */
418 static void
419 clear_significand_below (REAL_VALUE_TYPE *r, unsigned int n)
421 int i, w = n / HOST_BITS_PER_LONG;
423 for (i = 0; i < w; ++i)
424 r->sig[i] = 0;
426 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
429 /* Divide the significands of A and B, placing the result in R. Return
430 true if the division was inexact. */
432 static inline bool
433 div_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
434 const REAL_VALUE_TYPE *b)
436 REAL_VALUE_TYPE u;
437 int i, bit = SIGNIFICAND_BITS - 1;
438 unsigned long msb, inexact;
440 u = *a;
441 memset (r->sig, 0, sizeof (r->sig));
443 msb = 0;
444 goto start;
447 msb = u.sig[SIGSZ-1] & SIG_MSB;
448 lshift_significand_1 (&u, &u);
449 start:
450 if (msb || cmp_significands (&u, b) >= 0)
452 sub_significands (&u, &u, b, 0);
453 set_significand_bit (r, bit);
456 while (--bit >= 0);
458 for (i = 0, inexact = 0; i < SIGSZ; i++)
459 inexact |= u.sig[i];
461 return inexact != 0;
464 /* Adjust the exponent and significand of R such that the most
465 significant bit is set. We underflow to zero and overflow to
466 infinity here, without denormals. (The intermediate representation
467 exponent is large enough to handle target denormals normalized.) */
469 static void
470 normalize (REAL_VALUE_TYPE *r)
472 int shift = 0, exp;
473 int i, j;
475 if (r->decimal)
476 return;
478 /* Find the first word that is nonzero. */
479 for (i = SIGSZ - 1; i >= 0; i--)
480 if (r->sig[i] == 0)
481 shift += HOST_BITS_PER_LONG;
482 else
483 break;
485 /* Zero significand flushes to zero. */
486 if (i < 0)
488 r->cl = rvc_zero;
489 SET_REAL_EXP (r, 0);
490 return;
493 /* Find the first bit that is nonzero. */
494 for (j = 0; ; j++)
495 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
496 break;
497 shift += j;
499 if (shift > 0)
501 exp = REAL_EXP (r) - shift;
502 if (exp > MAX_EXP)
503 get_inf (r, r->sign);
504 else if (exp < -MAX_EXP)
505 get_zero (r, r->sign);
506 else
508 SET_REAL_EXP (r, exp);
509 lshift_significand (r, r, shift);
514 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
515 result may be inexact due to a loss of precision. */
517 static bool
518 do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
519 const REAL_VALUE_TYPE *b, int subtract_p)
521 int dexp, sign, exp;
522 REAL_VALUE_TYPE t;
523 bool inexact = false;
525 /* Determine if we need to add or subtract. */
526 sign = a->sign;
527 subtract_p = (sign ^ b->sign) ^ subtract_p;
529 switch (CLASS2 (a->cl, b->cl))
531 case CLASS2 (rvc_zero, rvc_zero):
532 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
533 get_zero (r, sign & !subtract_p);
534 return false;
536 case CLASS2 (rvc_zero, rvc_normal):
537 case CLASS2 (rvc_zero, rvc_inf):
538 case CLASS2 (rvc_zero, rvc_nan):
539 /* 0 + ANY = ANY. */
540 case CLASS2 (rvc_normal, rvc_nan):
541 case CLASS2 (rvc_inf, rvc_nan):
542 case CLASS2 (rvc_nan, rvc_nan):
543 /* ANY + NaN = NaN. */
544 case CLASS2 (rvc_normal, rvc_inf):
545 /* R + Inf = Inf. */
546 *r = *b;
547 r->sign = sign ^ subtract_p;
548 return false;
550 case CLASS2 (rvc_normal, rvc_zero):
551 case CLASS2 (rvc_inf, rvc_zero):
552 case CLASS2 (rvc_nan, rvc_zero):
553 /* ANY + 0 = ANY. */
554 case CLASS2 (rvc_nan, rvc_normal):
555 case CLASS2 (rvc_nan, rvc_inf):
556 /* NaN + ANY = NaN. */
557 case CLASS2 (rvc_inf, rvc_normal):
558 /* Inf + R = Inf. */
559 *r = *a;
560 return false;
562 case CLASS2 (rvc_inf, rvc_inf):
563 if (subtract_p)
564 /* Inf - Inf = NaN. */
565 get_canonical_qnan (r, 0);
566 else
567 /* Inf + Inf = Inf. */
568 *r = *a;
569 return false;
571 case CLASS2 (rvc_normal, rvc_normal):
572 break;
574 default:
575 gcc_unreachable ();
578 /* Swap the arguments such that A has the larger exponent. */
579 dexp = REAL_EXP (a) - REAL_EXP (b);
580 if (dexp < 0)
582 const REAL_VALUE_TYPE *t;
583 t = a, a = b, b = t;
584 dexp = -dexp;
585 sign ^= subtract_p;
587 exp = REAL_EXP (a);
589 /* If the exponents are not identical, we need to shift the
590 significand of B down. */
591 if (dexp > 0)
593 /* If the exponents are too far apart, the significands
594 do not overlap, which makes the subtraction a noop. */
595 if (dexp >= SIGNIFICAND_BITS)
597 *r = *a;
598 r->sign = sign;
599 return true;
602 inexact |= sticky_rshift_significand (&t, b, dexp);
603 b = &t;
606 if (subtract_p)
608 if (sub_significands (r, a, b, inexact))
610 /* We got a borrow out of the subtraction. That means that
611 A and B had the same exponent, and B had the larger
612 significand. We need to swap the sign and negate the
613 significand. */
614 sign ^= 1;
615 neg_significand (r, r);
618 else
620 if (add_significands (r, a, b))
622 /* We got carry out of the addition. This means we need to
623 shift the significand back down one bit and increase the
624 exponent. */
625 inexact |= sticky_rshift_significand (r, r, 1);
626 r->sig[SIGSZ-1] |= SIG_MSB;
627 if (++exp > MAX_EXP)
629 get_inf (r, sign);
630 return true;
635 r->cl = rvc_normal;
636 r->sign = sign;
637 SET_REAL_EXP (r, exp);
638 /* Zero out the remaining fields. */
639 r->signalling = 0;
640 r->canonical = 0;
641 r->decimal = 0;
643 /* Re-normalize the result. */
644 normalize (r);
646 /* Special case: if the subtraction results in zero, the result
647 is positive. */
648 if (r->cl == rvc_zero)
649 r->sign = 0;
650 else
651 r->sig[0] |= inexact;
653 return inexact;
656 /* Calculate R = A * B. Return true if the result may be inexact. */
658 static bool
659 do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
660 const REAL_VALUE_TYPE *b)
662 REAL_VALUE_TYPE u, t, *rr;
663 unsigned int i, j, k;
664 int sign = a->sign ^ b->sign;
665 bool inexact = false;
667 switch (CLASS2 (a->cl, b->cl))
669 case CLASS2 (rvc_zero, rvc_zero):
670 case CLASS2 (rvc_zero, rvc_normal):
671 case CLASS2 (rvc_normal, rvc_zero):
672 /* +-0 * ANY = 0 with appropriate sign. */
673 get_zero (r, sign);
674 return false;
676 case CLASS2 (rvc_zero, rvc_nan):
677 case CLASS2 (rvc_normal, rvc_nan):
678 case CLASS2 (rvc_inf, rvc_nan):
679 case CLASS2 (rvc_nan, rvc_nan):
680 /* ANY * NaN = NaN. */
681 *r = *b;
682 r->sign = sign;
683 return false;
685 case CLASS2 (rvc_nan, rvc_zero):
686 case CLASS2 (rvc_nan, rvc_normal):
687 case CLASS2 (rvc_nan, rvc_inf):
688 /* NaN * ANY = NaN. */
689 *r = *a;
690 r->sign = sign;
691 return false;
693 case CLASS2 (rvc_zero, rvc_inf):
694 case CLASS2 (rvc_inf, rvc_zero):
695 /* 0 * Inf = NaN */
696 get_canonical_qnan (r, sign);
697 return false;
699 case CLASS2 (rvc_inf, rvc_inf):
700 case CLASS2 (rvc_normal, rvc_inf):
701 case CLASS2 (rvc_inf, rvc_normal):
702 /* Inf * Inf = Inf, R * Inf = Inf */
703 get_inf (r, sign);
704 return false;
706 case CLASS2 (rvc_normal, rvc_normal):
707 break;
709 default:
710 gcc_unreachable ();
713 if (r == a || r == b)
714 rr = &t;
715 else
716 rr = r;
717 get_zero (rr, 0);
719 /* Collect all the partial products. Since we don't have sure access
720 to a widening multiply, we split each long into two half-words.
722 Consider the long-hand form of a four half-word multiplication:
724 A B C D
725 * E F G H
726 --------------
727 DE DF DG DH
728 CE CF CG CH
729 BE BF BG BH
730 AE AF AG AH
732 We construct partial products of the widened half-word products
733 that are known to not overlap, e.g. DF+DH. Each such partial
734 product is given its proper exponent, which allows us to sum them
735 and obtain the finished product. */
737 for (i = 0; i < SIGSZ * 2; ++i)
739 unsigned long ai = a->sig[i / 2];
740 if (i & 1)
741 ai >>= HOST_BITS_PER_LONG / 2;
742 else
743 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
745 if (ai == 0)
746 continue;
748 for (j = 0; j < 2; ++j)
750 int exp = (REAL_EXP (a) - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
751 + (REAL_EXP (b) - (1-j)*(HOST_BITS_PER_LONG/2)));
753 if (exp > MAX_EXP)
755 get_inf (r, sign);
756 return true;
758 if (exp < -MAX_EXP)
760 /* Would underflow to zero, which we shouldn't bother adding. */
761 inexact = true;
762 continue;
765 memset (&u, 0, sizeof (u));
766 u.cl = rvc_normal;
767 SET_REAL_EXP (&u, exp);
769 for (k = j; k < SIGSZ * 2; k += 2)
771 unsigned long bi = b->sig[k / 2];
772 if (k & 1)
773 bi >>= HOST_BITS_PER_LONG / 2;
774 else
775 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
777 u.sig[k / 2] = ai * bi;
780 normalize (&u);
781 inexact |= do_add (rr, rr, &u, 0);
785 rr->sign = sign;
786 if (rr != r)
787 *r = t;
789 return inexact;
792 /* Calculate R = A / B. Return true if the result may be inexact. */
794 static bool
795 do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
796 const REAL_VALUE_TYPE *b)
798 int exp, sign = a->sign ^ b->sign;
799 REAL_VALUE_TYPE t, *rr;
800 bool inexact;
802 switch (CLASS2 (a->cl, b->cl))
804 case CLASS2 (rvc_zero, rvc_zero):
805 /* 0 / 0 = NaN. */
806 case CLASS2 (rvc_inf, rvc_inf):
807 /* Inf / Inf = NaN. */
808 get_canonical_qnan (r, sign);
809 return false;
811 case CLASS2 (rvc_zero, rvc_normal):
812 case CLASS2 (rvc_zero, rvc_inf):
813 /* 0 / ANY = 0. */
814 case CLASS2 (rvc_normal, rvc_inf):
815 /* R / Inf = 0. */
816 get_zero (r, sign);
817 return false;
819 case CLASS2 (rvc_normal, rvc_zero):
820 /* R / 0 = Inf. */
821 case CLASS2 (rvc_inf, rvc_zero):
822 /* Inf / 0 = Inf. */
823 get_inf (r, sign);
824 return false;
826 case CLASS2 (rvc_zero, rvc_nan):
827 case CLASS2 (rvc_normal, rvc_nan):
828 case CLASS2 (rvc_inf, rvc_nan):
829 case CLASS2 (rvc_nan, rvc_nan):
830 /* ANY / NaN = NaN. */
831 *r = *b;
832 r->sign = sign;
833 return false;
835 case CLASS2 (rvc_nan, rvc_zero):
836 case CLASS2 (rvc_nan, rvc_normal):
837 case CLASS2 (rvc_nan, rvc_inf):
838 /* NaN / ANY = NaN. */
839 *r = *a;
840 r->sign = sign;
841 return false;
843 case CLASS2 (rvc_inf, rvc_normal):
844 /* Inf / R = Inf. */
845 get_inf (r, sign);
846 return false;
848 case CLASS2 (rvc_normal, rvc_normal):
849 break;
851 default:
852 gcc_unreachable ();
855 if (r == a || r == b)
856 rr = &t;
857 else
858 rr = r;
860 /* Make sure all fields in the result are initialized. */
861 get_zero (rr, 0);
862 rr->cl = rvc_normal;
863 rr->sign = sign;
865 exp = REAL_EXP (a) - REAL_EXP (b) + 1;
866 if (exp > MAX_EXP)
868 get_inf (r, sign);
869 return true;
871 if (exp < -MAX_EXP)
873 get_zero (r, sign);
874 return true;
876 SET_REAL_EXP (rr, exp);
878 inexact = div_significands (rr, a, b);
880 /* Re-normalize the result. */
881 normalize (rr);
882 rr->sig[0] |= inexact;
884 if (rr != r)
885 *r = t;
887 return inexact;
890 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
891 one of the two operands is a NaN. */
893 static int
894 do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
895 int nan_result)
897 int ret;
899 switch (CLASS2 (a->cl, b->cl))
901 case CLASS2 (rvc_zero, rvc_zero):
902 /* Sign of zero doesn't matter for compares. */
903 return 0;
905 case CLASS2 (rvc_normal, rvc_zero):
906 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
907 if (a->decimal)
908 return decimal_do_compare (a, b, nan_result);
909 /* Fall through. */
910 case CLASS2 (rvc_inf, rvc_zero):
911 case CLASS2 (rvc_inf, rvc_normal):
912 return (a->sign ? -1 : 1);
914 case CLASS2 (rvc_inf, rvc_inf):
915 return -a->sign - -b->sign;
917 case CLASS2 (rvc_zero, rvc_normal):
918 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
919 if (b->decimal)
920 return decimal_do_compare (a, b, nan_result);
921 /* Fall through. */
922 case CLASS2 (rvc_zero, rvc_inf):
923 case CLASS2 (rvc_normal, rvc_inf):
924 return (b->sign ? 1 : -1);
926 case CLASS2 (rvc_zero, rvc_nan):
927 case CLASS2 (rvc_normal, rvc_nan):
928 case CLASS2 (rvc_inf, rvc_nan):
929 case CLASS2 (rvc_nan, rvc_nan):
930 case CLASS2 (rvc_nan, rvc_zero):
931 case CLASS2 (rvc_nan, rvc_normal):
932 case CLASS2 (rvc_nan, rvc_inf):
933 return nan_result;
935 case CLASS2 (rvc_normal, rvc_normal):
936 break;
938 default:
939 gcc_unreachable ();
942 if (a->sign != b->sign)
943 return -a->sign - -b->sign;
945 if (a->decimal || b->decimal)
946 return decimal_do_compare (a, b, nan_result);
948 if (REAL_EXP (a) > REAL_EXP (b))
949 ret = 1;
950 else if (REAL_EXP (a) < REAL_EXP (b))
951 ret = -1;
952 else
953 ret = cmp_significands (a, b);
955 return (a->sign ? -ret : ret);
958 /* Return A truncated to an integral value toward zero. */
960 static void
961 do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
963 *r = *a;
965 switch (r->cl)
967 case rvc_zero:
968 case rvc_inf:
969 case rvc_nan:
970 break;
972 case rvc_normal:
973 if (r->decimal)
975 decimal_do_fix_trunc (r, a);
976 return;
978 if (REAL_EXP (r) <= 0)
979 get_zero (r, r->sign);
980 else if (REAL_EXP (r) < SIGNIFICAND_BITS)
981 clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r));
982 break;
984 default:
985 gcc_unreachable ();
989 /* Perform the binary or unary operation described by CODE.
990 For a unary operation, leave OP1 NULL. This function returns
991 true if the result may be inexact due to loss of precision. */
993 bool
994 real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0,
995 const REAL_VALUE_TYPE *op1)
997 enum tree_code code = (enum tree_code) icode;
999 if (op0->decimal || (op1 && op1->decimal))
1000 return decimal_real_arithmetic (r, code, op0, op1);
1002 switch (code)
1004 case PLUS_EXPR:
1005 /* Clear any padding areas in *r if it isn't equal to one of the
1006 operands so that we can later do bitwise comparisons later on. */
1007 if (r != op0 && r != op1)
1008 memset (r, '\0', sizeof (*r));
1009 return do_add (r, op0, op1, 0);
1011 case MINUS_EXPR:
1012 if (r != op0 && r != op1)
1013 memset (r, '\0', sizeof (*r));
1014 return do_add (r, op0, op1, 1);
1016 case MULT_EXPR:
1017 if (r != op0 && r != op1)
1018 memset (r, '\0', sizeof (*r));
1019 return do_multiply (r, op0, op1);
1021 case RDIV_EXPR:
1022 if (r != op0 && r != op1)
1023 memset (r, '\0', sizeof (*r));
1024 return do_divide (r, op0, op1);
1026 case MIN_EXPR:
1027 if (op1->cl == rvc_nan)
1028 *r = *op1;
1029 else if (do_compare (op0, op1, -1) < 0)
1030 *r = *op0;
1031 else
1032 *r = *op1;
1033 break;
1035 case MAX_EXPR:
1036 if (op1->cl == rvc_nan)
1037 *r = *op1;
1038 else if (do_compare (op0, op1, 1) < 0)
1039 *r = *op1;
1040 else
1041 *r = *op0;
1042 break;
1044 case NEGATE_EXPR:
1045 *r = *op0;
1046 r->sign ^= 1;
1047 break;
1049 case ABS_EXPR:
1050 *r = *op0;
1051 r->sign = 0;
1052 break;
1054 case FIX_TRUNC_EXPR:
1055 do_fix_trunc (r, op0);
1056 break;
1058 default:
1059 gcc_unreachable ();
1061 return false;
1064 REAL_VALUE_TYPE
1065 real_value_negate (const REAL_VALUE_TYPE *op0)
1067 REAL_VALUE_TYPE r;
1068 real_arithmetic (&r, NEGATE_EXPR, op0, NULL);
1069 return r;
1072 REAL_VALUE_TYPE
1073 real_value_abs (const REAL_VALUE_TYPE *op0)
1075 REAL_VALUE_TYPE r;
1076 real_arithmetic (&r, ABS_EXPR, op0, NULL);
1077 return r;
1080 bool
1081 real_compare (int icode, const REAL_VALUE_TYPE *op0,
1082 const REAL_VALUE_TYPE *op1)
1084 enum tree_code code = (enum tree_code) icode;
1086 switch (code)
1088 case LT_EXPR:
1089 return do_compare (op0, op1, 1) < 0;
1090 case LE_EXPR:
1091 return do_compare (op0, op1, 1) <= 0;
1092 case GT_EXPR:
1093 return do_compare (op0, op1, -1) > 0;
1094 case GE_EXPR:
1095 return do_compare (op0, op1, -1) >= 0;
1096 case EQ_EXPR:
1097 return do_compare (op0, op1, -1) == 0;
1098 case NE_EXPR:
1099 return do_compare (op0, op1, -1) != 0;
1100 case UNORDERED_EXPR:
1101 return op0->cl == rvc_nan || op1->cl == rvc_nan;
1102 case ORDERED_EXPR:
1103 return op0->cl != rvc_nan && op1->cl != rvc_nan;
1104 case UNLT_EXPR:
1105 return do_compare (op0, op1, -1) < 0;
1106 case UNLE_EXPR:
1107 return do_compare (op0, op1, -1) <= 0;
1108 case UNGT_EXPR:
1109 return do_compare (op0, op1, 1) > 0;
1110 case UNGE_EXPR:
1111 return do_compare (op0, op1, 1) >= 0;
1112 case UNEQ_EXPR:
1113 return do_compare (op0, op1, 0) == 0;
1114 case LTGT_EXPR:
1115 return do_compare (op0, op1, 0) != 0;
1117 default:
1118 gcc_unreachable ();
1122 /* Return floor log2(R). */
1125 real_exponent (const REAL_VALUE_TYPE *r)
1127 switch (r->cl)
1129 case rvc_zero:
1130 return 0;
1131 case rvc_inf:
1132 case rvc_nan:
1133 return (unsigned int)-1 >> 1;
1134 case rvc_normal:
1135 return REAL_EXP (r);
1136 default:
1137 gcc_unreachable ();
1141 /* R = OP0 * 2**EXP. */
1143 void
1144 real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1146 *r = *op0;
1147 switch (r->cl)
1149 case rvc_zero:
1150 case rvc_inf:
1151 case rvc_nan:
1152 break;
1154 case rvc_normal:
1155 exp += REAL_EXP (op0);
1156 if (exp > MAX_EXP)
1157 get_inf (r, r->sign);
1158 else if (exp < -MAX_EXP)
1159 get_zero (r, r->sign);
1160 else
1161 SET_REAL_EXP (r, exp);
1162 break;
1164 default:
1165 gcc_unreachable ();
1169 /* Determine whether a floating-point value X is infinite. */
1171 bool
1172 real_isinf (const REAL_VALUE_TYPE *r)
1174 return (r->cl == rvc_inf);
1177 /* Determine whether a floating-point value X is a NaN. */
1179 bool
1180 real_isnan (const REAL_VALUE_TYPE *r)
1182 return (r->cl == rvc_nan);
1185 /* Determine whether a floating-point value X is finite. */
1187 bool
1188 real_isfinite (const REAL_VALUE_TYPE *r)
1190 return (r->cl != rvc_nan) && (r->cl != rvc_inf);
1193 /* Determine whether a floating-point value X is negative. */
1195 bool
1196 real_isneg (const REAL_VALUE_TYPE *r)
1198 return r->sign;
1201 /* Determine whether a floating-point value X is minus zero. */
1203 bool
1204 real_isnegzero (const REAL_VALUE_TYPE *r)
1206 return r->sign && r->cl == rvc_zero;
1209 /* Compare two floating-point objects for bitwise identity. */
1211 bool
1212 real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1214 int i;
1216 if (a->cl != b->cl)
1217 return false;
1218 if (a->sign != b->sign)
1219 return false;
1221 switch (a->cl)
1223 case rvc_zero:
1224 case rvc_inf:
1225 return true;
1227 case rvc_normal:
1228 if (a->decimal != b->decimal)
1229 return false;
1230 if (REAL_EXP (a) != REAL_EXP (b))
1231 return false;
1232 break;
1234 case rvc_nan:
1235 if (a->signalling != b->signalling)
1236 return false;
1237 /* The significand is ignored for canonical NaNs. */
1238 if (a->canonical || b->canonical)
1239 return a->canonical == b->canonical;
1240 break;
1242 default:
1243 gcc_unreachable ();
1246 for (i = 0; i < SIGSZ; ++i)
1247 if (a->sig[i] != b->sig[i])
1248 return false;
1250 return true;
1253 /* Try to change R into its exact multiplicative inverse in machine
1254 mode MODE. Return true if successful. */
1256 bool
1257 exact_real_inverse (machine_mode mode, REAL_VALUE_TYPE *r)
1259 const REAL_VALUE_TYPE *one = real_digit (1);
1260 REAL_VALUE_TYPE u;
1261 int i;
1263 if (r->cl != rvc_normal)
1264 return false;
1266 /* Check for a power of two: all significand bits zero except the MSB. */
1267 for (i = 0; i < SIGSZ-1; ++i)
1268 if (r->sig[i] != 0)
1269 return false;
1270 if (r->sig[SIGSZ-1] != SIG_MSB)
1271 return false;
1273 /* Find the inverse and truncate to the required mode. */
1274 do_divide (&u, one, r);
1275 real_convert (&u, mode, &u);
1277 /* The rounding may have overflowed. */
1278 if (u.cl != rvc_normal)
1279 return false;
1280 for (i = 0; i < SIGSZ-1; ++i)
1281 if (u.sig[i] != 0)
1282 return false;
1283 if (u.sig[SIGSZ-1] != SIG_MSB)
1284 return false;
1286 *r = u;
1287 return true;
1290 /* Return true if arithmetic on values in IMODE that were promoted
1291 from values in TMODE is equivalent to direct arithmetic on values
1292 in TMODE. */
1294 bool
1295 real_can_shorten_arithmetic (machine_mode imode, machine_mode tmode)
1297 const struct real_format *tfmt, *ifmt;
1298 tfmt = REAL_MODE_FORMAT (tmode);
1299 ifmt = REAL_MODE_FORMAT (imode);
1300 /* These conditions are conservative rather than trying to catch the
1301 exact boundary conditions; the main case to allow is IEEE float
1302 and double. */
1303 return (ifmt->b == tfmt->b
1304 && ifmt->p > 2 * tfmt->p
1305 && ifmt->emin < 2 * tfmt->emin - tfmt->p - 2
1306 && ifmt->emin < tfmt->emin - tfmt->emax - tfmt->p - 2
1307 && ifmt->emax > 2 * tfmt->emax + 2
1308 && ifmt->emax > tfmt->emax - tfmt->emin + tfmt->p + 2
1309 && ifmt->round_towards_zero == tfmt->round_towards_zero
1310 && (ifmt->has_sign_dependent_rounding
1311 == tfmt->has_sign_dependent_rounding)
1312 && ifmt->has_nans >= tfmt->has_nans
1313 && ifmt->has_inf >= tfmt->has_inf
1314 && ifmt->has_signed_zero >= tfmt->has_signed_zero
1315 && !MODE_COMPOSITE_P (tmode)
1316 && !MODE_COMPOSITE_P (imode));
1319 /* Render R as an integer. */
1321 HOST_WIDE_INT
1322 real_to_integer (const REAL_VALUE_TYPE *r)
1324 unsigned HOST_WIDE_INT i;
1326 switch (r->cl)
1328 case rvc_zero:
1329 underflow:
1330 return 0;
1332 case rvc_inf:
1333 case rvc_nan:
1334 overflow:
1335 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1336 if (!r->sign)
1337 i--;
1338 return i;
1340 case rvc_normal:
1341 if (r->decimal)
1342 return decimal_real_to_integer (r);
1344 if (REAL_EXP (r) <= 0)
1345 goto underflow;
1346 /* Only force overflow for unsigned overflow. Signed overflow is
1347 undefined, so it doesn't matter what we return, and some callers
1348 expect to be able to use this routine for both signed and
1349 unsigned conversions. */
1350 if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1351 goto overflow;
1353 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1354 i = r->sig[SIGSZ-1];
1355 else
1357 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1358 i = r->sig[SIGSZ-1];
1359 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1360 i |= r->sig[SIGSZ-2];
1363 i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1365 if (r->sign)
1366 i = -i;
1367 return i;
1369 default:
1370 gcc_unreachable ();
1374 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1375 be represented in precision, *FAIL is set to TRUE. */
1377 wide_int
1378 real_to_integer (const REAL_VALUE_TYPE *r, bool *fail, int precision)
1380 HOST_WIDE_INT val[2 * WIDE_INT_MAX_ELTS];
1381 int exp;
1382 int words, w;
1383 wide_int result;
1385 switch (r->cl)
1387 case rvc_zero:
1388 underflow:
1389 return wi::zero (precision);
1391 case rvc_inf:
1392 case rvc_nan:
1393 overflow:
1394 *fail = true;
1396 if (r->sign)
1397 return wi::set_bit_in_zero (precision - 1, precision);
1398 else
1399 return ~wi::set_bit_in_zero (precision - 1, precision);
1401 case rvc_normal:
1402 if (r->decimal)
1403 return decimal_real_to_integer (r, fail, precision);
1405 exp = REAL_EXP (r);
1406 if (exp <= 0)
1407 goto underflow;
1408 /* Only force overflow for unsigned overflow. Signed overflow is
1409 undefined, so it doesn't matter what we return, and some callers
1410 expect to be able to use this routine for both signed and
1411 unsigned conversions. */
1412 if (exp > precision)
1413 goto overflow;
1415 /* Put the significand into a wide_int that has precision W, which
1416 is the smallest HWI-multiple that has at least PRECISION bits.
1417 This ensures that the top bit of the significand is in the
1418 top bit of the wide_int. */
1419 words = (precision + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT;
1420 w = words * HOST_BITS_PER_WIDE_INT;
1422 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1423 for (int i = 0; i < words; i++)
1425 int j = SIGSZ - words + i;
1426 val[i] = (j < 0) ? 0 : r->sig[j];
1428 #else
1429 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1430 for (int i = 0; i < words; i++)
1432 int j = SIGSZ - (words * 2) + (i * 2);
1433 if (j < 0)
1434 val[i] = 0;
1435 else
1436 val[i] = r->sig[j];
1437 j += 1;
1438 if (j >= 0)
1439 val[i] |= (unsigned HOST_WIDE_INT) r->sig[j] << HOST_BITS_PER_LONG;
1441 #endif
1442 /* Shift the value into place and truncate to the desired precision. */
1443 result = wide_int::from_array (val, words, w);
1444 result = wi::lrshift (result, w - exp);
1445 result = wide_int::from (result, precision, UNSIGNED);
1447 if (r->sign)
1448 return -result;
1449 else
1450 return result;
1452 default:
1453 gcc_unreachable ();
1457 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1458 of NUM / DEN. Return the quotient and place the remainder in NUM.
1459 It is expected that NUM / DEN are close enough that the quotient is
1460 small. */
1462 static unsigned long
1463 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1465 unsigned long q, msb;
1466 int expn = REAL_EXP (num), expd = REAL_EXP (den);
1468 if (expn < expd)
1469 return 0;
1471 q = msb = 0;
1472 goto start;
1475 msb = num->sig[SIGSZ-1] & SIG_MSB;
1476 q <<= 1;
1477 lshift_significand_1 (num, num);
1478 start:
1479 if (msb || cmp_significands (num, den) >= 0)
1481 sub_significands (num, num, den, 0);
1482 q |= 1;
1485 while (--expn >= expd);
1487 SET_REAL_EXP (num, expd);
1488 normalize (num);
1490 return q;
1493 /* Render R as a decimal floating point constant. Emit DIGITS significant
1494 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1495 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1496 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1497 to a string that, when parsed back in mode MODE, yields the same value. */
1499 #define M_LOG10_2 0.30102999566398119521
1501 void
1502 real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig,
1503 size_t buf_size, size_t digits,
1504 int crop_trailing_zeros, machine_mode mode)
1506 const struct real_format *fmt = NULL;
1507 const REAL_VALUE_TYPE *one, *ten;
1508 REAL_VALUE_TYPE r, pten, u, v;
1509 int dec_exp, cmp_one, digit;
1510 size_t max_digits;
1511 char *p, *first, *last;
1512 bool sign;
1513 bool round_up;
1515 if (mode != VOIDmode)
1517 fmt = REAL_MODE_FORMAT (mode);
1518 gcc_assert (fmt);
1521 r = *r_orig;
1522 switch (r.cl)
1524 case rvc_zero:
1525 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1526 return;
1527 case rvc_normal:
1528 break;
1529 case rvc_inf:
1530 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1531 return;
1532 case rvc_nan:
1533 /* ??? Print the significand as well, if not canonical? */
1534 sprintf (str, "%c%cNaN", (r_orig->sign ? '-' : '+'),
1535 (r_orig->signalling ? 'S' : 'Q'));
1536 return;
1537 default:
1538 gcc_unreachable ();
1541 if (r.decimal)
1543 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1544 return;
1547 /* Bound the number of digits printed by the size of the representation. */
1548 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1549 if (digits == 0 || digits > max_digits)
1550 digits = max_digits;
1552 /* Estimate the decimal exponent, and compute the length of the string it
1553 will print as. Be conservative and add one to account for possible
1554 overflow or rounding error. */
1555 dec_exp = REAL_EXP (&r) * M_LOG10_2;
1556 for (max_digits = 1; dec_exp ; max_digits++)
1557 dec_exp /= 10;
1559 /* Bound the number of digits printed by the size of the output buffer. */
1560 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1561 gcc_assert (max_digits <= buf_size);
1562 if (digits > max_digits)
1563 digits = max_digits;
1565 one = real_digit (1);
1566 ten = ten_to_ptwo (0);
1568 sign = r.sign;
1569 r.sign = 0;
1571 dec_exp = 0;
1572 pten = *one;
1574 cmp_one = do_compare (&r, one, 0);
1575 if (cmp_one > 0)
1577 int m;
1579 /* Number is greater than one. Convert significand to an integer
1580 and strip trailing decimal zeros. */
1582 u = r;
1583 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1585 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1586 m = floor_log2 (max_digits);
1588 /* Iterate over the bits of the possible powers of 10 that might
1589 be present in U and eliminate them. That is, if we find that
1590 10**2**M divides U evenly, keep the division and increase
1591 DEC_EXP by 2**M. */
1594 REAL_VALUE_TYPE t;
1596 do_divide (&t, &u, ten_to_ptwo (m));
1597 do_fix_trunc (&v, &t);
1598 if (cmp_significands (&v, &t) == 0)
1600 u = t;
1601 dec_exp += 1 << m;
1604 while (--m >= 0);
1606 /* Revert the scaling to integer that we performed earlier. */
1607 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1608 - (SIGNIFICAND_BITS - 1));
1609 r = u;
1611 /* Find power of 10. Do this by dividing out 10**2**M when
1612 this is larger than the current remainder. Fill PTEN with
1613 the power of 10 that we compute. */
1614 if (REAL_EXP (&r) > 0)
1616 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1619 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1620 if (do_compare (&u, ptentwo, 0) >= 0)
1622 do_divide (&u, &u, ptentwo);
1623 do_multiply (&pten, &pten, ptentwo);
1624 dec_exp += 1 << m;
1627 while (--m >= 0);
1629 else
1630 /* We managed to divide off enough tens in the above reduction
1631 loop that we've now got a negative exponent. Fall into the
1632 less-than-one code to compute the proper value for PTEN. */
1633 cmp_one = -1;
1635 if (cmp_one < 0)
1637 int m;
1639 /* Number is less than one. Pad significand with leading
1640 decimal zeros. */
1642 v = r;
1643 while (1)
1645 /* Stop if we'd shift bits off the bottom. */
1646 if (v.sig[0] & 7)
1647 break;
1649 do_multiply (&u, &v, ten);
1651 /* Stop if we're now >= 1. */
1652 if (REAL_EXP (&u) > 0)
1653 break;
1655 v = u;
1656 dec_exp -= 1;
1658 r = v;
1660 /* Find power of 10. Do this by multiplying in P=10**2**M when
1661 the current remainder is smaller than 1/P. Fill PTEN with the
1662 power of 10 that we compute. */
1663 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1666 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1667 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1669 if (do_compare (&v, ptenmtwo, 0) <= 0)
1671 do_multiply (&v, &v, ptentwo);
1672 do_multiply (&pten, &pten, ptentwo);
1673 dec_exp -= 1 << m;
1676 while (--m >= 0);
1678 /* Invert the positive power of 10 that we've collected so far. */
1679 do_divide (&pten, one, &pten);
1682 p = str;
1683 if (sign)
1684 *p++ = '-';
1685 first = p++;
1687 /* At this point, PTEN should contain the nearest power of 10 smaller
1688 than R, such that this division produces the first digit.
1690 Using a divide-step primitive that returns the complete integral
1691 remainder avoids the rounding error that would be produced if
1692 we were to use do_divide here and then simply multiply by 10 for
1693 each subsequent digit. */
1695 digit = rtd_divmod (&r, &pten);
1697 /* Be prepared for error in that division via underflow ... */
1698 if (digit == 0 && cmp_significand_0 (&r))
1700 /* Multiply by 10 and try again. */
1701 do_multiply (&r, &r, ten);
1702 digit = rtd_divmod (&r, &pten);
1703 dec_exp -= 1;
1704 gcc_assert (digit != 0);
1707 /* ... or overflow. */
1708 if (digit == 10)
1710 *p++ = '1';
1711 if (--digits > 0)
1712 *p++ = '0';
1713 dec_exp += 1;
1715 else
1717 gcc_assert (digit <= 10);
1718 *p++ = digit + '0';
1721 /* Generate subsequent digits. */
1722 while (--digits > 0)
1724 do_multiply (&r, &r, ten);
1725 digit = rtd_divmod (&r, &pten);
1726 *p++ = digit + '0';
1728 last = p;
1730 /* Generate one more digit with which to do rounding. */
1731 do_multiply (&r, &r, ten);
1732 digit = rtd_divmod (&r, &pten);
1734 /* Round the result. */
1735 if (fmt && fmt->round_towards_zero)
1737 /* If the format uses round towards zero when parsing the string
1738 back in, we need to always round away from zero here. */
1739 if (cmp_significand_0 (&r))
1740 digit++;
1741 round_up = digit > 0;
1743 else
1745 if (digit == 5)
1747 /* Round to nearest. If R is nonzero there are additional
1748 nonzero digits to be extracted. */
1749 if (cmp_significand_0 (&r))
1750 digit++;
1751 /* Round to even. */
1752 else if ((p[-1] - '0') & 1)
1753 digit++;
1756 round_up = digit > 5;
1759 if (round_up)
1761 while (p > first)
1763 digit = *--p;
1764 if (digit == '9')
1765 *p = '0';
1766 else
1768 *p = digit + 1;
1769 break;
1773 /* Carry out of the first digit. This means we had all 9's and
1774 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1775 if (p == first)
1777 first[1] = '1';
1778 dec_exp++;
1782 /* Insert the decimal point. */
1783 first[0] = first[1];
1784 first[1] = '.';
1786 /* If requested, drop trailing zeros. Never crop past "1.0". */
1787 if (crop_trailing_zeros)
1788 while (last > first + 3 && last[-1] == '0')
1789 last--;
1791 /* Append the exponent. */
1792 sprintf (last, "e%+d", dec_exp);
1794 #ifdef ENABLE_CHECKING
1795 /* Verify that we can read the original value back in. */
1796 if (mode != VOIDmode)
1798 real_from_string (&r, str);
1799 real_convert (&r, mode, &r);
1800 gcc_assert (real_identical (&r, r_orig));
1802 #endif
1805 /* Likewise, except always uses round-to-nearest. */
1807 void
1808 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1809 size_t digits, int crop_trailing_zeros)
1811 real_to_decimal_for_mode (str, r_orig, buf_size,
1812 digits, crop_trailing_zeros, VOIDmode);
1815 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1816 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1817 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1818 strip trailing zeros. */
1820 void
1821 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1822 size_t digits, int crop_trailing_zeros)
1824 int i, j, exp = REAL_EXP (r);
1825 char *p, *first;
1826 char exp_buf[16];
1827 size_t max_digits;
1829 switch (r->cl)
1831 case rvc_zero:
1832 exp = 0;
1833 break;
1834 case rvc_normal:
1835 break;
1836 case rvc_inf:
1837 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1838 return;
1839 case rvc_nan:
1840 /* ??? Print the significand as well, if not canonical? */
1841 sprintf (str, "%c%cNaN", (r->sign ? '-' : '+'),
1842 (r->signalling ? 'S' : 'Q'));
1843 return;
1844 default:
1845 gcc_unreachable ();
1848 if (r->decimal)
1850 /* Hexadecimal format for decimal floats is not interesting. */
1851 strcpy (str, "N/A");
1852 return;
1855 if (digits == 0)
1856 digits = SIGNIFICAND_BITS / 4;
1858 /* Bound the number of digits printed by the size of the output buffer. */
1860 sprintf (exp_buf, "p%+d", exp);
1861 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1862 gcc_assert (max_digits <= buf_size);
1863 if (digits > max_digits)
1864 digits = max_digits;
1866 p = str;
1867 if (r->sign)
1868 *p++ = '-';
1869 *p++ = '0';
1870 *p++ = 'x';
1871 *p++ = '0';
1872 *p++ = '.';
1873 first = p;
1875 for (i = SIGSZ - 1; i >= 0; --i)
1876 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1878 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1879 if (--digits == 0)
1880 goto out;
1883 out:
1884 if (crop_trailing_zeros)
1885 while (p > first + 1 && p[-1] == '0')
1886 p--;
1888 sprintf (p, "p%+d", exp);
1891 /* Initialize R from a decimal or hexadecimal string. The string is
1892 assumed to have been syntax checked already. Return -1 if the
1893 value underflows, +1 if overflows, and 0 otherwise. */
1896 real_from_string (REAL_VALUE_TYPE *r, const char *str)
1898 int exp = 0;
1899 bool sign = false;
1901 get_zero (r, 0);
1903 if (*str == '-')
1905 sign = true;
1906 str++;
1908 else if (*str == '+')
1909 str++;
1911 if (!strncmp (str, "QNaN", 4))
1913 get_canonical_qnan (r, sign);
1914 return 0;
1916 else if (!strncmp (str, "SNaN", 4))
1918 get_canonical_snan (r, sign);
1919 return 0;
1921 else if (!strncmp (str, "Inf", 3))
1923 get_inf (r, sign);
1924 return 0;
1927 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1929 /* Hexadecimal floating point. */
1930 int pos = SIGNIFICAND_BITS - 4, d;
1932 str += 2;
1934 while (*str == '0')
1935 str++;
1936 while (1)
1938 d = hex_value (*str);
1939 if (d == _hex_bad)
1940 break;
1941 if (pos >= 0)
1943 r->sig[pos / HOST_BITS_PER_LONG]
1944 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1945 pos -= 4;
1947 else if (d)
1948 /* Ensure correct rounding by setting last bit if there is
1949 a subsequent nonzero digit. */
1950 r->sig[0] |= 1;
1951 exp += 4;
1952 str++;
1954 if (*str == '.')
1956 str++;
1957 if (pos == SIGNIFICAND_BITS - 4)
1959 while (*str == '0')
1960 str++, exp -= 4;
1962 while (1)
1964 d = hex_value (*str);
1965 if (d == _hex_bad)
1966 break;
1967 if (pos >= 0)
1969 r->sig[pos / HOST_BITS_PER_LONG]
1970 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1971 pos -= 4;
1973 else if (d)
1974 /* Ensure correct rounding by setting last bit if there is
1975 a subsequent nonzero digit. */
1976 r->sig[0] |= 1;
1977 str++;
1981 /* If the mantissa is zero, ignore the exponent. */
1982 if (!cmp_significand_0 (r))
1983 goto is_a_zero;
1985 if (*str == 'p' || *str == 'P')
1987 bool exp_neg = false;
1989 str++;
1990 if (*str == '-')
1992 exp_neg = true;
1993 str++;
1995 else if (*str == '+')
1996 str++;
1998 d = 0;
1999 while (ISDIGIT (*str))
2001 d *= 10;
2002 d += *str - '0';
2003 if (d > MAX_EXP)
2005 /* Overflowed the exponent. */
2006 if (exp_neg)
2007 goto underflow;
2008 else
2009 goto overflow;
2011 str++;
2013 if (exp_neg)
2014 d = -d;
2016 exp += d;
2019 r->cl = rvc_normal;
2020 SET_REAL_EXP (r, exp);
2022 normalize (r);
2024 else
2026 /* Decimal floating point. */
2027 const char *cstr = str;
2028 mpfr_t m;
2029 bool inexact;
2031 while (*cstr == '0')
2032 cstr++;
2033 if (*cstr == '.')
2035 cstr++;
2036 while (*cstr == '0')
2037 cstr++;
2040 /* If the mantissa is zero, ignore the exponent. */
2041 if (!ISDIGIT (*cstr))
2042 goto is_a_zero;
2044 /* Nonzero value, possibly overflowing or underflowing. */
2045 mpfr_init2 (m, SIGNIFICAND_BITS);
2046 inexact = mpfr_strtofr (m, str, NULL, 10, GMP_RNDZ);
2047 /* The result should never be a NaN, and because the rounding is
2048 toward zero should never be an infinity. */
2049 gcc_assert (!mpfr_nan_p (m) && !mpfr_inf_p (m));
2050 if (mpfr_zero_p (m) || mpfr_get_exp (m) < -MAX_EXP + 4)
2052 mpfr_clear (m);
2053 goto underflow;
2055 else if (mpfr_get_exp (m) > MAX_EXP - 4)
2057 mpfr_clear (m);
2058 goto overflow;
2060 else
2062 real_from_mpfr (r, m, NULL_TREE, GMP_RNDZ);
2063 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2064 because the hex digits used in real_from_mpfr did not
2065 start with a digit 8 to f, but the exponent bounds above
2066 should have avoided underflow or overflow. */
2067 gcc_assert (r->cl = rvc_normal);
2068 /* Set a sticky bit if mpfr_strtofr was inexact. */
2069 r->sig[0] |= inexact;
2070 mpfr_clear (m);
2074 r->sign = sign;
2075 return 0;
2077 is_a_zero:
2078 get_zero (r, sign);
2079 return 0;
2081 underflow:
2082 get_zero (r, sign);
2083 return -1;
2085 overflow:
2086 get_inf (r, sign);
2087 return 1;
2090 /* Legacy. Similar, but return the result directly. */
2092 REAL_VALUE_TYPE
2093 real_from_string2 (const char *s, machine_mode mode)
2095 REAL_VALUE_TYPE r;
2097 real_from_string (&r, s);
2098 if (mode != VOIDmode)
2099 real_convert (&r, mode, &r);
2101 return r;
2104 /* Initialize R from string S and desired MODE. */
2106 void
2107 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, machine_mode mode)
2109 if (DECIMAL_FLOAT_MODE_P (mode))
2110 decimal_real_from_string (r, s);
2111 else
2112 real_from_string (r, s);
2114 if (mode != VOIDmode)
2115 real_convert (r, mode, r);
2118 /* Initialize R from the wide_int VAL_IN. The MODE is not VOIDmode,*/
2120 void
2121 real_from_integer (REAL_VALUE_TYPE *r, machine_mode mode,
2122 const wide_int_ref &val_in, signop sgn)
2124 if (val_in == 0)
2125 get_zero (r, 0);
2126 else
2128 unsigned int len = val_in.get_precision ();
2129 int i, j, e = 0;
2130 int maxbitlen = MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT;
2131 const unsigned int realmax = (SIGNIFICAND_BITS / HOST_BITS_PER_WIDE_INT
2132 * HOST_BITS_PER_WIDE_INT);
2134 memset (r, 0, sizeof (*r));
2135 r->cl = rvc_normal;
2136 r->sign = wi::neg_p (val_in, sgn);
2138 /* We have to ensure we can negate the largest negative number. */
2139 wide_int val = wide_int::from (val_in, maxbitlen, sgn);
2141 if (r->sign)
2142 val = -val;
2144 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2145 won't work with precisions that are not a multiple of
2146 HOST_BITS_PER_WIDE_INT. */
2147 len += HOST_BITS_PER_WIDE_INT - 1;
2149 /* Ensure we can represent the largest negative number. */
2150 len += 1;
2152 len = len/HOST_BITS_PER_WIDE_INT * HOST_BITS_PER_WIDE_INT;
2154 /* Cap the size to the size allowed by real.h. */
2155 if (len > realmax)
2157 HOST_WIDE_INT cnt_l_z;
2158 cnt_l_z = wi::clz (val);
2160 if (maxbitlen - cnt_l_z > realmax)
2162 e = maxbitlen - cnt_l_z - realmax;
2164 /* This value is too large, we must shift it right to
2165 preserve all the bits we can, and then bump the
2166 exponent up by that amount. */
2167 val = wi::lrshift (val, e);
2169 len = realmax;
2172 /* Clear out top bits so elt will work with precisions that aren't
2173 a multiple of HOST_BITS_PER_WIDE_INT. */
2174 val = wide_int::from (val, len, sgn);
2175 len = len / HOST_BITS_PER_WIDE_INT;
2177 SET_REAL_EXP (r, len * HOST_BITS_PER_WIDE_INT + e);
2179 j = SIGSZ - 1;
2180 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2181 for (i = len - 1; i >= 0; i--)
2183 r->sig[j--] = val.elt (i);
2184 if (j < 0)
2185 break;
2187 else
2189 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2190 for (i = len - 1; i >= 0; i--)
2192 HOST_WIDE_INT e = val.elt (i);
2193 r->sig[j--] = e >> (HOST_BITS_PER_LONG - 1) >> 1;
2194 if (j < 0)
2195 break;
2196 r->sig[j--] = e;
2197 if (j < 0)
2198 break;
2202 normalize (r);
2205 if (DECIMAL_FLOAT_MODE_P (mode))
2206 decimal_from_integer (r);
2207 else if (mode != VOIDmode)
2208 real_convert (r, mode, r);
2211 /* Render R, an integral value, as a floating point constant with no
2212 specified exponent. */
2214 static void
2215 decimal_integer_string (char *str, const REAL_VALUE_TYPE *r_orig,
2216 size_t buf_size)
2218 int dec_exp, digit, digits;
2219 REAL_VALUE_TYPE r, pten;
2220 char *p;
2221 bool sign;
2223 r = *r_orig;
2225 if (r.cl == rvc_zero)
2227 strcpy (str, "0.");
2228 return;
2231 sign = r.sign;
2232 r.sign = 0;
2234 dec_exp = REAL_EXP (&r) * M_LOG10_2;
2235 digits = dec_exp + 1;
2236 gcc_assert ((digits + 2) < (int)buf_size);
2238 pten = *real_digit (1);
2239 times_pten (&pten, dec_exp);
2241 p = str;
2242 if (sign)
2243 *p++ = '-';
2245 digit = rtd_divmod (&r, &pten);
2246 gcc_assert (digit >= 0 && digit <= 9);
2247 *p++ = digit + '0';
2248 while (--digits > 0)
2250 times_pten (&r, 1);
2251 digit = rtd_divmod (&r, &pten);
2252 *p++ = digit + '0';
2254 *p++ = '.';
2255 *p++ = '\0';
2258 /* Convert a real with an integral value to decimal float. */
2260 static void
2261 decimal_from_integer (REAL_VALUE_TYPE *r)
2263 char str[256];
2265 decimal_integer_string (str, r, sizeof (str) - 1);
2266 decimal_real_from_string (r, str);
2269 /* Returns 10**2**N. */
2271 static const REAL_VALUE_TYPE *
2272 ten_to_ptwo (int n)
2274 static REAL_VALUE_TYPE tens[EXP_BITS];
2276 gcc_assert (n >= 0);
2277 gcc_assert (n < EXP_BITS);
2279 if (tens[n].cl == rvc_zero)
2281 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2283 HOST_WIDE_INT t = 10;
2284 int i;
2286 for (i = 0; i < n; ++i)
2287 t *= t;
2289 real_from_integer (&tens[n], VOIDmode, t, UNSIGNED);
2291 else
2293 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2294 do_multiply (&tens[n], t, t);
2298 return &tens[n];
2301 /* Returns 10**(-2**N). */
2303 static const REAL_VALUE_TYPE *
2304 ten_to_mptwo (int n)
2306 static REAL_VALUE_TYPE tens[EXP_BITS];
2308 gcc_assert (n >= 0);
2309 gcc_assert (n < EXP_BITS);
2311 if (tens[n].cl == rvc_zero)
2312 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2314 return &tens[n];
2317 /* Returns N. */
2319 static const REAL_VALUE_TYPE *
2320 real_digit (int n)
2322 static REAL_VALUE_TYPE num[10];
2324 gcc_assert (n >= 0);
2325 gcc_assert (n <= 9);
2327 if (n > 0 && num[n].cl == rvc_zero)
2328 real_from_integer (&num[n], VOIDmode, n, UNSIGNED);
2330 return &num[n];
2333 /* Multiply R by 10**EXP. */
2335 static void
2336 times_pten (REAL_VALUE_TYPE *r, int exp)
2338 REAL_VALUE_TYPE pten, *rr;
2339 bool negative = (exp < 0);
2340 int i;
2342 if (negative)
2344 exp = -exp;
2345 pten = *real_digit (1);
2346 rr = &pten;
2348 else
2349 rr = r;
2351 for (i = 0; exp > 0; ++i, exp >>= 1)
2352 if (exp & 1)
2353 do_multiply (rr, rr, ten_to_ptwo (i));
2355 if (negative)
2356 do_divide (r, r, &pten);
2359 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2361 const REAL_VALUE_TYPE *
2362 dconst_e_ptr (void)
2364 static REAL_VALUE_TYPE value;
2366 /* Initialize mathematical constants for constant folding builtins.
2367 These constants need to be given to at least 160 bits precision. */
2368 if (value.cl == rvc_zero)
2370 mpfr_t m;
2371 mpfr_init2 (m, SIGNIFICAND_BITS);
2372 mpfr_set_ui (m, 1, GMP_RNDN);
2373 mpfr_exp (m, m, GMP_RNDN);
2374 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2375 mpfr_clear (m);
2378 return &value;
2381 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
2383 const REAL_VALUE_TYPE *
2384 dconst_third_ptr (void)
2386 static REAL_VALUE_TYPE value;
2388 /* Initialize mathematical constants for constant folding builtins.
2389 These constants need to be given to at least 160 bits precision. */
2390 if (value.cl == rvc_zero)
2392 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (3));
2394 return &value;
2397 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2399 const REAL_VALUE_TYPE *
2400 dconst_sqrt2_ptr (void)
2402 static REAL_VALUE_TYPE value;
2404 /* Initialize mathematical constants for constant folding builtins.
2405 These constants need to be given to at least 160 bits precision. */
2406 if (value.cl == rvc_zero)
2408 mpfr_t m;
2409 mpfr_init2 (m, SIGNIFICAND_BITS);
2410 mpfr_sqrt_ui (m, 2, GMP_RNDN);
2411 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2412 mpfr_clear (m);
2414 return &value;
2417 /* Fills R with +Inf. */
2419 void
2420 real_inf (REAL_VALUE_TYPE *r)
2422 get_inf (r, 0);
2425 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2426 we force a QNaN, else we force an SNaN. The string, if not empty,
2427 is parsed as a number and placed in the significand. Return true
2428 if the string was successfully parsed. */
2430 bool
2431 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2432 machine_mode mode)
2434 const struct real_format *fmt;
2436 fmt = REAL_MODE_FORMAT (mode);
2437 gcc_assert (fmt);
2439 if (*str == 0)
2441 if (quiet)
2442 get_canonical_qnan (r, 0);
2443 else
2444 get_canonical_snan (r, 0);
2446 else
2448 int base = 10, d;
2450 memset (r, 0, sizeof (*r));
2451 r->cl = rvc_nan;
2453 /* Parse akin to strtol into the significand of R. */
2455 while (ISSPACE (*str))
2456 str++;
2457 if (*str == '-')
2458 str++;
2459 else if (*str == '+')
2460 str++;
2461 if (*str == '0')
2463 str++;
2464 if (*str == 'x' || *str == 'X')
2466 base = 16;
2467 str++;
2469 else
2470 base = 8;
2473 while ((d = hex_value (*str)) < base)
2475 REAL_VALUE_TYPE u;
2477 switch (base)
2479 case 8:
2480 lshift_significand (r, r, 3);
2481 break;
2482 case 16:
2483 lshift_significand (r, r, 4);
2484 break;
2485 case 10:
2486 lshift_significand_1 (&u, r);
2487 lshift_significand (r, r, 3);
2488 add_significands (r, r, &u);
2489 break;
2490 default:
2491 gcc_unreachable ();
2494 get_zero (&u, 0);
2495 u.sig[0] = d;
2496 add_significands (r, r, &u);
2498 str++;
2501 /* Must have consumed the entire string for success. */
2502 if (*str != 0)
2503 return false;
2505 /* Shift the significand into place such that the bits
2506 are in the most significant bits for the format. */
2507 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2509 /* Our MSB is always unset for NaNs. */
2510 r->sig[SIGSZ-1] &= ~SIG_MSB;
2512 /* Force quiet or signalling NaN. */
2513 r->signalling = !quiet;
2516 return true;
2519 /* Fills R with the largest finite value representable in mode MODE.
2520 If SIGN is nonzero, R is set to the most negative finite value. */
2522 void
2523 real_maxval (REAL_VALUE_TYPE *r, int sign, machine_mode mode)
2525 const struct real_format *fmt;
2526 int np2;
2528 fmt = REAL_MODE_FORMAT (mode);
2529 gcc_assert (fmt);
2530 memset (r, 0, sizeof (*r));
2532 if (fmt->b == 10)
2533 decimal_real_maxval (r, sign, mode);
2534 else
2536 r->cl = rvc_normal;
2537 r->sign = sign;
2538 SET_REAL_EXP (r, fmt->emax);
2540 np2 = SIGNIFICAND_BITS - fmt->p;
2541 memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2542 clear_significand_below (r, np2);
2544 if (fmt->pnan < fmt->p)
2545 /* This is an IBM extended double format made up of two IEEE
2546 doubles. The value of the long double is the sum of the
2547 values of the two parts. The most significant part is
2548 required to be the value of the long double rounded to the
2549 nearest double. Rounding means we need a slightly smaller
2550 value for LDBL_MAX. */
2551 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1);
2555 /* Fills R with 2**N. */
2557 void
2558 real_2expN (REAL_VALUE_TYPE *r, int n, machine_mode fmode)
2560 memset (r, 0, sizeof (*r));
2562 n++;
2563 if (n > MAX_EXP)
2564 r->cl = rvc_inf;
2565 else if (n < -MAX_EXP)
2567 else
2569 r->cl = rvc_normal;
2570 SET_REAL_EXP (r, n);
2571 r->sig[SIGSZ-1] = SIG_MSB;
2573 if (DECIMAL_FLOAT_MODE_P (fmode))
2574 decimal_real_convert (r, fmode, r);
2578 static void
2579 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2581 int p2, np2, i, w;
2582 int emin2m1, emax2;
2583 bool round_up = false;
2585 if (r->decimal)
2587 if (fmt->b == 10)
2589 decimal_round_for_format (fmt, r);
2590 return;
2592 /* FIXME. We can come here via fp_easy_constant
2593 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2594 investigated whether this convert needs to be here, or
2595 something else is missing. */
2596 decimal_real_convert (r, DFmode, r);
2599 p2 = fmt->p;
2600 emin2m1 = fmt->emin - 1;
2601 emax2 = fmt->emax;
2603 np2 = SIGNIFICAND_BITS - p2;
2604 switch (r->cl)
2606 underflow:
2607 get_zero (r, r->sign);
2608 case rvc_zero:
2609 if (!fmt->has_signed_zero)
2610 r->sign = 0;
2611 return;
2613 overflow:
2614 get_inf (r, r->sign);
2615 case rvc_inf:
2616 return;
2618 case rvc_nan:
2619 clear_significand_below (r, np2);
2620 return;
2622 case rvc_normal:
2623 break;
2625 default:
2626 gcc_unreachable ();
2629 /* Check the range of the exponent. If we're out of range,
2630 either underflow or overflow. */
2631 if (REAL_EXP (r) > emax2)
2632 goto overflow;
2633 else if (REAL_EXP (r) <= emin2m1)
2635 int diff;
2637 if (!fmt->has_denorm)
2639 /* Don't underflow completely until we've had a chance to round. */
2640 if (REAL_EXP (r) < emin2m1)
2641 goto underflow;
2643 else
2645 diff = emin2m1 - REAL_EXP (r) + 1;
2646 if (diff > p2)
2647 goto underflow;
2649 /* De-normalize the significand. */
2650 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2651 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2655 if (!fmt->round_towards_zero)
2657 /* There are P2 true significand bits, followed by one guard bit,
2658 followed by one sticky bit, followed by stuff. Fold nonzero
2659 stuff into the sticky bit. */
2660 unsigned long sticky;
2661 bool guard, lsb;
2663 sticky = 0;
2664 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2665 sticky |= r->sig[i];
2666 sticky |= r->sig[w]
2667 & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2669 guard = test_significand_bit (r, np2 - 1);
2670 lsb = test_significand_bit (r, np2);
2672 /* Round to even. */
2673 round_up = guard && (sticky || lsb);
2676 if (round_up)
2678 REAL_VALUE_TYPE u;
2679 get_zero (&u, 0);
2680 set_significand_bit (&u, np2);
2682 if (add_significands (r, r, &u))
2684 /* Overflow. Means the significand had been all ones, and
2685 is now all zeros. Need to increase the exponent, and
2686 possibly re-normalize it. */
2687 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2688 if (REAL_EXP (r) > emax2)
2689 goto overflow;
2690 r->sig[SIGSZ-1] = SIG_MSB;
2694 /* Catch underflow that we deferred until after rounding. */
2695 if (REAL_EXP (r) <= emin2m1)
2696 goto underflow;
2698 /* Clear out trailing garbage. */
2699 clear_significand_below (r, np2);
2702 /* Extend or truncate to a new mode. */
2704 void
2705 real_convert (REAL_VALUE_TYPE *r, machine_mode mode,
2706 const REAL_VALUE_TYPE *a)
2708 const struct real_format *fmt;
2710 fmt = REAL_MODE_FORMAT (mode);
2711 gcc_assert (fmt);
2713 *r = *a;
2715 if (a->decimal || fmt->b == 10)
2716 decimal_real_convert (r, mode, a);
2718 round_for_format (fmt, r);
2720 /* round_for_format de-normalizes denormals. Undo just that part. */
2721 if (r->cl == rvc_normal)
2722 normalize (r);
2725 /* Legacy. Likewise, except return the struct directly. */
2727 REAL_VALUE_TYPE
2728 real_value_truncate (machine_mode mode, REAL_VALUE_TYPE a)
2730 REAL_VALUE_TYPE r;
2731 real_convert (&r, mode, &a);
2732 return r;
2735 /* Return true if truncating to MODE is exact. */
2737 bool
2738 exact_real_truncate (machine_mode mode, const REAL_VALUE_TYPE *a)
2740 const struct real_format *fmt;
2741 REAL_VALUE_TYPE t;
2742 int emin2m1;
2744 fmt = REAL_MODE_FORMAT (mode);
2745 gcc_assert (fmt);
2747 /* Don't allow conversion to denormals. */
2748 emin2m1 = fmt->emin - 1;
2749 if (REAL_EXP (a) <= emin2m1)
2750 return false;
2752 /* After conversion to the new mode, the value must be identical. */
2753 real_convert (&t, mode, a);
2754 return real_identical (&t, a);
2757 /* Write R to the given target format. Place the words of the result
2758 in target word order in BUF. There are always 32 bits in each
2759 long, no matter the size of the host long.
2761 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2763 long
2764 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
2765 const struct real_format *fmt)
2767 REAL_VALUE_TYPE r;
2768 long buf1;
2770 r = *r_orig;
2771 round_for_format (fmt, &r);
2773 if (!buf)
2774 buf = &buf1;
2775 (*fmt->encode) (fmt, buf, &r);
2777 return *buf;
2780 /* Similar, but look up the format from MODE. */
2782 long
2783 real_to_target (long *buf, const REAL_VALUE_TYPE *r, machine_mode mode)
2785 const struct real_format *fmt;
2787 fmt = REAL_MODE_FORMAT (mode);
2788 gcc_assert (fmt);
2790 return real_to_target_fmt (buf, r, fmt);
2793 /* Read R from the given target format. Read the words of the result
2794 in target word order in BUF. There are always 32 bits in each
2795 long, no matter the size of the host long. */
2797 void
2798 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
2799 const struct real_format *fmt)
2801 (*fmt->decode) (fmt, r, buf);
2804 /* Similar, but look up the format from MODE. */
2806 void
2807 real_from_target (REAL_VALUE_TYPE *r, const long *buf, machine_mode mode)
2809 const struct real_format *fmt;
2811 fmt = REAL_MODE_FORMAT (mode);
2812 gcc_assert (fmt);
2814 (*fmt->decode) (fmt, r, buf);
2817 /* Return the number of bits of the largest binary value that the
2818 significand of MODE will hold. */
2819 /* ??? Legacy. Should get access to real_format directly. */
2822 significand_size (machine_mode mode)
2824 const struct real_format *fmt;
2826 fmt = REAL_MODE_FORMAT (mode);
2827 if (fmt == NULL)
2828 return 0;
2830 if (fmt->b == 10)
2832 /* Return the size in bits of the largest binary value that can be
2833 held by the decimal coefficient for this mode. This is one more
2834 than the number of bits required to hold the largest coefficient
2835 of this mode. */
2836 double log2_10 = 3.3219281;
2837 return fmt->p * log2_10;
2839 return fmt->p;
2842 /* Return a hash value for the given real value. */
2843 /* ??? The "unsigned int" return value is intended to be hashval_t,
2844 but I didn't want to pull hashtab.h into real.h. */
2846 unsigned int
2847 real_hash (const REAL_VALUE_TYPE *r)
2849 unsigned int h;
2850 size_t i;
2852 h = r->cl | (r->sign << 2);
2853 switch (r->cl)
2855 case rvc_zero:
2856 case rvc_inf:
2857 return h;
2859 case rvc_normal:
2860 h |= REAL_EXP (r) << 3;
2861 break;
2863 case rvc_nan:
2864 if (r->signalling)
2865 h ^= (unsigned int)-1;
2866 if (r->canonical)
2867 return h;
2868 break;
2870 default:
2871 gcc_unreachable ();
2874 if (sizeof (unsigned long) > sizeof (unsigned int))
2875 for (i = 0; i < SIGSZ; ++i)
2877 unsigned long s = r->sig[i];
2878 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2880 else
2881 for (i = 0; i < SIGSZ; ++i)
2882 h ^= r->sig[i];
2884 return h;
2887 /* IEEE single-precision format. */
2889 static void encode_ieee_single (const struct real_format *fmt,
2890 long *, const REAL_VALUE_TYPE *);
2891 static void decode_ieee_single (const struct real_format *,
2892 REAL_VALUE_TYPE *, const long *);
2894 static void
2895 encode_ieee_single (const struct real_format *fmt, long *buf,
2896 const REAL_VALUE_TYPE *r)
2898 unsigned long image, sig, exp;
2899 unsigned long sign = r->sign;
2900 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2902 image = sign << 31;
2903 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2905 switch (r->cl)
2907 case rvc_zero:
2908 break;
2910 case rvc_inf:
2911 if (fmt->has_inf)
2912 image |= 255 << 23;
2913 else
2914 image |= 0x7fffffff;
2915 break;
2917 case rvc_nan:
2918 if (fmt->has_nans)
2920 if (r->canonical)
2921 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
2922 if (r->signalling == fmt->qnan_msb_set)
2923 sig &= ~(1 << 22);
2924 else
2925 sig |= 1 << 22;
2926 if (sig == 0)
2927 sig = 1 << 21;
2929 image |= 255 << 23;
2930 image |= sig;
2932 else
2933 image |= 0x7fffffff;
2934 break;
2936 case rvc_normal:
2937 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2938 whereas the intermediate representation is 0.F x 2**exp.
2939 Which means we're off by one. */
2940 if (denormal)
2941 exp = 0;
2942 else
2943 exp = REAL_EXP (r) + 127 - 1;
2944 image |= exp << 23;
2945 image |= sig;
2946 break;
2948 default:
2949 gcc_unreachable ();
2952 buf[0] = image;
2955 static void
2956 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2957 const long *buf)
2959 unsigned long image = buf[0] & 0xffffffff;
2960 bool sign = (image >> 31) & 1;
2961 int exp = (image >> 23) & 0xff;
2963 memset (r, 0, sizeof (*r));
2964 image <<= HOST_BITS_PER_LONG - 24;
2965 image &= ~SIG_MSB;
2967 if (exp == 0)
2969 if (image && fmt->has_denorm)
2971 r->cl = rvc_normal;
2972 r->sign = sign;
2973 SET_REAL_EXP (r, -126);
2974 r->sig[SIGSZ-1] = image << 1;
2975 normalize (r);
2977 else if (fmt->has_signed_zero)
2978 r->sign = sign;
2980 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2982 if (image)
2984 r->cl = rvc_nan;
2985 r->sign = sign;
2986 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
2987 ^ fmt->qnan_msb_set);
2988 r->sig[SIGSZ-1] = image;
2990 else
2992 r->cl = rvc_inf;
2993 r->sign = sign;
2996 else
2998 r->cl = rvc_normal;
2999 r->sign = sign;
3000 SET_REAL_EXP (r, exp - 127 + 1);
3001 r->sig[SIGSZ-1] = image | SIG_MSB;
3005 const struct real_format ieee_single_format =
3007 encode_ieee_single,
3008 decode_ieee_single,
3012 -125,
3013 128,
3016 false,
3017 true,
3018 true,
3019 true,
3020 true,
3021 true,
3022 true,
3023 false
3026 const struct real_format mips_single_format =
3028 encode_ieee_single,
3029 decode_ieee_single,
3033 -125,
3034 128,
3037 false,
3038 true,
3039 true,
3040 true,
3041 true,
3042 true,
3043 false,
3044 true
3047 const struct real_format motorola_single_format =
3049 encode_ieee_single,
3050 decode_ieee_single,
3054 -125,
3055 128,
3058 false,
3059 true,
3060 true,
3061 true,
3062 true,
3063 true,
3064 true,
3065 true
3068 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3069 single precision with the following differences:
3070 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3071 are generated.
3072 - NaNs are not supported.
3073 - The range of non-zero numbers in binary is
3074 (001)[1.]000...000 to (255)[1.]111...111.
3075 - Denormals can be represented, but are treated as +0.0 when
3076 used as an operand and are never generated as a result.
3077 - -0.0 can be represented, but a zero result is always +0.0.
3078 - the only supported rounding mode is trunction (towards zero). */
3079 const struct real_format spu_single_format =
3081 encode_ieee_single,
3082 decode_ieee_single,
3086 -125,
3087 129,
3090 true,
3091 false,
3092 false,
3093 false,
3094 true,
3095 true,
3096 false,
3097 false
3100 /* IEEE double-precision format. */
3102 static void encode_ieee_double (const struct real_format *fmt,
3103 long *, const REAL_VALUE_TYPE *);
3104 static void decode_ieee_double (const struct real_format *,
3105 REAL_VALUE_TYPE *, const long *);
3107 static void
3108 encode_ieee_double (const struct real_format *fmt, long *buf,
3109 const REAL_VALUE_TYPE *r)
3111 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
3112 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3114 image_hi = r->sign << 31;
3115 image_lo = 0;
3117 if (HOST_BITS_PER_LONG == 64)
3119 sig_hi = r->sig[SIGSZ-1];
3120 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
3121 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
3123 else
3125 sig_hi = r->sig[SIGSZ-1];
3126 sig_lo = r->sig[SIGSZ-2];
3127 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
3128 sig_hi = (sig_hi >> 11) & 0xfffff;
3131 switch (r->cl)
3133 case rvc_zero:
3134 break;
3136 case rvc_inf:
3137 if (fmt->has_inf)
3138 image_hi |= 2047 << 20;
3139 else
3141 image_hi |= 0x7fffffff;
3142 image_lo = 0xffffffff;
3144 break;
3146 case rvc_nan:
3147 if (fmt->has_nans)
3149 if (r->canonical)
3151 if (fmt->canonical_nan_lsbs_set)
3153 sig_hi = (1 << 19) - 1;
3154 sig_lo = 0xffffffff;
3156 else
3158 sig_hi = 0;
3159 sig_lo = 0;
3162 if (r->signalling == fmt->qnan_msb_set)
3163 sig_hi &= ~(1 << 19);
3164 else
3165 sig_hi |= 1 << 19;
3166 if (sig_hi == 0 && sig_lo == 0)
3167 sig_hi = 1 << 18;
3169 image_hi |= 2047 << 20;
3170 image_hi |= sig_hi;
3171 image_lo = sig_lo;
3173 else
3175 image_hi |= 0x7fffffff;
3176 image_lo = 0xffffffff;
3178 break;
3180 case rvc_normal:
3181 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3182 whereas the intermediate representation is 0.F x 2**exp.
3183 Which means we're off by one. */
3184 if (denormal)
3185 exp = 0;
3186 else
3187 exp = REAL_EXP (r) + 1023 - 1;
3188 image_hi |= exp << 20;
3189 image_hi |= sig_hi;
3190 image_lo = sig_lo;
3191 break;
3193 default:
3194 gcc_unreachable ();
3197 if (FLOAT_WORDS_BIG_ENDIAN)
3198 buf[0] = image_hi, buf[1] = image_lo;
3199 else
3200 buf[0] = image_lo, buf[1] = image_hi;
3203 static void
3204 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3205 const long *buf)
3207 unsigned long image_hi, image_lo;
3208 bool sign;
3209 int exp;
3211 if (FLOAT_WORDS_BIG_ENDIAN)
3212 image_hi = buf[0], image_lo = buf[1];
3213 else
3214 image_lo = buf[0], image_hi = buf[1];
3215 image_lo &= 0xffffffff;
3216 image_hi &= 0xffffffff;
3218 sign = (image_hi >> 31) & 1;
3219 exp = (image_hi >> 20) & 0x7ff;
3221 memset (r, 0, sizeof (*r));
3223 image_hi <<= 32 - 21;
3224 image_hi |= image_lo >> 21;
3225 image_hi &= 0x7fffffff;
3226 image_lo <<= 32 - 21;
3228 if (exp == 0)
3230 if ((image_hi || image_lo) && fmt->has_denorm)
3232 r->cl = rvc_normal;
3233 r->sign = sign;
3234 SET_REAL_EXP (r, -1022);
3235 if (HOST_BITS_PER_LONG == 32)
3237 image_hi = (image_hi << 1) | (image_lo >> 31);
3238 image_lo <<= 1;
3239 r->sig[SIGSZ-1] = image_hi;
3240 r->sig[SIGSZ-2] = image_lo;
3242 else
3244 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
3245 r->sig[SIGSZ-1] = image_hi;
3247 normalize (r);
3249 else if (fmt->has_signed_zero)
3250 r->sign = sign;
3252 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
3254 if (image_hi || image_lo)
3256 r->cl = rvc_nan;
3257 r->sign = sign;
3258 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3259 if (HOST_BITS_PER_LONG == 32)
3261 r->sig[SIGSZ-1] = image_hi;
3262 r->sig[SIGSZ-2] = image_lo;
3264 else
3265 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3267 else
3269 r->cl = rvc_inf;
3270 r->sign = sign;
3273 else
3275 r->cl = rvc_normal;
3276 r->sign = sign;
3277 SET_REAL_EXP (r, exp - 1023 + 1);
3278 if (HOST_BITS_PER_LONG == 32)
3280 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3281 r->sig[SIGSZ-2] = image_lo;
3283 else
3284 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3288 const struct real_format ieee_double_format =
3290 encode_ieee_double,
3291 decode_ieee_double,
3295 -1021,
3296 1024,
3299 false,
3300 true,
3301 true,
3302 true,
3303 true,
3304 true,
3305 true,
3306 false
3309 const struct real_format mips_double_format =
3311 encode_ieee_double,
3312 decode_ieee_double,
3316 -1021,
3317 1024,
3320 false,
3321 true,
3322 true,
3323 true,
3324 true,
3325 true,
3326 false,
3327 true
3330 const struct real_format motorola_double_format =
3332 encode_ieee_double,
3333 decode_ieee_double,
3337 -1021,
3338 1024,
3341 false,
3342 true,
3343 true,
3344 true,
3345 true,
3346 true,
3347 true,
3348 true
3351 /* IEEE extended real format. This comes in three flavors: Intel's as
3352 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3353 12- and 16-byte images may be big- or little endian; Motorola's is
3354 always big endian. */
3356 /* Helper subroutine which converts from the internal format to the
3357 12-byte little-endian Intel format. Functions below adjust this
3358 for the other possible formats. */
3359 static void
3360 encode_ieee_extended (const struct real_format *fmt, long *buf,
3361 const REAL_VALUE_TYPE *r)
3363 unsigned long image_hi, sig_hi, sig_lo;
3364 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3366 image_hi = r->sign << 15;
3367 sig_hi = sig_lo = 0;
3369 switch (r->cl)
3371 case rvc_zero:
3372 break;
3374 case rvc_inf:
3375 if (fmt->has_inf)
3377 image_hi |= 32767;
3379 /* Intel requires the explicit integer bit to be set, otherwise
3380 it considers the value a "pseudo-infinity". Motorola docs
3381 say it doesn't care. */
3382 sig_hi = 0x80000000;
3384 else
3386 image_hi |= 32767;
3387 sig_lo = sig_hi = 0xffffffff;
3389 break;
3391 case rvc_nan:
3392 if (fmt->has_nans)
3394 image_hi |= 32767;
3395 if (r->canonical)
3397 if (fmt->canonical_nan_lsbs_set)
3399 sig_hi = (1 << 30) - 1;
3400 sig_lo = 0xffffffff;
3403 else if (HOST_BITS_PER_LONG == 32)
3405 sig_hi = r->sig[SIGSZ-1];
3406 sig_lo = r->sig[SIGSZ-2];
3408 else
3410 sig_lo = r->sig[SIGSZ-1];
3411 sig_hi = sig_lo >> 31 >> 1;
3412 sig_lo &= 0xffffffff;
3414 if (r->signalling == fmt->qnan_msb_set)
3415 sig_hi &= ~(1 << 30);
3416 else
3417 sig_hi |= 1 << 30;
3418 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3419 sig_hi = 1 << 29;
3421 /* Intel requires the explicit integer bit to be set, otherwise
3422 it considers the value a "pseudo-nan". Motorola docs say it
3423 doesn't care. */
3424 sig_hi |= 0x80000000;
3426 else
3428 image_hi |= 32767;
3429 sig_lo = sig_hi = 0xffffffff;
3431 break;
3433 case rvc_normal:
3435 int exp = REAL_EXP (r);
3437 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3438 whereas the intermediate representation is 0.F x 2**exp.
3439 Which means we're off by one.
3441 Except for Motorola, which consider exp=0 and explicit
3442 integer bit set to continue to be normalized. In theory
3443 this discrepancy has been taken care of by the difference
3444 in fmt->emin in round_for_format. */
3446 if (denormal)
3447 exp = 0;
3448 else
3450 exp += 16383 - 1;
3451 gcc_assert (exp >= 0);
3453 image_hi |= exp;
3455 if (HOST_BITS_PER_LONG == 32)
3457 sig_hi = r->sig[SIGSZ-1];
3458 sig_lo = r->sig[SIGSZ-2];
3460 else
3462 sig_lo = r->sig[SIGSZ-1];
3463 sig_hi = sig_lo >> 31 >> 1;
3464 sig_lo &= 0xffffffff;
3467 break;
3469 default:
3470 gcc_unreachable ();
3473 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3476 /* Convert from the internal format to the 12-byte Motorola format
3477 for an IEEE extended real. */
3478 static void
3479 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3480 const REAL_VALUE_TYPE *r)
3482 long intermed[3];
3483 encode_ieee_extended (fmt, intermed, r);
3485 if (r->cl == rvc_inf)
3486 /* For infinity clear the explicit integer bit again, so that the
3487 format matches the canonical infinity generated by the FPU. */
3488 intermed[1] = 0;
3490 /* Motorola chips are assumed always to be big-endian. Also, the
3491 padding in a Motorola extended real goes between the exponent and
3492 the mantissa. At this point the mantissa is entirely within
3493 elements 0 and 1 of intermed, and the exponent entirely within
3494 element 2, so all we have to do is swap the order around, and
3495 shift element 2 left 16 bits. */
3496 buf[0] = intermed[2] << 16;
3497 buf[1] = intermed[1];
3498 buf[2] = intermed[0];
3501 /* Convert from the internal format to the 12-byte Intel format for
3502 an IEEE extended real. */
3503 static void
3504 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3505 const REAL_VALUE_TYPE *r)
3507 if (FLOAT_WORDS_BIG_ENDIAN)
3509 /* All the padding in an Intel-format extended real goes at the high
3510 end, which in this case is after the mantissa, not the exponent.
3511 Therefore we must shift everything down 16 bits. */
3512 long intermed[3];
3513 encode_ieee_extended (fmt, intermed, r);
3514 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3515 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3516 buf[2] = (intermed[0] << 16);
3518 else
3519 /* encode_ieee_extended produces what we want directly. */
3520 encode_ieee_extended (fmt, buf, r);
3523 /* Convert from the internal format to the 16-byte Intel format for
3524 an IEEE extended real. */
3525 static void
3526 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3527 const REAL_VALUE_TYPE *r)
3529 /* All the padding in an Intel-format extended real goes at the high end. */
3530 encode_ieee_extended_intel_96 (fmt, buf, r);
3531 buf[3] = 0;
3534 /* As above, we have a helper function which converts from 12-byte
3535 little-endian Intel format to internal format. Functions below
3536 adjust for the other possible formats. */
3537 static void
3538 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3539 const long *buf)
3541 unsigned long image_hi, sig_hi, sig_lo;
3542 bool sign;
3543 int exp;
3545 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3546 sig_lo &= 0xffffffff;
3547 sig_hi &= 0xffffffff;
3548 image_hi &= 0xffffffff;
3550 sign = (image_hi >> 15) & 1;
3551 exp = image_hi & 0x7fff;
3553 memset (r, 0, sizeof (*r));
3555 if (exp == 0)
3557 if ((sig_hi || sig_lo) && fmt->has_denorm)
3559 r->cl = rvc_normal;
3560 r->sign = sign;
3562 /* When the IEEE format contains a hidden bit, we know that
3563 it's zero at this point, and so shift up the significand
3564 and decrease the exponent to match. In this case, Motorola
3565 defines the explicit integer bit to be valid, so we don't
3566 know whether the msb is set or not. */
3567 SET_REAL_EXP (r, fmt->emin);
3568 if (HOST_BITS_PER_LONG == 32)
3570 r->sig[SIGSZ-1] = sig_hi;
3571 r->sig[SIGSZ-2] = sig_lo;
3573 else
3574 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3576 normalize (r);
3578 else if (fmt->has_signed_zero)
3579 r->sign = sign;
3581 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3583 /* See above re "pseudo-infinities" and "pseudo-nans".
3584 Short summary is that the MSB will likely always be
3585 set, and that we don't care about it. */
3586 sig_hi &= 0x7fffffff;
3588 if (sig_hi || sig_lo)
3590 r->cl = rvc_nan;
3591 r->sign = sign;
3592 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3593 if (HOST_BITS_PER_LONG == 32)
3595 r->sig[SIGSZ-1] = sig_hi;
3596 r->sig[SIGSZ-2] = sig_lo;
3598 else
3599 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3601 else
3603 r->cl = rvc_inf;
3604 r->sign = sign;
3607 else
3609 r->cl = rvc_normal;
3610 r->sign = sign;
3611 SET_REAL_EXP (r, exp - 16383 + 1);
3612 if (HOST_BITS_PER_LONG == 32)
3614 r->sig[SIGSZ-1] = sig_hi;
3615 r->sig[SIGSZ-2] = sig_lo;
3617 else
3618 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3622 /* Convert from the internal format to the 12-byte Motorola format
3623 for an IEEE extended real. */
3624 static void
3625 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3626 const long *buf)
3628 long intermed[3];
3630 /* Motorola chips are assumed always to be big-endian. Also, the
3631 padding in a Motorola extended real goes between the exponent and
3632 the mantissa; remove it. */
3633 intermed[0] = buf[2];
3634 intermed[1] = buf[1];
3635 intermed[2] = (unsigned long)buf[0] >> 16;
3637 decode_ieee_extended (fmt, r, intermed);
3640 /* Convert from the internal format to the 12-byte Intel format for
3641 an IEEE extended real. */
3642 static void
3643 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3644 const long *buf)
3646 if (FLOAT_WORDS_BIG_ENDIAN)
3648 /* All the padding in an Intel-format extended real goes at the high
3649 end, which in this case is after the mantissa, not the exponent.
3650 Therefore we must shift everything up 16 bits. */
3651 long intermed[3];
3653 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3654 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3655 intermed[2] = ((unsigned long)buf[0] >> 16);
3657 decode_ieee_extended (fmt, r, intermed);
3659 else
3660 /* decode_ieee_extended produces what we want directly. */
3661 decode_ieee_extended (fmt, r, buf);
3664 /* Convert from the internal format to the 16-byte Intel format for
3665 an IEEE extended real. */
3666 static void
3667 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3668 const long *buf)
3670 /* All the padding in an Intel-format extended real goes at the high end. */
3671 decode_ieee_extended_intel_96 (fmt, r, buf);
3674 const struct real_format ieee_extended_motorola_format =
3676 encode_ieee_extended_motorola,
3677 decode_ieee_extended_motorola,
3681 -16382,
3682 16384,
3685 false,
3686 true,
3687 true,
3688 true,
3689 true,
3690 true,
3691 true,
3692 true
3695 const struct real_format ieee_extended_intel_96_format =
3697 encode_ieee_extended_intel_96,
3698 decode_ieee_extended_intel_96,
3702 -16381,
3703 16384,
3706 false,
3707 true,
3708 true,
3709 true,
3710 true,
3711 true,
3712 true,
3713 false
3716 const struct real_format ieee_extended_intel_128_format =
3718 encode_ieee_extended_intel_128,
3719 decode_ieee_extended_intel_128,
3723 -16381,
3724 16384,
3727 false,
3728 true,
3729 true,
3730 true,
3731 true,
3732 true,
3733 true,
3734 false
3737 /* The following caters to i386 systems that set the rounding precision
3738 to 53 bits instead of 64, e.g. FreeBSD. */
3739 const struct real_format ieee_extended_intel_96_round_53_format =
3741 encode_ieee_extended_intel_96,
3742 decode_ieee_extended_intel_96,
3746 -16381,
3747 16384,
3750 false,
3751 true,
3752 true,
3753 true,
3754 true,
3755 true,
3756 true,
3757 false
3760 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3761 numbers whose sum is equal to the extended precision value. The number
3762 with greater magnitude is first. This format has the same magnitude
3763 range as an IEEE double precision value, but effectively 106 bits of
3764 significand precision. Infinity and NaN are represented by their IEEE
3765 double precision value stored in the first number, the second number is
3766 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3768 static void encode_ibm_extended (const struct real_format *fmt,
3769 long *, const REAL_VALUE_TYPE *);
3770 static void decode_ibm_extended (const struct real_format *,
3771 REAL_VALUE_TYPE *, const long *);
3773 static void
3774 encode_ibm_extended (const struct real_format *fmt, long *buf,
3775 const REAL_VALUE_TYPE *r)
3777 REAL_VALUE_TYPE u, normr, v;
3778 const struct real_format *base_fmt;
3780 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3782 /* Renormalize R before doing any arithmetic on it. */
3783 normr = *r;
3784 if (normr.cl == rvc_normal)
3785 normalize (&normr);
3787 /* u = IEEE double precision portion of significand. */
3788 u = normr;
3789 round_for_format (base_fmt, &u);
3790 encode_ieee_double (base_fmt, &buf[0], &u);
3792 if (u.cl == rvc_normal)
3794 do_add (&v, &normr, &u, 1);
3795 /* Call round_for_format since we might need to denormalize. */
3796 round_for_format (base_fmt, &v);
3797 encode_ieee_double (base_fmt, &buf[2], &v);
3799 else
3801 /* Inf, NaN, 0 are all representable as doubles, so the
3802 least-significant part can be 0.0. */
3803 buf[2] = 0;
3804 buf[3] = 0;
3808 static void
3809 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3810 const long *buf)
3812 REAL_VALUE_TYPE u, v;
3813 const struct real_format *base_fmt;
3815 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3816 decode_ieee_double (base_fmt, &u, &buf[0]);
3818 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3820 decode_ieee_double (base_fmt, &v, &buf[2]);
3821 do_add (r, &u, &v, 0);
3823 else
3824 *r = u;
3827 const struct real_format ibm_extended_format =
3829 encode_ibm_extended,
3830 decode_ibm_extended,
3832 53 + 53,
3834 -1021 + 53,
3835 1024,
3836 127,
3838 false,
3839 true,
3840 true,
3841 true,
3842 true,
3843 true,
3844 true,
3845 false
3848 const struct real_format mips_extended_format =
3850 encode_ibm_extended,
3851 decode_ibm_extended,
3853 53 + 53,
3855 -1021 + 53,
3856 1024,
3857 127,
3859 false,
3860 true,
3861 true,
3862 true,
3863 true,
3864 true,
3865 false,
3866 true
3870 /* IEEE quad precision format. */
3872 static void encode_ieee_quad (const struct real_format *fmt,
3873 long *, const REAL_VALUE_TYPE *);
3874 static void decode_ieee_quad (const struct real_format *,
3875 REAL_VALUE_TYPE *, const long *);
3877 static void
3878 encode_ieee_quad (const struct real_format *fmt, long *buf,
3879 const REAL_VALUE_TYPE *r)
3881 unsigned long image3, image2, image1, image0, exp;
3882 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3883 REAL_VALUE_TYPE u;
3885 image3 = r->sign << 31;
3886 image2 = 0;
3887 image1 = 0;
3888 image0 = 0;
3890 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3892 switch (r->cl)
3894 case rvc_zero:
3895 break;
3897 case rvc_inf:
3898 if (fmt->has_inf)
3899 image3 |= 32767 << 16;
3900 else
3902 image3 |= 0x7fffffff;
3903 image2 = 0xffffffff;
3904 image1 = 0xffffffff;
3905 image0 = 0xffffffff;
3907 break;
3909 case rvc_nan:
3910 if (fmt->has_nans)
3912 image3 |= 32767 << 16;
3914 if (r->canonical)
3916 if (fmt->canonical_nan_lsbs_set)
3918 image3 |= 0x7fff;
3919 image2 = image1 = image0 = 0xffffffff;
3922 else if (HOST_BITS_PER_LONG == 32)
3924 image0 = u.sig[0];
3925 image1 = u.sig[1];
3926 image2 = u.sig[2];
3927 image3 |= u.sig[3] & 0xffff;
3929 else
3931 image0 = u.sig[0];
3932 image1 = image0 >> 31 >> 1;
3933 image2 = u.sig[1];
3934 image3 |= (image2 >> 31 >> 1) & 0xffff;
3935 image0 &= 0xffffffff;
3936 image2 &= 0xffffffff;
3938 if (r->signalling == fmt->qnan_msb_set)
3939 image3 &= ~0x8000;
3940 else
3941 image3 |= 0x8000;
3942 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3943 image3 |= 0x4000;
3945 else
3947 image3 |= 0x7fffffff;
3948 image2 = 0xffffffff;
3949 image1 = 0xffffffff;
3950 image0 = 0xffffffff;
3952 break;
3954 case rvc_normal:
3955 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3956 whereas the intermediate representation is 0.F x 2**exp.
3957 Which means we're off by one. */
3958 if (denormal)
3959 exp = 0;
3960 else
3961 exp = REAL_EXP (r) + 16383 - 1;
3962 image3 |= exp << 16;
3964 if (HOST_BITS_PER_LONG == 32)
3966 image0 = u.sig[0];
3967 image1 = u.sig[1];
3968 image2 = u.sig[2];
3969 image3 |= u.sig[3] & 0xffff;
3971 else
3973 image0 = u.sig[0];
3974 image1 = image0 >> 31 >> 1;
3975 image2 = u.sig[1];
3976 image3 |= (image2 >> 31 >> 1) & 0xffff;
3977 image0 &= 0xffffffff;
3978 image2 &= 0xffffffff;
3980 break;
3982 default:
3983 gcc_unreachable ();
3986 if (FLOAT_WORDS_BIG_ENDIAN)
3988 buf[0] = image3;
3989 buf[1] = image2;
3990 buf[2] = image1;
3991 buf[3] = image0;
3993 else
3995 buf[0] = image0;
3996 buf[1] = image1;
3997 buf[2] = image2;
3998 buf[3] = image3;
4002 static void
4003 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4004 const long *buf)
4006 unsigned long image3, image2, image1, image0;
4007 bool sign;
4008 int exp;
4010 if (FLOAT_WORDS_BIG_ENDIAN)
4012 image3 = buf[0];
4013 image2 = buf[1];
4014 image1 = buf[2];
4015 image0 = buf[3];
4017 else
4019 image0 = buf[0];
4020 image1 = buf[1];
4021 image2 = buf[2];
4022 image3 = buf[3];
4024 image0 &= 0xffffffff;
4025 image1 &= 0xffffffff;
4026 image2 &= 0xffffffff;
4028 sign = (image3 >> 31) & 1;
4029 exp = (image3 >> 16) & 0x7fff;
4030 image3 &= 0xffff;
4032 memset (r, 0, sizeof (*r));
4034 if (exp == 0)
4036 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
4038 r->cl = rvc_normal;
4039 r->sign = sign;
4041 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
4042 if (HOST_BITS_PER_LONG == 32)
4044 r->sig[0] = image0;
4045 r->sig[1] = image1;
4046 r->sig[2] = image2;
4047 r->sig[3] = image3;
4049 else
4051 r->sig[0] = (image1 << 31 << 1) | image0;
4052 r->sig[1] = (image3 << 31 << 1) | image2;
4055 normalize (r);
4057 else if (fmt->has_signed_zero)
4058 r->sign = sign;
4060 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
4062 if (image3 | image2 | image1 | image0)
4064 r->cl = rvc_nan;
4065 r->sign = sign;
4066 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
4068 if (HOST_BITS_PER_LONG == 32)
4070 r->sig[0] = image0;
4071 r->sig[1] = image1;
4072 r->sig[2] = image2;
4073 r->sig[3] = image3;
4075 else
4077 r->sig[0] = (image1 << 31 << 1) | image0;
4078 r->sig[1] = (image3 << 31 << 1) | image2;
4080 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4082 else
4084 r->cl = rvc_inf;
4085 r->sign = sign;
4088 else
4090 r->cl = rvc_normal;
4091 r->sign = sign;
4092 SET_REAL_EXP (r, exp - 16383 + 1);
4094 if (HOST_BITS_PER_LONG == 32)
4096 r->sig[0] = image0;
4097 r->sig[1] = image1;
4098 r->sig[2] = image2;
4099 r->sig[3] = image3;
4101 else
4103 r->sig[0] = (image1 << 31 << 1) | image0;
4104 r->sig[1] = (image3 << 31 << 1) | image2;
4106 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4107 r->sig[SIGSZ-1] |= SIG_MSB;
4111 const struct real_format ieee_quad_format =
4113 encode_ieee_quad,
4114 decode_ieee_quad,
4116 113,
4117 113,
4118 -16381,
4119 16384,
4120 127,
4121 127,
4122 false,
4123 true,
4124 true,
4125 true,
4126 true,
4127 true,
4128 true,
4129 false
4132 const struct real_format mips_quad_format =
4134 encode_ieee_quad,
4135 decode_ieee_quad,
4137 113,
4138 113,
4139 -16381,
4140 16384,
4141 127,
4142 127,
4143 false,
4144 true,
4145 true,
4146 true,
4147 true,
4148 true,
4149 false,
4150 true
4153 /* Descriptions of VAX floating point formats can be found beginning at
4155 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4157 The thing to remember is that they're almost IEEE, except for word
4158 order, exponent bias, and the lack of infinities, nans, and denormals.
4160 We don't implement the H_floating format here, simply because neither
4161 the VAX or Alpha ports use it. */
4163 static void encode_vax_f (const struct real_format *fmt,
4164 long *, const REAL_VALUE_TYPE *);
4165 static void decode_vax_f (const struct real_format *,
4166 REAL_VALUE_TYPE *, const long *);
4167 static void encode_vax_d (const struct real_format *fmt,
4168 long *, const REAL_VALUE_TYPE *);
4169 static void decode_vax_d (const struct real_format *,
4170 REAL_VALUE_TYPE *, const long *);
4171 static void encode_vax_g (const struct real_format *fmt,
4172 long *, const REAL_VALUE_TYPE *);
4173 static void decode_vax_g (const struct real_format *,
4174 REAL_VALUE_TYPE *, const long *);
4176 static void
4177 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4178 const REAL_VALUE_TYPE *r)
4180 unsigned long sign, exp, sig, image;
4182 sign = r->sign << 15;
4184 switch (r->cl)
4186 case rvc_zero:
4187 image = 0;
4188 break;
4190 case rvc_inf:
4191 case rvc_nan:
4192 image = 0xffff7fff | sign;
4193 break;
4195 case rvc_normal:
4196 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4197 exp = REAL_EXP (r) + 128;
4199 image = (sig << 16) & 0xffff0000;
4200 image |= sign;
4201 image |= exp << 7;
4202 image |= sig >> 16;
4203 break;
4205 default:
4206 gcc_unreachable ();
4209 buf[0] = image;
4212 static void
4213 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
4214 REAL_VALUE_TYPE *r, const long *buf)
4216 unsigned long image = buf[0] & 0xffffffff;
4217 int exp = (image >> 7) & 0xff;
4219 memset (r, 0, sizeof (*r));
4221 if (exp != 0)
4223 r->cl = rvc_normal;
4224 r->sign = (image >> 15) & 1;
4225 SET_REAL_EXP (r, exp - 128);
4227 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
4228 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4232 static void
4233 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4234 const REAL_VALUE_TYPE *r)
4236 unsigned long image0, image1, sign = r->sign << 15;
4238 switch (r->cl)
4240 case rvc_zero:
4241 image0 = image1 = 0;
4242 break;
4244 case rvc_inf:
4245 case rvc_nan:
4246 image0 = 0xffff7fff | sign;
4247 image1 = 0xffffffff;
4248 break;
4250 case rvc_normal:
4251 /* Extract the significand into straight hi:lo. */
4252 if (HOST_BITS_PER_LONG == 64)
4254 image0 = r->sig[SIGSZ-1];
4255 image1 = (image0 >> (64 - 56)) & 0xffffffff;
4256 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
4258 else
4260 image0 = r->sig[SIGSZ-1];
4261 image1 = r->sig[SIGSZ-2];
4262 image1 = (image0 << 24) | (image1 >> 8);
4263 image0 = (image0 >> 8) & 0xffffff;
4266 /* Rearrange the half-words of the significand to match the
4267 external format. */
4268 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
4269 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4271 /* Add the sign and exponent. */
4272 image0 |= sign;
4273 image0 |= (REAL_EXP (r) + 128) << 7;
4274 break;
4276 default:
4277 gcc_unreachable ();
4280 if (FLOAT_WORDS_BIG_ENDIAN)
4281 buf[0] = image1, buf[1] = image0;
4282 else
4283 buf[0] = image0, buf[1] = image1;
4286 static void
4287 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
4288 REAL_VALUE_TYPE *r, const long *buf)
4290 unsigned long image0, image1;
4291 int exp;
4293 if (FLOAT_WORDS_BIG_ENDIAN)
4294 image1 = buf[0], image0 = buf[1];
4295 else
4296 image0 = buf[0], image1 = buf[1];
4297 image0 &= 0xffffffff;
4298 image1 &= 0xffffffff;
4300 exp = (image0 >> 7) & 0xff;
4302 memset (r, 0, sizeof (*r));
4304 if (exp != 0)
4306 r->cl = rvc_normal;
4307 r->sign = (image0 >> 15) & 1;
4308 SET_REAL_EXP (r, exp - 128);
4310 /* Rearrange the half-words of the external format into
4311 proper ascending order. */
4312 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4313 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4315 if (HOST_BITS_PER_LONG == 64)
4317 image0 = (image0 << 31 << 1) | image1;
4318 image0 <<= 64 - 56;
4319 image0 |= SIG_MSB;
4320 r->sig[SIGSZ-1] = image0;
4322 else
4324 r->sig[SIGSZ-1] = image0;
4325 r->sig[SIGSZ-2] = image1;
4326 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4327 r->sig[SIGSZ-1] |= SIG_MSB;
4332 static void
4333 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4334 const REAL_VALUE_TYPE *r)
4336 unsigned long image0, image1, sign = r->sign << 15;
4338 switch (r->cl)
4340 case rvc_zero:
4341 image0 = image1 = 0;
4342 break;
4344 case rvc_inf:
4345 case rvc_nan:
4346 image0 = 0xffff7fff | sign;
4347 image1 = 0xffffffff;
4348 break;
4350 case rvc_normal:
4351 /* Extract the significand into straight hi:lo. */
4352 if (HOST_BITS_PER_LONG == 64)
4354 image0 = r->sig[SIGSZ-1];
4355 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4356 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4358 else
4360 image0 = r->sig[SIGSZ-1];
4361 image1 = r->sig[SIGSZ-2];
4362 image1 = (image0 << 21) | (image1 >> 11);
4363 image0 = (image0 >> 11) & 0xfffff;
4366 /* Rearrange the half-words of the significand to match the
4367 external format. */
4368 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4369 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4371 /* Add the sign and exponent. */
4372 image0 |= sign;
4373 image0 |= (REAL_EXP (r) + 1024) << 4;
4374 break;
4376 default:
4377 gcc_unreachable ();
4380 if (FLOAT_WORDS_BIG_ENDIAN)
4381 buf[0] = image1, buf[1] = image0;
4382 else
4383 buf[0] = image0, buf[1] = image1;
4386 static void
4387 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4388 REAL_VALUE_TYPE *r, const long *buf)
4390 unsigned long image0, image1;
4391 int exp;
4393 if (FLOAT_WORDS_BIG_ENDIAN)
4394 image1 = buf[0], image0 = buf[1];
4395 else
4396 image0 = buf[0], image1 = buf[1];
4397 image0 &= 0xffffffff;
4398 image1 &= 0xffffffff;
4400 exp = (image0 >> 4) & 0x7ff;
4402 memset (r, 0, sizeof (*r));
4404 if (exp != 0)
4406 r->cl = rvc_normal;
4407 r->sign = (image0 >> 15) & 1;
4408 SET_REAL_EXP (r, exp - 1024);
4410 /* Rearrange the half-words of the external format into
4411 proper ascending order. */
4412 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4413 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4415 if (HOST_BITS_PER_LONG == 64)
4417 image0 = (image0 << 31 << 1) | image1;
4418 image0 <<= 64 - 53;
4419 image0 |= SIG_MSB;
4420 r->sig[SIGSZ-1] = image0;
4422 else
4424 r->sig[SIGSZ-1] = image0;
4425 r->sig[SIGSZ-2] = image1;
4426 lshift_significand (r, r, 64 - 53);
4427 r->sig[SIGSZ-1] |= SIG_MSB;
4432 const struct real_format vax_f_format =
4434 encode_vax_f,
4435 decode_vax_f,
4439 -127,
4440 127,
4443 false,
4444 false,
4445 false,
4446 false,
4447 false,
4448 false,
4449 false,
4450 false
4453 const struct real_format vax_d_format =
4455 encode_vax_d,
4456 decode_vax_d,
4460 -127,
4461 127,
4464 false,
4465 false,
4466 false,
4467 false,
4468 false,
4469 false,
4470 false,
4471 false
4474 const struct real_format vax_g_format =
4476 encode_vax_g,
4477 decode_vax_g,
4481 -1023,
4482 1023,
4485 false,
4486 false,
4487 false,
4488 false,
4489 false,
4490 false,
4491 false,
4492 false
4495 /* Encode real R into a single precision DFP value in BUF. */
4496 static void
4497 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4498 long *buf ATTRIBUTE_UNUSED,
4499 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4501 encode_decimal32 (fmt, buf, r);
4504 /* Decode a single precision DFP value in BUF into a real R. */
4505 static void
4506 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4507 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4508 const long *buf ATTRIBUTE_UNUSED)
4510 decode_decimal32 (fmt, r, buf);
4513 /* Encode real R into a double precision DFP value in BUF. */
4514 static void
4515 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4516 long *buf ATTRIBUTE_UNUSED,
4517 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4519 encode_decimal64 (fmt, buf, r);
4522 /* Decode a double precision DFP value in BUF into a real R. */
4523 static void
4524 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4525 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4526 const long *buf ATTRIBUTE_UNUSED)
4528 decode_decimal64 (fmt, r, buf);
4531 /* Encode real R into a quad precision DFP value in BUF. */
4532 static void
4533 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4534 long *buf ATTRIBUTE_UNUSED,
4535 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4537 encode_decimal128 (fmt, buf, r);
4540 /* Decode a quad precision DFP value in BUF into a real R. */
4541 static void
4542 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4543 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4544 const long *buf ATTRIBUTE_UNUSED)
4546 decode_decimal128 (fmt, r, buf);
4549 /* Single precision decimal floating point (IEEE 754). */
4550 const struct real_format decimal_single_format =
4552 encode_decimal_single,
4553 decode_decimal_single,
4557 -94,
4561 false,
4562 true,
4563 true,
4564 true,
4565 true,
4566 true,
4567 true,
4568 false
4571 /* Double precision decimal floating point (IEEE 754). */
4572 const struct real_format decimal_double_format =
4574 encode_decimal_double,
4575 decode_decimal_double,
4579 -382,
4580 385,
4583 false,
4584 true,
4585 true,
4586 true,
4587 true,
4588 true,
4589 true,
4590 false
4593 /* Quad precision decimal floating point (IEEE 754). */
4594 const struct real_format decimal_quad_format =
4596 encode_decimal_quad,
4597 decode_decimal_quad,
4601 -6142,
4602 6145,
4603 127,
4604 127,
4605 false,
4606 true,
4607 true,
4608 true,
4609 true,
4610 true,
4611 true,
4612 false
4615 /* Encode half-precision floats. This routine is used both for the IEEE
4616 ARM alternative encodings. */
4617 static void
4618 encode_ieee_half (const struct real_format *fmt, long *buf,
4619 const REAL_VALUE_TYPE *r)
4621 unsigned long image, sig, exp;
4622 unsigned long sign = r->sign;
4623 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
4625 image = sign << 15;
4626 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 11)) & 0x3ff;
4628 switch (r->cl)
4630 case rvc_zero:
4631 break;
4633 case rvc_inf:
4634 if (fmt->has_inf)
4635 image |= 31 << 10;
4636 else
4637 image |= 0x7fff;
4638 break;
4640 case rvc_nan:
4641 if (fmt->has_nans)
4643 if (r->canonical)
4644 sig = (fmt->canonical_nan_lsbs_set ? (1 << 9) - 1 : 0);
4645 if (r->signalling == fmt->qnan_msb_set)
4646 sig &= ~(1 << 9);
4647 else
4648 sig |= 1 << 9;
4649 if (sig == 0)
4650 sig = 1 << 8;
4652 image |= 31 << 10;
4653 image |= sig;
4655 else
4656 image |= 0x3ff;
4657 break;
4659 case rvc_normal:
4660 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4661 whereas the intermediate representation is 0.F x 2**exp.
4662 Which means we're off by one. */
4663 if (denormal)
4664 exp = 0;
4665 else
4666 exp = REAL_EXP (r) + 15 - 1;
4667 image |= exp << 10;
4668 image |= sig;
4669 break;
4671 default:
4672 gcc_unreachable ();
4675 buf[0] = image;
4678 /* Decode half-precision floats. This routine is used both for the IEEE
4679 ARM alternative encodings. */
4680 static void
4681 decode_ieee_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4682 const long *buf)
4684 unsigned long image = buf[0] & 0xffff;
4685 bool sign = (image >> 15) & 1;
4686 int exp = (image >> 10) & 0x1f;
4688 memset (r, 0, sizeof (*r));
4689 image <<= HOST_BITS_PER_LONG - 11;
4690 image &= ~SIG_MSB;
4692 if (exp == 0)
4694 if (image && fmt->has_denorm)
4696 r->cl = rvc_normal;
4697 r->sign = sign;
4698 SET_REAL_EXP (r, -14);
4699 r->sig[SIGSZ-1] = image << 1;
4700 normalize (r);
4702 else if (fmt->has_signed_zero)
4703 r->sign = sign;
4705 else if (exp == 31 && (fmt->has_nans || fmt->has_inf))
4707 if (image)
4709 r->cl = rvc_nan;
4710 r->sign = sign;
4711 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4712 ^ fmt->qnan_msb_set);
4713 r->sig[SIGSZ-1] = image;
4715 else
4717 r->cl = rvc_inf;
4718 r->sign = sign;
4721 else
4723 r->cl = rvc_normal;
4724 r->sign = sign;
4725 SET_REAL_EXP (r, exp - 15 + 1);
4726 r->sig[SIGSZ-1] = image | SIG_MSB;
4730 /* Half-precision format, as specified in IEEE 754R. */
4731 const struct real_format ieee_half_format =
4733 encode_ieee_half,
4734 decode_ieee_half,
4738 -13,
4742 false,
4743 true,
4744 true,
4745 true,
4746 true,
4747 true,
4748 true,
4749 false
4752 /* ARM's alternative half-precision format, similar to IEEE but with
4753 no reserved exponent value for NaNs and infinities; rather, it just
4754 extends the range of exponents by one. */
4755 const struct real_format arm_half_format =
4757 encode_ieee_half,
4758 decode_ieee_half,
4762 -13,
4766 false,
4767 true,
4768 false,
4769 false,
4770 true,
4771 true,
4772 false,
4773 false
4776 /* A synthetic "format" for internal arithmetic. It's the size of the
4777 internal significand minus the two bits needed for proper rounding.
4778 The encode and decode routines exist only to satisfy our paranoia
4779 harness. */
4781 static void encode_internal (const struct real_format *fmt,
4782 long *, const REAL_VALUE_TYPE *);
4783 static void decode_internal (const struct real_format *,
4784 REAL_VALUE_TYPE *, const long *);
4786 static void
4787 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4788 const REAL_VALUE_TYPE *r)
4790 memcpy (buf, r, sizeof (*r));
4793 static void
4794 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
4795 REAL_VALUE_TYPE *r, const long *buf)
4797 memcpy (r, buf, sizeof (*r));
4800 const struct real_format real_internal_format =
4802 encode_internal,
4803 decode_internal,
4805 SIGNIFICAND_BITS - 2,
4806 SIGNIFICAND_BITS - 2,
4807 -MAX_EXP,
4808 MAX_EXP,
4811 false,
4812 false,
4813 true,
4814 true,
4815 false,
4816 true,
4817 true,
4818 false
4821 /* Calculate X raised to the integer exponent N in mode MODE and store
4822 the result in R. Return true if the result may be inexact due to
4823 loss of precision. The algorithm is the classic "left-to-right binary
4824 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4825 Algorithms", "The Art of Computer Programming", Volume 2. */
4827 bool
4828 real_powi (REAL_VALUE_TYPE *r, machine_mode mode,
4829 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4831 unsigned HOST_WIDE_INT bit;
4832 REAL_VALUE_TYPE t;
4833 bool inexact = false;
4834 bool init = false;
4835 bool neg;
4836 int i;
4838 if (n == 0)
4840 *r = dconst1;
4841 return false;
4843 else if (n < 0)
4845 /* Don't worry about overflow, from now on n is unsigned. */
4846 neg = true;
4847 n = -n;
4849 else
4850 neg = false;
4852 t = *x;
4853 bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
4854 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4856 if (init)
4858 inexact |= do_multiply (&t, &t, &t);
4859 if (n & bit)
4860 inexact |= do_multiply (&t, &t, x);
4862 else if (n & bit)
4863 init = true;
4864 bit >>= 1;
4867 if (neg)
4868 inexact |= do_divide (&t, &dconst1, &t);
4870 real_convert (r, mode, &t);
4871 return inexact;
4874 /* Round X to the nearest integer not larger in absolute value, i.e.
4875 towards zero, placing the result in R in mode MODE. */
4877 void
4878 real_trunc (REAL_VALUE_TYPE *r, machine_mode mode,
4879 const REAL_VALUE_TYPE *x)
4881 do_fix_trunc (r, x);
4882 if (mode != VOIDmode)
4883 real_convert (r, mode, r);
4886 /* Round X to the largest integer not greater in value, i.e. round
4887 down, placing the result in R in mode MODE. */
4889 void
4890 real_floor (REAL_VALUE_TYPE *r, machine_mode mode,
4891 const REAL_VALUE_TYPE *x)
4893 REAL_VALUE_TYPE t;
4895 do_fix_trunc (&t, x);
4896 if (! real_identical (&t, x) && x->sign)
4897 do_add (&t, &t, &dconstm1, 0);
4898 if (mode != VOIDmode)
4899 real_convert (r, mode, &t);
4900 else
4901 *r = t;
4904 /* Round X to the smallest integer not less then argument, i.e. round
4905 up, placing the result in R in mode MODE. */
4907 void
4908 real_ceil (REAL_VALUE_TYPE *r, machine_mode mode,
4909 const REAL_VALUE_TYPE *x)
4911 REAL_VALUE_TYPE t;
4913 do_fix_trunc (&t, x);
4914 if (! real_identical (&t, x) && ! x->sign)
4915 do_add (&t, &t, &dconst1, 0);
4916 if (mode != VOIDmode)
4917 real_convert (r, mode, &t);
4918 else
4919 *r = t;
4922 /* Round X to the nearest integer, but round halfway cases away from
4923 zero. */
4925 void
4926 real_round (REAL_VALUE_TYPE *r, machine_mode mode,
4927 const REAL_VALUE_TYPE *x)
4929 do_add (r, x, &dconsthalf, x->sign);
4930 do_fix_trunc (r, r);
4931 if (mode != VOIDmode)
4932 real_convert (r, mode, r);
4935 /* Set the sign of R to the sign of X. */
4937 void
4938 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4940 r->sign = x->sign;
4943 /* Check whether the real constant value given is an integer. */
4945 bool
4946 real_isinteger (const REAL_VALUE_TYPE *c, machine_mode mode)
4948 REAL_VALUE_TYPE cint;
4950 real_trunc (&cint, mode, c);
4951 return real_identical (c, &cint);
4954 /* Write into BUF the maximum representable finite floating-point
4955 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4956 float string. LEN is the size of BUF, and the buffer must be large
4957 enough to contain the resulting string. */
4959 void
4960 get_max_float (const struct real_format *fmt, char *buf, size_t len)
4962 int i, n;
4963 char *p;
4965 strcpy (buf, "0x0.");
4966 n = fmt->p;
4967 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
4968 *p++ = 'f';
4969 if (i < n)
4970 *p++ = "08ce"[n - i];
4971 sprintf (p, "p%d", fmt->emax);
4972 if (fmt->pnan < fmt->p)
4974 /* This is an IBM extended double format made up of two IEEE
4975 doubles. The value of the long double is the sum of the
4976 values of the two parts. The most significant part is
4977 required to be the value of the long double rounded to the
4978 nearest double. Rounding means we need a slightly smaller
4979 value for LDBL_MAX. */
4980 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
4983 gcc_assert (strlen (buf) < len);