* rw.po: Remove.
[official-gcc.git] / gcc / real.c
blob875fce99bb409cb8d10867299fb19c73f10e4ce9
1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
4 Contributed by Stephen L. Moshier (moshier@world.std.com).
5 Re-written by Richard Henderson <rth@redhat.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "toplev.h"
29 #include "real.h"
30 #include "tm_p.h"
31 #include "dfp.h"
33 /* The floating point model used internally is not exactly IEEE 754
34 compliant, and close to the description in the ISO C99 standard,
35 section 5.2.4.2.2 Characteristics of floating types.
37 Specifically
39 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
41 where
42 s = sign (+- 1)
43 b = base or radix, here always 2
44 e = exponent
45 p = precision (the number of base-b digits in the significand)
46 f_k = the digits of the significand.
48 We differ from typical IEEE 754 encodings in that the entire
49 significand is fractional. Normalized significands are in the
50 range [0.5, 1.0).
52 A requirement of the model is that P be larger than the largest
53 supported target floating-point type by at least 2 bits. This gives
54 us proper rounding when we truncate to the target type. In addition,
55 E must be large enough to hold the smallest supported denormal number
56 in a normalized form.
58 Both of these requirements are easily satisfied. The largest target
59 significand is 113 bits; we store at least 160. The smallest
60 denormal number fits in 17 exponent bits; we store 27.
62 Note that the decimal string conversion routines are sensitive to
63 rounding errors. Since the raw arithmetic routines do not themselves
64 have guard digits or rounding, the computation of 10**exp can
65 accumulate more than a few digits of error. The previous incarnation
66 of real.c successfully used a 144-bit fraction; given the current
67 layout of REAL_VALUE_TYPE we're forced to expand to at least 160 bits.
69 Target floating point models that use base 16 instead of base 2
70 (i.e. IBM 370), are handled during round_for_format, in which we
71 canonicalize the exponent to be a multiple of 4 (log2(16)), and
72 adjust the significand to match. */
75 /* Used to classify two numbers simultaneously. */
76 #define CLASS2(A, B) ((A) << 2 | (B))
78 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
79 #error "Some constant folding done by hand to avoid shift count warnings"
80 #endif
82 static void get_zero (REAL_VALUE_TYPE *, int);
83 static void get_canonical_qnan (REAL_VALUE_TYPE *, int);
84 static void get_canonical_snan (REAL_VALUE_TYPE *, int);
85 static void get_inf (REAL_VALUE_TYPE *, int);
86 static bool sticky_rshift_significand (REAL_VALUE_TYPE *,
87 const REAL_VALUE_TYPE *, unsigned int);
88 static void rshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
89 unsigned int);
90 static void lshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
91 unsigned int);
92 static void lshift_significand_1 (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
93 static bool add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *,
94 const REAL_VALUE_TYPE *);
95 static bool sub_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
96 const REAL_VALUE_TYPE *, int);
97 static void neg_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
98 static int cmp_significands (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
99 static int cmp_significand_0 (const REAL_VALUE_TYPE *);
100 static void set_significand_bit (REAL_VALUE_TYPE *, unsigned int);
101 static void clear_significand_bit (REAL_VALUE_TYPE *, unsigned int);
102 static bool test_significand_bit (REAL_VALUE_TYPE *, unsigned int);
103 static void clear_significand_below (REAL_VALUE_TYPE *, unsigned int);
104 static bool div_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
105 const REAL_VALUE_TYPE *);
106 static void normalize (REAL_VALUE_TYPE *);
108 static bool do_add (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
109 const REAL_VALUE_TYPE *, int);
110 static bool do_multiply (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
111 const REAL_VALUE_TYPE *);
112 static bool do_divide (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
113 const REAL_VALUE_TYPE *);
114 static int do_compare (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int);
115 static void do_fix_trunc (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
117 static unsigned long rtd_divmod (REAL_VALUE_TYPE *, REAL_VALUE_TYPE *);
119 static const REAL_VALUE_TYPE * ten_to_ptwo (int);
120 static const REAL_VALUE_TYPE * ten_to_mptwo (int);
121 static const REAL_VALUE_TYPE * real_digit (int);
122 static void times_pten (REAL_VALUE_TYPE *, int);
124 static void round_for_format (const struct real_format *, REAL_VALUE_TYPE *);
126 /* Initialize R with a positive zero. */
128 static inline void
129 get_zero (REAL_VALUE_TYPE *r, int sign)
131 memset (r, 0, sizeof (*r));
132 r->sign = sign;
135 /* Initialize R with the canonical quiet NaN. */
137 static inline void
138 get_canonical_qnan (REAL_VALUE_TYPE *r, int sign)
140 memset (r, 0, sizeof (*r));
141 r->cl = rvc_nan;
142 r->sign = sign;
143 r->canonical = 1;
146 static inline void
147 get_canonical_snan (REAL_VALUE_TYPE *r, int sign)
149 memset (r, 0, sizeof (*r));
150 r->cl = rvc_nan;
151 r->sign = sign;
152 r->signalling = 1;
153 r->canonical = 1;
156 static inline void
157 get_inf (REAL_VALUE_TYPE *r, int sign)
159 memset (r, 0, sizeof (*r));
160 r->cl = rvc_inf;
161 r->sign = sign;
165 /* Right-shift the significand of A by N bits; put the result in the
166 significand of R. If any one bits are shifted out, return true. */
168 static bool
169 sticky_rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
170 unsigned int n)
172 unsigned long sticky = 0;
173 unsigned int i, ofs = 0;
175 if (n >= HOST_BITS_PER_LONG)
177 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
178 sticky |= a->sig[i];
179 n &= HOST_BITS_PER_LONG - 1;
182 if (n != 0)
184 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
185 for (i = 0; i < SIGSZ; ++i)
187 r->sig[i]
188 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
189 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
190 << (HOST_BITS_PER_LONG - n)));
193 else
195 for (i = 0; ofs + i < SIGSZ; ++i)
196 r->sig[i] = a->sig[ofs + i];
197 for (; i < SIGSZ; ++i)
198 r->sig[i] = 0;
201 return sticky != 0;
204 /* Right-shift the significand of A by N bits; put the result in the
205 significand of R. */
207 static void
208 rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
209 unsigned int n)
211 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
213 n &= HOST_BITS_PER_LONG - 1;
214 if (n != 0)
216 for (i = 0; i < SIGSZ; ++i)
218 r->sig[i]
219 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
220 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
221 << (HOST_BITS_PER_LONG - n)));
224 else
226 for (i = 0; ofs + i < SIGSZ; ++i)
227 r->sig[i] = a->sig[ofs + i];
228 for (; i < SIGSZ; ++i)
229 r->sig[i] = 0;
233 /* Left-shift the significand of A by N bits; put the result in the
234 significand of R. */
236 static void
237 lshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
238 unsigned int n)
240 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
242 n &= HOST_BITS_PER_LONG - 1;
243 if (n == 0)
245 for (i = 0; ofs + i < SIGSZ; ++i)
246 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
247 for (; i < SIGSZ; ++i)
248 r->sig[SIGSZ-1-i] = 0;
250 else
251 for (i = 0; i < SIGSZ; ++i)
253 r->sig[SIGSZ-1-i]
254 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
255 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
256 >> (HOST_BITS_PER_LONG - n)));
260 /* Likewise, but N is specialized to 1. */
262 static inline void
263 lshift_significand_1 (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
265 unsigned int i;
267 for (i = SIGSZ - 1; i > 0; --i)
268 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
269 r->sig[0] = a->sig[0] << 1;
272 /* Add the significands of A and B, placing the result in R. Return
273 true if there was carry out of the most significant word. */
275 static inline bool
276 add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
277 const REAL_VALUE_TYPE *b)
279 bool carry = false;
280 int i;
282 for (i = 0; i < SIGSZ; ++i)
284 unsigned long ai = a->sig[i];
285 unsigned long ri = ai + b->sig[i];
287 if (carry)
289 carry = ri < ai;
290 carry |= ++ri == 0;
292 else
293 carry = ri < ai;
295 r->sig[i] = ri;
298 return carry;
301 /* Subtract the significands of A and B, placing the result in R. CARRY is
302 true if there's a borrow incoming to the least significant word.
303 Return true if there was borrow out of the most significant word. */
305 static inline bool
306 sub_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
307 const REAL_VALUE_TYPE *b, int carry)
309 int i;
311 for (i = 0; i < SIGSZ; ++i)
313 unsigned long ai = a->sig[i];
314 unsigned long ri = ai - b->sig[i];
316 if (carry)
318 carry = ri > ai;
319 carry |= ~--ri == 0;
321 else
322 carry = ri > ai;
324 r->sig[i] = ri;
327 return carry;
330 /* Negate the significand A, placing the result in R. */
332 static inline void
333 neg_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
335 bool carry = true;
336 int i;
338 for (i = 0; i < SIGSZ; ++i)
340 unsigned long ri, ai = a->sig[i];
342 if (carry)
344 if (ai)
346 ri = -ai;
347 carry = false;
349 else
350 ri = ai;
352 else
353 ri = ~ai;
355 r->sig[i] = ri;
359 /* Compare significands. Return tri-state vs zero. */
361 static inline int
362 cmp_significands (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
364 int i;
366 for (i = SIGSZ - 1; i >= 0; --i)
368 unsigned long ai = a->sig[i];
369 unsigned long bi = b->sig[i];
371 if (ai > bi)
372 return 1;
373 if (ai < bi)
374 return -1;
377 return 0;
380 /* Return true if A is nonzero. */
382 static inline int
383 cmp_significand_0 (const REAL_VALUE_TYPE *a)
385 int i;
387 for (i = SIGSZ - 1; i >= 0; --i)
388 if (a->sig[i])
389 return 1;
391 return 0;
394 /* Set bit N of the significand of R. */
396 static inline void
397 set_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
399 r->sig[n / HOST_BITS_PER_LONG]
400 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
403 /* Clear bit N of the significand of R. */
405 static inline void
406 clear_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
408 r->sig[n / HOST_BITS_PER_LONG]
409 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
412 /* Test bit N of the significand of R. */
414 static inline bool
415 test_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
417 /* ??? Compiler bug here if we return this expression directly.
418 The conversion to bool strips the "&1" and we wind up testing
419 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
420 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
421 return t;
424 /* Clear bits 0..N-1 of the significand of R. */
426 static void
427 clear_significand_below (REAL_VALUE_TYPE *r, unsigned int n)
429 int i, w = n / HOST_BITS_PER_LONG;
431 for (i = 0; i < w; ++i)
432 r->sig[i] = 0;
434 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
437 /* Divide the significands of A and B, placing the result in R. Return
438 true if the division was inexact. */
440 static inline bool
441 div_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
442 const REAL_VALUE_TYPE *b)
444 REAL_VALUE_TYPE u;
445 int i, bit = SIGNIFICAND_BITS - 1;
446 unsigned long msb, inexact;
448 u = *a;
449 memset (r->sig, 0, sizeof (r->sig));
451 msb = 0;
452 goto start;
455 msb = u.sig[SIGSZ-1] & SIG_MSB;
456 lshift_significand_1 (&u, &u);
457 start:
458 if (msb || cmp_significands (&u, b) >= 0)
460 sub_significands (&u, &u, b, 0);
461 set_significand_bit (r, bit);
464 while (--bit >= 0);
466 for (i = 0, inexact = 0; i < SIGSZ; i++)
467 inexact |= u.sig[i];
469 return inexact != 0;
472 /* Adjust the exponent and significand of R such that the most
473 significant bit is set. We underflow to zero and overflow to
474 infinity here, without denormals. (The intermediate representation
475 exponent is large enough to handle target denormals normalized.) */
477 static void
478 normalize (REAL_VALUE_TYPE *r)
480 int shift = 0, exp;
481 int i, j;
483 if (r->decimal)
484 return;
486 /* Find the first word that is nonzero. */
487 for (i = SIGSZ - 1; i >= 0; i--)
488 if (r->sig[i] == 0)
489 shift += HOST_BITS_PER_LONG;
490 else
491 break;
493 /* Zero significand flushes to zero. */
494 if (i < 0)
496 r->cl = rvc_zero;
497 SET_REAL_EXP (r, 0);
498 return;
501 /* Find the first bit that is nonzero. */
502 for (j = 0; ; j++)
503 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
504 break;
505 shift += j;
507 if (shift > 0)
509 exp = REAL_EXP (r) - shift;
510 if (exp > MAX_EXP)
511 get_inf (r, r->sign);
512 else if (exp < -MAX_EXP)
513 get_zero (r, r->sign);
514 else
516 SET_REAL_EXP (r, exp);
517 lshift_significand (r, r, shift);
522 /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
523 result may be inexact due to a loss of precision. */
525 static bool
526 do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
527 const REAL_VALUE_TYPE *b, int subtract_p)
529 int dexp, sign, exp;
530 REAL_VALUE_TYPE t;
531 bool inexact = false;
533 /* Determine if we need to add or subtract. */
534 sign = a->sign;
535 subtract_p = (sign ^ b->sign) ^ subtract_p;
537 switch (CLASS2 (a->cl, b->cl))
539 case CLASS2 (rvc_zero, rvc_zero):
540 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
541 get_zero (r, sign & !subtract_p);
542 return false;
544 case CLASS2 (rvc_zero, rvc_normal):
545 case CLASS2 (rvc_zero, rvc_inf):
546 case CLASS2 (rvc_zero, rvc_nan):
547 /* 0 + ANY = ANY. */
548 case CLASS2 (rvc_normal, rvc_nan):
549 case CLASS2 (rvc_inf, rvc_nan):
550 case CLASS2 (rvc_nan, rvc_nan):
551 /* ANY + NaN = NaN. */
552 case CLASS2 (rvc_normal, rvc_inf):
553 /* R + Inf = Inf. */
554 *r = *b;
555 r->sign = sign ^ subtract_p;
556 return false;
558 case CLASS2 (rvc_normal, rvc_zero):
559 case CLASS2 (rvc_inf, rvc_zero):
560 case CLASS2 (rvc_nan, rvc_zero):
561 /* ANY + 0 = ANY. */
562 case CLASS2 (rvc_nan, rvc_normal):
563 case CLASS2 (rvc_nan, rvc_inf):
564 /* NaN + ANY = NaN. */
565 case CLASS2 (rvc_inf, rvc_normal):
566 /* Inf + R = Inf. */
567 *r = *a;
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 r->sign = sign;
691 return false;
693 case CLASS2 (rvc_nan, rvc_zero):
694 case CLASS2 (rvc_nan, rvc_normal):
695 case CLASS2 (rvc_nan, rvc_inf):
696 /* NaN * ANY = NaN. */
697 *r = *a;
698 r->sign = sign;
699 return false;
701 case CLASS2 (rvc_zero, rvc_inf):
702 case CLASS2 (rvc_inf, rvc_zero):
703 /* 0 * Inf = NaN */
704 get_canonical_qnan (r, sign);
705 return false;
707 case CLASS2 (rvc_inf, rvc_inf):
708 case CLASS2 (rvc_normal, rvc_inf):
709 case CLASS2 (rvc_inf, rvc_normal):
710 /* Inf * Inf = Inf, R * Inf = Inf */
711 get_inf (r, sign);
712 return false;
714 case CLASS2 (rvc_normal, rvc_normal):
715 break;
717 default:
718 gcc_unreachable ();
721 if (r == a || r == b)
722 rr = &t;
723 else
724 rr = r;
725 get_zero (rr, 0);
727 /* Collect all the partial products. Since we don't have sure access
728 to a widening multiply, we split each long into two half-words.
730 Consider the long-hand form of a four half-word multiplication:
732 A B C D
733 * E F G H
734 --------------
735 DE DF DG DH
736 CE CF CG CH
737 BE BF BG BH
738 AE AF AG AH
740 We construct partial products of the widened half-word products
741 that are known to not overlap, e.g. DF+DH. Each such partial
742 product is given its proper exponent, which allows us to sum them
743 and obtain the finished product. */
745 for (i = 0; i < SIGSZ * 2; ++i)
747 unsigned long ai = a->sig[i / 2];
748 if (i & 1)
749 ai >>= HOST_BITS_PER_LONG / 2;
750 else
751 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
753 if (ai == 0)
754 continue;
756 for (j = 0; j < 2; ++j)
758 int exp = (REAL_EXP (a) - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
759 + (REAL_EXP (b) - (1-j)*(HOST_BITS_PER_LONG/2)));
761 if (exp > MAX_EXP)
763 get_inf (r, sign);
764 return true;
766 if (exp < -MAX_EXP)
768 /* Would underflow to zero, which we shouldn't bother adding. */
769 inexact = true;
770 continue;
773 memset (&u, 0, sizeof (u));
774 u.cl = rvc_normal;
775 SET_REAL_EXP (&u, exp);
777 for (k = j; k < SIGSZ * 2; k += 2)
779 unsigned long bi = b->sig[k / 2];
780 if (k & 1)
781 bi >>= HOST_BITS_PER_LONG / 2;
782 else
783 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
785 u.sig[k / 2] = ai * bi;
788 normalize (&u);
789 inexact |= do_add (rr, rr, &u, 0);
793 rr->sign = sign;
794 if (rr != r)
795 *r = t;
797 return inexact;
800 /* Calculate R = A / B. Return true if the result may be inexact. */
802 static bool
803 do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
804 const REAL_VALUE_TYPE *b)
806 int exp, sign = a->sign ^ b->sign;
807 REAL_VALUE_TYPE t, *rr;
808 bool inexact;
810 switch (CLASS2 (a->cl, b->cl))
812 case CLASS2 (rvc_zero, rvc_zero):
813 /* 0 / 0 = NaN. */
814 case CLASS2 (rvc_inf, rvc_inf):
815 /* Inf / Inf = NaN. */
816 get_canonical_qnan (r, sign);
817 return false;
819 case CLASS2 (rvc_zero, rvc_normal):
820 case CLASS2 (rvc_zero, rvc_inf):
821 /* 0 / ANY = 0. */
822 case CLASS2 (rvc_normal, rvc_inf):
823 /* R / Inf = 0. */
824 get_zero (r, sign);
825 return false;
827 case CLASS2 (rvc_normal, rvc_zero):
828 /* R / 0 = Inf. */
829 case CLASS2 (rvc_inf, rvc_zero):
830 /* Inf / 0 = Inf. */
831 get_inf (r, sign);
832 return false;
834 case CLASS2 (rvc_zero, rvc_nan):
835 case CLASS2 (rvc_normal, rvc_nan):
836 case CLASS2 (rvc_inf, rvc_nan):
837 case CLASS2 (rvc_nan, rvc_nan):
838 /* ANY / NaN = NaN. */
839 *r = *b;
840 r->sign = sign;
841 return false;
843 case CLASS2 (rvc_nan, rvc_zero):
844 case CLASS2 (rvc_nan, rvc_normal):
845 case CLASS2 (rvc_nan, rvc_inf):
846 /* NaN / ANY = NaN. */
847 *r = *a;
848 r->sign = sign;
849 return false;
851 case CLASS2 (rvc_inf, rvc_normal):
852 /* Inf / R = Inf. */
853 get_inf (r, sign);
854 return false;
856 case CLASS2 (rvc_normal, rvc_normal):
857 break;
859 default:
860 gcc_unreachable ();
863 if (r == a || r == b)
864 rr = &t;
865 else
866 rr = r;
868 /* Make sure all fields in the result are initialized. */
869 get_zero (rr, 0);
870 rr->cl = rvc_normal;
871 rr->sign = sign;
873 exp = REAL_EXP (a) - REAL_EXP (b) + 1;
874 if (exp > MAX_EXP)
876 get_inf (r, sign);
877 return true;
879 if (exp < -MAX_EXP)
881 get_zero (r, sign);
882 return true;
884 SET_REAL_EXP (rr, exp);
886 inexact = div_significands (rr, a, b);
888 /* Re-normalize the result. */
889 normalize (rr);
890 rr->sig[0] |= inexact;
892 if (rr != r)
893 *r = t;
895 return inexact;
898 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
899 one of the two operands is a NaN. */
901 static int
902 do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
903 int nan_result)
905 int ret;
907 switch (CLASS2 (a->cl, b->cl))
909 case CLASS2 (rvc_zero, rvc_zero):
910 /* Sign of zero doesn't matter for compares. */
911 return 0;
913 case CLASS2 (rvc_inf, rvc_zero):
914 case CLASS2 (rvc_inf, rvc_normal):
915 case CLASS2 (rvc_normal, rvc_zero):
916 return (a->sign ? -1 : 1);
918 case CLASS2 (rvc_inf, rvc_inf):
919 return -a->sign - -b->sign;
921 case CLASS2 (rvc_zero, rvc_normal):
922 case CLASS2 (rvc_zero, rvc_inf):
923 case CLASS2 (rvc_normal, rvc_inf):
924 return (b->sign ? 1 : -1);
926 case CLASS2 (rvc_zero, rvc_nan):
927 case CLASS2 (rvc_normal, rvc_nan):
928 case CLASS2 (rvc_inf, rvc_nan):
929 case CLASS2 (rvc_nan, rvc_nan):
930 case CLASS2 (rvc_nan, rvc_zero):
931 case CLASS2 (rvc_nan, rvc_normal):
932 case CLASS2 (rvc_nan, rvc_inf):
933 return nan_result;
935 case CLASS2 (rvc_normal, rvc_normal):
936 break;
938 default:
939 gcc_unreachable ();
942 if (a->sign != b->sign)
943 return -a->sign - -b->sign;
945 if (a->decimal || b->decimal)
946 return decimal_do_compare (a, b, nan_result);
948 if (REAL_EXP (a) > REAL_EXP (b))
949 ret = 1;
950 else if (REAL_EXP (a) < REAL_EXP (b))
951 ret = -1;
952 else
953 ret = cmp_significands (a, b);
955 return (a->sign ? -ret : ret);
958 /* Return A truncated to an integral value toward zero. */
960 static void
961 do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
963 *r = *a;
965 switch (r->cl)
967 case rvc_zero:
968 case rvc_inf:
969 case rvc_nan:
970 break;
972 case rvc_normal:
973 if (r->decimal)
975 decimal_do_fix_trunc (r, a);
976 return;
978 if (REAL_EXP (r) <= 0)
979 get_zero (r, r->sign);
980 else if (REAL_EXP (r) < SIGNIFICAND_BITS)
981 clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r));
982 break;
984 default:
985 gcc_unreachable ();
989 /* Perform the binary or unary operation described by CODE.
990 For a unary operation, leave OP1 NULL. This function returns
991 true if the result may be inexact due to loss of precision. */
993 bool
994 real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0,
995 const REAL_VALUE_TYPE *op1)
997 enum tree_code code = icode;
999 if (op0->decimal || (op1 && op1->decimal))
1000 return decimal_real_arithmetic (r, icode, op0, op1);
1002 switch (code)
1004 case PLUS_EXPR:
1005 return do_add (r, op0, op1, 0);
1007 case MINUS_EXPR:
1008 return do_add (r, op0, op1, 1);
1010 case MULT_EXPR:
1011 return do_multiply (r, op0, op1);
1013 case RDIV_EXPR:
1014 return do_divide (r, op0, op1);
1016 case MIN_EXPR:
1017 if (op1->cl == rvc_nan)
1018 *r = *op1;
1019 else if (do_compare (op0, op1, -1) < 0)
1020 *r = *op0;
1021 else
1022 *r = *op1;
1023 break;
1025 case MAX_EXPR:
1026 if (op1->cl == rvc_nan)
1027 *r = *op1;
1028 else if (do_compare (op0, op1, 1) < 0)
1029 *r = *op1;
1030 else
1031 *r = *op0;
1032 break;
1034 case NEGATE_EXPR:
1035 *r = *op0;
1036 r->sign ^= 1;
1037 break;
1039 case ABS_EXPR:
1040 *r = *op0;
1041 r->sign = 0;
1042 break;
1044 case FIX_TRUNC_EXPR:
1045 do_fix_trunc (r, op0);
1046 break;
1048 default:
1049 gcc_unreachable ();
1051 return false;
1054 /* Legacy. Similar, but return the result directly. */
1056 REAL_VALUE_TYPE
1057 real_arithmetic2 (int icode, const REAL_VALUE_TYPE *op0,
1058 const REAL_VALUE_TYPE *op1)
1060 REAL_VALUE_TYPE r;
1061 real_arithmetic (&r, icode, op0, op1);
1062 return r;
1065 bool
1066 real_compare (int icode, const REAL_VALUE_TYPE *op0,
1067 const REAL_VALUE_TYPE *op1)
1069 enum tree_code code = icode;
1071 switch (code)
1073 case LT_EXPR:
1074 return do_compare (op0, op1, 1) < 0;
1075 case LE_EXPR:
1076 return do_compare (op0, op1, 1) <= 0;
1077 case GT_EXPR:
1078 return do_compare (op0, op1, -1) > 0;
1079 case GE_EXPR:
1080 return do_compare (op0, op1, -1) >= 0;
1081 case EQ_EXPR:
1082 return do_compare (op0, op1, -1) == 0;
1083 case NE_EXPR:
1084 return do_compare (op0, op1, -1) != 0;
1085 case UNORDERED_EXPR:
1086 return op0->cl == rvc_nan || op1->cl == rvc_nan;
1087 case ORDERED_EXPR:
1088 return op0->cl != rvc_nan && op1->cl != rvc_nan;
1089 case UNLT_EXPR:
1090 return do_compare (op0, op1, -1) < 0;
1091 case UNLE_EXPR:
1092 return do_compare (op0, op1, -1) <= 0;
1093 case UNGT_EXPR:
1094 return do_compare (op0, op1, 1) > 0;
1095 case UNGE_EXPR:
1096 return do_compare (op0, op1, 1) >= 0;
1097 case UNEQ_EXPR:
1098 return do_compare (op0, op1, 0) == 0;
1099 case LTGT_EXPR:
1100 return do_compare (op0, op1, 0) != 0;
1102 default:
1103 gcc_unreachable ();
1107 /* Return floor log2(R). */
1110 real_exponent (const REAL_VALUE_TYPE *r)
1112 switch (r->cl)
1114 case rvc_zero:
1115 return 0;
1116 case rvc_inf:
1117 case rvc_nan:
1118 return (unsigned int)-1 >> 1;
1119 case rvc_normal:
1120 return REAL_EXP (r);
1121 default:
1122 gcc_unreachable ();
1126 /* R = OP0 * 2**EXP. */
1128 void
1129 real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1131 *r = *op0;
1132 switch (r->cl)
1134 case rvc_zero:
1135 case rvc_inf:
1136 case rvc_nan:
1137 break;
1139 case rvc_normal:
1140 exp += REAL_EXP (op0);
1141 if (exp > MAX_EXP)
1142 get_inf (r, r->sign);
1143 else if (exp < -MAX_EXP)
1144 get_zero (r, r->sign);
1145 else
1146 SET_REAL_EXP (r, exp);
1147 break;
1149 default:
1150 gcc_unreachable ();
1154 /* Determine whether a floating-point value X is infinite. */
1156 bool
1157 real_isinf (const REAL_VALUE_TYPE *r)
1159 return (r->cl == rvc_inf);
1162 /* Determine whether a floating-point value X is a NaN. */
1164 bool
1165 real_isnan (const REAL_VALUE_TYPE *r)
1167 return (r->cl == rvc_nan);
1170 /* Determine whether a floating-point value X is negative. */
1172 bool
1173 real_isneg (const REAL_VALUE_TYPE *r)
1175 return r->sign;
1178 /* Determine whether a floating-point value X is minus zero. */
1180 bool
1181 real_isnegzero (const REAL_VALUE_TYPE *r)
1183 return r->sign && r->cl == rvc_zero;
1186 /* Compare two floating-point objects for bitwise identity. */
1188 bool
1189 real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1191 int i;
1193 if (a->cl != b->cl)
1194 return false;
1195 if (a->sign != b->sign)
1196 return false;
1198 switch (a->cl)
1200 case rvc_zero:
1201 case rvc_inf:
1202 return true;
1204 case rvc_normal:
1205 if (a->decimal != b->decimal)
1206 return false;
1207 if (REAL_EXP (a) != REAL_EXP (b))
1208 return false;
1209 break;
1211 case rvc_nan:
1212 if (a->signalling != b->signalling)
1213 return false;
1214 /* The significand is ignored for canonical NaNs. */
1215 if (a->canonical || b->canonical)
1216 return a->canonical == b->canonical;
1217 break;
1219 default:
1220 gcc_unreachable ();
1223 for (i = 0; i < SIGSZ; ++i)
1224 if (a->sig[i] != b->sig[i])
1225 return false;
1227 return true;
1230 /* Try to change R into its exact multiplicative inverse in machine
1231 mode MODE. Return true if successful. */
1233 bool
1234 exact_real_inverse (enum machine_mode mode, REAL_VALUE_TYPE *r)
1236 const REAL_VALUE_TYPE *one = real_digit (1);
1237 REAL_VALUE_TYPE u;
1238 int i;
1240 if (r->cl != rvc_normal)
1241 return false;
1243 /* Check for a power of two: all significand bits zero except the MSB. */
1244 for (i = 0; i < SIGSZ-1; ++i)
1245 if (r->sig[i] != 0)
1246 return false;
1247 if (r->sig[SIGSZ-1] != SIG_MSB)
1248 return false;
1250 /* Find the inverse and truncate to the required mode. */
1251 do_divide (&u, one, r);
1252 real_convert (&u, mode, &u);
1254 /* The rounding may have overflowed. */
1255 if (u.cl != rvc_normal)
1256 return false;
1257 for (i = 0; i < SIGSZ-1; ++i)
1258 if (u.sig[i] != 0)
1259 return false;
1260 if (u.sig[SIGSZ-1] != SIG_MSB)
1261 return false;
1263 *r = u;
1264 return true;
1267 /* Render R as an integer. */
1269 HOST_WIDE_INT
1270 real_to_integer (const REAL_VALUE_TYPE *r)
1272 unsigned HOST_WIDE_INT i;
1274 switch (r->cl)
1276 case rvc_zero:
1277 underflow:
1278 return 0;
1280 case rvc_inf:
1281 case rvc_nan:
1282 overflow:
1283 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1284 if (!r->sign)
1285 i--;
1286 return i;
1288 case rvc_normal:
1289 if (r->decimal)
1290 return decimal_real_to_integer (r);
1292 if (REAL_EXP (r) <= 0)
1293 goto underflow;
1294 /* Only force overflow for unsigned overflow. Signed overflow is
1295 undefined, so it doesn't matter what we return, and some callers
1296 expect to be able to use this routine for both signed and
1297 unsigned conversions. */
1298 if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1299 goto overflow;
1301 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1302 i = r->sig[SIGSZ-1];
1303 else
1305 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1306 i = r->sig[SIGSZ-1];
1307 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1308 i |= r->sig[SIGSZ-2];
1311 i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1313 if (r->sign)
1314 i = -i;
1315 return i;
1317 default:
1318 gcc_unreachable ();
1322 /* Likewise, but to an integer pair, HI+LOW. */
1324 void
1325 real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
1326 const REAL_VALUE_TYPE *r)
1328 REAL_VALUE_TYPE t;
1329 HOST_WIDE_INT low, high;
1330 int exp;
1332 switch (r->cl)
1334 case rvc_zero:
1335 underflow:
1336 low = high = 0;
1337 break;
1339 case rvc_inf:
1340 case rvc_nan:
1341 overflow:
1342 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1343 if (r->sign)
1344 low = 0;
1345 else
1347 high--;
1348 low = -1;
1350 break;
1352 case rvc_normal:
1353 if (r->decimal)
1355 decimal_real_to_integer2 (plow, phigh, r);
1356 return;
1359 exp = REAL_EXP (r);
1360 if (exp <= 0)
1361 goto underflow;
1362 /* Only force overflow for unsigned overflow. Signed overflow is
1363 undefined, so it doesn't matter what we return, and some callers
1364 expect to be able to use this routine for both signed and
1365 unsigned conversions. */
1366 if (exp > 2*HOST_BITS_PER_WIDE_INT)
1367 goto overflow;
1369 rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
1370 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1372 high = t.sig[SIGSZ-1];
1373 low = t.sig[SIGSZ-2];
1375 else
1377 gcc_assert (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG);
1378 high = t.sig[SIGSZ-1];
1379 high = high << (HOST_BITS_PER_LONG - 1) << 1;
1380 high |= t.sig[SIGSZ-2];
1382 low = t.sig[SIGSZ-3];
1383 low = low << (HOST_BITS_PER_LONG - 1) << 1;
1384 low |= t.sig[SIGSZ-4];
1387 if (r->sign)
1389 if (low == 0)
1390 high = -high;
1391 else
1392 low = -low, high = ~high;
1394 break;
1396 default:
1397 gcc_unreachable ();
1400 *plow = low;
1401 *phigh = high;
1404 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1405 of NUM / DEN. Return the quotient and place the remainder in NUM.
1406 It is expected that NUM / DEN are close enough that the quotient is
1407 small. */
1409 static unsigned long
1410 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1412 unsigned long q, msb;
1413 int expn = REAL_EXP (num), expd = REAL_EXP (den);
1415 if (expn < expd)
1416 return 0;
1418 q = msb = 0;
1419 goto start;
1422 msb = num->sig[SIGSZ-1] & SIG_MSB;
1423 q <<= 1;
1424 lshift_significand_1 (num, num);
1425 start:
1426 if (msb || cmp_significands (num, den) >= 0)
1428 sub_significands (num, num, den, 0);
1429 q |= 1;
1432 while (--expn >= expd);
1434 SET_REAL_EXP (num, expd);
1435 normalize (num);
1437 return q;
1440 /* Render R as a decimal floating point constant. Emit DIGITS significant
1441 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1442 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1443 zeros. */
1445 #define M_LOG10_2 0.30102999566398119521
1447 void
1448 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1449 size_t digits, int crop_trailing_zeros)
1451 const REAL_VALUE_TYPE *one, *ten;
1452 REAL_VALUE_TYPE r, pten, u, v;
1453 int dec_exp, cmp_one, digit;
1454 size_t max_digits;
1455 char *p, *first, *last;
1456 bool sign;
1458 r = *r_orig;
1459 switch (r.cl)
1461 case rvc_zero:
1462 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1463 return;
1464 case rvc_normal:
1465 break;
1466 case rvc_inf:
1467 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1468 return;
1469 case rvc_nan:
1470 /* ??? Print the significand as well, if not canonical? */
1471 strcpy (str, (r.sign ? "-NaN" : "+NaN"));
1472 return;
1473 default:
1474 gcc_unreachable ();
1477 if (r.decimal)
1479 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1480 return;
1483 /* Bound the number of digits printed by the size of the representation. */
1484 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1485 if (digits == 0 || digits > max_digits)
1486 digits = max_digits;
1488 /* Estimate the decimal exponent, and compute the length of the string it
1489 will print as. Be conservative and add one to account for possible
1490 overflow or rounding error. */
1491 dec_exp = REAL_EXP (&r) * M_LOG10_2;
1492 for (max_digits = 1; dec_exp ; max_digits++)
1493 dec_exp /= 10;
1495 /* Bound the number of digits printed by the size of the output buffer. */
1496 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1497 gcc_assert (max_digits <= buf_size);
1498 if (digits > max_digits)
1499 digits = max_digits;
1501 one = real_digit (1);
1502 ten = ten_to_ptwo (0);
1504 sign = r.sign;
1505 r.sign = 0;
1507 dec_exp = 0;
1508 pten = *one;
1510 cmp_one = do_compare (&r, one, 0);
1511 if (cmp_one > 0)
1513 int m;
1515 /* Number is greater than one. Convert significand to an integer
1516 and strip trailing decimal zeros. */
1518 u = r;
1519 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1521 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1522 m = floor_log2 (max_digits);
1524 /* Iterate over the bits of the possible powers of 10 that might
1525 be present in U and eliminate them. That is, if we find that
1526 10**2**M divides U evenly, keep the division and increase
1527 DEC_EXP by 2**M. */
1530 REAL_VALUE_TYPE t;
1532 do_divide (&t, &u, ten_to_ptwo (m));
1533 do_fix_trunc (&v, &t);
1534 if (cmp_significands (&v, &t) == 0)
1536 u = t;
1537 dec_exp += 1 << m;
1540 while (--m >= 0);
1542 /* Revert the scaling to integer that we performed earlier. */
1543 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1544 - (SIGNIFICAND_BITS - 1));
1545 r = u;
1547 /* Find power of 10. Do this by dividing out 10**2**M when
1548 this is larger than the current remainder. Fill PTEN with
1549 the power of 10 that we compute. */
1550 if (REAL_EXP (&r) > 0)
1552 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1555 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1556 if (do_compare (&u, ptentwo, 0) >= 0)
1558 do_divide (&u, &u, ptentwo);
1559 do_multiply (&pten, &pten, ptentwo);
1560 dec_exp += 1 << m;
1563 while (--m >= 0);
1565 else
1566 /* We managed to divide off enough tens in the above reduction
1567 loop that we've now got a negative exponent. Fall into the
1568 less-than-one code to compute the proper value for PTEN. */
1569 cmp_one = -1;
1571 if (cmp_one < 0)
1573 int m;
1575 /* Number is less than one. Pad significand with leading
1576 decimal zeros. */
1578 v = r;
1579 while (1)
1581 /* Stop if we'd shift bits off the bottom. */
1582 if (v.sig[0] & 7)
1583 break;
1585 do_multiply (&u, &v, ten);
1587 /* Stop if we're now >= 1. */
1588 if (REAL_EXP (&u) > 0)
1589 break;
1591 v = u;
1592 dec_exp -= 1;
1594 r = v;
1596 /* Find power of 10. Do this by multiplying in P=10**2**M when
1597 the current remainder is smaller than 1/P. Fill PTEN with the
1598 power of 10 that we compute. */
1599 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1602 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1603 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1605 if (do_compare (&v, ptenmtwo, 0) <= 0)
1607 do_multiply (&v, &v, ptentwo);
1608 do_multiply (&pten, &pten, ptentwo);
1609 dec_exp -= 1 << m;
1612 while (--m >= 0);
1614 /* Invert the positive power of 10 that we've collected so far. */
1615 do_divide (&pten, one, &pten);
1618 p = str;
1619 if (sign)
1620 *p++ = '-';
1621 first = p++;
1623 /* At this point, PTEN should contain the nearest power of 10 smaller
1624 than R, such that this division produces the first digit.
1626 Using a divide-step primitive that returns the complete integral
1627 remainder avoids the rounding error that would be produced if
1628 we were to use do_divide here and then simply multiply by 10 for
1629 each subsequent digit. */
1631 digit = rtd_divmod (&r, &pten);
1633 /* Be prepared for error in that division via underflow ... */
1634 if (digit == 0 && cmp_significand_0 (&r))
1636 /* Multiply by 10 and try again. */
1637 do_multiply (&r, &r, ten);
1638 digit = rtd_divmod (&r, &pten);
1639 dec_exp -= 1;
1640 gcc_assert (digit != 0);
1643 /* ... or overflow. */
1644 if (digit == 10)
1646 *p++ = '1';
1647 if (--digits > 0)
1648 *p++ = '0';
1649 dec_exp += 1;
1651 else
1653 gcc_assert (digit <= 10);
1654 *p++ = digit + '0';
1657 /* Generate subsequent digits. */
1658 while (--digits > 0)
1660 do_multiply (&r, &r, ten);
1661 digit = rtd_divmod (&r, &pten);
1662 *p++ = digit + '0';
1664 last = p;
1666 /* Generate one more digit with which to do rounding. */
1667 do_multiply (&r, &r, ten);
1668 digit = rtd_divmod (&r, &pten);
1670 /* Round the result. */
1671 if (digit == 5)
1673 /* Round to nearest. If R is nonzero there are additional
1674 nonzero digits to be extracted. */
1675 if (cmp_significand_0 (&r))
1676 digit++;
1677 /* Round to even. */
1678 else if ((p[-1] - '0') & 1)
1679 digit++;
1681 if (digit > 5)
1683 while (p > first)
1685 digit = *--p;
1686 if (digit == '9')
1687 *p = '0';
1688 else
1690 *p = digit + 1;
1691 break;
1695 /* Carry out of the first digit. This means we had all 9's and
1696 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1697 if (p == first)
1699 first[1] = '1';
1700 dec_exp++;
1704 /* Insert the decimal point. */
1705 first[0] = first[1];
1706 first[1] = '.';
1708 /* If requested, drop trailing zeros. Never crop past "1.0". */
1709 if (crop_trailing_zeros)
1710 while (last > first + 3 && last[-1] == '0')
1711 last--;
1713 /* Append the exponent. */
1714 sprintf (last, "e%+d", dec_exp);
1717 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1718 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1719 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1720 strip trailing zeros. */
1722 void
1723 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1724 size_t digits, int crop_trailing_zeros)
1726 int i, j, exp = REAL_EXP (r);
1727 char *p, *first;
1728 char exp_buf[16];
1729 size_t max_digits;
1731 switch (r->cl)
1733 case rvc_zero:
1734 exp = 0;
1735 break;
1736 case rvc_normal:
1737 break;
1738 case rvc_inf:
1739 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1740 return;
1741 case rvc_nan:
1742 /* ??? Print the significand as well, if not canonical? */
1743 strcpy (str, (r->sign ? "-NaN" : "+NaN"));
1744 return;
1745 default:
1746 gcc_unreachable ();
1749 if (r->decimal)
1751 /* Hexadecimal format for decimal floats is not interesting. */
1752 strcpy (str, "N/A");
1753 return;
1756 if (digits == 0)
1757 digits = SIGNIFICAND_BITS / 4;
1759 /* Bound the number of digits printed by the size of the output buffer. */
1761 sprintf (exp_buf, "p%+d", exp);
1762 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1763 gcc_assert (max_digits <= buf_size);
1764 if (digits > max_digits)
1765 digits = max_digits;
1767 p = str;
1768 if (r->sign)
1769 *p++ = '-';
1770 *p++ = '0';
1771 *p++ = 'x';
1772 *p++ = '0';
1773 *p++ = '.';
1774 first = p;
1776 for (i = SIGSZ - 1; i >= 0; --i)
1777 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1779 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1780 if (--digits == 0)
1781 goto out;
1784 out:
1785 if (crop_trailing_zeros)
1786 while (p > first + 1 && p[-1] == '0')
1787 p--;
1789 sprintf (p, "p%+d", exp);
1792 /* Initialize R from a decimal or hexadecimal string. The string is
1793 assumed to have been syntax checked already. */
1795 void
1796 real_from_string (REAL_VALUE_TYPE *r, const char *str)
1798 int exp = 0;
1799 bool sign = false;
1801 get_zero (r, 0);
1803 if (*str == '-')
1805 sign = true;
1806 str++;
1808 else if (*str == '+')
1809 str++;
1811 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1813 /* Hexadecimal floating point. */
1814 int pos = SIGNIFICAND_BITS - 4, d;
1816 str += 2;
1818 while (*str == '0')
1819 str++;
1820 while (1)
1822 d = hex_value (*str);
1823 if (d == _hex_bad)
1824 break;
1825 if (pos >= 0)
1827 r->sig[pos / HOST_BITS_PER_LONG]
1828 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1829 pos -= 4;
1831 else if (d)
1832 /* Ensure correct rounding by setting last bit if there is
1833 a subsequent nonzero digit. */
1834 r->sig[0] |= 1;
1835 exp += 4;
1836 str++;
1838 if (*str == '.')
1840 str++;
1841 if (pos == SIGNIFICAND_BITS - 4)
1843 while (*str == '0')
1844 str++, exp -= 4;
1846 while (1)
1848 d = hex_value (*str);
1849 if (d == _hex_bad)
1850 break;
1851 if (pos >= 0)
1853 r->sig[pos / HOST_BITS_PER_LONG]
1854 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1855 pos -= 4;
1857 else if (d)
1858 /* Ensure correct rounding by setting last bit if there is
1859 a subsequent nonzero digit. */
1860 r->sig[0] |= 1;
1861 str++;
1865 /* If the mantissa is zero, ignore the exponent. */
1866 if (!cmp_significand_0 (r))
1867 goto underflow;
1869 if (*str == 'p' || *str == 'P')
1871 bool exp_neg = false;
1873 str++;
1874 if (*str == '-')
1876 exp_neg = true;
1877 str++;
1879 else if (*str == '+')
1880 str++;
1882 d = 0;
1883 while (ISDIGIT (*str))
1885 d *= 10;
1886 d += *str - '0';
1887 if (d > MAX_EXP)
1889 /* Overflowed the exponent. */
1890 if (exp_neg)
1891 goto underflow;
1892 else
1893 goto overflow;
1895 str++;
1897 if (exp_neg)
1898 d = -d;
1900 exp += d;
1903 r->cl = rvc_normal;
1904 SET_REAL_EXP (r, exp);
1906 normalize (r);
1908 else
1910 /* Decimal floating point. */
1911 const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
1912 int d;
1914 while (*str == '0')
1915 str++;
1916 while (ISDIGIT (*str))
1918 d = *str++ - '0';
1919 do_multiply (r, r, ten);
1920 if (d)
1921 do_add (r, r, real_digit (d), 0);
1923 if (*str == '.')
1925 str++;
1926 if (r->cl == rvc_zero)
1928 while (*str == '0')
1929 str++, exp--;
1931 while (ISDIGIT (*str))
1933 d = *str++ - '0';
1934 do_multiply (r, r, ten);
1935 if (d)
1936 do_add (r, r, real_digit (d), 0);
1937 exp--;
1941 /* If the mantissa is zero, ignore the exponent. */
1942 if (r->cl == rvc_zero)
1943 goto underflow;
1945 if (*str == 'e' || *str == 'E')
1947 bool exp_neg = false;
1949 str++;
1950 if (*str == '-')
1952 exp_neg = true;
1953 str++;
1955 else if (*str == '+')
1956 str++;
1958 d = 0;
1959 while (ISDIGIT (*str))
1961 d *= 10;
1962 d += *str - '0';
1963 if (d > MAX_EXP)
1965 /* Overflowed the exponent. */
1966 if (exp_neg)
1967 goto underflow;
1968 else
1969 goto overflow;
1971 str++;
1973 if (exp_neg)
1974 d = -d;
1975 exp += d;
1978 if (exp)
1979 times_pten (r, exp);
1982 r->sign = sign;
1983 return;
1985 underflow:
1986 get_zero (r, sign);
1987 return;
1989 overflow:
1990 get_inf (r, sign);
1991 return;
1994 /* Legacy. Similar, but return the result directly. */
1996 REAL_VALUE_TYPE
1997 real_from_string2 (const char *s, enum machine_mode mode)
1999 REAL_VALUE_TYPE r;
2001 real_from_string (&r, s);
2002 if (mode != VOIDmode)
2003 real_convert (&r, mode, &r);
2005 return r;
2008 /* Initialize R from string S and desired MODE. */
2010 void
2011 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, enum machine_mode mode)
2013 if (DECIMAL_FLOAT_MODE_P (mode))
2014 decimal_real_from_string (r, s);
2015 else
2016 real_from_string (r, s);
2018 if (mode != VOIDmode)
2019 real_convert (r, mode, r);
2022 /* Initialize R from the integer pair HIGH+LOW. */
2024 void
2025 real_from_integer (REAL_VALUE_TYPE *r, enum machine_mode mode,
2026 unsigned HOST_WIDE_INT low, HOST_WIDE_INT high,
2027 int unsigned_p)
2029 if (low == 0 && high == 0)
2030 get_zero (r, 0);
2031 else
2033 memset (r, 0, sizeof (*r));
2034 r->cl = rvc_normal;
2035 r->sign = high < 0 && !unsigned_p;
2036 SET_REAL_EXP (r, 2 * HOST_BITS_PER_WIDE_INT);
2038 if (r->sign)
2040 high = ~high;
2041 if (low == 0)
2042 high += 1;
2043 else
2044 low = -low;
2047 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2049 r->sig[SIGSZ-1] = high;
2050 r->sig[SIGSZ-2] = low;
2052 else
2054 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2055 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
2056 r->sig[SIGSZ-2] = high;
2057 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
2058 r->sig[SIGSZ-4] = low;
2061 normalize (r);
2064 if (mode != VOIDmode)
2065 real_convert (r, mode, r);
2068 /* Returns 10**2**N. */
2070 static const REAL_VALUE_TYPE *
2071 ten_to_ptwo (int n)
2073 static REAL_VALUE_TYPE tens[EXP_BITS];
2075 gcc_assert (n >= 0);
2076 gcc_assert (n < EXP_BITS);
2078 if (tens[n].cl == rvc_zero)
2080 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2082 HOST_WIDE_INT t = 10;
2083 int i;
2085 for (i = 0; i < n; ++i)
2086 t *= t;
2088 real_from_integer (&tens[n], VOIDmode, t, 0, 1);
2090 else
2092 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2093 do_multiply (&tens[n], t, t);
2097 return &tens[n];
2100 /* Returns 10**(-2**N). */
2102 static const REAL_VALUE_TYPE *
2103 ten_to_mptwo (int n)
2105 static REAL_VALUE_TYPE tens[EXP_BITS];
2107 gcc_assert (n >= 0);
2108 gcc_assert (n < EXP_BITS);
2110 if (tens[n].cl == rvc_zero)
2111 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2113 return &tens[n];
2116 /* Returns N. */
2118 static const REAL_VALUE_TYPE *
2119 real_digit (int n)
2121 static REAL_VALUE_TYPE num[10];
2123 gcc_assert (n >= 0);
2124 gcc_assert (n <= 9);
2126 if (n > 0 && num[n].cl == rvc_zero)
2127 real_from_integer (&num[n], VOIDmode, n, 0, 1);
2129 return &num[n];
2132 /* Multiply R by 10**EXP. */
2134 static void
2135 times_pten (REAL_VALUE_TYPE *r, int exp)
2137 REAL_VALUE_TYPE pten, *rr;
2138 bool negative = (exp < 0);
2139 int i;
2141 if (negative)
2143 exp = -exp;
2144 pten = *real_digit (1);
2145 rr = &pten;
2147 else
2148 rr = r;
2150 for (i = 0; exp > 0; ++i, exp >>= 1)
2151 if (exp & 1)
2152 do_multiply (rr, rr, ten_to_ptwo (i));
2154 if (negative)
2155 do_divide (r, r, &pten);
2158 /* Fills R with +Inf. */
2160 void
2161 real_inf (REAL_VALUE_TYPE *r)
2163 get_inf (r, 0);
2166 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2167 we force a QNaN, else we force an SNaN. The string, if not empty,
2168 is parsed as a number and placed in the significand. Return true
2169 if the string was successfully parsed. */
2171 bool
2172 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2173 enum machine_mode mode)
2175 const struct real_format *fmt;
2177 fmt = REAL_MODE_FORMAT (mode);
2178 gcc_assert (fmt);
2180 if (*str == 0)
2182 if (quiet)
2183 get_canonical_qnan (r, 0);
2184 else
2185 get_canonical_snan (r, 0);
2187 else
2189 int base = 10, d;
2191 memset (r, 0, sizeof (*r));
2192 r->cl = rvc_nan;
2194 /* Parse akin to strtol into the significand of R. */
2196 while (ISSPACE (*str))
2197 str++;
2198 if (*str == '-')
2199 str++;
2200 else if (*str == '+')
2201 str++;
2202 if (*str == '0')
2204 str++;
2205 if (*str == 'x' || *str == 'X')
2207 base = 16;
2208 str++;
2210 else
2211 base = 8;
2214 while ((d = hex_value (*str)) < base)
2216 REAL_VALUE_TYPE u;
2218 switch (base)
2220 case 8:
2221 lshift_significand (r, r, 3);
2222 break;
2223 case 16:
2224 lshift_significand (r, r, 4);
2225 break;
2226 case 10:
2227 lshift_significand_1 (&u, r);
2228 lshift_significand (r, r, 3);
2229 add_significands (r, r, &u);
2230 break;
2231 default:
2232 gcc_unreachable ();
2235 get_zero (&u, 0);
2236 u.sig[0] = d;
2237 add_significands (r, r, &u);
2239 str++;
2242 /* Must have consumed the entire string for success. */
2243 if (*str != 0)
2244 return false;
2246 /* Shift the significand into place such that the bits
2247 are in the most significant bits for the format. */
2248 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2250 /* Our MSB is always unset for NaNs. */
2251 r->sig[SIGSZ-1] &= ~SIG_MSB;
2253 /* Force quiet or signalling NaN. */
2254 r->signalling = !quiet;
2257 return true;
2260 /* Fills R with the largest finite value representable in mode MODE.
2261 If SIGN is nonzero, R is set to the most negative finite value. */
2263 void
2264 real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
2266 const struct real_format *fmt;
2267 int np2;
2269 fmt = REAL_MODE_FORMAT (mode);
2270 gcc_assert (fmt);
2271 memset (r, 0, sizeof (*r));
2273 if (fmt->b == 10)
2274 decimal_real_maxval (r, sign, mode);
2275 else
2277 r->cl = rvc_normal;
2278 r->sign = sign;
2279 SET_REAL_EXP (r, fmt->emax * fmt->log2_b);
2281 np2 = SIGNIFICAND_BITS - fmt->p * fmt->log2_b;
2282 memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2283 clear_significand_below (r, np2);
2285 if (fmt->pnan < fmt->p)
2286 /* This is an IBM extended double format made up of two IEEE
2287 doubles. The value of the long double is the sum of the
2288 values of the two parts. The most significant part is
2289 required to be the value of the long double rounded to the
2290 nearest double. Rounding means we need a slightly smaller
2291 value for LDBL_MAX. */
2292 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan);
2296 /* Fills R with 2**N. */
2298 void
2299 real_2expN (REAL_VALUE_TYPE *r, int n)
2301 memset (r, 0, sizeof (*r));
2303 n++;
2304 if (n > MAX_EXP)
2305 r->cl = rvc_inf;
2306 else if (n < -MAX_EXP)
2308 else
2310 r->cl = rvc_normal;
2311 SET_REAL_EXP (r, n);
2312 r->sig[SIGSZ-1] = SIG_MSB;
2317 static void
2318 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2320 int p2, np2, i, w;
2321 unsigned long sticky;
2322 bool guard, lsb;
2323 int emin2m1, emax2;
2325 if (r->decimal)
2327 if (fmt->b == 10)
2329 decimal_round_for_format (fmt, r);
2330 return;
2332 /* FIXME. We can come here via fp_easy_constant
2333 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2334 investigated whether this convert needs to be here, or
2335 something else is missing. */
2336 decimal_real_convert (r, DFmode, r);
2339 p2 = fmt->p * fmt->log2_b;
2340 emin2m1 = (fmt->emin - 1) * fmt->log2_b;
2341 emax2 = fmt->emax * fmt->log2_b;
2343 np2 = SIGNIFICAND_BITS - p2;
2344 switch (r->cl)
2346 underflow:
2347 get_zero (r, r->sign);
2348 case rvc_zero:
2349 if (!fmt->has_signed_zero)
2350 r->sign = 0;
2351 return;
2353 overflow:
2354 get_inf (r, r->sign);
2355 case rvc_inf:
2356 return;
2358 case rvc_nan:
2359 clear_significand_below (r, np2);
2360 return;
2362 case rvc_normal:
2363 break;
2365 default:
2366 gcc_unreachable ();
2369 /* If we're not base2, normalize the exponent to a multiple of
2370 the true base. */
2371 if (fmt->log2_b != 1)
2373 int shift;
2375 gcc_assert (fmt->b != 10);
2376 shift = REAL_EXP (r) & (fmt->log2_b - 1);
2377 if (shift)
2379 shift = fmt->log2_b - shift;
2380 r->sig[0] |= sticky_rshift_significand (r, r, shift);
2381 SET_REAL_EXP (r, REAL_EXP (r) + shift);
2385 /* Check the range of the exponent. If we're out of range,
2386 either underflow or overflow. */
2387 if (REAL_EXP (r) > emax2)
2388 goto overflow;
2389 else if (REAL_EXP (r) <= emin2m1)
2391 int diff;
2393 if (!fmt->has_denorm)
2395 /* Don't underflow completely until we've had a chance to round. */
2396 if (REAL_EXP (r) < emin2m1)
2397 goto underflow;
2399 else
2401 diff = emin2m1 - REAL_EXP (r) + 1;
2402 if (diff > p2)
2403 goto underflow;
2405 /* De-normalize the significand. */
2406 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2407 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2411 /* There are P2 true significand bits, followed by one guard bit,
2412 followed by one sticky bit, followed by stuff. Fold nonzero
2413 stuff into the sticky bit. */
2415 sticky = 0;
2416 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2417 sticky |= r->sig[i];
2418 sticky |=
2419 r->sig[w] & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2421 guard = test_significand_bit (r, np2 - 1);
2422 lsb = test_significand_bit (r, np2);
2424 /* Round to even. */
2425 if (guard && (sticky || lsb))
2427 REAL_VALUE_TYPE u;
2428 get_zero (&u, 0);
2429 set_significand_bit (&u, np2);
2431 if (add_significands (r, r, &u))
2433 /* Overflow. Means the significand had been all ones, and
2434 is now all zeros. Need to increase the exponent, and
2435 possibly re-normalize it. */
2436 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2437 if (REAL_EXP (r) > emax2)
2438 goto overflow;
2439 r->sig[SIGSZ-1] = SIG_MSB;
2441 if (fmt->log2_b != 1)
2443 int shift = REAL_EXP (r) & (fmt->log2_b - 1);
2444 if (shift)
2446 shift = fmt->log2_b - shift;
2447 rshift_significand (r, r, shift);
2448 SET_REAL_EXP (r, REAL_EXP (r) + shift);
2449 if (REAL_EXP (r) > emax2)
2450 goto overflow;
2456 /* Catch underflow that we deferred until after rounding. */
2457 if (REAL_EXP (r) <= emin2m1)
2458 goto underflow;
2460 /* Clear out trailing garbage. */
2461 clear_significand_below (r, np2);
2464 /* Extend or truncate to a new mode. */
2466 void
2467 real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
2468 const REAL_VALUE_TYPE *a)
2470 const struct real_format *fmt;
2472 fmt = REAL_MODE_FORMAT (mode);
2473 gcc_assert (fmt);
2475 *r = *a;
2477 if (a->decimal || fmt->b == 10)
2478 decimal_real_convert (r, mode, a);
2480 round_for_format (fmt, r);
2482 /* round_for_format de-normalizes denormals. Undo just that part. */
2483 if (r->cl == rvc_normal)
2484 normalize (r);
2487 /* Legacy. Likewise, except return the struct directly. */
2489 REAL_VALUE_TYPE
2490 real_value_truncate (enum machine_mode mode, REAL_VALUE_TYPE a)
2492 REAL_VALUE_TYPE r;
2493 real_convert (&r, mode, &a);
2494 return r;
2497 /* Return true if truncating to MODE is exact. */
2499 bool
2500 exact_real_truncate (enum machine_mode mode, const REAL_VALUE_TYPE *a)
2502 const struct real_format *fmt;
2503 REAL_VALUE_TYPE t;
2504 int emin2m1;
2506 fmt = REAL_MODE_FORMAT (mode);
2507 gcc_assert (fmt);
2509 /* Don't allow conversion to denormals. */
2510 emin2m1 = (fmt->emin - 1) * fmt->log2_b;
2511 if (REAL_EXP (a) <= emin2m1)
2512 return false;
2514 /* After conversion to the new mode, the value must be identical. */
2515 real_convert (&t, mode, a);
2516 return real_identical (&t, a);
2519 /* Write R to the given target format. Place the words of the result
2520 in target word order in BUF. There are always 32 bits in each
2521 long, no matter the size of the host long.
2523 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2525 long
2526 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
2527 const struct real_format *fmt)
2529 REAL_VALUE_TYPE r;
2530 long buf1;
2532 r = *r_orig;
2533 round_for_format (fmt, &r);
2535 if (!buf)
2536 buf = &buf1;
2537 (*fmt->encode) (fmt, buf, &r);
2539 return *buf;
2542 /* Similar, but look up the format from MODE. */
2544 long
2545 real_to_target (long *buf, const REAL_VALUE_TYPE *r, enum machine_mode mode)
2547 const struct real_format *fmt;
2549 fmt = REAL_MODE_FORMAT (mode);
2550 gcc_assert (fmt);
2552 return real_to_target_fmt (buf, r, fmt);
2555 /* Read R from the given target format. Read the words of the result
2556 in target word order in BUF. There are always 32 bits in each
2557 long, no matter the size of the host long. */
2559 void
2560 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
2561 const struct real_format *fmt)
2563 (*fmt->decode) (fmt, r, buf);
2566 /* Similar, but look up the format from MODE. */
2568 void
2569 real_from_target (REAL_VALUE_TYPE *r, const long *buf, enum machine_mode mode)
2571 const struct real_format *fmt;
2573 fmt = REAL_MODE_FORMAT (mode);
2574 gcc_assert (fmt);
2576 (*fmt->decode) (fmt, r, buf);
2579 /* Return the number of bits of the largest binary value that the
2580 significand of MODE will hold. */
2581 /* ??? Legacy. Should get access to real_format directly. */
2584 significand_size (enum machine_mode mode)
2586 const struct real_format *fmt;
2588 fmt = REAL_MODE_FORMAT (mode);
2589 if (fmt == NULL)
2590 return 0;
2592 if (fmt->b == 10)
2594 /* Return the size in bits of the largest binary value that can be
2595 held by the decimal coefficient for this mode. This is one more
2596 than the number of bits required to hold the largest coefficient
2597 of this mode. */
2598 double log2_10 = 3.3219281;
2599 return fmt->p * log2_10;
2601 return fmt->p * fmt->log2_b;
2604 /* Return a hash value for the given real value. */
2605 /* ??? The "unsigned int" return value is intended to be hashval_t,
2606 but I didn't want to pull hashtab.h into real.h. */
2608 unsigned int
2609 real_hash (const REAL_VALUE_TYPE *r)
2611 unsigned int h;
2612 size_t i;
2614 h = r->cl | (r->sign << 2);
2615 switch (r->cl)
2617 case rvc_zero:
2618 case rvc_inf:
2619 return h;
2621 case rvc_normal:
2622 h |= REAL_EXP (r) << 3;
2623 break;
2625 case rvc_nan:
2626 if (r->signalling)
2627 h ^= (unsigned int)-1;
2628 if (r->canonical)
2629 return h;
2630 break;
2632 default:
2633 gcc_unreachable ();
2636 if (sizeof(unsigned long) > sizeof(unsigned int))
2637 for (i = 0; i < SIGSZ; ++i)
2639 unsigned long s = r->sig[i];
2640 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2642 else
2643 for (i = 0; i < SIGSZ; ++i)
2644 h ^= r->sig[i];
2646 return h;
2649 /* IEEE single-precision format. */
2651 static void encode_ieee_single (const struct real_format *fmt,
2652 long *, const REAL_VALUE_TYPE *);
2653 static void decode_ieee_single (const struct real_format *,
2654 REAL_VALUE_TYPE *, const long *);
2656 static void
2657 encode_ieee_single (const struct real_format *fmt, long *buf,
2658 const REAL_VALUE_TYPE *r)
2660 unsigned long image, sig, exp;
2661 unsigned long sign = r->sign;
2662 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2664 image = sign << 31;
2665 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2667 switch (r->cl)
2669 case rvc_zero:
2670 break;
2672 case rvc_inf:
2673 if (fmt->has_inf)
2674 image |= 255 << 23;
2675 else
2676 image |= 0x7fffffff;
2677 break;
2679 case rvc_nan:
2680 if (fmt->has_nans)
2682 if (r->canonical)
2683 sig = 0;
2684 if (r->signalling == fmt->qnan_msb_set)
2685 sig &= ~(1 << 22);
2686 else
2687 sig |= 1 << 22;
2688 /* We overload qnan_msb_set here: it's only clear for
2689 mips_ieee_single, which wants all mantissa bits but the
2690 quiet/signalling one set in canonical NaNs (at least
2691 Quiet ones). */
2692 if (r->canonical && !fmt->qnan_msb_set)
2693 sig |= (1 << 22) - 1;
2694 else if (sig == 0)
2695 sig = 1 << 21;
2697 image |= 255 << 23;
2698 image |= sig;
2700 else
2701 image |= 0x7fffffff;
2702 break;
2704 case rvc_normal:
2705 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2706 whereas the intermediate representation is 0.F x 2**exp.
2707 Which means we're off by one. */
2708 if (denormal)
2709 exp = 0;
2710 else
2711 exp = REAL_EXP (r) + 127 - 1;
2712 image |= exp << 23;
2713 image |= sig;
2714 break;
2716 default:
2717 gcc_unreachable ();
2720 buf[0] = image;
2723 static void
2724 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2725 const long *buf)
2727 unsigned long image = buf[0] & 0xffffffff;
2728 bool sign = (image >> 31) & 1;
2729 int exp = (image >> 23) & 0xff;
2731 memset (r, 0, sizeof (*r));
2732 image <<= HOST_BITS_PER_LONG - 24;
2733 image &= ~SIG_MSB;
2735 if (exp == 0)
2737 if (image && fmt->has_denorm)
2739 r->cl = rvc_normal;
2740 r->sign = sign;
2741 SET_REAL_EXP (r, -126);
2742 r->sig[SIGSZ-1] = image << 1;
2743 normalize (r);
2745 else if (fmt->has_signed_zero)
2746 r->sign = sign;
2748 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2750 if (image)
2752 r->cl = rvc_nan;
2753 r->sign = sign;
2754 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
2755 ^ fmt->qnan_msb_set);
2756 r->sig[SIGSZ-1] = image;
2758 else
2760 r->cl = rvc_inf;
2761 r->sign = sign;
2764 else
2766 r->cl = rvc_normal;
2767 r->sign = sign;
2768 SET_REAL_EXP (r, exp - 127 + 1);
2769 r->sig[SIGSZ-1] = image | SIG_MSB;
2773 const struct real_format ieee_single_format =
2775 encode_ieee_single,
2776 decode_ieee_single,
2781 -125,
2782 128,
2785 true,
2786 true,
2787 true,
2788 true,
2789 true
2792 const struct real_format mips_single_format =
2794 encode_ieee_single,
2795 decode_ieee_single,
2800 -125,
2801 128,
2804 true,
2805 true,
2806 true,
2807 true,
2808 false
2812 /* IEEE double-precision format. */
2814 static void encode_ieee_double (const struct real_format *fmt,
2815 long *, const REAL_VALUE_TYPE *);
2816 static void decode_ieee_double (const struct real_format *,
2817 REAL_VALUE_TYPE *, const long *);
2819 static void
2820 encode_ieee_double (const struct real_format *fmt, long *buf,
2821 const REAL_VALUE_TYPE *r)
2823 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
2824 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2826 image_hi = r->sign << 31;
2827 image_lo = 0;
2829 if (HOST_BITS_PER_LONG == 64)
2831 sig_hi = r->sig[SIGSZ-1];
2832 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
2833 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
2835 else
2837 sig_hi = r->sig[SIGSZ-1];
2838 sig_lo = r->sig[SIGSZ-2];
2839 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
2840 sig_hi = (sig_hi >> 11) & 0xfffff;
2843 switch (r->cl)
2845 case rvc_zero:
2846 break;
2848 case rvc_inf:
2849 if (fmt->has_inf)
2850 image_hi |= 2047 << 20;
2851 else
2853 image_hi |= 0x7fffffff;
2854 image_lo = 0xffffffff;
2856 break;
2858 case rvc_nan:
2859 if (fmt->has_nans)
2861 if (r->canonical)
2862 sig_hi = sig_lo = 0;
2863 if (r->signalling == fmt->qnan_msb_set)
2864 sig_hi &= ~(1 << 19);
2865 else
2866 sig_hi |= 1 << 19;
2867 /* We overload qnan_msb_set here: it's only clear for
2868 mips_ieee_single, which wants all mantissa bits but the
2869 quiet/signalling one set in canonical NaNs (at least
2870 Quiet ones). */
2871 if (r->canonical && !fmt->qnan_msb_set)
2873 sig_hi |= (1 << 19) - 1;
2874 sig_lo = 0xffffffff;
2876 else if (sig_hi == 0 && sig_lo == 0)
2877 sig_hi = 1 << 18;
2879 image_hi |= 2047 << 20;
2880 image_hi |= sig_hi;
2881 image_lo = sig_lo;
2883 else
2885 image_hi |= 0x7fffffff;
2886 image_lo = 0xffffffff;
2888 break;
2890 case rvc_normal:
2891 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2892 whereas the intermediate representation is 0.F x 2**exp.
2893 Which means we're off by one. */
2894 if (denormal)
2895 exp = 0;
2896 else
2897 exp = REAL_EXP (r) + 1023 - 1;
2898 image_hi |= exp << 20;
2899 image_hi |= sig_hi;
2900 image_lo = sig_lo;
2901 break;
2903 default:
2904 gcc_unreachable ();
2907 if (FLOAT_WORDS_BIG_ENDIAN)
2908 buf[0] = image_hi, buf[1] = image_lo;
2909 else
2910 buf[0] = image_lo, buf[1] = image_hi;
2913 static void
2914 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2915 const long *buf)
2917 unsigned long image_hi, image_lo;
2918 bool sign;
2919 int exp;
2921 if (FLOAT_WORDS_BIG_ENDIAN)
2922 image_hi = buf[0], image_lo = buf[1];
2923 else
2924 image_lo = buf[0], image_hi = buf[1];
2925 image_lo &= 0xffffffff;
2926 image_hi &= 0xffffffff;
2928 sign = (image_hi >> 31) & 1;
2929 exp = (image_hi >> 20) & 0x7ff;
2931 memset (r, 0, sizeof (*r));
2933 image_hi <<= 32 - 21;
2934 image_hi |= image_lo >> 21;
2935 image_hi &= 0x7fffffff;
2936 image_lo <<= 32 - 21;
2938 if (exp == 0)
2940 if ((image_hi || image_lo) && fmt->has_denorm)
2942 r->cl = rvc_normal;
2943 r->sign = sign;
2944 SET_REAL_EXP (r, -1022);
2945 if (HOST_BITS_PER_LONG == 32)
2947 image_hi = (image_hi << 1) | (image_lo >> 31);
2948 image_lo <<= 1;
2949 r->sig[SIGSZ-1] = image_hi;
2950 r->sig[SIGSZ-2] = image_lo;
2952 else
2954 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
2955 r->sig[SIGSZ-1] = image_hi;
2957 normalize (r);
2959 else if (fmt->has_signed_zero)
2960 r->sign = sign;
2962 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
2964 if (image_hi || image_lo)
2966 r->cl = rvc_nan;
2967 r->sign = sign;
2968 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
2969 if (HOST_BITS_PER_LONG == 32)
2971 r->sig[SIGSZ-1] = image_hi;
2972 r->sig[SIGSZ-2] = image_lo;
2974 else
2975 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
2977 else
2979 r->cl = rvc_inf;
2980 r->sign = sign;
2983 else
2985 r->cl = rvc_normal;
2986 r->sign = sign;
2987 SET_REAL_EXP (r, exp - 1023 + 1);
2988 if (HOST_BITS_PER_LONG == 32)
2990 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
2991 r->sig[SIGSZ-2] = image_lo;
2993 else
2994 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
2998 const struct real_format ieee_double_format =
3000 encode_ieee_double,
3001 decode_ieee_double,
3006 -1021,
3007 1024,
3010 true,
3011 true,
3012 true,
3013 true,
3014 true
3017 const struct real_format mips_double_format =
3019 encode_ieee_double,
3020 decode_ieee_double,
3025 -1021,
3026 1024,
3029 true,
3030 true,
3031 true,
3032 true,
3033 false
3037 /* IEEE extended real format. This comes in three flavors: Intel's as
3038 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3039 12- and 16-byte images may be big- or little endian; Motorola's is
3040 always big endian. */
3042 /* Helper subroutine which converts from the internal format to the
3043 12-byte little-endian Intel format. Functions below adjust this
3044 for the other possible formats. */
3045 static void
3046 encode_ieee_extended (const struct real_format *fmt, long *buf,
3047 const REAL_VALUE_TYPE *r)
3049 unsigned long image_hi, sig_hi, sig_lo;
3050 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3052 image_hi = r->sign << 15;
3053 sig_hi = sig_lo = 0;
3055 switch (r->cl)
3057 case rvc_zero:
3058 break;
3060 case rvc_inf:
3061 if (fmt->has_inf)
3063 image_hi |= 32767;
3065 /* Intel requires the explicit integer bit to be set, otherwise
3066 it considers the value a "pseudo-infinity". Motorola docs
3067 say it doesn't care. */
3068 sig_hi = 0x80000000;
3070 else
3072 image_hi |= 32767;
3073 sig_lo = sig_hi = 0xffffffff;
3075 break;
3077 case rvc_nan:
3078 if (fmt->has_nans)
3080 image_hi |= 32767;
3081 if (HOST_BITS_PER_LONG == 32)
3083 sig_hi = r->sig[SIGSZ-1];
3084 sig_lo = r->sig[SIGSZ-2];
3086 else
3088 sig_lo = r->sig[SIGSZ-1];
3089 sig_hi = sig_lo >> 31 >> 1;
3090 sig_lo &= 0xffffffff;
3092 if (r->signalling == fmt->qnan_msb_set)
3093 sig_hi &= ~(1 << 30);
3094 else
3095 sig_hi |= 1 << 30;
3096 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3097 sig_hi = 1 << 29;
3099 /* Intel requires the explicit integer bit to be set, otherwise
3100 it considers the value a "pseudo-nan". Motorola docs say it
3101 doesn't care. */
3102 sig_hi |= 0x80000000;
3104 else
3106 image_hi |= 32767;
3107 sig_lo = sig_hi = 0xffffffff;
3109 break;
3111 case rvc_normal:
3113 int exp = REAL_EXP (r);
3115 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3116 whereas the intermediate representation is 0.F x 2**exp.
3117 Which means we're off by one.
3119 Except for Motorola, which consider exp=0 and explicit
3120 integer bit set to continue to be normalized. In theory
3121 this discrepancy has been taken care of by the difference
3122 in fmt->emin in round_for_format. */
3124 if (denormal)
3125 exp = 0;
3126 else
3128 exp += 16383 - 1;
3129 gcc_assert (exp >= 0);
3131 image_hi |= exp;
3133 if (HOST_BITS_PER_LONG == 32)
3135 sig_hi = r->sig[SIGSZ-1];
3136 sig_lo = r->sig[SIGSZ-2];
3138 else
3140 sig_lo = r->sig[SIGSZ-1];
3141 sig_hi = sig_lo >> 31 >> 1;
3142 sig_lo &= 0xffffffff;
3145 break;
3147 default:
3148 gcc_unreachable ();
3151 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3154 /* Convert from the internal format to the 12-byte Motorola format
3155 for an IEEE extended real. */
3156 static void
3157 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3158 const REAL_VALUE_TYPE *r)
3160 long intermed[3];
3161 encode_ieee_extended (fmt, intermed, r);
3163 /* Motorola chips are assumed always to be big-endian. Also, the
3164 padding in a Motorola extended real goes between the exponent and
3165 the mantissa. At this point the mantissa is entirely within
3166 elements 0 and 1 of intermed, and the exponent entirely within
3167 element 2, so all we have to do is swap the order around, and
3168 shift element 2 left 16 bits. */
3169 buf[0] = intermed[2] << 16;
3170 buf[1] = intermed[1];
3171 buf[2] = intermed[0];
3174 /* Convert from the internal format to the 12-byte Intel format for
3175 an IEEE extended real. */
3176 static void
3177 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3178 const REAL_VALUE_TYPE *r)
3180 if (FLOAT_WORDS_BIG_ENDIAN)
3182 /* All the padding in an Intel-format extended real goes at the high
3183 end, which in this case is after the mantissa, not the exponent.
3184 Therefore we must shift everything down 16 bits. */
3185 long intermed[3];
3186 encode_ieee_extended (fmt, intermed, r);
3187 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3188 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3189 buf[2] = (intermed[0] << 16);
3191 else
3192 /* encode_ieee_extended produces what we want directly. */
3193 encode_ieee_extended (fmt, buf, r);
3196 /* Convert from the internal format to the 16-byte Intel format for
3197 an IEEE extended real. */
3198 static void
3199 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3200 const REAL_VALUE_TYPE *r)
3202 /* All the padding in an Intel-format extended real goes at the high end. */
3203 encode_ieee_extended_intel_96 (fmt, buf, r);
3204 buf[3] = 0;
3207 /* As above, we have a helper function which converts from 12-byte
3208 little-endian Intel format to internal format. Functions below
3209 adjust for the other possible formats. */
3210 static void
3211 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3212 const long *buf)
3214 unsigned long image_hi, sig_hi, sig_lo;
3215 bool sign;
3216 int exp;
3218 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3219 sig_lo &= 0xffffffff;
3220 sig_hi &= 0xffffffff;
3221 image_hi &= 0xffffffff;
3223 sign = (image_hi >> 15) & 1;
3224 exp = image_hi & 0x7fff;
3226 memset (r, 0, sizeof (*r));
3228 if (exp == 0)
3230 if ((sig_hi || sig_lo) && fmt->has_denorm)
3232 r->cl = rvc_normal;
3233 r->sign = sign;
3235 /* When the IEEE format contains a hidden bit, we know that
3236 it's zero at this point, and so shift up the significand
3237 and decrease the exponent to match. In this case, Motorola
3238 defines the explicit integer bit to be valid, so we don't
3239 know whether the msb is set or not. */
3240 SET_REAL_EXP (r, fmt->emin);
3241 if (HOST_BITS_PER_LONG == 32)
3243 r->sig[SIGSZ-1] = sig_hi;
3244 r->sig[SIGSZ-2] = sig_lo;
3246 else
3247 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3249 normalize (r);
3251 else if (fmt->has_signed_zero)
3252 r->sign = sign;
3254 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3256 /* See above re "pseudo-infinities" and "pseudo-nans".
3257 Short summary is that the MSB will likely always be
3258 set, and that we don't care about it. */
3259 sig_hi &= 0x7fffffff;
3261 if (sig_hi || sig_lo)
3263 r->cl = rvc_nan;
3264 r->sign = sign;
3265 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3266 if (HOST_BITS_PER_LONG == 32)
3268 r->sig[SIGSZ-1] = sig_hi;
3269 r->sig[SIGSZ-2] = sig_lo;
3271 else
3272 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3274 else
3276 r->cl = rvc_inf;
3277 r->sign = sign;
3280 else
3282 r->cl = rvc_normal;
3283 r->sign = sign;
3284 SET_REAL_EXP (r, exp - 16383 + 1);
3285 if (HOST_BITS_PER_LONG == 32)
3287 r->sig[SIGSZ-1] = sig_hi;
3288 r->sig[SIGSZ-2] = sig_lo;
3290 else
3291 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3295 /* Convert from the internal format to the 12-byte Motorola format
3296 for an IEEE extended real. */
3297 static void
3298 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3299 const long *buf)
3301 long intermed[3];
3303 /* Motorola chips are assumed always to be big-endian. Also, the
3304 padding in a Motorola extended real goes between the exponent and
3305 the mantissa; remove it. */
3306 intermed[0] = buf[2];
3307 intermed[1] = buf[1];
3308 intermed[2] = (unsigned long)buf[0] >> 16;
3310 decode_ieee_extended (fmt, r, intermed);
3313 /* Convert from the internal format to the 12-byte Intel format for
3314 an IEEE extended real. */
3315 static void
3316 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3317 const long *buf)
3319 if (FLOAT_WORDS_BIG_ENDIAN)
3321 /* All the padding in an Intel-format extended real goes at the high
3322 end, which in this case is after the mantissa, not the exponent.
3323 Therefore we must shift everything up 16 bits. */
3324 long intermed[3];
3326 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3327 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3328 intermed[2] = ((unsigned long)buf[0] >> 16);
3330 decode_ieee_extended (fmt, r, intermed);
3332 else
3333 /* decode_ieee_extended produces what we want directly. */
3334 decode_ieee_extended (fmt, r, buf);
3337 /* Convert from the internal format to the 16-byte Intel format for
3338 an IEEE extended real. */
3339 static void
3340 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3341 const long *buf)
3343 /* All the padding in an Intel-format extended real goes at the high end. */
3344 decode_ieee_extended_intel_96 (fmt, r, buf);
3347 const struct real_format ieee_extended_motorola_format =
3349 encode_ieee_extended_motorola,
3350 decode_ieee_extended_motorola,
3355 -16382,
3356 16384,
3359 true,
3360 true,
3361 true,
3362 true,
3363 true
3366 const struct real_format ieee_extended_intel_96_format =
3368 encode_ieee_extended_intel_96,
3369 decode_ieee_extended_intel_96,
3374 -16381,
3375 16384,
3378 true,
3379 true,
3380 true,
3381 true,
3382 true
3385 const struct real_format ieee_extended_intel_128_format =
3387 encode_ieee_extended_intel_128,
3388 decode_ieee_extended_intel_128,
3393 -16381,
3394 16384,
3397 true,
3398 true,
3399 true,
3400 true,
3401 true
3404 /* The following caters to i386 systems that set the rounding precision
3405 to 53 bits instead of 64, e.g. FreeBSD. */
3406 const struct real_format ieee_extended_intel_96_round_53_format =
3408 encode_ieee_extended_intel_96,
3409 decode_ieee_extended_intel_96,
3414 -16381,
3415 16384,
3418 true,
3419 true,
3420 true,
3421 true,
3422 true
3425 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3426 numbers whose sum is equal to the extended precision value. The number
3427 with greater magnitude is first. This format has the same magnitude
3428 range as an IEEE double precision value, but effectively 106 bits of
3429 significand precision. Infinity and NaN are represented by their IEEE
3430 double precision value stored in the first number, the second number is
3431 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3433 static void encode_ibm_extended (const struct real_format *fmt,
3434 long *, const REAL_VALUE_TYPE *);
3435 static void decode_ibm_extended (const struct real_format *,
3436 REAL_VALUE_TYPE *, const long *);
3438 static void
3439 encode_ibm_extended (const struct real_format *fmt, long *buf,
3440 const REAL_VALUE_TYPE *r)
3442 REAL_VALUE_TYPE u, normr, v;
3443 const struct real_format *base_fmt;
3445 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3447 /* Renormlize R before doing any arithmetic on it. */
3448 normr = *r;
3449 if (normr.cl == rvc_normal)
3450 normalize (&normr);
3452 /* u = IEEE double precision portion of significand. */
3453 u = normr;
3454 round_for_format (base_fmt, &u);
3455 encode_ieee_double (base_fmt, &buf[0], &u);
3457 if (u.cl == rvc_normal)
3459 do_add (&v, &normr, &u, 1);
3460 /* Call round_for_format since we might need to denormalize. */
3461 round_for_format (base_fmt, &v);
3462 encode_ieee_double (base_fmt, &buf[2], &v);
3464 else
3466 /* Inf, NaN, 0 are all representable as doubles, so the
3467 least-significant part can be 0.0. */
3468 buf[2] = 0;
3469 buf[3] = 0;
3473 static void
3474 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3475 const long *buf)
3477 REAL_VALUE_TYPE u, v;
3478 const struct real_format *base_fmt;
3480 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3481 decode_ieee_double (base_fmt, &u, &buf[0]);
3483 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3485 decode_ieee_double (base_fmt, &v, &buf[2]);
3486 do_add (r, &u, &v, 0);
3488 else
3489 *r = u;
3492 const struct real_format ibm_extended_format =
3494 encode_ibm_extended,
3495 decode_ibm_extended,
3498 53 + 53,
3500 -1021 + 53,
3501 1024,
3502 127,
3504 true,
3505 true,
3506 true,
3507 true,
3508 true
3511 const struct real_format mips_extended_format =
3513 encode_ibm_extended,
3514 decode_ibm_extended,
3517 53 + 53,
3519 -1021 + 53,
3520 1024,
3521 127,
3523 true,
3524 true,
3525 true,
3526 true,
3527 false
3531 /* IEEE quad precision format. */
3533 static void encode_ieee_quad (const struct real_format *fmt,
3534 long *, const REAL_VALUE_TYPE *);
3535 static void decode_ieee_quad (const struct real_format *,
3536 REAL_VALUE_TYPE *, const long *);
3538 static void
3539 encode_ieee_quad (const struct real_format *fmt, long *buf,
3540 const REAL_VALUE_TYPE *r)
3542 unsigned long image3, image2, image1, image0, exp;
3543 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3544 REAL_VALUE_TYPE u;
3546 image3 = r->sign << 31;
3547 image2 = 0;
3548 image1 = 0;
3549 image0 = 0;
3551 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3553 switch (r->cl)
3555 case rvc_zero:
3556 break;
3558 case rvc_inf:
3559 if (fmt->has_inf)
3560 image3 |= 32767 << 16;
3561 else
3563 image3 |= 0x7fffffff;
3564 image2 = 0xffffffff;
3565 image1 = 0xffffffff;
3566 image0 = 0xffffffff;
3568 break;
3570 case rvc_nan:
3571 if (fmt->has_nans)
3573 image3 |= 32767 << 16;
3575 if (r->canonical)
3577 /* Don't use bits from the significand. The
3578 initialization above is right. */
3580 else if (HOST_BITS_PER_LONG == 32)
3582 image0 = u.sig[0];
3583 image1 = u.sig[1];
3584 image2 = u.sig[2];
3585 image3 |= u.sig[3] & 0xffff;
3587 else
3589 image0 = u.sig[0];
3590 image1 = image0 >> 31 >> 1;
3591 image2 = u.sig[1];
3592 image3 |= (image2 >> 31 >> 1) & 0xffff;
3593 image0 &= 0xffffffff;
3594 image2 &= 0xffffffff;
3596 if (r->signalling == fmt->qnan_msb_set)
3597 image3 &= ~0x8000;
3598 else
3599 image3 |= 0x8000;
3600 /* We overload qnan_msb_set here: it's only clear for
3601 mips_ieee_single, which wants all mantissa bits but the
3602 quiet/signalling one set in canonical NaNs (at least
3603 Quiet ones). */
3604 if (r->canonical && !fmt->qnan_msb_set)
3606 image3 |= 0x7fff;
3607 image2 = image1 = image0 = 0xffffffff;
3609 else if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3610 image3 |= 0x4000;
3612 else
3614 image3 |= 0x7fffffff;
3615 image2 = 0xffffffff;
3616 image1 = 0xffffffff;
3617 image0 = 0xffffffff;
3619 break;
3621 case rvc_normal:
3622 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3623 whereas the intermediate representation is 0.F x 2**exp.
3624 Which means we're off by one. */
3625 if (denormal)
3626 exp = 0;
3627 else
3628 exp = REAL_EXP (r) + 16383 - 1;
3629 image3 |= exp << 16;
3631 if (HOST_BITS_PER_LONG == 32)
3633 image0 = u.sig[0];
3634 image1 = u.sig[1];
3635 image2 = u.sig[2];
3636 image3 |= u.sig[3] & 0xffff;
3638 else
3640 image0 = u.sig[0];
3641 image1 = image0 >> 31 >> 1;
3642 image2 = u.sig[1];
3643 image3 |= (image2 >> 31 >> 1) & 0xffff;
3644 image0 &= 0xffffffff;
3645 image2 &= 0xffffffff;
3647 break;
3649 default:
3650 gcc_unreachable ();
3653 if (FLOAT_WORDS_BIG_ENDIAN)
3655 buf[0] = image3;
3656 buf[1] = image2;
3657 buf[2] = image1;
3658 buf[3] = image0;
3660 else
3662 buf[0] = image0;
3663 buf[1] = image1;
3664 buf[2] = image2;
3665 buf[3] = image3;
3669 static void
3670 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3671 const long *buf)
3673 unsigned long image3, image2, image1, image0;
3674 bool sign;
3675 int exp;
3677 if (FLOAT_WORDS_BIG_ENDIAN)
3679 image3 = buf[0];
3680 image2 = buf[1];
3681 image1 = buf[2];
3682 image0 = buf[3];
3684 else
3686 image0 = buf[0];
3687 image1 = buf[1];
3688 image2 = buf[2];
3689 image3 = buf[3];
3691 image0 &= 0xffffffff;
3692 image1 &= 0xffffffff;
3693 image2 &= 0xffffffff;
3695 sign = (image3 >> 31) & 1;
3696 exp = (image3 >> 16) & 0x7fff;
3697 image3 &= 0xffff;
3699 memset (r, 0, sizeof (*r));
3701 if (exp == 0)
3703 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3705 r->cl = rvc_normal;
3706 r->sign = sign;
3708 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
3709 if (HOST_BITS_PER_LONG == 32)
3711 r->sig[0] = image0;
3712 r->sig[1] = image1;
3713 r->sig[2] = image2;
3714 r->sig[3] = image3;
3716 else
3718 r->sig[0] = (image1 << 31 << 1) | image0;
3719 r->sig[1] = (image3 << 31 << 1) | image2;
3722 normalize (r);
3724 else if (fmt->has_signed_zero)
3725 r->sign = sign;
3727 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3729 if (image3 | image2 | image1 | image0)
3731 r->cl = rvc_nan;
3732 r->sign = sign;
3733 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
3735 if (HOST_BITS_PER_LONG == 32)
3737 r->sig[0] = image0;
3738 r->sig[1] = image1;
3739 r->sig[2] = image2;
3740 r->sig[3] = image3;
3742 else
3744 r->sig[0] = (image1 << 31 << 1) | image0;
3745 r->sig[1] = (image3 << 31 << 1) | image2;
3747 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3749 else
3751 r->cl = rvc_inf;
3752 r->sign = sign;
3755 else
3757 r->cl = rvc_normal;
3758 r->sign = sign;
3759 SET_REAL_EXP (r, exp - 16383 + 1);
3761 if (HOST_BITS_PER_LONG == 32)
3763 r->sig[0] = image0;
3764 r->sig[1] = image1;
3765 r->sig[2] = image2;
3766 r->sig[3] = image3;
3768 else
3770 r->sig[0] = (image1 << 31 << 1) | image0;
3771 r->sig[1] = (image3 << 31 << 1) | image2;
3773 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3774 r->sig[SIGSZ-1] |= SIG_MSB;
3778 const struct real_format ieee_quad_format =
3780 encode_ieee_quad,
3781 decode_ieee_quad,
3784 113,
3785 113,
3786 -16381,
3787 16384,
3788 127,
3789 127,
3790 true,
3791 true,
3792 true,
3793 true,
3794 true
3797 const struct real_format mips_quad_format =
3799 encode_ieee_quad,
3800 decode_ieee_quad,
3803 113,
3804 113,
3805 -16381,
3806 16384,
3807 127,
3808 127,
3809 true,
3810 true,
3811 true,
3812 true,
3813 false
3816 /* Descriptions of VAX floating point formats can be found beginning at
3818 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
3820 The thing to remember is that they're almost IEEE, except for word
3821 order, exponent bias, and the lack of infinities, nans, and denormals.
3823 We don't implement the H_floating format here, simply because neither
3824 the VAX or Alpha ports use it. */
3826 static void encode_vax_f (const struct real_format *fmt,
3827 long *, const REAL_VALUE_TYPE *);
3828 static void decode_vax_f (const struct real_format *,
3829 REAL_VALUE_TYPE *, const long *);
3830 static void encode_vax_d (const struct real_format *fmt,
3831 long *, const REAL_VALUE_TYPE *);
3832 static void decode_vax_d (const struct real_format *,
3833 REAL_VALUE_TYPE *, const long *);
3834 static void encode_vax_g (const struct real_format *fmt,
3835 long *, const REAL_VALUE_TYPE *);
3836 static void decode_vax_g (const struct real_format *,
3837 REAL_VALUE_TYPE *, const long *);
3839 static void
3840 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
3841 const REAL_VALUE_TYPE *r)
3843 unsigned long sign, exp, sig, image;
3845 sign = r->sign << 15;
3847 switch (r->cl)
3849 case rvc_zero:
3850 image = 0;
3851 break;
3853 case rvc_inf:
3854 case rvc_nan:
3855 image = 0xffff7fff | sign;
3856 break;
3858 case rvc_normal:
3859 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3860 exp = REAL_EXP (r) + 128;
3862 image = (sig << 16) & 0xffff0000;
3863 image |= sign;
3864 image |= exp << 7;
3865 image |= sig >> 16;
3866 break;
3868 default:
3869 gcc_unreachable ();
3872 buf[0] = image;
3875 static void
3876 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
3877 REAL_VALUE_TYPE *r, const long *buf)
3879 unsigned long image = buf[0] & 0xffffffff;
3880 int exp = (image >> 7) & 0xff;
3882 memset (r, 0, sizeof (*r));
3884 if (exp != 0)
3886 r->cl = rvc_normal;
3887 r->sign = (image >> 15) & 1;
3888 SET_REAL_EXP (r, exp - 128);
3890 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
3891 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3895 static void
3896 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
3897 const REAL_VALUE_TYPE *r)
3899 unsigned long image0, image1, sign = r->sign << 15;
3901 switch (r->cl)
3903 case rvc_zero:
3904 image0 = image1 = 0;
3905 break;
3907 case rvc_inf:
3908 case rvc_nan:
3909 image0 = 0xffff7fff | sign;
3910 image1 = 0xffffffff;
3911 break;
3913 case rvc_normal:
3914 /* Extract the significand into straight hi:lo. */
3915 if (HOST_BITS_PER_LONG == 64)
3917 image0 = r->sig[SIGSZ-1];
3918 image1 = (image0 >> (64 - 56)) & 0xffffffff;
3919 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
3921 else
3923 image0 = r->sig[SIGSZ-1];
3924 image1 = r->sig[SIGSZ-2];
3925 image1 = (image0 << 24) | (image1 >> 8);
3926 image0 = (image0 >> 8) & 0xffffff;
3929 /* Rearrange the half-words of the significand to match the
3930 external format. */
3931 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
3932 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3934 /* Add the sign and exponent. */
3935 image0 |= sign;
3936 image0 |= (REAL_EXP (r) + 128) << 7;
3937 break;
3939 default:
3940 gcc_unreachable ();
3943 if (FLOAT_WORDS_BIG_ENDIAN)
3944 buf[0] = image1, buf[1] = image0;
3945 else
3946 buf[0] = image0, buf[1] = image1;
3949 static void
3950 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
3951 REAL_VALUE_TYPE *r, const long *buf)
3953 unsigned long image0, image1;
3954 int exp;
3956 if (FLOAT_WORDS_BIG_ENDIAN)
3957 image1 = buf[0], image0 = buf[1];
3958 else
3959 image0 = buf[0], image1 = buf[1];
3960 image0 &= 0xffffffff;
3961 image1 &= 0xffffffff;
3963 exp = (image0 >> 7) & 0xff;
3965 memset (r, 0, sizeof (*r));
3967 if (exp != 0)
3969 r->cl = rvc_normal;
3970 r->sign = (image0 >> 15) & 1;
3971 SET_REAL_EXP (r, exp - 128);
3973 /* Rearrange the half-words of the external format into
3974 proper ascending order. */
3975 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
3976 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3978 if (HOST_BITS_PER_LONG == 64)
3980 image0 = (image0 << 31 << 1) | image1;
3981 image0 <<= 64 - 56;
3982 image0 |= SIG_MSB;
3983 r->sig[SIGSZ-1] = image0;
3985 else
3987 r->sig[SIGSZ-1] = image0;
3988 r->sig[SIGSZ-2] = image1;
3989 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
3990 r->sig[SIGSZ-1] |= SIG_MSB;
3995 static void
3996 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
3997 const REAL_VALUE_TYPE *r)
3999 unsigned long image0, image1, sign = r->sign << 15;
4001 switch (r->cl)
4003 case rvc_zero:
4004 image0 = image1 = 0;
4005 break;
4007 case rvc_inf:
4008 case rvc_nan:
4009 image0 = 0xffff7fff | sign;
4010 image1 = 0xffffffff;
4011 break;
4013 case rvc_normal:
4014 /* Extract the significand into straight hi:lo. */
4015 if (HOST_BITS_PER_LONG == 64)
4017 image0 = r->sig[SIGSZ-1];
4018 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4019 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4021 else
4023 image0 = r->sig[SIGSZ-1];
4024 image1 = r->sig[SIGSZ-2];
4025 image1 = (image0 << 21) | (image1 >> 11);
4026 image0 = (image0 >> 11) & 0xfffff;
4029 /* Rearrange the half-words of the significand to match the
4030 external format. */
4031 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4032 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4034 /* Add the sign and exponent. */
4035 image0 |= sign;
4036 image0 |= (REAL_EXP (r) + 1024) << 4;
4037 break;
4039 default:
4040 gcc_unreachable ();
4043 if (FLOAT_WORDS_BIG_ENDIAN)
4044 buf[0] = image1, buf[1] = image0;
4045 else
4046 buf[0] = image0, buf[1] = image1;
4049 static void
4050 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4051 REAL_VALUE_TYPE *r, const long *buf)
4053 unsigned long image0, image1;
4054 int exp;
4056 if (FLOAT_WORDS_BIG_ENDIAN)
4057 image1 = buf[0], image0 = buf[1];
4058 else
4059 image0 = buf[0], image1 = buf[1];
4060 image0 &= 0xffffffff;
4061 image1 &= 0xffffffff;
4063 exp = (image0 >> 4) & 0x7ff;
4065 memset (r, 0, sizeof (*r));
4067 if (exp != 0)
4069 r->cl = rvc_normal;
4070 r->sign = (image0 >> 15) & 1;
4071 SET_REAL_EXP (r, exp - 1024);
4073 /* Rearrange the half-words of the external format into
4074 proper ascending order. */
4075 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4076 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4078 if (HOST_BITS_PER_LONG == 64)
4080 image0 = (image0 << 31 << 1) | image1;
4081 image0 <<= 64 - 53;
4082 image0 |= SIG_MSB;
4083 r->sig[SIGSZ-1] = image0;
4085 else
4087 r->sig[SIGSZ-1] = image0;
4088 r->sig[SIGSZ-2] = image1;
4089 lshift_significand (r, r, 64 - 53);
4090 r->sig[SIGSZ-1] |= SIG_MSB;
4095 const struct real_format vax_f_format =
4097 encode_vax_f,
4098 decode_vax_f,
4103 -127,
4104 127,
4107 false,
4108 false,
4109 false,
4110 false,
4111 false
4114 const struct real_format vax_d_format =
4116 encode_vax_d,
4117 decode_vax_d,
4122 -127,
4123 127,
4126 false,
4127 false,
4128 false,
4129 false,
4130 false
4133 const struct real_format vax_g_format =
4135 encode_vax_g,
4136 decode_vax_g,
4141 -1023,
4142 1023,
4145 false,
4146 false,
4147 false,
4148 false,
4149 false
4152 /* A good reference for these can be found in chapter 9 of
4153 "ESA/390 Principles of Operation", IBM document number SA22-7201-01.
4154 An on-line version can be found here:
4156 http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
4159 static void encode_i370_single (const struct real_format *fmt,
4160 long *, const REAL_VALUE_TYPE *);
4161 static void decode_i370_single (const struct real_format *,
4162 REAL_VALUE_TYPE *, const long *);
4163 static void encode_i370_double (const struct real_format *fmt,
4164 long *, const REAL_VALUE_TYPE *);
4165 static void decode_i370_double (const struct real_format *,
4166 REAL_VALUE_TYPE *, const long *);
4168 static void
4169 encode_i370_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4170 long *buf, const REAL_VALUE_TYPE *r)
4172 unsigned long sign, exp, sig, image;
4174 sign = r->sign << 31;
4176 switch (r->cl)
4178 case rvc_zero:
4179 image = 0;
4180 break;
4182 case rvc_inf:
4183 case rvc_nan:
4184 image = 0x7fffffff | sign;
4185 break;
4187 case rvc_normal:
4188 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0xffffff;
4189 exp = ((REAL_EXP (r) / 4) + 64) << 24;
4190 image = sign | exp | sig;
4191 break;
4193 default:
4194 gcc_unreachable ();
4197 buf[0] = image;
4200 static void
4201 decode_i370_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4202 REAL_VALUE_TYPE *r, const long *buf)
4204 unsigned long sign, sig, image = buf[0];
4205 int exp;
4207 sign = (image >> 31) & 1;
4208 exp = (image >> 24) & 0x7f;
4209 sig = image & 0xffffff;
4211 memset (r, 0, sizeof (*r));
4213 if (exp || sig)
4215 r->cl = rvc_normal;
4216 r->sign = sign;
4217 SET_REAL_EXP (r, (exp - 64) * 4);
4218 r->sig[SIGSZ-1] = sig << (HOST_BITS_PER_LONG - 24);
4219 normalize (r);
4223 static void
4224 encode_i370_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4225 long *buf, const REAL_VALUE_TYPE *r)
4227 unsigned long sign, exp, image_hi, image_lo;
4229 sign = r->sign << 31;
4231 switch (r->cl)
4233 case rvc_zero:
4234 image_hi = image_lo = 0;
4235 break;
4237 case rvc_inf:
4238 case rvc_nan:
4239 image_hi = 0x7fffffff | sign;
4240 image_lo = 0xffffffff;
4241 break;
4243 case rvc_normal:
4244 if (HOST_BITS_PER_LONG == 64)
4246 image_hi = r->sig[SIGSZ-1];
4247 image_lo = (image_hi >> (64 - 56)) & 0xffffffff;
4248 image_hi = (image_hi >> (64 - 56 + 1) >> 31) & 0xffffff;
4250 else
4252 image_hi = r->sig[SIGSZ-1];
4253 image_lo = r->sig[SIGSZ-2];
4254 image_lo = (image_lo >> 8) | (image_hi << 24);
4255 image_hi >>= 8;
4258 exp = ((REAL_EXP (r) / 4) + 64) << 24;
4259 image_hi |= sign | exp;
4260 break;
4262 default:
4263 gcc_unreachable ();
4266 if (FLOAT_WORDS_BIG_ENDIAN)
4267 buf[0] = image_hi, buf[1] = image_lo;
4268 else
4269 buf[0] = image_lo, buf[1] = image_hi;
4272 static void
4273 decode_i370_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4274 REAL_VALUE_TYPE *r, const long *buf)
4276 unsigned long sign, image_hi, image_lo;
4277 int exp;
4279 if (FLOAT_WORDS_BIG_ENDIAN)
4280 image_hi = buf[0], image_lo = buf[1];
4281 else
4282 image_lo = buf[0], image_hi = buf[1];
4284 sign = (image_hi >> 31) & 1;
4285 exp = (image_hi >> 24) & 0x7f;
4286 image_hi &= 0xffffff;
4287 image_lo &= 0xffffffff;
4289 memset (r, 0, sizeof (*r));
4291 if (exp || image_hi || image_lo)
4293 r->cl = rvc_normal;
4294 r->sign = sign;
4295 SET_REAL_EXP (r, (exp - 64) * 4 + (SIGNIFICAND_BITS - 56));
4297 if (HOST_BITS_PER_LONG == 32)
4299 r->sig[0] = image_lo;
4300 r->sig[1] = image_hi;
4302 else
4303 r->sig[0] = image_lo | (image_hi << 31 << 1);
4305 normalize (r);
4309 const struct real_format i370_single_format =
4311 encode_i370_single,
4312 decode_i370_single,
4317 -64,
4321 false,
4322 false,
4323 false, /* ??? The encoding does allow for "unnormals". */
4324 false, /* ??? The encoding does allow for "unnormals". */
4325 false
4328 const struct real_format i370_double_format =
4330 encode_i370_double,
4331 decode_i370_double,
4336 -64,
4340 false,
4341 false,
4342 false, /* ??? The encoding does allow for "unnormals". */
4343 false, /* ??? The encoding does allow for "unnormals". */
4344 false
4347 /* Encode real R into a single precision DFP value in BUF. */
4348 static void
4349 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4350 long *buf ATTRIBUTE_UNUSED,
4351 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4353 encode_decimal32 (fmt, buf, r);
4356 /* Decode a single precision DFP value in BUF into a real R. */
4357 static void
4358 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4359 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4360 const long *buf ATTRIBUTE_UNUSED)
4362 decode_decimal32 (fmt, r, buf);
4365 /* Encode real R into a double precision DFP value in BUF. */
4366 static void
4367 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4368 long *buf ATTRIBUTE_UNUSED,
4369 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4371 encode_decimal64 (fmt, buf, r);
4374 /* Decode a double precision DFP value in BUF into a real R. */
4375 static void
4376 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4377 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4378 const long *buf ATTRIBUTE_UNUSED)
4380 decode_decimal64 (fmt, r, buf);
4383 /* Encode real R into a quad precision DFP value in BUF. */
4384 static void
4385 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4386 long *buf ATTRIBUTE_UNUSED,
4387 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4389 encode_decimal128 (fmt, buf, r);
4392 /* Decode a quad precision DFP value in BUF into a real R. */
4393 static void
4394 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4395 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4396 const long *buf ATTRIBUTE_UNUSED)
4398 decode_decimal128 (fmt, r, buf);
4401 /* Single precision decimal floating point (IEEE 754R). */
4402 const struct real_format decimal_single_format =
4404 encode_decimal_single,
4405 decode_decimal_single,
4406 10,
4407 1, /* log10 */
4410 -95,
4414 true,
4415 true,
4416 true,
4417 true,
4418 true
4421 /* Double precision decimal floating point (IEEE 754R). */
4422 const struct real_format decimal_double_format =
4424 encode_decimal_double,
4425 decode_decimal_double,
4427 1, /* log10 */
4430 -383,
4431 384,
4434 true,
4435 true,
4436 true,
4437 true,
4438 true
4441 /* Quad precision decimal floating point (IEEE 754R). */
4442 const struct real_format decimal_quad_format =
4444 encode_decimal_quad,
4445 decode_decimal_quad,
4447 1, /* log10 */
4450 -6143,
4451 6144,
4452 127,
4453 127,
4454 true,
4455 true,
4456 true,
4457 true,
4458 true
4461 /* The "twos-complement" c4x format is officially defined as
4463 x = s(~s).f * 2**e
4465 This is rather misleading. One must remember that F is signed.
4466 A better description would be
4468 x = -1**s * ((s + 1 + .f) * 2**e
4470 So if we have a (4 bit) fraction of .1000 with a sign bit of 1,
4471 that's -1 * (1+1+(-.5)) == -1.5. I think.
4473 The constructions here are taken from Tables 5-1 and 5-2 of the
4474 TMS320C4x User's Guide wherein step-by-step instructions for
4475 conversion from IEEE are presented. That's close enough to our
4476 internal representation so as to make things easy.
4478 See http://www-s.ti.com/sc/psheets/spru063c/spru063c.pdf */
4480 static void encode_c4x_single (const struct real_format *fmt,
4481 long *, const REAL_VALUE_TYPE *);
4482 static void decode_c4x_single (const struct real_format *,
4483 REAL_VALUE_TYPE *, const long *);
4484 static void encode_c4x_extended (const struct real_format *fmt,
4485 long *, const REAL_VALUE_TYPE *);
4486 static void decode_c4x_extended (const struct real_format *,
4487 REAL_VALUE_TYPE *, const long *);
4489 static void
4490 encode_c4x_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4491 long *buf, const REAL_VALUE_TYPE *r)
4493 unsigned long image, exp, sig;
4495 switch (r->cl)
4497 case rvc_zero:
4498 exp = -128;
4499 sig = 0;
4500 break;
4502 case rvc_inf:
4503 case rvc_nan:
4504 exp = 127;
4505 sig = 0x800000 - r->sign;
4506 break;
4508 case rvc_normal:
4509 exp = REAL_EXP (r) - 1;
4510 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4511 if (r->sign)
4513 if (sig)
4514 sig = -sig;
4515 else
4516 exp--;
4517 sig |= 0x800000;
4519 break;
4521 default:
4522 gcc_unreachable ();
4525 image = ((exp & 0xff) << 24) | (sig & 0xffffff);
4526 buf[0] = image;
4529 static void
4530 decode_c4x_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4531 REAL_VALUE_TYPE *r, const long *buf)
4533 unsigned long image = buf[0];
4534 unsigned long sig;
4535 int exp, sf;
4537 exp = (((image >> 24) & 0xff) ^ 0x80) - 0x80;
4538 sf = ((image & 0xffffff) ^ 0x800000) - 0x800000;
4540 memset (r, 0, sizeof (*r));
4542 if (exp != -128)
4544 r->cl = rvc_normal;
4546 sig = sf & 0x7fffff;
4547 if (sf < 0)
4549 r->sign = 1;
4550 if (sig)
4551 sig = -sig;
4552 else
4553 exp++;
4555 sig = (sig << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4557 SET_REAL_EXP (r, exp + 1);
4558 r->sig[SIGSZ-1] = sig;
4562 static void
4563 encode_c4x_extended (const struct real_format *fmt ATTRIBUTE_UNUSED,
4564 long *buf, const REAL_VALUE_TYPE *r)
4566 unsigned long exp, sig;
4568 switch (r->cl)
4570 case rvc_zero:
4571 exp = -128;
4572 sig = 0;
4573 break;
4575 case rvc_inf:
4576 case rvc_nan:
4577 exp = 127;
4578 sig = 0x80000000 - r->sign;
4579 break;
4581 case rvc_normal:
4582 exp = REAL_EXP (r) - 1;
4584 sig = r->sig[SIGSZ-1];
4585 if (HOST_BITS_PER_LONG == 64)
4586 sig = sig >> 1 >> 31;
4587 sig &= 0x7fffffff;
4589 if (r->sign)
4591 if (sig)
4592 sig = -sig;
4593 else
4594 exp--;
4595 sig |= 0x80000000;
4597 break;
4599 default:
4600 gcc_unreachable ();
4603 exp = (exp & 0xff) << 24;
4604 sig &= 0xffffffff;
4606 if (FLOAT_WORDS_BIG_ENDIAN)
4607 buf[0] = exp, buf[1] = sig;
4608 else
4609 buf[0] = sig, buf[0] = exp;
4612 static void
4613 decode_c4x_extended (const struct real_format *fmt ATTRIBUTE_UNUSED,
4614 REAL_VALUE_TYPE *r, const long *buf)
4616 unsigned long sig;
4617 int exp, sf;
4619 if (FLOAT_WORDS_BIG_ENDIAN)
4620 exp = buf[0], sf = buf[1];
4621 else
4622 sf = buf[0], exp = buf[1];
4624 exp = (((exp >> 24) & 0xff) & 0x80) - 0x80;
4625 sf = ((sf & 0xffffffff) ^ 0x80000000) - 0x80000000;
4627 memset (r, 0, sizeof (*r));
4629 if (exp != -128)
4631 r->cl = rvc_normal;
4633 sig = sf & 0x7fffffff;
4634 if (sf < 0)
4636 r->sign = 1;
4637 if (sig)
4638 sig = -sig;
4639 else
4640 exp++;
4642 if (HOST_BITS_PER_LONG == 64)
4643 sig = sig << 1 << 31;
4644 sig |= SIG_MSB;
4646 SET_REAL_EXP (r, exp + 1);
4647 r->sig[SIGSZ-1] = sig;
4651 const struct real_format c4x_single_format =
4653 encode_c4x_single,
4654 decode_c4x_single,
4659 -126,
4660 128,
4663 false,
4664 false,
4665 false,
4666 false,
4667 false
4670 const struct real_format c4x_extended_format =
4672 encode_c4x_extended,
4673 decode_c4x_extended,
4678 -126,
4679 128,
4682 false,
4683 false,
4684 false,
4685 false,
4686 false
4690 /* A synthetic "format" for internal arithmetic. It's the size of the
4691 internal significand minus the two bits needed for proper rounding.
4692 The encode and decode routines exist only to satisfy our paranoia
4693 harness. */
4695 static void encode_internal (const struct real_format *fmt,
4696 long *, const REAL_VALUE_TYPE *);
4697 static void decode_internal (const struct real_format *,
4698 REAL_VALUE_TYPE *, const long *);
4700 static void
4701 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4702 const REAL_VALUE_TYPE *r)
4704 memcpy (buf, r, sizeof (*r));
4707 static void
4708 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
4709 REAL_VALUE_TYPE *r, const long *buf)
4711 memcpy (r, buf, sizeof (*r));
4714 const struct real_format real_internal_format =
4716 encode_internal,
4717 decode_internal,
4720 SIGNIFICAND_BITS - 2,
4721 SIGNIFICAND_BITS - 2,
4722 -MAX_EXP,
4723 MAX_EXP,
4726 true,
4727 true,
4728 false,
4729 true,
4730 true
4733 /* Calculate the square root of X in mode MODE, and store the result
4734 in R. Return TRUE if the operation does not raise an exception.
4735 For details see "High Precision Division and Square Root",
4736 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4737 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4739 bool
4740 real_sqrt (REAL_VALUE_TYPE *r, enum machine_mode mode,
4741 const REAL_VALUE_TYPE *x)
4743 static REAL_VALUE_TYPE halfthree;
4744 static bool init = false;
4745 REAL_VALUE_TYPE h, t, i;
4746 int iter, exp;
4748 /* sqrt(-0.0) is -0.0. */
4749 if (real_isnegzero (x))
4751 *r = *x;
4752 return false;
4755 /* Negative arguments return NaN. */
4756 if (real_isneg (x))
4758 get_canonical_qnan (r, 0);
4759 return false;
4762 /* Infinity and NaN return themselves. */
4763 if (real_isinf (x) || real_isnan (x))
4765 *r = *x;
4766 return false;
4769 if (!init)
4771 do_add (&halfthree, &dconst1, &dconsthalf, 0);
4772 init = true;
4775 /* Initial guess for reciprocal sqrt, i. */
4776 exp = real_exponent (x);
4777 real_ldexp (&i, &dconst1, -exp/2);
4779 /* Newton's iteration for reciprocal sqrt, i. */
4780 for (iter = 0; iter < 16; iter++)
4782 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4783 do_multiply (&t, x, &i);
4784 do_multiply (&h, &t, &i);
4785 do_multiply (&t, &h, &dconsthalf);
4786 do_add (&h, &halfthree, &t, 1);
4787 do_multiply (&t, &i, &h);
4789 /* Check for early convergence. */
4790 if (iter >= 6 && real_identical (&i, &t))
4791 break;
4793 /* ??? Unroll loop to avoid copying. */
4794 i = t;
4797 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4798 do_multiply (&t, x, &i);
4799 do_multiply (&h, &t, &i);
4800 do_add (&i, &dconst1, &h, 1);
4801 do_multiply (&h, &t, &i);
4802 do_multiply (&i, &dconsthalf, &h);
4803 do_add (&h, &t, &i, 0);
4805 /* ??? We need a Tuckerman test to get the last bit. */
4807 real_convert (r, mode, &h);
4808 return true;
4811 /* Calculate X raised to the integer exponent N in mode MODE and store
4812 the result in R. Return true if the result may be inexact due to
4813 loss of precision. The algorithm is the classic "left-to-right binary
4814 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4815 Algorithms", "The Art of Computer Programming", Volume 2. */
4817 bool
4818 real_powi (REAL_VALUE_TYPE *r, enum machine_mode mode,
4819 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4821 unsigned HOST_WIDE_INT bit;
4822 REAL_VALUE_TYPE t;
4823 bool inexact = false;
4824 bool init = false;
4825 bool neg;
4826 int i;
4828 if (n == 0)
4830 *r = dconst1;
4831 return false;
4833 else if (n < 0)
4835 /* Don't worry about overflow, from now on n is unsigned. */
4836 neg = true;
4837 n = -n;
4839 else
4840 neg = false;
4842 t = *x;
4843 bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
4844 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4846 if (init)
4848 inexact |= do_multiply (&t, &t, &t);
4849 if (n & bit)
4850 inexact |= do_multiply (&t, &t, x);
4852 else if (n & bit)
4853 init = true;
4854 bit >>= 1;
4857 if (neg)
4858 inexact |= do_divide (&t, &dconst1, &t);
4860 real_convert (r, mode, &t);
4861 return inexact;
4864 /* Round X to the nearest integer not larger in absolute value, i.e.
4865 towards zero, placing the result in R in mode MODE. */
4867 void
4868 real_trunc (REAL_VALUE_TYPE *r, enum machine_mode mode,
4869 const REAL_VALUE_TYPE *x)
4871 do_fix_trunc (r, x);
4872 if (mode != VOIDmode)
4873 real_convert (r, mode, r);
4876 /* Round X to the largest integer not greater in value, i.e. round
4877 down, placing the result in R in mode MODE. */
4879 void
4880 real_floor (REAL_VALUE_TYPE *r, enum machine_mode mode,
4881 const REAL_VALUE_TYPE *x)
4883 REAL_VALUE_TYPE t;
4885 do_fix_trunc (&t, x);
4886 if (! real_identical (&t, x) && x->sign)
4887 do_add (&t, &t, &dconstm1, 0);
4888 if (mode != VOIDmode)
4889 real_convert (r, mode, &t);
4890 else
4891 *r = t;
4894 /* Round X to the smallest integer not less then argument, i.e. round
4895 up, placing the result in R in mode MODE. */
4897 void
4898 real_ceil (REAL_VALUE_TYPE *r, enum machine_mode mode,
4899 const REAL_VALUE_TYPE *x)
4901 REAL_VALUE_TYPE t;
4903 do_fix_trunc (&t, x);
4904 if (! real_identical (&t, x) && ! x->sign)
4905 do_add (&t, &t, &dconst1, 0);
4906 if (mode != VOIDmode)
4907 real_convert (r, mode, &t);
4908 else
4909 *r = t;
4912 /* Round X to the nearest integer, but round halfway cases away from
4913 zero. */
4915 void
4916 real_round (REAL_VALUE_TYPE *r, enum machine_mode mode,
4917 const REAL_VALUE_TYPE *x)
4919 do_add (r, x, &dconsthalf, x->sign);
4920 do_fix_trunc (r, r);
4921 if (mode != VOIDmode)
4922 real_convert (r, mode, r);
4925 /* Set the sign of R to the sign of X. */
4927 void
4928 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4930 r->sign = x->sign;