Require target lra in gcc.dg/pr108095.c
[official-gcc.git] / gcc / real.cc
blobfc6b3a75b1e7ec5a1e7348206f2344ce4a4facc0
1 /* real.cc - software floating point emulation.
2 Copyright (C) 1993-2023 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 "rtl.h"
27 #include "tree.h"
28 #include "realmpfr.h"
29 #include "dfp.h"
31 /* The floating point model used internally is not exactly IEEE 754
32 compliant, and close to the description in the ISO C99 standard,
33 section 5.2.4.2.2 Characteristics of floating types.
35 Specifically
37 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
39 where
40 s = sign (+- 1)
41 b = base or radix, here always 2
42 e = exponent
43 p = precision (the number of base-b digits in the significand)
44 f_k = the digits of the significand.
46 We differ from typical IEEE 754 encodings in that the entire
47 significand is fractional. Normalized significands are in the
48 range [0.5, 1.0).
50 A requirement of the model is that P be larger than the largest
51 supported target floating-point type by at least 2 bits. This gives
52 us proper rounding when we truncate to the target type. In addition,
53 E must be large enough to hold the smallest supported denormal number
54 in a normalized form.
56 Both of these requirements are easily satisfied. The largest target
57 significand is 113 bits; we store at least 160. The smallest
58 denormal number fits in 17 exponent bits; we store 26. */
61 /* Used to classify two numbers simultaneously. */
62 #define CLASS2(A, B) ((A) << 2 | (B))
64 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
65 #error "Some constant folding done by hand to avoid shift count warnings"
66 #endif
68 static void get_zero (REAL_VALUE_TYPE *, int);
69 static void get_canonical_qnan (REAL_VALUE_TYPE *, int);
70 static void get_canonical_snan (REAL_VALUE_TYPE *, int);
71 static void get_inf (REAL_VALUE_TYPE *, int);
72 static bool sticky_rshift_significand (REAL_VALUE_TYPE *,
73 const REAL_VALUE_TYPE *, unsigned int);
74 static void rshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
75 unsigned int);
76 static void lshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
77 unsigned int);
78 static void lshift_significand_1 (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
79 static bool add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *,
80 const REAL_VALUE_TYPE *);
81 static bool sub_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
82 const REAL_VALUE_TYPE *, int);
83 static void neg_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
84 static int cmp_significands (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
85 static int cmp_significand_0 (const REAL_VALUE_TYPE *);
86 static void set_significand_bit (REAL_VALUE_TYPE *, unsigned int);
87 static void clear_significand_bit (REAL_VALUE_TYPE *, unsigned int);
88 static bool test_significand_bit (REAL_VALUE_TYPE *, unsigned int);
89 static void clear_significand_below (REAL_VALUE_TYPE *, unsigned int);
90 static bool div_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
91 const REAL_VALUE_TYPE *);
92 static void normalize (REAL_VALUE_TYPE *);
94 static bool do_add (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
95 const REAL_VALUE_TYPE *, int);
96 static bool do_multiply (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
97 const REAL_VALUE_TYPE *);
98 static bool do_divide (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
99 const REAL_VALUE_TYPE *);
100 static int do_compare (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int);
101 static void do_fix_trunc (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
103 static unsigned long rtd_divmod (REAL_VALUE_TYPE *, REAL_VALUE_TYPE *);
104 static void decimal_from_integer (REAL_VALUE_TYPE *);
105 static void decimal_integer_string (char *, const REAL_VALUE_TYPE *,
106 size_t);
108 static const REAL_VALUE_TYPE * ten_to_ptwo (int);
109 static const REAL_VALUE_TYPE * ten_to_mptwo (int);
110 static const REAL_VALUE_TYPE * real_digit (int);
111 static void times_pten (REAL_VALUE_TYPE *, int);
113 static void round_for_format (const struct real_format *, REAL_VALUE_TYPE *);
115 /* Determine whether a floating-point value X is a denormal. R is
116 expected to be in denormal form, so this function is only
117 meaningful after a call to round_for_format. */
119 static inline bool
120 real_isdenormal (const REAL_VALUE_TYPE *r)
122 return r->cl == rvc_normal && (r->sig[SIGSZ-1] & SIG_MSB) == 0;
125 /* Initialize R with a positive zero. */
127 static inline void
128 get_zero (REAL_VALUE_TYPE *r, int sign)
130 memset (r, 0, sizeof (*r));
131 r->sign = sign;
134 /* Initialize R with the canonical quiet NaN. */
136 static inline void
137 get_canonical_qnan (REAL_VALUE_TYPE *r, int sign)
139 memset (r, 0, sizeof (*r));
140 r->cl = rvc_nan;
141 r->sign = sign;
142 r->canonical = 1;
145 static inline void
146 get_canonical_snan (REAL_VALUE_TYPE *r, int sign)
148 memset (r, 0, sizeof (*r));
149 r->cl = rvc_nan;
150 r->sign = sign;
151 r->signalling = 1;
152 r->canonical = 1;
155 static inline void
156 get_inf (REAL_VALUE_TYPE *r, int sign)
158 memset (r, 0, sizeof (*r));
159 r->cl = rvc_inf;
160 r->sign = sign;
164 /* Right-shift the significand of A by N bits; put the result in the
165 significand of R. If any one bits are shifted out, return true. */
167 static bool
168 sticky_rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
169 unsigned int n)
171 unsigned long sticky = 0;
172 unsigned int i, ofs = 0;
174 if (n >= HOST_BITS_PER_LONG)
176 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
177 sticky |= a->sig[i];
178 n &= HOST_BITS_PER_LONG - 1;
181 if (n != 0)
183 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
184 for (i = 0; i < SIGSZ; ++i)
186 r->sig[i]
187 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
188 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
189 << (HOST_BITS_PER_LONG - n)));
192 else
194 for (i = 0; ofs + i < SIGSZ; ++i)
195 r->sig[i] = a->sig[ofs + i];
196 for (; i < SIGSZ; ++i)
197 r->sig[i] = 0;
200 return sticky != 0;
203 /* Right-shift the significand of A by N bits; put the result in the
204 significand of R. */
206 static void
207 rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
208 unsigned int n)
210 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
212 n &= HOST_BITS_PER_LONG - 1;
213 if (n != 0)
215 for (i = 0; i < SIGSZ; ++i)
217 r->sig[i]
218 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
219 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
220 << (HOST_BITS_PER_LONG - n)));
223 else
225 for (i = 0; ofs + i < SIGSZ; ++i)
226 r->sig[i] = a->sig[ofs + i];
227 for (; i < SIGSZ; ++i)
228 r->sig[i] = 0;
232 /* Left-shift the significand of A by N bits; put the result in the
233 significand of R. */
235 static void
236 lshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
237 unsigned int n)
239 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
241 n &= HOST_BITS_PER_LONG - 1;
242 if (n == 0)
244 for (i = 0; ofs + i < SIGSZ; ++i)
245 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
246 for (; i < SIGSZ; ++i)
247 r->sig[SIGSZ-1-i] = 0;
249 else
250 for (i = 0; i < SIGSZ; ++i)
252 r->sig[SIGSZ-1-i]
253 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
254 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
255 >> (HOST_BITS_PER_LONG - n)));
259 /* Likewise, but N is specialized to 1. */
261 static inline void
262 lshift_significand_1 (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
264 unsigned int i;
266 for (i = SIGSZ - 1; i > 0; --i)
267 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
268 r->sig[0] = a->sig[0] << 1;
271 /* Add the significands of A and B, placing the result in R. Return
272 true if there was carry out of the most significant word. */
274 static inline bool
275 add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
276 const REAL_VALUE_TYPE *b)
278 bool carry = false;
279 int i;
281 for (i = 0; i < SIGSZ; ++i)
283 unsigned long ai = a->sig[i];
284 unsigned long ri = ai + b->sig[i];
286 if (carry)
288 carry = ri < ai;
289 carry |= ++ri == 0;
291 else
292 carry = ri < ai;
294 r->sig[i] = ri;
297 return carry;
300 /* Subtract the significands of A and B, placing the result in R. CARRY is
301 true if there's a borrow incoming to the least significant word.
302 Return true if there was borrow out of the most significant word. */
304 static inline bool
305 sub_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
306 const REAL_VALUE_TYPE *b, int carry)
308 int i;
310 for (i = 0; i < SIGSZ; ++i)
312 unsigned long ai = a->sig[i];
313 unsigned long ri = ai - b->sig[i];
315 if (carry)
317 carry = ri > ai;
318 carry |= ~--ri == 0;
320 else
321 carry = ri > ai;
323 r->sig[i] = ri;
326 return carry;
329 /* Negate the significand A, placing the result in R. */
331 static inline void
332 neg_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
334 bool carry = true;
335 int i;
337 for (i = 0; i < SIGSZ; ++i)
339 unsigned long ri, ai = a->sig[i];
341 if (carry)
343 if (ai)
345 ri = -ai;
346 carry = false;
348 else
349 ri = ai;
351 else
352 ri = ~ai;
354 r->sig[i] = ri;
358 /* Compare significands. Return tri-state vs zero. */
360 static inline int
361 cmp_significands (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
363 int i;
365 for (i = SIGSZ - 1; i >= 0; --i)
367 unsigned long ai = a->sig[i];
368 unsigned long bi = b->sig[i];
370 if (ai > bi)
371 return 1;
372 if (ai < bi)
373 return -1;
376 return 0;
379 /* Return true if A is nonzero. */
381 static inline int
382 cmp_significand_0 (const REAL_VALUE_TYPE *a)
384 int i;
386 for (i = SIGSZ - 1; i >= 0; --i)
387 if (a->sig[i])
388 return 1;
390 return 0;
393 /* Set bit N of the significand of R. */
395 static inline void
396 set_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
398 r->sig[n / HOST_BITS_PER_LONG]
399 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
402 /* Clear bit N of the significand of R. */
404 static inline void
405 clear_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
407 r->sig[n / HOST_BITS_PER_LONG]
408 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
411 /* Test bit N of the significand of R. */
413 static inline bool
414 test_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
416 /* ??? Compiler bug here if we return this expression directly.
417 The conversion to bool strips the "&1" and we wind up testing
418 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
419 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
420 return t;
423 /* Clear bits 0..N-1 of the significand of R. */
425 static void
426 clear_significand_below (REAL_VALUE_TYPE *r, unsigned int n)
428 int i, w = n / HOST_BITS_PER_LONG;
430 for (i = 0; i < w; ++i)
431 r->sig[i] = 0;
433 /* We are actually passing N == SIGNIFICAND_BITS which would result
434 in an out-of-bound access below. */
435 if (n % HOST_BITS_PER_LONG != 0)
436 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
439 /* Divide the significands of A and B, placing the result in R. Return
440 true if the division was inexact. */
442 static inline bool
443 div_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
444 const REAL_VALUE_TYPE *b)
446 REAL_VALUE_TYPE u;
447 int i, bit = SIGNIFICAND_BITS - 1;
448 unsigned long msb, inexact;
450 u = *a;
451 memset (r->sig, 0, sizeof (r->sig));
453 msb = 0;
454 goto start;
457 msb = u.sig[SIGSZ-1] & SIG_MSB;
458 lshift_significand_1 (&u, &u);
459 start:
460 if (msb || cmp_significands (&u, b) >= 0)
462 sub_significands (&u, &u, b, 0);
463 set_significand_bit (r, bit);
466 while (--bit >= 0);
468 for (i = 0, inexact = 0; i < SIGSZ; i++)
469 inexact |= u.sig[i];
471 return inexact != 0;
474 /* Adjust the exponent and significand of R such that the most
475 significant bit is set. We underflow to zero and overflow to
476 infinity here, without denormals. (The intermediate representation
477 exponent is large enough to handle target denormals normalized.) */
479 static void
480 normalize (REAL_VALUE_TYPE *r)
482 int shift = 0, exp;
483 int i, j;
485 if (r->decimal)
486 return;
488 /* Find the first word that is nonzero. */
489 for (i = SIGSZ - 1; i >= 0; i--)
490 if (r->sig[i] == 0)
491 shift += HOST_BITS_PER_LONG;
492 else
493 break;
495 /* Zero significand flushes to zero. */
496 if (i < 0)
498 r->cl = rvc_zero;
499 SET_REAL_EXP (r, 0);
500 return;
503 /* Find the first bit that is nonzero. */
504 for (j = 0; ; j++)
505 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
506 break;
507 shift += j;
509 if (shift > 0)
511 exp = REAL_EXP (r) - shift;
512 if (exp > MAX_EXP)
513 get_inf (r, r->sign);
514 else if (exp < -MAX_EXP)
515 get_zero (r, r->sign);
516 else
518 SET_REAL_EXP (r, exp);
519 lshift_significand (r, r, shift);
524 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
525 result may be inexact due to a loss of precision. */
527 static bool
528 do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
529 const REAL_VALUE_TYPE *b, int subtract_p)
531 int dexp, sign, exp;
532 REAL_VALUE_TYPE t;
533 bool inexact = false;
535 /* Determine if we need to add or subtract. */
536 sign = a->sign;
537 subtract_p = (sign ^ b->sign) ^ subtract_p;
539 switch (CLASS2 (a->cl, b->cl))
541 case CLASS2 (rvc_zero, rvc_zero):
542 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
543 get_zero (r, sign & !subtract_p);
544 return false;
546 case CLASS2 (rvc_zero, rvc_normal):
547 case CLASS2 (rvc_zero, rvc_inf):
548 case CLASS2 (rvc_zero, rvc_nan):
549 /* 0 + ANY = ANY. */
550 case CLASS2 (rvc_normal, rvc_nan):
551 case CLASS2 (rvc_inf, rvc_nan):
552 case CLASS2 (rvc_nan, rvc_nan):
553 /* ANY + NaN = NaN. */
554 case CLASS2 (rvc_normal, rvc_inf):
555 /* R + Inf = Inf. */
556 *r = *b;
557 /* Make resulting NaN value to be qNaN. The caller has the
558 responsibility to avoid the operation if flag_signaling_nans
559 is on. */
560 r->signalling = 0;
561 r->sign = sign ^ subtract_p;
562 return false;
564 case CLASS2 (rvc_normal, rvc_zero):
565 case CLASS2 (rvc_inf, rvc_zero):
566 case CLASS2 (rvc_nan, rvc_zero):
567 /* ANY + 0 = ANY. */
568 case CLASS2 (rvc_nan, rvc_normal):
569 case CLASS2 (rvc_nan, rvc_inf):
570 /* NaN + ANY = NaN. */
571 case CLASS2 (rvc_inf, rvc_normal):
572 /* Inf + R = Inf. */
573 *r = *a;
574 /* Make resulting NaN value to be qNaN. The caller has the
575 responsibility to avoid the operation if flag_signaling_nans
576 is on. */
577 r->signalling = 0;
578 return false;
580 case CLASS2 (rvc_inf, rvc_inf):
581 if (subtract_p)
582 /* Inf - Inf = NaN. */
583 get_canonical_qnan (r, 0);
584 else
585 /* Inf + Inf = Inf. */
586 *r = *a;
587 return false;
589 case CLASS2 (rvc_normal, rvc_normal):
590 break;
592 default:
593 gcc_unreachable ();
596 /* Swap the arguments such that A has the larger exponent. */
597 dexp = REAL_EXP (a) - REAL_EXP (b);
598 if (dexp < 0)
600 const REAL_VALUE_TYPE *t;
601 t = a, a = b, b = t;
602 dexp = -dexp;
603 sign ^= subtract_p;
605 exp = REAL_EXP (a);
607 /* If the exponents are not identical, we need to shift the
608 significand of B down. */
609 if (dexp > 0)
611 /* If the exponents are too far apart, the significands
612 do not overlap, which makes the subtraction a noop. */
613 if (dexp >= SIGNIFICAND_BITS)
615 *r = *a;
616 r->sign = sign;
617 return true;
620 inexact |= sticky_rshift_significand (&t, b, dexp);
621 b = &t;
624 if (subtract_p)
626 if (sub_significands (r, a, b, inexact))
628 /* We got a borrow out of the subtraction. That means that
629 A and B had the same exponent, and B had the larger
630 significand. We need to swap the sign and negate the
631 significand. */
632 sign ^= 1;
633 neg_significand (r, r);
636 else
638 if (add_significands (r, a, b))
640 /* We got carry out of the addition. This means we need to
641 shift the significand back down one bit and increase the
642 exponent. */
643 inexact |= sticky_rshift_significand (r, r, 1);
644 r->sig[SIGSZ-1] |= SIG_MSB;
645 if (++exp > MAX_EXP)
647 get_inf (r, sign);
648 return true;
653 r->cl = rvc_normal;
654 r->sign = sign;
655 SET_REAL_EXP (r, exp);
656 /* Zero out the remaining fields. */
657 r->signalling = 0;
658 r->canonical = 0;
659 r->decimal = 0;
661 /* Re-normalize the result. */
662 normalize (r);
664 /* Special case: if the subtraction results in zero, the result
665 is positive. */
666 if (r->cl == rvc_zero)
667 r->sign = 0;
668 else
669 r->sig[0] |= inexact;
671 return inexact;
674 /* Calculate R = A * B. Return true if the result may be inexact. */
676 static bool
677 do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
678 const REAL_VALUE_TYPE *b)
680 REAL_VALUE_TYPE u, t, *rr;
681 unsigned int i, j, k;
682 int sign = a->sign ^ b->sign;
683 bool inexact = false;
685 switch (CLASS2 (a->cl, b->cl))
687 case CLASS2 (rvc_zero, rvc_zero):
688 case CLASS2 (rvc_zero, rvc_normal):
689 case CLASS2 (rvc_normal, rvc_zero):
690 /* +-0 * ANY = 0 with appropriate sign. */
691 get_zero (r, sign);
692 return false;
694 case CLASS2 (rvc_zero, rvc_nan):
695 case CLASS2 (rvc_normal, rvc_nan):
696 case CLASS2 (rvc_inf, rvc_nan):
697 case CLASS2 (rvc_nan, rvc_nan):
698 /* ANY * NaN = NaN. */
699 *r = *b;
700 /* Make resulting NaN value to be qNaN. The caller has the
701 responsibility to avoid the operation if flag_signaling_nans
702 is on. */
703 r->signalling = 0;
704 r->sign = sign;
705 return false;
707 case CLASS2 (rvc_nan, rvc_zero):
708 case CLASS2 (rvc_nan, rvc_normal):
709 case CLASS2 (rvc_nan, rvc_inf):
710 /* NaN * ANY = NaN. */
711 *r = *a;
712 /* Make resulting NaN value to be qNaN. The caller has the
713 responsibility to avoid the operation if flag_signaling_nans
714 is on. */
715 r->signalling = 0;
716 r->sign = sign;
717 return false;
719 case CLASS2 (rvc_zero, rvc_inf):
720 case CLASS2 (rvc_inf, rvc_zero):
721 /* 0 * Inf = NaN */
722 get_canonical_qnan (r, sign);
723 return false;
725 case CLASS2 (rvc_inf, rvc_inf):
726 case CLASS2 (rvc_normal, rvc_inf):
727 case CLASS2 (rvc_inf, rvc_normal):
728 /* Inf * Inf = Inf, R * Inf = Inf */
729 get_inf (r, sign);
730 return false;
732 case CLASS2 (rvc_normal, rvc_normal):
733 break;
735 default:
736 gcc_unreachable ();
739 if (r == a || r == b)
740 rr = &t;
741 else
742 rr = r;
743 get_zero (rr, 0);
745 /* Collect all the partial products. Since we don't have sure access
746 to a widening multiply, we split each long into two half-words.
748 Consider the long-hand form of a four half-word multiplication:
750 A B C D
751 * E F G H
752 --------------
753 DE DF DG DH
754 CE CF CG CH
755 BE BF BG BH
756 AE AF AG AH
758 We construct partial products of the widened half-word products
759 that are known to not overlap, e.g. DF+DH. Each such partial
760 product is given its proper exponent, which allows us to sum them
761 and obtain the finished product. */
763 for (i = 0; i < SIGSZ * 2; ++i)
765 unsigned long ai = a->sig[i / 2];
766 if (i & 1)
767 ai >>= HOST_BITS_PER_LONG / 2;
768 else
769 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
771 if (ai == 0)
772 continue;
774 for (j = 0; j < 2; ++j)
776 int exp = (REAL_EXP (a) - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
777 + (REAL_EXP (b) - (1-j)*(HOST_BITS_PER_LONG/2)));
779 if (exp > MAX_EXP)
781 get_inf (r, sign);
782 return true;
784 if (exp < -MAX_EXP)
786 /* Would underflow to zero, which we shouldn't bother adding. */
787 inexact = true;
788 continue;
791 memset (&u, 0, sizeof (u));
792 u.cl = rvc_normal;
793 SET_REAL_EXP (&u, exp);
795 for (k = j; k < SIGSZ * 2; k += 2)
797 unsigned long bi = b->sig[k / 2];
798 if (k & 1)
799 bi >>= HOST_BITS_PER_LONG / 2;
800 else
801 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
803 u.sig[k / 2] = ai * bi;
806 normalize (&u);
807 inexact |= do_add (rr, rr, &u, 0);
811 rr->sign = sign;
812 if (rr != r)
813 *r = t;
815 return inexact;
818 /* Calculate R = A / B. Return true if the result may be inexact. */
820 static bool
821 do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
822 const REAL_VALUE_TYPE *b)
824 int exp, sign = a->sign ^ b->sign;
825 REAL_VALUE_TYPE t, *rr;
826 bool inexact;
828 switch (CLASS2 (a->cl, b->cl))
830 case CLASS2 (rvc_zero, rvc_zero):
831 /* 0 / 0 = NaN. */
832 case CLASS2 (rvc_inf, rvc_inf):
833 /* Inf / Inf = NaN. */
834 get_canonical_qnan (r, sign);
835 return false;
837 case CLASS2 (rvc_zero, rvc_normal):
838 case CLASS2 (rvc_zero, rvc_inf):
839 /* 0 / ANY = 0. */
840 case CLASS2 (rvc_normal, rvc_inf):
841 /* R / Inf = 0. */
842 get_zero (r, sign);
843 return false;
845 case CLASS2 (rvc_normal, rvc_zero):
846 /* R / 0 = Inf. */
847 case CLASS2 (rvc_inf, rvc_zero):
848 /* Inf / 0 = Inf. */
849 get_inf (r, sign);
850 return false;
852 case CLASS2 (rvc_zero, rvc_nan):
853 case CLASS2 (rvc_normal, rvc_nan):
854 case CLASS2 (rvc_inf, rvc_nan):
855 case CLASS2 (rvc_nan, rvc_nan):
856 /* ANY / NaN = NaN. */
857 *r = *b;
858 /* Make resulting NaN value to be qNaN. The caller has the
859 responsibility to avoid the operation if flag_signaling_nans
860 is on. */
861 r->signalling = 0;
862 r->sign = sign;
863 return false;
865 case CLASS2 (rvc_nan, rvc_zero):
866 case CLASS2 (rvc_nan, rvc_normal):
867 case CLASS2 (rvc_nan, rvc_inf):
868 /* NaN / ANY = NaN. */
869 *r = *a;
870 /* Make resulting NaN value to be qNaN. The caller has the
871 responsibility to avoid the operation if flag_signaling_nans
872 is on. */
873 r->signalling = 0;
874 r->sign = sign;
875 return false;
877 case CLASS2 (rvc_inf, rvc_normal):
878 /* Inf / R = Inf. */
879 get_inf (r, sign);
880 return false;
882 case CLASS2 (rvc_normal, rvc_normal):
883 break;
885 default:
886 gcc_unreachable ();
889 if (r == a || r == b)
890 rr = &t;
891 else
892 rr = r;
894 /* Make sure all fields in the result are initialized. */
895 get_zero (rr, 0);
896 rr->cl = rvc_normal;
897 rr->sign = sign;
899 exp = REAL_EXP (a) - REAL_EXP (b) + 1;
900 if (exp > MAX_EXP)
902 get_inf (r, sign);
903 return true;
905 if (exp < -MAX_EXP)
907 get_zero (r, sign);
908 return true;
910 SET_REAL_EXP (rr, exp);
912 inexact = div_significands (rr, a, b);
914 /* Re-normalize the result. */
915 normalize (rr);
916 rr->sig[0] |= inexact;
918 if (rr != r)
919 *r = t;
921 return inexact;
924 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
925 one of the two operands is a NaN. */
927 static int
928 do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
929 int nan_result)
931 int ret;
933 switch (CLASS2 (a->cl, b->cl))
935 case CLASS2 (rvc_zero, rvc_zero):
936 /* Sign of zero doesn't matter for compares. */
937 return 0;
939 case CLASS2 (rvc_normal, rvc_zero):
940 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
941 if (a->decimal)
942 return decimal_do_compare (a, b, nan_result);
943 /* Fall through. */
944 case CLASS2 (rvc_inf, rvc_zero):
945 case CLASS2 (rvc_inf, rvc_normal):
946 return (a->sign ? -1 : 1);
948 case CLASS2 (rvc_inf, rvc_inf):
949 return -a->sign - -b->sign;
951 case CLASS2 (rvc_zero, rvc_normal):
952 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
953 if (b->decimal)
954 return decimal_do_compare (a, b, nan_result);
955 /* Fall through. */
956 case CLASS2 (rvc_zero, rvc_inf):
957 case CLASS2 (rvc_normal, rvc_inf):
958 return (b->sign ? 1 : -1);
960 case CLASS2 (rvc_zero, rvc_nan):
961 case CLASS2 (rvc_normal, rvc_nan):
962 case CLASS2 (rvc_inf, rvc_nan):
963 case CLASS2 (rvc_nan, rvc_nan):
964 case CLASS2 (rvc_nan, rvc_zero):
965 case CLASS2 (rvc_nan, rvc_normal):
966 case CLASS2 (rvc_nan, rvc_inf):
967 return nan_result;
969 case CLASS2 (rvc_normal, rvc_normal):
970 break;
972 default:
973 gcc_unreachable ();
976 if (a->decimal || b->decimal)
977 return decimal_do_compare (a, b, nan_result);
979 if (a->sign != b->sign)
980 return -a->sign - -b->sign;
982 if (REAL_EXP (a) > REAL_EXP (b))
983 ret = 1;
984 else if (REAL_EXP (a) < REAL_EXP (b))
985 ret = -1;
986 else
987 ret = cmp_significands (a, b);
989 return (a->sign ? -ret : ret);
992 /* Return A truncated to an integral value toward zero. */
994 static void
995 do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
997 *r = *a;
999 switch (r->cl)
1001 case rvc_zero:
1002 case rvc_inf:
1003 case rvc_nan:
1004 /* Make resulting NaN value to be qNaN. The caller has the
1005 responsibility to avoid the operation if flag_signaling_nans
1006 is on. */
1007 r->signalling = 0;
1008 break;
1010 case rvc_normal:
1011 if (r->decimal)
1013 decimal_do_fix_trunc (r, a);
1014 return;
1016 if (REAL_EXP (r) <= 0)
1017 get_zero (r, r->sign);
1018 else if (REAL_EXP (r) < SIGNIFICAND_BITS)
1019 clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r));
1020 break;
1022 default:
1023 gcc_unreachable ();
1027 /* Perform the binary or unary operation described by CODE.
1028 For a unary operation, leave OP1 NULL. This function returns
1029 true if the result may be inexact due to loss of precision. */
1031 bool
1032 real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0,
1033 const REAL_VALUE_TYPE *op1)
1035 enum tree_code code = (enum tree_code) icode;
1037 if (op0->decimal || (op1 && op1->decimal))
1038 return decimal_real_arithmetic (r, code, op0, op1);
1040 switch (code)
1042 case PLUS_EXPR:
1043 /* Clear any padding areas in *r if it isn't equal to one of the
1044 operands so that we can later do bitwise comparisons later on. */
1045 if (r != op0 && r != op1)
1046 memset (r, '\0', sizeof (*r));
1047 return do_add (r, op0, op1, 0);
1049 case MINUS_EXPR:
1050 if (r != op0 && r != op1)
1051 memset (r, '\0', sizeof (*r));
1052 return do_add (r, op0, op1, 1);
1054 case MULT_EXPR:
1055 if (r != op0 && r != op1)
1056 memset (r, '\0', sizeof (*r));
1057 return do_multiply (r, op0, op1);
1059 case RDIV_EXPR:
1060 if (r != op0 && r != op1)
1061 memset (r, '\0', sizeof (*r));
1062 return do_divide (r, op0, op1);
1064 case MIN_EXPR:
1065 if (op1->cl == rvc_nan)
1067 *r = *op1;
1068 /* Make resulting NaN value to be qNaN. The caller has the
1069 responsibility to avoid the operation if flag_signaling_nans
1070 is on. */
1071 r->signalling = 0;
1073 else if (do_compare (op0, op1, -1) < 0)
1074 *r = *op0;
1075 else
1076 *r = *op1;
1077 break;
1079 case MAX_EXPR:
1080 if (op1->cl == rvc_nan)
1082 *r = *op1;
1083 /* Make resulting NaN value to be qNaN. The caller has the
1084 responsibility to avoid the operation if flag_signaling_nans
1085 is on. */
1086 r->signalling = 0;
1088 else if (do_compare (op0, op1, 1) < 0)
1089 *r = *op1;
1090 else
1091 *r = *op0;
1092 break;
1094 case NEGATE_EXPR:
1095 *r = *op0;
1096 r->sign ^= 1;
1097 break;
1099 case ABS_EXPR:
1100 *r = *op0;
1101 r->sign = 0;
1102 break;
1104 case FIX_TRUNC_EXPR:
1105 do_fix_trunc (r, op0);
1106 break;
1108 default:
1109 gcc_unreachable ();
1111 return false;
1114 REAL_VALUE_TYPE
1115 real_value_negate (const REAL_VALUE_TYPE *op0)
1117 REAL_VALUE_TYPE r;
1118 real_arithmetic (&r, NEGATE_EXPR, op0, NULL);
1119 return r;
1122 REAL_VALUE_TYPE
1123 real_value_abs (const REAL_VALUE_TYPE *op0)
1125 REAL_VALUE_TYPE r;
1126 real_arithmetic (&r, ABS_EXPR, op0, NULL);
1127 return r;
1130 /* Return whether OP0 == OP1. */
1132 bool
1133 real_equal (const REAL_VALUE_TYPE *op0, const REAL_VALUE_TYPE *op1)
1135 return do_compare (op0, op1, -1) == 0;
1138 /* Return whether OP0 < OP1. */
1140 bool
1141 real_less (const REAL_VALUE_TYPE *op0, const REAL_VALUE_TYPE *op1)
1143 return do_compare (op0, op1, 1) < 0;
1146 bool
1147 real_compare (int icode, const REAL_VALUE_TYPE *op0,
1148 const REAL_VALUE_TYPE *op1)
1150 enum tree_code code = (enum tree_code) icode;
1152 switch (code)
1154 case LT_EXPR:
1155 return real_less (op0, op1);
1156 case LE_EXPR:
1157 return do_compare (op0, op1, 1) <= 0;
1158 case GT_EXPR:
1159 return do_compare (op0, op1, -1) > 0;
1160 case GE_EXPR:
1161 return do_compare (op0, op1, -1) >= 0;
1162 case EQ_EXPR:
1163 return real_equal (op0, op1);
1164 case NE_EXPR:
1165 return do_compare (op0, op1, -1) != 0;
1166 case UNORDERED_EXPR:
1167 return op0->cl == rvc_nan || op1->cl == rvc_nan;
1168 case ORDERED_EXPR:
1169 return op0->cl != rvc_nan && op1->cl != rvc_nan;
1170 case UNLT_EXPR:
1171 return do_compare (op0, op1, -1) < 0;
1172 case UNLE_EXPR:
1173 return do_compare (op0, op1, -1) <= 0;
1174 case UNGT_EXPR:
1175 return do_compare (op0, op1, 1) > 0;
1176 case UNGE_EXPR:
1177 return do_compare (op0, op1, 1) >= 0;
1178 case UNEQ_EXPR:
1179 return do_compare (op0, op1, 0) == 0;
1180 case LTGT_EXPR:
1181 return do_compare (op0, op1, 0) != 0;
1183 default:
1184 gcc_unreachable ();
1188 /* Return floor log2(R). */
1191 real_exponent (const REAL_VALUE_TYPE *r)
1193 switch (r->cl)
1195 case rvc_zero:
1196 return 0;
1197 case rvc_inf:
1198 case rvc_nan:
1199 return (unsigned int)-1 >> 1;
1200 case rvc_normal:
1201 return REAL_EXP (r);
1202 default:
1203 gcc_unreachable ();
1207 /* R = OP0 * 2**EXP. */
1209 void
1210 real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1212 *r = *op0;
1213 switch (r->cl)
1215 case rvc_zero:
1216 case rvc_inf:
1217 case rvc_nan:
1218 /* Make resulting NaN value to be qNaN. The caller has the
1219 responsibility to avoid the operation if flag_signaling_nans
1220 is on. */
1221 r->signalling = 0;
1222 break;
1224 case rvc_normal:
1225 exp += REAL_EXP (op0);
1226 if (exp > MAX_EXP)
1227 get_inf (r, r->sign);
1228 else if (exp < -MAX_EXP)
1229 get_zero (r, r->sign);
1230 else
1231 SET_REAL_EXP (r, exp);
1232 break;
1234 default:
1235 gcc_unreachable ();
1239 /* Determine whether a floating-point value X is infinite. */
1241 bool
1242 real_isinf (const REAL_VALUE_TYPE *r)
1244 return (r->cl == rvc_inf);
1247 /* Determine whether a floating-point value X is infinite with SIGN. */
1249 bool
1250 real_isinf (const REAL_VALUE_TYPE *r, bool sign)
1252 return real_isinf (r) && r->sign == sign;
1255 /* Determine whether a floating-point value X is a NaN. */
1257 bool
1258 real_isnan (const REAL_VALUE_TYPE *r)
1260 return (r->cl == rvc_nan);
1263 /* Determine whether a floating-point value X is a signaling NaN. */
1264 bool real_issignaling_nan (const REAL_VALUE_TYPE *r)
1266 return real_isnan (r) && r->signalling;
1269 /* Determine whether a floating-point value X is finite. */
1271 bool
1272 real_isfinite (const REAL_VALUE_TYPE *r)
1274 return (r->cl != rvc_nan) && (r->cl != rvc_inf);
1277 /* Determine whether a floating-point value X is negative. */
1279 bool
1280 real_isneg (const REAL_VALUE_TYPE *r)
1282 return r->sign;
1285 /* Determine whether a floating-point value X is plus or minus zero. */
1287 bool
1288 real_iszero (const REAL_VALUE_TYPE *r)
1290 return r->cl == rvc_zero;
1293 /* Determine whether a floating-point value X is zero with SIGN. */
1295 bool
1296 real_iszero (const REAL_VALUE_TYPE *r, bool sign)
1298 return real_iszero (r) && r->sign == sign;
1301 /* Determine whether a floating-point value X is minus zero. */
1303 bool
1304 real_isnegzero (const REAL_VALUE_TYPE *r)
1306 return r->sign && r->cl == rvc_zero;
1309 /* Compare two floating-point objects for bitwise identity. */
1311 bool
1312 real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1314 int i;
1316 if (a->cl != b->cl)
1317 return false;
1318 if (a->sign != b->sign)
1319 return false;
1321 switch (a->cl)
1323 case rvc_zero:
1324 case rvc_inf:
1325 return true;
1327 case rvc_normal:
1328 if (a->decimal != b->decimal)
1329 return false;
1330 if (REAL_EXP (a) != REAL_EXP (b))
1331 return false;
1332 break;
1334 case rvc_nan:
1335 if (a->signalling != b->signalling)
1336 return false;
1337 /* The significand is ignored for canonical NaNs. */
1338 if (a->canonical || b->canonical)
1339 return a->canonical == b->canonical;
1340 break;
1342 default:
1343 gcc_unreachable ();
1346 for (i = 0; i < SIGSZ; ++i)
1347 if (a->sig[i] != b->sig[i])
1348 return false;
1350 return true;
1353 /* Try to change R into its exact multiplicative inverse in format FMT.
1354 Return true if successful. */
1356 bool
1357 exact_real_inverse (format_helper fmt, REAL_VALUE_TYPE *r)
1359 const REAL_VALUE_TYPE *one = real_digit (1);
1360 REAL_VALUE_TYPE u;
1361 int i;
1363 if (r->cl != rvc_normal)
1364 return false;
1366 /* Check for a power of two: all significand bits zero except the MSB. */
1367 for (i = 0; i < SIGSZ-1; ++i)
1368 if (r->sig[i] != 0)
1369 return false;
1370 if (r->sig[SIGSZ-1] != SIG_MSB)
1371 return false;
1373 /* Find the inverse and truncate to the required format. */
1374 do_divide (&u, one, r);
1375 real_convert (&u, fmt, &u);
1377 /* The rounding may have overflowed. */
1378 if (u.cl != rvc_normal)
1379 return false;
1380 for (i = 0; i < SIGSZ-1; ++i)
1381 if (u.sig[i] != 0)
1382 return false;
1383 if (u.sig[SIGSZ-1] != SIG_MSB)
1384 return false;
1386 *r = u;
1387 return true;
1390 /* Return true if arithmetic on values in IMODE that were promoted
1391 from values in TMODE is equivalent to direct arithmetic on values
1392 in TMODE. */
1394 bool
1395 real_can_shorten_arithmetic (machine_mode imode, machine_mode tmode)
1397 const struct real_format *tfmt, *ifmt;
1398 tfmt = REAL_MODE_FORMAT (tmode);
1399 ifmt = REAL_MODE_FORMAT (imode);
1400 /* These conditions are conservative rather than trying to catch the
1401 exact boundary conditions; the main case to allow is IEEE float
1402 and double. */
1403 return (ifmt->b == tfmt->b
1404 && ifmt->p > 2 * tfmt->p
1405 && ifmt->emin < 2 * tfmt->emin - tfmt->p - 2
1406 && ifmt->emin < tfmt->emin - tfmt->emax - tfmt->p - 2
1407 && ifmt->emax > 2 * tfmt->emax + 2
1408 && ifmt->emax > tfmt->emax - tfmt->emin + tfmt->p + 2
1409 && ifmt->round_towards_zero == tfmt->round_towards_zero
1410 && (ifmt->has_sign_dependent_rounding
1411 == tfmt->has_sign_dependent_rounding)
1412 && ifmt->has_nans >= tfmt->has_nans
1413 && ifmt->has_inf >= tfmt->has_inf
1414 && ifmt->has_signed_zero >= tfmt->has_signed_zero
1415 && !MODE_COMPOSITE_P (tmode)
1416 && !MODE_COMPOSITE_P (imode));
1419 /* Render R as an integer. */
1421 HOST_WIDE_INT
1422 real_to_integer (const REAL_VALUE_TYPE *r)
1424 unsigned HOST_WIDE_INT i;
1426 switch (r->cl)
1428 case rvc_zero:
1429 underflow:
1430 return 0;
1432 case rvc_inf:
1433 case rvc_nan:
1434 overflow:
1435 i = HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1);
1436 if (!r->sign)
1437 i--;
1438 return i;
1440 case rvc_normal:
1441 if (r->decimal)
1442 return decimal_real_to_integer (r);
1444 if (REAL_EXP (r) <= 0)
1445 goto underflow;
1446 /* Only force overflow for unsigned overflow. Signed overflow is
1447 undefined, so it doesn't matter what we return, and some callers
1448 expect to be able to use this routine for both signed and
1449 unsigned conversions. */
1450 if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1451 goto overflow;
1453 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1454 i = r->sig[SIGSZ-1];
1455 else
1457 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1458 i = r->sig[SIGSZ-1];
1459 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1460 i |= r->sig[SIGSZ-2];
1463 i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1465 if (r->sign)
1466 i = -i;
1467 return i;
1469 default:
1470 gcc_unreachable ();
1474 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1475 be represented in precision, *FAIL is set to TRUE. */
1477 wide_int
1478 real_to_integer (const REAL_VALUE_TYPE *r, bool *fail, int precision)
1480 HOST_WIDE_INT val[2 * WIDE_INT_MAX_ELTS];
1481 int exp;
1482 int words, w;
1483 wide_int result;
1485 switch (r->cl)
1487 case rvc_zero:
1488 underflow:
1489 return wi::zero (precision);
1491 case rvc_inf:
1492 case rvc_nan:
1493 overflow:
1494 *fail = true;
1496 if (r->sign)
1497 return wi::set_bit_in_zero (precision - 1, precision);
1498 else
1499 return ~wi::set_bit_in_zero (precision - 1, precision);
1501 case rvc_normal:
1502 if (r->decimal)
1503 return decimal_real_to_integer (r, fail, precision);
1505 exp = REAL_EXP (r);
1506 if (exp <= 0)
1507 goto underflow;
1508 /* Only force overflow for unsigned overflow. Signed overflow is
1509 undefined, so it doesn't matter what we return, and some callers
1510 expect to be able to use this routine for both signed and
1511 unsigned conversions. */
1512 if (exp > precision)
1513 goto overflow;
1515 /* Put the significand into a wide_int that has precision W, which
1516 is the smallest HWI-multiple that has at least PRECISION bits.
1517 This ensures that the top bit of the significand is in the
1518 top bit of the wide_int. */
1519 words = (precision + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT;
1520 w = words * HOST_BITS_PER_WIDE_INT;
1522 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1523 for (int i = 0; i < words; i++)
1525 int j = SIGSZ - words + i;
1526 val[i] = (j < 0) ? 0 : r->sig[j];
1528 #else
1529 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1530 for (int i = 0; i < words; i++)
1532 int j = SIGSZ - (words * 2) + (i * 2);
1533 if (j < 0)
1534 val[i] = 0;
1535 else
1536 val[i] = r->sig[j];
1537 j += 1;
1538 if (j >= 0)
1539 val[i] |= (unsigned HOST_WIDE_INT) r->sig[j] << HOST_BITS_PER_LONG;
1541 #endif
1542 /* Shift the value into place and truncate to the desired precision. */
1543 result = wide_int::from_array (val, words, w);
1544 result = wi::lrshift (result, w - exp);
1545 result = wide_int::from (result, precision, UNSIGNED);
1547 if (r->sign)
1548 return -result;
1549 else
1550 return result;
1552 default:
1553 gcc_unreachable ();
1557 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1558 of NUM / DEN. Return the quotient and place the remainder in NUM.
1559 It is expected that NUM / DEN are close enough that the quotient is
1560 small. */
1562 static unsigned long
1563 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1565 unsigned long q, msb;
1566 int expn = REAL_EXP (num), expd = REAL_EXP (den);
1568 if (expn < expd)
1569 return 0;
1571 q = msb = 0;
1572 goto start;
1575 msb = num->sig[SIGSZ-1] & SIG_MSB;
1576 q <<= 1;
1577 lshift_significand_1 (num, num);
1578 start:
1579 if (msb || cmp_significands (num, den) >= 0)
1581 sub_significands (num, num, den, 0);
1582 q |= 1;
1585 while (--expn >= expd);
1587 SET_REAL_EXP (num, expd);
1588 normalize (num);
1590 return q;
1593 /* Render R as a decimal floating point constant. Emit DIGITS significant
1594 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1595 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1596 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1597 to a string that, when parsed back in mode MODE, yields the same value. */
1599 #define M_LOG10_2 0.30102999566398119521
1601 void
1602 real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig,
1603 size_t buf_size, size_t digits,
1604 int crop_trailing_zeros, machine_mode mode)
1606 const struct real_format *fmt = NULL;
1607 const REAL_VALUE_TYPE *one, *ten;
1608 REAL_VALUE_TYPE r, pten, u, v;
1609 int dec_exp, cmp_one, digit;
1610 size_t max_digits;
1611 char *p, *first, *last;
1612 bool sign;
1613 bool round_up;
1615 if (mode != VOIDmode)
1617 fmt = REAL_MODE_FORMAT (mode);
1618 gcc_assert (fmt);
1621 r = *r_orig;
1622 switch (r.cl)
1624 case rvc_zero:
1625 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1626 return;
1627 case rvc_normal:
1628 break;
1629 case rvc_inf:
1630 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1631 return;
1632 case rvc_nan:
1633 /* ??? Print the significand as well, if not canonical? */
1634 sprintf (str, "%c%cNaN", (r_orig->sign ? '-' : '+'),
1635 (r_orig->signalling ? 'S' : 'Q'));
1636 return;
1637 default:
1638 gcc_unreachable ();
1641 if (r.decimal)
1643 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1644 return;
1647 /* Bound the number of digits printed by the size of the representation. */
1648 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1649 if (digits == 0 || digits > max_digits)
1650 digits = max_digits;
1652 /* Estimate the decimal exponent, and compute the length of the string it
1653 will print as. Be conservative and add one to account for possible
1654 overflow or rounding error. */
1655 dec_exp = REAL_EXP (&r) * M_LOG10_2;
1656 for (max_digits = 1; dec_exp ; max_digits++)
1657 dec_exp /= 10;
1659 /* Bound the number of digits printed by the size of the output buffer. */
1660 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1661 gcc_assert (max_digits <= buf_size);
1662 if (digits > max_digits)
1663 digits = max_digits;
1665 one = real_digit (1);
1666 ten = ten_to_ptwo (0);
1668 sign = r.sign;
1669 r.sign = 0;
1671 dec_exp = 0;
1672 pten = *one;
1674 cmp_one = do_compare (&r, one, 0);
1675 if (cmp_one > 0)
1677 int m;
1679 /* Number is greater than one. Convert significand to an integer
1680 and strip trailing decimal zeros. */
1682 u = r;
1683 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1685 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1686 m = floor_log2 (max_digits);
1688 /* Iterate over the bits of the possible powers of 10 that might
1689 be present in U and eliminate them. That is, if we find that
1690 10**2**M divides U evenly, keep the division and increase
1691 DEC_EXP by 2**M. */
1694 REAL_VALUE_TYPE t;
1696 do_divide (&t, &u, ten_to_ptwo (m));
1697 do_fix_trunc (&v, &t);
1698 if (cmp_significands (&v, &t) == 0)
1700 u = t;
1701 dec_exp += 1 << m;
1704 while (--m >= 0);
1706 /* Revert the scaling to integer that we performed earlier. */
1707 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1708 - (SIGNIFICAND_BITS - 1));
1709 r = u;
1711 /* Find power of 10. Do this by dividing out 10**2**M when
1712 this is larger than the current remainder. Fill PTEN with
1713 the power of 10 that we compute. */
1714 if (REAL_EXP (&r) > 0)
1716 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1719 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1720 if (do_compare (&u, ptentwo, 0) >= 0)
1722 do_divide (&u, &u, ptentwo);
1723 do_multiply (&pten, &pten, ptentwo);
1724 dec_exp += 1 << m;
1727 while (--m >= 0);
1729 else
1730 /* We managed to divide off enough tens in the above reduction
1731 loop that we've now got a negative exponent. Fall into the
1732 less-than-one code to compute the proper value for PTEN. */
1733 cmp_one = -1;
1735 if (cmp_one < 0)
1737 int m;
1739 /* Number is less than one. Pad significand with leading
1740 decimal zeros. */
1742 v = r;
1743 while (1)
1745 /* Stop if we'd shift bits off the bottom. */
1746 if (v.sig[0] & 7)
1747 break;
1749 do_multiply (&u, &v, ten);
1751 /* Stop if we're now >= 1 or zero. */
1752 if (REAL_EXP (&u) > 0 || u.cl == rvc_zero)
1753 break;
1755 v = u;
1756 dec_exp -= 1;
1758 r = v;
1760 /* Find power of 10. Do this by multiplying in P=10**2**M when
1761 the current remainder is smaller than 1/P. Fill PTEN with the
1762 power of 10 that we compute. */
1763 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1766 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1767 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1769 if (do_compare (&v, ptenmtwo, 0) <= 0)
1771 do_multiply (&v, &v, ptentwo);
1772 do_multiply (&pten, &pten, ptentwo);
1773 dec_exp -= 1 << m;
1776 while (--m >= 0);
1778 /* Invert the positive power of 10 that we've collected so far. */
1779 do_divide (&pten, one, &pten);
1782 p = str;
1783 if (sign)
1784 *p++ = '-';
1785 first = p++;
1787 /* At this point, PTEN should contain the nearest power of 10 smaller
1788 than R, such that this division produces the first digit.
1790 Using a divide-step primitive that returns the complete integral
1791 remainder avoids the rounding error that would be produced if
1792 we were to use do_divide here and then simply multiply by 10 for
1793 each subsequent digit. */
1795 digit = rtd_divmod (&r, &pten);
1797 /* Be prepared for error in that division via underflow ... */
1798 if (digit == 0 && cmp_significand_0 (&r))
1800 /* Multiply by 10 and try again. */
1801 do_multiply (&r, &r, ten);
1802 digit = rtd_divmod (&r, &pten);
1803 dec_exp -= 1;
1804 gcc_assert (digit != 0);
1807 /* ... or overflow. */
1808 if (digit == 10)
1810 *p++ = '1';
1811 if (--digits > 0)
1812 *p++ = '0';
1813 dec_exp += 1;
1815 else
1817 gcc_assert (digit <= 10);
1818 *p++ = digit + '0';
1821 /* Generate subsequent digits. */
1822 while (--digits > 0)
1824 do_multiply (&r, &r, ten);
1825 digit = rtd_divmod (&r, &pten);
1826 *p++ = digit + '0';
1828 last = p;
1830 /* Generate one more digit with which to do rounding. */
1831 do_multiply (&r, &r, ten);
1832 digit = rtd_divmod (&r, &pten);
1834 /* Round the result. */
1835 if (fmt && fmt->round_towards_zero)
1837 /* If the format uses round towards zero when parsing the string
1838 back in, we need to always round away from zero here. */
1839 if (cmp_significand_0 (&r))
1840 digit++;
1841 round_up = digit > 0;
1843 else
1845 if (digit == 5)
1847 /* Round to nearest. If R is nonzero there are additional
1848 nonzero digits to be extracted. */
1849 if (cmp_significand_0 (&r))
1850 digit++;
1851 /* Round to even. */
1852 else if ((p[-1] - '0') & 1)
1853 digit++;
1856 round_up = digit > 5;
1859 if (round_up)
1861 while (p > first)
1863 digit = *--p;
1864 if (digit == '9')
1865 *p = '0';
1866 else
1868 *p = digit + 1;
1869 break;
1873 /* Carry out of the first digit. This means we had all 9's and
1874 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1875 if (p == first)
1877 first[1] = '1';
1878 dec_exp++;
1882 /* Insert the decimal point. */
1883 first[0] = first[1];
1884 first[1] = '.';
1886 /* If requested, drop trailing zeros. Never crop past "1.0". */
1887 if (crop_trailing_zeros)
1888 while (last > first + 3 && last[-1] == '0')
1889 last--;
1891 /* Append the exponent. */
1892 sprintf (last, "e%+d", dec_exp);
1894 /* Verify that we can read the original value back in. */
1895 if (flag_checking && mode != VOIDmode)
1897 real_from_string (&r, str);
1898 real_convert (&r, mode, &r);
1899 gcc_assert (real_identical (&r, r_orig));
1903 /* Likewise, except always uses round-to-nearest. */
1905 void
1906 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1907 size_t digits, int crop_trailing_zeros)
1909 real_to_decimal_for_mode (str, r_orig, buf_size,
1910 digits, crop_trailing_zeros, VOIDmode);
1913 DEBUG_FUNCTION void
1914 debug (const REAL_VALUE_TYPE &r)
1916 char s[60];
1917 real_to_hexadecimal (s, &r, sizeof (s), 0, 1);
1918 fprintf (stderr, "%s\n", s);
1921 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1922 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1923 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1924 strip trailing zeros. */
1926 void
1927 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1928 size_t digits, int crop_trailing_zeros)
1930 int i, j, exp = REAL_EXP (r);
1931 char *p, *first;
1932 char exp_buf[16];
1933 size_t max_digits;
1935 switch (r->cl)
1937 case rvc_zero:
1938 exp = 0;
1939 break;
1940 case rvc_normal:
1941 break;
1942 case rvc_inf:
1943 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1944 return;
1945 case rvc_nan:
1946 /* ??? Print the significand as well, if not canonical? */
1947 sprintf (str, "%c%cNaN", (r->sign ? '-' : '+'),
1948 (r->signalling ? 'S' : 'Q'));
1949 return;
1950 default:
1951 gcc_unreachable ();
1954 if (r->decimal)
1956 /* Hexadecimal format for decimal floats is not interesting. */
1957 strcpy (str, "N/A");
1958 return;
1961 if (digits == 0)
1962 digits = SIGNIFICAND_BITS / 4;
1964 /* Bound the number of digits printed by the size of the output buffer. */
1966 sprintf (exp_buf, "p%+d", exp);
1967 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1968 gcc_assert (max_digits <= buf_size);
1969 if (digits > max_digits)
1970 digits = max_digits;
1972 p = str;
1973 if (r->sign)
1974 *p++ = '-';
1975 *p++ = '0';
1976 *p++ = 'x';
1977 *p++ = '0';
1978 *p++ = '.';
1979 first = p;
1981 for (i = SIGSZ - 1; i >= 0; --i)
1982 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1984 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1985 if (--digits == 0)
1986 goto out;
1989 out:
1990 if (crop_trailing_zeros)
1991 while (p > first + 1 && p[-1] == '0')
1992 p--;
1994 sprintf (p, "p%+d", exp);
1997 /* Initialize R from a decimal or hexadecimal string. The string is
1998 assumed to have been syntax checked already. Return -1 if the
1999 value underflows, +1 if overflows, and 0 otherwise. */
2002 real_from_string (REAL_VALUE_TYPE *r, const char *str)
2004 int exp = 0;
2005 bool sign = false;
2007 get_zero (r, 0);
2009 if (*str == '-')
2011 sign = true;
2012 str++;
2014 else if (*str == '+')
2015 str++;
2017 if (startswith (str, "QNaN"))
2019 get_canonical_qnan (r, sign);
2020 return 0;
2022 else if (startswith (str, "SNaN"))
2024 get_canonical_snan (r, sign);
2025 return 0;
2027 else if (startswith (str, "Inf"))
2029 get_inf (r, sign);
2030 return 0;
2033 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
2035 /* Hexadecimal floating point. */
2036 int pos = SIGNIFICAND_BITS - 4, d;
2038 str += 2;
2040 while (*str == '0')
2041 str++;
2042 while (1)
2044 d = hex_value (*str);
2045 if (d == _hex_bad)
2046 break;
2047 if (pos >= 0)
2049 r->sig[pos / HOST_BITS_PER_LONG]
2050 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
2051 pos -= 4;
2053 else if (d)
2054 /* Ensure correct rounding by setting last bit if there is
2055 a subsequent nonzero digit. */
2056 r->sig[0] |= 1;
2057 exp += 4;
2058 str++;
2060 if (*str == '.')
2062 str++;
2063 if (pos == SIGNIFICAND_BITS - 4)
2065 while (*str == '0')
2066 str++, exp -= 4;
2068 while (1)
2070 d = hex_value (*str);
2071 if (d == _hex_bad)
2072 break;
2073 if (pos >= 0)
2075 r->sig[pos / HOST_BITS_PER_LONG]
2076 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
2077 pos -= 4;
2079 else if (d)
2080 /* Ensure correct rounding by setting last bit if there is
2081 a subsequent nonzero digit. */
2082 r->sig[0] |= 1;
2083 str++;
2087 /* If the mantissa is zero, ignore the exponent. */
2088 if (!cmp_significand_0 (r))
2089 goto is_a_zero;
2091 if (*str == 'p' || *str == 'P')
2093 bool exp_neg = false;
2095 str++;
2096 if (*str == '-')
2098 exp_neg = true;
2099 str++;
2101 else if (*str == '+')
2102 str++;
2104 d = 0;
2105 while (ISDIGIT (*str))
2107 d *= 10;
2108 d += *str - '0';
2109 if (d > MAX_EXP)
2111 /* Overflowed the exponent. */
2112 if (exp_neg)
2113 goto underflow;
2114 else
2115 goto overflow;
2117 str++;
2119 if (exp_neg)
2120 d = -d;
2122 exp += d;
2125 r->cl = rvc_normal;
2126 SET_REAL_EXP (r, exp);
2128 normalize (r);
2130 else
2132 /* Decimal floating point. */
2133 const char *cstr = str;
2134 bool inexact;
2136 while (*cstr == '0')
2137 cstr++;
2138 if (*cstr == '.')
2140 cstr++;
2141 while (*cstr == '0')
2142 cstr++;
2145 /* If the mantissa is zero, ignore the exponent. */
2146 if (!ISDIGIT (*cstr))
2147 goto is_a_zero;
2149 /* Nonzero value, possibly overflowing or underflowing. */
2150 auto_mpfr m (SIGNIFICAND_BITS);
2151 inexact = mpfr_strtofr (m, str, NULL, 10, MPFR_RNDZ);
2152 /* The result should never be a NaN, and because the rounding is
2153 toward zero should never be an infinity. */
2154 gcc_assert (!mpfr_nan_p (m) && !mpfr_inf_p (m));
2155 if (mpfr_zero_p (m) || mpfr_get_exp (m) < -MAX_EXP + 4)
2156 goto underflow;
2157 else if (mpfr_get_exp (m) > MAX_EXP - 4)
2158 goto overflow;
2159 else
2161 real_from_mpfr (r, m, NULL_TREE, MPFR_RNDZ);
2162 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2163 because the hex digits used in real_from_mpfr did not
2164 start with a digit 8 to f, but the exponent bounds above
2165 should have avoided underflow or overflow. */
2166 gcc_assert (r->cl == rvc_normal);
2167 /* Set a sticky bit if mpfr_strtofr was inexact. */
2168 r->sig[0] |= inexact;
2172 r->sign = sign;
2173 return 0;
2175 is_a_zero:
2176 get_zero (r, sign);
2177 return 0;
2179 underflow:
2180 get_zero (r, sign);
2181 return -1;
2183 overflow:
2184 get_inf (r, sign);
2185 return 1;
2188 /* Legacy. Similar, but return the result directly. */
2190 REAL_VALUE_TYPE
2191 real_from_string2 (const char *s, format_helper fmt)
2193 REAL_VALUE_TYPE r;
2195 real_from_string (&r, s);
2196 if (fmt)
2197 real_convert (&r, fmt, &r);
2199 return r;
2202 /* Initialize R from string S and desired format FMT. */
2204 void
2205 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, format_helper fmt)
2207 if (fmt.decimal_p ())
2208 decimal_real_from_string (r, s);
2209 else
2210 real_from_string (r, s);
2212 if (fmt)
2213 real_convert (r, fmt, r);
2216 /* Initialize R from the wide_int VAL_IN. Round it to format FMT if
2217 FMT is nonnull. */
2219 void
2220 real_from_integer (REAL_VALUE_TYPE *r, format_helper fmt,
2221 const wide_int_ref &val_in, signop sgn)
2223 if (val_in == 0)
2224 get_zero (r, 0);
2225 else
2227 unsigned int len = val_in.get_precision ();
2228 int i, j, e = 0;
2229 int maxbitlen = MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT;
2230 const unsigned int realmax = (SIGNIFICAND_BITS / HOST_BITS_PER_WIDE_INT
2231 * HOST_BITS_PER_WIDE_INT);
2233 memset (r, 0, sizeof (*r));
2234 r->cl = rvc_normal;
2235 r->sign = wi::neg_p (val_in, sgn);
2237 /* We have to ensure we can negate the largest negative number. */
2238 wide_int val = wide_int::from (val_in, maxbitlen, sgn);
2240 if (r->sign)
2241 val = -val;
2243 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2244 won't work with precisions that are not a multiple of
2245 HOST_BITS_PER_WIDE_INT. */
2246 len += HOST_BITS_PER_WIDE_INT - 1;
2248 /* Ensure we can represent the largest negative number. */
2249 len += 1;
2251 len = len/HOST_BITS_PER_WIDE_INT * HOST_BITS_PER_WIDE_INT;
2253 /* Cap the size to the size allowed by real.h. */
2254 if (len > realmax)
2256 HOST_WIDE_INT cnt_l_z;
2257 cnt_l_z = wi::clz (val);
2259 if (maxbitlen - cnt_l_z > realmax)
2261 e = maxbitlen - cnt_l_z - realmax;
2263 /* This value is too large, we must shift it right to
2264 preserve all the bits we can, and then bump the
2265 exponent up by that amount. */
2266 val = wi::lrshift (val, e);
2268 len = realmax;
2271 /* Clear out top bits so elt will work with precisions that aren't
2272 a multiple of HOST_BITS_PER_WIDE_INT. */
2273 val = wide_int::from (val, len, sgn);
2274 len = len / HOST_BITS_PER_WIDE_INT;
2276 SET_REAL_EXP (r, len * HOST_BITS_PER_WIDE_INT + e);
2278 j = SIGSZ - 1;
2279 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2280 for (i = len - 1; i >= 0; i--)
2282 r->sig[j--] = val.elt (i);
2283 if (j < 0)
2284 break;
2286 else
2288 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2289 for (i = len - 1; i >= 0; i--)
2291 HOST_WIDE_INT e = val.elt (i);
2292 r->sig[j--] = e >> (HOST_BITS_PER_LONG - 1) >> 1;
2293 if (j < 0)
2294 break;
2295 r->sig[j--] = e;
2296 if (j < 0)
2297 break;
2301 normalize (r);
2304 if (fmt.decimal_p ())
2305 decimal_from_integer (r);
2306 if (fmt)
2307 real_convert (r, fmt, r);
2310 /* Render R, an integral value, as a floating point constant with no
2311 specified exponent. */
2313 static void
2314 decimal_integer_string (char *str, const REAL_VALUE_TYPE *r_orig,
2315 size_t buf_size)
2317 int dec_exp, digit, digits;
2318 REAL_VALUE_TYPE r, pten;
2319 char *p;
2320 bool sign;
2322 r = *r_orig;
2324 if (r.cl == rvc_zero)
2326 strcpy (str, "0.");
2327 return;
2330 sign = r.sign;
2331 r.sign = 0;
2333 dec_exp = REAL_EXP (&r) * M_LOG10_2;
2334 digits = dec_exp + 1;
2335 gcc_assert ((digits + 2) < (int)buf_size);
2337 pten = *real_digit (1);
2338 times_pten (&pten, dec_exp);
2340 p = str;
2341 if (sign)
2342 *p++ = '-';
2344 digit = rtd_divmod (&r, &pten);
2345 gcc_assert (digit >= 0 && digit <= 9);
2346 *p++ = digit + '0';
2347 while (--digits > 0)
2349 times_pten (&r, 1);
2350 digit = rtd_divmod (&r, &pten);
2351 *p++ = digit + '0';
2353 *p++ = '.';
2354 *p++ = '\0';
2357 /* Convert a real with an integral value to decimal float. */
2359 static void
2360 decimal_from_integer (REAL_VALUE_TYPE *r)
2362 char str[256];
2364 decimal_integer_string (str, r, sizeof (str) - 1);
2365 decimal_real_from_string (r, str);
2368 /* Returns 10**2**N. */
2370 static const REAL_VALUE_TYPE *
2371 ten_to_ptwo (int n)
2373 static REAL_VALUE_TYPE tens[EXP_BITS];
2375 gcc_assert (n >= 0);
2376 gcc_assert (n < EXP_BITS);
2378 if (tens[n].cl == rvc_zero)
2380 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2382 HOST_WIDE_INT t = 10;
2383 int i;
2385 for (i = 0; i < n; ++i)
2386 t *= t;
2388 real_from_integer (&tens[n], VOIDmode, t, UNSIGNED);
2390 else
2392 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2393 do_multiply (&tens[n], t, t);
2397 return &tens[n];
2400 /* Returns 10**(-2**N). */
2402 static const REAL_VALUE_TYPE *
2403 ten_to_mptwo (int n)
2405 static REAL_VALUE_TYPE tens[EXP_BITS];
2407 gcc_assert (n >= 0);
2408 gcc_assert (n < EXP_BITS);
2410 if (tens[n].cl == rvc_zero)
2411 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2413 return &tens[n];
2416 /* Returns N. */
2418 static const REAL_VALUE_TYPE *
2419 real_digit (int n)
2421 static REAL_VALUE_TYPE num[10];
2423 gcc_assert (n >= 0);
2424 gcc_assert (n <= 9);
2426 if (n > 0 && num[n].cl == rvc_zero)
2427 real_from_integer (&num[n], VOIDmode, n, UNSIGNED);
2429 return &num[n];
2432 /* Multiply R by 10**EXP. */
2434 static void
2435 times_pten (REAL_VALUE_TYPE *r, int exp)
2437 REAL_VALUE_TYPE pten, *rr;
2438 bool negative = (exp < 0);
2439 int i;
2441 if (negative)
2443 exp = -exp;
2444 pten = *real_digit (1);
2445 rr = &pten;
2447 else
2448 rr = r;
2450 for (i = 0; exp > 0; ++i, exp >>= 1)
2451 if (exp & 1)
2452 do_multiply (rr, rr, ten_to_ptwo (i));
2454 if (negative)
2455 do_divide (r, r, &pten);
2458 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2460 const REAL_VALUE_TYPE *
2461 dconst_e_ptr (void)
2463 static REAL_VALUE_TYPE value;
2465 /* Initialize mathematical constants for constant folding builtins.
2466 These constants need to be given to at least 160 bits precision. */
2467 if (value.cl == rvc_zero)
2469 auto_mpfr m (SIGNIFICAND_BITS);
2470 mpfr_set_ui (m, 1, MPFR_RNDN);
2471 mpfr_exp (m, m, MPFR_RNDN);
2472 real_from_mpfr (&value, m, NULL_TREE, MPFR_RNDN);
2475 return &value;
2478 /* Returns the special REAL_VALUE_TYPE corresponding to 'pi'. */
2480 const REAL_VALUE_TYPE *
2481 dconst_pi_ptr (void)
2483 static REAL_VALUE_TYPE value;
2485 /* Initialize mathematical constants for constant folding builtins.
2486 These constants need to be given to at least 160 bits precision. */
2487 if (value.cl == rvc_zero)
2489 auto_mpfr m (SIGNIFICAND_BITS);
2490 mpfr_set_si (m, -1, MPFR_RNDN);
2491 mpfr_acos (m, m, MPFR_RNDN);
2492 real_from_mpfr (&value, m, NULL_TREE, MPFR_RNDN);
2495 return &value;
2498 /* Returns a cached REAL_VALUE_TYPE corresponding to 1/n, for various n. */
2500 #define CACHED_FRACTION(NAME, N) \
2501 const REAL_VALUE_TYPE * \
2502 NAME (void) \
2504 static REAL_VALUE_TYPE value; \
2506 /* Initialize mathematical constants for constant folding builtins. \
2507 These constants need to be given to at least 160 bits \
2508 precision. */ \
2509 if (value.cl == rvc_zero) \
2510 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (N)); \
2511 return &value; \
2514 CACHED_FRACTION (dconst_third_ptr, 3)
2515 CACHED_FRACTION (dconst_quarter_ptr, 4)
2516 CACHED_FRACTION (dconst_sixth_ptr, 6)
2517 CACHED_FRACTION (dconst_ninth_ptr, 9)
2519 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2521 const REAL_VALUE_TYPE *
2522 dconst_sqrt2_ptr (void)
2524 static REAL_VALUE_TYPE value;
2526 /* Initialize mathematical constants for constant folding builtins.
2527 These constants need to be given to at least 160 bits precision. */
2528 if (value.cl == rvc_zero)
2530 auto_mpfr m (SIGNIFICAND_BITS);
2531 mpfr_sqrt_ui (m, 2, MPFR_RNDN);
2532 real_from_mpfr (&value, m, NULL_TREE, MPFR_RNDN);
2534 return &value;
2537 /* Fills R with Inf with SIGN. */
2539 void
2540 real_inf (REAL_VALUE_TYPE *r, bool sign)
2542 get_inf (r, sign);
2545 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2546 we force a QNaN, else we force an SNaN. The string, if not empty,
2547 is parsed as a number and placed in the significand. Return true
2548 if the string was successfully parsed. */
2550 bool
2551 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2552 format_helper fmt)
2554 if (*str == 0)
2556 if (quiet)
2557 get_canonical_qnan (r, 0);
2558 else
2559 get_canonical_snan (r, 0);
2561 else
2563 int base = 10, d;
2565 memset (r, 0, sizeof (*r));
2566 r->cl = rvc_nan;
2568 /* Parse akin to strtol into the significand of R. */
2570 while (ISSPACE (*str))
2571 str++;
2572 if (*str == '-')
2573 str++;
2574 else if (*str == '+')
2575 str++;
2576 if (*str == '0')
2578 str++;
2579 if (*str == 'x' || *str == 'X')
2581 base = 16;
2582 str++;
2584 else
2585 base = 8;
2588 while ((d = hex_value (*str)) < base)
2590 REAL_VALUE_TYPE u;
2592 switch (base)
2594 case 8:
2595 lshift_significand (r, r, 3);
2596 break;
2597 case 16:
2598 lshift_significand (r, r, 4);
2599 break;
2600 case 10:
2601 lshift_significand_1 (&u, r);
2602 lshift_significand (r, r, 3);
2603 add_significands (r, r, &u);
2604 break;
2605 default:
2606 gcc_unreachable ();
2609 get_zero (&u, 0);
2610 u.sig[0] = d;
2611 add_significands (r, r, &u);
2613 str++;
2616 /* Must have consumed the entire string for success. */
2617 if (*str != 0)
2618 return false;
2620 /* Shift the significand into place such that the bits
2621 are in the most significant bits for the format. */
2622 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2624 /* Our MSB is always unset for NaNs. */
2625 r->sig[SIGSZ-1] &= ~SIG_MSB;
2627 /* Force quiet or signaling NaN. */
2628 r->signalling = !quiet;
2631 return true;
2634 /* Fills R with the largest finite value representable in mode MODE.
2635 If SIGN is nonzero, R is set to the most negative finite value. */
2637 void
2638 real_maxval (REAL_VALUE_TYPE *r, int sign, machine_mode mode)
2640 const struct real_format *fmt;
2641 int np2;
2643 fmt = REAL_MODE_FORMAT (mode);
2644 gcc_assert (fmt);
2645 memset (r, 0, sizeof (*r));
2647 if (fmt->b == 10)
2648 decimal_real_maxval (r, sign, mode);
2649 else
2651 r->cl = rvc_normal;
2652 r->sign = sign;
2653 SET_REAL_EXP (r, fmt->emax);
2655 np2 = SIGNIFICAND_BITS - fmt->p;
2656 memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2657 clear_significand_below (r, np2);
2659 if (fmt->pnan < fmt->p)
2660 /* This is an IBM extended double format made up of two IEEE
2661 doubles. The value of the long double is the sum of the
2662 values of the two parts. The most significant part is
2663 required to be the value of the long double rounded to the
2664 nearest double. Rounding means we need a slightly smaller
2665 value for LDBL_MAX. */
2666 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1);
2670 /* Fills R with 2**N. */
2672 void
2673 real_2expN (REAL_VALUE_TYPE *r, int n, format_helper fmt)
2675 memset (r, 0, sizeof (*r));
2677 n++;
2678 if (n > MAX_EXP)
2679 r->cl = rvc_inf;
2680 else if (n < -MAX_EXP)
2682 else
2684 r->cl = rvc_normal;
2685 SET_REAL_EXP (r, n);
2686 r->sig[SIGSZ-1] = SIG_MSB;
2688 if (fmt.decimal_p ())
2689 decimal_real_convert (r, fmt, r);
2693 static void
2694 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2696 int p2, np2, i, w;
2697 int emin2m1, emax2;
2698 bool round_up = false;
2700 if (r->decimal)
2702 if (fmt->b == 10)
2704 decimal_round_for_format (fmt, r);
2705 return;
2707 /* FIXME. We can come here via fp_easy_constant
2708 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2709 investigated whether this convert needs to be here, or
2710 something else is missing. */
2711 decimal_real_convert (r, REAL_MODE_FORMAT (DFmode), r);
2714 p2 = fmt->p;
2715 emin2m1 = fmt->emin - 1;
2716 emax2 = fmt->emax;
2718 np2 = SIGNIFICAND_BITS - p2;
2719 switch (r->cl)
2721 underflow:
2722 get_zero (r, r->sign);
2723 /* FALLTHRU */
2724 case rvc_zero:
2725 if (!fmt->has_signed_zero)
2726 r->sign = 0;
2727 return;
2729 overflow:
2730 get_inf (r, r->sign);
2731 case rvc_inf:
2732 return;
2734 case rvc_nan:
2735 clear_significand_below (r, np2);
2736 return;
2738 case rvc_normal:
2739 break;
2741 default:
2742 gcc_unreachable ();
2745 /* Check the range of the exponent. If we're out of range,
2746 either underflow or overflow. */
2747 if (REAL_EXP (r) > emax2)
2748 goto overflow;
2749 else if (REAL_EXP (r) <= emin2m1)
2751 int diff;
2753 if (!fmt->has_denorm)
2755 /* Don't underflow completely until we've had a chance to round. */
2756 if (REAL_EXP (r) < emin2m1)
2757 goto underflow;
2759 else
2761 diff = emin2m1 - REAL_EXP (r) + 1;
2762 if (diff > p2)
2763 goto underflow;
2765 /* De-normalize the significand. */
2766 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2767 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2771 if (!fmt->round_towards_zero)
2773 /* There are P2 true significand bits, followed by one guard bit,
2774 followed by one sticky bit, followed by stuff. Fold nonzero
2775 stuff into the sticky bit. */
2776 unsigned long sticky;
2777 bool guard, lsb;
2779 sticky = 0;
2780 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2781 sticky |= r->sig[i];
2782 sticky |= r->sig[w]
2783 & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2785 guard = test_significand_bit (r, np2 - 1);
2786 lsb = test_significand_bit (r, np2);
2788 /* Round to even. */
2789 round_up = guard && (sticky || lsb);
2792 if (round_up)
2794 REAL_VALUE_TYPE u;
2795 get_zero (&u, 0);
2796 set_significand_bit (&u, np2);
2798 if (add_significands (r, r, &u))
2800 /* Overflow. Means the significand had been all ones, and
2801 is now all zeros. Need to increase the exponent, and
2802 possibly re-normalize it. */
2803 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2804 if (REAL_EXP (r) > emax2)
2805 goto overflow;
2806 r->sig[SIGSZ-1] = SIG_MSB;
2810 /* Catch underflow that we deferred until after rounding. */
2811 if (REAL_EXP (r) <= emin2m1)
2812 goto underflow;
2814 /* Clear out trailing garbage. */
2815 clear_significand_below (r, np2);
2818 /* Extend or truncate to a new format. */
2820 void
2821 real_convert (REAL_VALUE_TYPE *r, format_helper fmt,
2822 const REAL_VALUE_TYPE *a)
2824 *r = *a;
2826 if (a->decimal || fmt->b == 10)
2827 decimal_real_convert (r, fmt, a);
2829 round_for_format (fmt, r);
2831 /* Make resulting NaN value to be qNaN. The caller has the
2832 responsibility to avoid the operation if flag_signaling_nans
2833 is on. */
2834 if (r->cl == rvc_nan)
2835 r->signalling = 0;
2837 /* round_for_format de-normalizes denormals. Undo just that part. */
2838 if (r->cl == rvc_normal)
2839 normalize (r);
2842 /* Legacy. Likewise, except return the struct directly. */
2844 REAL_VALUE_TYPE
2845 real_value_truncate (format_helper fmt, REAL_VALUE_TYPE a)
2847 REAL_VALUE_TYPE r;
2848 real_convert (&r, fmt, &a);
2849 return r;
2852 /* Return true if truncating to FMT is exact. */
2854 bool
2855 exact_real_truncate (format_helper fmt, const REAL_VALUE_TYPE *a)
2857 REAL_VALUE_TYPE t;
2858 int emin2m1;
2860 /* Don't allow conversion to denormals. */
2861 emin2m1 = fmt->emin - 1;
2862 if (REAL_EXP (a) <= emin2m1)
2863 return false;
2865 /* After conversion to the new format, the value must be identical. */
2866 real_convert (&t, fmt, a);
2867 return real_identical (&t, a);
2870 /* Write R to the given target format. Place the words of the result
2871 in target word order in BUF. There are always 32 bits in each
2872 long, no matter the size of the host long.
2874 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2876 long
2877 real_to_target (long *buf, const REAL_VALUE_TYPE *r_orig,
2878 format_helper fmt)
2880 REAL_VALUE_TYPE r;
2881 long buf1;
2883 r = *r_orig;
2884 round_for_format (fmt, &r);
2886 if (!buf)
2887 buf = &buf1;
2888 (*fmt->encode) (fmt, buf, &r);
2890 return *buf;
2893 /* Read R from the given target format. Read the words of the result
2894 in target word order in BUF. There are always 32 bits in each
2895 long, no matter the size of the host long. */
2897 void
2898 real_from_target (REAL_VALUE_TYPE *r, const long *buf, format_helper fmt)
2900 (*fmt->decode) (fmt, r, buf);
2903 /* Return the number of bits of the largest binary value that the
2904 significand of FMT will hold. */
2905 /* ??? Legacy. Should get access to real_format directly. */
2908 significand_size (format_helper fmt)
2910 if (fmt == NULL)
2911 return 0;
2913 if (fmt->b == 10)
2915 /* Return the size in bits of the largest binary value that can be
2916 held by the decimal coefficient for this format. This is one more
2917 than the number of bits required to hold the largest coefficient
2918 of this format. */
2919 double log2_10 = 3.3219281;
2920 return fmt->p * log2_10;
2922 return fmt->p;
2925 /* Return a hash value for the given real value. */
2926 /* ??? The "unsigned int" return value is intended to be hashval_t,
2927 but I didn't want to pull hashtab.h into real.h. */
2929 unsigned int
2930 real_hash (const REAL_VALUE_TYPE *r)
2932 unsigned int h;
2933 size_t i;
2935 h = r->cl | (r->sign << 2);
2936 switch (r->cl)
2938 case rvc_zero:
2939 case rvc_inf:
2940 return h;
2942 case rvc_normal:
2943 h |= (unsigned int)REAL_EXP (r) << 3;
2944 break;
2946 case rvc_nan:
2947 if (r->signalling)
2948 h ^= (unsigned int)-1;
2949 if (r->canonical)
2950 return h;
2951 break;
2953 default:
2954 gcc_unreachable ();
2957 if (sizeof (unsigned long) > sizeof (unsigned int))
2958 for (i = 0; i < SIGSZ; ++i)
2960 unsigned long s = r->sig[i];
2961 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2963 else
2964 for (i = 0; i < SIGSZ; ++i)
2965 h ^= r->sig[i];
2967 return h;
2970 /* IEEE single-precision format. */
2972 static void encode_ieee_single (const struct real_format *fmt,
2973 long *, const REAL_VALUE_TYPE *);
2974 static void decode_ieee_single (const struct real_format *,
2975 REAL_VALUE_TYPE *, const long *);
2977 static void
2978 encode_ieee_single (const struct real_format *fmt, long *buf,
2979 const REAL_VALUE_TYPE *r)
2981 unsigned long image, sig, exp;
2982 unsigned long sign = r->sign;
2984 image = sign << 31;
2985 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2987 switch (r->cl)
2989 case rvc_zero:
2990 break;
2992 case rvc_inf:
2993 if (fmt->has_inf)
2994 image |= 255 << 23;
2995 else
2996 image |= 0x7fffffff;
2997 break;
2999 case rvc_nan:
3000 if (fmt->has_nans)
3002 if (r->canonical)
3003 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
3004 if (r->signalling == fmt->qnan_msb_set)
3005 sig &= ~(1 << 22);
3006 else
3007 sig |= 1 << 22;
3008 if (sig == 0)
3009 sig = 1 << 21;
3011 image |= 255 << 23;
3012 image |= sig;
3014 else
3015 image |= 0x7fffffff;
3016 break;
3018 case rvc_normal:
3019 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3020 whereas the intermediate representation is 0.F x 2**exp.
3021 Which means we're off by one. */
3022 if (real_isdenormal (r))
3023 exp = 0;
3024 else
3025 exp = REAL_EXP (r) + 127 - 1;
3026 image |= exp << 23;
3027 image |= sig;
3028 break;
3030 default:
3031 gcc_unreachable ();
3034 buf[0] = image;
3037 static void
3038 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3039 const long *buf)
3041 unsigned long image = buf[0] & 0xffffffff;
3042 bool sign = (image >> 31) & 1;
3043 int exp = (image >> 23) & 0xff;
3045 memset (r, 0, sizeof (*r));
3046 image <<= HOST_BITS_PER_LONG - 24;
3047 image &= ~SIG_MSB;
3049 if (exp == 0)
3051 if (image && fmt->has_denorm)
3053 r->cl = rvc_normal;
3054 r->sign = sign;
3055 SET_REAL_EXP (r, -126);
3056 r->sig[SIGSZ-1] = image << 1;
3057 normalize (r);
3059 else if (fmt->has_signed_zero)
3060 r->sign = sign;
3062 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
3064 if (image)
3066 r->cl = rvc_nan;
3067 r->sign = sign;
3068 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
3069 ^ fmt->qnan_msb_set);
3070 r->sig[SIGSZ-1] = image;
3072 else
3074 r->cl = rvc_inf;
3075 r->sign = sign;
3078 else
3080 r->cl = rvc_normal;
3081 r->sign = sign;
3082 SET_REAL_EXP (r, exp - 127 + 1);
3083 r->sig[SIGSZ-1] = image | SIG_MSB;
3087 const struct real_format ieee_single_format =
3089 encode_ieee_single,
3090 decode_ieee_single,
3094 -125,
3095 128,
3099 false,
3100 true,
3101 true,
3102 true,
3103 true,
3104 true,
3105 true,
3106 false,
3107 "ieee_single"
3110 const struct real_format mips_single_format =
3112 encode_ieee_single,
3113 decode_ieee_single,
3117 -125,
3118 128,
3122 false,
3123 true,
3124 true,
3125 true,
3126 true,
3127 true,
3128 false,
3129 true,
3130 "mips_single"
3133 const struct real_format motorola_single_format =
3135 encode_ieee_single,
3136 decode_ieee_single,
3140 -125,
3141 128,
3145 false,
3146 true,
3147 true,
3148 true,
3149 true,
3150 true,
3151 true,
3152 true,
3153 "motorola_single"
3156 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3157 single precision with the following differences:
3158 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3159 are generated.
3160 - NaNs are not supported.
3161 - The range of non-zero numbers in binary is
3162 (001)[1.]000...000 to (255)[1.]111...111.
3163 - Denormals can be represented, but are treated as +0.0 when
3164 used as an operand and are never generated as a result.
3165 - -0.0 can be represented, but a zero result is always +0.0.
3166 - the only supported rounding mode is trunction (towards zero). */
3167 const struct real_format spu_single_format =
3169 encode_ieee_single,
3170 decode_ieee_single,
3174 -125,
3175 129,
3179 true,
3180 false,
3181 false,
3182 false,
3183 true,
3184 true,
3185 false,
3186 false,
3187 "spu_single"
3190 /* IEEE double-precision format. */
3192 static void encode_ieee_double (const struct real_format *fmt,
3193 long *, const REAL_VALUE_TYPE *);
3194 static void decode_ieee_double (const struct real_format *,
3195 REAL_VALUE_TYPE *, const long *);
3197 static void
3198 encode_ieee_double (const struct real_format *fmt, long *buf,
3199 const REAL_VALUE_TYPE *r)
3201 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
3202 unsigned long sign = r->sign;
3204 image_hi = sign << 31;
3205 image_lo = 0;
3207 if (HOST_BITS_PER_LONG == 64)
3209 sig_hi = r->sig[SIGSZ-1];
3210 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
3211 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
3213 else
3215 sig_hi = r->sig[SIGSZ-1];
3216 sig_lo = r->sig[SIGSZ-2];
3217 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
3218 sig_hi = (sig_hi >> 11) & 0xfffff;
3221 switch (r->cl)
3223 case rvc_zero:
3224 break;
3226 case rvc_inf:
3227 if (fmt->has_inf)
3228 image_hi |= 2047 << 20;
3229 else
3231 image_hi |= 0x7fffffff;
3232 image_lo = 0xffffffff;
3234 break;
3236 case rvc_nan:
3237 if (fmt->has_nans)
3239 if (r->canonical)
3241 if (fmt->canonical_nan_lsbs_set)
3243 sig_hi = (1 << 19) - 1;
3244 sig_lo = 0xffffffff;
3246 else
3248 sig_hi = 0;
3249 sig_lo = 0;
3252 if (r->signalling == fmt->qnan_msb_set)
3253 sig_hi &= ~(1 << 19);
3254 else
3255 sig_hi |= 1 << 19;
3256 if (sig_hi == 0 && sig_lo == 0)
3257 sig_hi = 1 << 18;
3259 image_hi |= 2047 << 20;
3260 image_hi |= sig_hi;
3261 image_lo = sig_lo;
3263 else
3265 image_hi |= 0x7fffffff;
3266 image_lo = 0xffffffff;
3268 break;
3270 case rvc_normal:
3271 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3272 whereas the intermediate representation is 0.F x 2**exp.
3273 Which means we're off by one. */
3274 if (real_isdenormal (r))
3275 exp = 0;
3276 else
3277 exp = REAL_EXP (r) + 1023 - 1;
3278 image_hi |= exp << 20;
3279 image_hi |= sig_hi;
3280 image_lo = sig_lo;
3281 break;
3283 default:
3284 gcc_unreachable ();
3287 if (FLOAT_WORDS_BIG_ENDIAN)
3288 buf[0] = image_hi, buf[1] = image_lo;
3289 else
3290 buf[0] = image_lo, buf[1] = image_hi;
3293 static void
3294 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3295 const long *buf)
3297 unsigned long image_hi, image_lo;
3298 bool sign;
3299 int exp;
3301 if (FLOAT_WORDS_BIG_ENDIAN)
3302 image_hi = buf[0], image_lo = buf[1];
3303 else
3304 image_lo = buf[0], image_hi = buf[1];
3305 image_lo &= 0xffffffff;
3306 image_hi &= 0xffffffff;
3308 sign = (image_hi >> 31) & 1;
3309 exp = (image_hi >> 20) & 0x7ff;
3311 memset (r, 0, sizeof (*r));
3313 image_hi <<= 32 - 21;
3314 image_hi |= image_lo >> 21;
3315 image_hi &= 0x7fffffff;
3316 image_lo <<= 32 - 21;
3318 if (exp == 0)
3320 if ((image_hi || image_lo) && fmt->has_denorm)
3322 r->cl = rvc_normal;
3323 r->sign = sign;
3324 SET_REAL_EXP (r, -1022);
3325 if (HOST_BITS_PER_LONG == 32)
3327 image_hi = (image_hi << 1) | (image_lo >> 31);
3328 image_lo <<= 1;
3329 r->sig[SIGSZ-1] = image_hi;
3330 r->sig[SIGSZ-2] = image_lo;
3332 else
3334 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
3335 r->sig[SIGSZ-1] = image_hi;
3337 normalize (r);
3339 else if (fmt->has_signed_zero)
3340 r->sign = sign;
3342 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
3344 if (image_hi || image_lo)
3346 r->cl = rvc_nan;
3347 r->sign = sign;
3348 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3349 if (HOST_BITS_PER_LONG == 32)
3351 r->sig[SIGSZ-1] = image_hi;
3352 r->sig[SIGSZ-2] = image_lo;
3354 else
3355 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3357 else
3359 r->cl = rvc_inf;
3360 r->sign = sign;
3363 else
3365 r->cl = rvc_normal;
3366 r->sign = sign;
3367 SET_REAL_EXP (r, exp - 1023 + 1);
3368 if (HOST_BITS_PER_LONG == 32)
3370 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3371 r->sig[SIGSZ-2] = image_lo;
3373 else
3374 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3378 const struct real_format ieee_double_format =
3380 encode_ieee_double,
3381 decode_ieee_double,
3385 -1021,
3386 1024,
3390 false,
3391 true,
3392 true,
3393 true,
3394 true,
3395 true,
3396 true,
3397 false,
3398 "ieee_double"
3401 const struct real_format mips_double_format =
3403 encode_ieee_double,
3404 decode_ieee_double,
3408 -1021,
3409 1024,
3413 false,
3414 true,
3415 true,
3416 true,
3417 true,
3418 true,
3419 false,
3420 true,
3421 "mips_double"
3424 const struct real_format motorola_double_format =
3426 encode_ieee_double,
3427 decode_ieee_double,
3431 -1021,
3432 1024,
3436 false,
3437 true,
3438 true,
3439 true,
3440 true,
3441 true,
3442 true,
3443 true,
3444 "motorola_double"
3447 /* IEEE extended real format. This comes in three flavors: Intel's as
3448 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3449 12- and 16-byte images may be big- or little endian; Motorola's is
3450 always big endian. */
3452 /* Helper subroutine which converts from the internal format to the
3453 12-byte little-endian Intel format. Functions below adjust this
3454 for the other possible formats. */
3455 static void
3456 encode_ieee_extended (const struct real_format *fmt, long *buf,
3457 const REAL_VALUE_TYPE *r)
3459 unsigned long image_hi, sig_hi, sig_lo;
3461 image_hi = r->sign << 15;
3462 sig_hi = sig_lo = 0;
3464 switch (r->cl)
3466 case rvc_zero:
3467 break;
3469 case rvc_inf:
3470 if (fmt->has_inf)
3472 image_hi |= 32767;
3474 /* Intel requires the explicit integer bit to be set, otherwise
3475 it considers the value a "pseudo-infinity". Motorola docs
3476 say it doesn't care. */
3477 sig_hi = 0x80000000;
3479 else
3481 image_hi |= 32767;
3482 sig_lo = sig_hi = 0xffffffff;
3484 break;
3486 case rvc_nan:
3487 if (fmt->has_nans)
3489 image_hi |= 32767;
3490 if (r->canonical)
3492 if (fmt->canonical_nan_lsbs_set)
3494 sig_hi = (1 << 30) - 1;
3495 sig_lo = 0xffffffff;
3498 else if (HOST_BITS_PER_LONG == 32)
3500 sig_hi = r->sig[SIGSZ-1];
3501 sig_lo = r->sig[SIGSZ-2];
3503 else
3505 sig_lo = r->sig[SIGSZ-1];
3506 sig_hi = sig_lo >> 31 >> 1;
3507 sig_lo &= 0xffffffff;
3509 if (r->signalling == fmt->qnan_msb_set)
3510 sig_hi &= ~(1 << 30);
3511 else
3512 sig_hi |= 1 << 30;
3513 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3514 sig_hi = 1 << 29;
3516 /* Intel requires the explicit integer bit to be set, otherwise
3517 it considers the value a "pseudo-nan". Motorola docs say it
3518 doesn't care. */
3519 sig_hi |= 0x80000000;
3521 else
3523 image_hi |= 32767;
3524 sig_lo = sig_hi = 0xffffffff;
3526 break;
3528 case rvc_normal:
3530 int exp = REAL_EXP (r);
3532 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3533 whereas the intermediate representation is 0.F x 2**exp.
3534 Which means we're off by one.
3536 Except for Motorola, which consider exp=0 and explicit
3537 integer bit set to continue to be normalized. In theory
3538 this discrepancy has been taken care of by the difference
3539 in fmt->emin in round_for_format. */
3541 if (real_isdenormal (r))
3542 exp = 0;
3543 else
3545 exp += 16383 - 1;
3546 gcc_assert (exp >= 0);
3548 image_hi |= exp;
3550 if (HOST_BITS_PER_LONG == 32)
3552 sig_hi = r->sig[SIGSZ-1];
3553 sig_lo = r->sig[SIGSZ-2];
3555 else
3557 sig_lo = r->sig[SIGSZ-1];
3558 sig_hi = sig_lo >> 31 >> 1;
3559 sig_lo &= 0xffffffff;
3562 break;
3564 default:
3565 gcc_unreachable ();
3568 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3571 /* Convert from the internal format to the 12-byte Motorola format
3572 for an IEEE extended real. */
3573 static void
3574 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3575 const REAL_VALUE_TYPE *r)
3577 long intermed[3];
3578 encode_ieee_extended (fmt, intermed, r);
3580 if (r->cl == rvc_inf)
3581 /* For infinity clear the explicit integer bit again, so that the
3582 format matches the canonical infinity generated by the FPU. */
3583 intermed[1] = 0;
3585 /* Motorola chips are assumed always to be big-endian. Also, the
3586 padding in a Motorola extended real goes between the exponent and
3587 the mantissa. At this point the mantissa is entirely within
3588 elements 0 and 1 of intermed, and the exponent entirely within
3589 element 2, so all we have to do is swap the order around, and
3590 shift element 2 left 16 bits. */
3591 buf[0] = intermed[2] << 16;
3592 buf[1] = intermed[1];
3593 buf[2] = intermed[0];
3596 /* Convert from the internal format to the 12-byte Intel format for
3597 an IEEE extended real. */
3598 static void
3599 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3600 const REAL_VALUE_TYPE *r)
3602 if (FLOAT_WORDS_BIG_ENDIAN)
3604 /* All the padding in an Intel-format extended real goes at the high
3605 end, which in this case is after the mantissa, not the exponent.
3606 Therefore we must shift everything down 16 bits. */
3607 long intermed[3];
3608 encode_ieee_extended (fmt, intermed, r);
3609 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3610 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3611 buf[2] = (intermed[0] << 16);
3613 else
3614 /* encode_ieee_extended produces what we want directly. */
3615 encode_ieee_extended (fmt, buf, r);
3618 /* Convert from the internal format to the 16-byte Intel format for
3619 an IEEE extended real. */
3620 static void
3621 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3622 const REAL_VALUE_TYPE *r)
3624 /* All the padding in an Intel-format extended real goes at the high end. */
3625 encode_ieee_extended_intel_96 (fmt, buf, r);
3626 buf[3] = 0;
3629 /* As above, we have a helper function which converts from 12-byte
3630 little-endian Intel format to internal format. Functions below
3631 adjust for the other possible formats. */
3632 static void
3633 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3634 const long *buf)
3636 unsigned long image_hi, sig_hi, sig_lo;
3637 bool sign;
3638 int exp;
3640 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3641 sig_lo &= 0xffffffff;
3642 sig_hi &= 0xffffffff;
3643 image_hi &= 0xffffffff;
3645 sign = (image_hi >> 15) & 1;
3646 exp = image_hi & 0x7fff;
3648 memset (r, 0, sizeof (*r));
3650 if (exp == 0)
3652 if ((sig_hi || sig_lo) && fmt->has_denorm)
3654 r->cl = rvc_normal;
3655 r->sign = sign;
3657 /* When the IEEE format contains a hidden bit, we know that
3658 it's zero at this point, and so shift up the significand
3659 and decrease the exponent to match. In this case, Motorola
3660 defines the explicit integer bit to be valid, so we don't
3661 know whether the msb is set or not. */
3662 SET_REAL_EXP (r, fmt->emin);
3663 if (HOST_BITS_PER_LONG == 32)
3665 r->sig[SIGSZ-1] = sig_hi;
3666 r->sig[SIGSZ-2] = sig_lo;
3668 else
3669 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3671 normalize (r);
3673 else if (fmt->has_signed_zero)
3674 r->sign = sign;
3676 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3678 /* See above re "pseudo-infinities" and "pseudo-nans".
3679 Short summary is that the MSB will likely always be
3680 set, and that we don't care about it. */
3681 sig_hi &= 0x7fffffff;
3683 if (sig_hi || sig_lo)
3685 r->cl = rvc_nan;
3686 r->sign = sign;
3687 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3688 if (HOST_BITS_PER_LONG == 32)
3690 r->sig[SIGSZ-1] = sig_hi;
3691 r->sig[SIGSZ-2] = sig_lo;
3693 else
3694 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3696 else
3698 r->cl = rvc_inf;
3699 r->sign = sign;
3702 else
3704 r->cl = rvc_normal;
3705 r->sign = sign;
3706 SET_REAL_EXP (r, exp - 16383 + 1);
3707 if (HOST_BITS_PER_LONG == 32)
3709 r->sig[SIGSZ-1] = sig_hi;
3710 r->sig[SIGSZ-2] = sig_lo;
3712 else
3713 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3717 /* Convert from the internal format to the 12-byte Motorola format
3718 for an IEEE extended real. */
3719 static void
3720 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3721 const long *buf)
3723 long intermed[3];
3725 /* Motorola chips are assumed always to be big-endian. Also, the
3726 padding in a Motorola extended real goes between the exponent and
3727 the mantissa; remove it. */
3728 intermed[0] = buf[2];
3729 intermed[1] = buf[1];
3730 intermed[2] = (unsigned long)buf[0] >> 16;
3732 decode_ieee_extended (fmt, r, intermed);
3735 /* Convert from the internal format to the 12-byte Intel format for
3736 an IEEE extended real. */
3737 static void
3738 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3739 const long *buf)
3741 if (FLOAT_WORDS_BIG_ENDIAN)
3743 /* All the padding in an Intel-format extended real goes at the high
3744 end, which in this case is after the mantissa, not the exponent.
3745 Therefore we must shift everything up 16 bits. */
3746 long intermed[3];
3748 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3749 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3750 intermed[2] = ((unsigned long)buf[0] >> 16);
3752 decode_ieee_extended (fmt, r, intermed);
3754 else
3755 /* decode_ieee_extended produces what we want directly. */
3756 decode_ieee_extended (fmt, r, buf);
3759 /* Convert from the internal format to the 16-byte Intel format for
3760 an IEEE extended real. */
3761 static void
3762 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3763 const long *buf)
3765 /* All the padding in an Intel-format extended real goes at the high end. */
3766 decode_ieee_extended_intel_96 (fmt, r, buf);
3769 const struct real_format ieee_extended_motorola_format =
3771 encode_ieee_extended_motorola,
3772 decode_ieee_extended_motorola,
3776 -16382,
3777 16384,
3781 false,
3782 true,
3783 true,
3784 true,
3785 true,
3786 true,
3787 true,
3788 true,
3789 "ieee_extended_motorola"
3792 const struct real_format ieee_extended_intel_96_format =
3794 encode_ieee_extended_intel_96,
3795 decode_ieee_extended_intel_96,
3799 -16381,
3800 16384,
3804 false,
3805 true,
3806 true,
3807 true,
3808 true,
3809 true,
3810 true,
3811 false,
3812 "ieee_extended_intel_96"
3815 const struct real_format ieee_extended_intel_128_format =
3817 encode_ieee_extended_intel_128,
3818 decode_ieee_extended_intel_128,
3822 -16381,
3823 16384,
3827 false,
3828 true,
3829 true,
3830 true,
3831 true,
3832 true,
3833 true,
3834 false,
3835 "ieee_extended_intel_128"
3838 /* The following caters to i386 systems that set the rounding precision
3839 to 53 bits instead of 64, e.g. FreeBSD. */
3840 const struct real_format ieee_extended_intel_96_round_53_format =
3842 encode_ieee_extended_intel_96,
3843 decode_ieee_extended_intel_96,
3847 -16381,
3848 16384,
3852 false,
3853 true,
3854 true,
3855 true,
3856 true,
3857 true,
3858 true,
3859 false,
3860 "ieee_extended_intel_96_round_53"
3863 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3864 numbers whose sum is equal to the extended precision value. The number
3865 with greater magnitude is first. This format has the same magnitude
3866 range as an IEEE double precision value, but effectively 106 bits of
3867 significand precision. Infinity and NaN are represented by their IEEE
3868 double precision value stored in the first number, the second number is
3869 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3871 static void encode_ibm_extended (const struct real_format *fmt,
3872 long *, const REAL_VALUE_TYPE *);
3873 static void decode_ibm_extended (const struct real_format *,
3874 REAL_VALUE_TYPE *, const long *);
3876 static void
3877 encode_ibm_extended (const struct real_format *fmt, long *buf,
3878 const REAL_VALUE_TYPE *r)
3880 REAL_VALUE_TYPE u, normr, v;
3881 const struct real_format *base_fmt;
3883 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3885 /* Renormalize R before doing any arithmetic on it. */
3886 normr = *r;
3887 if (normr.cl == rvc_normal)
3888 normalize (&normr);
3890 /* u = IEEE double precision portion of significand. */
3891 u = normr;
3892 round_for_format (base_fmt, &u);
3893 encode_ieee_double (base_fmt, &buf[0], &u);
3895 if (u.cl == rvc_normal)
3897 do_add (&v, &normr, &u, 1);
3898 /* Call round_for_format since we might need to denormalize. */
3899 round_for_format (base_fmt, &v);
3900 encode_ieee_double (base_fmt, &buf[2], &v);
3902 else
3904 /* Inf, NaN, 0 are all representable as doubles, so the
3905 least-significant part can be 0.0. */
3906 buf[2] = 0;
3907 buf[3] = 0;
3911 static void
3912 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3913 const long *buf)
3915 REAL_VALUE_TYPE u, v;
3916 const struct real_format *base_fmt;
3918 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3919 decode_ieee_double (base_fmt, &u, &buf[0]);
3921 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3923 decode_ieee_double (base_fmt, &v, &buf[2]);
3924 do_add (r, &u, &v, 0);
3926 else
3927 *r = u;
3930 const struct real_format ibm_extended_format =
3932 encode_ibm_extended,
3933 decode_ibm_extended,
3935 53 + 53,
3937 -1021 + 53,
3938 1024,
3939 127,
3942 false,
3943 true,
3944 true,
3945 true,
3946 true,
3947 true,
3948 true,
3949 false,
3950 "ibm_extended"
3953 const struct real_format mips_extended_format =
3955 encode_ibm_extended,
3956 decode_ibm_extended,
3958 53 + 53,
3960 -1021 + 53,
3961 1024,
3962 127,
3965 false,
3966 true,
3967 true,
3968 true,
3969 true,
3970 true,
3971 false,
3972 true,
3973 "mips_extended"
3977 /* IEEE quad precision format. */
3979 static void encode_ieee_quad (const struct real_format *fmt,
3980 long *, const REAL_VALUE_TYPE *);
3981 static void decode_ieee_quad (const struct real_format *,
3982 REAL_VALUE_TYPE *, const long *);
3984 static void
3985 encode_ieee_quad (const struct real_format *fmt, long *buf,
3986 const REAL_VALUE_TYPE *r)
3988 unsigned long image3, image2, image1, image0, exp;
3989 unsigned long sign = r->sign;
3990 REAL_VALUE_TYPE u;
3992 image3 = sign << 31;
3993 image2 = 0;
3994 image1 = 0;
3995 image0 = 0;
3997 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3999 switch (r->cl)
4001 case rvc_zero:
4002 break;
4004 case rvc_inf:
4005 if (fmt->has_inf)
4006 image3 |= 32767 << 16;
4007 else
4009 image3 |= 0x7fffffff;
4010 image2 = 0xffffffff;
4011 image1 = 0xffffffff;
4012 image0 = 0xffffffff;
4014 break;
4016 case rvc_nan:
4017 if (fmt->has_nans)
4019 image3 |= 32767 << 16;
4021 if (r->canonical)
4023 if (fmt->canonical_nan_lsbs_set)
4025 image3 |= 0x7fff;
4026 image2 = image1 = image0 = 0xffffffff;
4029 else if (HOST_BITS_PER_LONG == 32)
4031 image0 = u.sig[0];
4032 image1 = u.sig[1];
4033 image2 = u.sig[2];
4034 image3 |= u.sig[3] & 0xffff;
4036 else
4038 image0 = u.sig[0];
4039 image1 = image0 >> 31 >> 1;
4040 image2 = u.sig[1];
4041 image3 |= (image2 >> 31 >> 1) & 0xffff;
4042 image0 &= 0xffffffff;
4043 image2 &= 0xffffffff;
4045 if (r->signalling == fmt->qnan_msb_set)
4046 image3 &= ~0x8000;
4047 else
4048 image3 |= 0x8000;
4049 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
4050 image3 |= 0x4000;
4052 else
4054 image3 |= 0x7fffffff;
4055 image2 = 0xffffffff;
4056 image1 = 0xffffffff;
4057 image0 = 0xffffffff;
4059 break;
4061 case rvc_normal:
4062 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4063 whereas the intermediate representation is 0.F x 2**exp.
4064 Which means we're off by one. */
4065 if (real_isdenormal (r))
4066 exp = 0;
4067 else
4068 exp = REAL_EXP (r) + 16383 - 1;
4069 image3 |= exp << 16;
4071 if (HOST_BITS_PER_LONG == 32)
4073 image0 = u.sig[0];
4074 image1 = u.sig[1];
4075 image2 = u.sig[2];
4076 image3 |= u.sig[3] & 0xffff;
4078 else
4080 image0 = u.sig[0];
4081 image1 = image0 >> 31 >> 1;
4082 image2 = u.sig[1];
4083 image3 |= (image2 >> 31 >> 1) & 0xffff;
4084 image0 &= 0xffffffff;
4085 image2 &= 0xffffffff;
4087 break;
4089 default:
4090 gcc_unreachable ();
4093 if (FLOAT_WORDS_BIG_ENDIAN)
4095 buf[0] = image3;
4096 buf[1] = image2;
4097 buf[2] = image1;
4098 buf[3] = image0;
4100 else
4102 buf[0] = image0;
4103 buf[1] = image1;
4104 buf[2] = image2;
4105 buf[3] = image3;
4109 static void
4110 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4111 const long *buf)
4113 unsigned long image3, image2, image1, image0;
4114 bool sign;
4115 int exp;
4117 if (FLOAT_WORDS_BIG_ENDIAN)
4119 image3 = buf[0];
4120 image2 = buf[1];
4121 image1 = buf[2];
4122 image0 = buf[3];
4124 else
4126 image0 = buf[0];
4127 image1 = buf[1];
4128 image2 = buf[2];
4129 image3 = buf[3];
4131 image0 &= 0xffffffff;
4132 image1 &= 0xffffffff;
4133 image2 &= 0xffffffff;
4135 sign = (image3 >> 31) & 1;
4136 exp = (image3 >> 16) & 0x7fff;
4137 image3 &= 0xffff;
4139 memset (r, 0, sizeof (*r));
4141 if (exp == 0)
4143 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
4145 r->cl = rvc_normal;
4146 r->sign = sign;
4148 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
4149 if (HOST_BITS_PER_LONG == 32)
4151 r->sig[0] = image0;
4152 r->sig[1] = image1;
4153 r->sig[2] = image2;
4154 r->sig[3] = image3;
4156 else
4158 r->sig[0] = (image1 << 31 << 1) | image0;
4159 r->sig[1] = (image3 << 31 << 1) | image2;
4162 normalize (r);
4164 else if (fmt->has_signed_zero)
4165 r->sign = sign;
4167 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
4169 if (image3 | image2 | image1 | image0)
4171 r->cl = rvc_nan;
4172 r->sign = sign;
4173 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
4175 if (HOST_BITS_PER_LONG == 32)
4177 r->sig[0] = image0;
4178 r->sig[1] = image1;
4179 r->sig[2] = image2;
4180 r->sig[3] = image3;
4182 else
4184 r->sig[0] = (image1 << 31 << 1) | image0;
4185 r->sig[1] = (image3 << 31 << 1) | image2;
4187 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4189 else
4191 r->cl = rvc_inf;
4192 r->sign = sign;
4195 else
4197 r->cl = rvc_normal;
4198 r->sign = sign;
4199 SET_REAL_EXP (r, exp - 16383 + 1);
4201 if (HOST_BITS_PER_LONG == 32)
4203 r->sig[0] = image0;
4204 r->sig[1] = image1;
4205 r->sig[2] = image2;
4206 r->sig[3] = image3;
4208 else
4210 r->sig[0] = (image1 << 31 << 1) | image0;
4211 r->sig[1] = (image3 << 31 << 1) | image2;
4213 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4214 r->sig[SIGSZ-1] |= SIG_MSB;
4218 const struct real_format ieee_quad_format =
4220 encode_ieee_quad,
4221 decode_ieee_quad,
4223 113,
4224 113,
4225 -16381,
4226 16384,
4227 127,
4228 127,
4229 128,
4230 false,
4231 true,
4232 true,
4233 true,
4234 true,
4235 true,
4236 true,
4237 false,
4238 "ieee_quad"
4241 const struct real_format mips_quad_format =
4243 encode_ieee_quad,
4244 decode_ieee_quad,
4246 113,
4247 113,
4248 -16381,
4249 16384,
4250 127,
4251 127,
4252 128,
4253 false,
4254 true,
4255 true,
4256 true,
4257 true,
4258 true,
4259 false,
4260 true,
4261 "mips_quad"
4264 /* Descriptions of VAX floating point formats can be found beginning at
4266 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4268 The thing to remember is that they're almost IEEE, except for word
4269 order, exponent bias, and the lack of infinities, nans, and denormals.
4271 We don't implement the H_floating format here, simply because neither
4272 the VAX or Alpha ports use it. */
4274 static void encode_vax_f (const struct real_format *fmt,
4275 long *, const REAL_VALUE_TYPE *);
4276 static void decode_vax_f (const struct real_format *,
4277 REAL_VALUE_TYPE *, const long *);
4278 static void encode_vax_d (const struct real_format *fmt,
4279 long *, const REAL_VALUE_TYPE *);
4280 static void decode_vax_d (const struct real_format *,
4281 REAL_VALUE_TYPE *, const long *);
4282 static void encode_vax_g (const struct real_format *fmt,
4283 long *, const REAL_VALUE_TYPE *);
4284 static void decode_vax_g (const struct real_format *,
4285 REAL_VALUE_TYPE *, const long *);
4287 static void
4288 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4289 const REAL_VALUE_TYPE *r)
4291 unsigned long sign, exp, sig, image;
4293 sign = r->sign << 15;
4295 switch (r->cl)
4297 case rvc_zero:
4298 image = 0;
4299 break;
4301 case rvc_inf:
4302 case rvc_nan:
4303 image = 0xffff7fff | sign;
4304 break;
4306 case rvc_normal:
4307 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4308 exp = REAL_EXP (r) + 128;
4310 image = (sig << 16) & 0xffff0000;
4311 image |= sign;
4312 image |= exp << 7;
4313 image |= sig >> 16;
4314 break;
4316 default:
4317 gcc_unreachable ();
4320 buf[0] = image;
4323 static void
4324 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
4325 REAL_VALUE_TYPE *r, const long *buf)
4327 unsigned long image = buf[0] & 0xffffffff;
4328 int exp = (image >> 7) & 0xff;
4330 memset (r, 0, sizeof (*r));
4332 if (exp != 0)
4334 r->cl = rvc_normal;
4335 r->sign = (image >> 15) & 1;
4336 SET_REAL_EXP (r, exp - 128);
4338 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
4339 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4343 static void
4344 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4345 const REAL_VALUE_TYPE *r)
4347 unsigned long image0, image1, sign = r->sign << 15;
4349 switch (r->cl)
4351 case rvc_zero:
4352 image0 = image1 = 0;
4353 break;
4355 case rvc_inf:
4356 case rvc_nan:
4357 image0 = 0xffff7fff | sign;
4358 image1 = 0xffffffff;
4359 break;
4361 case rvc_normal:
4362 /* Extract the significand into straight hi:lo. */
4363 if (HOST_BITS_PER_LONG == 64)
4365 image0 = r->sig[SIGSZ-1];
4366 image1 = (image0 >> (64 - 56)) & 0xffffffff;
4367 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
4369 else
4371 image0 = r->sig[SIGSZ-1];
4372 image1 = r->sig[SIGSZ-2];
4373 image1 = (image0 << 24) | (image1 >> 8);
4374 image0 = (image0 >> 8) & 0xffffff;
4377 /* Rearrange the half-words of the significand to match the
4378 external format. */
4379 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
4380 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4382 /* Add the sign and exponent. */
4383 image0 |= sign;
4384 image0 |= (REAL_EXP (r) + 128) << 7;
4385 break;
4387 default:
4388 gcc_unreachable ();
4391 if (FLOAT_WORDS_BIG_ENDIAN)
4392 buf[0] = image1, buf[1] = image0;
4393 else
4394 buf[0] = image0, buf[1] = image1;
4397 static void
4398 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
4399 REAL_VALUE_TYPE *r, const long *buf)
4401 unsigned long image0, image1;
4402 int exp;
4404 if (FLOAT_WORDS_BIG_ENDIAN)
4405 image1 = buf[0], image0 = buf[1];
4406 else
4407 image0 = buf[0], image1 = buf[1];
4408 image0 &= 0xffffffff;
4409 image1 &= 0xffffffff;
4411 exp = (image0 >> 7) & 0xff;
4413 memset (r, 0, sizeof (*r));
4415 if (exp != 0)
4417 r->cl = rvc_normal;
4418 r->sign = (image0 >> 15) & 1;
4419 SET_REAL_EXP (r, exp - 128);
4421 /* Rearrange the half-words of the external format into
4422 proper ascending order. */
4423 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4424 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4426 if (HOST_BITS_PER_LONG == 64)
4428 image0 = (image0 << 31 << 1) | image1;
4429 image0 <<= 64 - 56;
4430 image0 |= SIG_MSB;
4431 r->sig[SIGSZ-1] = image0;
4433 else
4435 r->sig[SIGSZ-1] = image0;
4436 r->sig[SIGSZ-2] = image1;
4437 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4438 r->sig[SIGSZ-1] |= SIG_MSB;
4443 static void
4444 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4445 const REAL_VALUE_TYPE *r)
4447 unsigned long image0, image1, sign = r->sign << 15;
4449 switch (r->cl)
4451 case rvc_zero:
4452 image0 = image1 = 0;
4453 break;
4455 case rvc_inf:
4456 case rvc_nan:
4457 image0 = 0xffff7fff | sign;
4458 image1 = 0xffffffff;
4459 break;
4461 case rvc_normal:
4462 /* Extract the significand into straight hi:lo. */
4463 if (HOST_BITS_PER_LONG == 64)
4465 image0 = r->sig[SIGSZ-1];
4466 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4467 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4469 else
4471 image0 = r->sig[SIGSZ-1];
4472 image1 = r->sig[SIGSZ-2];
4473 image1 = (image0 << 21) | (image1 >> 11);
4474 image0 = (image0 >> 11) & 0xfffff;
4477 /* Rearrange the half-words of the significand to match the
4478 external format. */
4479 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4480 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4482 /* Add the sign and exponent. */
4483 image0 |= sign;
4484 image0 |= (REAL_EXP (r) + 1024) << 4;
4485 break;
4487 default:
4488 gcc_unreachable ();
4491 if (FLOAT_WORDS_BIG_ENDIAN)
4492 buf[0] = image1, buf[1] = image0;
4493 else
4494 buf[0] = image0, buf[1] = image1;
4497 static void
4498 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4499 REAL_VALUE_TYPE *r, const long *buf)
4501 unsigned long image0, image1;
4502 int exp;
4504 if (FLOAT_WORDS_BIG_ENDIAN)
4505 image1 = buf[0], image0 = buf[1];
4506 else
4507 image0 = buf[0], image1 = buf[1];
4508 image0 &= 0xffffffff;
4509 image1 &= 0xffffffff;
4511 exp = (image0 >> 4) & 0x7ff;
4513 memset (r, 0, sizeof (*r));
4515 if (exp != 0)
4517 r->cl = rvc_normal;
4518 r->sign = (image0 >> 15) & 1;
4519 SET_REAL_EXP (r, exp - 1024);
4521 /* Rearrange the half-words of the external format into
4522 proper ascending order. */
4523 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4524 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4526 if (HOST_BITS_PER_LONG == 64)
4528 image0 = (image0 << 31 << 1) | image1;
4529 image0 <<= 64 - 53;
4530 image0 |= SIG_MSB;
4531 r->sig[SIGSZ-1] = image0;
4533 else
4535 r->sig[SIGSZ-1] = image0;
4536 r->sig[SIGSZ-2] = image1;
4537 lshift_significand (r, r, 64 - 53);
4538 r->sig[SIGSZ-1] |= SIG_MSB;
4543 const struct real_format vax_f_format =
4545 encode_vax_f,
4546 decode_vax_f,
4550 -127,
4551 127,
4555 false,
4556 false,
4557 false,
4558 false,
4559 false,
4560 false,
4561 false,
4562 false,
4563 "vax_f"
4566 const struct real_format vax_d_format =
4568 encode_vax_d,
4569 decode_vax_d,
4573 -127,
4574 127,
4578 false,
4579 false,
4580 false,
4581 false,
4582 false,
4583 false,
4584 false,
4585 false,
4586 "vax_d"
4589 const struct real_format vax_g_format =
4591 encode_vax_g,
4592 decode_vax_g,
4596 -1023,
4597 1023,
4601 false,
4602 false,
4603 false,
4604 false,
4605 false,
4606 false,
4607 false,
4608 false,
4609 "vax_g"
4612 /* Encode real R into a single precision DFP value in BUF. */
4613 static void
4614 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4615 long *buf ATTRIBUTE_UNUSED,
4616 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4618 encode_decimal32 (fmt, buf, r);
4621 /* Decode a single precision DFP value in BUF into a real R. */
4622 static void
4623 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4624 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4625 const long *buf ATTRIBUTE_UNUSED)
4627 decode_decimal32 (fmt, r, buf);
4630 /* Encode real R into a double precision DFP value in BUF. */
4631 static void
4632 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4633 long *buf ATTRIBUTE_UNUSED,
4634 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4636 encode_decimal64 (fmt, buf, r);
4639 /* Decode a double precision DFP value in BUF into a real R. */
4640 static void
4641 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4642 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4643 const long *buf ATTRIBUTE_UNUSED)
4645 decode_decimal64 (fmt, r, buf);
4648 /* Encode real R into a quad precision DFP value in BUF. */
4649 static void
4650 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4651 long *buf ATTRIBUTE_UNUSED,
4652 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4654 encode_decimal128 (fmt, buf, r);
4657 /* Decode a quad precision DFP value in BUF into a real R. */
4658 static void
4659 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4660 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4661 const long *buf ATTRIBUTE_UNUSED)
4663 decode_decimal128 (fmt, r, buf);
4666 /* Single precision decimal floating point (IEEE 754). */
4667 const struct real_format decimal_single_format =
4669 encode_decimal_single,
4670 decode_decimal_single,
4674 -94,
4679 false,
4680 true,
4681 true,
4682 true,
4683 true,
4684 true,
4685 true,
4686 false,
4687 "decimal_single"
4690 /* Double precision decimal floating point (IEEE 754). */
4691 const struct real_format decimal_double_format =
4693 encode_decimal_double,
4694 decode_decimal_double,
4698 -382,
4699 385,
4703 false,
4704 true,
4705 true,
4706 true,
4707 true,
4708 true,
4709 true,
4710 false,
4711 "decimal_double"
4714 /* Quad precision decimal floating point (IEEE 754). */
4715 const struct real_format decimal_quad_format =
4717 encode_decimal_quad,
4718 decode_decimal_quad,
4722 -6142,
4723 6145,
4724 127,
4725 127,
4726 128,
4727 false,
4728 true,
4729 true,
4730 true,
4731 true,
4732 true,
4733 true,
4734 false,
4735 "decimal_quad"
4738 /* Encode half-precision floats. This routine is used both for the IEEE
4739 ARM alternative encodings. */
4740 static void
4741 encode_ieee_half (const struct real_format *fmt, long *buf,
4742 const REAL_VALUE_TYPE *r)
4744 unsigned long image, sig, exp;
4745 unsigned long sign = r->sign;
4747 image = sign << 15;
4748 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 11)) & 0x3ff;
4750 switch (r->cl)
4752 case rvc_zero:
4753 break;
4755 case rvc_inf:
4756 if (fmt->has_inf)
4757 image |= 31 << 10;
4758 else
4759 image |= 0x7fff;
4760 break;
4762 case rvc_nan:
4763 if (fmt->has_nans)
4765 if (r->canonical)
4766 sig = (fmt->canonical_nan_lsbs_set ? (1 << 9) - 1 : 0);
4767 if (r->signalling == fmt->qnan_msb_set)
4768 sig &= ~(1 << 9);
4769 else
4770 sig |= 1 << 9;
4771 if (sig == 0)
4772 sig = 1 << 8;
4774 image |= 31 << 10;
4775 image |= sig;
4777 else
4778 image |= 0x3ff;
4779 break;
4781 case rvc_normal:
4782 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4783 whereas the intermediate representation is 0.F x 2**exp.
4784 Which means we're off by one. */
4785 if (real_isdenormal (r))
4786 exp = 0;
4787 else
4788 exp = REAL_EXP (r) + 15 - 1;
4789 image |= exp << 10;
4790 image |= sig;
4791 break;
4793 default:
4794 gcc_unreachable ();
4797 buf[0] = image;
4800 /* Decode half-precision floats. This routine is used both for the IEEE
4801 ARM alternative encodings. */
4802 static void
4803 decode_ieee_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4804 const long *buf)
4806 unsigned long image = buf[0] & 0xffff;
4807 bool sign = (image >> 15) & 1;
4808 int exp = (image >> 10) & 0x1f;
4810 memset (r, 0, sizeof (*r));
4811 image <<= HOST_BITS_PER_LONG - 11;
4812 image &= ~SIG_MSB;
4814 if (exp == 0)
4816 if (image && fmt->has_denorm)
4818 r->cl = rvc_normal;
4819 r->sign = sign;
4820 SET_REAL_EXP (r, -14);
4821 r->sig[SIGSZ-1] = image << 1;
4822 normalize (r);
4824 else if (fmt->has_signed_zero)
4825 r->sign = sign;
4827 else if (exp == 31 && (fmt->has_nans || fmt->has_inf))
4829 if (image)
4831 r->cl = rvc_nan;
4832 r->sign = sign;
4833 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4834 ^ fmt->qnan_msb_set);
4835 r->sig[SIGSZ-1] = image;
4837 else
4839 r->cl = rvc_inf;
4840 r->sign = sign;
4843 else
4845 r->cl = rvc_normal;
4846 r->sign = sign;
4847 SET_REAL_EXP (r, exp - 15 + 1);
4848 r->sig[SIGSZ-1] = image | SIG_MSB;
4852 /* Encode arm_bfloat types. */
4853 static void
4854 encode_arm_bfloat_half (const struct real_format *fmt, long *buf,
4855 const REAL_VALUE_TYPE *r)
4857 unsigned long image, sig, exp;
4858 unsigned long sign = r->sign;
4860 image = sign << 15;
4861 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 8)) & 0x7f;
4863 switch (r->cl)
4865 case rvc_zero:
4866 break;
4868 case rvc_inf:
4869 if (fmt->has_inf)
4870 image |= 255 << 7;
4871 else
4872 image |= 0x7fff;
4873 break;
4875 case rvc_nan:
4876 if (fmt->has_nans)
4878 if (r->canonical)
4879 sig = (fmt->canonical_nan_lsbs_set ? (1 << 6) - 1 : 0);
4880 if (r->signalling == fmt->qnan_msb_set)
4881 sig &= ~(1 << 6);
4882 else
4883 sig |= 1 << 6;
4884 if (sig == 0)
4885 sig = 1 << 5;
4887 image |= 255 << 7;
4888 image |= sig;
4890 else
4891 image |= 0x7fff;
4892 break;
4894 case rvc_normal:
4895 if (real_isdenormal (r))
4896 exp = 0;
4897 else
4898 exp = REAL_EXP (r) + 127 - 1;
4899 image |= exp << 7;
4900 image |= sig;
4901 break;
4903 default:
4904 gcc_unreachable ();
4907 buf[0] = image;
4910 /* Decode arm_bfloat types. */
4911 static void
4912 decode_arm_bfloat_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4913 const long *buf)
4915 unsigned long image = buf[0] & 0xffff;
4916 bool sign = (image >> 15) & 1;
4917 int exp = (image >> 7) & 0xff;
4919 memset (r, 0, sizeof (*r));
4920 image <<= HOST_BITS_PER_LONG - 8;
4921 image &= ~SIG_MSB;
4923 if (exp == 0)
4925 if (image && fmt->has_denorm)
4927 r->cl = rvc_normal;
4928 r->sign = sign;
4929 SET_REAL_EXP (r, -126);
4930 r->sig[SIGSZ-1] = image << 1;
4931 normalize (r);
4933 else if (fmt->has_signed_zero)
4934 r->sign = sign;
4936 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
4938 if (image)
4940 r->cl = rvc_nan;
4941 r->sign = sign;
4942 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4943 ^ fmt->qnan_msb_set);
4944 r->sig[SIGSZ-1] = image;
4946 else
4948 r->cl = rvc_inf;
4949 r->sign = sign;
4952 else
4954 r->cl = rvc_normal;
4955 r->sign = sign;
4956 SET_REAL_EXP (r, exp - 127 + 1);
4957 r->sig[SIGSZ-1] = image | SIG_MSB;
4961 /* Half-precision format, as specified in IEEE 754R. */
4962 const struct real_format ieee_half_format =
4964 encode_ieee_half,
4965 decode_ieee_half,
4969 -13,
4974 false,
4975 true,
4976 true,
4977 true,
4978 true,
4979 true,
4980 true,
4981 false,
4982 "ieee_half"
4985 /* ARM's alternative half-precision format, similar to IEEE but with
4986 no reserved exponent value for NaNs and infinities; rather, it just
4987 extends the range of exponents by one. */
4988 const struct real_format arm_half_format =
4990 encode_ieee_half,
4991 decode_ieee_half,
4995 -13,
5000 false,
5001 true,
5002 false,
5003 false,
5004 true,
5005 true,
5006 false,
5007 false,
5008 "arm_half"
5011 /* ARM Bfloat half-precision format. This format resembles a truncated
5012 (16-bit) version of the 32-bit IEEE 754 single-precision floating-point
5013 format. */
5014 const struct real_format arm_bfloat_half_format =
5016 encode_arm_bfloat_half,
5017 decode_arm_bfloat_half,
5021 -125,
5022 128,
5026 false,
5027 true,
5028 true,
5029 true,
5030 true,
5031 true,
5032 true,
5033 false,
5034 "arm_bfloat_half"
5038 /* A synthetic "format" for internal arithmetic. It's the size of the
5039 internal significand minus the two bits needed for proper rounding.
5040 The encode and decode routines exist only to satisfy our paranoia
5041 harness. */
5043 static void encode_internal (const struct real_format *fmt,
5044 long *, const REAL_VALUE_TYPE *);
5045 static void decode_internal (const struct real_format *,
5046 REAL_VALUE_TYPE *, const long *);
5048 static void
5049 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
5050 const REAL_VALUE_TYPE *r)
5052 memcpy (buf, r, sizeof (*r));
5055 static void
5056 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
5057 REAL_VALUE_TYPE *r, const long *buf)
5059 memcpy (r, buf, sizeof (*r));
5062 const struct real_format real_internal_format =
5064 encode_internal,
5065 decode_internal,
5067 SIGNIFICAND_BITS - 2,
5068 SIGNIFICAND_BITS - 2,
5069 -MAX_EXP,
5070 MAX_EXP,
5074 false,
5075 false,
5076 true,
5077 true,
5078 false,
5079 true,
5080 true,
5081 false,
5082 "real_internal"
5085 /* Calculate X raised to the integer exponent N in format FMT and store
5086 the result in R. Return true if the result may be inexact due to
5087 loss of precision. The algorithm is the classic "left-to-right binary
5088 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
5089 Algorithms", "The Art of Computer Programming", Volume 2. */
5091 bool
5092 real_powi (REAL_VALUE_TYPE *r, format_helper fmt,
5093 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
5095 unsigned HOST_WIDE_INT bit;
5096 REAL_VALUE_TYPE t;
5097 bool inexact = false;
5098 bool init = false;
5099 bool neg;
5100 int i;
5102 if (n == 0)
5104 *r = dconst1;
5105 return false;
5107 else if (n < 0)
5109 /* Don't worry about overflow, from now on n is unsigned. */
5110 neg = true;
5111 n = -n;
5113 else
5114 neg = false;
5116 t = *x;
5117 bit = HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1);
5118 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
5120 if (init)
5122 inexact |= do_multiply (&t, &t, &t);
5123 if (n & bit)
5124 inexact |= do_multiply (&t, &t, x);
5126 else if (n & bit)
5127 init = true;
5128 bit >>= 1;
5131 if (neg)
5132 inexact |= do_divide (&t, &dconst1, &t);
5134 real_convert (r, fmt, &t);
5135 return inexact;
5138 /* Round X to the nearest integer not larger in absolute value, i.e.
5139 towards zero, placing the result in R in format FMT. */
5141 void
5142 real_trunc (REAL_VALUE_TYPE *r, format_helper fmt,
5143 const REAL_VALUE_TYPE *x)
5145 do_fix_trunc (r, x);
5146 if (fmt)
5147 real_convert (r, fmt, r);
5150 /* Round X to the largest integer not greater in value, i.e. round
5151 down, placing the result in R in format FMT. */
5153 void
5154 real_floor (REAL_VALUE_TYPE *r, format_helper fmt,
5155 const REAL_VALUE_TYPE *x)
5157 REAL_VALUE_TYPE t;
5159 do_fix_trunc (&t, x);
5160 if (! real_identical (&t, x) && x->sign)
5161 do_add (&t, &t, &dconstm1, 0);
5162 if (fmt)
5163 real_convert (r, fmt, &t);
5164 else
5165 *r = t;
5168 /* Round X to the smallest integer not less then argument, i.e. round
5169 up, placing the result in R in format FMT. */
5171 void
5172 real_ceil (REAL_VALUE_TYPE *r, format_helper fmt,
5173 const REAL_VALUE_TYPE *x)
5175 REAL_VALUE_TYPE t;
5177 do_fix_trunc (&t, x);
5178 if (! real_identical (&t, x) && ! x->sign)
5179 do_add (&t, &t, &dconst1, 0);
5180 if (fmt)
5181 real_convert (r, fmt, &t);
5182 else
5183 *r = t;
5186 /* Round X to the nearest integer, but round halfway cases away from
5187 zero. */
5189 void
5190 real_round (REAL_VALUE_TYPE *r, format_helper fmt,
5191 const REAL_VALUE_TYPE *x)
5193 do_add (r, x, &dconsthalf, x->sign);
5194 do_fix_trunc (r, r);
5195 if (fmt)
5196 real_convert (r, fmt, r);
5199 /* Return true (including 0) if integer part of R is even, else return
5200 false. The function is not valid for rvc_inf and rvc_nan classes. */
5202 static bool
5203 is_even (REAL_VALUE_TYPE *r)
5205 gcc_assert (r->cl != rvc_inf);
5206 gcc_assert (r->cl != rvc_nan);
5208 if (r->cl == rvc_zero)
5209 return true;
5211 /* For (-1,1), number is even. */
5212 if (REAL_EXP (r) <= 0)
5213 return true;
5215 /* Check lowest bit, if not set, return true. */
5216 else if (REAL_EXP (r) <= SIGNIFICAND_BITS)
5218 unsigned int n = SIGNIFICAND_BITS - REAL_EXP (r);
5219 int w = n / HOST_BITS_PER_LONG;
5221 unsigned long num = ((unsigned long)1 << (n % HOST_BITS_PER_LONG));
5223 if ((r->sig[w] & num) == 0)
5224 return true;
5226 else
5227 return true;
5229 return false;
5232 /* Return true if R is halfway between two integers, else return
5233 false. */
5235 static bool
5236 is_halfway_below (const REAL_VALUE_TYPE *r)
5238 if (r->cl != rvc_normal)
5239 return false;
5241 /* For numbers (-0.5,0) and (0,0.5). */
5242 if (REAL_EXP (r) < 0)
5243 return false;
5245 else if (REAL_EXP (r) < SIGNIFICAND_BITS)
5247 unsigned int n = SIGNIFICAND_BITS - REAL_EXP (r) - 1;
5248 int w = n / HOST_BITS_PER_LONG;
5250 for (int i = 0; i < w; ++i)
5251 if (r->sig[i] != 0)
5252 return false;
5254 unsigned long num = 1UL << (n % HOST_BITS_PER_LONG);
5256 if ((r->sig[w] & num) != 0 && (r->sig[w] & (num - 1)) == 0)
5257 return true;
5259 return false;
5262 /* Round X to nearest integer, rounding halfway cases towards even. */
5264 void
5265 real_roundeven (REAL_VALUE_TYPE *r, format_helper fmt,
5266 const REAL_VALUE_TYPE *x)
5268 if (is_halfway_below (x))
5270 /* Special case as -0.5 rounds to -0.0 and
5271 similarly +0.5 rounds to +0.0. */
5272 if (REAL_EXP (x) == 0)
5274 *r = *x;
5275 clear_significand_below (r, SIGNIFICAND_BITS);
5277 else
5279 do_add (r, x, &dconsthalf, x->sign);
5280 if (!is_even (r))
5281 do_add (r, r, &dconstm1, x->sign);
5283 if (fmt)
5284 real_convert (r, fmt, r);
5286 else
5287 real_round (r, fmt, x);
5290 /* Set the sign of R to the sign of X. */
5292 void
5293 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
5295 r->sign = x->sign;
5298 /* Check whether the real constant value given is an integer.
5299 Returns false for signaling NaN. */
5301 bool
5302 real_isinteger (const REAL_VALUE_TYPE *c, format_helper fmt)
5304 REAL_VALUE_TYPE cint;
5306 real_trunc (&cint, fmt, c);
5307 return real_identical (c, &cint);
5310 /* Check whether C is an integer that fits in a HOST_WIDE_INT,
5311 storing it in *INT_OUT if so. */
5313 bool
5314 real_isinteger (const REAL_VALUE_TYPE *c, HOST_WIDE_INT *int_out)
5316 REAL_VALUE_TYPE cint;
5318 HOST_WIDE_INT n = real_to_integer (c);
5319 real_from_integer (&cint, VOIDmode, n, SIGNED);
5320 if (real_identical (c, &cint))
5322 *int_out = n;
5323 return true;
5325 return false;
5328 /* Calculate nextafter (X, Y) or nexttoward (X, Y). Return true if
5329 underflow or overflow needs to be raised. */
5331 bool
5332 real_nextafter (REAL_VALUE_TYPE *r, format_helper fmt,
5333 const REAL_VALUE_TYPE *x, const REAL_VALUE_TYPE *y)
5335 int cmp = do_compare (x, y, 2);
5336 /* If either operand is NaN, return qNaN. */
5337 if (cmp == 2)
5339 get_canonical_qnan (r, 0);
5340 return false;
5342 /* If x == y, return y cast to target type. */
5343 if (cmp == 0)
5345 real_convert (r, fmt, y);
5346 return false;
5349 if (x->cl == rvc_zero)
5351 get_zero (r, y->sign);
5352 r->cl = rvc_normal;
5353 SET_REAL_EXP (r, fmt->emin - fmt->p + 1);
5354 r->sig[SIGSZ - 1] = SIG_MSB;
5355 return false;
5358 int np2 = SIGNIFICAND_BITS - fmt->p;
5359 /* For denormals adjust np2 correspondingly. */
5360 if (x->cl == rvc_normal && REAL_EXP (x) < fmt->emin)
5361 np2 += fmt->emin - REAL_EXP (x);
5363 REAL_VALUE_TYPE u;
5364 get_zero (r, x->sign);
5365 get_zero (&u, 0);
5366 set_significand_bit (&u, np2);
5367 r->cl = rvc_normal;
5368 SET_REAL_EXP (r, REAL_EXP (x));
5370 if (x->cl == rvc_inf)
5372 bool borrow = sub_significands (r, r, &u, 0);
5373 gcc_assert (borrow);
5374 SET_REAL_EXP (r, fmt->emax);
5376 else if (cmp == (x->sign ? 1 : -1))
5378 if (add_significands (r, x, &u))
5380 /* Overflow. Means the significand had been all ones, and
5381 is now all zeros. Need to increase the exponent, and
5382 possibly re-normalize it. */
5383 SET_REAL_EXP (r, REAL_EXP (r) + 1);
5384 if (REAL_EXP (r) > fmt->emax)
5386 get_inf (r, x->sign);
5387 return true;
5389 r->sig[SIGSZ - 1] = SIG_MSB;
5392 else
5394 if (REAL_EXP (x) > fmt->emin && x->sig[SIGSZ - 1] == SIG_MSB)
5396 int i;
5397 for (i = SIGSZ - 2; i >= 0; i--)
5398 if (x->sig[i])
5399 break;
5400 if (i < 0)
5402 /* When mantissa is 1.0, we need to subtract only
5403 half of u: nextafter (1.0, 0.0) is 1.0 - __DBL_EPSILON__ / 2
5404 rather than 1.0 - __DBL_EPSILON__. */
5405 clear_significand_bit (&u, np2);
5406 np2--;
5407 set_significand_bit (&u, np2);
5410 sub_significands (r, x, &u, 0);
5413 /* Clear out trailing garbage. */
5414 clear_significand_below (r, np2);
5415 normalize (r);
5416 if (REAL_EXP (r) <= fmt->emin - fmt->p)
5418 get_zero (r, x->sign);
5419 return true;
5421 return r->cl == rvc_zero || REAL_EXP (r) < fmt->emin;
5424 /* Write into BUF the maximum representable finite floating-point
5425 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5426 float string. LEN is the size of BUF, and the buffer must be large
5427 enough to contain the resulting string. If NORM_MAX, instead write
5428 the maximum representable finite normalized floating-point number,
5429 defined to be such that all choices of digits for that exponent are
5430 representable in the format (this only makes a difference for IBM
5431 long double). */
5433 void
5434 get_max_float (const struct real_format *fmt, char *buf, size_t len,
5435 bool norm_max)
5437 int i, n;
5438 char *p;
5439 bool is_ibm_extended = fmt->pnan < fmt->p;
5441 strcpy (buf, "0x0.");
5442 n = fmt->p;
5443 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
5444 *p++ = 'f';
5445 if (i < n)
5446 *p++ = "08ce"[n - i];
5447 sprintf (p, "p%d",
5448 (is_ibm_extended && norm_max) ? fmt->emax - 1 : fmt->emax);
5449 if (is_ibm_extended && !norm_max)
5451 /* This is an IBM extended double format made up of two IEEE
5452 doubles. The value of the long double is the sum of the
5453 values of the two parts. The most significant part is
5454 required to be the value of the long double rounded to the
5455 nearest double. Rounding means we need a slightly smaller
5456 value for LDBL_MAX. */
5457 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
5460 gcc_assert (strlen (buf) < len);
5463 /* True if all values of integral type can be represented
5464 by this floating-point type exactly. */
5466 bool format_helper::can_represent_integral_type_p (tree type) const
5468 gcc_assert (! decimal_p () && INTEGRAL_TYPE_P (type));
5470 /* INT?_MIN is power-of-two so it takes
5471 only one mantissa bit. */
5472 bool signed_p = TYPE_SIGN (type) == SIGNED;
5473 return TYPE_PRECISION (type) - signed_p <= significand_size (*this);
5476 /* True if mode M has a NaN representation and
5477 the treatment of NaN operands is important. */
5479 bool
5480 HONOR_NANS (machine_mode m)
5482 return MODE_HAS_NANS (m) && !flag_finite_math_only;
5485 bool
5486 HONOR_NANS (const_tree t)
5488 return HONOR_NANS (element_mode (t));
5491 bool
5492 HONOR_NANS (const_rtx x)
5494 return HONOR_NANS (GET_MODE (x));
5497 /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5499 bool
5500 HONOR_SNANS (machine_mode m)
5502 return flag_signaling_nans && HONOR_NANS (m);
5505 bool
5506 HONOR_SNANS (const_tree t)
5508 return HONOR_SNANS (element_mode (t));
5511 bool
5512 HONOR_SNANS (const_rtx x)
5514 return HONOR_SNANS (GET_MODE (x));
5517 /* As for HONOR_NANS, but true if the mode can represent infinity and
5518 the treatment of infinite values is important. */
5520 bool
5521 HONOR_INFINITIES (machine_mode m)
5523 return MODE_HAS_INFINITIES (m) && !flag_finite_math_only;
5526 bool
5527 HONOR_INFINITIES (const_tree t)
5529 return HONOR_INFINITIES (element_mode (t));
5532 bool
5533 HONOR_INFINITIES (const_rtx x)
5535 return HONOR_INFINITIES (GET_MODE (x));
5538 /* Like HONOR_NANS, but true if the given mode distinguishes between
5539 positive and negative zero, and the sign of zero is important. */
5541 bool
5542 HONOR_SIGNED_ZEROS (machine_mode m)
5544 return MODE_HAS_SIGNED_ZEROS (m) && flag_signed_zeros;
5547 bool
5548 HONOR_SIGNED_ZEROS (const_tree t)
5550 return HONOR_SIGNED_ZEROS (element_mode (t));
5553 bool
5554 HONOR_SIGNED_ZEROS (const_rtx x)
5556 return HONOR_SIGNED_ZEROS (GET_MODE (x));
5559 /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5560 and the rounding mode is important. */
5562 bool
5563 HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m)
5565 return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m) && flag_rounding_math;
5568 bool
5569 HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t)
5571 return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t));
5574 bool
5575 HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x)
5577 return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x));
5580 /* Fills r with the largest value such that 1 + r*r won't overflow.
5581 This is used in both sin (atan (x)) and cos (atan(x)) optimizations. */
5583 void
5584 build_sinatan_real (REAL_VALUE_TYPE * r, tree type)
5586 REAL_VALUE_TYPE maxval;
5587 mpfr_t mpfr_const1, mpfr_c, mpfr_maxval;
5588 machine_mode mode = TYPE_MODE (type);
5589 const struct real_format * fmt = REAL_MODE_FORMAT (mode);
5591 real_maxval (&maxval, 0, mode);
5593 mpfr_inits (mpfr_const1, mpfr_c, mpfr_maxval, NULL);
5595 mpfr_from_real (mpfr_const1, &dconst1, MPFR_RNDN);
5596 mpfr_from_real (mpfr_maxval, &maxval, MPFR_RNDN);
5598 mpfr_sub (mpfr_c, mpfr_maxval, mpfr_const1, MPFR_RNDN);
5599 mpfr_sqrt (mpfr_c, mpfr_c, MPFR_RNDZ);
5601 real_from_mpfr (r, mpfr_c, fmt, MPFR_RNDZ);
5603 mpfr_clears (mpfr_const1, mpfr_c, mpfr_maxval, NULL);