compiler: only build thunk struct type when it is needed
[official-gcc.git] / gcc / real.cc
blobaae7c335d595e142fbb5988b5abf3b88033ba8c1
1 /* real.cc - software floating point emulation.
2 Copyright (C) 1993-2022 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 /* Initialize R with a positive zero. */
117 static inline void
118 get_zero (REAL_VALUE_TYPE *r, int sign)
120 memset (r, 0, sizeof (*r));
121 r->sign = sign;
124 /* Initialize R with the canonical quiet NaN. */
126 static inline void
127 get_canonical_qnan (REAL_VALUE_TYPE *r, int sign)
129 memset (r, 0, sizeof (*r));
130 r->cl = rvc_nan;
131 r->sign = sign;
132 r->canonical = 1;
135 static inline void
136 get_canonical_snan (REAL_VALUE_TYPE *r, int sign)
138 memset (r, 0, sizeof (*r));
139 r->cl = rvc_nan;
140 r->sign = sign;
141 r->signalling = 1;
142 r->canonical = 1;
145 static inline void
146 get_inf (REAL_VALUE_TYPE *r, int sign)
148 memset (r, 0, sizeof (*r));
149 r->cl = rvc_inf;
150 r->sign = sign;
154 /* Right-shift the significand of A by N bits; put the result in the
155 significand of R. If any one bits are shifted out, return true. */
157 static bool
158 sticky_rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
159 unsigned int n)
161 unsigned long sticky = 0;
162 unsigned int i, ofs = 0;
164 if (n >= HOST_BITS_PER_LONG)
166 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
167 sticky |= a->sig[i];
168 n &= HOST_BITS_PER_LONG - 1;
171 if (n != 0)
173 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
174 for (i = 0; i < SIGSZ; ++i)
176 r->sig[i]
177 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
178 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
179 << (HOST_BITS_PER_LONG - n)));
182 else
184 for (i = 0; ofs + i < SIGSZ; ++i)
185 r->sig[i] = a->sig[ofs + i];
186 for (; i < SIGSZ; ++i)
187 r->sig[i] = 0;
190 return sticky != 0;
193 /* Right-shift the significand of A by N bits; put the result in the
194 significand of R. */
196 static void
197 rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
198 unsigned int n)
200 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
202 n &= HOST_BITS_PER_LONG - 1;
203 if (n != 0)
205 for (i = 0; i < SIGSZ; ++i)
207 r->sig[i]
208 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
209 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
210 << (HOST_BITS_PER_LONG - n)));
213 else
215 for (i = 0; ofs + i < SIGSZ; ++i)
216 r->sig[i] = a->sig[ofs + i];
217 for (; i < SIGSZ; ++i)
218 r->sig[i] = 0;
222 /* Left-shift the significand of A by N bits; put the result in the
223 significand of R. */
225 static void
226 lshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
227 unsigned int n)
229 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
231 n &= HOST_BITS_PER_LONG - 1;
232 if (n == 0)
234 for (i = 0; ofs + i < SIGSZ; ++i)
235 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
236 for (; i < SIGSZ; ++i)
237 r->sig[SIGSZ-1-i] = 0;
239 else
240 for (i = 0; i < SIGSZ; ++i)
242 r->sig[SIGSZ-1-i]
243 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
244 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
245 >> (HOST_BITS_PER_LONG - n)));
249 /* Likewise, but N is specialized to 1. */
251 static inline void
252 lshift_significand_1 (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
254 unsigned int i;
256 for (i = SIGSZ - 1; i > 0; --i)
257 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
258 r->sig[0] = a->sig[0] << 1;
261 /* Add the significands of A and B, placing the result in R. Return
262 true if there was carry out of the most significant word. */
264 static inline bool
265 add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
266 const REAL_VALUE_TYPE *b)
268 bool carry = false;
269 int i;
271 for (i = 0; i < SIGSZ; ++i)
273 unsigned long ai = a->sig[i];
274 unsigned long ri = ai + b->sig[i];
276 if (carry)
278 carry = ri < ai;
279 carry |= ++ri == 0;
281 else
282 carry = ri < ai;
284 r->sig[i] = ri;
287 return carry;
290 /* Subtract the significands of A and B, placing the result in R. CARRY is
291 true if there's a borrow incoming to the least significant word.
292 Return true if there was borrow out of the most significant word. */
294 static inline bool
295 sub_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
296 const REAL_VALUE_TYPE *b, int carry)
298 int i;
300 for (i = 0; i < SIGSZ; ++i)
302 unsigned long ai = a->sig[i];
303 unsigned long ri = ai - b->sig[i];
305 if (carry)
307 carry = ri > ai;
308 carry |= ~--ri == 0;
310 else
311 carry = ri > ai;
313 r->sig[i] = ri;
316 return carry;
319 /* Negate the significand A, placing the result in R. */
321 static inline void
322 neg_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
324 bool carry = true;
325 int i;
327 for (i = 0; i < SIGSZ; ++i)
329 unsigned long ri, ai = a->sig[i];
331 if (carry)
333 if (ai)
335 ri = -ai;
336 carry = false;
338 else
339 ri = ai;
341 else
342 ri = ~ai;
344 r->sig[i] = ri;
348 /* Compare significands. Return tri-state vs zero. */
350 static inline int
351 cmp_significands (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
353 int i;
355 for (i = SIGSZ - 1; i >= 0; --i)
357 unsigned long ai = a->sig[i];
358 unsigned long bi = b->sig[i];
360 if (ai > bi)
361 return 1;
362 if (ai < bi)
363 return -1;
366 return 0;
369 /* Return true if A is nonzero. */
371 static inline int
372 cmp_significand_0 (const REAL_VALUE_TYPE *a)
374 int i;
376 for (i = SIGSZ - 1; i >= 0; --i)
377 if (a->sig[i])
378 return 1;
380 return 0;
383 /* Set bit N of the significand of R. */
385 static inline void
386 set_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
388 r->sig[n / HOST_BITS_PER_LONG]
389 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
392 /* Clear bit N of the significand of R. */
394 static inline void
395 clear_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
397 r->sig[n / HOST_BITS_PER_LONG]
398 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
401 /* Test bit N of the significand of R. */
403 static inline bool
404 test_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
406 /* ??? Compiler bug here if we return this expression directly.
407 The conversion to bool strips the "&1" and we wind up testing
408 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
409 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
410 return t;
413 /* Clear bits 0..N-1 of the significand of R. */
415 static void
416 clear_significand_below (REAL_VALUE_TYPE *r, unsigned int n)
418 int i, w = n / HOST_BITS_PER_LONG;
420 for (i = 0; i < w; ++i)
421 r->sig[i] = 0;
423 /* We are actually passing N == SIGNIFICAND_BITS which would result
424 in an out-of-bound access below. */
425 if (n % HOST_BITS_PER_LONG != 0)
426 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
429 /* Divide the significands of A and B, placing the result in R. Return
430 true if the division was inexact. */
432 static inline bool
433 div_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
434 const REAL_VALUE_TYPE *b)
436 REAL_VALUE_TYPE u;
437 int i, bit = SIGNIFICAND_BITS - 1;
438 unsigned long msb, inexact;
440 u = *a;
441 memset (r->sig, 0, sizeof (r->sig));
443 msb = 0;
444 goto start;
447 msb = u.sig[SIGSZ-1] & SIG_MSB;
448 lshift_significand_1 (&u, &u);
449 start:
450 if (msb || cmp_significands (&u, b) >= 0)
452 sub_significands (&u, &u, b, 0);
453 set_significand_bit (r, bit);
456 while (--bit >= 0);
458 for (i = 0, inexact = 0; i < SIGSZ; i++)
459 inexact |= u.sig[i];
461 return inexact != 0;
464 /* Adjust the exponent and significand of R such that the most
465 significant bit is set. We underflow to zero and overflow to
466 infinity here, without denormals. (The intermediate representation
467 exponent is large enough to handle target denormals normalized.) */
469 static void
470 normalize (REAL_VALUE_TYPE *r)
472 int shift = 0, exp;
473 int i, j;
475 if (r->decimal)
476 return;
478 /* Find the first word that is nonzero. */
479 for (i = SIGSZ - 1; i >= 0; i--)
480 if (r->sig[i] == 0)
481 shift += HOST_BITS_PER_LONG;
482 else
483 break;
485 /* Zero significand flushes to zero. */
486 if (i < 0)
488 r->cl = rvc_zero;
489 SET_REAL_EXP (r, 0);
490 return;
493 /* Find the first bit that is nonzero. */
494 for (j = 0; ; j++)
495 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
496 break;
497 shift += j;
499 if (shift > 0)
501 exp = REAL_EXP (r) - shift;
502 if (exp > MAX_EXP)
503 get_inf (r, r->sign);
504 else if (exp < -MAX_EXP)
505 get_zero (r, r->sign);
506 else
508 SET_REAL_EXP (r, exp);
509 lshift_significand (r, r, shift);
514 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
515 result may be inexact due to a loss of precision. */
517 static bool
518 do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
519 const REAL_VALUE_TYPE *b, int subtract_p)
521 int dexp, sign, exp;
522 REAL_VALUE_TYPE t;
523 bool inexact = false;
525 /* Determine if we need to add or subtract. */
526 sign = a->sign;
527 subtract_p = (sign ^ b->sign) ^ subtract_p;
529 switch (CLASS2 (a->cl, b->cl))
531 case CLASS2 (rvc_zero, rvc_zero):
532 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
533 get_zero (r, sign & !subtract_p);
534 return false;
536 case CLASS2 (rvc_zero, rvc_normal):
537 case CLASS2 (rvc_zero, rvc_inf):
538 case CLASS2 (rvc_zero, rvc_nan):
539 /* 0 + ANY = ANY. */
540 case CLASS2 (rvc_normal, rvc_nan):
541 case CLASS2 (rvc_inf, rvc_nan):
542 case CLASS2 (rvc_nan, rvc_nan):
543 /* ANY + NaN = NaN. */
544 case CLASS2 (rvc_normal, rvc_inf):
545 /* R + Inf = Inf. */
546 *r = *b;
547 /* Make resulting NaN value to be qNaN. The caller has the
548 responsibility to avoid the operation if flag_signaling_nans
549 is on. */
550 r->signalling = 0;
551 r->sign = sign ^ subtract_p;
552 return false;
554 case CLASS2 (rvc_normal, rvc_zero):
555 case CLASS2 (rvc_inf, rvc_zero):
556 case CLASS2 (rvc_nan, rvc_zero):
557 /* ANY + 0 = ANY. */
558 case CLASS2 (rvc_nan, rvc_normal):
559 case CLASS2 (rvc_nan, rvc_inf):
560 /* NaN + ANY = NaN. */
561 case CLASS2 (rvc_inf, rvc_normal):
562 /* Inf + R = Inf. */
563 *r = *a;
564 /* Make resulting NaN value to be qNaN. The caller has the
565 responsibility to avoid the operation if flag_signaling_nans
566 is on. */
567 r->signalling = 0;
568 return false;
570 case CLASS2 (rvc_inf, rvc_inf):
571 if (subtract_p)
572 /* Inf - Inf = NaN. */
573 get_canonical_qnan (r, 0);
574 else
575 /* Inf + Inf = Inf. */
576 *r = *a;
577 return false;
579 case CLASS2 (rvc_normal, rvc_normal):
580 break;
582 default:
583 gcc_unreachable ();
586 /* Swap the arguments such that A has the larger exponent. */
587 dexp = REAL_EXP (a) - REAL_EXP (b);
588 if (dexp < 0)
590 const REAL_VALUE_TYPE *t;
591 t = a, a = b, b = t;
592 dexp = -dexp;
593 sign ^= subtract_p;
595 exp = REAL_EXP (a);
597 /* If the exponents are not identical, we need to shift the
598 significand of B down. */
599 if (dexp > 0)
601 /* If the exponents are too far apart, the significands
602 do not overlap, which makes the subtraction a noop. */
603 if (dexp >= SIGNIFICAND_BITS)
605 *r = *a;
606 r->sign = sign;
607 return true;
610 inexact |= sticky_rshift_significand (&t, b, dexp);
611 b = &t;
614 if (subtract_p)
616 if (sub_significands (r, a, b, inexact))
618 /* We got a borrow out of the subtraction. That means that
619 A and B had the same exponent, and B had the larger
620 significand. We need to swap the sign and negate the
621 significand. */
622 sign ^= 1;
623 neg_significand (r, r);
626 else
628 if (add_significands (r, a, b))
630 /* We got carry out of the addition. This means we need to
631 shift the significand back down one bit and increase the
632 exponent. */
633 inexact |= sticky_rshift_significand (r, r, 1);
634 r->sig[SIGSZ-1] |= SIG_MSB;
635 if (++exp > MAX_EXP)
637 get_inf (r, sign);
638 return true;
643 r->cl = rvc_normal;
644 r->sign = sign;
645 SET_REAL_EXP (r, exp);
646 /* Zero out the remaining fields. */
647 r->signalling = 0;
648 r->canonical = 0;
649 r->decimal = 0;
651 /* Re-normalize the result. */
652 normalize (r);
654 /* Special case: if the subtraction results in zero, the result
655 is positive. */
656 if (r->cl == rvc_zero)
657 r->sign = 0;
658 else
659 r->sig[0] |= inexact;
661 return inexact;
664 /* Calculate R = A * B. Return true if the result may be inexact. */
666 static bool
667 do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
668 const REAL_VALUE_TYPE *b)
670 REAL_VALUE_TYPE u, t, *rr;
671 unsigned int i, j, k;
672 int sign = a->sign ^ b->sign;
673 bool inexact = false;
675 switch (CLASS2 (a->cl, b->cl))
677 case CLASS2 (rvc_zero, rvc_zero):
678 case CLASS2 (rvc_zero, rvc_normal):
679 case CLASS2 (rvc_normal, rvc_zero):
680 /* +-0 * ANY = 0 with appropriate sign. */
681 get_zero (r, sign);
682 return false;
684 case CLASS2 (rvc_zero, rvc_nan):
685 case CLASS2 (rvc_normal, rvc_nan):
686 case CLASS2 (rvc_inf, rvc_nan):
687 case CLASS2 (rvc_nan, rvc_nan):
688 /* ANY * NaN = NaN. */
689 *r = *b;
690 /* Make resulting NaN value to be qNaN. The caller has the
691 responsibility to avoid the operation if flag_signaling_nans
692 is on. */
693 r->signalling = 0;
694 r->sign = sign;
695 return false;
697 case CLASS2 (rvc_nan, rvc_zero):
698 case CLASS2 (rvc_nan, rvc_normal):
699 case CLASS2 (rvc_nan, rvc_inf):
700 /* NaN * ANY = NaN. */
701 *r = *a;
702 /* Make resulting NaN value to be qNaN. The caller has the
703 responsibility to avoid the operation if flag_signaling_nans
704 is on. */
705 r->signalling = 0;
706 r->sign = sign;
707 return false;
709 case CLASS2 (rvc_zero, rvc_inf):
710 case CLASS2 (rvc_inf, rvc_zero):
711 /* 0 * Inf = NaN */
712 get_canonical_qnan (r, sign);
713 return false;
715 case CLASS2 (rvc_inf, rvc_inf):
716 case CLASS2 (rvc_normal, rvc_inf):
717 case CLASS2 (rvc_inf, rvc_normal):
718 /* Inf * Inf = Inf, R * Inf = Inf */
719 get_inf (r, sign);
720 return false;
722 case CLASS2 (rvc_normal, rvc_normal):
723 break;
725 default:
726 gcc_unreachable ();
729 if (r == a || r == b)
730 rr = &t;
731 else
732 rr = r;
733 get_zero (rr, 0);
735 /* Collect all the partial products. Since we don't have sure access
736 to a widening multiply, we split each long into two half-words.
738 Consider the long-hand form of a four half-word multiplication:
740 A B C D
741 * E F G H
742 --------------
743 DE DF DG DH
744 CE CF CG CH
745 BE BF BG BH
746 AE AF AG AH
748 We construct partial products of the widened half-word products
749 that are known to not overlap, e.g. DF+DH. Each such partial
750 product is given its proper exponent, which allows us to sum them
751 and obtain the finished product. */
753 for (i = 0; i < SIGSZ * 2; ++i)
755 unsigned long ai = a->sig[i / 2];
756 if (i & 1)
757 ai >>= HOST_BITS_PER_LONG / 2;
758 else
759 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
761 if (ai == 0)
762 continue;
764 for (j = 0; j < 2; ++j)
766 int exp = (REAL_EXP (a) - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
767 + (REAL_EXP (b) - (1-j)*(HOST_BITS_PER_LONG/2)));
769 if (exp > MAX_EXP)
771 get_inf (r, sign);
772 return true;
774 if (exp < -MAX_EXP)
776 /* Would underflow to zero, which we shouldn't bother adding. */
777 inexact = true;
778 continue;
781 memset (&u, 0, sizeof (u));
782 u.cl = rvc_normal;
783 SET_REAL_EXP (&u, exp);
785 for (k = j; k < SIGSZ * 2; k += 2)
787 unsigned long bi = b->sig[k / 2];
788 if (k & 1)
789 bi >>= HOST_BITS_PER_LONG / 2;
790 else
791 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
793 u.sig[k / 2] = ai * bi;
796 normalize (&u);
797 inexact |= do_add (rr, rr, &u, 0);
801 rr->sign = sign;
802 if (rr != r)
803 *r = t;
805 return inexact;
808 /* Calculate R = A / B. Return true if the result may be inexact. */
810 static bool
811 do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
812 const REAL_VALUE_TYPE *b)
814 int exp, sign = a->sign ^ b->sign;
815 REAL_VALUE_TYPE t, *rr;
816 bool inexact;
818 switch (CLASS2 (a->cl, b->cl))
820 case CLASS2 (rvc_zero, rvc_zero):
821 /* 0 / 0 = NaN. */
822 case CLASS2 (rvc_inf, rvc_inf):
823 /* Inf / Inf = NaN. */
824 get_canonical_qnan (r, sign);
825 return false;
827 case CLASS2 (rvc_zero, rvc_normal):
828 case CLASS2 (rvc_zero, rvc_inf):
829 /* 0 / ANY = 0. */
830 case CLASS2 (rvc_normal, rvc_inf):
831 /* R / Inf = 0. */
832 get_zero (r, sign);
833 return false;
835 case CLASS2 (rvc_normal, rvc_zero):
836 /* R / 0 = Inf. */
837 case CLASS2 (rvc_inf, rvc_zero):
838 /* Inf / 0 = Inf. */
839 get_inf (r, sign);
840 return false;
842 case CLASS2 (rvc_zero, rvc_nan):
843 case CLASS2 (rvc_normal, rvc_nan):
844 case CLASS2 (rvc_inf, rvc_nan):
845 case CLASS2 (rvc_nan, rvc_nan):
846 /* ANY / NaN = NaN. */
847 *r = *b;
848 /* Make resulting NaN value to be qNaN. The caller has the
849 responsibility to avoid the operation if flag_signaling_nans
850 is on. */
851 r->signalling = 0;
852 r->sign = sign;
853 return false;
855 case CLASS2 (rvc_nan, rvc_zero):
856 case CLASS2 (rvc_nan, rvc_normal):
857 case CLASS2 (rvc_nan, rvc_inf):
858 /* NaN / ANY = NaN. */
859 *r = *a;
860 /* Make resulting NaN value to be qNaN. The caller has the
861 responsibility to avoid the operation if flag_signaling_nans
862 is on. */
863 r->signalling = 0;
864 r->sign = sign;
865 return false;
867 case CLASS2 (rvc_inf, rvc_normal):
868 /* Inf / R = Inf. */
869 get_inf (r, sign);
870 return false;
872 case CLASS2 (rvc_normal, rvc_normal):
873 break;
875 default:
876 gcc_unreachable ();
879 if (r == a || r == b)
880 rr = &t;
881 else
882 rr = r;
884 /* Make sure all fields in the result are initialized. */
885 get_zero (rr, 0);
886 rr->cl = rvc_normal;
887 rr->sign = sign;
889 exp = REAL_EXP (a) - REAL_EXP (b) + 1;
890 if (exp > MAX_EXP)
892 get_inf (r, sign);
893 return true;
895 if (exp < -MAX_EXP)
897 get_zero (r, sign);
898 return true;
900 SET_REAL_EXP (rr, exp);
902 inexact = div_significands (rr, a, b);
904 /* Re-normalize the result. */
905 normalize (rr);
906 rr->sig[0] |= inexact;
908 if (rr != r)
909 *r = t;
911 return inexact;
914 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
915 one of the two operands is a NaN. */
917 static int
918 do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
919 int nan_result)
921 int ret;
923 switch (CLASS2 (a->cl, b->cl))
925 case CLASS2 (rvc_zero, rvc_zero):
926 /* Sign of zero doesn't matter for compares. */
927 return 0;
929 case CLASS2 (rvc_normal, rvc_zero):
930 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
931 if (a->decimal)
932 return decimal_do_compare (a, b, nan_result);
933 /* Fall through. */
934 case CLASS2 (rvc_inf, rvc_zero):
935 case CLASS2 (rvc_inf, rvc_normal):
936 return (a->sign ? -1 : 1);
938 case CLASS2 (rvc_inf, rvc_inf):
939 return -a->sign - -b->sign;
941 case CLASS2 (rvc_zero, rvc_normal):
942 /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
943 if (b->decimal)
944 return decimal_do_compare (a, b, nan_result);
945 /* Fall through. */
946 case CLASS2 (rvc_zero, rvc_inf):
947 case CLASS2 (rvc_normal, rvc_inf):
948 return (b->sign ? 1 : -1);
950 case CLASS2 (rvc_zero, rvc_nan):
951 case CLASS2 (rvc_normal, rvc_nan):
952 case CLASS2 (rvc_inf, rvc_nan):
953 case CLASS2 (rvc_nan, rvc_nan):
954 case CLASS2 (rvc_nan, rvc_zero):
955 case CLASS2 (rvc_nan, rvc_normal):
956 case CLASS2 (rvc_nan, rvc_inf):
957 return nan_result;
959 case CLASS2 (rvc_normal, rvc_normal):
960 break;
962 default:
963 gcc_unreachable ();
966 if (a->decimal || b->decimal)
967 return decimal_do_compare (a, b, nan_result);
969 if (a->sign != b->sign)
970 return -a->sign - -b->sign;
972 if (REAL_EXP (a) > REAL_EXP (b))
973 ret = 1;
974 else if (REAL_EXP (a) < REAL_EXP (b))
975 ret = -1;
976 else
977 ret = cmp_significands (a, b);
979 return (a->sign ? -ret : ret);
982 /* Return A truncated to an integral value toward zero. */
984 static void
985 do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
987 *r = *a;
989 switch (r->cl)
991 case rvc_zero:
992 case rvc_inf:
993 case rvc_nan:
994 /* Make resulting NaN value to be qNaN. The caller has the
995 responsibility to avoid the operation if flag_signaling_nans
996 is on. */
997 r->signalling = 0;
998 break;
1000 case rvc_normal:
1001 if (r->decimal)
1003 decimal_do_fix_trunc (r, a);
1004 return;
1006 if (REAL_EXP (r) <= 0)
1007 get_zero (r, r->sign);
1008 else if (REAL_EXP (r) < SIGNIFICAND_BITS)
1009 clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r));
1010 break;
1012 default:
1013 gcc_unreachable ();
1017 /* Perform the binary or unary operation described by CODE.
1018 For a unary operation, leave OP1 NULL. This function returns
1019 true if the result may be inexact due to loss of precision. */
1021 bool
1022 real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0,
1023 const REAL_VALUE_TYPE *op1)
1025 enum tree_code code = (enum tree_code) icode;
1027 if (op0->decimal || (op1 && op1->decimal))
1028 return decimal_real_arithmetic (r, code, op0, op1);
1030 switch (code)
1032 case PLUS_EXPR:
1033 /* Clear any padding areas in *r if it isn't equal to one of the
1034 operands so that we can later do bitwise comparisons later on. */
1035 if (r != op0 && r != op1)
1036 memset (r, '\0', sizeof (*r));
1037 return do_add (r, op0, op1, 0);
1039 case MINUS_EXPR:
1040 if (r != op0 && r != op1)
1041 memset (r, '\0', sizeof (*r));
1042 return do_add (r, op0, op1, 1);
1044 case MULT_EXPR:
1045 if (r != op0 && r != op1)
1046 memset (r, '\0', sizeof (*r));
1047 return do_multiply (r, op0, op1);
1049 case RDIV_EXPR:
1050 if (r != op0 && r != op1)
1051 memset (r, '\0', sizeof (*r));
1052 return do_divide (r, op0, op1);
1054 case MIN_EXPR:
1055 if (op1->cl == rvc_nan)
1057 *r = *op1;
1058 /* Make resulting NaN value to be qNaN. The caller has the
1059 responsibility to avoid the operation if flag_signaling_nans
1060 is on. */
1061 r->signalling = 0;
1063 else if (do_compare (op0, op1, -1) < 0)
1064 *r = *op0;
1065 else
1066 *r = *op1;
1067 break;
1069 case MAX_EXPR:
1070 if (op1->cl == rvc_nan)
1072 *r = *op1;
1073 /* Make resulting NaN value to be qNaN. The caller has the
1074 responsibility to avoid the operation if flag_signaling_nans
1075 is on. */
1076 r->signalling = 0;
1078 else if (do_compare (op0, op1, 1) < 0)
1079 *r = *op1;
1080 else
1081 *r = *op0;
1082 break;
1084 case NEGATE_EXPR:
1085 *r = *op0;
1086 r->sign ^= 1;
1087 break;
1089 case ABS_EXPR:
1090 *r = *op0;
1091 r->sign = 0;
1092 break;
1094 case FIX_TRUNC_EXPR:
1095 do_fix_trunc (r, op0);
1096 break;
1098 default:
1099 gcc_unreachable ();
1101 return false;
1104 REAL_VALUE_TYPE
1105 real_value_negate (const REAL_VALUE_TYPE *op0)
1107 REAL_VALUE_TYPE r;
1108 real_arithmetic (&r, NEGATE_EXPR, op0, NULL);
1109 return r;
1112 REAL_VALUE_TYPE
1113 real_value_abs (const REAL_VALUE_TYPE *op0)
1115 REAL_VALUE_TYPE r;
1116 real_arithmetic (&r, ABS_EXPR, op0, NULL);
1117 return r;
1120 /* Return whether OP0 == OP1. */
1122 bool
1123 real_equal (const REAL_VALUE_TYPE *op0, const REAL_VALUE_TYPE *op1)
1125 return do_compare (op0, op1, -1) == 0;
1128 /* Return whether OP0 < OP1. */
1130 bool
1131 real_less (const REAL_VALUE_TYPE *op0, const REAL_VALUE_TYPE *op1)
1133 return do_compare (op0, op1, 1) < 0;
1136 bool
1137 real_compare (int icode, const REAL_VALUE_TYPE *op0,
1138 const REAL_VALUE_TYPE *op1)
1140 enum tree_code code = (enum tree_code) icode;
1142 switch (code)
1144 case LT_EXPR:
1145 return real_less (op0, op1);
1146 case LE_EXPR:
1147 return do_compare (op0, op1, 1) <= 0;
1148 case GT_EXPR:
1149 return do_compare (op0, op1, -1) > 0;
1150 case GE_EXPR:
1151 return do_compare (op0, op1, -1) >= 0;
1152 case EQ_EXPR:
1153 return real_equal (op0, op1);
1154 case NE_EXPR:
1155 return do_compare (op0, op1, -1) != 0;
1156 case UNORDERED_EXPR:
1157 return op0->cl == rvc_nan || op1->cl == rvc_nan;
1158 case ORDERED_EXPR:
1159 return op0->cl != rvc_nan && op1->cl != rvc_nan;
1160 case UNLT_EXPR:
1161 return do_compare (op0, op1, -1) < 0;
1162 case UNLE_EXPR:
1163 return do_compare (op0, op1, -1) <= 0;
1164 case UNGT_EXPR:
1165 return do_compare (op0, op1, 1) > 0;
1166 case UNGE_EXPR:
1167 return do_compare (op0, op1, 1) >= 0;
1168 case UNEQ_EXPR:
1169 return do_compare (op0, op1, 0) == 0;
1170 case LTGT_EXPR:
1171 return do_compare (op0, op1, 0) != 0;
1173 default:
1174 gcc_unreachable ();
1178 /* Return floor log2(R). */
1181 real_exponent (const REAL_VALUE_TYPE *r)
1183 switch (r->cl)
1185 case rvc_zero:
1186 return 0;
1187 case rvc_inf:
1188 case rvc_nan:
1189 return (unsigned int)-1 >> 1;
1190 case rvc_normal:
1191 return REAL_EXP (r);
1192 default:
1193 gcc_unreachable ();
1197 /* R = OP0 * 2**EXP. */
1199 void
1200 real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1202 *r = *op0;
1203 switch (r->cl)
1205 case rvc_zero:
1206 case rvc_inf:
1207 case rvc_nan:
1208 /* Make resulting NaN value to be qNaN. The caller has the
1209 responsibility to avoid the operation if flag_signaling_nans
1210 is on. */
1211 r->signalling = 0;
1212 break;
1214 case rvc_normal:
1215 exp += REAL_EXP (op0);
1216 if (exp > MAX_EXP)
1217 get_inf (r, r->sign);
1218 else if (exp < -MAX_EXP)
1219 get_zero (r, r->sign);
1220 else
1221 SET_REAL_EXP (r, exp);
1222 break;
1224 default:
1225 gcc_unreachable ();
1229 /* Determine whether a floating-point value X is infinite. */
1231 bool
1232 real_isinf (const REAL_VALUE_TYPE *r)
1234 return (r->cl == rvc_inf);
1237 /* Determine whether a floating-point value X is infinite with SIGN. */
1239 bool
1240 real_isinf (const REAL_VALUE_TYPE *r, bool sign)
1242 return real_isinf (r) && r->sign == sign;
1245 /* Determine whether a floating-point value X is a NaN. */
1247 bool
1248 real_isnan (const REAL_VALUE_TYPE *r)
1250 return (r->cl == rvc_nan);
1253 /* Determine whether a floating-point value X is a signaling NaN. */
1254 bool real_issignaling_nan (const REAL_VALUE_TYPE *r)
1256 return real_isnan (r) && r->signalling;
1259 /* Determine whether a floating-point value X is finite. */
1261 bool
1262 real_isfinite (const REAL_VALUE_TYPE *r)
1264 return (r->cl != rvc_nan) && (r->cl != rvc_inf);
1267 /* Determine whether a floating-point value X is negative. */
1269 bool
1270 real_isneg (const REAL_VALUE_TYPE *r)
1272 return r->sign;
1275 /* Determine whether a floating-point value X is plus or minus zero. */
1277 bool
1278 real_iszero (const REAL_VALUE_TYPE *r)
1280 return r->cl == rvc_zero;
1283 /* Determine whether a floating-point value X is zero with SIGN. */
1285 bool
1286 real_iszero (const REAL_VALUE_TYPE *r, bool sign)
1288 return real_iszero (r) && r->sign == sign;
1291 /* Determine whether a floating-point value X is minus zero. */
1293 bool
1294 real_isnegzero (const REAL_VALUE_TYPE *r)
1296 return r->sign && r->cl == rvc_zero;
1299 /* Compare two floating-point objects for bitwise identity. */
1301 bool
1302 real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1304 int i;
1306 if (a->cl != b->cl)
1307 return false;
1308 if (a->sign != b->sign)
1309 return false;
1311 switch (a->cl)
1313 case rvc_zero:
1314 case rvc_inf:
1315 return true;
1317 case rvc_normal:
1318 if (a->decimal != b->decimal)
1319 return false;
1320 if (REAL_EXP (a) != REAL_EXP (b))
1321 return false;
1322 break;
1324 case rvc_nan:
1325 if (a->signalling != b->signalling)
1326 return false;
1327 /* The significand is ignored for canonical NaNs. */
1328 if (a->canonical || b->canonical)
1329 return a->canonical == b->canonical;
1330 break;
1332 default:
1333 gcc_unreachable ();
1336 for (i = 0; i < SIGSZ; ++i)
1337 if (a->sig[i] != b->sig[i])
1338 return false;
1340 return true;
1343 /* Try to change R into its exact multiplicative inverse in format FMT.
1344 Return true if successful. */
1346 bool
1347 exact_real_inverse (format_helper fmt, REAL_VALUE_TYPE *r)
1349 const REAL_VALUE_TYPE *one = real_digit (1);
1350 REAL_VALUE_TYPE u;
1351 int i;
1353 if (r->cl != rvc_normal)
1354 return false;
1356 /* Check for a power of two: all significand bits zero except the MSB. */
1357 for (i = 0; i < SIGSZ-1; ++i)
1358 if (r->sig[i] != 0)
1359 return false;
1360 if (r->sig[SIGSZ-1] != SIG_MSB)
1361 return false;
1363 /* Find the inverse and truncate to the required format. */
1364 do_divide (&u, one, r);
1365 real_convert (&u, fmt, &u);
1367 /* The rounding may have overflowed. */
1368 if (u.cl != rvc_normal)
1369 return false;
1370 for (i = 0; i < SIGSZ-1; ++i)
1371 if (u.sig[i] != 0)
1372 return false;
1373 if (u.sig[SIGSZ-1] != SIG_MSB)
1374 return false;
1376 *r = u;
1377 return true;
1380 /* Return true if arithmetic on values in IMODE that were promoted
1381 from values in TMODE is equivalent to direct arithmetic on values
1382 in TMODE. */
1384 bool
1385 real_can_shorten_arithmetic (machine_mode imode, machine_mode tmode)
1387 const struct real_format *tfmt, *ifmt;
1388 tfmt = REAL_MODE_FORMAT (tmode);
1389 ifmt = REAL_MODE_FORMAT (imode);
1390 /* These conditions are conservative rather than trying to catch the
1391 exact boundary conditions; the main case to allow is IEEE float
1392 and double. */
1393 return (ifmt->b == tfmt->b
1394 && ifmt->p > 2 * tfmt->p
1395 && ifmt->emin < 2 * tfmt->emin - tfmt->p - 2
1396 && ifmt->emin < tfmt->emin - tfmt->emax - tfmt->p - 2
1397 && ifmt->emax > 2 * tfmt->emax + 2
1398 && ifmt->emax > tfmt->emax - tfmt->emin + tfmt->p + 2
1399 && ifmt->round_towards_zero == tfmt->round_towards_zero
1400 && (ifmt->has_sign_dependent_rounding
1401 == tfmt->has_sign_dependent_rounding)
1402 && ifmt->has_nans >= tfmt->has_nans
1403 && ifmt->has_inf >= tfmt->has_inf
1404 && ifmt->has_signed_zero >= tfmt->has_signed_zero
1405 && !MODE_COMPOSITE_P (tmode)
1406 && !MODE_COMPOSITE_P (imode));
1409 /* Render R as an integer. */
1411 HOST_WIDE_INT
1412 real_to_integer (const REAL_VALUE_TYPE *r)
1414 unsigned HOST_WIDE_INT i;
1416 switch (r->cl)
1418 case rvc_zero:
1419 underflow:
1420 return 0;
1422 case rvc_inf:
1423 case rvc_nan:
1424 overflow:
1425 i = HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1);
1426 if (!r->sign)
1427 i--;
1428 return i;
1430 case rvc_normal:
1431 if (r->decimal)
1432 return decimal_real_to_integer (r);
1434 if (REAL_EXP (r) <= 0)
1435 goto underflow;
1436 /* Only force overflow for unsigned overflow. Signed overflow is
1437 undefined, so it doesn't matter what we return, and some callers
1438 expect to be able to use this routine for both signed and
1439 unsigned conversions. */
1440 if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1441 goto overflow;
1443 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1444 i = r->sig[SIGSZ-1];
1445 else
1447 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1448 i = r->sig[SIGSZ-1];
1449 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1450 i |= r->sig[SIGSZ-2];
1453 i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1455 if (r->sign)
1456 i = -i;
1457 return i;
1459 default:
1460 gcc_unreachable ();
1464 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1465 be represented in precision, *FAIL is set to TRUE. */
1467 wide_int
1468 real_to_integer (const REAL_VALUE_TYPE *r, bool *fail, int precision)
1470 HOST_WIDE_INT val[2 * WIDE_INT_MAX_ELTS];
1471 int exp;
1472 int words, w;
1473 wide_int result;
1475 switch (r->cl)
1477 case rvc_zero:
1478 underflow:
1479 return wi::zero (precision);
1481 case rvc_inf:
1482 case rvc_nan:
1483 overflow:
1484 *fail = true;
1486 if (r->sign)
1487 return wi::set_bit_in_zero (precision - 1, precision);
1488 else
1489 return ~wi::set_bit_in_zero (precision - 1, precision);
1491 case rvc_normal:
1492 if (r->decimal)
1493 return decimal_real_to_integer (r, fail, precision);
1495 exp = REAL_EXP (r);
1496 if (exp <= 0)
1497 goto underflow;
1498 /* Only force overflow for unsigned overflow. Signed overflow is
1499 undefined, so it doesn't matter what we return, and some callers
1500 expect to be able to use this routine for both signed and
1501 unsigned conversions. */
1502 if (exp > precision)
1503 goto overflow;
1505 /* Put the significand into a wide_int that has precision W, which
1506 is the smallest HWI-multiple that has at least PRECISION bits.
1507 This ensures that the top bit of the significand is in the
1508 top bit of the wide_int. */
1509 words = (precision + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT;
1510 w = words * HOST_BITS_PER_WIDE_INT;
1512 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1513 for (int i = 0; i < words; i++)
1515 int j = SIGSZ - words + i;
1516 val[i] = (j < 0) ? 0 : r->sig[j];
1518 #else
1519 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1520 for (int i = 0; i < words; i++)
1522 int j = SIGSZ - (words * 2) + (i * 2);
1523 if (j < 0)
1524 val[i] = 0;
1525 else
1526 val[i] = r->sig[j];
1527 j += 1;
1528 if (j >= 0)
1529 val[i] |= (unsigned HOST_WIDE_INT) r->sig[j] << HOST_BITS_PER_LONG;
1531 #endif
1532 /* Shift the value into place and truncate to the desired precision. */
1533 result = wide_int::from_array (val, words, w);
1534 result = wi::lrshift (result, w - exp);
1535 result = wide_int::from (result, precision, UNSIGNED);
1537 if (r->sign)
1538 return -result;
1539 else
1540 return result;
1542 default:
1543 gcc_unreachable ();
1547 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1548 of NUM / DEN. Return the quotient and place the remainder in NUM.
1549 It is expected that NUM / DEN are close enough that the quotient is
1550 small. */
1552 static unsigned long
1553 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1555 unsigned long q, msb;
1556 int expn = REAL_EXP (num), expd = REAL_EXP (den);
1558 if (expn < expd)
1559 return 0;
1561 q = msb = 0;
1562 goto start;
1565 msb = num->sig[SIGSZ-1] & SIG_MSB;
1566 q <<= 1;
1567 lshift_significand_1 (num, num);
1568 start:
1569 if (msb || cmp_significands (num, den) >= 0)
1571 sub_significands (num, num, den, 0);
1572 q |= 1;
1575 while (--expn >= expd);
1577 SET_REAL_EXP (num, expd);
1578 normalize (num);
1580 return q;
1583 /* Render R as a decimal floating point constant. Emit DIGITS significant
1584 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1585 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1586 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1587 to a string that, when parsed back in mode MODE, yields the same value. */
1589 #define M_LOG10_2 0.30102999566398119521
1591 void
1592 real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig,
1593 size_t buf_size, size_t digits,
1594 int crop_trailing_zeros, machine_mode mode)
1596 const struct real_format *fmt = NULL;
1597 const REAL_VALUE_TYPE *one, *ten;
1598 REAL_VALUE_TYPE r, pten, u, v;
1599 int dec_exp, cmp_one, digit;
1600 size_t max_digits;
1601 char *p, *first, *last;
1602 bool sign;
1603 bool round_up;
1605 if (mode != VOIDmode)
1607 fmt = REAL_MODE_FORMAT (mode);
1608 gcc_assert (fmt);
1611 r = *r_orig;
1612 switch (r.cl)
1614 case rvc_zero:
1615 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1616 return;
1617 case rvc_normal:
1618 break;
1619 case rvc_inf:
1620 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1621 return;
1622 case rvc_nan:
1623 /* ??? Print the significand as well, if not canonical? */
1624 sprintf (str, "%c%cNaN", (r_orig->sign ? '-' : '+'),
1625 (r_orig->signalling ? 'S' : 'Q'));
1626 return;
1627 default:
1628 gcc_unreachable ();
1631 if (r.decimal)
1633 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1634 return;
1637 /* Bound the number of digits printed by the size of the representation. */
1638 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1639 if (digits == 0 || digits > max_digits)
1640 digits = max_digits;
1642 /* Estimate the decimal exponent, and compute the length of the string it
1643 will print as. Be conservative and add one to account for possible
1644 overflow or rounding error. */
1645 dec_exp = REAL_EXP (&r) * M_LOG10_2;
1646 for (max_digits = 1; dec_exp ; max_digits++)
1647 dec_exp /= 10;
1649 /* Bound the number of digits printed by the size of the output buffer. */
1650 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1651 gcc_assert (max_digits <= buf_size);
1652 if (digits > max_digits)
1653 digits = max_digits;
1655 one = real_digit (1);
1656 ten = ten_to_ptwo (0);
1658 sign = r.sign;
1659 r.sign = 0;
1661 dec_exp = 0;
1662 pten = *one;
1664 cmp_one = do_compare (&r, one, 0);
1665 if (cmp_one > 0)
1667 int m;
1669 /* Number is greater than one. Convert significand to an integer
1670 and strip trailing decimal zeros. */
1672 u = r;
1673 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1675 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1676 m = floor_log2 (max_digits);
1678 /* Iterate over the bits of the possible powers of 10 that might
1679 be present in U and eliminate them. That is, if we find that
1680 10**2**M divides U evenly, keep the division and increase
1681 DEC_EXP by 2**M. */
1684 REAL_VALUE_TYPE t;
1686 do_divide (&t, &u, ten_to_ptwo (m));
1687 do_fix_trunc (&v, &t);
1688 if (cmp_significands (&v, &t) == 0)
1690 u = t;
1691 dec_exp += 1 << m;
1694 while (--m >= 0);
1696 /* Revert the scaling to integer that we performed earlier. */
1697 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1698 - (SIGNIFICAND_BITS - 1));
1699 r = u;
1701 /* Find power of 10. Do this by dividing out 10**2**M when
1702 this is larger than the current remainder. Fill PTEN with
1703 the power of 10 that we compute. */
1704 if (REAL_EXP (&r) > 0)
1706 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1709 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1710 if (do_compare (&u, ptentwo, 0) >= 0)
1712 do_divide (&u, &u, ptentwo);
1713 do_multiply (&pten, &pten, ptentwo);
1714 dec_exp += 1 << m;
1717 while (--m >= 0);
1719 else
1720 /* We managed to divide off enough tens in the above reduction
1721 loop that we've now got a negative exponent. Fall into the
1722 less-than-one code to compute the proper value for PTEN. */
1723 cmp_one = -1;
1725 if (cmp_one < 0)
1727 int m;
1729 /* Number is less than one. Pad significand with leading
1730 decimal zeros. */
1732 v = r;
1733 while (1)
1735 /* Stop if we'd shift bits off the bottom. */
1736 if (v.sig[0] & 7)
1737 break;
1739 do_multiply (&u, &v, ten);
1741 /* Stop if we're now >= 1 or zero. */
1742 if (REAL_EXP (&u) > 0 || u.cl == rvc_zero)
1743 break;
1745 v = u;
1746 dec_exp -= 1;
1748 r = v;
1750 /* Find power of 10. Do this by multiplying in P=10**2**M when
1751 the current remainder is smaller than 1/P. Fill PTEN with the
1752 power of 10 that we compute. */
1753 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1756 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1757 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1759 if (do_compare (&v, ptenmtwo, 0) <= 0)
1761 do_multiply (&v, &v, ptentwo);
1762 do_multiply (&pten, &pten, ptentwo);
1763 dec_exp -= 1 << m;
1766 while (--m >= 0);
1768 /* Invert the positive power of 10 that we've collected so far. */
1769 do_divide (&pten, one, &pten);
1772 p = str;
1773 if (sign)
1774 *p++ = '-';
1775 first = p++;
1777 /* At this point, PTEN should contain the nearest power of 10 smaller
1778 than R, such that this division produces the first digit.
1780 Using a divide-step primitive that returns the complete integral
1781 remainder avoids the rounding error that would be produced if
1782 we were to use do_divide here and then simply multiply by 10 for
1783 each subsequent digit. */
1785 digit = rtd_divmod (&r, &pten);
1787 /* Be prepared for error in that division via underflow ... */
1788 if (digit == 0 && cmp_significand_0 (&r))
1790 /* Multiply by 10 and try again. */
1791 do_multiply (&r, &r, ten);
1792 digit = rtd_divmod (&r, &pten);
1793 dec_exp -= 1;
1794 gcc_assert (digit != 0);
1797 /* ... or overflow. */
1798 if (digit == 10)
1800 *p++ = '1';
1801 if (--digits > 0)
1802 *p++ = '0';
1803 dec_exp += 1;
1805 else
1807 gcc_assert (digit <= 10);
1808 *p++ = digit + '0';
1811 /* Generate subsequent digits. */
1812 while (--digits > 0)
1814 do_multiply (&r, &r, ten);
1815 digit = rtd_divmod (&r, &pten);
1816 *p++ = digit + '0';
1818 last = p;
1820 /* Generate one more digit with which to do rounding. */
1821 do_multiply (&r, &r, ten);
1822 digit = rtd_divmod (&r, &pten);
1824 /* Round the result. */
1825 if (fmt && fmt->round_towards_zero)
1827 /* If the format uses round towards zero when parsing the string
1828 back in, we need to always round away from zero here. */
1829 if (cmp_significand_0 (&r))
1830 digit++;
1831 round_up = digit > 0;
1833 else
1835 if (digit == 5)
1837 /* Round to nearest. If R is nonzero there are additional
1838 nonzero digits to be extracted. */
1839 if (cmp_significand_0 (&r))
1840 digit++;
1841 /* Round to even. */
1842 else if ((p[-1] - '0') & 1)
1843 digit++;
1846 round_up = digit > 5;
1849 if (round_up)
1851 while (p > first)
1853 digit = *--p;
1854 if (digit == '9')
1855 *p = '0';
1856 else
1858 *p = digit + 1;
1859 break;
1863 /* Carry out of the first digit. This means we had all 9's and
1864 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1865 if (p == first)
1867 first[1] = '1';
1868 dec_exp++;
1872 /* Insert the decimal point. */
1873 first[0] = first[1];
1874 first[1] = '.';
1876 /* If requested, drop trailing zeros. Never crop past "1.0". */
1877 if (crop_trailing_zeros)
1878 while (last > first + 3 && last[-1] == '0')
1879 last--;
1881 /* Append the exponent. */
1882 sprintf (last, "e%+d", dec_exp);
1884 /* Verify that we can read the original value back in. */
1885 if (flag_checking && mode != VOIDmode)
1887 real_from_string (&r, str);
1888 real_convert (&r, mode, &r);
1889 gcc_assert (real_identical (&r, r_orig));
1893 /* Likewise, except always uses round-to-nearest. */
1895 void
1896 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1897 size_t digits, int crop_trailing_zeros)
1899 real_to_decimal_for_mode (str, r_orig, buf_size,
1900 digits, crop_trailing_zeros, VOIDmode);
1903 DEBUG_FUNCTION void
1904 debug (const REAL_VALUE_TYPE &r)
1906 char s[60];
1907 real_to_hexadecimal (s, &r, sizeof (s), 0, 1);
1908 fprintf (stderr, "%s\n", s);
1911 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1912 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1913 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1914 strip trailing zeros. */
1916 void
1917 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1918 size_t digits, int crop_trailing_zeros)
1920 int i, j, exp = REAL_EXP (r);
1921 char *p, *first;
1922 char exp_buf[16];
1923 size_t max_digits;
1925 switch (r->cl)
1927 case rvc_zero:
1928 exp = 0;
1929 break;
1930 case rvc_normal:
1931 break;
1932 case rvc_inf:
1933 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1934 return;
1935 case rvc_nan:
1936 /* ??? Print the significand as well, if not canonical? */
1937 sprintf (str, "%c%cNaN", (r->sign ? '-' : '+'),
1938 (r->signalling ? 'S' : 'Q'));
1939 return;
1940 default:
1941 gcc_unreachable ();
1944 if (r->decimal)
1946 /* Hexadecimal format for decimal floats is not interesting. */
1947 strcpy (str, "N/A");
1948 return;
1951 if (digits == 0)
1952 digits = SIGNIFICAND_BITS / 4;
1954 /* Bound the number of digits printed by the size of the output buffer. */
1956 sprintf (exp_buf, "p%+d", exp);
1957 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1958 gcc_assert (max_digits <= buf_size);
1959 if (digits > max_digits)
1960 digits = max_digits;
1962 p = str;
1963 if (r->sign)
1964 *p++ = '-';
1965 *p++ = '0';
1966 *p++ = 'x';
1967 *p++ = '0';
1968 *p++ = '.';
1969 first = p;
1971 for (i = SIGSZ - 1; i >= 0; --i)
1972 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1974 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1975 if (--digits == 0)
1976 goto out;
1979 out:
1980 if (crop_trailing_zeros)
1981 while (p > first + 1 && p[-1] == '0')
1982 p--;
1984 sprintf (p, "p%+d", exp);
1987 /* Initialize R from a decimal or hexadecimal string. The string is
1988 assumed to have been syntax checked already. Return -1 if the
1989 value underflows, +1 if overflows, and 0 otherwise. */
1992 real_from_string (REAL_VALUE_TYPE *r, const char *str)
1994 int exp = 0;
1995 bool sign = false;
1997 get_zero (r, 0);
1999 if (*str == '-')
2001 sign = true;
2002 str++;
2004 else if (*str == '+')
2005 str++;
2007 if (startswith (str, "QNaN"))
2009 get_canonical_qnan (r, sign);
2010 return 0;
2012 else if (startswith (str, "SNaN"))
2014 get_canonical_snan (r, sign);
2015 return 0;
2017 else if (startswith (str, "Inf"))
2019 get_inf (r, sign);
2020 return 0;
2023 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
2025 /* Hexadecimal floating point. */
2026 int pos = SIGNIFICAND_BITS - 4, d;
2028 str += 2;
2030 while (*str == '0')
2031 str++;
2032 while (1)
2034 d = hex_value (*str);
2035 if (d == _hex_bad)
2036 break;
2037 if (pos >= 0)
2039 r->sig[pos / HOST_BITS_PER_LONG]
2040 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
2041 pos -= 4;
2043 else if (d)
2044 /* Ensure correct rounding by setting last bit if there is
2045 a subsequent nonzero digit. */
2046 r->sig[0] |= 1;
2047 exp += 4;
2048 str++;
2050 if (*str == '.')
2052 str++;
2053 if (pos == SIGNIFICAND_BITS - 4)
2055 while (*str == '0')
2056 str++, exp -= 4;
2058 while (1)
2060 d = hex_value (*str);
2061 if (d == _hex_bad)
2062 break;
2063 if (pos >= 0)
2065 r->sig[pos / HOST_BITS_PER_LONG]
2066 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
2067 pos -= 4;
2069 else if (d)
2070 /* Ensure correct rounding by setting last bit if there is
2071 a subsequent nonzero digit. */
2072 r->sig[0] |= 1;
2073 str++;
2077 /* If the mantissa is zero, ignore the exponent. */
2078 if (!cmp_significand_0 (r))
2079 goto is_a_zero;
2081 if (*str == 'p' || *str == 'P')
2083 bool exp_neg = false;
2085 str++;
2086 if (*str == '-')
2088 exp_neg = true;
2089 str++;
2091 else if (*str == '+')
2092 str++;
2094 d = 0;
2095 while (ISDIGIT (*str))
2097 d *= 10;
2098 d += *str - '0';
2099 if (d > MAX_EXP)
2101 /* Overflowed the exponent. */
2102 if (exp_neg)
2103 goto underflow;
2104 else
2105 goto overflow;
2107 str++;
2109 if (exp_neg)
2110 d = -d;
2112 exp += d;
2115 r->cl = rvc_normal;
2116 SET_REAL_EXP (r, exp);
2118 normalize (r);
2120 else
2122 /* Decimal floating point. */
2123 const char *cstr = str;
2124 mpfr_t m;
2125 bool inexact;
2127 while (*cstr == '0')
2128 cstr++;
2129 if (*cstr == '.')
2131 cstr++;
2132 while (*cstr == '0')
2133 cstr++;
2136 /* If the mantissa is zero, ignore the exponent. */
2137 if (!ISDIGIT (*cstr))
2138 goto is_a_zero;
2140 /* Nonzero value, possibly overflowing or underflowing. */
2141 mpfr_init2 (m, SIGNIFICAND_BITS);
2142 inexact = mpfr_strtofr (m, str, NULL, 10, MPFR_RNDZ);
2143 /* The result should never be a NaN, and because the rounding is
2144 toward zero should never be an infinity. */
2145 gcc_assert (!mpfr_nan_p (m) && !mpfr_inf_p (m));
2146 if (mpfr_zero_p (m) || mpfr_get_exp (m) < -MAX_EXP + 4)
2148 mpfr_clear (m);
2149 goto underflow;
2151 else if (mpfr_get_exp (m) > MAX_EXP - 4)
2153 mpfr_clear (m);
2154 goto overflow;
2156 else
2158 real_from_mpfr (r, m, NULL_TREE, MPFR_RNDZ);
2159 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2160 because the hex digits used in real_from_mpfr did not
2161 start with a digit 8 to f, but the exponent bounds above
2162 should have avoided underflow or overflow. */
2163 gcc_assert (r->cl == rvc_normal);
2164 /* Set a sticky bit if mpfr_strtofr was inexact. */
2165 r->sig[0] |= inexact;
2166 mpfr_clear (m);
2170 r->sign = sign;
2171 return 0;
2173 is_a_zero:
2174 get_zero (r, sign);
2175 return 0;
2177 underflow:
2178 get_zero (r, sign);
2179 return -1;
2181 overflow:
2182 get_inf (r, sign);
2183 return 1;
2186 /* Legacy. Similar, but return the result directly. */
2188 REAL_VALUE_TYPE
2189 real_from_string2 (const char *s, format_helper fmt)
2191 REAL_VALUE_TYPE r;
2193 real_from_string (&r, s);
2194 if (fmt)
2195 real_convert (&r, fmt, &r);
2197 return r;
2200 /* Initialize R from string S and desired format FMT. */
2202 void
2203 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, format_helper fmt)
2205 if (fmt.decimal_p ())
2206 decimal_real_from_string (r, s);
2207 else
2208 real_from_string (r, s);
2210 if (fmt)
2211 real_convert (r, fmt, r);
2214 /* Initialize R from the wide_int VAL_IN. Round it to format FMT if
2215 FMT is nonnull. */
2217 void
2218 real_from_integer (REAL_VALUE_TYPE *r, format_helper fmt,
2219 const wide_int_ref &val_in, signop sgn)
2221 if (val_in == 0)
2222 get_zero (r, 0);
2223 else
2225 unsigned int len = val_in.get_precision ();
2226 int i, j, e = 0;
2227 int maxbitlen = MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT;
2228 const unsigned int realmax = (SIGNIFICAND_BITS / HOST_BITS_PER_WIDE_INT
2229 * HOST_BITS_PER_WIDE_INT);
2231 memset (r, 0, sizeof (*r));
2232 r->cl = rvc_normal;
2233 r->sign = wi::neg_p (val_in, sgn);
2235 /* We have to ensure we can negate the largest negative number. */
2236 wide_int val = wide_int::from (val_in, maxbitlen, sgn);
2238 if (r->sign)
2239 val = -val;
2241 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2242 won't work with precisions that are not a multiple of
2243 HOST_BITS_PER_WIDE_INT. */
2244 len += HOST_BITS_PER_WIDE_INT - 1;
2246 /* Ensure we can represent the largest negative number. */
2247 len += 1;
2249 len = len/HOST_BITS_PER_WIDE_INT * HOST_BITS_PER_WIDE_INT;
2251 /* Cap the size to the size allowed by real.h. */
2252 if (len > realmax)
2254 HOST_WIDE_INT cnt_l_z;
2255 cnt_l_z = wi::clz (val);
2257 if (maxbitlen - cnt_l_z > realmax)
2259 e = maxbitlen - cnt_l_z - realmax;
2261 /* This value is too large, we must shift it right to
2262 preserve all the bits we can, and then bump the
2263 exponent up by that amount. */
2264 val = wi::lrshift (val, e);
2266 len = realmax;
2269 /* Clear out top bits so elt will work with precisions that aren't
2270 a multiple of HOST_BITS_PER_WIDE_INT. */
2271 val = wide_int::from (val, len, sgn);
2272 len = len / HOST_BITS_PER_WIDE_INT;
2274 SET_REAL_EXP (r, len * HOST_BITS_PER_WIDE_INT + e);
2276 j = SIGSZ - 1;
2277 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2278 for (i = len - 1; i >= 0; i--)
2280 r->sig[j--] = val.elt (i);
2281 if (j < 0)
2282 break;
2284 else
2286 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2287 for (i = len - 1; i >= 0; i--)
2289 HOST_WIDE_INT e = val.elt (i);
2290 r->sig[j--] = e >> (HOST_BITS_PER_LONG - 1) >> 1;
2291 if (j < 0)
2292 break;
2293 r->sig[j--] = e;
2294 if (j < 0)
2295 break;
2299 normalize (r);
2302 if (fmt.decimal_p ())
2303 decimal_from_integer (r);
2304 if (fmt)
2305 real_convert (r, fmt, r);
2308 /* Render R, an integral value, as a floating point constant with no
2309 specified exponent. */
2311 static void
2312 decimal_integer_string (char *str, const REAL_VALUE_TYPE *r_orig,
2313 size_t buf_size)
2315 int dec_exp, digit, digits;
2316 REAL_VALUE_TYPE r, pten;
2317 char *p;
2318 bool sign;
2320 r = *r_orig;
2322 if (r.cl == rvc_zero)
2324 strcpy (str, "0.");
2325 return;
2328 sign = r.sign;
2329 r.sign = 0;
2331 dec_exp = REAL_EXP (&r) * M_LOG10_2;
2332 digits = dec_exp + 1;
2333 gcc_assert ((digits + 2) < (int)buf_size);
2335 pten = *real_digit (1);
2336 times_pten (&pten, dec_exp);
2338 p = str;
2339 if (sign)
2340 *p++ = '-';
2342 digit = rtd_divmod (&r, &pten);
2343 gcc_assert (digit >= 0 && digit <= 9);
2344 *p++ = digit + '0';
2345 while (--digits > 0)
2347 times_pten (&r, 1);
2348 digit = rtd_divmod (&r, &pten);
2349 *p++ = digit + '0';
2351 *p++ = '.';
2352 *p++ = '\0';
2355 /* Convert a real with an integral value to decimal float. */
2357 static void
2358 decimal_from_integer (REAL_VALUE_TYPE *r)
2360 char str[256];
2362 decimal_integer_string (str, r, sizeof (str) - 1);
2363 decimal_real_from_string (r, str);
2366 /* Returns 10**2**N. */
2368 static const REAL_VALUE_TYPE *
2369 ten_to_ptwo (int n)
2371 static REAL_VALUE_TYPE tens[EXP_BITS];
2373 gcc_assert (n >= 0);
2374 gcc_assert (n < EXP_BITS);
2376 if (tens[n].cl == rvc_zero)
2378 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2380 HOST_WIDE_INT t = 10;
2381 int i;
2383 for (i = 0; i < n; ++i)
2384 t *= t;
2386 real_from_integer (&tens[n], VOIDmode, t, UNSIGNED);
2388 else
2390 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2391 do_multiply (&tens[n], t, t);
2395 return &tens[n];
2398 /* Returns 10**(-2**N). */
2400 static const REAL_VALUE_TYPE *
2401 ten_to_mptwo (int n)
2403 static REAL_VALUE_TYPE tens[EXP_BITS];
2405 gcc_assert (n >= 0);
2406 gcc_assert (n < EXP_BITS);
2408 if (tens[n].cl == rvc_zero)
2409 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2411 return &tens[n];
2414 /* Returns N. */
2416 static const REAL_VALUE_TYPE *
2417 real_digit (int n)
2419 static REAL_VALUE_TYPE num[10];
2421 gcc_assert (n >= 0);
2422 gcc_assert (n <= 9);
2424 if (n > 0 && num[n].cl == rvc_zero)
2425 real_from_integer (&num[n], VOIDmode, n, UNSIGNED);
2427 return &num[n];
2430 /* Multiply R by 10**EXP. */
2432 static void
2433 times_pten (REAL_VALUE_TYPE *r, int exp)
2435 REAL_VALUE_TYPE pten, *rr;
2436 bool negative = (exp < 0);
2437 int i;
2439 if (negative)
2441 exp = -exp;
2442 pten = *real_digit (1);
2443 rr = &pten;
2445 else
2446 rr = r;
2448 for (i = 0; exp > 0; ++i, exp >>= 1)
2449 if (exp & 1)
2450 do_multiply (rr, rr, ten_to_ptwo (i));
2452 if (negative)
2453 do_divide (r, r, &pten);
2456 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2458 const REAL_VALUE_TYPE *
2459 dconst_e_ptr (void)
2461 static REAL_VALUE_TYPE value;
2463 /* Initialize mathematical constants for constant folding builtins.
2464 These constants need to be given to at least 160 bits precision. */
2465 if (value.cl == rvc_zero)
2467 mpfr_t m;
2468 mpfr_init2 (m, SIGNIFICAND_BITS);
2469 mpfr_set_ui (m, 1, MPFR_RNDN);
2470 mpfr_exp (m, m, MPFR_RNDN);
2471 real_from_mpfr (&value, m, NULL_TREE, MPFR_RNDN);
2472 mpfr_clear (m);
2475 return &value;
2478 /* Returns a cached REAL_VALUE_TYPE corresponding to 1/n, for various n. */
2480 #define CACHED_FRACTION(NAME, N) \
2481 const REAL_VALUE_TYPE * \
2482 NAME (void) \
2484 static REAL_VALUE_TYPE value; \
2486 /* Initialize mathematical constants for constant folding builtins. \
2487 These constants need to be given to at least 160 bits \
2488 precision. */ \
2489 if (value.cl == rvc_zero) \
2490 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (N)); \
2491 return &value; \
2494 CACHED_FRACTION (dconst_third_ptr, 3)
2495 CACHED_FRACTION (dconst_quarter_ptr, 4)
2496 CACHED_FRACTION (dconst_sixth_ptr, 6)
2497 CACHED_FRACTION (dconst_ninth_ptr, 9)
2499 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2501 const REAL_VALUE_TYPE *
2502 dconst_sqrt2_ptr (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 precision. */
2508 if (value.cl == rvc_zero)
2510 mpfr_t m;
2511 mpfr_init2 (m, SIGNIFICAND_BITS);
2512 mpfr_sqrt_ui (m, 2, MPFR_RNDN);
2513 real_from_mpfr (&value, m, NULL_TREE, MPFR_RNDN);
2514 mpfr_clear (m);
2516 return &value;
2519 /* Fills R with Inf with SIGN. */
2521 void
2522 real_inf (REAL_VALUE_TYPE *r, bool sign)
2524 get_inf (r, sign);
2527 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2528 we force a QNaN, else we force an SNaN. The string, if not empty,
2529 is parsed as a number and placed in the significand. Return true
2530 if the string was successfully parsed. */
2532 bool
2533 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2534 format_helper fmt)
2536 if (*str == 0)
2538 if (quiet)
2539 get_canonical_qnan (r, 0);
2540 else
2541 get_canonical_snan (r, 0);
2543 else
2545 int base = 10, d;
2547 memset (r, 0, sizeof (*r));
2548 r->cl = rvc_nan;
2550 /* Parse akin to strtol into the significand of R. */
2552 while (ISSPACE (*str))
2553 str++;
2554 if (*str == '-')
2555 str++;
2556 else if (*str == '+')
2557 str++;
2558 if (*str == '0')
2560 str++;
2561 if (*str == 'x' || *str == 'X')
2563 base = 16;
2564 str++;
2566 else
2567 base = 8;
2570 while ((d = hex_value (*str)) < base)
2572 REAL_VALUE_TYPE u;
2574 switch (base)
2576 case 8:
2577 lshift_significand (r, r, 3);
2578 break;
2579 case 16:
2580 lshift_significand (r, r, 4);
2581 break;
2582 case 10:
2583 lshift_significand_1 (&u, r);
2584 lshift_significand (r, r, 3);
2585 add_significands (r, r, &u);
2586 break;
2587 default:
2588 gcc_unreachable ();
2591 get_zero (&u, 0);
2592 u.sig[0] = d;
2593 add_significands (r, r, &u);
2595 str++;
2598 /* Must have consumed the entire string for success. */
2599 if (*str != 0)
2600 return false;
2602 /* Shift the significand into place such that the bits
2603 are in the most significant bits for the format. */
2604 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2606 /* Our MSB is always unset for NaNs. */
2607 r->sig[SIGSZ-1] &= ~SIG_MSB;
2609 /* Force quiet or signaling NaN. */
2610 r->signalling = !quiet;
2613 return true;
2616 /* Fills R with the largest finite value representable in mode MODE.
2617 If SIGN is nonzero, R is set to the most negative finite value. */
2619 void
2620 real_maxval (REAL_VALUE_TYPE *r, int sign, machine_mode mode)
2622 const struct real_format *fmt;
2623 int np2;
2625 fmt = REAL_MODE_FORMAT (mode);
2626 gcc_assert (fmt);
2627 memset (r, 0, sizeof (*r));
2629 if (fmt->b == 10)
2630 decimal_real_maxval (r, sign, mode);
2631 else
2633 r->cl = rvc_normal;
2634 r->sign = sign;
2635 SET_REAL_EXP (r, fmt->emax);
2637 np2 = SIGNIFICAND_BITS - fmt->p;
2638 memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2639 clear_significand_below (r, np2);
2641 if (fmt->pnan < fmt->p)
2642 /* This is an IBM extended double format made up of two IEEE
2643 doubles. The value of the long double is the sum of the
2644 values of the two parts. The most significant part is
2645 required to be the value of the long double rounded to the
2646 nearest double. Rounding means we need a slightly smaller
2647 value for LDBL_MAX. */
2648 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1);
2652 /* Fills R with 2**N. */
2654 void
2655 real_2expN (REAL_VALUE_TYPE *r, int n, format_helper fmt)
2657 memset (r, 0, sizeof (*r));
2659 n++;
2660 if (n > MAX_EXP)
2661 r->cl = rvc_inf;
2662 else if (n < -MAX_EXP)
2664 else
2666 r->cl = rvc_normal;
2667 SET_REAL_EXP (r, n);
2668 r->sig[SIGSZ-1] = SIG_MSB;
2670 if (fmt.decimal_p ())
2671 decimal_real_convert (r, fmt, r);
2675 static void
2676 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2678 int p2, np2, i, w;
2679 int emin2m1, emax2;
2680 bool round_up = false;
2682 if (r->decimal)
2684 if (fmt->b == 10)
2686 decimal_round_for_format (fmt, r);
2687 return;
2689 /* FIXME. We can come here via fp_easy_constant
2690 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2691 investigated whether this convert needs to be here, or
2692 something else is missing. */
2693 decimal_real_convert (r, REAL_MODE_FORMAT (DFmode), r);
2696 p2 = fmt->p;
2697 emin2m1 = fmt->emin - 1;
2698 emax2 = fmt->emax;
2700 np2 = SIGNIFICAND_BITS - p2;
2701 switch (r->cl)
2703 underflow:
2704 get_zero (r, r->sign);
2705 /* FALLTHRU */
2706 case rvc_zero:
2707 if (!fmt->has_signed_zero)
2708 r->sign = 0;
2709 return;
2711 overflow:
2712 get_inf (r, r->sign);
2713 case rvc_inf:
2714 return;
2716 case rvc_nan:
2717 clear_significand_below (r, np2);
2718 return;
2720 case rvc_normal:
2721 break;
2723 default:
2724 gcc_unreachable ();
2727 /* Check the range of the exponent. If we're out of range,
2728 either underflow or overflow. */
2729 if (REAL_EXP (r) > emax2)
2730 goto overflow;
2731 else if (REAL_EXP (r) <= emin2m1)
2733 int diff;
2735 if (!fmt->has_denorm)
2737 /* Don't underflow completely until we've had a chance to round. */
2738 if (REAL_EXP (r) < emin2m1)
2739 goto underflow;
2741 else
2743 diff = emin2m1 - REAL_EXP (r) + 1;
2744 if (diff > p2)
2745 goto underflow;
2747 /* De-normalize the significand. */
2748 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2749 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2753 if (!fmt->round_towards_zero)
2755 /* There are P2 true significand bits, followed by one guard bit,
2756 followed by one sticky bit, followed by stuff. Fold nonzero
2757 stuff into the sticky bit. */
2758 unsigned long sticky;
2759 bool guard, lsb;
2761 sticky = 0;
2762 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2763 sticky |= r->sig[i];
2764 sticky |= r->sig[w]
2765 & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2767 guard = test_significand_bit (r, np2 - 1);
2768 lsb = test_significand_bit (r, np2);
2770 /* Round to even. */
2771 round_up = guard && (sticky || lsb);
2774 if (round_up)
2776 REAL_VALUE_TYPE u;
2777 get_zero (&u, 0);
2778 set_significand_bit (&u, np2);
2780 if (add_significands (r, r, &u))
2782 /* Overflow. Means the significand had been all ones, and
2783 is now all zeros. Need to increase the exponent, and
2784 possibly re-normalize it. */
2785 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2786 if (REAL_EXP (r) > emax2)
2787 goto overflow;
2788 r->sig[SIGSZ-1] = SIG_MSB;
2792 /* Catch underflow that we deferred until after rounding. */
2793 if (REAL_EXP (r) <= emin2m1)
2794 goto underflow;
2796 /* Clear out trailing garbage. */
2797 clear_significand_below (r, np2);
2800 /* Extend or truncate to a new format. */
2802 void
2803 real_convert (REAL_VALUE_TYPE *r, format_helper fmt,
2804 const REAL_VALUE_TYPE *a)
2806 *r = *a;
2808 if (a->decimal || fmt->b == 10)
2809 decimal_real_convert (r, fmt, a);
2811 round_for_format (fmt, r);
2813 /* Make resulting NaN value to be qNaN. The caller has the
2814 responsibility to avoid the operation if flag_signaling_nans
2815 is on. */
2816 if (r->cl == rvc_nan)
2817 r->signalling = 0;
2819 /* round_for_format de-normalizes denormals. Undo just that part. */
2820 if (r->cl == rvc_normal)
2821 normalize (r);
2824 /* Legacy. Likewise, except return the struct directly. */
2826 REAL_VALUE_TYPE
2827 real_value_truncate (format_helper fmt, REAL_VALUE_TYPE a)
2829 REAL_VALUE_TYPE r;
2830 real_convert (&r, fmt, &a);
2831 return r;
2834 /* Return true if truncating to FMT is exact. */
2836 bool
2837 exact_real_truncate (format_helper fmt, const REAL_VALUE_TYPE *a)
2839 REAL_VALUE_TYPE t;
2840 int emin2m1;
2842 /* Don't allow conversion to denormals. */
2843 emin2m1 = fmt->emin - 1;
2844 if (REAL_EXP (a) <= emin2m1)
2845 return false;
2847 /* After conversion to the new format, the value must be identical. */
2848 real_convert (&t, fmt, a);
2849 return real_identical (&t, a);
2852 /* Write R to the given target format. Place the words of the result
2853 in target word order in BUF. There are always 32 bits in each
2854 long, no matter the size of the host long.
2856 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2858 long
2859 real_to_target (long *buf, const REAL_VALUE_TYPE *r_orig,
2860 format_helper fmt)
2862 REAL_VALUE_TYPE r;
2863 long buf1;
2865 r = *r_orig;
2866 round_for_format (fmt, &r);
2868 if (!buf)
2869 buf = &buf1;
2870 (*fmt->encode) (fmt, buf, &r);
2872 return *buf;
2875 /* Read R from the given target format. Read the words of the result
2876 in target word order in BUF. There are always 32 bits in each
2877 long, no matter the size of the host long. */
2879 void
2880 real_from_target (REAL_VALUE_TYPE *r, const long *buf, format_helper fmt)
2882 (*fmt->decode) (fmt, r, buf);
2885 /* Return the number of bits of the largest binary value that the
2886 significand of FMT will hold. */
2887 /* ??? Legacy. Should get access to real_format directly. */
2890 significand_size (format_helper fmt)
2892 if (fmt == NULL)
2893 return 0;
2895 if (fmt->b == 10)
2897 /* Return the size in bits of the largest binary value that can be
2898 held by the decimal coefficient for this format. This is one more
2899 than the number of bits required to hold the largest coefficient
2900 of this format. */
2901 double log2_10 = 3.3219281;
2902 return fmt->p * log2_10;
2904 return fmt->p;
2907 /* Return a hash value for the given real value. */
2908 /* ??? The "unsigned int" return value is intended to be hashval_t,
2909 but I didn't want to pull hashtab.h into real.h. */
2911 unsigned int
2912 real_hash (const REAL_VALUE_TYPE *r)
2914 unsigned int h;
2915 size_t i;
2917 h = r->cl | (r->sign << 2);
2918 switch (r->cl)
2920 case rvc_zero:
2921 case rvc_inf:
2922 return h;
2924 case rvc_normal:
2925 h |= (unsigned int)REAL_EXP (r) << 3;
2926 break;
2928 case rvc_nan:
2929 if (r->signalling)
2930 h ^= (unsigned int)-1;
2931 if (r->canonical)
2932 return h;
2933 break;
2935 default:
2936 gcc_unreachable ();
2939 if (sizeof (unsigned long) > sizeof (unsigned int))
2940 for (i = 0; i < SIGSZ; ++i)
2942 unsigned long s = r->sig[i];
2943 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2945 else
2946 for (i = 0; i < SIGSZ; ++i)
2947 h ^= r->sig[i];
2949 return h;
2952 /* IEEE single-precision format. */
2954 static void encode_ieee_single (const struct real_format *fmt,
2955 long *, const REAL_VALUE_TYPE *);
2956 static void decode_ieee_single (const struct real_format *,
2957 REAL_VALUE_TYPE *, const long *);
2959 static void
2960 encode_ieee_single (const struct real_format *fmt, long *buf,
2961 const REAL_VALUE_TYPE *r)
2963 unsigned long image, sig, exp;
2964 unsigned long sign = r->sign;
2965 bool denormal = real_isdenormal (r);
2967 image = sign << 31;
2968 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2970 switch (r->cl)
2972 case rvc_zero:
2973 break;
2975 case rvc_inf:
2976 if (fmt->has_inf)
2977 image |= 255 << 23;
2978 else
2979 image |= 0x7fffffff;
2980 break;
2982 case rvc_nan:
2983 if (fmt->has_nans)
2985 if (r->canonical)
2986 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
2987 if (r->signalling == fmt->qnan_msb_set)
2988 sig &= ~(1 << 22);
2989 else
2990 sig |= 1 << 22;
2991 if (sig == 0)
2992 sig = 1 << 21;
2994 image |= 255 << 23;
2995 image |= sig;
2997 else
2998 image |= 0x7fffffff;
2999 break;
3001 case rvc_normal:
3002 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3003 whereas the intermediate representation is 0.F x 2**exp.
3004 Which means we're off by one. */
3005 if (denormal)
3006 exp = 0;
3007 else
3008 exp = REAL_EXP (r) + 127 - 1;
3009 image |= exp << 23;
3010 image |= sig;
3011 break;
3013 default:
3014 gcc_unreachable ();
3017 buf[0] = image;
3020 static void
3021 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3022 const long *buf)
3024 unsigned long image = buf[0] & 0xffffffff;
3025 bool sign = (image >> 31) & 1;
3026 int exp = (image >> 23) & 0xff;
3028 memset (r, 0, sizeof (*r));
3029 image <<= HOST_BITS_PER_LONG - 24;
3030 image &= ~SIG_MSB;
3032 if (exp == 0)
3034 if (image && fmt->has_denorm)
3036 r->cl = rvc_normal;
3037 r->sign = sign;
3038 SET_REAL_EXP (r, -126);
3039 r->sig[SIGSZ-1] = image << 1;
3040 normalize (r);
3042 else if (fmt->has_signed_zero)
3043 r->sign = sign;
3045 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
3047 if (image)
3049 r->cl = rvc_nan;
3050 r->sign = sign;
3051 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
3052 ^ fmt->qnan_msb_set);
3053 r->sig[SIGSZ-1] = image;
3055 else
3057 r->cl = rvc_inf;
3058 r->sign = sign;
3061 else
3063 r->cl = rvc_normal;
3064 r->sign = sign;
3065 SET_REAL_EXP (r, exp - 127 + 1);
3066 r->sig[SIGSZ-1] = image | SIG_MSB;
3070 const struct real_format ieee_single_format =
3072 encode_ieee_single,
3073 decode_ieee_single,
3077 -125,
3078 128,
3082 false,
3083 true,
3084 true,
3085 true,
3086 true,
3087 true,
3088 true,
3089 false,
3090 "ieee_single"
3093 const struct real_format mips_single_format =
3095 encode_ieee_single,
3096 decode_ieee_single,
3100 -125,
3101 128,
3105 false,
3106 true,
3107 true,
3108 true,
3109 true,
3110 true,
3111 false,
3112 true,
3113 "mips_single"
3116 const struct real_format motorola_single_format =
3118 encode_ieee_single,
3119 decode_ieee_single,
3123 -125,
3124 128,
3128 false,
3129 true,
3130 true,
3131 true,
3132 true,
3133 true,
3134 true,
3135 true,
3136 "motorola_single"
3139 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3140 single precision with the following differences:
3141 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3142 are generated.
3143 - NaNs are not supported.
3144 - The range of non-zero numbers in binary is
3145 (001)[1.]000...000 to (255)[1.]111...111.
3146 - Denormals can be represented, but are treated as +0.0 when
3147 used as an operand and are never generated as a result.
3148 - -0.0 can be represented, but a zero result is always +0.0.
3149 - the only supported rounding mode is trunction (towards zero). */
3150 const struct real_format spu_single_format =
3152 encode_ieee_single,
3153 decode_ieee_single,
3157 -125,
3158 129,
3162 true,
3163 false,
3164 false,
3165 false,
3166 true,
3167 true,
3168 false,
3169 false,
3170 "spu_single"
3173 /* IEEE double-precision format. */
3175 static void encode_ieee_double (const struct real_format *fmt,
3176 long *, const REAL_VALUE_TYPE *);
3177 static void decode_ieee_double (const struct real_format *,
3178 REAL_VALUE_TYPE *, const long *);
3180 static void
3181 encode_ieee_double (const struct real_format *fmt, long *buf,
3182 const REAL_VALUE_TYPE *r)
3184 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
3185 unsigned long sign = r->sign;
3186 bool denormal = real_isdenormal (r);
3188 image_hi = sign << 31;
3189 image_lo = 0;
3191 if (HOST_BITS_PER_LONG == 64)
3193 sig_hi = r->sig[SIGSZ-1];
3194 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
3195 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
3197 else
3199 sig_hi = r->sig[SIGSZ-1];
3200 sig_lo = r->sig[SIGSZ-2];
3201 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
3202 sig_hi = (sig_hi >> 11) & 0xfffff;
3205 switch (r->cl)
3207 case rvc_zero:
3208 break;
3210 case rvc_inf:
3211 if (fmt->has_inf)
3212 image_hi |= 2047 << 20;
3213 else
3215 image_hi |= 0x7fffffff;
3216 image_lo = 0xffffffff;
3218 break;
3220 case rvc_nan:
3221 if (fmt->has_nans)
3223 if (r->canonical)
3225 if (fmt->canonical_nan_lsbs_set)
3227 sig_hi = (1 << 19) - 1;
3228 sig_lo = 0xffffffff;
3230 else
3232 sig_hi = 0;
3233 sig_lo = 0;
3236 if (r->signalling == fmt->qnan_msb_set)
3237 sig_hi &= ~(1 << 19);
3238 else
3239 sig_hi |= 1 << 19;
3240 if (sig_hi == 0 && sig_lo == 0)
3241 sig_hi = 1 << 18;
3243 image_hi |= 2047 << 20;
3244 image_hi |= sig_hi;
3245 image_lo = sig_lo;
3247 else
3249 image_hi |= 0x7fffffff;
3250 image_lo = 0xffffffff;
3252 break;
3254 case rvc_normal:
3255 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3256 whereas the intermediate representation is 0.F x 2**exp.
3257 Which means we're off by one. */
3258 if (denormal)
3259 exp = 0;
3260 else
3261 exp = REAL_EXP (r) + 1023 - 1;
3262 image_hi |= exp << 20;
3263 image_hi |= sig_hi;
3264 image_lo = sig_lo;
3265 break;
3267 default:
3268 gcc_unreachable ();
3271 if (FLOAT_WORDS_BIG_ENDIAN)
3272 buf[0] = image_hi, buf[1] = image_lo;
3273 else
3274 buf[0] = image_lo, buf[1] = image_hi;
3277 static void
3278 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3279 const long *buf)
3281 unsigned long image_hi, image_lo;
3282 bool sign;
3283 int exp;
3285 if (FLOAT_WORDS_BIG_ENDIAN)
3286 image_hi = buf[0], image_lo = buf[1];
3287 else
3288 image_lo = buf[0], image_hi = buf[1];
3289 image_lo &= 0xffffffff;
3290 image_hi &= 0xffffffff;
3292 sign = (image_hi >> 31) & 1;
3293 exp = (image_hi >> 20) & 0x7ff;
3295 memset (r, 0, sizeof (*r));
3297 image_hi <<= 32 - 21;
3298 image_hi |= image_lo >> 21;
3299 image_hi &= 0x7fffffff;
3300 image_lo <<= 32 - 21;
3302 if (exp == 0)
3304 if ((image_hi || image_lo) && fmt->has_denorm)
3306 r->cl = rvc_normal;
3307 r->sign = sign;
3308 SET_REAL_EXP (r, -1022);
3309 if (HOST_BITS_PER_LONG == 32)
3311 image_hi = (image_hi << 1) | (image_lo >> 31);
3312 image_lo <<= 1;
3313 r->sig[SIGSZ-1] = image_hi;
3314 r->sig[SIGSZ-2] = image_lo;
3316 else
3318 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
3319 r->sig[SIGSZ-1] = image_hi;
3321 normalize (r);
3323 else if (fmt->has_signed_zero)
3324 r->sign = sign;
3326 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
3328 if (image_hi || image_lo)
3330 r->cl = rvc_nan;
3331 r->sign = sign;
3332 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3333 if (HOST_BITS_PER_LONG == 32)
3335 r->sig[SIGSZ-1] = image_hi;
3336 r->sig[SIGSZ-2] = image_lo;
3338 else
3339 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3341 else
3343 r->cl = rvc_inf;
3344 r->sign = sign;
3347 else
3349 r->cl = rvc_normal;
3350 r->sign = sign;
3351 SET_REAL_EXP (r, exp - 1023 + 1);
3352 if (HOST_BITS_PER_LONG == 32)
3354 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3355 r->sig[SIGSZ-2] = image_lo;
3357 else
3358 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3362 const struct real_format ieee_double_format =
3364 encode_ieee_double,
3365 decode_ieee_double,
3369 -1021,
3370 1024,
3374 false,
3375 true,
3376 true,
3377 true,
3378 true,
3379 true,
3380 true,
3381 false,
3382 "ieee_double"
3385 const struct real_format mips_double_format =
3387 encode_ieee_double,
3388 decode_ieee_double,
3392 -1021,
3393 1024,
3397 false,
3398 true,
3399 true,
3400 true,
3401 true,
3402 true,
3403 false,
3404 true,
3405 "mips_double"
3408 const struct real_format motorola_double_format =
3410 encode_ieee_double,
3411 decode_ieee_double,
3415 -1021,
3416 1024,
3420 false,
3421 true,
3422 true,
3423 true,
3424 true,
3425 true,
3426 true,
3427 true,
3428 "motorola_double"
3431 /* IEEE extended real format. This comes in three flavors: Intel's as
3432 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3433 12- and 16-byte images may be big- or little endian; Motorola's is
3434 always big endian. */
3436 /* Helper subroutine which converts from the internal format to the
3437 12-byte little-endian Intel format. Functions below adjust this
3438 for the other possible formats. */
3439 static void
3440 encode_ieee_extended (const struct real_format *fmt, long *buf,
3441 const REAL_VALUE_TYPE *r)
3443 unsigned long image_hi, sig_hi, sig_lo;
3444 bool denormal = real_isdenormal (r);
3446 image_hi = r->sign << 15;
3447 sig_hi = sig_lo = 0;
3449 switch (r->cl)
3451 case rvc_zero:
3452 break;
3454 case rvc_inf:
3455 if (fmt->has_inf)
3457 image_hi |= 32767;
3459 /* Intel requires the explicit integer bit to be set, otherwise
3460 it considers the value a "pseudo-infinity". Motorola docs
3461 say it doesn't care. */
3462 sig_hi = 0x80000000;
3464 else
3466 image_hi |= 32767;
3467 sig_lo = sig_hi = 0xffffffff;
3469 break;
3471 case rvc_nan:
3472 if (fmt->has_nans)
3474 image_hi |= 32767;
3475 if (r->canonical)
3477 if (fmt->canonical_nan_lsbs_set)
3479 sig_hi = (1 << 30) - 1;
3480 sig_lo = 0xffffffff;
3483 else if (HOST_BITS_PER_LONG == 32)
3485 sig_hi = r->sig[SIGSZ-1];
3486 sig_lo = r->sig[SIGSZ-2];
3488 else
3490 sig_lo = r->sig[SIGSZ-1];
3491 sig_hi = sig_lo >> 31 >> 1;
3492 sig_lo &= 0xffffffff;
3494 if (r->signalling == fmt->qnan_msb_set)
3495 sig_hi &= ~(1 << 30);
3496 else
3497 sig_hi |= 1 << 30;
3498 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3499 sig_hi = 1 << 29;
3501 /* Intel requires the explicit integer bit to be set, otherwise
3502 it considers the value a "pseudo-nan". Motorola docs say it
3503 doesn't care. */
3504 sig_hi |= 0x80000000;
3506 else
3508 image_hi |= 32767;
3509 sig_lo = sig_hi = 0xffffffff;
3511 break;
3513 case rvc_normal:
3515 int exp = REAL_EXP (r);
3517 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3518 whereas the intermediate representation is 0.F x 2**exp.
3519 Which means we're off by one.
3521 Except for Motorola, which consider exp=0 and explicit
3522 integer bit set to continue to be normalized. In theory
3523 this discrepancy has been taken care of by the difference
3524 in fmt->emin in round_for_format. */
3526 if (denormal)
3527 exp = 0;
3528 else
3530 exp += 16383 - 1;
3531 gcc_assert (exp >= 0);
3533 image_hi |= exp;
3535 if (HOST_BITS_PER_LONG == 32)
3537 sig_hi = r->sig[SIGSZ-1];
3538 sig_lo = r->sig[SIGSZ-2];
3540 else
3542 sig_lo = r->sig[SIGSZ-1];
3543 sig_hi = sig_lo >> 31 >> 1;
3544 sig_lo &= 0xffffffff;
3547 break;
3549 default:
3550 gcc_unreachable ();
3553 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3556 /* Convert from the internal format to the 12-byte Motorola format
3557 for an IEEE extended real. */
3558 static void
3559 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3560 const REAL_VALUE_TYPE *r)
3562 long intermed[3];
3563 encode_ieee_extended (fmt, intermed, r);
3565 if (r->cl == rvc_inf)
3566 /* For infinity clear the explicit integer bit again, so that the
3567 format matches the canonical infinity generated by the FPU. */
3568 intermed[1] = 0;
3570 /* Motorola chips are assumed always to be big-endian. Also, the
3571 padding in a Motorola extended real goes between the exponent and
3572 the mantissa. At this point the mantissa is entirely within
3573 elements 0 and 1 of intermed, and the exponent entirely within
3574 element 2, so all we have to do is swap the order around, and
3575 shift element 2 left 16 bits. */
3576 buf[0] = intermed[2] << 16;
3577 buf[1] = intermed[1];
3578 buf[2] = intermed[0];
3581 /* Convert from the internal format to the 12-byte Intel format for
3582 an IEEE extended real. */
3583 static void
3584 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3585 const REAL_VALUE_TYPE *r)
3587 if (FLOAT_WORDS_BIG_ENDIAN)
3589 /* All the padding in an Intel-format extended real goes at the high
3590 end, which in this case is after the mantissa, not the exponent.
3591 Therefore we must shift everything down 16 bits. */
3592 long intermed[3];
3593 encode_ieee_extended (fmt, intermed, r);
3594 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3595 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3596 buf[2] = (intermed[0] << 16);
3598 else
3599 /* encode_ieee_extended produces what we want directly. */
3600 encode_ieee_extended (fmt, buf, r);
3603 /* Convert from the internal format to the 16-byte Intel format for
3604 an IEEE extended real. */
3605 static void
3606 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3607 const REAL_VALUE_TYPE *r)
3609 /* All the padding in an Intel-format extended real goes at the high end. */
3610 encode_ieee_extended_intel_96 (fmt, buf, r);
3611 buf[3] = 0;
3614 /* As above, we have a helper function which converts from 12-byte
3615 little-endian Intel format to internal format. Functions below
3616 adjust for the other possible formats. */
3617 static void
3618 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3619 const long *buf)
3621 unsigned long image_hi, sig_hi, sig_lo;
3622 bool sign;
3623 int exp;
3625 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3626 sig_lo &= 0xffffffff;
3627 sig_hi &= 0xffffffff;
3628 image_hi &= 0xffffffff;
3630 sign = (image_hi >> 15) & 1;
3631 exp = image_hi & 0x7fff;
3633 memset (r, 0, sizeof (*r));
3635 if (exp == 0)
3637 if ((sig_hi || sig_lo) && fmt->has_denorm)
3639 r->cl = rvc_normal;
3640 r->sign = sign;
3642 /* When the IEEE format contains a hidden bit, we know that
3643 it's zero at this point, and so shift up the significand
3644 and decrease the exponent to match. In this case, Motorola
3645 defines the explicit integer bit to be valid, so we don't
3646 know whether the msb is set or not. */
3647 SET_REAL_EXP (r, fmt->emin);
3648 if (HOST_BITS_PER_LONG == 32)
3650 r->sig[SIGSZ-1] = sig_hi;
3651 r->sig[SIGSZ-2] = sig_lo;
3653 else
3654 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3656 normalize (r);
3658 else if (fmt->has_signed_zero)
3659 r->sign = sign;
3661 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3663 /* See above re "pseudo-infinities" and "pseudo-nans".
3664 Short summary is that the MSB will likely always be
3665 set, and that we don't care about it. */
3666 sig_hi &= 0x7fffffff;
3668 if (sig_hi || sig_lo)
3670 r->cl = rvc_nan;
3671 r->sign = sign;
3672 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3673 if (HOST_BITS_PER_LONG == 32)
3675 r->sig[SIGSZ-1] = sig_hi;
3676 r->sig[SIGSZ-2] = sig_lo;
3678 else
3679 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3681 else
3683 r->cl = rvc_inf;
3684 r->sign = sign;
3687 else
3689 r->cl = rvc_normal;
3690 r->sign = sign;
3691 SET_REAL_EXP (r, exp - 16383 + 1);
3692 if (HOST_BITS_PER_LONG == 32)
3694 r->sig[SIGSZ-1] = sig_hi;
3695 r->sig[SIGSZ-2] = sig_lo;
3697 else
3698 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3702 /* Convert from the internal format to the 12-byte Motorola format
3703 for an IEEE extended real. */
3704 static void
3705 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3706 const long *buf)
3708 long intermed[3];
3710 /* Motorola chips are assumed always to be big-endian. Also, the
3711 padding in a Motorola extended real goes between the exponent and
3712 the mantissa; remove it. */
3713 intermed[0] = buf[2];
3714 intermed[1] = buf[1];
3715 intermed[2] = (unsigned long)buf[0] >> 16;
3717 decode_ieee_extended (fmt, r, intermed);
3720 /* Convert from the internal format to the 12-byte Intel format for
3721 an IEEE extended real. */
3722 static void
3723 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3724 const long *buf)
3726 if (FLOAT_WORDS_BIG_ENDIAN)
3728 /* All the padding in an Intel-format extended real goes at the high
3729 end, which in this case is after the mantissa, not the exponent.
3730 Therefore we must shift everything up 16 bits. */
3731 long intermed[3];
3733 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3734 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3735 intermed[2] = ((unsigned long)buf[0] >> 16);
3737 decode_ieee_extended (fmt, r, intermed);
3739 else
3740 /* decode_ieee_extended produces what we want directly. */
3741 decode_ieee_extended (fmt, r, buf);
3744 /* Convert from the internal format to the 16-byte Intel format for
3745 an IEEE extended real. */
3746 static void
3747 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3748 const long *buf)
3750 /* All the padding in an Intel-format extended real goes at the high end. */
3751 decode_ieee_extended_intel_96 (fmt, r, buf);
3754 const struct real_format ieee_extended_motorola_format =
3756 encode_ieee_extended_motorola,
3757 decode_ieee_extended_motorola,
3761 -16382,
3762 16384,
3766 false,
3767 true,
3768 true,
3769 true,
3770 true,
3771 true,
3772 true,
3773 true,
3774 "ieee_extended_motorola"
3777 const struct real_format ieee_extended_intel_96_format =
3779 encode_ieee_extended_intel_96,
3780 decode_ieee_extended_intel_96,
3784 -16381,
3785 16384,
3789 false,
3790 true,
3791 true,
3792 true,
3793 true,
3794 true,
3795 true,
3796 false,
3797 "ieee_extended_intel_96"
3800 const struct real_format ieee_extended_intel_128_format =
3802 encode_ieee_extended_intel_128,
3803 decode_ieee_extended_intel_128,
3807 -16381,
3808 16384,
3812 false,
3813 true,
3814 true,
3815 true,
3816 true,
3817 true,
3818 true,
3819 false,
3820 "ieee_extended_intel_128"
3823 /* The following caters to i386 systems that set the rounding precision
3824 to 53 bits instead of 64, e.g. FreeBSD. */
3825 const struct real_format ieee_extended_intel_96_round_53_format =
3827 encode_ieee_extended_intel_96,
3828 decode_ieee_extended_intel_96,
3832 -16381,
3833 16384,
3837 false,
3838 true,
3839 true,
3840 true,
3841 true,
3842 true,
3843 true,
3844 false,
3845 "ieee_extended_intel_96_round_53"
3848 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3849 numbers whose sum is equal to the extended precision value. The number
3850 with greater magnitude is first. This format has the same magnitude
3851 range as an IEEE double precision value, but effectively 106 bits of
3852 significand precision. Infinity and NaN are represented by their IEEE
3853 double precision value stored in the first number, the second number is
3854 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3856 static void encode_ibm_extended (const struct real_format *fmt,
3857 long *, const REAL_VALUE_TYPE *);
3858 static void decode_ibm_extended (const struct real_format *,
3859 REAL_VALUE_TYPE *, const long *);
3861 static void
3862 encode_ibm_extended (const struct real_format *fmt, long *buf,
3863 const REAL_VALUE_TYPE *r)
3865 REAL_VALUE_TYPE u, normr, v;
3866 const struct real_format *base_fmt;
3868 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3870 /* Renormalize R before doing any arithmetic on it. */
3871 normr = *r;
3872 if (normr.cl == rvc_normal)
3873 normalize (&normr);
3875 /* u = IEEE double precision portion of significand. */
3876 u = normr;
3877 round_for_format (base_fmt, &u);
3878 encode_ieee_double (base_fmt, &buf[0], &u);
3880 if (u.cl == rvc_normal)
3882 do_add (&v, &normr, &u, 1);
3883 /* Call round_for_format since we might need to denormalize. */
3884 round_for_format (base_fmt, &v);
3885 encode_ieee_double (base_fmt, &buf[2], &v);
3887 else
3889 /* Inf, NaN, 0 are all representable as doubles, so the
3890 least-significant part can be 0.0. */
3891 buf[2] = 0;
3892 buf[3] = 0;
3896 static void
3897 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3898 const long *buf)
3900 REAL_VALUE_TYPE u, v;
3901 const struct real_format *base_fmt;
3903 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3904 decode_ieee_double (base_fmt, &u, &buf[0]);
3906 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3908 decode_ieee_double (base_fmt, &v, &buf[2]);
3909 do_add (r, &u, &v, 0);
3911 else
3912 *r = u;
3915 const struct real_format ibm_extended_format =
3917 encode_ibm_extended,
3918 decode_ibm_extended,
3920 53 + 53,
3922 -1021 + 53,
3923 1024,
3924 127,
3927 false,
3928 true,
3929 true,
3930 true,
3931 true,
3932 true,
3933 true,
3934 false,
3935 "ibm_extended"
3938 const struct real_format mips_extended_format =
3940 encode_ibm_extended,
3941 decode_ibm_extended,
3943 53 + 53,
3945 -1021 + 53,
3946 1024,
3947 127,
3950 false,
3951 true,
3952 true,
3953 true,
3954 true,
3955 true,
3956 false,
3957 true,
3958 "mips_extended"
3962 /* IEEE quad precision format. */
3964 static void encode_ieee_quad (const struct real_format *fmt,
3965 long *, const REAL_VALUE_TYPE *);
3966 static void decode_ieee_quad (const struct real_format *,
3967 REAL_VALUE_TYPE *, const long *);
3969 static void
3970 encode_ieee_quad (const struct real_format *fmt, long *buf,
3971 const REAL_VALUE_TYPE *r)
3973 unsigned long image3, image2, image1, image0, exp;
3974 unsigned long sign = r->sign;
3975 bool denormal = real_isdenormal (r);
3976 REAL_VALUE_TYPE u;
3978 image3 = sign << 31;
3979 image2 = 0;
3980 image1 = 0;
3981 image0 = 0;
3983 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3985 switch (r->cl)
3987 case rvc_zero:
3988 break;
3990 case rvc_inf:
3991 if (fmt->has_inf)
3992 image3 |= 32767 << 16;
3993 else
3995 image3 |= 0x7fffffff;
3996 image2 = 0xffffffff;
3997 image1 = 0xffffffff;
3998 image0 = 0xffffffff;
4000 break;
4002 case rvc_nan:
4003 if (fmt->has_nans)
4005 image3 |= 32767 << 16;
4007 if (r->canonical)
4009 if (fmt->canonical_nan_lsbs_set)
4011 image3 |= 0x7fff;
4012 image2 = image1 = image0 = 0xffffffff;
4015 else if (HOST_BITS_PER_LONG == 32)
4017 image0 = u.sig[0];
4018 image1 = u.sig[1];
4019 image2 = u.sig[2];
4020 image3 |= u.sig[3] & 0xffff;
4022 else
4024 image0 = u.sig[0];
4025 image1 = image0 >> 31 >> 1;
4026 image2 = u.sig[1];
4027 image3 |= (image2 >> 31 >> 1) & 0xffff;
4028 image0 &= 0xffffffff;
4029 image2 &= 0xffffffff;
4031 if (r->signalling == fmt->qnan_msb_set)
4032 image3 &= ~0x8000;
4033 else
4034 image3 |= 0x8000;
4035 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
4036 image3 |= 0x4000;
4038 else
4040 image3 |= 0x7fffffff;
4041 image2 = 0xffffffff;
4042 image1 = 0xffffffff;
4043 image0 = 0xffffffff;
4045 break;
4047 case rvc_normal:
4048 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4049 whereas the intermediate representation is 0.F x 2**exp.
4050 Which means we're off by one. */
4051 if (denormal)
4052 exp = 0;
4053 else
4054 exp = REAL_EXP (r) + 16383 - 1;
4055 image3 |= exp << 16;
4057 if (HOST_BITS_PER_LONG == 32)
4059 image0 = u.sig[0];
4060 image1 = u.sig[1];
4061 image2 = u.sig[2];
4062 image3 |= u.sig[3] & 0xffff;
4064 else
4066 image0 = u.sig[0];
4067 image1 = image0 >> 31 >> 1;
4068 image2 = u.sig[1];
4069 image3 |= (image2 >> 31 >> 1) & 0xffff;
4070 image0 &= 0xffffffff;
4071 image2 &= 0xffffffff;
4073 break;
4075 default:
4076 gcc_unreachable ();
4079 if (FLOAT_WORDS_BIG_ENDIAN)
4081 buf[0] = image3;
4082 buf[1] = image2;
4083 buf[2] = image1;
4084 buf[3] = image0;
4086 else
4088 buf[0] = image0;
4089 buf[1] = image1;
4090 buf[2] = image2;
4091 buf[3] = image3;
4095 static void
4096 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4097 const long *buf)
4099 unsigned long image3, image2, image1, image0;
4100 bool sign;
4101 int exp;
4103 if (FLOAT_WORDS_BIG_ENDIAN)
4105 image3 = buf[0];
4106 image2 = buf[1];
4107 image1 = buf[2];
4108 image0 = buf[3];
4110 else
4112 image0 = buf[0];
4113 image1 = buf[1];
4114 image2 = buf[2];
4115 image3 = buf[3];
4117 image0 &= 0xffffffff;
4118 image1 &= 0xffffffff;
4119 image2 &= 0xffffffff;
4121 sign = (image3 >> 31) & 1;
4122 exp = (image3 >> 16) & 0x7fff;
4123 image3 &= 0xffff;
4125 memset (r, 0, sizeof (*r));
4127 if (exp == 0)
4129 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
4131 r->cl = rvc_normal;
4132 r->sign = sign;
4134 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
4135 if (HOST_BITS_PER_LONG == 32)
4137 r->sig[0] = image0;
4138 r->sig[1] = image1;
4139 r->sig[2] = image2;
4140 r->sig[3] = image3;
4142 else
4144 r->sig[0] = (image1 << 31 << 1) | image0;
4145 r->sig[1] = (image3 << 31 << 1) | image2;
4148 normalize (r);
4150 else if (fmt->has_signed_zero)
4151 r->sign = sign;
4153 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
4155 if (image3 | image2 | image1 | image0)
4157 r->cl = rvc_nan;
4158 r->sign = sign;
4159 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
4161 if (HOST_BITS_PER_LONG == 32)
4163 r->sig[0] = image0;
4164 r->sig[1] = image1;
4165 r->sig[2] = image2;
4166 r->sig[3] = image3;
4168 else
4170 r->sig[0] = (image1 << 31 << 1) | image0;
4171 r->sig[1] = (image3 << 31 << 1) | image2;
4173 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4175 else
4177 r->cl = rvc_inf;
4178 r->sign = sign;
4181 else
4183 r->cl = rvc_normal;
4184 r->sign = sign;
4185 SET_REAL_EXP (r, exp - 16383 + 1);
4187 if (HOST_BITS_PER_LONG == 32)
4189 r->sig[0] = image0;
4190 r->sig[1] = image1;
4191 r->sig[2] = image2;
4192 r->sig[3] = image3;
4194 else
4196 r->sig[0] = (image1 << 31 << 1) | image0;
4197 r->sig[1] = (image3 << 31 << 1) | image2;
4199 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4200 r->sig[SIGSZ-1] |= SIG_MSB;
4204 const struct real_format ieee_quad_format =
4206 encode_ieee_quad,
4207 decode_ieee_quad,
4209 113,
4210 113,
4211 -16381,
4212 16384,
4213 127,
4214 127,
4215 128,
4216 false,
4217 true,
4218 true,
4219 true,
4220 true,
4221 true,
4222 true,
4223 false,
4224 "ieee_quad"
4227 const struct real_format mips_quad_format =
4229 encode_ieee_quad,
4230 decode_ieee_quad,
4232 113,
4233 113,
4234 -16381,
4235 16384,
4236 127,
4237 127,
4238 128,
4239 false,
4240 true,
4241 true,
4242 true,
4243 true,
4244 true,
4245 false,
4246 true,
4247 "mips_quad"
4250 /* Descriptions of VAX floating point formats can be found beginning at
4252 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4254 The thing to remember is that they're almost IEEE, except for word
4255 order, exponent bias, and the lack of infinities, nans, and denormals.
4257 We don't implement the H_floating format here, simply because neither
4258 the VAX or Alpha ports use it. */
4260 static void encode_vax_f (const struct real_format *fmt,
4261 long *, const REAL_VALUE_TYPE *);
4262 static void decode_vax_f (const struct real_format *,
4263 REAL_VALUE_TYPE *, const long *);
4264 static void encode_vax_d (const struct real_format *fmt,
4265 long *, const REAL_VALUE_TYPE *);
4266 static void decode_vax_d (const struct real_format *,
4267 REAL_VALUE_TYPE *, const long *);
4268 static void encode_vax_g (const struct real_format *fmt,
4269 long *, const REAL_VALUE_TYPE *);
4270 static void decode_vax_g (const struct real_format *,
4271 REAL_VALUE_TYPE *, const long *);
4273 static void
4274 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4275 const REAL_VALUE_TYPE *r)
4277 unsigned long sign, exp, sig, image;
4279 sign = r->sign << 15;
4281 switch (r->cl)
4283 case rvc_zero:
4284 image = 0;
4285 break;
4287 case rvc_inf:
4288 case rvc_nan:
4289 image = 0xffff7fff | sign;
4290 break;
4292 case rvc_normal:
4293 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4294 exp = REAL_EXP (r) + 128;
4296 image = (sig << 16) & 0xffff0000;
4297 image |= sign;
4298 image |= exp << 7;
4299 image |= sig >> 16;
4300 break;
4302 default:
4303 gcc_unreachable ();
4306 buf[0] = image;
4309 static void
4310 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
4311 REAL_VALUE_TYPE *r, const long *buf)
4313 unsigned long image = buf[0] & 0xffffffff;
4314 int exp = (image >> 7) & 0xff;
4316 memset (r, 0, sizeof (*r));
4318 if (exp != 0)
4320 r->cl = rvc_normal;
4321 r->sign = (image >> 15) & 1;
4322 SET_REAL_EXP (r, exp - 128);
4324 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
4325 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4329 static void
4330 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4331 const REAL_VALUE_TYPE *r)
4333 unsigned long image0, image1, sign = r->sign << 15;
4335 switch (r->cl)
4337 case rvc_zero:
4338 image0 = image1 = 0;
4339 break;
4341 case rvc_inf:
4342 case rvc_nan:
4343 image0 = 0xffff7fff | sign;
4344 image1 = 0xffffffff;
4345 break;
4347 case rvc_normal:
4348 /* Extract the significand into straight hi:lo. */
4349 if (HOST_BITS_PER_LONG == 64)
4351 image0 = r->sig[SIGSZ-1];
4352 image1 = (image0 >> (64 - 56)) & 0xffffffff;
4353 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
4355 else
4357 image0 = r->sig[SIGSZ-1];
4358 image1 = r->sig[SIGSZ-2];
4359 image1 = (image0 << 24) | (image1 >> 8);
4360 image0 = (image0 >> 8) & 0xffffff;
4363 /* Rearrange the half-words of the significand to match the
4364 external format. */
4365 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
4366 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4368 /* Add the sign and exponent. */
4369 image0 |= sign;
4370 image0 |= (REAL_EXP (r) + 128) << 7;
4371 break;
4373 default:
4374 gcc_unreachable ();
4377 if (FLOAT_WORDS_BIG_ENDIAN)
4378 buf[0] = image1, buf[1] = image0;
4379 else
4380 buf[0] = image0, buf[1] = image1;
4383 static void
4384 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
4385 REAL_VALUE_TYPE *r, const long *buf)
4387 unsigned long image0, image1;
4388 int exp;
4390 if (FLOAT_WORDS_BIG_ENDIAN)
4391 image1 = buf[0], image0 = buf[1];
4392 else
4393 image0 = buf[0], image1 = buf[1];
4394 image0 &= 0xffffffff;
4395 image1 &= 0xffffffff;
4397 exp = (image0 >> 7) & 0xff;
4399 memset (r, 0, sizeof (*r));
4401 if (exp != 0)
4403 r->cl = rvc_normal;
4404 r->sign = (image0 >> 15) & 1;
4405 SET_REAL_EXP (r, exp - 128);
4407 /* Rearrange the half-words of the external format into
4408 proper ascending order. */
4409 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4410 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4412 if (HOST_BITS_PER_LONG == 64)
4414 image0 = (image0 << 31 << 1) | image1;
4415 image0 <<= 64 - 56;
4416 image0 |= SIG_MSB;
4417 r->sig[SIGSZ-1] = image0;
4419 else
4421 r->sig[SIGSZ-1] = image0;
4422 r->sig[SIGSZ-2] = image1;
4423 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4424 r->sig[SIGSZ-1] |= SIG_MSB;
4429 static void
4430 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4431 const REAL_VALUE_TYPE *r)
4433 unsigned long image0, image1, sign = r->sign << 15;
4435 switch (r->cl)
4437 case rvc_zero:
4438 image0 = image1 = 0;
4439 break;
4441 case rvc_inf:
4442 case rvc_nan:
4443 image0 = 0xffff7fff | sign;
4444 image1 = 0xffffffff;
4445 break;
4447 case rvc_normal:
4448 /* Extract the significand into straight hi:lo. */
4449 if (HOST_BITS_PER_LONG == 64)
4451 image0 = r->sig[SIGSZ-1];
4452 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4453 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4455 else
4457 image0 = r->sig[SIGSZ-1];
4458 image1 = r->sig[SIGSZ-2];
4459 image1 = (image0 << 21) | (image1 >> 11);
4460 image0 = (image0 >> 11) & 0xfffff;
4463 /* Rearrange the half-words of the significand to match the
4464 external format. */
4465 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4466 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4468 /* Add the sign and exponent. */
4469 image0 |= sign;
4470 image0 |= (REAL_EXP (r) + 1024) << 4;
4471 break;
4473 default:
4474 gcc_unreachable ();
4477 if (FLOAT_WORDS_BIG_ENDIAN)
4478 buf[0] = image1, buf[1] = image0;
4479 else
4480 buf[0] = image0, buf[1] = image1;
4483 static void
4484 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4485 REAL_VALUE_TYPE *r, const long *buf)
4487 unsigned long image0, image1;
4488 int exp;
4490 if (FLOAT_WORDS_BIG_ENDIAN)
4491 image1 = buf[0], image0 = buf[1];
4492 else
4493 image0 = buf[0], image1 = buf[1];
4494 image0 &= 0xffffffff;
4495 image1 &= 0xffffffff;
4497 exp = (image0 >> 4) & 0x7ff;
4499 memset (r, 0, sizeof (*r));
4501 if (exp != 0)
4503 r->cl = rvc_normal;
4504 r->sign = (image0 >> 15) & 1;
4505 SET_REAL_EXP (r, exp - 1024);
4507 /* Rearrange the half-words of the external format into
4508 proper ascending order. */
4509 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4510 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4512 if (HOST_BITS_PER_LONG == 64)
4514 image0 = (image0 << 31 << 1) | image1;
4515 image0 <<= 64 - 53;
4516 image0 |= SIG_MSB;
4517 r->sig[SIGSZ-1] = image0;
4519 else
4521 r->sig[SIGSZ-1] = image0;
4522 r->sig[SIGSZ-2] = image1;
4523 lshift_significand (r, r, 64 - 53);
4524 r->sig[SIGSZ-1] |= SIG_MSB;
4529 const struct real_format vax_f_format =
4531 encode_vax_f,
4532 decode_vax_f,
4536 -127,
4537 127,
4541 false,
4542 false,
4543 false,
4544 false,
4545 false,
4546 false,
4547 false,
4548 false,
4549 "vax_f"
4552 const struct real_format vax_d_format =
4554 encode_vax_d,
4555 decode_vax_d,
4559 -127,
4560 127,
4564 false,
4565 false,
4566 false,
4567 false,
4568 false,
4569 false,
4570 false,
4571 false,
4572 "vax_d"
4575 const struct real_format vax_g_format =
4577 encode_vax_g,
4578 decode_vax_g,
4582 -1023,
4583 1023,
4587 false,
4588 false,
4589 false,
4590 false,
4591 false,
4592 false,
4593 false,
4594 false,
4595 "vax_g"
4598 /* Encode real R into a single precision DFP value in BUF. */
4599 static void
4600 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4601 long *buf ATTRIBUTE_UNUSED,
4602 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4604 encode_decimal32 (fmt, buf, r);
4607 /* Decode a single precision DFP value in BUF into a real R. */
4608 static void
4609 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4610 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4611 const long *buf ATTRIBUTE_UNUSED)
4613 decode_decimal32 (fmt, r, buf);
4616 /* Encode real R into a double precision DFP value in BUF. */
4617 static void
4618 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4619 long *buf ATTRIBUTE_UNUSED,
4620 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4622 encode_decimal64 (fmt, buf, r);
4625 /* Decode a double precision DFP value in BUF into a real R. */
4626 static void
4627 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4628 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4629 const long *buf ATTRIBUTE_UNUSED)
4631 decode_decimal64 (fmt, r, buf);
4634 /* Encode real R into a quad precision DFP value in BUF. */
4635 static void
4636 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4637 long *buf ATTRIBUTE_UNUSED,
4638 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4640 encode_decimal128 (fmt, buf, r);
4643 /* Decode a quad precision DFP value in BUF into a real R. */
4644 static void
4645 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4646 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4647 const long *buf ATTRIBUTE_UNUSED)
4649 decode_decimal128 (fmt, r, buf);
4652 /* Single precision decimal floating point (IEEE 754). */
4653 const struct real_format decimal_single_format =
4655 encode_decimal_single,
4656 decode_decimal_single,
4660 -94,
4665 false,
4666 true,
4667 true,
4668 true,
4669 true,
4670 true,
4671 true,
4672 false,
4673 "decimal_single"
4676 /* Double precision decimal floating point (IEEE 754). */
4677 const struct real_format decimal_double_format =
4679 encode_decimal_double,
4680 decode_decimal_double,
4684 -382,
4685 385,
4689 false,
4690 true,
4691 true,
4692 true,
4693 true,
4694 true,
4695 true,
4696 false,
4697 "decimal_double"
4700 /* Quad precision decimal floating point (IEEE 754). */
4701 const struct real_format decimal_quad_format =
4703 encode_decimal_quad,
4704 decode_decimal_quad,
4708 -6142,
4709 6145,
4710 127,
4711 127,
4712 128,
4713 false,
4714 true,
4715 true,
4716 true,
4717 true,
4718 true,
4719 true,
4720 false,
4721 "decimal_quad"
4724 /* Encode half-precision floats. This routine is used both for the IEEE
4725 ARM alternative encodings. */
4726 static void
4727 encode_ieee_half (const struct real_format *fmt, long *buf,
4728 const REAL_VALUE_TYPE *r)
4730 unsigned long image, sig, exp;
4731 unsigned long sign = r->sign;
4732 bool denormal = real_isdenormal (r);
4734 image = sign << 15;
4735 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 11)) & 0x3ff;
4737 switch (r->cl)
4739 case rvc_zero:
4740 break;
4742 case rvc_inf:
4743 if (fmt->has_inf)
4744 image |= 31 << 10;
4745 else
4746 image |= 0x7fff;
4747 break;
4749 case rvc_nan:
4750 if (fmt->has_nans)
4752 if (r->canonical)
4753 sig = (fmt->canonical_nan_lsbs_set ? (1 << 9) - 1 : 0);
4754 if (r->signalling == fmt->qnan_msb_set)
4755 sig &= ~(1 << 9);
4756 else
4757 sig |= 1 << 9;
4758 if (sig == 0)
4759 sig = 1 << 8;
4761 image |= 31 << 10;
4762 image |= sig;
4764 else
4765 image |= 0x3ff;
4766 break;
4768 case rvc_normal:
4769 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4770 whereas the intermediate representation is 0.F x 2**exp.
4771 Which means we're off by one. */
4772 if (denormal)
4773 exp = 0;
4774 else
4775 exp = REAL_EXP (r) + 15 - 1;
4776 image |= exp << 10;
4777 image |= sig;
4778 break;
4780 default:
4781 gcc_unreachable ();
4784 buf[0] = image;
4787 /* Decode half-precision floats. This routine is used both for the IEEE
4788 ARM alternative encodings. */
4789 static void
4790 decode_ieee_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4791 const long *buf)
4793 unsigned long image = buf[0] & 0xffff;
4794 bool sign = (image >> 15) & 1;
4795 int exp = (image >> 10) & 0x1f;
4797 memset (r, 0, sizeof (*r));
4798 image <<= HOST_BITS_PER_LONG - 11;
4799 image &= ~SIG_MSB;
4801 if (exp == 0)
4803 if (image && fmt->has_denorm)
4805 r->cl = rvc_normal;
4806 r->sign = sign;
4807 SET_REAL_EXP (r, -14);
4808 r->sig[SIGSZ-1] = image << 1;
4809 normalize (r);
4811 else if (fmt->has_signed_zero)
4812 r->sign = sign;
4814 else if (exp == 31 && (fmt->has_nans || fmt->has_inf))
4816 if (image)
4818 r->cl = rvc_nan;
4819 r->sign = sign;
4820 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4821 ^ fmt->qnan_msb_set);
4822 r->sig[SIGSZ-1] = image;
4824 else
4826 r->cl = rvc_inf;
4827 r->sign = sign;
4830 else
4832 r->cl = rvc_normal;
4833 r->sign = sign;
4834 SET_REAL_EXP (r, exp - 15 + 1);
4835 r->sig[SIGSZ-1] = image | SIG_MSB;
4839 /* Encode arm_bfloat types. */
4840 static void
4841 encode_arm_bfloat_half (const struct real_format *fmt, long *buf,
4842 const REAL_VALUE_TYPE *r)
4844 unsigned long image, sig, exp;
4845 unsigned long sign = r->sign;
4846 bool denormal = real_isdenormal (r);
4848 image = sign << 15;
4849 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 8)) & 0x7f;
4851 switch (r->cl)
4853 case rvc_zero:
4854 break;
4856 case rvc_inf:
4857 if (fmt->has_inf)
4858 image |= 255 << 7;
4859 else
4860 image |= 0x7fff;
4861 break;
4863 case rvc_nan:
4864 if (fmt->has_nans)
4866 if (r->canonical)
4867 sig = (fmt->canonical_nan_lsbs_set ? (1 << 6) - 1 : 0);
4868 if (r->signalling == fmt->qnan_msb_set)
4869 sig &= ~(1 << 6);
4870 else
4871 sig |= 1 << 6;
4872 if (sig == 0)
4873 sig = 1 << 5;
4875 image |= 255 << 7;
4876 image |= sig;
4878 else
4879 image |= 0x7fff;
4880 break;
4882 case rvc_normal:
4883 if (denormal)
4884 exp = 0;
4885 else
4886 exp = REAL_EXP (r) + 127 - 1;
4887 image |= exp << 7;
4888 image |= sig;
4889 break;
4891 default:
4892 gcc_unreachable ();
4895 buf[0] = image;
4898 /* Decode arm_bfloat types. */
4899 static void
4900 decode_arm_bfloat_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4901 const long *buf)
4903 unsigned long image = buf[0] & 0xffff;
4904 bool sign = (image >> 15) & 1;
4905 int exp = (image >> 7) & 0xff;
4907 memset (r, 0, sizeof (*r));
4908 image <<= HOST_BITS_PER_LONG - 8;
4909 image &= ~SIG_MSB;
4911 if (exp == 0)
4913 if (image && fmt->has_denorm)
4915 r->cl = rvc_normal;
4916 r->sign = sign;
4917 SET_REAL_EXP (r, -126);
4918 r->sig[SIGSZ-1] = image << 1;
4919 normalize (r);
4921 else if (fmt->has_signed_zero)
4922 r->sign = sign;
4924 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
4926 if (image)
4928 r->cl = rvc_nan;
4929 r->sign = sign;
4930 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4931 ^ fmt->qnan_msb_set);
4932 r->sig[SIGSZ-1] = image;
4934 else
4936 r->cl = rvc_inf;
4937 r->sign = sign;
4940 else
4942 r->cl = rvc_normal;
4943 r->sign = sign;
4944 SET_REAL_EXP (r, exp - 127 + 1);
4945 r->sig[SIGSZ-1] = image | SIG_MSB;
4949 /* Half-precision format, as specified in IEEE 754R. */
4950 const struct real_format ieee_half_format =
4952 encode_ieee_half,
4953 decode_ieee_half,
4957 -13,
4962 false,
4963 true,
4964 true,
4965 true,
4966 true,
4967 true,
4968 true,
4969 false,
4970 "ieee_half"
4973 /* ARM's alternative half-precision format, similar to IEEE but with
4974 no reserved exponent value for NaNs and infinities; rather, it just
4975 extends the range of exponents by one. */
4976 const struct real_format arm_half_format =
4978 encode_ieee_half,
4979 decode_ieee_half,
4983 -13,
4988 false,
4989 true,
4990 false,
4991 false,
4992 true,
4993 true,
4994 false,
4995 false,
4996 "arm_half"
4999 /* ARM Bfloat half-precision format. This format resembles a truncated
5000 (16-bit) version of the 32-bit IEEE 754 single-precision floating-point
5001 format. */
5002 const struct real_format arm_bfloat_half_format =
5004 encode_arm_bfloat_half,
5005 decode_arm_bfloat_half,
5009 -125,
5010 128,
5014 false,
5015 true,
5016 true,
5017 true,
5018 true,
5019 true,
5020 true,
5021 false,
5022 "arm_bfloat_half"
5026 /* A synthetic "format" for internal arithmetic. It's the size of the
5027 internal significand minus the two bits needed for proper rounding.
5028 The encode and decode routines exist only to satisfy our paranoia
5029 harness. */
5031 static void encode_internal (const struct real_format *fmt,
5032 long *, const REAL_VALUE_TYPE *);
5033 static void decode_internal (const struct real_format *,
5034 REAL_VALUE_TYPE *, const long *);
5036 static void
5037 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
5038 const REAL_VALUE_TYPE *r)
5040 memcpy (buf, r, sizeof (*r));
5043 static void
5044 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
5045 REAL_VALUE_TYPE *r, const long *buf)
5047 memcpy (r, buf, sizeof (*r));
5050 const struct real_format real_internal_format =
5052 encode_internal,
5053 decode_internal,
5055 SIGNIFICAND_BITS - 2,
5056 SIGNIFICAND_BITS - 2,
5057 -MAX_EXP,
5058 MAX_EXP,
5062 false,
5063 false,
5064 true,
5065 true,
5066 false,
5067 true,
5068 true,
5069 false,
5070 "real_internal"
5073 /* Calculate X raised to the integer exponent N in format FMT and store
5074 the result in R. Return true if the result may be inexact due to
5075 loss of precision. The algorithm is the classic "left-to-right binary
5076 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
5077 Algorithms", "The Art of Computer Programming", Volume 2. */
5079 bool
5080 real_powi (REAL_VALUE_TYPE *r, format_helper fmt,
5081 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
5083 unsigned HOST_WIDE_INT bit;
5084 REAL_VALUE_TYPE t;
5085 bool inexact = false;
5086 bool init = false;
5087 bool neg;
5088 int i;
5090 if (n == 0)
5092 *r = dconst1;
5093 return false;
5095 else if (n < 0)
5097 /* Don't worry about overflow, from now on n is unsigned. */
5098 neg = true;
5099 n = -n;
5101 else
5102 neg = false;
5104 t = *x;
5105 bit = HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1);
5106 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
5108 if (init)
5110 inexact |= do_multiply (&t, &t, &t);
5111 if (n & bit)
5112 inexact |= do_multiply (&t, &t, x);
5114 else if (n & bit)
5115 init = true;
5116 bit >>= 1;
5119 if (neg)
5120 inexact |= do_divide (&t, &dconst1, &t);
5122 real_convert (r, fmt, &t);
5123 return inexact;
5126 /* Round X to the nearest integer not larger in absolute value, i.e.
5127 towards zero, placing the result in R in format FMT. */
5129 void
5130 real_trunc (REAL_VALUE_TYPE *r, format_helper fmt,
5131 const REAL_VALUE_TYPE *x)
5133 do_fix_trunc (r, x);
5134 if (fmt)
5135 real_convert (r, fmt, r);
5138 /* Round X to the largest integer not greater in value, i.e. round
5139 down, placing the result in R in format FMT. */
5141 void
5142 real_floor (REAL_VALUE_TYPE *r, format_helper fmt,
5143 const REAL_VALUE_TYPE *x)
5145 REAL_VALUE_TYPE t;
5147 do_fix_trunc (&t, x);
5148 if (! real_identical (&t, x) && x->sign)
5149 do_add (&t, &t, &dconstm1, 0);
5150 if (fmt)
5151 real_convert (r, fmt, &t);
5152 else
5153 *r = t;
5156 /* Round X to the smallest integer not less then argument, i.e. round
5157 up, placing the result in R in format FMT. */
5159 void
5160 real_ceil (REAL_VALUE_TYPE *r, format_helper fmt,
5161 const REAL_VALUE_TYPE *x)
5163 REAL_VALUE_TYPE t;
5165 do_fix_trunc (&t, x);
5166 if (! real_identical (&t, x) && ! x->sign)
5167 do_add (&t, &t, &dconst1, 0);
5168 if (fmt)
5169 real_convert (r, fmt, &t);
5170 else
5171 *r = t;
5174 /* Round X to the nearest integer, but round halfway cases away from
5175 zero. */
5177 void
5178 real_round (REAL_VALUE_TYPE *r, format_helper fmt,
5179 const REAL_VALUE_TYPE *x)
5181 do_add (r, x, &dconsthalf, x->sign);
5182 do_fix_trunc (r, r);
5183 if (fmt)
5184 real_convert (r, fmt, r);
5187 /* Return true (including 0) if integer part of R is even, else return
5188 false. The function is not valid for rvc_inf and rvc_nan classes. */
5190 static bool
5191 is_even (REAL_VALUE_TYPE *r)
5193 gcc_assert (r->cl != rvc_inf);
5194 gcc_assert (r->cl != rvc_nan);
5196 if (r->cl == rvc_zero)
5197 return true;
5199 /* For (-1,1), number is even. */
5200 if (REAL_EXP (r) <= 0)
5201 return true;
5203 /* Check lowest bit, if not set, return true. */
5204 else if (REAL_EXP (r) <= SIGNIFICAND_BITS)
5206 unsigned int n = SIGNIFICAND_BITS - REAL_EXP (r);
5207 int w = n / HOST_BITS_PER_LONG;
5209 unsigned long num = ((unsigned long)1 << (n % HOST_BITS_PER_LONG));
5211 if ((r->sig[w] & num) == 0)
5212 return true;
5214 else
5215 return true;
5217 return false;
5220 /* Return true if R is halfway between two integers, else return
5221 false. */
5223 static bool
5224 is_halfway_below (const REAL_VALUE_TYPE *r)
5226 if (r->cl != rvc_normal)
5227 return false;
5229 /* For numbers (-0.5,0) and (0,0.5). */
5230 if (REAL_EXP (r) < 0)
5231 return false;
5233 else if (REAL_EXP (r) < SIGNIFICAND_BITS)
5235 unsigned int n = SIGNIFICAND_BITS - REAL_EXP (r) - 1;
5236 int w = n / HOST_BITS_PER_LONG;
5238 for (int i = 0; i < w; ++i)
5239 if (r->sig[i] != 0)
5240 return false;
5242 unsigned long num = 1UL << (n % HOST_BITS_PER_LONG);
5244 if ((r->sig[w] & num) != 0 && (r->sig[w] & (num - 1)) == 0)
5245 return true;
5247 return false;
5250 /* Round X to nearest integer, rounding halfway cases towards even. */
5252 void
5253 real_roundeven (REAL_VALUE_TYPE *r, format_helper fmt,
5254 const REAL_VALUE_TYPE *x)
5256 if (is_halfway_below (x))
5258 /* Special case as -0.5 rounds to -0.0 and
5259 similarly +0.5 rounds to +0.0. */
5260 if (REAL_EXP (x) == 0)
5262 *r = *x;
5263 clear_significand_below (r, SIGNIFICAND_BITS);
5265 else
5267 do_add (r, x, &dconsthalf, x->sign);
5268 if (!is_even (r))
5269 do_add (r, r, &dconstm1, x->sign);
5271 if (fmt)
5272 real_convert (r, fmt, r);
5274 else
5275 real_round (r, fmt, x);
5278 /* Set the sign of R to the sign of X. */
5280 void
5281 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
5283 r->sign = x->sign;
5286 /* Check whether the real constant value given is an integer.
5287 Returns false for signaling NaN. */
5289 bool
5290 real_isinteger (const REAL_VALUE_TYPE *c, format_helper fmt)
5292 REAL_VALUE_TYPE cint;
5294 real_trunc (&cint, fmt, c);
5295 return real_identical (c, &cint);
5298 /* Check whether C is an integer that fits in a HOST_WIDE_INT,
5299 storing it in *INT_OUT if so. */
5301 bool
5302 real_isinteger (const REAL_VALUE_TYPE *c, HOST_WIDE_INT *int_out)
5304 REAL_VALUE_TYPE cint;
5306 HOST_WIDE_INT n = real_to_integer (c);
5307 real_from_integer (&cint, VOIDmode, n, SIGNED);
5308 if (real_identical (c, &cint))
5310 *int_out = n;
5311 return true;
5313 return false;
5316 /* Calculate nextafter (X, Y) or nexttoward (X, Y). Return true if
5317 underflow or overflow needs to be raised. */
5319 bool
5320 real_nextafter (REAL_VALUE_TYPE *r, format_helper fmt,
5321 const REAL_VALUE_TYPE *x, const REAL_VALUE_TYPE *y)
5323 int cmp = do_compare (x, y, 2);
5324 /* If either operand is NaN, return qNaN. */
5325 if (cmp == 2)
5327 get_canonical_qnan (r, 0);
5328 return false;
5330 /* If x == y, return y cast to target type. */
5331 if (cmp == 0)
5333 real_convert (r, fmt, y);
5334 return false;
5337 if (x->cl == rvc_zero)
5339 get_zero (r, y->sign);
5340 r->cl = rvc_normal;
5341 SET_REAL_EXP (r, fmt->emin - fmt->p + 1);
5342 r->sig[SIGSZ - 1] = SIG_MSB;
5343 return false;
5346 int np2 = SIGNIFICAND_BITS - fmt->p;
5347 /* For denormals adjust np2 correspondingly. */
5348 if (x->cl == rvc_normal && REAL_EXP (x) < fmt->emin)
5349 np2 += fmt->emin - REAL_EXP (x);
5351 REAL_VALUE_TYPE u;
5352 get_zero (r, x->sign);
5353 get_zero (&u, 0);
5354 set_significand_bit (&u, np2);
5355 r->cl = rvc_normal;
5356 SET_REAL_EXP (r, REAL_EXP (x));
5358 if (x->cl == rvc_inf)
5360 bool borrow = sub_significands (r, r, &u, 0);
5361 gcc_assert (borrow);
5362 SET_REAL_EXP (r, fmt->emax);
5364 else if (cmp == (x->sign ? 1 : -1))
5366 if (add_significands (r, x, &u))
5368 /* Overflow. Means the significand had been all ones, and
5369 is now all zeros. Need to increase the exponent, and
5370 possibly re-normalize it. */
5371 SET_REAL_EXP (r, REAL_EXP (r) + 1);
5372 if (REAL_EXP (r) > fmt->emax)
5374 get_inf (r, x->sign);
5375 return true;
5377 r->sig[SIGSZ - 1] = SIG_MSB;
5380 else
5382 if (REAL_EXP (x) > fmt->emin && x->sig[SIGSZ - 1] == SIG_MSB)
5384 int i;
5385 for (i = SIGSZ - 2; i >= 0; i--)
5386 if (x->sig[i])
5387 break;
5388 if (i < 0)
5390 /* When mantissa is 1.0, we need to subtract only
5391 half of u: nextafter (1.0, 0.0) is 1.0 - __DBL_EPSILON__ / 2
5392 rather than 1.0 - __DBL_EPSILON__. */
5393 clear_significand_bit (&u, np2);
5394 np2--;
5395 set_significand_bit (&u, np2);
5398 sub_significands (r, x, &u, 0);
5401 /* Clear out trailing garbage. */
5402 clear_significand_below (r, np2);
5403 normalize (r);
5404 if (REAL_EXP (r) <= fmt->emin - fmt->p)
5406 get_zero (r, x->sign);
5407 return true;
5409 return r->cl == rvc_zero || REAL_EXP (r) < fmt->emin;
5412 /* Write into BUF the maximum representable finite floating-point
5413 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5414 float string. LEN is the size of BUF, and the buffer must be large
5415 enough to contain the resulting string. If NORM_MAX, instead write
5416 the maximum representable finite normalized floating-point number,
5417 defined to be such that all choices of digits for that exponent are
5418 representable in the format (this only makes a difference for IBM
5419 long double). */
5421 void
5422 get_max_float (const struct real_format *fmt, char *buf, size_t len,
5423 bool norm_max)
5425 int i, n;
5426 char *p;
5427 bool is_ibm_extended = fmt->pnan < fmt->p;
5429 strcpy (buf, "0x0.");
5430 n = fmt->p;
5431 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
5432 *p++ = 'f';
5433 if (i < n)
5434 *p++ = "08ce"[n - i];
5435 sprintf (p, "p%d",
5436 (is_ibm_extended && norm_max) ? fmt->emax - 1 : fmt->emax);
5437 if (is_ibm_extended && !norm_max)
5439 /* This is an IBM extended double format made up of two IEEE
5440 doubles. The value of the long double is the sum of the
5441 values of the two parts. The most significant part is
5442 required to be the value of the long double rounded to the
5443 nearest double. Rounding means we need a slightly smaller
5444 value for LDBL_MAX. */
5445 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
5448 gcc_assert (strlen (buf) < len);
5451 /* True if all values of integral type can be represented
5452 by this floating-point type exactly. */
5454 bool format_helper::can_represent_integral_type_p (tree type) const
5456 gcc_assert (! decimal_p () && INTEGRAL_TYPE_P (type));
5458 /* INT?_MIN is power-of-two so it takes
5459 only one mantissa bit. */
5460 bool signed_p = TYPE_SIGN (type) == SIGNED;
5461 return TYPE_PRECISION (type) - signed_p <= significand_size (*this);
5464 /* True if mode M has a NaN representation and
5465 the treatment of NaN operands is important. */
5467 bool
5468 HONOR_NANS (machine_mode m)
5470 return MODE_HAS_NANS (m) && !flag_finite_math_only;
5473 bool
5474 HONOR_NANS (const_tree t)
5476 return HONOR_NANS (element_mode (t));
5479 bool
5480 HONOR_NANS (const_rtx x)
5482 return HONOR_NANS (GET_MODE (x));
5485 /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5487 bool
5488 HONOR_SNANS (machine_mode m)
5490 return flag_signaling_nans && HONOR_NANS (m);
5493 bool
5494 HONOR_SNANS (const_tree t)
5496 return HONOR_SNANS (element_mode (t));
5499 bool
5500 HONOR_SNANS (const_rtx x)
5502 return HONOR_SNANS (GET_MODE (x));
5505 /* As for HONOR_NANS, but true if the mode can represent infinity and
5506 the treatment of infinite values is important. */
5508 bool
5509 HONOR_INFINITIES (machine_mode m)
5511 return MODE_HAS_INFINITIES (m) && !flag_finite_math_only;
5514 bool
5515 HONOR_INFINITIES (const_tree t)
5517 return HONOR_INFINITIES (element_mode (t));
5520 bool
5521 HONOR_INFINITIES (const_rtx x)
5523 return HONOR_INFINITIES (GET_MODE (x));
5526 /* Like HONOR_NANS, but true if the given mode distinguishes between
5527 positive and negative zero, and the sign of zero is important. */
5529 bool
5530 HONOR_SIGNED_ZEROS (machine_mode m)
5532 return MODE_HAS_SIGNED_ZEROS (m) && flag_signed_zeros;
5535 bool
5536 HONOR_SIGNED_ZEROS (const_tree t)
5538 return HONOR_SIGNED_ZEROS (element_mode (t));
5541 bool
5542 HONOR_SIGNED_ZEROS (const_rtx x)
5544 return HONOR_SIGNED_ZEROS (GET_MODE (x));
5547 /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5548 and the rounding mode is important. */
5550 bool
5551 HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m)
5553 return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m) && flag_rounding_math;
5556 bool
5557 HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t)
5559 return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t));
5562 bool
5563 HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x)
5565 return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x));
5568 /* Fills r with the largest value such that 1 + r*r won't overflow.
5569 This is used in both sin (atan (x)) and cos (atan(x)) optimizations. */
5571 void
5572 build_sinatan_real (REAL_VALUE_TYPE * r, tree type)
5574 REAL_VALUE_TYPE maxval;
5575 mpfr_t mpfr_const1, mpfr_c, mpfr_maxval;
5576 machine_mode mode = TYPE_MODE (type);
5577 const struct real_format * fmt = REAL_MODE_FORMAT (mode);
5579 real_maxval (&maxval, 0, mode);
5581 mpfr_inits (mpfr_const1, mpfr_c, mpfr_maxval, NULL);
5583 mpfr_from_real (mpfr_const1, &dconst1, MPFR_RNDN);
5584 mpfr_from_real (mpfr_maxval, &maxval, MPFR_RNDN);
5586 mpfr_sub (mpfr_c, mpfr_maxval, mpfr_const1, MPFR_RNDN);
5587 mpfr_sqrt (mpfr_c, mpfr_c, MPFR_RNDZ);
5589 real_from_mpfr (r, mpfr_c, fmt, MPFR_RNDZ);
5591 mpfr_clears (mpfr_const1, mpfr_c, mpfr_maxval, NULL);