cselib.c (cselib_current_insn_in_libcall): New static variable.
[official-gcc.git] / gcc / real.c
blob7d7b4bb16377138bfc03e67cd56014eea7347087
1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2002 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 2, 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 COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "toplev.h"
30 #include "real.h"
31 #include "tm_p.h"
33 /* The floating point model used internally is not exactly IEEE 754
34 compliant, and close to the description in the ISO C 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 than the
53 largest supported target floating-point type by at least 2 bits.
54 This gives us proper rounding when we truncate to the target type.
55 In addition, E must be large enough to hold the smallest supported
56 denormal number 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 29.
62 Note that the decimal string conversion routines are sensitive to
63 rounding error. 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 PARAMS ((REAL_VALUE_TYPE *, int));
83 static void get_canonical_qnan PARAMS ((REAL_VALUE_TYPE *, int));
84 static void get_canonical_snan PARAMS ((REAL_VALUE_TYPE *, int));
85 static void get_inf PARAMS ((REAL_VALUE_TYPE *, int));
86 static bool sticky_rshift_significand PARAMS ((REAL_VALUE_TYPE *,
87 const REAL_VALUE_TYPE *,
88 unsigned int));
89 static void rshift_significand PARAMS ((REAL_VALUE_TYPE *,
90 const REAL_VALUE_TYPE *,
91 unsigned int));
92 static void lshift_significand PARAMS ((REAL_VALUE_TYPE *,
93 const REAL_VALUE_TYPE *,
94 unsigned int));
95 static void lshift_significand_1 PARAMS ((REAL_VALUE_TYPE *,
96 const REAL_VALUE_TYPE *));
97 static bool add_significands PARAMS ((REAL_VALUE_TYPE *r,
98 const REAL_VALUE_TYPE *,
99 const REAL_VALUE_TYPE *));
100 static bool sub_significands PARAMS ((REAL_VALUE_TYPE *,
101 const REAL_VALUE_TYPE *,
102 const REAL_VALUE_TYPE *, int));
103 static void neg_significand PARAMS ((REAL_VALUE_TYPE *,
104 const REAL_VALUE_TYPE *));
105 static int cmp_significands PARAMS ((const REAL_VALUE_TYPE *,
106 const REAL_VALUE_TYPE *));
107 static int cmp_significand_0 PARAMS ((const REAL_VALUE_TYPE *));
108 static void set_significand_bit PARAMS ((REAL_VALUE_TYPE *, unsigned int));
109 static void clear_significand_bit PARAMS ((REAL_VALUE_TYPE *, unsigned int));
110 static bool test_significand_bit PARAMS ((REAL_VALUE_TYPE *, unsigned int));
111 static void clear_significand_below PARAMS ((REAL_VALUE_TYPE *,
112 unsigned int));
113 static bool div_significands PARAMS ((REAL_VALUE_TYPE *,
114 const REAL_VALUE_TYPE *,
115 const REAL_VALUE_TYPE *));
116 static void normalize PARAMS ((REAL_VALUE_TYPE *));
118 static void do_add PARAMS ((REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
119 const REAL_VALUE_TYPE *, int));
120 static void do_multiply PARAMS ((REAL_VALUE_TYPE *,
121 const REAL_VALUE_TYPE *,
122 const REAL_VALUE_TYPE *));
123 static void do_divide PARAMS ((REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
124 const REAL_VALUE_TYPE *));
125 static int do_compare PARAMS ((const REAL_VALUE_TYPE *,
126 const REAL_VALUE_TYPE *, int));
127 static void do_fix_trunc PARAMS ((REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *));
129 static unsigned long rtd_divmod PARAMS ((REAL_VALUE_TYPE *,
130 REAL_VALUE_TYPE *));
132 static const REAL_VALUE_TYPE * ten_to_ptwo PARAMS ((int));
133 static const REAL_VALUE_TYPE * ten_to_mptwo PARAMS ((int));
134 static const REAL_VALUE_TYPE * real_digit PARAMS ((int));
135 static void times_pten PARAMS ((REAL_VALUE_TYPE *, int));
137 static void round_for_format PARAMS ((const struct real_format *,
138 REAL_VALUE_TYPE *));
140 /* Initialize R with a positive zero. */
142 static inline void
143 get_zero (r, sign)
144 REAL_VALUE_TYPE *r;
145 int sign;
147 memset (r, 0, sizeof (*r));
148 r->sign = sign;
151 /* Initialize R with the canonical quiet NaN. */
153 static inline void
154 get_canonical_qnan (r, sign)
155 REAL_VALUE_TYPE *r;
156 int sign;
158 memset (r, 0, sizeof (*r));
159 r->class = rvc_nan;
160 r->sign = sign;
161 r->sig[SIGSZ-1] = SIG_MSB >> 1;
164 static inline void
165 get_canonical_snan (r, sign)
166 REAL_VALUE_TYPE *r;
167 int sign;
169 memset (r, 0, sizeof (*r));
170 r->class = rvc_nan;
171 r->sign = sign;
172 r->sig[SIGSZ-1] = SIG_MSB >> 2;
175 static inline void
176 get_inf (r, sign)
177 REAL_VALUE_TYPE *r;
178 int sign;
180 memset (r, 0, sizeof (*r));
181 r->class = rvc_inf;
182 r->sign = sign;
186 /* Right-shift the significand of A by N bits; put the result in the
187 significand of R. If any one bits are shifted out, return true. */
189 static bool
190 sticky_rshift_significand (r, a, n)
191 REAL_VALUE_TYPE *r;
192 const REAL_VALUE_TYPE *a;
193 unsigned int n;
195 unsigned long sticky = 0;
196 unsigned int i, ofs = 0;
198 if (n >= HOST_BITS_PER_LONG)
200 for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
201 sticky |= a->sig[i];
202 n &= HOST_BITS_PER_LONG - 1;
205 if (n != 0)
207 sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
208 for (i = 0; i < SIGSZ; ++i)
210 r->sig[i]
211 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
212 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
213 << (HOST_BITS_PER_LONG - n)));
216 else
218 for (i = 0; ofs + i < SIGSZ; ++i)
219 r->sig[i] = a->sig[ofs + i];
220 for (; i < SIGSZ; ++i)
221 r->sig[i] = 0;
224 return sticky != 0;
227 /* Right-shift the significand of A by N bits; put the result in the
228 significand of R. */
230 static void
231 rshift_significand (r, a, n)
232 REAL_VALUE_TYPE *r;
233 const REAL_VALUE_TYPE *a;
234 unsigned int n;
236 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
238 n &= HOST_BITS_PER_LONG - 1;
239 if (n != 0)
241 for (i = 0; i < SIGSZ; ++i)
243 r->sig[i]
244 = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
245 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
246 << (HOST_BITS_PER_LONG - n)));
249 else
251 for (i = 0; ofs + i < SIGSZ; ++i)
252 r->sig[i] = a->sig[ofs + i];
253 for (; i < SIGSZ; ++i)
254 r->sig[i] = 0;
258 /* Left-shift the significand of A by N bits; put the result in the
259 significand of R. */
261 static void
262 lshift_significand (r, a, n)
263 REAL_VALUE_TYPE *r;
264 const REAL_VALUE_TYPE *a;
265 unsigned int n;
267 unsigned int i, ofs = n / HOST_BITS_PER_LONG;
269 n &= HOST_BITS_PER_LONG - 1;
270 if (n == 0)
272 for (i = 0; ofs + i < SIGSZ; ++i)
273 r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
274 for (; i < SIGSZ; ++i)
275 r->sig[SIGSZ-1-i] = 0;
277 else
278 for (i = 0; i < SIGSZ; ++i)
280 r->sig[SIGSZ-1-i]
281 = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
282 | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
283 >> (HOST_BITS_PER_LONG - n)));
287 /* Likewise, but N is specialized to 1. */
289 static inline void
290 lshift_significand_1 (r, a)
291 REAL_VALUE_TYPE *r;
292 const REAL_VALUE_TYPE *a;
294 unsigned int i;
296 for (i = SIGSZ - 1; i > 0; --i)
297 r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
298 r->sig[0] = a->sig[0] << 1;
301 /* Add the significands of A and B, placing the result in R. Return
302 true if there was carry out of the most significant word. */
304 static inline bool
305 add_significands (r, a, b)
306 REAL_VALUE_TYPE *r;
307 const REAL_VALUE_TYPE *a, *b;
309 bool carry = false;
310 int i;
312 for (i = 0; i < SIGSZ; ++i)
314 unsigned long ai = a->sig[i];
315 unsigned long ri = ai + b->sig[i];
317 if (carry)
319 carry = ri < ai;
320 carry |= ++ri == 0;
322 else
323 carry = ri < ai;
325 r->sig[i] = ri;
328 return carry;
331 /* Subtract the significands of A and B, placing the result in R. CARRY is
332 true if there's a borrow incoming to the least significant word.
333 Return true if there was borrow out of the most significant word. */
335 static inline bool
336 sub_significands (r, a, b, carry)
337 REAL_VALUE_TYPE *r;
338 const REAL_VALUE_TYPE *a, *b;
339 int carry;
341 int i;
343 for (i = 0; i < SIGSZ; ++i)
345 unsigned long ai = a->sig[i];
346 unsigned long ri = ai - b->sig[i];
348 if (carry)
350 carry = ri > ai;
351 carry |= ~--ri == 0;
353 else
354 carry = ri > ai;
356 r->sig[i] = ri;
359 return carry;
362 /* Negate the significand A, placing the result in R. */
364 static inline void
365 neg_significand (r, a)
366 REAL_VALUE_TYPE *r;
367 const REAL_VALUE_TYPE *a;
369 bool carry = true;
370 int i;
372 for (i = 0; i < SIGSZ; ++i)
374 unsigned long ri, ai = a->sig[i];
376 if (carry)
378 if (ai)
380 ri = -ai;
381 carry = false;
383 else
384 ri = ai;
386 else
387 ri = ~ai;
389 r->sig[i] = ri;
393 /* Compare significands. Return tri-state vs zero. */
395 static inline int
396 cmp_significands (a, b)
397 const REAL_VALUE_TYPE *a, *b;
399 int i;
401 for (i = SIGSZ - 1; i >= 0; --i)
403 unsigned long ai = a->sig[i];
404 unsigned long bi = b->sig[i];
406 if (ai > bi)
407 return 1;
408 if (ai < bi)
409 return -1;
412 return 0;
415 /* Return true if A is nonzero. */
417 static inline int
418 cmp_significand_0 (a)
419 const REAL_VALUE_TYPE *a;
421 int i;
423 for (i = SIGSZ - 1; i >= 0; --i)
424 if (a->sig[i])
425 return 1;
427 return 0;
430 /* Set bit N of the significand of R. */
432 static inline void
433 set_significand_bit (r, n)
434 REAL_VALUE_TYPE *r;
435 unsigned int n;
437 r->sig[n / HOST_BITS_PER_LONG]
438 |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
441 /* Clear bit N of the significand of R. */
443 static inline void
444 clear_significand_bit (r, n)
445 REAL_VALUE_TYPE *r;
446 unsigned int n;
448 r->sig[n / HOST_BITS_PER_LONG]
449 &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
452 /* Test bit N of the significand of R. */
454 static inline bool
455 test_significand_bit (r, n)
456 REAL_VALUE_TYPE *r;
457 unsigned int n;
459 /* ??? Compiler bug here if we return this expression directly.
460 The conversion to bool strips the "&1" and we wind up testing
461 e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
462 int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
463 return t;
466 /* Clear bits 0..N-1 of the significand of R. */
468 static void
469 clear_significand_below (r, n)
470 REAL_VALUE_TYPE *r;
471 unsigned int n;
473 int i, w = n / HOST_BITS_PER_LONG;
475 for (i = 0; i < w; ++i)
476 r->sig[i] = 0;
478 r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
481 /* Divide the significands of A and B, placing the result in R. Return
482 true if the division was inexact. */
484 static inline bool
485 div_significands (r, a, b)
486 REAL_VALUE_TYPE *r;
487 const REAL_VALUE_TYPE *a, *b;
489 REAL_VALUE_TYPE u;
490 int i, bit = SIGNIFICAND_BITS - 1;
491 unsigned long msb, inexact;
493 u = *a;
494 memset (r->sig, 0, sizeof (r->sig));
496 msb = 0;
497 goto start;
500 msb = u.sig[SIGSZ-1] & SIG_MSB;
501 lshift_significand_1 (&u, &u);
502 start:
503 if (msb || cmp_significands (&u, b) >= 0)
505 sub_significands (&u, &u, b, 0);
506 set_significand_bit (r, bit);
509 while (--bit >= 0);
511 for (i = 0, inexact = 0; i < SIGSZ; i++)
512 inexact |= u.sig[i];
514 return inexact != 0;
517 /* Adjust the exponent and significand of R such that the most
518 significant bit is set. We underflow to zero and overflow to
519 infinity here, without denormals. (The intermediate representation
520 exponent is large enough to handle target denormals normalized.) */
522 static void
523 normalize (r)
524 REAL_VALUE_TYPE *r;
526 int shift = 0, exp;
527 int i, j;
529 /* Find the first word that is nonzero. */
530 for (i = SIGSZ - 1; i >= 0; i--)
531 if (r->sig[i] == 0)
532 shift += HOST_BITS_PER_LONG;
533 else
534 break;
536 /* Zero significand flushes to zero. */
537 if (i < 0)
539 r->class = rvc_zero;
540 r->exp = 0;
541 return;
544 /* Find the first bit that is nonzero. */
545 for (j = 0; ; j++)
546 if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
547 break;
548 shift += j;
550 if (shift > 0)
552 exp = r->exp - shift;
553 if (exp > MAX_EXP)
554 get_inf (r, r->sign);
555 else if (exp < -MAX_EXP)
556 get_zero (r, r->sign);
557 else
559 r->exp = exp;
560 lshift_significand (r, r, shift);
565 /* Return R = A + (SUBTRACT_P ? -B : B). */
567 static void
568 do_add (r, a, b, subtract_p)
569 REAL_VALUE_TYPE *r;
570 const REAL_VALUE_TYPE *a, *b;
571 int subtract_p;
573 int dexp, sign, exp;
574 REAL_VALUE_TYPE t;
575 bool inexact = false;
577 /* Determine if we need to add or subtract. */
578 sign = a->sign;
579 subtract_p = (sign ^ b->sign) ^ subtract_p;
581 switch (CLASS2 (a->class, b->class))
583 case CLASS2 (rvc_zero, rvc_zero):
584 /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
585 get_zero (r, sign & !subtract_p);
586 return;
588 case CLASS2 (rvc_zero, rvc_normal):
589 case CLASS2 (rvc_zero, rvc_inf):
590 case CLASS2 (rvc_zero, rvc_nan):
591 /* 0 + ANY = ANY. */
592 case CLASS2 (rvc_normal, rvc_nan):
593 case CLASS2 (rvc_inf, rvc_nan):
594 case CLASS2 (rvc_nan, rvc_nan):
595 /* ANY + NaN = NaN. */
596 case CLASS2 (rvc_normal, rvc_inf):
597 /* R + Inf = Inf. */
598 *r = *b;
599 r->sign = sign ^ subtract_p;
600 return;
602 case CLASS2 (rvc_normal, rvc_zero):
603 case CLASS2 (rvc_inf, rvc_zero):
604 case CLASS2 (rvc_nan, rvc_zero):
605 /* ANY + 0 = ANY. */
606 case CLASS2 (rvc_nan, rvc_normal):
607 case CLASS2 (rvc_nan, rvc_inf):
608 /* NaN + ANY = NaN. */
609 case CLASS2 (rvc_inf, rvc_normal):
610 /* Inf + R = Inf. */
611 *r = *a;
612 return;
614 case CLASS2 (rvc_inf, rvc_inf):
615 if (subtract_p)
616 /* Inf - Inf = NaN. */
617 get_canonical_qnan (r, 0);
618 else
619 /* Inf + Inf = Inf. */
620 *r = *a;
621 return;
623 case CLASS2 (rvc_normal, rvc_normal):
624 break;
626 default:
627 abort ();
630 /* Swap the arguments such that A has the larger exponent. */
631 dexp = a->exp - b->exp;
632 if (dexp < 0)
634 const REAL_VALUE_TYPE *t;
635 t = a, a = b, b = t;
636 dexp = -dexp;
637 sign ^= subtract_p;
639 exp = a->exp;
641 /* If the exponents are not identical, we need to shift the
642 significand of B down. */
643 if (dexp > 0)
645 /* If the exponents are too far apart, the significands
646 do not overlap, which makes the subtraction a noop. */
647 if (dexp >= SIGNIFICAND_BITS)
649 *r = *a;
650 r->sign = sign;
651 return;
654 inexact |= sticky_rshift_significand (&t, b, dexp);
655 b = &t;
658 if (subtract_p)
660 if (sub_significands (r, a, b, inexact))
662 /* We got a borrow out of the subtraction. That means that
663 A and B had the same exponent, and B had the larger
664 significand. We need to swap the sign and negate the
665 significand. */
666 sign ^= 1;
667 neg_significand (r, r);
670 else
672 if (add_significands (r, a, b))
674 /* We got carry out of the addition. This means we need to
675 shift the significand back down one bit and increase the
676 exponent. */
677 inexact |= sticky_rshift_significand (r, r, 1);
678 r->sig[SIGSZ-1] |= SIG_MSB;
679 if (++exp > MAX_EXP)
681 get_inf (r, sign);
682 return;
687 r->class = rvc_normal;
688 r->sign = sign;
689 r->exp = exp;
691 /* Re-normalize the result. */
692 normalize (r);
694 /* Special case: if the subtraction results in zero, the result
695 is positive. */
696 if (r->class == rvc_zero)
697 r->sign = 0;
698 else
699 r->sig[0] |= inexact;
702 /* Return R = A * B. */
704 static void
705 do_multiply (r, a, b)
706 REAL_VALUE_TYPE *r;
707 const REAL_VALUE_TYPE *a, *b;
709 REAL_VALUE_TYPE u, t, *rr;
710 unsigned int i, j, k;
711 int sign = a->sign ^ b->sign;
713 switch (CLASS2 (a->class, b->class))
715 case CLASS2 (rvc_zero, rvc_zero):
716 case CLASS2 (rvc_zero, rvc_normal):
717 case CLASS2 (rvc_normal, rvc_zero):
718 /* +-0 * ANY = 0 with appropriate sign. */
719 get_zero (r, sign);
720 return;
722 case CLASS2 (rvc_zero, rvc_nan):
723 case CLASS2 (rvc_normal, rvc_nan):
724 case CLASS2 (rvc_inf, rvc_nan):
725 case CLASS2 (rvc_nan, rvc_nan):
726 /* ANY * NaN = NaN. */
727 *r = *b;
728 r->sign = sign;
729 return;
731 case CLASS2 (rvc_nan, rvc_zero):
732 case CLASS2 (rvc_nan, rvc_normal):
733 case CLASS2 (rvc_nan, rvc_inf):
734 /* NaN * ANY = NaN. */
735 *r = *a;
736 r->sign = sign;
737 return;
739 case CLASS2 (rvc_zero, rvc_inf):
740 case CLASS2 (rvc_inf, rvc_zero):
741 /* 0 * Inf = NaN */
742 get_canonical_qnan (r, sign);
743 return;
745 case CLASS2 (rvc_inf, rvc_inf):
746 case CLASS2 (rvc_normal, rvc_inf):
747 case CLASS2 (rvc_inf, rvc_normal):
748 /* Inf * Inf = Inf, R * Inf = Inf */
749 overflow:
750 get_inf (r, sign);
751 return;
753 case CLASS2 (rvc_normal, rvc_normal):
754 break;
756 default:
757 abort ();
760 if (r == a || r == b)
761 rr = &t;
762 else
763 rr = r;
764 get_zero (rr, 0);
766 /* Collect all the partial products. Since we don't have sure access
767 to a widening multiply, we split each long into two half-words.
769 Consider the long-hand form of a four half-word multiplication:
771 A B C D
772 * E F G H
773 --------------
774 DE DF DG DH
775 CE CF CG CH
776 BE BF BG BH
777 AE AF AG AH
779 We construct partial products of the widened half-word products
780 that are known to not overlap, e.g. DF+DH. Each such partial
781 product is given its proper exponent, which allows us to sum them
782 and obtain the finished product. */
784 for (i = 0; i < SIGSZ * 2; ++i)
786 unsigned long ai = a->sig[i / 2];
787 if (i & 1)
788 ai >>= HOST_BITS_PER_LONG / 2;
789 else
790 ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
792 if (ai == 0)
793 continue;
795 for (j = 0; j < 2; ++j)
797 int exp = (a->exp - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
798 + (b->exp - (1-j)*(HOST_BITS_PER_LONG/2)));
800 if (exp > MAX_EXP)
801 goto overflow;
802 if (exp < -MAX_EXP)
803 /* Would underflow to zero, which we shouldn't bother adding. */
804 continue;
806 u.class = rvc_normal;
807 u.sign = 0;
808 u.exp = exp;
810 for (k = j; k < SIGSZ * 2; k += 2)
812 unsigned long bi = b->sig[k / 2];
813 if (k & 1)
814 bi >>= HOST_BITS_PER_LONG / 2;
815 else
816 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
818 u.sig[k / 2] = ai * bi;
821 normalize (&u);
822 do_add (rr, rr, &u, 0);
826 rr->sign = sign;
827 if (rr != r)
828 *r = t;
831 /* Return R = A / B. */
833 static void
834 do_divide (r, a, b)
835 REAL_VALUE_TYPE *r;
836 const REAL_VALUE_TYPE *a, *b;
838 int exp, sign = a->sign ^ b->sign;
839 REAL_VALUE_TYPE t, *rr;
840 bool inexact;
842 switch (CLASS2 (a->class, b->class))
844 case CLASS2 (rvc_zero, rvc_zero):
845 /* 0 / 0 = NaN. */
846 case CLASS2 (rvc_inf, rvc_inf):
847 /* Inf / Inf = NaN. */
848 get_canonical_qnan (r, sign);
849 return;
851 case CLASS2 (rvc_zero, rvc_normal):
852 case CLASS2 (rvc_zero, rvc_inf):
853 /* 0 / ANY = 0. */
854 case CLASS2 (rvc_normal, rvc_inf):
855 /* R / Inf = 0. */
856 underflow:
857 get_zero (r, sign);
858 return;
860 case CLASS2 (rvc_normal, rvc_zero):
861 /* R / 0 = Inf. */
862 case CLASS2 (rvc_inf, rvc_zero):
863 /* Inf / 0 = Inf. */
864 get_inf (r, sign);
865 return;
867 case CLASS2 (rvc_zero, rvc_nan):
868 case CLASS2 (rvc_normal, rvc_nan):
869 case CLASS2 (rvc_inf, rvc_nan):
870 case CLASS2 (rvc_nan, rvc_nan):
871 /* ANY / NaN = NaN. */
872 *r = *b;
873 r->sign = sign;
874 return;
876 case CLASS2 (rvc_nan, rvc_zero):
877 case CLASS2 (rvc_nan, rvc_normal):
878 case CLASS2 (rvc_nan, rvc_inf):
879 /* NaN / ANY = NaN. */
880 *r = *a;
881 r->sign = sign;
882 return;
884 case CLASS2 (rvc_inf, rvc_normal):
885 /* Inf / R = Inf. */
886 overflow:
887 get_inf (r, sign);
888 return;
890 case CLASS2 (rvc_normal, rvc_normal):
891 break;
893 default:
894 abort ();
897 if (r == a || r == b)
898 rr = &t;
899 else
900 rr = r;
902 rr->class = rvc_normal;
903 rr->sign = sign;
905 exp = a->exp - b->exp + 1;
906 if (exp > MAX_EXP)
907 goto overflow;
908 if (exp < -MAX_EXP)
909 goto underflow;
910 rr->exp = exp;
912 inexact = div_significands (rr, a, b);
914 /* Re-normalize the result. */
915 normalize (rr);
916 rr->sig[0] |= inexact;
918 if (rr != r)
919 *r = t;
922 /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
923 one of the two operands is a NaN. */
925 static int
926 do_compare (a, b, nan_result)
927 const REAL_VALUE_TYPE *a, *b;
928 int nan_result;
930 int ret;
932 switch (CLASS2 (a->class, b->class))
934 case CLASS2 (rvc_zero, rvc_zero):
935 /* Sign of zero doesn't matter for compares. */
936 return 0;
938 case CLASS2 (rvc_inf, rvc_zero):
939 case CLASS2 (rvc_inf, rvc_normal):
940 case CLASS2 (rvc_normal, rvc_zero):
941 return (a->sign ? -1 : 1);
943 case CLASS2 (rvc_inf, rvc_inf):
944 return -a->sign - -b->sign;
946 case CLASS2 (rvc_zero, rvc_normal):
947 case CLASS2 (rvc_zero, rvc_inf):
948 case CLASS2 (rvc_normal, rvc_inf):
949 return (b->sign ? 1 : -1);
951 case CLASS2 (rvc_zero, rvc_nan):
952 case CLASS2 (rvc_normal, rvc_nan):
953 case CLASS2 (rvc_inf, rvc_nan):
954 case CLASS2 (rvc_nan, rvc_nan):
955 case CLASS2 (rvc_nan, rvc_zero):
956 case CLASS2 (rvc_nan, rvc_normal):
957 case CLASS2 (rvc_nan, rvc_inf):
958 return nan_result;
960 case CLASS2 (rvc_normal, rvc_normal):
961 break;
963 default:
964 abort ();
967 if (a->sign != b->sign)
968 return -a->sign - -b->sign;
970 if (a->exp > b->exp)
971 ret = 1;
972 else if (a->exp < b->exp)
973 ret = -1;
974 else
975 ret = cmp_significands (a, b);
977 return (a->sign ? -ret : ret);
980 /* Return A truncated to an integral value toward zero. */
982 static void
983 do_fix_trunc (r, a)
984 REAL_VALUE_TYPE *r;
985 const REAL_VALUE_TYPE *a;
987 *r = *a;
989 switch (r->class)
991 case rvc_zero:
992 case rvc_inf:
993 case rvc_nan:
994 break;
996 case rvc_normal:
997 if (r->exp <= 0)
998 get_zero (r, r->sign);
999 else if (r->exp < SIGNIFICAND_BITS)
1000 clear_significand_below (r, SIGNIFICAND_BITS - r->exp);
1001 break;
1003 default:
1004 abort ();
1008 /* Perform the binary or unary operation described by CODE.
1009 For a unary operation, leave OP1 NULL. */
1011 void
1012 real_arithmetic (r, icode, op0, op1)
1013 REAL_VALUE_TYPE *r;
1014 int icode;
1015 const REAL_VALUE_TYPE *op0, *op1;
1017 enum tree_code code = icode;
1019 switch (code)
1021 case PLUS_EXPR:
1022 do_add (r, op0, op1, 0);
1023 break;
1025 case MINUS_EXPR:
1026 do_add (r, op0, op1, 1);
1027 break;
1029 case MULT_EXPR:
1030 do_multiply (r, op0, op1);
1031 break;
1033 case RDIV_EXPR:
1034 do_divide (r, op0, op1);
1035 break;
1037 case MIN_EXPR:
1038 if (op1->class == rvc_nan)
1039 *r = *op1;
1040 else if (do_compare (op0, op1, -1) < 0)
1041 *r = *op0;
1042 else
1043 *r = *op1;
1044 break;
1046 case MAX_EXPR:
1047 if (op1->class == rvc_nan)
1048 *r = *op1;
1049 else if (do_compare (op0, op1, 1) < 0)
1050 *r = *op1;
1051 else
1052 *r = *op0;
1053 break;
1055 case NEGATE_EXPR:
1056 *r = *op0;
1057 r->sign ^= 1;
1058 break;
1060 case ABS_EXPR:
1061 *r = *op0;
1062 r->sign = 0;
1063 break;
1065 case FIX_TRUNC_EXPR:
1066 do_fix_trunc (r, op0);
1067 break;
1069 default:
1070 abort ();
1074 /* Legacy. Similar, but return the result directly. */
1076 REAL_VALUE_TYPE
1077 real_arithmetic2 (icode, op0, op1)
1078 int icode;
1079 const REAL_VALUE_TYPE *op0, *op1;
1081 REAL_VALUE_TYPE r;
1082 real_arithmetic (&r, icode, op0, op1);
1083 return r;
1086 bool
1087 real_compare (icode, op0, op1)
1088 int icode;
1089 const REAL_VALUE_TYPE *op0, *op1;
1091 enum tree_code code = icode;
1093 switch (code)
1095 case LT_EXPR:
1096 return do_compare (op0, op1, 1) < 0;
1097 case LE_EXPR:
1098 return do_compare (op0, op1, 1) <= 0;
1099 case GT_EXPR:
1100 return do_compare (op0, op1, -1) > 0;
1101 case GE_EXPR:
1102 return do_compare (op0, op1, -1) >= 0;
1103 case EQ_EXPR:
1104 return do_compare (op0, op1, -1) == 0;
1105 case NE_EXPR:
1106 return do_compare (op0, op1, -1) != 0;
1107 case UNORDERED_EXPR:
1108 return op0->class == rvc_nan || op1->class == rvc_nan;
1109 case ORDERED_EXPR:
1110 return op0->class != rvc_nan && op1->class != rvc_nan;
1111 case UNLT_EXPR:
1112 return do_compare (op0, op1, -1) < 0;
1113 case UNLE_EXPR:
1114 return do_compare (op0, op1, -1) <= 0;
1115 case UNGT_EXPR:
1116 return do_compare (op0, op1, 1) > 0;
1117 case UNGE_EXPR:
1118 return do_compare (op0, op1, 1) >= 0;
1119 case UNEQ_EXPR:
1120 return do_compare (op0, op1, 0) == 0;
1122 default:
1123 abort ();
1127 /* Return floor log2(R). */
1130 real_exponent (r)
1131 const REAL_VALUE_TYPE *r;
1133 switch (r->class)
1135 case rvc_zero:
1136 return 0;
1137 case rvc_inf:
1138 case rvc_nan:
1139 return (unsigned int)-1 >> 1;
1140 case rvc_normal:
1141 return r->exp;
1142 default:
1143 abort ();
1147 /* R = OP0 * 2**EXP. */
1149 void
1150 real_ldexp (r, op0, exp)
1151 REAL_VALUE_TYPE *r;
1152 const REAL_VALUE_TYPE *op0;
1153 int exp;
1155 *r = *op0;
1156 switch (r->class)
1158 case rvc_zero:
1159 case rvc_inf:
1160 case rvc_nan:
1161 break;
1163 case rvc_normal:
1164 exp += op0->exp;
1165 if (exp > MAX_EXP)
1166 get_inf (r, r->sign);
1167 else if (exp < -MAX_EXP)
1168 get_zero (r, r->sign);
1169 else
1170 r->exp = exp;
1171 break;
1173 default:
1174 abort ();
1178 /* Determine whether a floating-point value X is infinite. */
1180 bool
1181 real_isinf (r)
1182 const REAL_VALUE_TYPE *r;
1184 return (r->class == rvc_inf);
1187 /* Determine whether a floating-point value X is a NaN. */
1189 bool
1190 real_isnan (r)
1191 const REAL_VALUE_TYPE *r;
1193 return (r->class == rvc_nan);
1196 /* Determine whether a floating-point value X is negative. */
1198 bool
1199 real_isneg (r)
1200 const REAL_VALUE_TYPE *r;
1202 return r->sign;
1205 /* Determine whether a floating-point value X is minus zero. */
1207 bool
1208 real_isnegzero (r)
1209 const REAL_VALUE_TYPE *r;
1211 return r->sign && r->class == rvc_zero;
1214 /* Compare two floating-point objects for bitwise identity. */
1216 extern bool
1217 real_identical (a, b)
1218 const REAL_VALUE_TYPE *a, *b;
1220 int i;
1222 if (a->class != b->class)
1223 return false;
1224 if (a->sign != b->sign)
1225 return false;
1227 switch (a->class)
1229 case rvc_zero:
1230 case rvc_inf:
1231 break;
1233 case rvc_normal:
1234 if (a->exp != b->exp)
1235 return false;
1236 /* FALLTHRU */
1237 case rvc_nan:
1238 for (i = 0; i < SIGSZ; ++i)
1239 if (a->sig[i] != b->sig[i])
1240 return false;
1241 break;
1243 default:
1244 abort ();
1247 return true;
1250 /* Try to change R into its exact multiplicative inverse in machine
1251 mode MODE. Return true if successful. */
1253 bool
1254 exact_real_inverse (mode, r)
1255 enum machine_mode mode;
1256 REAL_VALUE_TYPE *r;
1258 const REAL_VALUE_TYPE *one = real_digit (1);
1259 REAL_VALUE_TYPE u;
1260 int i;
1262 if (r->class != rvc_normal)
1263 return false;
1265 /* Check for a power of two: all significand bits zero except the MSB. */
1266 for (i = 0; i < SIGSZ-1; ++i)
1267 if (r->sig[i] != 0)
1268 return false;
1269 if (r->sig[SIGSZ-1] != SIG_MSB)
1270 return false;
1272 /* Find the inverse and truncate to the required mode. */
1273 do_divide (&u, one, r);
1274 real_convert (&u, mode, &u);
1276 /* The rounding may have overflowed. */
1277 if (u.class != rvc_normal)
1278 return false;
1279 for (i = 0; i < SIGSZ-1; ++i)
1280 if (u.sig[i] != 0)
1281 return false;
1282 if (u.sig[SIGSZ-1] != SIG_MSB)
1283 return false;
1285 *r = u;
1286 return true;
1289 /* Render R as an integer. */
1291 HOST_WIDE_INT
1292 real_to_integer (r)
1293 const REAL_VALUE_TYPE *r;
1295 unsigned HOST_WIDE_INT i;
1297 switch (r->class)
1299 case rvc_zero:
1300 underflow:
1301 return 0;
1303 case rvc_inf:
1304 case rvc_nan:
1305 overflow:
1306 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1307 if (!r->sign)
1308 i--;
1309 return i;
1311 case rvc_normal:
1312 if (r->exp <= 0)
1313 goto underflow;
1314 if (r->exp > HOST_BITS_PER_WIDE_INT)
1315 goto overflow;
1317 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1318 i = r->sig[SIGSZ-1];
1319 else if (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG)
1321 i = r->sig[SIGSZ-1];
1322 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1323 i |= r->sig[SIGSZ-2];
1325 else
1326 abort ();
1328 i >>= HOST_BITS_PER_WIDE_INT - r->exp;
1330 if (r->sign)
1331 i = -i;
1332 return i;
1334 default:
1335 abort ();
1339 /* Likewise, but to an integer pair, HI+LOW. */
1341 void
1342 real_to_integer2 (plow, phigh, r)
1343 HOST_WIDE_INT *plow, *phigh;
1344 const REAL_VALUE_TYPE *r;
1346 REAL_VALUE_TYPE t;
1347 HOST_WIDE_INT low, high;
1348 int exp;
1350 switch (r->class)
1352 case rvc_zero:
1353 underflow:
1354 low = high = 0;
1355 break;
1357 case rvc_inf:
1358 case rvc_nan:
1359 overflow:
1360 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1361 if (r->sign)
1362 low = 0;
1363 else
1365 high--;
1366 low = -1;
1368 break;
1370 case rvc_normal:
1371 exp = r->exp;
1372 if (exp <= 0)
1373 goto underflow;
1374 if (exp >= 2*HOST_BITS_PER_WIDE_INT)
1375 goto overflow;
1377 rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
1378 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1380 high = t.sig[SIGSZ-1];
1381 low = t.sig[SIGSZ-2];
1383 else if (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG)
1385 high = t.sig[SIGSZ-1];
1386 high = high << (HOST_BITS_PER_LONG - 1) << 1;
1387 high |= t.sig[SIGSZ-2];
1389 low = t.sig[SIGSZ-3];
1390 low = low << (HOST_BITS_PER_LONG - 1) << 1;
1391 low |= t.sig[SIGSZ-4];
1393 else
1394 abort ();
1396 if (r->sign)
1398 if (low == 0)
1399 high = -high;
1400 else
1401 low = -low, high = ~high;
1403 break;
1405 default:
1406 abort ();
1409 *plow = low;
1410 *phigh = high;
1413 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1414 of NUM / DEN. Return the quotient and place the remainder in NUM.
1415 It is expected that NUM / DEN are close enough that the quotient is
1416 small. */
1418 static unsigned long
1419 rtd_divmod (num, den)
1420 REAL_VALUE_TYPE *num, *den;
1422 unsigned long q, msb;
1423 int expn = num->exp, expd = den->exp;
1425 if (expn < expd)
1426 return 0;
1428 q = msb = 0;
1429 goto start;
1432 msb = num->sig[SIGSZ-1] & SIG_MSB;
1433 q <<= 1;
1434 lshift_significand_1 (num, num);
1435 start:
1436 if (msb || cmp_significands (num, den) >= 0)
1438 sub_significands (num, num, den, 0);
1439 q |= 1;
1442 while (--expn >= expd);
1444 num->exp = expd;
1445 normalize (num);
1447 return q;
1450 /* Render R as a decimal floating point constant. Emit DIGITS significant
1451 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1452 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1453 zeros. */
1455 #define M_LOG10_2 0.30102999566398119521
1457 void
1458 real_to_decimal (str, r_orig, buf_size, digits, crop_trailing_zeros)
1459 char *str;
1460 const REAL_VALUE_TYPE *r_orig;
1461 size_t buf_size, digits;
1462 int crop_trailing_zeros;
1464 const REAL_VALUE_TYPE *one, *ten;
1465 REAL_VALUE_TYPE r, pten, u, v;
1466 int dec_exp, cmp_one, digit;
1467 size_t max_digits;
1468 char *p, *first, *last;
1469 bool sign;
1471 r = *r_orig;
1472 switch (r.class)
1474 case rvc_zero:
1475 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1476 return;
1477 case rvc_normal:
1478 break;
1479 case rvc_inf:
1480 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1481 return;
1482 case rvc_nan:
1483 /* ??? Print the significand as well, if not canonical? */
1484 strcpy (str, (r.sign ? "-NaN" : "+NaN"));
1485 return;
1486 default:
1487 abort ();
1490 /* Bound the number of digits printed by the size of the representation. */
1491 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1492 if (digits == 0 || digits > max_digits)
1493 digits = max_digits;
1495 /* Estimate the decimal exponent, and compute the length of the string it
1496 will print as. Be conservative and add one to account for possible
1497 overflow or rounding error. */
1498 dec_exp = r.exp * M_LOG10_2;
1499 for (max_digits = 1; dec_exp ; max_digits++)
1500 dec_exp /= 10;
1502 /* Bound the number of digits printed by the size of the output buffer. */
1503 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1504 if (max_digits > buf_size)
1505 abort ();
1506 if (digits > max_digits)
1507 digits = max_digits;
1509 one = real_digit (1);
1510 ten = ten_to_ptwo (0);
1512 sign = r.sign;
1513 r.sign = 0;
1515 dec_exp = 0;
1516 pten = *one;
1518 cmp_one = do_compare (&r, one, 0);
1519 if (cmp_one > 0)
1521 int m;
1523 /* Number is greater than one. Convert significand to an integer
1524 and strip trailing decimal zeros. */
1526 u = r;
1527 u.exp = SIGNIFICAND_BITS - 1;
1529 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1530 m = floor_log2 (max_digits);
1532 /* Iterate over the bits of the possible powers of 10 that might
1533 be present in U and eliminate them. That is, if we find that
1534 10**2**M divides U evenly, keep the division and increase
1535 DEC_EXP by 2**M. */
1538 REAL_VALUE_TYPE t;
1540 do_divide (&t, &u, ten_to_ptwo (m));
1541 do_fix_trunc (&v, &t);
1542 if (cmp_significands (&v, &t) == 0)
1544 u = t;
1545 dec_exp += 1 << m;
1548 while (--m >= 0);
1550 /* Revert the scaling to integer that we performed earlier. */
1551 u.exp += r.exp - (SIGNIFICAND_BITS - 1);
1552 r = u;
1554 /* Find power of 10. Do this by dividing out 10**2**M when
1555 this is larger than the current remainder. Fill PTEN with
1556 the power of 10 that we compute. */
1557 if (r.exp > 0)
1559 m = floor_log2 ((int)(r.exp * M_LOG10_2)) + 1;
1562 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1563 if (do_compare (&u, ptentwo, 0) >= 0)
1565 do_divide (&u, &u, ptentwo);
1566 do_multiply (&pten, &pten, ptentwo);
1567 dec_exp += 1 << m;
1570 while (--m >= 0);
1572 else
1573 /* We managed to divide off enough tens in the above reduction
1574 loop that we've now got a negative exponent. Fall into the
1575 less-than-one code to compute the proper value for PTEN. */
1576 cmp_one = -1;
1578 if (cmp_one < 0)
1580 int m;
1582 /* Number is less than one. Pad significand with leading
1583 decimal zeros. */
1585 v = r;
1586 while (1)
1588 /* Stop if we'd shift bits off the bottom. */
1589 if (v.sig[0] & 7)
1590 break;
1592 do_multiply (&u, &v, ten);
1594 /* Stop if we're now >= 1. */
1595 if (u.exp > 0)
1596 break;
1598 v = u;
1599 dec_exp -= 1;
1601 r = v;
1603 /* Find power of 10. Do this by multiplying in P=10**2**M when
1604 the current remainder is smaller than 1/P. Fill PTEN with the
1605 power of 10 that we compute. */
1606 m = floor_log2 ((int)(-r.exp * M_LOG10_2)) + 1;
1609 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1610 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1612 if (do_compare (&v, ptenmtwo, 0) <= 0)
1614 do_multiply (&v, &v, ptentwo);
1615 do_multiply (&pten, &pten, ptentwo);
1616 dec_exp -= 1 << m;
1619 while (--m >= 0);
1621 /* Invert the positive power of 10 that we've collected so far. */
1622 do_divide (&pten, one, &pten);
1625 p = str;
1626 if (sign)
1627 *p++ = '-';
1628 first = p++;
1630 /* At this point, PTEN should contain the nearest power of 10 smaller
1631 than R, such that this division produces the first digit.
1633 Using a divide-step primitive that returns the complete integral
1634 remainder avoids the rounding error that would be produced if
1635 we were to use do_divide here and then simply multiply by 10 for
1636 each subsequent digit. */
1638 digit = rtd_divmod (&r, &pten);
1640 /* Be prepared for error in that division via underflow ... */
1641 if (digit == 0 && cmp_significand_0 (&r))
1643 /* Multiply by 10 and try again. */
1644 do_multiply (&r, &r, ten);
1645 digit = rtd_divmod (&r, &pten);
1646 dec_exp -= 1;
1647 if (digit == 0)
1648 abort ();
1651 /* ... or overflow. */
1652 if (digit == 10)
1654 *p++ = '1';
1655 if (--digits > 0)
1656 *p++ = '0';
1657 dec_exp += 1;
1659 else if (digit > 10)
1660 abort ();
1661 else
1662 *p++ = digit + '0';
1664 /* Generate subsequent digits. */
1665 while (--digits > 0)
1667 do_multiply (&r, &r, ten);
1668 digit = rtd_divmod (&r, &pten);
1669 *p++ = digit + '0';
1671 last = p;
1673 /* Generate one more digit with which to do rounding. */
1674 do_multiply (&r, &r, ten);
1675 digit = rtd_divmod (&r, &pten);
1677 /* Round the result. */
1678 if (digit == 5)
1680 /* Round to nearest. If R is nonzero there are additional
1681 nonzero digits to be extracted. */
1682 if (cmp_significand_0 (&r))
1683 digit++;
1684 /* Round to even. */
1685 else if ((p[-1] - '0') & 1)
1686 digit++;
1688 if (digit > 5)
1690 while (p > first)
1692 digit = *--p;
1693 if (digit == '9')
1694 *p = '0';
1695 else
1697 *p = digit + 1;
1698 break;
1702 /* Carry out of the first digit. This means we had all 9's and
1703 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1704 if (p == first)
1706 first[1] = '1';
1707 dec_exp++;
1711 /* Insert the decimal point. */
1712 first[0] = first[1];
1713 first[1] = '.';
1715 /* If requested, drop trailing zeros. Never crop past "1.0". */
1716 if (crop_trailing_zeros)
1717 while (last > first + 3 && last[-1] == '0')
1718 last--;
1720 /* Append the exponent. */
1721 sprintf (last, "e%+d", dec_exp);
1724 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1725 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1726 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1727 strip trailing zeros. */
1729 void
1730 real_to_hexadecimal (str, r, buf_size, digits, crop_trailing_zeros)
1731 char *str;
1732 const REAL_VALUE_TYPE *r;
1733 size_t buf_size, digits;
1734 int crop_trailing_zeros;
1736 int i, j, exp = r->exp;
1737 char *p, *first;
1738 char exp_buf[16];
1739 size_t max_digits;
1741 switch (r->class)
1743 case rvc_zero:
1744 exp = 0;
1745 break;
1746 case rvc_normal:
1747 break;
1748 case rvc_inf:
1749 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1750 return;
1751 case rvc_nan:
1752 /* ??? Print the significand as well, if not canonical? */
1753 strcpy (str, (r->sign ? "-NaN" : "+NaN"));
1754 return;
1755 default:
1756 abort ();
1759 if (digits == 0)
1760 digits = SIGNIFICAND_BITS / 4;
1762 /* Bound the number of digits printed by the size of the output buffer. */
1764 sprintf (exp_buf, "p%+d", exp);
1765 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1766 if (max_digits > buf_size)
1767 abort ();
1768 if (digits > max_digits)
1769 digits = max_digits;
1771 p = str;
1772 if (r->sign)
1773 *p++ = '-';
1774 *p++ = '0';
1775 *p++ = 'x';
1776 *p++ = '0';
1777 *p++ = '.';
1778 first = p;
1780 for (i = SIGSZ - 1; i >= 0; --i)
1781 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1783 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1784 if (--digits == 0)
1785 goto out;
1788 out:
1789 if (crop_trailing_zeros)
1790 while (p > first + 1 && p[-1] == '0')
1791 p--;
1793 sprintf (p, "p%+d", exp);
1796 /* Initialize R from a decimal or hexadecimal string. The string is
1797 assumed to have been syntax checked already. */
1799 void
1800 real_from_string (r, str)
1801 REAL_VALUE_TYPE *r;
1802 const char *str;
1804 int exp = 0;
1805 bool sign = false;
1807 get_zero (r, 0);
1809 if (*str == '-')
1811 sign = true;
1812 str++;
1814 else if (*str == '+')
1815 str++;
1817 if (str[0] == '0' && str[1] == 'x')
1819 /* Hexadecimal floating point. */
1820 int pos = SIGNIFICAND_BITS - 4, d;
1822 str += 2;
1824 while (*str == '0')
1825 str++;
1826 while (1)
1828 d = hex_value (*str);
1829 if (d == _hex_bad)
1830 break;
1831 if (pos >= 0)
1833 r->sig[pos / HOST_BITS_PER_LONG]
1834 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1835 pos -= 4;
1837 exp += 4;
1838 str++;
1840 if (*str == '.')
1842 str++;
1843 if (pos == SIGNIFICAND_BITS - 4)
1845 while (*str == '0')
1846 str++, exp -= 4;
1848 while (1)
1850 d = hex_value (*str);
1851 if (d == _hex_bad)
1852 break;
1853 if (pos >= 0)
1855 r->sig[pos / HOST_BITS_PER_LONG]
1856 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1857 pos -= 4;
1859 str++;
1862 if (*str == 'p' || *str == 'P')
1864 bool exp_neg = false;
1866 str++;
1867 if (*str == '-')
1869 exp_neg = true;
1870 str++;
1872 else if (*str == '+')
1873 str++;
1875 d = 0;
1876 while (ISDIGIT (*str))
1878 d *= 10;
1879 d += *str - '0';
1880 if (d > MAX_EXP)
1882 /* Overflowed the exponent. */
1883 if (exp_neg)
1884 goto underflow;
1885 else
1886 goto overflow;
1888 str++;
1890 if (exp_neg)
1891 d = -d;
1893 exp += d;
1896 r->class = rvc_normal;
1897 r->exp = exp;
1899 normalize (r);
1901 else
1903 /* Decimal floating point. */
1904 const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
1905 int d;
1907 while (*str == '0')
1908 str++;
1909 while (ISDIGIT (*str))
1911 d = *str++ - '0';
1912 do_multiply (r, r, ten);
1913 if (d)
1914 do_add (r, r, real_digit (d), 0);
1916 if (*str == '.')
1918 str++;
1919 if (r->class == rvc_zero)
1921 while (*str == '0')
1922 str++, exp--;
1924 while (ISDIGIT (*str))
1926 d = *str++ - '0';
1927 do_multiply (r, r, ten);
1928 if (d)
1929 do_add (r, r, real_digit (d), 0);
1930 exp--;
1934 if (*str == 'e' || *str == 'E')
1936 bool exp_neg = false;
1938 str++;
1939 if (*str == '-')
1941 exp_neg = true;
1942 str++;
1944 else if (*str == '+')
1945 str++;
1947 d = 0;
1948 while (ISDIGIT (*str))
1950 d *= 10;
1951 d += *str - '0';
1952 if (d > MAX_EXP)
1954 /* Overflowed the exponent. */
1955 if (exp_neg)
1956 goto underflow;
1957 else
1958 goto overflow;
1960 str++;
1962 if (exp_neg)
1963 d = -d;
1964 exp += d;
1967 if (exp)
1968 times_pten (r, exp);
1971 r->sign = sign;
1972 return;
1974 underflow:
1975 get_zero (r, sign);
1976 return;
1978 overflow:
1979 get_inf (r, sign);
1980 return;
1983 /* Legacy. Similar, but return the result directly. */
1985 REAL_VALUE_TYPE
1986 real_from_string2 (s, mode)
1987 const char *s;
1988 enum machine_mode mode;
1990 REAL_VALUE_TYPE r;
1992 real_from_string (&r, s);
1993 if (mode != VOIDmode)
1994 real_convert (&r, mode, &r);
1996 return r;
1999 /* Initialize R from the integer pair HIGH+LOW. */
2001 void
2002 real_from_integer (r, mode, low, high, unsigned_p)
2003 REAL_VALUE_TYPE *r;
2004 enum machine_mode mode;
2005 unsigned HOST_WIDE_INT low;
2006 HOST_WIDE_INT high;
2007 int unsigned_p;
2009 if (low == 0 && high == 0)
2010 get_zero (r, 0);
2011 else
2013 r->class = rvc_normal;
2014 r->sign = high < 0 && !unsigned_p;
2015 r->exp = 2 * HOST_BITS_PER_WIDE_INT;
2017 if (r->sign)
2019 high = ~high;
2020 if (low == 0)
2021 high += 1;
2022 else
2023 low = -low;
2026 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2028 r->sig[SIGSZ-1] = high;
2029 r->sig[SIGSZ-2] = low;
2030 memset (r->sig, 0, sizeof(long)*(SIGSZ-2));
2032 else if (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT)
2034 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
2035 r->sig[SIGSZ-2] = high;
2036 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
2037 r->sig[SIGSZ-4] = low;
2038 if (SIGSZ > 4)
2039 memset (r->sig, 0, sizeof(long)*(SIGSZ-4));
2041 else
2042 abort ();
2044 normalize (r);
2047 if (mode != VOIDmode)
2048 real_convert (r, mode, r);
2051 /* Returns 10**2**N. */
2053 static const REAL_VALUE_TYPE *
2054 ten_to_ptwo (n)
2055 int n;
2057 static REAL_VALUE_TYPE tens[EXP_BITS];
2059 if (n < 0 || n >= EXP_BITS)
2060 abort ();
2062 if (tens[n].class == rvc_zero)
2064 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2066 HOST_WIDE_INT t = 10;
2067 int i;
2069 for (i = 0; i < n; ++i)
2070 t *= t;
2072 real_from_integer (&tens[n], VOIDmode, t, 0, 1);
2074 else
2076 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2077 do_multiply (&tens[n], t, t);
2081 return &tens[n];
2084 /* Returns 10**(-2**N). */
2086 static const REAL_VALUE_TYPE *
2087 ten_to_mptwo (n)
2088 int n;
2090 static REAL_VALUE_TYPE tens[EXP_BITS];
2092 if (n < 0 || n >= EXP_BITS)
2093 abort ();
2095 if (tens[n].class == rvc_zero)
2096 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2098 return &tens[n];
2101 /* Returns N. */
2103 static const REAL_VALUE_TYPE *
2104 real_digit (n)
2105 int n;
2107 static REAL_VALUE_TYPE num[10];
2109 if (n < 0 || n > 9)
2110 abort ();
2112 if (n > 0 && num[n].class == rvc_zero)
2113 real_from_integer (&num[n], VOIDmode, n, 0, 1);
2115 return &num[n];
2118 /* Multiply R by 10**EXP. */
2120 static void
2121 times_pten (r, exp)
2122 REAL_VALUE_TYPE *r;
2123 int exp;
2125 REAL_VALUE_TYPE pten, *rr;
2126 bool negative = (exp < 0);
2127 int i;
2129 if (negative)
2131 exp = -exp;
2132 pten = *real_digit (1);
2133 rr = &pten;
2135 else
2136 rr = r;
2138 for (i = 0; exp > 0; ++i, exp >>= 1)
2139 if (exp & 1)
2140 do_multiply (rr, rr, ten_to_ptwo (i));
2142 if (negative)
2143 do_divide (r, r, &pten);
2146 /* Fills R with +Inf. */
2148 void
2149 real_inf (r)
2150 REAL_VALUE_TYPE *r;
2152 get_inf (r, 0);
2155 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2156 we force a QNaN, else we force an SNaN. The string, if not empty,
2157 is parsed as a number and placed in the significand. Return true
2158 if the string was successfully parsed. */
2160 bool
2161 real_nan (r, str, quiet, mode)
2162 REAL_VALUE_TYPE *r;
2163 const char *str;
2164 int quiet;
2165 enum machine_mode mode;
2167 const struct real_format *fmt;
2169 fmt = real_format_for_mode[mode - QFmode];
2170 if (fmt == NULL)
2171 abort ();
2173 if (*str == 0)
2175 if (quiet)
2176 get_canonical_qnan (r, 0);
2177 else
2178 get_canonical_snan (r, 0);
2180 else
2182 int base = 10, d;
2183 bool neg = false;
2185 memset (r, 0, sizeof (*r));
2186 r->class = rvc_nan;
2188 /* Parse akin to strtol into the significand of R. */
2190 while (ISSPACE (*str))
2191 str++;
2192 if (*str == '-')
2193 str++, neg = true;
2194 else if (*str == '+')
2195 str++;
2196 if (*str == '0')
2198 if (*++str == 'x')
2199 str++, base = 16;
2200 else
2201 base = 8;
2204 while ((d = hex_value (*str)) < base)
2206 REAL_VALUE_TYPE u;
2208 switch (base)
2210 case 8:
2211 lshift_significand (r, r, 3);
2212 break;
2213 case 16:
2214 lshift_significand (r, r, 4);
2215 break;
2216 case 10:
2217 lshift_significand_1 (&u, r);
2218 lshift_significand (r, r, 3);
2219 add_significands (r, r, &u);
2220 break;
2221 default:
2222 abort ();
2225 get_zero (&u, 0);
2226 u.sig[0] = d;
2227 add_significands (r, r, &u);
2229 str++;
2232 /* Must have consumed the entire string for success. */
2233 if (*str != 0)
2234 return false;
2236 /* Shift the significand into place such that the bits
2237 are in the most significant bits for the format. */
2238 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->p);
2240 /* Our MSB is always unset for NaNs. */
2241 r->sig[SIGSZ-1] &= ~SIG_MSB;
2243 /* Force quiet or signalling NaN. */
2244 if (quiet)
2245 r->sig[SIGSZ-1] |= SIG_MSB >> 1;
2246 else
2247 r->sig[SIGSZ-1] &= ~(SIG_MSB >> 1);
2249 /* Force at least one bit of the significand set. */
2250 for (d = 0; d < SIGSZ; ++d)
2251 if (r->sig[d])
2252 break;
2253 if (d == SIGSZ)
2254 r->sig[SIGSZ-1] |= SIG_MSB >> 2;
2256 /* Our intermediate format forces QNaNs to have MSB-1 set.
2257 If the target format has QNaNs with the top bit unset,
2258 mirror the output routines and invert the top two bits. */
2259 if (!fmt->qnan_msb_set)
2260 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1) | (SIG_MSB >> 2);
2263 return true;
2266 /* Fills R with 2**N. */
2268 void
2269 real_2expN (r, n)
2270 REAL_VALUE_TYPE *r;
2271 int n;
2273 memset (r, 0, sizeof (*r));
2275 n++;
2276 if (n > MAX_EXP)
2277 r->class = rvc_inf;
2278 else if (n < -MAX_EXP)
2280 else
2282 r->class = rvc_normal;
2283 r->exp = n;
2284 r->sig[SIGSZ-1] = SIG_MSB;
2289 static void
2290 round_for_format (fmt, r)
2291 const struct real_format *fmt;
2292 REAL_VALUE_TYPE *r;
2294 int p2, np2, i, w;
2295 unsigned long sticky;
2296 bool guard, lsb;
2297 int emin2m1, emax2;
2299 p2 = fmt->p * fmt->log2_b;
2300 emin2m1 = (fmt->emin - 1) * fmt->log2_b;
2301 emax2 = fmt->emax * fmt->log2_b;
2303 np2 = SIGNIFICAND_BITS - p2;
2304 switch (r->class)
2306 underflow:
2307 get_zero (r, r->sign);
2308 case rvc_zero:
2309 if (!fmt->has_signed_zero)
2310 r->sign = 0;
2311 return;
2313 overflow:
2314 get_inf (r, r->sign);
2315 case rvc_inf:
2316 return;
2318 case rvc_nan:
2319 clear_significand_below (r, np2);
2321 /* If we've cleared the entire significand, we need one bit
2322 set for this to continue to be a NaN. */
2323 for (i = 0; i < SIGSZ; ++i)
2324 if (r->sig[i])
2325 break;
2326 if (i == SIGSZ)
2327 r->sig[SIGSZ-1] = SIG_MSB >> 2;
2328 return;
2330 case rvc_normal:
2331 break;
2333 default:
2334 abort ();
2337 /* If we're not base2, normalize the exponent to a multiple of
2338 the true base. */
2339 if (fmt->log2_b != 1)
2341 int shift = r->exp & (fmt->log2_b - 1);
2342 if (shift)
2344 shift = fmt->log2_b - shift;
2345 r->sig[0] |= sticky_rshift_significand (r, r, shift);
2346 r->exp += shift;
2350 /* Check the range of the exponent. If we're out of range,
2351 either underflow or overflow. */
2352 if (r->exp > emax2)
2353 goto overflow;
2354 else if (r->exp <= emin2m1)
2356 int diff;
2358 if (!fmt->has_denorm)
2360 /* Don't underflow completely until we've had a chance to round. */
2361 if (r->exp < emin2m1)
2362 goto underflow;
2364 else
2366 diff = emin2m1 - r->exp + 1;
2367 if (diff > p2)
2368 goto underflow;
2370 /* De-normalize the significand. */
2371 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2372 r->exp += diff;
2376 /* There are P2 true significand bits, followed by one guard bit,
2377 followed by one sticky bit, followed by stuff. Fold nonzero
2378 stuff into the sticky bit. */
2380 sticky = 0;
2381 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2382 sticky |= r->sig[i];
2383 sticky |=
2384 r->sig[w] & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2386 guard = test_significand_bit (r, np2 - 1);
2387 lsb = test_significand_bit (r, np2);
2389 /* Round to even. */
2390 if (guard && (sticky || lsb))
2392 REAL_VALUE_TYPE u;
2393 get_zero (&u, 0);
2394 set_significand_bit (&u, np2);
2396 if (add_significands (r, r, &u))
2398 /* Overflow. Means the significand had been all ones, and
2399 is now all zeros. Need to increase the exponent, and
2400 possibly re-normalize it. */
2401 if (++r->exp > emax2)
2402 goto overflow;
2403 r->sig[SIGSZ-1] = SIG_MSB;
2405 if (fmt->log2_b != 1)
2407 int shift = r->exp & (fmt->log2_b - 1);
2408 if (shift)
2410 shift = fmt->log2_b - shift;
2411 rshift_significand (r, r, shift);
2412 r->exp += shift;
2413 if (r->exp > emax2)
2414 goto overflow;
2420 /* Catch underflow that we deferred until after rounding. */
2421 if (r->exp <= emin2m1)
2422 goto underflow;
2424 /* Clear out trailing garbage. */
2425 clear_significand_below (r, np2);
2428 /* Extend or truncate to a new mode. */
2430 void
2431 real_convert (r, mode, a)
2432 REAL_VALUE_TYPE *r;
2433 enum machine_mode mode;
2434 const REAL_VALUE_TYPE *a;
2436 const struct real_format *fmt;
2438 fmt = real_format_for_mode[mode - QFmode];
2439 if (fmt == NULL)
2440 abort ();
2442 *r = *a;
2443 round_for_format (fmt, r);
2445 /* round_for_format de-normalizes denormals. Undo just that part. */
2446 if (r->class == rvc_normal)
2447 normalize (r);
2450 /* Legacy. Likewise, except return the struct directly. */
2452 REAL_VALUE_TYPE
2453 real_value_truncate (mode, a)
2454 enum machine_mode mode;
2455 REAL_VALUE_TYPE a;
2457 REAL_VALUE_TYPE r;
2458 real_convert (&r, mode, &a);
2459 return r;
2462 /* Return true if truncating to MODE is exact. */
2464 bool
2465 exact_real_truncate (mode, a)
2466 enum machine_mode mode;
2467 const REAL_VALUE_TYPE *a;
2469 REAL_VALUE_TYPE t;
2470 real_convert (&t, mode, a);
2471 return real_identical (&t, a);
2474 /* Write R to the given target format. Place the words of the result
2475 in target word order in BUF. There are always 32 bits in each
2476 long, no matter the size of the host long.
2478 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2480 long
2481 real_to_target_fmt (buf, r_orig, fmt)
2482 long *buf;
2483 const REAL_VALUE_TYPE *r_orig;
2484 const struct real_format *fmt;
2486 REAL_VALUE_TYPE r;
2487 long buf1;
2489 r = *r_orig;
2490 round_for_format (fmt, &r);
2492 if (!buf)
2493 buf = &buf1;
2494 (*fmt->encode) (fmt, buf, &r);
2496 return *buf;
2499 /* Similar, but look up the format from MODE. */
2501 long
2502 real_to_target (buf, r, mode)
2503 long *buf;
2504 const REAL_VALUE_TYPE *r;
2505 enum machine_mode mode;
2507 const struct real_format *fmt;
2509 fmt = real_format_for_mode[mode - QFmode];
2510 if (fmt == NULL)
2511 abort ();
2513 return real_to_target_fmt (buf, r, fmt);
2516 /* Read R from the given target format. Read the words of the result
2517 in target word order in BUF. There are always 32 bits in each
2518 long, no matter the size of the host long. */
2520 void
2521 real_from_target_fmt (r, buf, fmt)
2522 REAL_VALUE_TYPE *r;
2523 const long *buf;
2524 const struct real_format *fmt;
2526 (*fmt->decode) (fmt, r, buf);
2529 /* Similar, but look up the format from MODE. */
2531 void
2532 real_from_target (r, buf, mode)
2533 REAL_VALUE_TYPE *r;
2534 const long *buf;
2535 enum machine_mode mode;
2537 const struct real_format *fmt;
2539 fmt = real_format_for_mode[mode - QFmode];
2540 if (fmt == NULL)
2541 abort ();
2543 (*fmt->decode) (fmt, r, buf);
2546 /* Return the number of bits in the significand for MODE. */
2547 /* ??? Legacy. Should get access to real_format directly. */
2550 significand_size (mode)
2551 enum machine_mode mode;
2553 const struct real_format *fmt;
2555 fmt = real_format_for_mode[mode - QFmode];
2556 if (fmt == NULL)
2557 return 0;
2559 return fmt->p * fmt->log2_b;
2562 /* Return a hash value for the given real value. */
2563 /* ??? The "unsigned int" return value is intended to be hashval_t,
2564 but I didn't want to pull hashtab.h into real.h. */
2566 unsigned int
2567 real_hash (r)
2568 const REAL_VALUE_TYPE *r;
2570 unsigned int h;
2571 size_t i;
2573 h = r->class | (r->sign << 2);
2574 switch (r->class)
2576 case rvc_zero:
2577 case rvc_inf:
2578 break;
2580 case rvc_normal:
2581 h |= r->exp << 3;
2582 /* FALLTHRU */
2584 case rvc_nan:
2585 if (sizeof(unsigned long) > sizeof(unsigned int))
2586 for (i = 0; i < SIGSZ; ++i)
2588 unsigned long s = r->sig[i];
2589 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2591 else
2592 for (i = 0; i < SIGSZ; ++i)
2593 h ^= r->sig[i];
2594 break;
2596 default:
2597 abort ();
2600 return h;
2603 /* IEEE single-precision format. */
2605 static void encode_ieee_single PARAMS ((const struct real_format *fmt,
2606 long *, const REAL_VALUE_TYPE *));
2607 static void decode_ieee_single PARAMS ((const struct real_format *,
2608 REAL_VALUE_TYPE *, const long *));
2610 static void
2611 encode_ieee_single (fmt, buf, r)
2612 const struct real_format *fmt;
2613 long *buf;
2614 const REAL_VALUE_TYPE *r;
2616 unsigned long image, sig, exp;
2617 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2619 image = r->sign << 31;
2620 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2622 switch (r->class)
2624 case rvc_zero:
2625 break;
2627 case rvc_inf:
2628 if (fmt->has_inf)
2629 image |= 255 << 23;
2630 else
2631 image |= 0x7fffffff;
2632 break;
2634 case rvc_nan:
2635 if (fmt->has_nans)
2637 image |= 255 << 23;
2638 image |= sig;
2639 if (!fmt->qnan_msb_set)
2640 image ^= 1 << 23 | 1 << 22;
2642 else
2643 image |= 0x7fffffff;
2644 break;
2646 case rvc_normal:
2647 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2648 whereas the intermediate representation is 0.F x 2**exp.
2649 Which means we're off by one. */
2650 if (denormal)
2651 exp = 0;
2652 else
2653 exp = r->exp + 127 - 1;
2654 image |= exp << 23;
2655 image |= sig;
2656 break;
2658 default:
2659 abort ();
2662 buf[0] = image;
2665 static void
2666 decode_ieee_single (fmt, r, buf)
2667 const struct real_format *fmt;
2668 REAL_VALUE_TYPE *r;
2669 const long *buf;
2671 unsigned long image = buf[0] & 0xffffffff;
2672 bool sign = (image >> 31) & 1;
2673 int exp = (image >> 23) & 0xff;
2675 memset (r, 0, sizeof (*r));
2676 image <<= HOST_BITS_PER_LONG - 24;
2677 image &= ~SIG_MSB;
2679 if (exp == 0)
2681 if (image && fmt->has_denorm)
2683 r->class = rvc_normal;
2684 r->sign = sign;
2685 r->exp = -126;
2686 r->sig[SIGSZ-1] = image << 1;
2687 normalize (r);
2689 else if (fmt->has_signed_zero)
2690 r->sign = sign;
2692 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2694 if (image)
2696 r->class = rvc_nan;
2697 r->sign = sign;
2698 if (!fmt->qnan_msb_set)
2699 image ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2700 r->sig[SIGSZ-1] = image;
2702 else
2704 r->class = rvc_inf;
2705 r->sign = sign;
2708 else
2710 r->class = rvc_normal;
2711 r->sign = sign;
2712 r->exp = exp - 127 + 1;
2713 r->sig[SIGSZ-1] = image | SIG_MSB;
2717 const struct real_format ieee_single_format =
2719 encode_ieee_single,
2720 decode_ieee_single,
2724 -125,
2725 128,
2727 true,
2728 true,
2729 true,
2730 true,
2731 true
2735 /* IEEE double-precision format. */
2737 static void encode_ieee_double PARAMS ((const struct real_format *fmt,
2738 long *, const REAL_VALUE_TYPE *));
2739 static void decode_ieee_double PARAMS ((const struct real_format *,
2740 REAL_VALUE_TYPE *, const long *));
2742 static void
2743 encode_ieee_double (fmt, buf, r)
2744 const struct real_format *fmt;
2745 long *buf;
2746 const REAL_VALUE_TYPE *r;
2748 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
2749 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2751 image_hi = r->sign << 31;
2752 image_lo = 0;
2754 if (HOST_BITS_PER_LONG == 64)
2756 sig_hi = r->sig[SIGSZ-1];
2757 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
2758 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
2760 else
2762 sig_hi = r->sig[SIGSZ-1];
2763 sig_lo = r->sig[SIGSZ-2];
2764 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
2765 sig_hi = (sig_hi >> 11) & 0xfffff;
2768 switch (r->class)
2770 case rvc_zero:
2771 break;
2773 case rvc_inf:
2774 if (fmt->has_inf)
2775 image_hi |= 2047 << 20;
2776 else
2778 image_hi |= 0x7fffffff;
2779 image_lo = 0xffffffff;
2781 break;
2783 case rvc_nan:
2784 if (fmt->has_nans)
2786 image_hi |= 2047 << 20;
2787 image_hi |= sig_hi;
2788 if (!fmt->qnan_msb_set)
2789 image_hi ^= 1 << 19 | 1 << 18;
2790 image_lo = sig_lo;
2792 else
2794 image_hi |= 0x7fffffff;
2795 image_lo = 0xffffffff;
2797 break;
2799 case rvc_normal:
2800 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2801 whereas the intermediate representation is 0.F x 2**exp.
2802 Which means we're off by one. */
2803 if (denormal)
2804 exp = 0;
2805 else
2806 exp = r->exp + 1023 - 1;
2807 image_hi |= exp << 20;
2808 image_hi |= sig_hi;
2809 image_lo = sig_lo;
2810 break;
2812 default:
2813 abort ();
2816 if (FLOAT_WORDS_BIG_ENDIAN)
2817 buf[0] = image_hi, buf[1] = image_lo;
2818 else
2819 buf[0] = image_lo, buf[1] = image_hi;
2822 static void
2823 decode_ieee_double (fmt, r, buf)
2824 const struct real_format *fmt;
2825 REAL_VALUE_TYPE *r;
2826 const long *buf;
2828 unsigned long image_hi, image_lo;
2829 bool sign;
2830 int exp;
2832 if (FLOAT_WORDS_BIG_ENDIAN)
2833 image_hi = buf[0], image_lo = buf[1];
2834 else
2835 image_lo = buf[0], image_hi = buf[1];
2836 image_lo &= 0xffffffff;
2837 image_hi &= 0xffffffff;
2839 sign = (image_hi >> 31) & 1;
2840 exp = (image_hi >> 20) & 0x7ff;
2842 memset (r, 0, sizeof (*r));
2844 image_hi <<= 32 - 21;
2845 image_hi |= image_lo >> 21;
2846 image_hi &= 0x7fffffff;
2847 image_lo <<= 32 - 21;
2849 if (exp == 0)
2851 if ((image_hi || image_lo) && fmt->has_denorm)
2853 r->class = rvc_normal;
2854 r->sign = sign;
2855 r->exp = -1022;
2856 if (HOST_BITS_PER_LONG == 32)
2858 image_hi = (image_hi << 1) | (image_lo >> 31);
2859 image_lo <<= 1;
2860 r->sig[SIGSZ-1] = image_hi;
2861 r->sig[SIGSZ-2] = image_lo;
2863 else
2865 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
2866 r->sig[SIGSZ-1] = image_hi;
2868 normalize (r);
2870 else if (fmt->has_signed_zero)
2871 r->sign = sign;
2873 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
2875 if (image_hi || image_lo)
2877 r->class = rvc_nan;
2878 r->sign = sign;
2879 if (HOST_BITS_PER_LONG == 32)
2881 r->sig[SIGSZ-1] = image_hi;
2882 r->sig[SIGSZ-2] = image_lo;
2884 else
2885 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
2887 if (!fmt->qnan_msb_set)
2888 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2890 else
2892 r->class = rvc_inf;
2893 r->sign = sign;
2896 else
2898 r->class = rvc_normal;
2899 r->sign = sign;
2900 r->exp = exp - 1023 + 1;
2901 if (HOST_BITS_PER_LONG == 32)
2903 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
2904 r->sig[SIGSZ-2] = image_lo;
2906 else
2907 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
2911 const struct real_format ieee_double_format =
2913 encode_ieee_double,
2914 decode_ieee_double,
2918 -1021,
2919 1024,
2921 true,
2922 true,
2923 true,
2924 true,
2925 true
2929 /* IEEE extended double precision format. This comes in three
2930 flavours: Intel's as a 12 byte image, Intel's as a 16 byte image,
2931 and Motorola's. */
2933 static void encode_ieee_extended PARAMS ((const struct real_format *fmt,
2934 long *, const REAL_VALUE_TYPE *));
2935 static void decode_ieee_extended PARAMS ((const struct real_format *,
2936 REAL_VALUE_TYPE *, const long *));
2938 static void encode_ieee_extended_128 PARAMS ((const struct real_format *fmt,
2939 long *,
2940 const REAL_VALUE_TYPE *));
2941 static void decode_ieee_extended_128 PARAMS ((const struct real_format *,
2942 REAL_VALUE_TYPE *,
2943 const long *));
2945 static void
2946 encode_ieee_extended (fmt, buf, r)
2947 const struct real_format *fmt;
2948 long *buf;
2949 const REAL_VALUE_TYPE *r;
2951 unsigned long image_hi, sig_hi, sig_lo;
2952 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2954 image_hi = r->sign << 15;
2955 sig_hi = sig_lo = 0;
2957 switch (r->class)
2959 case rvc_zero:
2960 break;
2962 case rvc_inf:
2963 if (fmt->has_inf)
2965 image_hi |= 32767;
2967 /* Intel requires the explicit integer bit to be set, otherwise
2968 it considers the value a "pseudo-infinity". Motorola docs
2969 say it doesn't care. */
2970 sig_hi = 0x80000000;
2972 else
2974 image_hi |= 32767;
2975 sig_lo = sig_hi = 0xffffffff;
2977 break;
2979 case rvc_nan:
2980 if (fmt->has_nans)
2982 image_hi |= 32767;
2983 if (HOST_BITS_PER_LONG == 32)
2985 sig_hi = r->sig[SIGSZ-1];
2986 sig_lo = r->sig[SIGSZ-2];
2988 else
2990 sig_lo = r->sig[SIGSZ-1];
2991 sig_hi = sig_lo >> 31 >> 1;
2992 sig_lo &= 0xffffffff;
2994 if (!fmt->qnan_msb_set)
2995 sig_hi ^= 1 << 30 | 1 << 29;
2997 /* Intel requires the explicit integer bit to be set, otherwise
2998 it considers the value a "pseudo-nan". Motorola docs say it
2999 doesn't care. */
3000 sig_hi |= 0x80000000;
3002 else
3004 image_hi |= 32767;
3005 sig_lo = sig_hi = 0xffffffff;
3007 break;
3009 case rvc_normal:
3011 int exp = r->exp;
3013 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3014 whereas the intermediate representation is 0.F x 2**exp.
3015 Which means we're off by one.
3017 Except for Motorola, which consider exp=0 and explicit
3018 integer bit set to continue to be normalized. In theory
3019 this discrepancy has been taken care of by the difference
3020 in fmt->emin in round_for_format. */
3022 if (denormal)
3023 exp = 0;
3024 else
3026 exp += 16383 - 1;
3027 if (exp < 0)
3028 abort ();
3030 image_hi |= exp;
3032 if (HOST_BITS_PER_LONG == 32)
3034 sig_hi = r->sig[SIGSZ-1];
3035 sig_lo = r->sig[SIGSZ-2];
3037 else
3039 sig_lo = r->sig[SIGSZ-1];
3040 sig_hi = sig_lo >> 31 >> 1;
3041 sig_lo &= 0xffffffff;
3044 break;
3046 default:
3047 abort ();
3050 if (FLOAT_WORDS_BIG_ENDIAN)
3051 buf[0] = image_hi << 16, buf[1] = sig_hi, buf[2] = sig_lo;
3052 else
3053 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3056 static void
3057 encode_ieee_extended_128 (fmt, buf, r)
3058 const struct real_format *fmt;
3059 long *buf;
3060 const REAL_VALUE_TYPE *r;
3062 buf[3 * !FLOAT_WORDS_BIG_ENDIAN] = 0;
3063 encode_ieee_extended (fmt, buf+!!FLOAT_WORDS_BIG_ENDIAN, r);
3066 static void
3067 decode_ieee_extended (fmt, r, buf)
3068 const struct real_format *fmt;
3069 REAL_VALUE_TYPE *r;
3070 const long *buf;
3072 unsigned long image_hi, sig_hi, sig_lo;
3073 bool sign;
3074 int exp;
3076 if (FLOAT_WORDS_BIG_ENDIAN)
3077 image_hi = buf[0] >> 16, sig_hi = buf[1], sig_lo = buf[2];
3078 else
3079 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3080 sig_lo &= 0xffffffff;
3081 sig_hi &= 0xffffffff;
3082 image_hi &= 0xffffffff;
3084 sign = (image_hi >> 15) & 1;
3085 exp = image_hi & 0x7fff;
3087 memset (r, 0, sizeof (*r));
3089 if (exp == 0)
3091 if ((sig_hi || sig_lo) && fmt->has_denorm)
3093 r->class = rvc_normal;
3094 r->sign = sign;
3096 /* When the IEEE format contains a hidden bit, we know that
3097 it's zero at this point, and so shift up the significand
3098 and decrease the exponent to match. In this case, Motorola
3099 defines the explicit integer bit to be valid, so we don't
3100 know whether the msb is set or not. */
3101 r->exp = fmt->emin;
3102 if (HOST_BITS_PER_LONG == 32)
3104 r->sig[SIGSZ-1] = sig_hi;
3105 r->sig[SIGSZ-2] = sig_lo;
3107 else
3108 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3110 normalize (r);
3112 else if (fmt->has_signed_zero)
3113 r->sign = sign;
3115 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3117 /* See above re "pseudo-infinities" and "pseudo-nans".
3118 Short summary is that the MSB will likely always be
3119 set, and that we don't care about it. */
3120 sig_hi &= 0x7fffffff;
3122 if (sig_hi || sig_lo)
3124 r->class = rvc_nan;
3125 r->sign = sign;
3126 if (HOST_BITS_PER_LONG == 32)
3128 r->sig[SIGSZ-1] = sig_hi;
3129 r->sig[SIGSZ-2] = sig_lo;
3131 else
3132 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3134 if (!fmt->qnan_msb_set)
3135 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
3137 else
3139 r->class = rvc_inf;
3140 r->sign = sign;
3143 else
3145 r->class = rvc_normal;
3146 r->sign = sign;
3147 r->exp = exp - 16383 + 1;
3148 if (HOST_BITS_PER_LONG == 32)
3150 r->sig[SIGSZ-1] = sig_hi;
3151 r->sig[SIGSZ-2] = sig_lo;
3153 else
3154 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3158 static void
3159 decode_ieee_extended_128 (fmt, r, buf)
3160 const struct real_format *fmt;
3161 REAL_VALUE_TYPE *r;
3162 const long *buf;
3164 decode_ieee_extended (fmt, r, buf+!!FLOAT_WORDS_BIG_ENDIAN);
3167 const struct real_format ieee_extended_motorola_format =
3169 encode_ieee_extended,
3170 decode_ieee_extended,
3174 -16382,
3175 16384,
3177 true,
3178 true,
3179 true,
3180 true,
3181 true
3184 const struct real_format ieee_extended_intel_96_format =
3186 encode_ieee_extended,
3187 decode_ieee_extended,
3191 -16381,
3192 16384,
3194 true,
3195 true,
3196 true,
3197 true,
3198 true
3201 const struct real_format ieee_extended_intel_128_format =
3203 encode_ieee_extended_128,
3204 decode_ieee_extended_128,
3208 -16381,
3209 16384,
3211 true,
3212 true,
3213 true,
3214 true,
3215 true
3219 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3220 numbers whose sum is equal to the extended precision value. The number
3221 with greater magnitude is first. This format has the same magnitude
3222 range as an IEEE double precision value, but effectively 106 bits of
3223 significand precision. Infinity and NaN are represented by their IEEE
3224 double precision value stored in the first number, the second number is
3225 ignored. Zeroes, Infinities, and NaNs are set in both doubles
3226 due to precedent. */
3228 static void encode_ibm_extended PARAMS ((const struct real_format *fmt,
3229 long *, const REAL_VALUE_TYPE *));
3230 static void decode_ibm_extended PARAMS ((const struct real_format *,
3231 REAL_VALUE_TYPE *, const long *));
3233 static void
3234 encode_ibm_extended (fmt, buf, r)
3235 const struct real_format *fmt ATTRIBUTE_UNUSED;
3236 long *buf;
3237 const REAL_VALUE_TYPE *r;
3239 REAL_VALUE_TYPE u, v;
3241 switch (r->class)
3243 case rvc_zero:
3244 /* Both doubles have sign bit set. */
3245 buf[0] = FLOAT_WORDS_BIG_ENDIAN ? r->sign << 31 : 0;
3246 buf[1] = FLOAT_WORDS_BIG_ENDIAN ? 0 : r->sign << 31;
3247 buf[2] = buf[0];
3248 buf[3] = buf[1];
3249 break;
3251 case rvc_inf:
3252 case rvc_nan:
3253 /* Both doubles set to Inf / NaN. */
3254 encode_ieee_double (&ieee_double_format, &buf[0], r);
3255 buf[2] = buf[0];
3256 buf[3] = buf[1];
3257 return;
3259 case rvc_normal:
3260 /* u = IEEE double precision portion of significand. */
3261 u = *r;
3262 clear_significand_below (&u, SIGNIFICAND_BITS - 53);
3264 /* v = remainder containing additional 53 bits of significand. */
3265 do_add (&v, r, &u, 1);
3267 encode_ieee_double (&ieee_double_format, &buf[0], &u);
3268 encode_ieee_double (&ieee_double_format, &buf[2], &v);
3269 break;
3271 default:
3272 abort ();
3276 static void
3277 decode_ibm_extended (fmt, r, buf)
3278 const struct real_format *fmt ATTRIBUTE_UNUSED;
3279 REAL_VALUE_TYPE *r;
3280 const long *buf;
3282 REAL_VALUE_TYPE u, v;
3284 decode_ieee_double (&ieee_double_format, &u, &buf[0]);
3286 if (u.class != rvc_zero && u.class != rvc_inf && u.class != rvc_nan)
3288 decode_ieee_double (&ieee_double_format, &v, &buf[2]);
3289 do_add (r, &u, &v, 0);
3291 else
3292 *r = u;
3295 const struct real_format ibm_extended_format =
3297 encode_ibm_extended,
3298 decode_ibm_extended,
3301 53 + 53,
3302 -1021,
3303 1024,
3305 true,
3306 true,
3307 true,
3308 true,
3309 true
3313 /* IEEE quad precision format. */
3315 static void encode_ieee_quad PARAMS ((const struct real_format *fmt,
3316 long *, const REAL_VALUE_TYPE *));
3317 static void decode_ieee_quad PARAMS ((const struct real_format *,
3318 REAL_VALUE_TYPE *, const long *));
3320 static void
3321 encode_ieee_quad (fmt, buf, r)
3322 const struct real_format *fmt;
3323 long *buf;
3324 const REAL_VALUE_TYPE *r;
3326 unsigned long image3, image2, image1, image0, exp;
3327 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3328 REAL_VALUE_TYPE u;
3330 image3 = r->sign << 31;
3331 image2 = 0;
3332 image1 = 0;
3333 image0 = 0;
3335 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3337 switch (r->class)
3339 case rvc_zero:
3340 break;
3342 case rvc_inf:
3343 if (fmt->has_inf)
3344 image3 |= 32767 << 16;
3345 else
3347 image3 |= 0x7fffffff;
3348 image2 = 0xffffffff;
3349 image1 = 0xffffffff;
3350 image0 = 0xffffffff;
3352 break;
3354 case rvc_nan:
3355 if (fmt->has_nans)
3357 image3 |= 32767 << 16;
3359 if (HOST_BITS_PER_LONG == 32)
3361 image0 = u.sig[0];
3362 image1 = u.sig[1];
3363 image2 = u.sig[2];
3364 image3 |= u.sig[3] & 0xffff;
3366 else
3368 image0 = u.sig[0];
3369 image1 = image0 >> 31 >> 1;
3370 image2 = u.sig[1];
3371 image3 |= (image2 >> 31 >> 1) & 0xffff;
3372 image0 &= 0xffffffff;
3373 image2 &= 0xffffffff;
3376 if (!fmt->qnan_msb_set)
3377 image3 ^= 1 << 15 | 1 << 14;
3379 else
3381 image3 |= 0x7fffffff;
3382 image2 = 0xffffffff;
3383 image1 = 0xffffffff;
3384 image0 = 0xffffffff;
3386 break;
3388 case rvc_normal:
3389 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3390 whereas the intermediate representation is 0.F x 2**exp.
3391 Which means we're off by one. */
3392 if (denormal)
3393 exp = 0;
3394 else
3395 exp = r->exp + 16383 - 1;
3396 image3 |= exp << 16;
3398 if (HOST_BITS_PER_LONG == 32)
3400 image0 = u.sig[0];
3401 image1 = u.sig[1];
3402 image2 = u.sig[2];
3403 image3 |= u.sig[3] & 0xffff;
3405 else
3407 image0 = u.sig[0];
3408 image1 = image0 >> 31 >> 1;
3409 image2 = u.sig[1];
3410 image3 |= (image2 >> 31 >> 1) & 0xffff;
3411 image0 &= 0xffffffff;
3412 image2 &= 0xffffffff;
3414 break;
3416 default:
3417 abort ();
3420 if (FLOAT_WORDS_BIG_ENDIAN)
3422 buf[0] = image3;
3423 buf[1] = image2;
3424 buf[2] = image1;
3425 buf[3] = image0;
3427 else
3429 buf[0] = image0;
3430 buf[1] = image1;
3431 buf[2] = image2;
3432 buf[3] = image3;
3436 static void
3437 decode_ieee_quad (fmt, r, buf)
3438 const struct real_format *fmt;
3439 REAL_VALUE_TYPE *r;
3440 const long *buf;
3442 unsigned long image3, image2, image1, image0;
3443 bool sign;
3444 int exp;
3446 if (FLOAT_WORDS_BIG_ENDIAN)
3448 image3 = buf[0];
3449 image2 = buf[1];
3450 image1 = buf[2];
3451 image0 = buf[3];
3453 else
3455 image0 = buf[0];
3456 image1 = buf[1];
3457 image2 = buf[2];
3458 image3 = buf[3];
3460 image0 &= 0xffffffff;
3461 image1 &= 0xffffffff;
3462 image2 &= 0xffffffff;
3464 sign = (image3 >> 31) & 1;
3465 exp = (image3 >> 16) & 0x7fff;
3466 image3 &= 0xffff;
3468 memset (r, 0, sizeof (*r));
3470 if (exp == 0)
3472 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3474 r->class = rvc_normal;
3475 r->sign = sign;
3477 r->exp = -16382 + (SIGNIFICAND_BITS - 112);
3478 if (HOST_BITS_PER_LONG == 32)
3480 r->sig[0] = image0;
3481 r->sig[1] = image1;
3482 r->sig[2] = image2;
3483 r->sig[3] = image3;
3485 else
3487 r->sig[0] = (image1 << 31 << 1) | image0;
3488 r->sig[1] = (image3 << 31 << 1) | image2;
3491 normalize (r);
3493 else if (fmt->has_signed_zero)
3494 r->sign = sign;
3496 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3498 if (image3 | image2 | image1 | image0)
3500 r->class = rvc_nan;
3501 r->sign = sign;
3503 if (HOST_BITS_PER_LONG == 32)
3505 r->sig[0] = image0;
3506 r->sig[1] = image1;
3507 r->sig[2] = image2;
3508 r->sig[3] = image3;
3510 else
3512 r->sig[0] = (image1 << 31 << 1) | image0;
3513 r->sig[1] = (image3 << 31 << 1) | image2;
3515 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3517 if (!fmt->qnan_msb_set)
3518 r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
3520 else
3522 r->class = rvc_inf;
3523 r->sign = sign;
3526 else
3528 r->class = rvc_normal;
3529 r->sign = sign;
3530 r->exp = exp - 16383 + 1;
3532 if (HOST_BITS_PER_LONG == 32)
3534 r->sig[0] = image0;
3535 r->sig[1] = image1;
3536 r->sig[2] = image2;
3537 r->sig[3] = image3;
3539 else
3541 r->sig[0] = (image1 << 31 << 1) | image0;
3542 r->sig[1] = (image3 << 31 << 1) | image2;
3544 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3545 r->sig[SIGSZ-1] |= SIG_MSB;
3549 const struct real_format ieee_quad_format =
3551 encode_ieee_quad,
3552 decode_ieee_quad,
3555 113,
3556 -16381,
3557 16384,
3558 127,
3559 true,
3560 true,
3561 true,
3562 true,
3563 true
3566 /* Descriptions of VAX floating point formats can be found beginning at
3568 http://www.openvms.compaq.com:8000/73final/4515/4515pro_013.html#f_floating_point_format
3570 The thing to remember is that they're almost IEEE, except for word
3571 order, exponent bias, and the lack of infinities, nans, and denormals.
3573 We don't implement the H_floating format here, simply because neither
3574 the VAX or Alpha ports use it. */
3576 static void encode_vax_f PARAMS ((const struct real_format *fmt,
3577 long *, const REAL_VALUE_TYPE *));
3578 static void decode_vax_f PARAMS ((const struct real_format *,
3579 REAL_VALUE_TYPE *, const long *));
3580 static void encode_vax_d PARAMS ((const struct real_format *fmt,
3581 long *, const REAL_VALUE_TYPE *));
3582 static void decode_vax_d PARAMS ((const struct real_format *,
3583 REAL_VALUE_TYPE *, const long *));
3584 static void encode_vax_g PARAMS ((const struct real_format *fmt,
3585 long *, const REAL_VALUE_TYPE *));
3586 static void decode_vax_g PARAMS ((const struct real_format *,
3587 REAL_VALUE_TYPE *, const long *));
3589 static void
3590 encode_vax_f (fmt, buf, r)
3591 const struct real_format *fmt ATTRIBUTE_UNUSED;
3592 long *buf;
3593 const REAL_VALUE_TYPE *r;
3595 unsigned long sign, exp, sig, image;
3597 sign = r->sign << 15;
3599 switch (r->class)
3601 case rvc_zero:
3602 image = 0;
3603 break;
3605 case rvc_inf:
3606 case rvc_nan:
3607 image = 0xffff7fff | sign;
3608 break;
3610 case rvc_normal:
3611 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3612 exp = r->exp + 128;
3614 image = (sig << 16) & 0xffff0000;
3615 image |= sign;
3616 image |= exp << 7;
3617 image |= sig >> 16;
3618 break;
3620 default:
3621 abort ();
3624 buf[0] = image;
3627 static void
3628 decode_vax_f (fmt, r, buf)
3629 const struct real_format *fmt ATTRIBUTE_UNUSED;
3630 REAL_VALUE_TYPE *r;
3631 const long *buf;
3633 unsigned long image = buf[0] & 0xffffffff;
3634 int exp = (image >> 7) & 0xff;
3636 memset (r, 0, sizeof (*r));
3638 if (exp != 0)
3640 r->class = rvc_normal;
3641 r->sign = (image >> 15) & 1;
3642 r->exp = exp - 128;
3644 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
3645 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3649 static void
3650 encode_vax_d (fmt, buf, r)
3651 const struct real_format *fmt ATTRIBUTE_UNUSED;
3652 long *buf;
3653 const REAL_VALUE_TYPE *r;
3655 unsigned long image0, image1, sign = r->sign << 15;
3657 switch (r->class)
3659 case rvc_zero:
3660 image0 = image1 = 0;
3661 break;
3663 case rvc_inf:
3664 case rvc_nan:
3665 image0 = 0xffff7fff | sign;
3666 image1 = 0xffffffff;
3667 break;
3669 case rvc_normal:
3670 /* Extract the significand into straight hi:lo. */
3671 if (HOST_BITS_PER_LONG == 64)
3673 image0 = r->sig[SIGSZ-1];
3674 image1 = (image0 >> (64 - 56)) & 0xffffffff;
3675 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
3677 else
3679 image0 = r->sig[SIGSZ-1];
3680 image1 = r->sig[SIGSZ-2];
3681 image1 = (image0 << 24) | (image1 >> 8);
3682 image0 = (image0 >> 8) & 0xffffff;
3685 /* Rearrange the half-words of the significand to match the
3686 external format. */
3687 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
3688 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3690 /* Add the sign and exponent. */
3691 image0 |= sign;
3692 image0 |= (r->exp + 128) << 7;
3693 break;
3695 default:
3696 abort ();
3699 if (FLOAT_WORDS_BIG_ENDIAN)
3700 buf[0] = image1, buf[1] = image0;
3701 else
3702 buf[0] = image0, buf[1] = image1;
3705 static void
3706 decode_vax_d (fmt, r, buf)
3707 const struct real_format *fmt ATTRIBUTE_UNUSED;
3708 REAL_VALUE_TYPE *r;
3709 const long *buf;
3711 unsigned long image0, image1;
3712 int exp;
3714 if (FLOAT_WORDS_BIG_ENDIAN)
3715 image1 = buf[0], image0 = buf[1];
3716 else
3717 image0 = buf[0], image1 = buf[1];
3718 image0 &= 0xffffffff;
3719 image1 &= 0xffffffff;
3721 exp = (image0 >> 7) & 0x7f;
3723 memset (r, 0, sizeof (*r));
3725 if (exp != 0)
3727 r->class = rvc_normal;
3728 r->sign = (image0 >> 15) & 1;
3729 r->exp = exp - 128;
3731 /* Rearrange the half-words of the external format into
3732 proper ascending order. */
3733 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
3734 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3736 if (HOST_BITS_PER_LONG == 64)
3738 image0 = (image0 << 31 << 1) | image1;
3739 image0 <<= 64 - 56;
3740 image0 |= SIG_MSB;
3741 r->sig[SIGSZ-1] = image0;
3743 else
3745 r->sig[SIGSZ-1] = image0;
3746 r->sig[SIGSZ-2] = image1;
3747 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
3748 r->sig[SIGSZ-1] |= SIG_MSB;
3753 static void
3754 encode_vax_g (fmt, buf, r)
3755 const struct real_format *fmt ATTRIBUTE_UNUSED;
3756 long *buf;
3757 const REAL_VALUE_TYPE *r;
3759 unsigned long image0, image1, sign = r->sign << 15;
3761 switch (r->class)
3763 case rvc_zero:
3764 image0 = image1 = 0;
3765 break;
3767 case rvc_inf:
3768 case rvc_nan:
3769 image0 = 0xffff7fff | sign;
3770 image1 = 0xffffffff;
3771 break;
3773 case rvc_normal:
3774 /* Extract the significand into straight hi:lo. */
3775 if (HOST_BITS_PER_LONG == 64)
3777 image0 = r->sig[SIGSZ-1];
3778 image1 = (image0 >> (64 - 53)) & 0xffffffff;
3779 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
3781 else
3783 image0 = r->sig[SIGSZ-1];
3784 image1 = r->sig[SIGSZ-2];
3785 image1 = (image0 << 21) | (image1 >> 11);
3786 image0 = (image0 >> 11) & 0xfffff;
3789 /* Rearrange the half-words of the significand to match the
3790 external format. */
3791 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
3792 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3794 /* Add the sign and exponent. */
3795 image0 |= sign;
3796 image0 |= (r->exp + 1024) << 4;
3797 break;
3799 default:
3800 abort ();
3803 if (FLOAT_WORDS_BIG_ENDIAN)
3804 buf[0] = image1, buf[1] = image0;
3805 else
3806 buf[0] = image0, buf[1] = image1;
3809 static void
3810 decode_vax_g (fmt, r, buf)
3811 const struct real_format *fmt ATTRIBUTE_UNUSED;
3812 REAL_VALUE_TYPE *r;
3813 const long *buf;
3815 unsigned long image0, image1;
3816 int exp;
3818 if (FLOAT_WORDS_BIG_ENDIAN)
3819 image1 = buf[0], image0 = buf[1];
3820 else
3821 image0 = buf[0], image1 = buf[1];
3822 image0 &= 0xffffffff;
3823 image1 &= 0xffffffff;
3825 exp = (image0 >> 4) & 0x7ff;
3827 memset (r, 0, sizeof (*r));
3829 if (exp != 0)
3831 r->class = rvc_normal;
3832 r->sign = (image0 >> 15) & 1;
3833 r->exp = exp - 1024;
3835 /* Rearrange the half-words of the external format into
3836 proper ascending order. */
3837 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
3838 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3840 if (HOST_BITS_PER_LONG == 64)
3842 image0 = (image0 << 31 << 1) | image1;
3843 image0 <<= 64 - 53;
3844 image0 |= SIG_MSB;
3845 r->sig[SIGSZ-1] = image0;
3847 else
3849 r->sig[SIGSZ-1] = image0;
3850 r->sig[SIGSZ-2] = image1;
3851 lshift_significand (r, r, 64 - 53);
3852 r->sig[SIGSZ-1] |= SIG_MSB;
3857 const struct real_format vax_f_format =
3859 encode_vax_f,
3860 decode_vax_f,
3864 -127,
3865 127,
3867 false,
3868 false,
3869 false,
3870 false,
3871 false
3874 const struct real_format vax_d_format =
3876 encode_vax_d,
3877 decode_vax_d,
3881 -127,
3882 127,
3884 false,
3885 false,
3886 false,
3887 false,
3888 false
3891 const struct real_format vax_g_format =
3893 encode_vax_g,
3894 decode_vax_g,
3898 -1023,
3899 1023,
3901 false,
3902 false,
3903 false,
3904 false,
3905 false
3908 /* A good reference for these can be found in chapter 9 of
3909 "ESA/390 Principles of Operation", IBM document number SA22-7201-01.
3910 An on-line version can be found here:
3912 http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
3915 static void encode_i370_single PARAMS ((const struct real_format *fmt,
3916 long *, const REAL_VALUE_TYPE *));
3917 static void decode_i370_single PARAMS ((const struct real_format *,
3918 REAL_VALUE_TYPE *, const long *));
3919 static void encode_i370_double PARAMS ((const struct real_format *fmt,
3920 long *, const REAL_VALUE_TYPE *));
3921 static void decode_i370_double PARAMS ((const struct real_format *,
3922 REAL_VALUE_TYPE *, const long *));
3924 static void
3925 encode_i370_single (fmt, buf, r)
3926 const struct real_format *fmt ATTRIBUTE_UNUSED;
3927 long *buf;
3928 const REAL_VALUE_TYPE *r;
3930 unsigned long sign, exp, sig, image;
3932 sign = r->sign << 31;
3934 switch (r->class)
3936 case rvc_zero:
3937 image = 0;
3938 break;
3940 case rvc_inf:
3941 case rvc_nan:
3942 image = 0x7fffffff | sign;
3943 break;
3945 case rvc_normal:
3946 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0xffffff;
3947 exp = ((r->exp / 4) + 64) << 24;
3948 image = sign | exp | sig;
3949 break;
3951 default:
3952 abort ();
3955 buf[0] = image;
3958 static void
3959 decode_i370_single (fmt, r, buf)
3960 const struct real_format *fmt ATTRIBUTE_UNUSED;
3961 REAL_VALUE_TYPE *r;
3962 const long *buf;
3964 unsigned long sign, sig, image = buf[0];
3965 int exp;
3967 sign = (image >> 31) & 1;
3968 exp = (image >> 24) & 0x7f;
3969 sig = image & 0xffffff;
3971 memset (r, 0, sizeof (*r));
3973 if (exp || sig)
3975 r->class = rvc_normal;
3976 r->sign = sign;
3977 r->exp = (exp - 64) * 4;
3978 r->sig[SIGSZ-1] = sig << (HOST_BITS_PER_LONG - 24);
3979 normalize (r);
3983 static void
3984 encode_i370_double (fmt, buf, r)
3985 const struct real_format *fmt ATTRIBUTE_UNUSED;
3986 long *buf;
3987 const REAL_VALUE_TYPE *r;
3989 unsigned long sign, exp, image_hi, image_lo;
3991 sign = r->sign << 31;
3993 switch (r->class)
3995 case rvc_zero:
3996 image_hi = image_lo = 0;
3997 break;
3999 case rvc_inf:
4000 case rvc_nan:
4001 image_hi = 0x7fffffff | sign;
4002 image_lo = 0xffffffff;
4003 break;
4005 case rvc_normal:
4006 if (HOST_BITS_PER_LONG == 64)
4008 image_hi = r->sig[SIGSZ-1];
4009 image_lo = (image_hi >> (64 - 56)) & 0xffffffff;
4010 image_hi = (image_hi >> (64 - 56 + 1) >> 31) & 0xffffff;
4012 else
4014 image_hi = r->sig[SIGSZ-1];
4015 image_lo = r->sig[SIGSZ-2];
4016 image_lo = (image_lo >> 8) | (image_hi << 24);
4017 image_hi >>= 8;
4020 exp = ((r->exp / 4) + 64) << 24;
4021 image_hi |= sign | exp;
4022 break;
4024 default:
4025 abort ();
4028 if (FLOAT_WORDS_BIG_ENDIAN)
4029 buf[0] = image_hi, buf[1] = image_lo;
4030 else
4031 buf[0] = image_lo, buf[1] = image_hi;
4034 static void
4035 decode_i370_double (fmt, r, buf)
4036 const struct real_format *fmt ATTRIBUTE_UNUSED;
4037 REAL_VALUE_TYPE *r;
4038 const long *buf;
4040 unsigned long sign, image_hi, image_lo;
4041 int exp;
4043 if (FLOAT_WORDS_BIG_ENDIAN)
4044 image_hi = buf[0], image_lo = buf[1];
4045 else
4046 image_lo = buf[0], image_hi = buf[1];
4048 sign = (image_hi >> 31) & 1;
4049 exp = (image_hi >> 24) & 0x7f;
4050 image_hi &= 0xffffff;
4051 image_lo &= 0xffffffff;
4053 memset (r, 0, sizeof (*r));
4055 if (exp || image_hi || image_lo)
4057 r->class = rvc_normal;
4058 r->sign = sign;
4059 r->exp = (exp - 64) * 4 + (SIGNIFICAND_BITS - 56);
4061 if (HOST_BITS_PER_LONG == 32)
4063 r->sig[0] = image_lo;
4064 r->sig[1] = image_hi;
4066 else
4067 r->sig[0] = image_lo | (image_hi << 31 << 1);
4069 normalize (r);
4073 const struct real_format i370_single_format =
4075 encode_i370_single,
4076 decode_i370_single,
4080 -64,
4083 false,
4084 false,
4085 false, /* ??? The encoding does allow for "unnormals". */
4086 false, /* ??? The encoding does allow for "unnormals". */
4087 false
4090 const struct real_format i370_double_format =
4092 encode_i370_double,
4093 decode_i370_double,
4097 -64,
4100 false,
4101 false,
4102 false, /* ??? The encoding does allow for "unnormals". */
4103 false, /* ??? The encoding does allow for "unnormals". */
4104 false
4107 /* The "twos-complement" c4x format is officially defined as
4109 x = s(~s).f * 2**e
4111 This is rather misleading. One must remember that F is signed.
4112 A better description would be
4114 x = -1**s * ((s + 1 + .f) * 2**e
4116 So if we have a (4 bit) fraction of .1000 with a sign bit of 1,
4117 that's -1 * (1+1+(-.5)) == -1.5. I think.
4119 The constructions here are taken from Tables 5-1 and 5-2 of the
4120 TMS320C4x User's Guide wherein step-by-step instructions for
4121 conversion from IEEE are presented. That's close enough to our
4122 internal representation so as to make things easy.
4124 See http://www-s.ti.com/sc/psheets/spru063c/spru063c.pdf */
4126 static void encode_c4x_single PARAMS ((const struct real_format *fmt,
4127 long *, const REAL_VALUE_TYPE *));
4128 static void decode_c4x_single PARAMS ((const struct real_format *,
4129 REAL_VALUE_TYPE *, const long *));
4130 static void encode_c4x_extended PARAMS ((const struct real_format *fmt,
4131 long *, const REAL_VALUE_TYPE *));
4132 static void decode_c4x_extended PARAMS ((const struct real_format *,
4133 REAL_VALUE_TYPE *, const long *));
4135 static void
4136 encode_c4x_single (fmt, buf, r)
4137 const struct real_format *fmt ATTRIBUTE_UNUSED;
4138 long *buf;
4139 const REAL_VALUE_TYPE *r;
4141 unsigned long image, exp, sig;
4143 switch (r->class)
4145 case rvc_zero:
4146 exp = -128;
4147 sig = 0;
4148 break;
4150 case rvc_inf:
4151 case rvc_nan:
4152 exp = 127;
4153 sig = 0x800000 - r->sign;
4154 break;
4156 case rvc_normal:
4157 exp = r->exp - 1;
4158 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4159 if (r->sign)
4161 if (sig)
4162 sig = -sig;
4163 else
4164 exp--;
4165 sig |= 0x800000;
4167 break;
4169 default:
4170 abort ();
4173 image = ((exp & 0xff) << 24) | (sig & 0xffffff);
4174 buf[0] = image;
4177 static void
4178 decode_c4x_single (fmt, r, buf)
4179 const struct real_format *fmt ATTRIBUTE_UNUSED;
4180 REAL_VALUE_TYPE *r;
4181 const long *buf;
4183 unsigned long image = buf[0];
4184 unsigned long sig;
4185 int exp, sf;
4187 exp = (((image >> 24) & 0xff) ^ 0x80) - 0x80;
4188 sf = ((image & 0xffffff) ^ 0x800000) - 0x800000;
4190 memset (r, 0, sizeof (*r));
4192 if (exp != -128)
4194 r->class = rvc_normal;
4196 sig = sf & 0x7fffff;
4197 if (sf < 0)
4199 r->sign = 1;
4200 if (sig)
4201 sig = -sig;
4202 else
4203 exp++;
4205 sig = (sig << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4207 r->exp = exp + 1;
4208 r->sig[SIGSZ-1] = sig;
4212 static void
4213 encode_c4x_extended (fmt, buf, r)
4214 const struct real_format *fmt ATTRIBUTE_UNUSED;
4215 long *buf;
4216 const REAL_VALUE_TYPE *r;
4218 unsigned long exp, sig;
4220 switch (r->class)
4222 case rvc_zero:
4223 exp = -128;
4224 sig = 0;
4225 break;
4227 case rvc_inf:
4228 case rvc_nan:
4229 exp = 127;
4230 sig = 0x80000000 - r->sign;
4231 break;
4233 case rvc_normal:
4234 exp = r->exp - 1;
4236 sig = r->sig[SIGSZ-1];
4237 if (HOST_BITS_PER_LONG == 64)
4238 sig = sig >> 1 >> 31;
4239 sig &= 0x7fffffff;
4241 if (r->sign)
4243 if (sig)
4244 sig = -sig;
4245 else
4246 exp--;
4247 sig |= 0x80000000;
4249 break;
4251 default:
4252 abort ();
4255 exp = (exp & 0xff) << 24;
4256 sig &= 0xffffffff;
4258 if (FLOAT_WORDS_BIG_ENDIAN)
4259 buf[0] = exp, buf[1] = sig;
4260 else
4261 buf[0] = sig, buf[0] = exp;
4264 static void
4265 decode_c4x_extended (fmt, r, buf)
4266 const struct real_format *fmt ATTRIBUTE_UNUSED;
4267 REAL_VALUE_TYPE *r;
4268 const long *buf;
4270 unsigned long sig;
4271 int exp, sf;
4273 if (FLOAT_WORDS_BIG_ENDIAN)
4274 exp = buf[0], sf = buf[1];
4275 else
4276 sf = buf[0], exp = buf[1];
4278 exp = (((exp >> 24) & 0xff) & 0x80) - 0x80;
4279 sf = ((sf & 0xffffffff) ^ 0x80000000) - 0x80000000;
4281 memset (r, 0, sizeof (*r));
4283 if (exp != -128)
4285 r->class = rvc_normal;
4287 sig = sf & 0x7fffffff;
4288 if (sf < 0)
4290 r->sign = 1;
4291 if (sig)
4292 sig = -sig;
4293 else
4294 exp++;
4296 if (HOST_BITS_PER_LONG == 64)
4297 sig = sig << 1 << 31;
4298 sig |= SIG_MSB;
4300 r->exp = exp + 1;
4301 r->sig[SIGSZ-1] = sig;
4305 const struct real_format c4x_single_format =
4307 encode_c4x_single,
4308 decode_c4x_single,
4312 -126,
4313 128,
4315 false,
4316 false,
4317 false,
4318 false,
4319 false
4322 const struct real_format c4x_extended_format =
4324 encode_c4x_extended,
4325 decode_c4x_extended,
4329 -126,
4330 128,
4332 false,
4333 false,
4334 false,
4335 false,
4336 false
4340 /* A synthetic "format" for internal arithmetic. It's the size of the
4341 internal significand minus the two bits needed for proper rounding.
4342 The encode and decode routines exist only to satisfy our paranoia
4343 harness. */
4345 static void encode_internal PARAMS ((const struct real_format *fmt,
4346 long *, const REAL_VALUE_TYPE *));
4347 static void decode_internal PARAMS ((const struct real_format *,
4348 REAL_VALUE_TYPE *, const long *));
4350 static void
4351 encode_internal (fmt, buf, r)
4352 const struct real_format *fmt ATTRIBUTE_UNUSED;
4353 long *buf;
4354 const REAL_VALUE_TYPE *r;
4356 memcpy (buf, r, sizeof (*r));
4359 static void
4360 decode_internal (fmt, r, buf)
4361 const struct real_format *fmt ATTRIBUTE_UNUSED;
4362 REAL_VALUE_TYPE *r;
4363 const long *buf;
4365 memcpy (r, buf, sizeof (*r));
4368 const struct real_format real_internal_format =
4370 encode_internal,
4371 decode_internal,
4374 SIGNIFICAND_BITS - 2,
4375 -MAX_EXP,
4376 MAX_EXP,
4378 true,
4379 true,
4380 false,
4381 true,
4382 true
4385 /* Set up default mode to format mapping for IEEE. Everyone else has
4386 to set these values in OVERRIDE_OPTIONS. */
4388 const struct real_format *real_format_for_mode[TFmode - QFmode + 1] =
4390 NULL, /* QFmode */
4391 NULL, /* HFmode */
4392 NULL, /* TQFmode */
4393 &ieee_single_format, /* SFmode */
4394 &ieee_double_format, /* DFmode */
4396 /* We explicitly don't handle XFmode. There are two formats,
4397 pretty much equally common. Choose one in OVERRIDE_OPTIONS. */
4398 NULL, /* XFmode */
4399 &ieee_quad_format /* TFmode */
4403 /* Calculate the square root of X in mode MODE, and store the result
4404 in R. For details see "High Precision Division and Square Root",
4405 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4406 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4408 void
4409 real_sqrt (r, mode, x)
4410 REAL_VALUE_TYPE *r;
4411 enum machine_mode mode;
4412 const REAL_VALUE_TYPE *x;
4414 static REAL_VALUE_TYPE halfthree;
4415 static REAL_VALUE_TYPE half;
4416 static bool init = false;
4417 REAL_VALUE_TYPE h, t, i;
4418 int iter, exp;
4420 /* sqrt(-0.0) is -0.0. */
4421 if (real_isnegzero (x))
4423 *r = *x;
4424 return;
4427 /* Negative arguments return NaN. */
4428 if (real_isneg (x))
4430 /* Mode is ignored for canonical NaN. */
4431 real_nan (r, "", 1, SFmode);
4432 return;
4435 /* Infinity and NaN return themselves. */
4436 if (real_isinf (x) || real_isnan (x))
4438 *r = *x;
4439 return;
4442 if (!init)
4444 real_arithmetic (&half, RDIV_EXPR, &dconst1, &dconst2);
4445 real_arithmetic (&halfthree, PLUS_EXPR, &dconst1, &half);
4446 init = true;
4449 /* Initial guess for reciprocal sqrt, i. */
4450 exp = real_exponent (x);
4451 real_ldexp (&i, &dconst1, -exp/2);
4453 /* Newton's iteration for reciprocal sqrt, i. */
4454 for (iter = 0; iter < 16; iter++)
4456 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4457 real_arithmetic (&t, MULT_EXPR, x, &i);
4458 real_arithmetic (&h, MULT_EXPR, &t, &i);
4459 real_arithmetic (&t, MULT_EXPR, &h, &half);
4460 real_arithmetic (&h, MINUS_EXPR, &halfthree, &t);
4461 real_arithmetic (&t, MULT_EXPR, &i, &h);
4463 /* Check for early convergence. */
4464 if (iter >= 6 && real_identical (&i, &t))
4465 break;
4467 /* ??? Unroll loop to avoid copying. */
4468 i = t;
4471 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4472 real_arithmetic (&t, MULT_EXPR, x, &i);
4473 real_arithmetic (&h, MULT_EXPR, &t, &i);
4474 real_arithmetic (&i, MINUS_EXPR, &dconst1, &h);
4475 real_arithmetic (&h, MULT_EXPR, &t, &i);
4476 real_arithmetic (&i, MULT_EXPR, &half, &h);
4477 real_arithmetic (&h, PLUS_EXPR, &t, &i);
4479 /* ??? We need a Tuckerman test to get the last bit. */
4481 real_convert (r, mode, &h);