OSDN Git Service

* configure.in: Delete three unused variables. Move a variable
[pf3gnuchains/gcc-fork.git] / gcc / real.c
1 /* real.c - software floating point emulation.
2    Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2002, 2003 Free Software Foundation, Inc.
4    Contributed by Stephen L. Moshier (moshier@world.std.com).
5    Re-written by Richard Henderson  <rth@redhat.com>
6
7    This file is part of GCC.
8
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.
13
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.
18
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.  */
23
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"
32
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.
36
37    Specifically
38
39         x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
40
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.
47
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).
51
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.
57
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.
61
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.
68
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.  */
73
74
75 /* Used to classify two numbers simultaneously.  */
76 #define CLASS2(A, B)  ((A) << 2 | (B))
77
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
81
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 *));
117
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 *));
128
129 static unsigned long rtd_divmod PARAMS ((REAL_VALUE_TYPE *,
130                                          REAL_VALUE_TYPE *));
131
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));
136
137 static void round_for_format PARAMS ((const struct real_format *,
138                                       REAL_VALUE_TYPE *));
139 \f
140 /* Initialize R with a positive zero.  */
141
142 static inline void
143 get_zero (r, sign)
144      REAL_VALUE_TYPE *r;
145      int sign;
146 {
147   memset (r, 0, sizeof (*r));
148   r->sign = sign;
149 }
150
151 /* Initialize R with the canonical quiet NaN.  */
152
153 static inline void
154 get_canonical_qnan (r, sign)
155      REAL_VALUE_TYPE *r;
156      int sign;
157 {
158   memset (r, 0, sizeof (*r));
159   r->class = rvc_nan;
160   r->sign = sign;
161   r->sig[SIGSZ-1] = SIG_MSB >> 1;
162 }
163
164 static inline void
165 get_canonical_snan (r, sign)
166      REAL_VALUE_TYPE *r;
167      int sign;
168 {
169   memset (r, 0, sizeof (*r));
170   r->class = rvc_nan;
171   r->sign = sign;
172   r->sig[SIGSZ-1] = SIG_MSB >> 2;
173 }
174
175 static inline void
176 get_inf (r, sign)
177      REAL_VALUE_TYPE *r;
178      int sign;
179 {
180   memset (r, 0, sizeof (*r));
181   r->class = rvc_inf;
182   r->sign = sign;
183 }
184
185 \f
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.  */
188
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;
194 {
195   unsigned long sticky = 0;
196   unsigned int i, ofs = 0;
197
198   if (n >= HOST_BITS_PER_LONG)
199     {
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;
203     }
204
205   if (n != 0)
206     {
207       sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
208       for (i = 0; i < SIGSZ; ++i)
209         {
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)));
214         }
215     }
216   else
217     {
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;
222     }
223
224   return sticky != 0;
225 }
226
227 /* Right-shift the significand of A by N bits; put the result in the
228    significand of R.  */
229
230 static void
231 rshift_significand (r, a, n)
232      REAL_VALUE_TYPE *r;
233      const REAL_VALUE_TYPE *a;
234      unsigned int n;
235 {
236   unsigned int i, ofs = n / HOST_BITS_PER_LONG;
237
238   n &= HOST_BITS_PER_LONG - 1;
239   if (n != 0)
240     {
241       for (i = 0; i < SIGSZ; ++i)
242         {
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)));
247         }
248     }
249   else
250     {
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;
255     }
256 }
257
258 /* Left-shift the significand of A by N bits; put the result in the
259    significand of R.  */
260
261 static void
262 lshift_significand (r, a, n)
263      REAL_VALUE_TYPE *r;
264      const REAL_VALUE_TYPE *a;
265      unsigned int n;
266 {
267   unsigned int i, ofs = n / HOST_BITS_PER_LONG;
268
269   n &= HOST_BITS_PER_LONG - 1;
270   if (n == 0)
271     {
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;
276     }
277   else
278     for (i = 0; i < SIGSZ; ++i)
279       {
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)));
284       }
285 }
286
287 /* Likewise, but N is specialized to 1.  */
288
289 static inline void
290 lshift_significand_1 (r, a)
291      REAL_VALUE_TYPE *r;
292      const REAL_VALUE_TYPE *a;
293 {
294   unsigned int i;
295
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;
299 }
300
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.  */
303
304 static inline bool
305 add_significands (r, a, b)
306      REAL_VALUE_TYPE *r;
307      const REAL_VALUE_TYPE *a, *b;
308 {
309   bool carry = false;
310   int i;
311
312   for (i = 0; i < SIGSZ; ++i)
313     {
314       unsigned long ai = a->sig[i];
315       unsigned long ri = ai + b->sig[i];
316
317       if (carry)
318         {
319           carry = ri < ai;
320           carry |= ++ri == 0;
321         }
322       else
323         carry = ri < ai;
324
325       r->sig[i] = ri;
326     }
327
328   return carry;
329 }
330
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.  */
334
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;
340 {
341   int i;
342
343   for (i = 0; i < SIGSZ; ++i)
344     {
345       unsigned long ai = a->sig[i];
346       unsigned long ri = ai - b->sig[i];
347
348       if (carry)
349         {
350           carry = ri > ai;
351           carry |= ~--ri == 0;
352         }
353       else
354         carry = ri > ai;
355
356       r->sig[i] = ri;
357     }
358
359   return carry;
360 }  
361
362 /* Negate the significand A, placing the result in R.  */
363
364 static inline void
365 neg_significand (r, a)
366      REAL_VALUE_TYPE *r;
367      const REAL_VALUE_TYPE *a;
368 {
369   bool carry = true;
370   int i;
371
372   for (i = 0; i < SIGSZ; ++i)
373     {
374       unsigned long ri, ai = a->sig[i];
375
376       if (carry)
377         {
378           if (ai)
379             {
380               ri = -ai;
381               carry = false;
382             }
383           else
384             ri = ai;
385         }
386       else
387         ri = ~ai;
388
389       r->sig[i] = ri;
390     }
391 }  
392
393 /* Compare significands.  Return tri-state vs zero.  */
394
395 static inline int 
396 cmp_significands (a, b)
397      const REAL_VALUE_TYPE *a, *b;
398 {
399   int i;
400
401   for (i = SIGSZ - 1; i >= 0; --i)
402     {
403       unsigned long ai = a->sig[i];
404       unsigned long bi = b->sig[i];
405
406       if (ai > bi)
407         return 1;
408       if (ai < bi)
409         return -1;
410     }
411
412   return 0;
413 }
414
415 /* Return true if A is nonzero.  */
416
417 static inline int 
418 cmp_significand_0 (a)
419      const REAL_VALUE_TYPE *a;
420 {
421   int i;
422
423   for (i = SIGSZ - 1; i >= 0; --i)
424     if (a->sig[i])
425       return 1;
426
427   return 0;
428 }
429
430 /* Set bit N of the significand of R.  */
431
432 static inline void
433 set_significand_bit (r, n)
434      REAL_VALUE_TYPE *r;
435      unsigned int n;
436 {
437   r->sig[n / HOST_BITS_PER_LONG]
438     |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
439 }
440
441 /* Clear bit N of the significand of R.  */
442
443 static inline void
444 clear_significand_bit (r, n)
445      REAL_VALUE_TYPE *r;
446      unsigned int n;
447 {
448   r->sig[n / HOST_BITS_PER_LONG]
449     &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
450 }
451
452 /* Test bit N of the significand of R.  */
453
454 static inline bool
455 test_significand_bit (r, n)
456      REAL_VALUE_TYPE *r;
457      unsigned int n;
458 {
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;
464 }
465
466 /* Clear bits 0..N-1 of the significand of R.  */
467
468 static void
469 clear_significand_below (r, n)
470      REAL_VALUE_TYPE *r;
471      unsigned int n;
472 {
473   int i, w = n / HOST_BITS_PER_LONG;
474
475   for (i = 0; i < w; ++i)
476     r->sig[i] = 0;
477
478   r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
479 }
480
481 /* Divide the significands of A and B, placing the result in R.  Return
482    true if the division was inexact.  */
483
484 static inline bool
485 div_significands (r, a, b)
486      REAL_VALUE_TYPE *r;
487      const REAL_VALUE_TYPE *a, *b;
488 {
489   REAL_VALUE_TYPE u;
490   int i, bit = SIGNIFICAND_BITS - 1;
491   unsigned long msb, inexact;
492
493   u = *a;
494   memset (r->sig, 0, sizeof (r->sig));
495
496   msb = 0;
497   goto start;
498   do
499     {
500       msb = u.sig[SIGSZ-1] & SIG_MSB;
501       lshift_significand_1 (&u, &u);
502     start:
503       if (msb || cmp_significands (&u, b) >= 0)
504         {
505           sub_significands (&u, &u, b, 0);
506           set_significand_bit (r, bit);
507         }
508     }
509   while (--bit >= 0);
510
511   for (i = 0, inexact = 0; i < SIGSZ; i++)
512     inexact |= u.sig[i];
513
514   return inexact != 0;
515 }
516
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.)  */
521
522 static void
523 normalize (r)
524      REAL_VALUE_TYPE *r;
525 {
526   int shift = 0, exp;
527   int i, j;
528
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;
535
536   /* Zero significand flushes to zero.  */
537   if (i < 0)
538     {
539       r->class = rvc_zero;
540       r->exp = 0;
541       return;
542     }
543
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;
549
550   if (shift > 0)
551     {
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
558         {
559           r->exp = exp;
560           lshift_significand (r, r, shift);
561         }
562     }
563 }
564 \f
565 /* Return R = A + (SUBTRACT_P ? -B : B).  */
566
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;
572 {
573   int dexp, sign, exp;
574   REAL_VALUE_TYPE t;
575   bool inexact = false;
576
577   /* Determine if we need to add or subtract.  */
578   sign = a->sign;
579   subtract_p = (sign ^ b->sign) ^ subtract_p;
580
581   switch (CLASS2 (a->class, b->class))
582     {
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;
587
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;
601
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;
613
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;
622
623     case CLASS2 (rvc_normal, rvc_normal):
624       break;
625
626     default:
627       abort ();
628     }
629
630   /* Swap the arguments such that A has the larger exponent.  */
631   dexp = a->exp - b->exp;
632   if (dexp < 0)
633     {
634       const REAL_VALUE_TYPE *t;
635       t = a, a = b, b = t;
636       dexp = -dexp;
637       sign ^= subtract_p;
638     }
639   exp = a->exp;
640
641   /* If the exponents are not identical, we need to shift the
642      significand of B down.  */
643   if (dexp > 0)
644     {
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)
648         {
649           *r = *a;
650           r->sign = sign;
651           return;
652         }
653
654       inexact |= sticky_rshift_significand (&t, b, dexp);
655       b = &t;
656     }
657
658   if (subtract_p)
659     {
660       if (sub_significands (r, a, b, inexact))
661         {
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);
668         }
669     }
670   else
671     {
672       if (add_significands (r, a, b))
673         {
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)
680             {
681               get_inf (r, sign);
682               return;
683             }
684         }
685     }
686
687   r->class = rvc_normal;
688   r->sign = sign;
689   r->exp = exp;
690
691   /* Re-normalize the result.  */
692   normalize (r);
693
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;
700 }
701
702 /* Return R = A * B.  */
703
704 static void
705 do_multiply (r, a, b)
706      REAL_VALUE_TYPE *r;
707      const REAL_VALUE_TYPE *a, *b;
708 {
709   REAL_VALUE_TYPE u, t, *rr;
710   unsigned int i, j, k;
711   int sign = a->sign ^ b->sign;
712
713   switch (CLASS2 (a->class, b->class))
714     {
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;
721
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;
730
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;
738
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;
744
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;
752
753     case CLASS2 (rvc_normal, rvc_normal):
754       break;
755
756     default:
757       abort ();
758     }
759
760   if (r == a || r == b)
761     rr = &t;
762   else
763     rr = r;
764   get_zero (rr, 0);
765
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.
768
769      Consider the long-hand form of a four half-word multiplication:
770
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
778
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.  */
783
784   for (i = 0; i < SIGSZ * 2; ++i)
785     {
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;
791
792       if (ai == 0)
793         continue;
794
795       for (j = 0; j < 2; ++j)
796         {
797           int exp = (a->exp - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
798                      + (b->exp - (1-j)*(HOST_BITS_PER_LONG/2)));
799
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;
805
806           u.class = rvc_normal;
807           u.sign = 0;
808           u.exp = exp;
809
810           for (k = j; k < SIGSZ * 2; k += 2)
811             {
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;
817
818               u.sig[k / 2] = ai * bi;
819             }
820
821           normalize (&u);
822           do_add (rr, rr, &u, 0);
823         }
824     }
825
826   rr->sign = sign;
827   if (rr != r)
828     *r = t;
829 }
830
831 /* Return R = A / B.  */
832
833 static void
834 do_divide (r, a, b)
835      REAL_VALUE_TYPE *r;
836      const REAL_VALUE_TYPE *a, *b;
837 {
838   int exp, sign = a->sign ^ b->sign;
839   REAL_VALUE_TYPE t, *rr;
840   bool inexact;
841
842   switch (CLASS2 (a->class, b->class))
843     {
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;
850
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;
859
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;
866
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;
875
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;
883
884     case CLASS2 (rvc_inf, rvc_normal):
885       /* Inf / R = Inf.  */
886     overflow:
887       get_inf (r, sign);
888       return;
889
890     case CLASS2 (rvc_normal, rvc_normal):
891       break;
892
893     default:
894       abort ();
895     }
896
897   if (r == a || r == b)
898     rr = &t;
899   else
900     rr = r;
901
902   rr->class = rvc_normal;
903   rr->sign = sign;
904
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;
911
912   inexact = div_significands (rr, a, b);
913
914   /* Re-normalize the result.  */
915   normalize (rr);
916   rr->sig[0] |= inexact;
917
918   if (rr != r)
919     *r = t;
920 }
921
922 /* Return a tri-state comparison of A vs B.  Return NAN_RESULT if
923    one of the two operands is a NaN.  */
924
925 static int
926 do_compare (a, b, nan_result)
927      const REAL_VALUE_TYPE *a, *b;
928      int nan_result;
929 {
930   int ret;
931
932   switch (CLASS2 (a->class, b->class))
933     {
934     case CLASS2 (rvc_zero, rvc_zero):
935       /* Sign of zero doesn't matter for compares.  */
936       return 0;
937
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);
942
943     case CLASS2 (rvc_inf, rvc_inf):
944       return -a->sign - -b->sign;
945
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);
950
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;
959
960     case CLASS2 (rvc_normal, rvc_normal):
961       break;
962
963     default:
964       abort ();
965     }
966
967   if (a->sign != b->sign)
968     return -a->sign - -b->sign;
969
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);
976
977   return (a->sign ? -ret : ret);
978 }
979
980 /* Return A truncated to an integral value toward zero.  */
981
982 static void
983 do_fix_trunc (r, a)
984      REAL_VALUE_TYPE *r;
985      const REAL_VALUE_TYPE *a;
986 {
987   *r = *a;
988
989   switch (r->class)
990     {
991     case rvc_zero:
992     case rvc_inf:
993     case rvc_nan:
994       break;
995
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;
1002
1003     default:
1004       abort ();
1005     }
1006 }
1007
1008 /* Perform the binary or unary operation described by CODE.
1009    For a unary operation, leave OP1 NULL.  */
1010
1011 void
1012 real_arithmetic (r, icode, op0, op1)
1013      REAL_VALUE_TYPE *r;
1014      int icode;
1015      const REAL_VALUE_TYPE *op0, *op1;
1016 {
1017   enum tree_code code = icode;
1018
1019   switch (code)
1020     {
1021     case PLUS_EXPR:
1022       do_add (r, op0, op1, 0);
1023       break;
1024
1025     case MINUS_EXPR:
1026       do_add (r, op0, op1, 1);
1027       break;
1028
1029     case MULT_EXPR:
1030       do_multiply (r, op0, op1);
1031       break;
1032
1033     case RDIV_EXPR:
1034       do_divide (r, op0, op1);
1035       break;
1036
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;
1045
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;
1054
1055     case NEGATE_EXPR:
1056       *r = *op0;
1057       r->sign ^= 1;
1058       break;
1059
1060     case ABS_EXPR:
1061       *r = *op0;
1062       r->sign = 0;
1063       break;
1064
1065     case FIX_TRUNC_EXPR:
1066       do_fix_trunc (r, op0);
1067       break;
1068
1069     default:
1070       abort ();
1071     }
1072 }
1073
1074 /* Legacy.  Similar, but return the result directly.  */
1075
1076 REAL_VALUE_TYPE
1077 real_arithmetic2 (icode, op0, op1)
1078      int icode;
1079      const REAL_VALUE_TYPE *op0, *op1;
1080 {
1081   REAL_VALUE_TYPE r;
1082   real_arithmetic (&r, icode, op0, op1);
1083   return r;
1084 }
1085
1086 bool
1087 real_compare (icode, op0, op1)
1088      int icode;
1089      const REAL_VALUE_TYPE *op0, *op1;
1090 {
1091   enum tree_code code = icode;
1092
1093   switch (code)
1094     {
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;
1121
1122     default:
1123       abort ();
1124     }
1125 }
1126
1127 /* Return floor log2(R).  */
1128
1129 int
1130 real_exponent (r)
1131      const REAL_VALUE_TYPE *r;
1132 {
1133   switch (r->class)
1134     {
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 ();
1144     }
1145 }
1146
1147 /* R = OP0 * 2**EXP.  */
1148
1149 void
1150 real_ldexp (r, op0, exp)
1151      REAL_VALUE_TYPE *r;
1152      const REAL_VALUE_TYPE *op0;
1153      int exp;
1154 {
1155   *r = *op0;
1156   switch (r->class)
1157     {
1158     case rvc_zero:
1159     case rvc_inf:
1160     case rvc_nan:
1161       break;
1162
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;
1172
1173     default:
1174       abort ();
1175     }
1176 }
1177
1178 /* Determine whether a floating-point value X is infinite.  */
1179
1180 bool
1181 real_isinf (r)
1182      const REAL_VALUE_TYPE *r;
1183 {
1184   return (r->class == rvc_inf);
1185 }
1186
1187 /* Determine whether a floating-point value X is a NaN.  */
1188
1189 bool
1190 real_isnan (r)
1191      const REAL_VALUE_TYPE *r;
1192 {
1193   return (r->class == rvc_nan);
1194 }
1195
1196 /* Determine whether a floating-point value X is negative.  */
1197
1198 bool
1199 real_isneg (r)
1200      const REAL_VALUE_TYPE *r;
1201 {
1202   return r->sign;
1203 }
1204
1205 /* Determine whether a floating-point value X is minus zero.  */
1206
1207 bool
1208 real_isnegzero (r)
1209      const REAL_VALUE_TYPE *r;
1210 {
1211   return r->sign && r->class == rvc_zero;
1212 }
1213
1214 /* Compare two floating-point objects for bitwise identity.  */
1215
1216 extern bool
1217 real_identical (a, b)
1218      const REAL_VALUE_TYPE *a, *b;
1219 {
1220   int i;
1221
1222   if (a->class != b->class)
1223     return false;
1224   if (a->sign != b->sign)
1225     return false;
1226
1227   switch (a->class)
1228     {
1229     case rvc_zero:
1230     case rvc_inf:
1231       break;
1232
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;
1242
1243     default:
1244       abort ();
1245     }
1246
1247   return true;
1248 }
1249
1250 /* Try to change R into its exact multiplicative inverse in machine
1251    mode MODE.  Return true if successful.  */
1252
1253 bool
1254 exact_real_inverse (mode, r)
1255      enum machine_mode mode;
1256      REAL_VALUE_TYPE *r;
1257 {
1258   const REAL_VALUE_TYPE *one = real_digit (1);
1259   REAL_VALUE_TYPE u;
1260   int i;
1261   
1262   if (r->class != rvc_normal)
1263     return false;
1264
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;
1271
1272   /* Find the inverse and truncate to the required mode.  */
1273   do_divide (&u, one, r);
1274   real_convert (&u, mode, &u);
1275   
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;
1284
1285   *r = u;
1286   return true;
1287 }
1288 \f
1289 /* Render R as an integer.  */
1290
1291 HOST_WIDE_INT
1292 real_to_integer (r)
1293      const REAL_VALUE_TYPE *r;
1294 {
1295   unsigned HOST_WIDE_INT i;
1296
1297   switch (r->class)
1298     {
1299     case rvc_zero:
1300     underflow:
1301       return 0;
1302
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;
1310
1311     case rvc_normal:
1312       if (r->exp <= 0)
1313         goto underflow;
1314       /* Only force overflow for unsigned overflow.  Signed overflow is
1315          undefined, so it doesn't matter what we return, and some callers
1316          expect to be able to use this routine for both signed and 
1317          unsigned conversions.  */
1318       if (r->exp > HOST_BITS_PER_WIDE_INT)
1319         goto overflow;
1320
1321       if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1322         i = r->sig[SIGSZ-1];
1323       else if (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG)
1324         {
1325           i = r->sig[SIGSZ-1];
1326           i = i << (HOST_BITS_PER_LONG - 1) << 1;
1327           i |= r->sig[SIGSZ-2];
1328         }
1329       else
1330         abort ();
1331
1332       i >>= HOST_BITS_PER_WIDE_INT - r->exp;
1333
1334       if (r->sign)
1335         i = -i;
1336       return i;
1337
1338     default:
1339       abort ();
1340     }
1341 }
1342
1343 /* Likewise, but to an integer pair, HI+LOW.  */
1344
1345 void
1346 real_to_integer2 (plow, phigh, r)
1347      HOST_WIDE_INT *plow, *phigh;
1348      const REAL_VALUE_TYPE *r;
1349 {
1350   REAL_VALUE_TYPE t;
1351   HOST_WIDE_INT low, high;
1352   int exp;
1353
1354   switch (r->class)
1355     {
1356     case rvc_zero:
1357     underflow:
1358       low = high = 0;
1359       break;
1360
1361     case rvc_inf:
1362     case rvc_nan:
1363     overflow:
1364       high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1365       if (r->sign)
1366         low = 0;
1367       else
1368         {
1369           high--;
1370           low = -1;
1371         }
1372       break;
1373
1374     case rvc_normal:
1375       exp = r->exp;
1376       if (exp <= 0)
1377         goto underflow;
1378       /* Only force overflow for unsigned overflow.  Signed overflow is
1379          undefined, so it doesn't matter what we return, and some callers
1380          expect to be able to use this routine for both signed and 
1381          unsigned conversions.  */
1382       if (exp > 2*HOST_BITS_PER_WIDE_INT)
1383         goto overflow;
1384
1385       rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
1386       if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1387         {
1388           high = t.sig[SIGSZ-1];
1389           low = t.sig[SIGSZ-2];
1390         }
1391       else if (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG)
1392         {
1393           high = t.sig[SIGSZ-1];
1394           high = high << (HOST_BITS_PER_LONG - 1) << 1;
1395           high |= t.sig[SIGSZ-2];
1396
1397           low = t.sig[SIGSZ-3];
1398           low = low << (HOST_BITS_PER_LONG - 1) << 1;
1399           low |= t.sig[SIGSZ-4];
1400         }
1401       else
1402         abort ();
1403
1404       if (r->sign)
1405         {
1406           if (low == 0)
1407             high = -high;
1408           else
1409             low = -low, high = ~high;
1410         }
1411       break;
1412
1413     default:
1414       abort ();
1415     }
1416
1417   *plow = low;
1418   *phigh = high;
1419 }
1420
1421 /* A subroutine of real_to_decimal.  Compute the quotient and remainder
1422    of NUM / DEN.  Return the quotient and place the remainder in NUM.
1423    It is expected that NUM / DEN are close enough that the quotient is
1424    small.  */
1425
1426 static unsigned long
1427 rtd_divmod (num, den)
1428      REAL_VALUE_TYPE *num, *den;
1429 {
1430   unsigned long q, msb;
1431   int expn = num->exp, expd = den->exp;
1432
1433   if (expn < expd)
1434     return 0;
1435
1436   q = msb = 0;
1437   goto start;
1438   do
1439     {
1440       msb = num->sig[SIGSZ-1] & SIG_MSB;
1441       q <<= 1;
1442       lshift_significand_1 (num, num);
1443     start:
1444       if (msb || cmp_significands (num, den) >= 0)
1445         {
1446           sub_significands (num, num, den, 0);
1447           q |= 1;
1448         }
1449     }
1450   while (--expn >= expd);
1451
1452   num->exp = expd;
1453   normalize (num);
1454
1455   return q;
1456 }
1457
1458 /* Render R as a decimal floating point constant.  Emit DIGITS significant
1459    digits in the result, bounded by BUF_SIZE.  If DIGITS is 0, choose the
1460    maximum for the representation.  If CROP_TRAILING_ZEROS, strip trailing
1461    zeros.  */
1462
1463 #define M_LOG10_2       0.30102999566398119521
1464
1465 void
1466 real_to_decimal (str, r_orig, buf_size, digits, crop_trailing_zeros)
1467      char *str;
1468      const REAL_VALUE_TYPE *r_orig;
1469      size_t buf_size, digits;
1470      int crop_trailing_zeros;
1471 {
1472   const REAL_VALUE_TYPE *one, *ten;
1473   REAL_VALUE_TYPE r, pten, u, v;
1474   int dec_exp, cmp_one, digit;
1475   size_t max_digits;
1476   char *p, *first, *last;
1477   bool sign;
1478
1479   r = *r_orig;
1480   switch (r.class)
1481     {
1482     case rvc_zero:
1483       strcpy (str, (r.sign ? "-0.0" : "0.0"));
1484       return;
1485     case rvc_normal:
1486       break;
1487     case rvc_inf:
1488       strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1489       return;
1490     case rvc_nan:
1491       /* ??? Print the significand as well, if not canonical?  */
1492       strcpy (str, (r.sign ? "-NaN" : "+NaN"));
1493       return;
1494     default:
1495       abort ();
1496     }
1497
1498   /* Bound the number of digits printed by the size of the representation.  */
1499   max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1500   if (digits == 0 || digits > max_digits)
1501     digits = max_digits;
1502
1503   /* Estimate the decimal exponent, and compute the length of the string it
1504      will print as.  Be conservative and add one to account for possible
1505      overflow or rounding error.  */
1506   dec_exp = r.exp * M_LOG10_2;
1507   for (max_digits = 1; dec_exp ; max_digits++)
1508     dec_exp /= 10;
1509
1510   /* Bound the number of digits printed by the size of the output buffer.  */
1511   max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1512   if (max_digits > buf_size)
1513     abort ();
1514   if (digits > max_digits)
1515     digits = max_digits;
1516
1517   one = real_digit (1);
1518   ten = ten_to_ptwo (0);
1519
1520   sign = r.sign;
1521   r.sign = 0;
1522
1523   dec_exp = 0;
1524   pten = *one;
1525
1526   cmp_one = do_compare (&r, one, 0);
1527   if (cmp_one > 0)
1528     {
1529       int m;
1530
1531       /* Number is greater than one.  Convert significand to an integer
1532          and strip trailing decimal zeros.  */
1533
1534       u = r;
1535       u.exp = SIGNIFICAND_BITS - 1;
1536
1537       /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS.  */
1538       m = floor_log2 (max_digits);
1539
1540       /* Iterate over the bits of the possible powers of 10 that might
1541          be present in U and eliminate them.  That is, if we find that
1542          10**2**M divides U evenly, keep the division and increase 
1543          DEC_EXP by 2**M.  */
1544       do
1545         {
1546           REAL_VALUE_TYPE t;
1547
1548           do_divide (&t, &u, ten_to_ptwo (m));
1549           do_fix_trunc (&v, &t);
1550           if (cmp_significands (&v, &t) == 0)
1551             {
1552               u = t;
1553               dec_exp += 1 << m;
1554             }
1555         }
1556       while (--m >= 0);
1557
1558       /* Revert the scaling to integer that we performed earlier.  */
1559       u.exp += r.exp - (SIGNIFICAND_BITS - 1);
1560       r = u;
1561
1562       /* Find power of 10.  Do this by dividing out 10**2**M when
1563          this is larger than the current remainder.  Fill PTEN with 
1564          the power of 10 that we compute.  */
1565       if (r.exp > 0)
1566         {
1567           m = floor_log2 ((int)(r.exp * M_LOG10_2)) + 1;
1568           do
1569             {
1570               const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1571               if (do_compare (&u, ptentwo, 0) >= 0)
1572                 {
1573                   do_divide (&u, &u, ptentwo);
1574                   do_multiply (&pten, &pten, ptentwo);
1575                   dec_exp += 1 << m;
1576                 }
1577             }
1578           while (--m >= 0);
1579         }
1580       else
1581         /* We managed to divide off enough tens in the above reduction
1582            loop that we've now got a negative exponent.  Fall into the
1583            less-than-one code to compute the proper value for PTEN.  */
1584         cmp_one = -1;
1585     }
1586   if (cmp_one < 0)
1587     {
1588       int m;
1589
1590       /* Number is less than one.  Pad significand with leading
1591          decimal zeros.  */
1592
1593       v = r;
1594       while (1)
1595         {
1596           /* Stop if we'd shift bits off the bottom.  */
1597           if (v.sig[0] & 7)
1598             break;
1599
1600           do_multiply (&u, &v, ten);
1601
1602           /* Stop if we're now >= 1.  */
1603           if (u.exp > 0)
1604             break;
1605
1606           v = u;
1607           dec_exp -= 1;
1608         }
1609       r = v;
1610
1611       /* Find power of 10.  Do this by multiplying in P=10**2**M when
1612          the current remainder is smaller than 1/P.  Fill PTEN with the
1613          power of 10 that we compute.  */
1614       m = floor_log2 ((int)(-r.exp * M_LOG10_2)) + 1;
1615       do
1616         {
1617           const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1618           const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1619
1620           if (do_compare (&v, ptenmtwo, 0) <= 0)
1621             {
1622               do_multiply (&v, &v, ptentwo);
1623               do_multiply (&pten, &pten, ptentwo);
1624               dec_exp -= 1 << m;
1625             }
1626         }
1627       while (--m >= 0);
1628
1629       /* Invert the positive power of 10 that we've collected so far.  */
1630       do_divide (&pten, one, &pten);
1631     }
1632
1633   p = str;
1634   if (sign)
1635     *p++ = '-';
1636   first = p++;
1637
1638   /* At this point, PTEN should contain the nearest power of 10 smaller
1639      than R, such that this division produces the first digit.
1640
1641      Using a divide-step primitive that returns the complete integral
1642      remainder avoids the rounding error that would be produced if
1643      we were to use do_divide here and then simply multiply by 10 for
1644      each subsequent digit.  */
1645
1646   digit = rtd_divmod (&r, &pten);
1647
1648   /* Be prepared for error in that division via underflow ...  */
1649   if (digit == 0 && cmp_significand_0 (&r))
1650     {
1651       /* Multiply by 10 and try again.  */
1652       do_multiply (&r, &r, ten);
1653       digit = rtd_divmod (&r, &pten);
1654       dec_exp -= 1;
1655       if (digit == 0)
1656         abort ();
1657     }
1658
1659   /* ... or overflow.  */
1660   if (digit == 10)
1661     {
1662       *p++ = '1';
1663       if (--digits > 0)
1664         *p++ = '0';
1665       dec_exp += 1;
1666     }
1667   else if (digit > 10)
1668     abort ();
1669   else
1670     *p++ = digit + '0';
1671
1672   /* Generate subsequent digits.  */
1673   while (--digits > 0)
1674     {
1675       do_multiply (&r, &r, ten);
1676       digit = rtd_divmod (&r, &pten);
1677       *p++ = digit + '0';
1678     }
1679   last = p;
1680
1681   /* Generate one more digit with which to do rounding.  */
1682   do_multiply (&r, &r, ten);
1683   digit = rtd_divmod (&r, &pten);
1684
1685   /* Round the result.  */
1686   if (digit == 5)
1687     {
1688       /* Round to nearest.  If R is nonzero there are additional
1689          nonzero digits to be extracted.  */
1690       if (cmp_significand_0 (&r))
1691         digit++;
1692       /* Round to even.  */
1693       else if ((p[-1] - '0') & 1)
1694         digit++;
1695     }
1696   if (digit > 5)
1697     {
1698       while (p > first)
1699         {
1700           digit = *--p;
1701           if (digit == '9')
1702             *p = '0';
1703           else
1704             {
1705               *p = digit + 1;
1706               break;
1707             }
1708         }
1709
1710       /* Carry out of the first digit.  This means we had all 9's and
1711          now have all 0's.  "Prepend" a 1 by overwriting the first 0.  */
1712       if (p == first)
1713         {
1714           first[1] = '1';
1715           dec_exp++;
1716         }
1717     }
1718   
1719   /* Insert the decimal point.  */
1720   first[0] = first[1];
1721   first[1] = '.';
1722
1723   /* If requested, drop trailing zeros.  Never crop past "1.0".  */
1724   if (crop_trailing_zeros)
1725     while (last > first + 3 && last[-1] == '0')
1726       last--;
1727
1728   /* Append the exponent.  */
1729   sprintf (last, "e%+d", dec_exp);
1730 }
1731
1732 /* Render R as a hexadecimal floating point constant.  Emit DIGITS
1733    significant digits in the result, bounded by BUF_SIZE.  If DIGITS is 0,
1734    choose the maximum for the representation.  If CROP_TRAILING_ZEROS,
1735    strip trailing zeros.  */
1736
1737 void
1738 real_to_hexadecimal (str, r, buf_size, digits, crop_trailing_zeros)
1739      char *str;
1740      const REAL_VALUE_TYPE *r;
1741      size_t buf_size, digits;
1742      int crop_trailing_zeros;
1743 {
1744   int i, j, exp = r->exp;
1745   char *p, *first;
1746   char exp_buf[16];
1747   size_t max_digits;
1748
1749   switch (r->class)
1750     {
1751     case rvc_zero:
1752       exp = 0;
1753       break;
1754     case rvc_normal:
1755       break;
1756     case rvc_inf:
1757       strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1758       return;
1759     case rvc_nan:
1760       /* ??? Print the significand as well, if not canonical?  */
1761       strcpy (str, (r->sign ? "-NaN" : "+NaN"));
1762       return;
1763     default:
1764       abort ();
1765     }
1766
1767   if (digits == 0)
1768     digits = SIGNIFICAND_BITS / 4;
1769
1770   /* Bound the number of digits printed by the size of the output buffer.  */
1771
1772   sprintf (exp_buf, "p%+d", exp);
1773   max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1774   if (max_digits > buf_size)
1775     abort ();
1776   if (digits > max_digits)
1777     digits = max_digits;
1778
1779   p = str;
1780   if (r->sign)
1781     *p++ = '-';
1782   *p++ = '0';
1783   *p++ = 'x';
1784   *p++ = '0';
1785   *p++ = '.';
1786   first = p;
1787
1788   for (i = SIGSZ - 1; i >= 0; --i)
1789     for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1790       {
1791         *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1792         if (--digits == 0)
1793           goto out;
1794       }
1795
1796  out:
1797   if (crop_trailing_zeros)
1798     while (p > first + 1 && p[-1] == '0')
1799       p--;
1800
1801   sprintf (p, "p%+d", exp);
1802 }
1803
1804 /* Initialize R from a decimal or hexadecimal string.  The string is
1805    assumed to have been syntax checked already.  */
1806
1807 void
1808 real_from_string (r, str)
1809      REAL_VALUE_TYPE *r;
1810      const char *str;
1811 {
1812   int exp = 0;
1813   bool sign = false;
1814
1815   get_zero (r, 0);
1816
1817   if (*str == '-')
1818     {
1819       sign = true;
1820       str++;
1821     }
1822   else if (*str == '+')
1823     str++;
1824
1825   if (str[0] == '0' && str[1] == 'x')
1826     {
1827       /* Hexadecimal floating point.  */
1828       int pos = SIGNIFICAND_BITS - 4, d;
1829
1830       str += 2;
1831
1832       while (*str == '0')
1833         str++;
1834       while (1)
1835         {
1836           d = hex_value (*str);
1837           if (d == _hex_bad)
1838             break;
1839           if (pos >= 0)
1840             {
1841               r->sig[pos / HOST_BITS_PER_LONG]
1842                 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1843               pos -= 4;
1844             }
1845           exp += 4;
1846           str++;
1847         }
1848       if (*str == '.')
1849         {
1850           str++;
1851           if (pos == SIGNIFICAND_BITS - 4)
1852             {
1853               while (*str == '0')
1854                 str++, exp -= 4;
1855             }
1856           while (1)
1857             {
1858               d = hex_value (*str);
1859               if (d == _hex_bad)
1860                 break;
1861               if (pos >= 0)
1862                 {
1863                   r->sig[pos / HOST_BITS_PER_LONG]
1864                     |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1865                   pos -= 4;
1866                 }
1867               str++;
1868             }
1869         }
1870       if (*str == 'p' || *str == 'P')
1871         {
1872           bool exp_neg = false;
1873
1874           str++;
1875           if (*str == '-')
1876             {
1877               exp_neg = true;
1878               str++;
1879             }
1880           else if (*str == '+')
1881             str++;
1882
1883           d = 0;
1884           while (ISDIGIT (*str))
1885             {
1886               d *= 10;
1887               d += *str - '0';
1888               if (d > MAX_EXP)
1889                 {
1890                   /* Overflowed the exponent.  */
1891                   if (exp_neg)
1892                     goto underflow;
1893                   else
1894                     goto overflow;
1895                 }
1896               str++;
1897             }
1898           if (exp_neg)
1899             d = -d;
1900
1901           exp += d;
1902         }
1903
1904       r->class = rvc_normal;
1905       r->exp = exp;
1906
1907       normalize (r);
1908     }
1909   else
1910     {
1911       /* Decimal floating point.  */
1912       const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
1913       int d;
1914
1915       while (*str == '0')
1916         str++;
1917       while (ISDIGIT (*str))
1918         {
1919           d = *str++ - '0';
1920           do_multiply (r, r, ten);
1921           if (d)
1922             do_add (r, r, real_digit (d), 0);
1923         }
1924       if (*str == '.')
1925         {
1926           str++;
1927           if (r->class == rvc_zero)
1928             {
1929               while (*str == '0')
1930                 str++, exp--;
1931             }
1932           while (ISDIGIT (*str))
1933             {
1934               d = *str++ - '0';
1935               do_multiply (r, r, ten);
1936               if (d)
1937                 do_add (r, r, real_digit (d), 0);
1938               exp--;
1939             }
1940         }
1941
1942       if (*str == 'e' || *str == 'E')
1943         {
1944           bool exp_neg = false;
1945
1946           str++;
1947           if (*str == '-')
1948             {
1949               exp_neg = true;
1950               str++;
1951             }
1952           else if (*str == '+')
1953             str++;
1954
1955           d = 0;
1956           while (ISDIGIT (*str))
1957             {
1958               d *= 10;
1959               d += *str - '0';
1960               if (d > MAX_EXP)
1961                 {
1962                   /* Overflowed the exponent.  */
1963                   if (exp_neg)
1964                     goto underflow;
1965                   else
1966                     goto overflow;
1967                 }
1968               str++;
1969             }
1970           if (exp_neg)
1971             d = -d;
1972           exp += d;
1973         }
1974
1975       if (exp)
1976         times_pten (r, exp);
1977     }
1978
1979   r->sign = sign;
1980   return;
1981
1982  underflow:
1983   get_zero (r, sign);
1984   return;
1985
1986  overflow:
1987   get_inf (r, sign);
1988   return;
1989 }
1990
1991 /* Legacy.  Similar, but return the result directly.  */
1992
1993 REAL_VALUE_TYPE
1994 real_from_string2 (s, mode)
1995      const char *s;
1996      enum machine_mode mode;
1997 {
1998   REAL_VALUE_TYPE r;
1999
2000   real_from_string (&r, s);
2001   if (mode != VOIDmode)
2002     real_convert (&r, mode, &r);
2003
2004   return r;
2005 }
2006
2007 /* Initialize R from the integer pair HIGH+LOW.  */
2008
2009 void
2010 real_from_integer (r, mode, low, high, unsigned_p)
2011      REAL_VALUE_TYPE *r;
2012      enum machine_mode mode;
2013      unsigned HOST_WIDE_INT low;
2014      HOST_WIDE_INT high;
2015      int unsigned_p;
2016 {
2017   if (low == 0 && high == 0)
2018     get_zero (r, 0);
2019   else
2020     {
2021       r->class = rvc_normal;
2022       r->sign = high < 0 && !unsigned_p;
2023       r->exp = 2 * HOST_BITS_PER_WIDE_INT;
2024
2025       if (r->sign)
2026         {
2027           high = ~high;
2028           if (low == 0)
2029             high += 1;
2030           else
2031             low = -low;
2032         }
2033
2034       if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2035         {
2036           r->sig[SIGSZ-1] = high;
2037           r->sig[SIGSZ-2] = low;
2038           memset (r->sig, 0, sizeof(long)*(SIGSZ-2));
2039         }
2040       else if (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT)
2041         {
2042           r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
2043           r->sig[SIGSZ-2] = high;
2044           r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
2045           r->sig[SIGSZ-4] = low;
2046           if (SIGSZ > 4)
2047             memset (r->sig, 0, sizeof(long)*(SIGSZ-4));
2048         }
2049       else
2050         abort ();
2051
2052       normalize (r);
2053     }
2054
2055   if (mode != VOIDmode)
2056     real_convert (r, mode, r);
2057 }
2058
2059 /* Returns 10**2**N.  */
2060
2061 static const REAL_VALUE_TYPE *
2062 ten_to_ptwo (n)
2063      int n;
2064 {
2065   static REAL_VALUE_TYPE tens[EXP_BITS];
2066
2067   if (n < 0 || n >= EXP_BITS)
2068     abort ();
2069
2070   if (tens[n].class == rvc_zero)
2071     {
2072       if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2073         {
2074           HOST_WIDE_INT t = 10;
2075           int i;
2076
2077           for (i = 0; i < n; ++i)
2078             t *= t;
2079
2080           real_from_integer (&tens[n], VOIDmode, t, 0, 1);
2081         }
2082       else
2083         {
2084           const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2085           do_multiply (&tens[n], t, t);
2086         }
2087     }
2088
2089   return &tens[n];
2090 }
2091
2092 /* Returns 10**(-2**N).  */
2093
2094 static const REAL_VALUE_TYPE *
2095 ten_to_mptwo (n)
2096      int n;
2097 {
2098   static REAL_VALUE_TYPE tens[EXP_BITS];
2099
2100   if (n < 0 || n >= EXP_BITS)
2101     abort ();
2102
2103   if (tens[n].class == rvc_zero)
2104     do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2105
2106   return &tens[n];
2107 }
2108
2109 /* Returns N.  */
2110
2111 static const REAL_VALUE_TYPE *
2112 real_digit (n)
2113      int n;
2114 {
2115   static REAL_VALUE_TYPE num[10];
2116
2117   if (n < 0 || n > 9)
2118     abort ();
2119
2120   if (n > 0 && num[n].class == rvc_zero)
2121     real_from_integer (&num[n], VOIDmode, n, 0, 1);
2122
2123   return &num[n];
2124 }
2125
2126 /* Multiply R by 10**EXP.  */
2127
2128 static void
2129 times_pten (r, exp)
2130      REAL_VALUE_TYPE *r;
2131      int exp;
2132 {
2133   REAL_VALUE_TYPE pten, *rr;
2134   bool negative = (exp < 0);
2135   int i;
2136
2137   if (negative)
2138     {
2139       exp = -exp;
2140       pten = *real_digit (1);
2141       rr = &pten;
2142     }
2143   else
2144     rr = r;
2145
2146   for (i = 0; exp > 0; ++i, exp >>= 1)
2147     if (exp & 1)
2148       do_multiply (rr, rr, ten_to_ptwo (i));
2149
2150   if (negative)
2151     do_divide (r, r, &pten);
2152 }
2153
2154 /* Fills R with +Inf.  */
2155
2156 void
2157 real_inf (r)
2158      REAL_VALUE_TYPE *r;
2159 {
2160   get_inf (r, 0);
2161 }
2162
2163 /* Fills R with a NaN whose significand is described by STR.  If QUIET,
2164    we force a QNaN, else we force an SNaN.  The string, if not empty,
2165    is parsed as a number and placed in the significand.  Return true
2166    if the string was successfully parsed.  */
2167
2168 bool
2169 real_nan (r, str, quiet, mode)
2170      REAL_VALUE_TYPE *r;
2171      const char *str;
2172      int quiet;
2173      enum machine_mode mode;
2174 {
2175   const struct real_format *fmt;
2176
2177   fmt = real_format_for_mode[mode - QFmode];
2178   if (fmt == NULL)
2179     abort ();
2180
2181   if (*str == 0)
2182     {
2183       if (quiet)
2184         get_canonical_qnan (r, 0);
2185       else
2186         get_canonical_snan (r, 0);
2187     }
2188   else
2189     {
2190       int base = 10, d;
2191       bool neg = false;
2192
2193       memset (r, 0, sizeof (*r));
2194       r->class = rvc_nan;
2195
2196       /* Parse akin to strtol into the significand of R.  */
2197
2198       while (ISSPACE (*str))
2199         str++;
2200       if (*str == '-')
2201         str++, neg = true;
2202       else if (*str == '+')
2203         str++;
2204       if (*str == '0')
2205         {
2206           if (*++str == 'x')
2207             str++, base = 16;
2208           else
2209             base = 8;
2210         }
2211
2212       while ((d = hex_value (*str)) < base)
2213         {
2214           REAL_VALUE_TYPE u;
2215
2216           switch (base)
2217             {
2218             case 8:
2219               lshift_significand (r, r, 3);
2220               break;
2221             case 16:
2222               lshift_significand (r, r, 4);
2223               break;
2224             case 10:
2225               lshift_significand_1 (&u, r);
2226               lshift_significand (r, r, 3);
2227               add_significands (r, r, &u);
2228               break;
2229             default:
2230               abort ();
2231             }
2232
2233           get_zero (&u, 0);
2234           u.sig[0] = d;
2235           add_significands (r, r, &u);
2236
2237           str++;
2238         }
2239
2240       /* Must have consumed the entire string for success.  */
2241       if (*str != 0)
2242         return false;
2243
2244       /* Shift the significand into place such that the bits
2245          are in the most significant bits for the format.  */
2246       lshift_significand (r, r, SIGNIFICAND_BITS - fmt->p);
2247
2248       /* Our MSB is always unset for NaNs.  */
2249       r->sig[SIGSZ-1] &= ~SIG_MSB;
2250
2251       /* Force quiet or signalling NaN.  */
2252       if (quiet)
2253         r->sig[SIGSZ-1] |= SIG_MSB >> 1;
2254       else
2255         r->sig[SIGSZ-1] &= ~(SIG_MSB >> 1);
2256
2257       /* Force at least one bit of the significand set.  */
2258       for (d = 0; d < SIGSZ; ++d)
2259         if (r->sig[d])
2260           break;
2261       if (d == SIGSZ)
2262         r->sig[SIGSZ-1] |= SIG_MSB >> 2;
2263
2264       /* Our intermediate format forces QNaNs to have MSB-1 set.
2265          If the target format has QNaNs with the top bit unset,
2266          mirror the output routines and invert the top two bits.  */
2267       if (!fmt->qnan_msb_set)
2268         r->sig[SIGSZ-1] ^= (SIG_MSB >> 1) | (SIG_MSB >> 2);
2269     }
2270
2271   return true;
2272 }
2273
2274 /* Fills R with 2**N.  */
2275
2276 void
2277 real_2expN (r, n)
2278      REAL_VALUE_TYPE *r;
2279      int n;
2280 {
2281   memset (r, 0, sizeof (*r));
2282
2283   n++;
2284   if (n > MAX_EXP)
2285     r->class = rvc_inf;
2286   else if (n < -MAX_EXP)
2287     ;
2288   else
2289     {
2290       r->class = rvc_normal;
2291       r->exp = n;
2292       r->sig[SIGSZ-1] = SIG_MSB;
2293     }
2294 }
2295
2296 \f
2297 static void
2298 round_for_format (fmt, r)
2299      const struct real_format *fmt;
2300      REAL_VALUE_TYPE *r;
2301 {
2302   int p2, np2, i, w;
2303   unsigned long sticky;
2304   bool guard, lsb;
2305   int emin2m1, emax2;
2306
2307   p2 = fmt->p * fmt->log2_b;
2308   emin2m1 = (fmt->emin - 1) * fmt->log2_b;
2309   emax2 = fmt->emax * fmt->log2_b;
2310
2311   np2 = SIGNIFICAND_BITS - p2;
2312   switch (r->class)
2313     {
2314     underflow:
2315       get_zero (r, r->sign);
2316     case rvc_zero:
2317       if (!fmt->has_signed_zero)
2318         r->sign = 0;
2319       return;
2320
2321     overflow:
2322       get_inf (r, r->sign);
2323     case rvc_inf:
2324       return;
2325
2326     case rvc_nan:
2327       clear_significand_below (r, np2);
2328
2329       /* If we've cleared the entire significand, we need one bit
2330          set for this to continue to be a NaN.  */
2331       for (i = 0; i < SIGSZ; ++i)
2332         if (r->sig[i])
2333           break;
2334       if (i == SIGSZ)
2335         r->sig[SIGSZ-1] = SIG_MSB >> 2;
2336       return;
2337
2338     case rvc_normal:
2339       break;
2340
2341     default:
2342       abort ();
2343     }
2344
2345   /* If we're not base2, normalize the exponent to a multiple of
2346      the true base.  */
2347   if (fmt->log2_b != 1)
2348     {
2349       int shift = r->exp & (fmt->log2_b - 1);
2350       if (shift)
2351         {
2352           shift = fmt->log2_b - shift;
2353           r->sig[0] |= sticky_rshift_significand (r, r, shift);
2354           r->exp += shift;
2355         }
2356     }
2357
2358   /* Check the range of the exponent.  If we're out of range,
2359      either underflow or overflow.  */
2360   if (r->exp > emax2)
2361     goto overflow;
2362   else if (r->exp <= emin2m1)
2363     {
2364       int diff;
2365
2366       if (!fmt->has_denorm)
2367         {
2368           /* Don't underflow completely until we've had a chance to round.  */
2369           if (r->exp < emin2m1)
2370             goto underflow;
2371         }
2372       else
2373         {
2374           diff = emin2m1 - r->exp + 1;
2375           if (diff > p2)
2376             goto underflow;
2377
2378           /* De-normalize the significand.  */
2379           r->sig[0] |= sticky_rshift_significand (r, r, diff);
2380           r->exp += diff;
2381         }
2382     }
2383
2384   /* There are P2 true significand bits, followed by one guard bit,
2385      followed by one sticky bit, followed by stuff.  Fold nonzero
2386      stuff into the sticky bit.  */
2387
2388   sticky = 0;
2389   for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2390     sticky |= r->sig[i];
2391   sticky |=
2392     r->sig[w] & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2393
2394   guard = test_significand_bit (r, np2 - 1);
2395   lsb = test_significand_bit (r, np2);
2396
2397   /* Round to even.  */
2398   if (guard && (sticky || lsb))
2399     {
2400       REAL_VALUE_TYPE u;
2401       get_zero (&u, 0);
2402       set_significand_bit (&u, np2);
2403
2404       if (add_significands (r, r, &u))
2405         {
2406           /* Overflow.  Means the significand had been all ones, and
2407              is now all zeros.  Need to increase the exponent, and
2408              possibly re-normalize it.  */
2409           if (++r->exp > emax2)
2410             goto overflow;
2411           r->sig[SIGSZ-1] = SIG_MSB;
2412
2413           if (fmt->log2_b != 1)
2414             {
2415               int shift = r->exp & (fmt->log2_b - 1);
2416               if (shift)
2417                 {
2418                   shift = fmt->log2_b - shift;
2419                   rshift_significand (r, r, shift);
2420                   r->exp += shift;
2421                   if (r->exp > emax2)
2422                     goto overflow;
2423                 }
2424             }
2425         }
2426     }
2427
2428   /* Catch underflow that we deferred until after rounding.  */
2429   if (r->exp <= emin2m1)
2430     goto underflow;
2431
2432   /* Clear out trailing garbage.  */
2433   clear_significand_below (r, np2);
2434 }
2435
2436 /* Extend or truncate to a new mode.  */
2437
2438 void
2439 real_convert (r, mode, a)
2440      REAL_VALUE_TYPE *r;
2441      enum machine_mode mode;
2442      const REAL_VALUE_TYPE *a;
2443 {
2444   const struct real_format *fmt;
2445
2446   fmt = real_format_for_mode[mode - QFmode];
2447   if (fmt == NULL)
2448     abort ();
2449
2450   *r = *a;
2451   round_for_format (fmt, r);
2452
2453   /* round_for_format de-normalizes denormals.  Undo just that part.  */
2454   if (r->class == rvc_normal)
2455     normalize (r);
2456 }
2457
2458 /* Legacy.  Likewise, except return the struct directly.  */
2459
2460 REAL_VALUE_TYPE
2461 real_value_truncate (mode, a)
2462      enum machine_mode mode;
2463      REAL_VALUE_TYPE a;
2464 {
2465   REAL_VALUE_TYPE r;
2466   real_convert (&r, mode, &a);
2467   return r;
2468 }
2469
2470 /* Return true if truncating to MODE is exact.  */
2471
2472 bool
2473 exact_real_truncate (mode, a)
2474      enum machine_mode mode;
2475      const REAL_VALUE_TYPE *a;
2476 {
2477   REAL_VALUE_TYPE t;
2478   real_convert (&t, mode, a);
2479   return real_identical (&t, a);
2480 }
2481
2482 /* Write R to the given target format.  Place the words of the result
2483    in target word order in BUF.  There are always 32 bits in each
2484    long, no matter the size of the host long.
2485
2486    Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE.  */
2487
2488 long
2489 real_to_target_fmt (buf, r_orig, fmt)
2490      long *buf;
2491      const REAL_VALUE_TYPE *r_orig;
2492      const struct real_format *fmt;
2493 {
2494   REAL_VALUE_TYPE r;
2495   long buf1;
2496
2497   r = *r_orig;
2498   round_for_format (fmt, &r);
2499
2500   if (!buf)
2501     buf = &buf1;
2502   (*fmt->encode) (fmt, buf, &r);
2503
2504   return *buf;
2505 }
2506
2507 /* Similar, but look up the format from MODE.  */
2508
2509 long
2510 real_to_target (buf, r, mode)
2511      long *buf;
2512      const REAL_VALUE_TYPE *r;
2513      enum machine_mode mode;
2514 {
2515   const struct real_format *fmt;
2516
2517   fmt = real_format_for_mode[mode - QFmode];
2518   if (fmt == NULL)
2519     abort ();
2520
2521   return real_to_target_fmt (buf, r, fmt);
2522 }
2523
2524 /* Read R from the given target format.  Read the words of the result
2525    in target word order in BUF.  There are always 32 bits in each
2526    long, no matter the size of the host long.  */
2527
2528 void
2529 real_from_target_fmt (r, buf, fmt)
2530      REAL_VALUE_TYPE *r;
2531      const long *buf;
2532      const struct real_format *fmt;
2533 {
2534   (*fmt->decode) (fmt, r, buf);
2535 }     
2536
2537 /* Similar, but look up the format from MODE.  */
2538
2539 void
2540 real_from_target (r, buf, mode)
2541      REAL_VALUE_TYPE *r;
2542      const long *buf;
2543      enum machine_mode mode;
2544 {
2545   const struct real_format *fmt;
2546
2547   fmt = real_format_for_mode[mode - QFmode];
2548   if (fmt == NULL)
2549     abort ();
2550
2551   (*fmt->decode) (fmt, r, buf);
2552 }     
2553
2554 /* Return the number of bits in the significand for MODE.  */
2555 /* ??? Legacy.  Should get access to real_format directly.  */
2556
2557 int
2558 significand_size (mode)
2559      enum machine_mode mode;
2560 {
2561   const struct real_format *fmt;
2562
2563   fmt = real_format_for_mode[mode - QFmode];
2564   if (fmt == NULL)
2565     return 0;
2566
2567   return fmt->p * fmt->log2_b;
2568 }
2569
2570 /* Return a hash value for the given real value.  */
2571 /* ??? The "unsigned int" return value is intended to be hashval_t,
2572    but I didn't want to pull hashtab.h into real.h.  */
2573
2574 unsigned int
2575 real_hash (r)
2576      const REAL_VALUE_TYPE *r;
2577 {
2578   unsigned int h;
2579   size_t i;
2580
2581   h = r->class | (r->sign << 2);
2582   switch (r->class)
2583     {
2584     case rvc_zero:
2585     case rvc_inf:
2586       break;
2587
2588     case rvc_normal:
2589       h |= r->exp << 3;
2590       /* FALLTHRU */
2591
2592     case rvc_nan:
2593       if (sizeof(unsigned long) > sizeof(unsigned int))
2594         for (i = 0; i < SIGSZ; ++i)
2595           {
2596             unsigned long s = r->sig[i];
2597             h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2598           }
2599       else
2600         for (i = 0; i < SIGSZ; ++i)
2601           h ^= r->sig[i];
2602       break;
2603
2604     default:
2605       abort ();
2606     }
2607
2608   return h;
2609 }
2610 \f
2611 /* IEEE single-precision format.  */
2612
2613 static void encode_ieee_single PARAMS ((const struct real_format *fmt,
2614                                         long *, const REAL_VALUE_TYPE *));
2615 static void decode_ieee_single PARAMS ((const struct real_format *,
2616                                         REAL_VALUE_TYPE *, const long *));
2617
2618 static void
2619 encode_ieee_single (fmt, buf, r)
2620      const struct real_format *fmt;
2621      long *buf;
2622      const REAL_VALUE_TYPE *r;
2623 {
2624   unsigned long image, sig, exp;
2625   bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2626
2627   image = r->sign << 31;
2628   sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2629
2630   switch (r->class)
2631     {
2632     case rvc_zero:
2633       break;
2634
2635     case rvc_inf:
2636       if (fmt->has_inf)
2637         image |= 255 << 23;
2638       else
2639         image |= 0x7fffffff;
2640       break;
2641
2642     case rvc_nan:
2643       if (fmt->has_nans)
2644         {
2645           image |= 255 << 23;
2646           image |= sig;
2647           if (!fmt->qnan_msb_set)
2648             image ^= 1 << 23 | 1 << 22;
2649         }
2650       else
2651         image |= 0x7fffffff;
2652       break;
2653
2654     case rvc_normal:
2655       /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2656          whereas the intermediate representation is 0.F x 2**exp.
2657          Which means we're off by one.  */
2658       if (denormal)
2659         exp = 0;
2660       else
2661       exp = r->exp + 127 - 1;
2662       image |= exp << 23;
2663       image |= sig;
2664       break;
2665
2666     default:
2667       abort ();
2668     }
2669
2670   buf[0] = image;
2671 }
2672
2673 static void
2674 decode_ieee_single (fmt, r, buf)
2675      const struct real_format *fmt;
2676      REAL_VALUE_TYPE *r;
2677      const long *buf;
2678 {
2679   unsigned long image = buf[0] & 0xffffffff;
2680   bool sign = (image >> 31) & 1;
2681   int exp = (image >> 23) & 0xff;
2682
2683   memset (r, 0, sizeof (*r));
2684   image <<= HOST_BITS_PER_LONG - 24;
2685   image &= ~SIG_MSB;
2686
2687   if (exp == 0)
2688     {
2689       if (image && fmt->has_denorm)
2690         {
2691           r->class = rvc_normal;
2692           r->sign = sign;
2693           r->exp = -126;
2694           r->sig[SIGSZ-1] = image << 1;
2695           normalize (r);
2696         }
2697       else if (fmt->has_signed_zero)
2698         r->sign = sign;
2699     }
2700   else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2701     {
2702       if (image)
2703         {
2704           r->class = rvc_nan;
2705           r->sign = sign;
2706           if (!fmt->qnan_msb_set)
2707             image ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2708           r->sig[SIGSZ-1] = image;
2709         }
2710       else
2711         {
2712           r->class = rvc_inf;
2713           r->sign = sign;
2714         }
2715     }
2716   else
2717     {
2718       r->class = rvc_normal;
2719       r->sign = sign;
2720       r->exp = exp - 127 + 1;
2721       r->sig[SIGSZ-1] = image | SIG_MSB;
2722     }
2723 }
2724
2725 const struct real_format ieee_single_format = 
2726   {
2727     encode_ieee_single,
2728     decode_ieee_single,
2729     2,
2730     1,
2731     24,
2732     -125,
2733     128,
2734     31,
2735     true,
2736     true,
2737     true,
2738     true,
2739     true
2740   };
2741
2742 \f
2743 /* IEEE double-precision format.  */
2744
2745 static void encode_ieee_double PARAMS ((const struct real_format *fmt,
2746                                         long *, const REAL_VALUE_TYPE *));
2747 static void decode_ieee_double PARAMS ((const struct real_format *,
2748                                         REAL_VALUE_TYPE *, const long *));
2749
2750 static void
2751 encode_ieee_double (fmt, buf, r)
2752      const struct real_format *fmt;
2753      long *buf;
2754      const REAL_VALUE_TYPE *r;
2755 {
2756   unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
2757   bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2758
2759   image_hi = r->sign << 31;
2760   image_lo = 0;
2761
2762   if (HOST_BITS_PER_LONG == 64)
2763     {
2764       sig_hi = r->sig[SIGSZ-1];
2765       sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
2766       sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
2767     }
2768   else
2769     {
2770       sig_hi = r->sig[SIGSZ-1];
2771       sig_lo = r->sig[SIGSZ-2];
2772       sig_lo = (sig_hi << 21) | (sig_lo >> 11);
2773       sig_hi = (sig_hi >> 11) & 0xfffff;
2774     }
2775
2776   switch (r->class)
2777     {
2778     case rvc_zero:
2779       break;
2780
2781     case rvc_inf:
2782       if (fmt->has_inf)
2783         image_hi |= 2047 << 20;
2784       else
2785         {
2786           image_hi |= 0x7fffffff;
2787           image_lo = 0xffffffff;
2788         }
2789       break;
2790
2791     case rvc_nan:
2792       if (fmt->has_nans)
2793         {
2794           image_hi |= 2047 << 20;
2795           image_hi |= sig_hi;
2796           if (!fmt->qnan_msb_set)
2797             image_hi ^= 1 << 19 | 1 << 18;
2798           image_lo = sig_lo;
2799         }
2800       else
2801         {
2802           image_hi |= 0x7fffffff;
2803           image_lo = 0xffffffff;
2804         }
2805       break;
2806
2807     case rvc_normal:
2808       /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2809          whereas the intermediate representation is 0.F x 2**exp.
2810          Which means we're off by one.  */
2811       if (denormal)
2812         exp = 0;
2813       else
2814         exp = r->exp + 1023 - 1;
2815       image_hi |= exp << 20;
2816       image_hi |= sig_hi;
2817       image_lo = sig_lo;
2818       break;
2819
2820     default:
2821       abort ();
2822     }
2823
2824   if (FLOAT_WORDS_BIG_ENDIAN)
2825     buf[0] = image_hi, buf[1] = image_lo;
2826   else
2827     buf[0] = image_lo, buf[1] = image_hi;
2828 }
2829
2830 static void
2831 decode_ieee_double (fmt, r, buf)
2832      const struct real_format *fmt;
2833      REAL_VALUE_TYPE *r;
2834      const long *buf;
2835 {
2836   unsigned long image_hi, image_lo;
2837   bool sign;
2838   int exp;
2839
2840   if (FLOAT_WORDS_BIG_ENDIAN)
2841     image_hi = buf[0], image_lo = buf[1];
2842   else
2843     image_lo = buf[0], image_hi = buf[1];
2844   image_lo &= 0xffffffff;
2845   image_hi &= 0xffffffff;
2846
2847   sign = (image_hi >> 31) & 1;
2848   exp = (image_hi >> 20) & 0x7ff;
2849
2850   memset (r, 0, sizeof (*r));
2851
2852   image_hi <<= 32 - 21;
2853   image_hi |= image_lo >> 21;
2854   image_hi &= 0x7fffffff;
2855   image_lo <<= 32 - 21;
2856
2857   if (exp == 0)
2858     {
2859       if ((image_hi || image_lo) && fmt->has_denorm)
2860         {
2861           r->class = rvc_normal;
2862           r->sign = sign;
2863           r->exp = -1022;
2864           if (HOST_BITS_PER_LONG == 32)
2865             {
2866               image_hi = (image_hi << 1) | (image_lo >> 31);
2867               image_lo <<= 1;
2868               r->sig[SIGSZ-1] = image_hi;
2869               r->sig[SIGSZ-2] = image_lo;
2870             }
2871           else
2872             {
2873               image_hi = (image_hi << 31 << 2) | (image_lo << 1);
2874               r->sig[SIGSZ-1] = image_hi;
2875             }
2876           normalize (r);
2877         }
2878       else if (fmt->has_signed_zero)
2879         r->sign = sign;
2880     }
2881   else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
2882     {
2883       if (image_hi || image_lo)
2884         {
2885           r->class = rvc_nan;
2886           r->sign = sign;
2887           if (HOST_BITS_PER_LONG == 32)
2888             {
2889               r->sig[SIGSZ-1] = image_hi;
2890               r->sig[SIGSZ-2] = image_lo;
2891             }
2892           else
2893             r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
2894
2895           if (!fmt->qnan_msb_set)
2896             r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2897         }
2898       else
2899         {
2900           r->class = rvc_inf;
2901           r->sign = sign;
2902         }
2903     }
2904   else
2905     {
2906       r->class = rvc_normal;
2907       r->sign = sign;
2908       r->exp = exp - 1023 + 1;
2909       if (HOST_BITS_PER_LONG == 32)
2910         {
2911           r->sig[SIGSZ-1] = image_hi | SIG_MSB;
2912           r->sig[SIGSZ-2] = image_lo;
2913         }
2914       else
2915         r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
2916     }
2917 }
2918
2919 const struct real_format ieee_double_format = 
2920   {
2921     encode_ieee_double,
2922     decode_ieee_double,
2923     2,
2924     1,
2925     53,
2926     -1021,
2927     1024,
2928     63,
2929     true,
2930     true,
2931     true,
2932     true,
2933     true
2934   };
2935
2936 \f
2937 /* IEEE extended double precision format.  This comes in three
2938    flavours: Intel's as a 12 byte image, Intel's as a 16 byte image,
2939    and Motorola's.  */
2940
2941 static void encode_ieee_extended PARAMS ((const struct real_format *fmt,
2942                                           long *, const REAL_VALUE_TYPE *));
2943 static void decode_ieee_extended PARAMS ((const struct real_format *,
2944                                           REAL_VALUE_TYPE *, const long *));
2945
2946 static void encode_ieee_extended_128 PARAMS ((const struct real_format *fmt,
2947                                               long *,
2948                                               const REAL_VALUE_TYPE *));
2949 static void decode_ieee_extended_128 PARAMS ((const struct real_format *,
2950                                               REAL_VALUE_TYPE *,
2951                                               const long *));
2952
2953 static void
2954 encode_ieee_extended (fmt, buf, r)
2955      const struct real_format *fmt;
2956      long *buf;
2957      const REAL_VALUE_TYPE *r;
2958 {
2959   unsigned long image_hi, sig_hi, sig_lo;
2960   bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2961
2962   image_hi = r->sign << 15;
2963   sig_hi = sig_lo = 0;
2964
2965   switch (r->class)
2966     {
2967     case rvc_zero:
2968       break;
2969
2970     case rvc_inf:
2971       if (fmt->has_inf)
2972         {
2973           image_hi |= 32767;
2974
2975           /* Intel requires the explicit integer bit to be set, otherwise
2976              it considers the value a "pseudo-infinity".  Motorola docs
2977              say it doesn't care.  */
2978           sig_hi = 0x80000000;
2979         }
2980       else
2981         {
2982           image_hi |= 32767;
2983           sig_lo = sig_hi = 0xffffffff;
2984         }
2985       break;
2986
2987     case rvc_nan:
2988       if (fmt->has_nans)
2989         {
2990           image_hi |= 32767;
2991           if (HOST_BITS_PER_LONG == 32)
2992             {
2993               sig_hi = r->sig[SIGSZ-1];
2994               sig_lo = r->sig[SIGSZ-2];
2995             }
2996           else
2997             {
2998               sig_lo = r->sig[SIGSZ-1];
2999               sig_hi = sig_lo >> 31 >> 1;
3000               sig_lo &= 0xffffffff;
3001             }
3002           if (!fmt->qnan_msb_set)
3003             sig_hi ^= 1 << 30 | 1 << 29;
3004
3005           /* Intel requires the explicit integer bit to be set, otherwise
3006              it considers the value a "pseudo-nan".  Motorola docs say it
3007              doesn't care.  */
3008           sig_hi |= 0x80000000;
3009         }
3010       else
3011         {
3012           image_hi |= 32767;
3013           sig_lo = sig_hi = 0xffffffff;
3014         }
3015       break;
3016
3017     case rvc_normal:
3018       {
3019         int exp = r->exp;
3020
3021         /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3022            whereas the intermediate representation is 0.F x 2**exp.
3023            Which means we're off by one. 
3024
3025            Except for Motorola, which consider exp=0 and explicit
3026            integer bit set to continue to be normalized.  In theory
3027            this discrepancy has been taken care of by the difference
3028            in fmt->emin in round_for_format.  */
3029
3030         if (denormal)
3031           exp = 0;
3032         else
3033           {
3034             exp += 16383 - 1;
3035             if (exp < 0)
3036               abort ();
3037           }
3038         image_hi |= exp;
3039
3040         if (HOST_BITS_PER_LONG == 32)
3041           {
3042             sig_hi = r->sig[SIGSZ-1];
3043             sig_lo = r->sig[SIGSZ-2];
3044           }
3045         else
3046           {
3047             sig_lo = r->sig[SIGSZ-1];
3048             sig_hi = sig_lo >> 31 >> 1;
3049             sig_lo &= 0xffffffff;
3050           }
3051       }
3052       break;
3053
3054     default:
3055       abort ();
3056     }
3057
3058   if (FLOAT_WORDS_BIG_ENDIAN)
3059     buf[0] = image_hi << 16, buf[1] = sig_hi, buf[2] = sig_lo;
3060   else
3061     buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3062 }
3063
3064 static void
3065 encode_ieee_extended_128 (fmt, buf, r)
3066      const struct real_format *fmt;
3067      long *buf;
3068      const REAL_VALUE_TYPE *r;
3069 {
3070   buf[3 * !FLOAT_WORDS_BIG_ENDIAN] = 0;
3071   encode_ieee_extended (fmt, buf+!!FLOAT_WORDS_BIG_ENDIAN, r);
3072 }
3073
3074 static void
3075 decode_ieee_extended (fmt, r, buf)
3076      const struct real_format *fmt;
3077      REAL_VALUE_TYPE *r;
3078      const long *buf;
3079 {
3080   unsigned long image_hi, sig_hi, sig_lo;
3081   bool sign;
3082   int exp;
3083
3084   if (FLOAT_WORDS_BIG_ENDIAN)
3085     image_hi = buf[0] >> 16, sig_hi = buf[1], sig_lo = buf[2];
3086   else
3087     sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3088   sig_lo &= 0xffffffff;
3089   sig_hi &= 0xffffffff;
3090   image_hi &= 0xffffffff;
3091
3092   sign = (image_hi >> 15) & 1;
3093   exp = image_hi & 0x7fff;
3094
3095   memset (r, 0, sizeof (*r));
3096
3097   if (exp == 0)
3098     {
3099       if ((sig_hi || sig_lo) && fmt->has_denorm)
3100         {
3101           r->class = rvc_normal;
3102           r->sign = sign;
3103
3104           /* When the IEEE format contains a hidden bit, we know that
3105              it's zero at this point, and so shift up the significand
3106              and decrease the exponent to match.  In this case, Motorola
3107              defines the explicit integer bit to be valid, so we don't
3108              know whether the msb is set or not.  */
3109           r->exp = fmt->emin;
3110           if (HOST_BITS_PER_LONG == 32)
3111             {
3112               r->sig[SIGSZ-1] = sig_hi;
3113               r->sig[SIGSZ-2] = sig_lo;
3114             }
3115           else
3116             r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3117
3118           normalize (r);
3119         }
3120       else if (fmt->has_signed_zero)
3121         r->sign = sign;
3122     }
3123   else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3124     {
3125       /* See above re "pseudo-infinities" and "pseudo-nans".
3126          Short summary is that the MSB will likely always be
3127          set, and that we don't care about it.  */
3128       sig_hi &= 0x7fffffff;
3129
3130       if (sig_hi || sig_lo)
3131         {
3132           r->class = rvc_nan;
3133           r->sign = sign;
3134           if (HOST_BITS_PER_LONG == 32)
3135             {
3136               r->sig[SIGSZ-1] = sig_hi;
3137               r->sig[SIGSZ-2] = sig_lo;
3138             }
3139           else
3140             r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3141
3142           if (!fmt->qnan_msb_set)
3143             r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
3144         }
3145       else
3146         {
3147           r->class = rvc_inf;
3148           r->sign = sign;
3149         }
3150     }
3151   else
3152     {
3153       r->class = rvc_normal;
3154       r->sign = sign;
3155       r->exp = exp - 16383 + 1;
3156       if (HOST_BITS_PER_LONG == 32)
3157         {
3158           r->sig[SIGSZ-1] = sig_hi;
3159           r->sig[SIGSZ-2] = sig_lo;
3160         }
3161       else
3162         r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3163     }
3164 }
3165
3166 static void
3167 decode_ieee_extended_128 (fmt, r, buf)
3168      const struct real_format *fmt;
3169      REAL_VALUE_TYPE *r;
3170      const long *buf;
3171 {
3172   decode_ieee_extended (fmt, r, buf+!!FLOAT_WORDS_BIG_ENDIAN);
3173 }
3174
3175 const struct real_format ieee_extended_motorola_format = 
3176   {
3177     encode_ieee_extended,
3178     decode_ieee_extended,
3179     2,
3180     1,
3181     64,
3182     -16382,
3183     16384,
3184     95,
3185     true,
3186     true,
3187     true,
3188     true,
3189     true
3190   };
3191
3192 const struct real_format ieee_extended_intel_96_format = 
3193   {
3194     encode_ieee_extended,
3195     decode_ieee_extended,
3196     2,
3197     1,
3198     64,
3199     -16381,
3200     16384,
3201     79,
3202     true,
3203     true,
3204     true,
3205     true,
3206     true
3207   };
3208
3209 const struct real_format ieee_extended_intel_128_format = 
3210   {
3211     encode_ieee_extended_128,
3212     decode_ieee_extended_128,
3213     2,
3214     1,
3215     64,
3216     -16381,
3217     16384,
3218     79,
3219     true,
3220     true,
3221     true,
3222     true,
3223     true
3224   };
3225
3226 \f
3227 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3228    numbers whose sum is equal to the extended precision value.  The number
3229    with greater magnitude is first.  This format has the same magnitude
3230    range as an IEEE double precision value, but effectively 106 bits of
3231    significand precision.  Infinity and NaN are represented by their IEEE
3232    double precision value stored in the first number, the second number is
3233    ignored.  Zeroes, Infinities, and NaNs are set in both doubles
3234    due to precedent.  */
3235
3236 static void encode_ibm_extended PARAMS ((const struct real_format *fmt,
3237                                          long *, const REAL_VALUE_TYPE *));
3238 static void decode_ibm_extended PARAMS ((const struct real_format *,
3239                                          REAL_VALUE_TYPE *, const long *));
3240
3241 static void
3242 encode_ibm_extended (fmt, buf, r)
3243      const struct real_format *fmt ATTRIBUTE_UNUSED;
3244      long *buf;
3245      const REAL_VALUE_TYPE *r;
3246 {
3247   REAL_VALUE_TYPE u, v;
3248
3249   switch (r->class)
3250     {
3251     case rvc_zero:
3252       /* Both doubles have sign bit set.  */
3253       buf[0] = FLOAT_WORDS_BIG_ENDIAN ? r->sign << 31 : 0;
3254       buf[1] = FLOAT_WORDS_BIG_ENDIAN ? 0 : r->sign << 31;
3255       buf[2] = buf[0];
3256       buf[3] = buf[1];
3257       break;
3258
3259     case rvc_inf:
3260     case rvc_nan:
3261       /* Both doubles set to Inf / NaN.  */
3262       encode_ieee_double (&ieee_double_format, &buf[0], r);
3263       buf[2] = buf[0];
3264       buf[3] = buf[1];
3265       return;
3266       
3267     case rvc_normal:
3268       /* u = IEEE double precision portion of significand.  */
3269       u = *r;
3270       clear_significand_below (&u, SIGNIFICAND_BITS - 53);
3271
3272       normalize (&u);
3273       /* If the upper double is zero, we have a denormal double, so
3274          move it to the first double and leave the second as zero.  */
3275       if (u.class == rvc_zero)
3276         {
3277           v = u;
3278           u = *r;
3279           normalize (&u);
3280         }
3281       else
3282         {
3283           /* v = remainder containing additional 53 bits of significand.  */
3284           do_add (&v, r, &u, 1);
3285           round_for_format (&ieee_double_format, &v);
3286         }
3287
3288       round_for_format (&ieee_double_format, &u);
3289
3290       encode_ieee_double (&ieee_double_format, &buf[0], &u);
3291       encode_ieee_double (&ieee_double_format, &buf[2], &v);
3292       break;
3293
3294     default:
3295       abort ();
3296     }
3297 }
3298
3299 static void
3300 decode_ibm_extended (fmt, r, buf)
3301      const struct real_format *fmt ATTRIBUTE_UNUSED;
3302      REAL_VALUE_TYPE *r;
3303      const long *buf;
3304 {
3305   REAL_VALUE_TYPE u, v;
3306
3307   decode_ieee_double (&ieee_double_format, &u, &buf[0]);
3308
3309   if (u.class != rvc_zero && u.class != rvc_inf && u.class != rvc_nan)
3310     {
3311       decode_ieee_double (&ieee_double_format, &v, &buf[2]);
3312       do_add (r, &u, &v, 0);
3313     }
3314   else
3315     *r = u;
3316 }
3317
3318 const struct real_format ibm_extended_format = 
3319   {
3320     encode_ibm_extended,
3321     decode_ibm_extended,
3322     2,
3323     1,
3324     53 + 53,
3325     -1021 + 53,
3326     1024,
3327     -1,
3328     true,
3329     true,
3330     true,
3331     true,
3332     true
3333   };
3334
3335 \f
3336 /* IEEE quad precision format.  */
3337
3338 static void encode_ieee_quad PARAMS ((const struct real_format *fmt,
3339                                       long *, const REAL_VALUE_TYPE *));
3340 static void decode_ieee_quad PARAMS ((const struct real_format *,
3341                                       REAL_VALUE_TYPE *, const long *));
3342
3343 static void
3344 encode_ieee_quad (fmt, buf, r)
3345      const struct real_format *fmt;
3346      long *buf;
3347      const REAL_VALUE_TYPE *r;
3348 {
3349   unsigned long image3, image2, image1, image0, exp;
3350   bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3351   REAL_VALUE_TYPE u;
3352
3353   image3 = r->sign << 31;
3354   image2 = 0;
3355   image1 = 0;
3356   image0 = 0;
3357
3358   rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3359
3360   switch (r->class)
3361     {
3362     case rvc_zero:
3363       break;
3364
3365     case rvc_inf:
3366       if (fmt->has_inf)
3367         image3 |= 32767 << 16;
3368       else
3369         {
3370           image3 |= 0x7fffffff;
3371           image2 = 0xffffffff;
3372           image1 = 0xffffffff;
3373           image0 = 0xffffffff;
3374         }
3375       break;
3376
3377     case rvc_nan:
3378       if (fmt->has_nans)
3379         {
3380           image3 |= 32767 << 16;
3381
3382           if (HOST_BITS_PER_LONG == 32)
3383             {
3384               image0 = u.sig[0];
3385               image1 = u.sig[1];
3386               image2 = u.sig[2];
3387               image3 |= u.sig[3] & 0xffff;
3388             }
3389           else
3390             {
3391               image0 = u.sig[0];
3392               image1 = image0 >> 31 >> 1;
3393               image2 = u.sig[1];
3394               image3 |= (image2 >> 31 >> 1) & 0xffff;
3395               image0 &= 0xffffffff;
3396               image2 &= 0xffffffff;
3397             }
3398
3399           if (!fmt->qnan_msb_set)
3400             image3 ^= 1 << 15 | 1 << 14;
3401         }
3402       else
3403         {
3404           image3 |= 0x7fffffff;
3405           image2 = 0xffffffff;
3406           image1 = 0xffffffff;
3407           image0 = 0xffffffff;
3408         }
3409       break;
3410
3411     case rvc_normal:
3412       /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3413          whereas the intermediate representation is 0.F x 2**exp.
3414          Which means we're off by one.  */
3415       if (denormal)
3416         exp = 0;
3417       else
3418         exp = r->exp + 16383 - 1;
3419       image3 |= exp << 16;
3420
3421       if (HOST_BITS_PER_LONG == 32)
3422         {
3423           image0 = u.sig[0];
3424           image1 = u.sig[1];
3425           image2 = u.sig[2];
3426           image3 |= u.sig[3] & 0xffff;
3427         }
3428       else
3429         {
3430           image0 = u.sig[0];
3431           image1 = image0 >> 31 >> 1;
3432           image2 = u.sig[1];
3433           image3 |= (image2 >> 31 >> 1) & 0xffff;
3434           image0 &= 0xffffffff;
3435           image2 &= 0xffffffff;
3436         }
3437       break;
3438
3439     default:
3440       abort ();
3441     }
3442
3443   if (FLOAT_WORDS_BIG_ENDIAN)
3444     {
3445       buf[0] = image3;
3446       buf[1] = image2;
3447       buf[2] = image1;
3448       buf[3] = image0;
3449     }
3450   else
3451     {
3452       buf[0] = image0;
3453       buf[1] = image1;
3454       buf[2] = image2;
3455       buf[3] = image3;
3456     }
3457 }
3458
3459 static void
3460 decode_ieee_quad (fmt, r, buf)
3461      const struct real_format *fmt;
3462      REAL_VALUE_TYPE *r;
3463      const long *buf;
3464 {
3465   unsigned long image3, image2, image1, image0;
3466   bool sign;
3467   int exp;
3468
3469   if (FLOAT_WORDS_BIG_ENDIAN)
3470     {
3471       image3 = buf[0];
3472       image2 = buf[1];
3473       image1 = buf[2];
3474       image0 = buf[3];
3475     }
3476   else
3477     {
3478       image0 = buf[0];
3479       image1 = buf[1];
3480       image2 = buf[2];
3481       image3 = buf[3];
3482     }
3483   image0 &= 0xffffffff;
3484   image1 &= 0xffffffff;
3485   image2 &= 0xffffffff;
3486
3487   sign = (image3 >> 31) & 1;
3488   exp = (image3 >> 16) & 0x7fff;
3489   image3 &= 0xffff;
3490
3491   memset (r, 0, sizeof (*r));
3492
3493   if (exp == 0)
3494     {
3495       if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3496         {
3497           r->class = rvc_normal;
3498           r->sign = sign;
3499
3500           r->exp = -16382 + (SIGNIFICAND_BITS - 112);
3501           if (HOST_BITS_PER_LONG == 32)
3502             {
3503               r->sig[0] = image0;
3504               r->sig[1] = image1;
3505               r->sig[2] = image2;
3506               r->sig[3] = image3;
3507             }
3508           else
3509             {
3510               r->sig[0] = (image1 << 31 << 1) | image0;
3511               r->sig[1] = (image3 << 31 << 1) | image2;
3512             }
3513
3514           normalize (r);
3515         }
3516       else if (fmt->has_signed_zero)
3517         r->sign = sign;
3518     }
3519   else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3520     {
3521       if (image3 | image2 | image1 | image0)
3522         {
3523           r->class = rvc_nan;
3524           r->sign = sign;
3525
3526           if (HOST_BITS_PER_LONG == 32)
3527             {
3528               r->sig[0] = image0;
3529               r->sig[1] = image1;
3530               r->sig[2] = image2;
3531               r->sig[3] = image3;
3532             }
3533           else
3534             {
3535               r->sig[0] = (image1 << 31 << 1) | image0;
3536               r->sig[1] = (image3 << 31 << 1) | image2;
3537             }
3538           lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3539
3540           if (!fmt->qnan_msb_set)
3541             r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
3542         }
3543       else
3544         {
3545           r->class = rvc_inf;
3546           r->sign = sign;
3547         }
3548     }
3549   else
3550     {
3551       r->class = rvc_normal;
3552       r->sign = sign;
3553       r->exp = exp - 16383 + 1;
3554
3555       if (HOST_BITS_PER_LONG == 32)
3556         {
3557           r->sig[0] = image0;
3558           r->sig[1] = image1;
3559           r->sig[2] = image2;
3560           r->sig[3] = image3;
3561         }
3562       else
3563         {
3564           r->sig[0] = (image1 << 31 << 1) | image0;
3565           r->sig[1] = (image3 << 31 << 1) | image2;
3566         }
3567       lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3568       r->sig[SIGSZ-1] |= SIG_MSB;
3569     }
3570 }
3571
3572 const struct real_format ieee_quad_format = 
3573   {
3574     encode_ieee_quad,
3575     decode_ieee_quad,
3576     2,
3577     1,
3578     113,
3579     -16381,
3580     16384,
3581     127,
3582     true,
3583     true,
3584     true,
3585     true,
3586     true
3587   };
3588 \f
3589 /* Descriptions of VAX floating point formats can be found beginning at
3590
3591    http://www.openvms.compaq.com:8000/73final/4515/4515pro_013.html#f_floating_point_format
3592
3593    The thing to remember is that they're almost IEEE, except for word
3594    order, exponent bias, and the lack of infinities, nans, and denormals.
3595
3596    We don't implement the H_floating format here, simply because neither
3597    the VAX or Alpha ports use it.  */
3598    
3599 static void encode_vax_f PARAMS ((const struct real_format *fmt,
3600                                   long *, const REAL_VALUE_TYPE *));
3601 static void decode_vax_f PARAMS ((const struct real_format *,
3602                                   REAL_VALUE_TYPE *, const long *));
3603 static void encode_vax_d PARAMS ((const struct real_format *fmt,
3604                                   long *, const REAL_VALUE_TYPE *));
3605 static void decode_vax_d PARAMS ((const struct real_format *,
3606                                   REAL_VALUE_TYPE *, const long *));
3607 static void encode_vax_g PARAMS ((const struct real_format *fmt,
3608                                   long *, const REAL_VALUE_TYPE *));
3609 static void decode_vax_g PARAMS ((const struct real_format *,
3610                                   REAL_VALUE_TYPE *, const long *));
3611
3612 static void
3613 encode_vax_f (fmt, buf, r)
3614      const struct real_format *fmt ATTRIBUTE_UNUSED;
3615      long *buf;
3616      const REAL_VALUE_TYPE *r;
3617 {
3618   unsigned long sign, exp, sig, image;
3619
3620   sign = r->sign << 15;
3621
3622   switch (r->class)
3623     {
3624     case rvc_zero:
3625       image = 0;
3626       break;
3627
3628     case rvc_inf:
3629     case rvc_nan:
3630       image = 0xffff7fff | sign;
3631       break;
3632
3633     case rvc_normal:
3634       sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3635       exp = r->exp + 128;
3636
3637       image = (sig << 16) & 0xffff0000;
3638       image |= sign;
3639       image |= exp << 7;
3640       image |= sig >> 16;
3641       break;
3642
3643     default:
3644       abort ();
3645     }
3646
3647   buf[0] = image;
3648 }
3649
3650 static void
3651 decode_vax_f (fmt, r, buf)
3652      const struct real_format *fmt ATTRIBUTE_UNUSED;
3653      REAL_VALUE_TYPE *r;
3654      const long *buf;
3655 {
3656   unsigned long image = buf[0] & 0xffffffff;
3657   int exp = (image >> 7) & 0xff;
3658
3659   memset (r, 0, sizeof (*r));
3660
3661   if (exp != 0)
3662     {
3663       r->class = rvc_normal;
3664       r->sign = (image >> 15) & 1;
3665       r->exp = exp - 128;
3666
3667       image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
3668       r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3669     }
3670 }
3671
3672 static void
3673 encode_vax_d (fmt, buf, r)
3674      const struct real_format *fmt ATTRIBUTE_UNUSED;
3675      long *buf;
3676      const REAL_VALUE_TYPE *r;
3677 {
3678   unsigned long image0, image1, sign = r->sign << 15;
3679
3680   switch (r->class)
3681     {
3682     case rvc_zero:
3683       image0 = image1 = 0;
3684       break;
3685
3686     case rvc_inf:
3687     case rvc_nan:
3688       image0 = 0xffff7fff | sign;
3689       image1 = 0xffffffff;
3690       break;
3691
3692     case rvc_normal:
3693       /* Extract the significand into straight hi:lo.  */
3694       if (HOST_BITS_PER_LONG == 64)
3695         {
3696           image0 = r->sig[SIGSZ-1];
3697           image1 = (image0 >> (64 - 56)) & 0xffffffff;
3698           image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
3699         }
3700       else
3701         {
3702           image0 = r->sig[SIGSZ-1];
3703           image1 = r->sig[SIGSZ-2];
3704           image1 = (image0 << 24) | (image1 >> 8);
3705           image0 = (image0 >> 8) & 0xffffff;
3706         }
3707
3708       /* Rearrange the half-words of the significand to match the
3709          external format.  */
3710       image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
3711       image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3712
3713       /* Add the sign and exponent.  */
3714       image0 |= sign;
3715       image0 |= (r->exp + 128) << 7;
3716       break;
3717
3718     default:
3719       abort ();
3720     }
3721
3722   if (FLOAT_WORDS_BIG_ENDIAN)
3723     buf[0] = image1, buf[1] = image0;
3724   else
3725     buf[0] = image0, buf[1] = image1;
3726 }
3727
3728 static void
3729 decode_vax_d (fmt, r, buf)
3730      const struct real_format *fmt ATTRIBUTE_UNUSED;
3731      REAL_VALUE_TYPE *r;
3732      const long *buf;
3733 {
3734   unsigned long image0, image1;
3735   int exp;
3736
3737   if (FLOAT_WORDS_BIG_ENDIAN)
3738     image1 = buf[0], image0 = buf[1];
3739   else
3740     image0 = buf[0], image1 = buf[1];
3741   image0 &= 0xffffffff;
3742   image1 &= 0xffffffff;
3743
3744   exp = (image0 >> 7) & 0x7f;
3745
3746   memset (r, 0, sizeof (*r));
3747
3748   if (exp != 0)
3749     {
3750       r->class = rvc_normal;
3751       r->sign = (image0 >> 15) & 1;
3752       r->exp = exp - 128;
3753
3754       /* Rearrange the half-words of the external format into
3755          proper ascending order.  */
3756       image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
3757       image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3758
3759       if (HOST_BITS_PER_LONG == 64)
3760         {
3761           image0 = (image0 << 31 << 1) | image1;
3762           image0 <<= 64 - 56;
3763           image0 |= SIG_MSB;
3764           r->sig[SIGSZ-1] = image0;
3765         }
3766       else
3767         {
3768           r->sig[SIGSZ-1] = image0;
3769           r->sig[SIGSZ-2] = image1;
3770           lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
3771           r->sig[SIGSZ-1] |= SIG_MSB;
3772         }
3773     }
3774 }
3775
3776 static void
3777 encode_vax_g (fmt, buf, r)
3778      const struct real_format *fmt ATTRIBUTE_UNUSED;
3779      long *buf;
3780      const REAL_VALUE_TYPE *r;
3781 {
3782   unsigned long image0, image1, sign = r->sign << 15;
3783
3784   switch (r->class)
3785     {
3786     case rvc_zero:
3787       image0 = image1 = 0;
3788       break;
3789
3790     case rvc_inf:
3791     case rvc_nan:
3792       image0 = 0xffff7fff | sign;
3793       image1 = 0xffffffff;
3794       break;
3795
3796     case rvc_normal:
3797       /* Extract the significand into straight hi:lo.  */
3798       if (HOST_BITS_PER_LONG == 64)
3799         {
3800           image0 = r->sig[SIGSZ-1];
3801           image1 = (image0 >> (64 - 53)) & 0xffffffff;
3802           image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
3803         }
3804       else
3805         {
3806           image0 = r->sig[SIGSZ-1];
3807           image1 = r->sig[SIGSZ-2];
3808           image1 = (image0 << 21) | (image1 >> 11);
3809           image0 = (image0 >> 11) & 0xfffff;
3810         }
3811
3812       /* Rearrange the half-words of the significand to match the
3813          external format.  */
3814       image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
3815       image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3816
3817       /* Add the sign and exponent.  */
3818       image0 |= sign;
3819       image0 |= (r->exp + 1024) << 4;
3820       break;
3821
3822     default:
3823       abort ();
3824     }
3825
3826   if (FLOAT_WORDS_BIG_ENDIAN)
3827     buf[0] = image1, buf[1] = image0;
3828   else
3829     buf[0] = image0, buf[1] = image1;
3830 }
3831
3832 static void
3833 decode_vax_g (fmt, r, buf)
3834      const struct real_format *fmt ATTRIBUTE_UNUSED;
3835      REAL_VALUE_TYPE *r;
3836      const long *buf;
3837 {
3838   unsigned long image0, image1;
3839   int exp;
3840
3841   if (FLOAT_WORDS_BIG_ENDIAN)
3842     image1 = buf[0], image0 = buf[1];
3843   else
3844     image0 = buf[0], image1 = buf[1];
3845   image0 &= 0xffffffff;
3846   image1 &= 0xffffffff;
3847
3848   exp = (image0 >> 4) & 0x7ff;
3849
3850   memset (r, 0, sizeof (*r));
3851
3852   if (exp != 0)
3853     {
3854       r->class = rvc_normal;
3855       r->sign = (image0 >> 15) & 1;
3856       r->exp = exp - 1024;
3857
3858       /* Rearrange the half-words of the external format into
3859          proper ascending order.  */
3860       image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
3861       image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3862
3863       if (HOST_BITS_PER_LONG == 64)
3864         {
3865           image0 = (image0 << 31 << 1) | image1;
3866           image0 <<= 64 - 53;
3867           image0 |= SIG_MSB;
3868           r->sig[SIGSZ-1] = image0;
3869         }
3870       else
3871         {
3872           r->sig[SIGSZ-1] = image0;
3873           r->sig[SIGSZ-2] = image1;
3874           lshift_significand (r, r, 64 - 53);
3875           r->sig[SIGSZ-1] |= SIG_MSB;
3876         }
3877     }
3878 }
3879
3880 const struct real_format vax_f_format = 
3881   {
3882     encode_vax_f,
3883     decode_vax_f,
3884     2,
3885     1,
3886     24,
3887     -127,
3888     127,
3889     15,
3890     false,
3891     false,
3892     false,
3893     false,
3894     false
3895   };
3896
3897 const struct real_format vax_d_format = 
3898   {
3899     encode_vax_d,
3900     decode_vax_d,
3901     2,
3902     1,
3903     56,
3904     -127,
3905     127,
3906     15,
3907     false,
3908     false,
3909     false,
3910     false,
3911     false
3912   };
3913
3914 const struct real_format vax_g_format = 
3915   {
3916     encode_vax_g,
3917     decode_vax_g,
3918     2,
3919     1,
3920     53,
3921     -1023,
3922     1023,
3923     15,
3924     false,
3925     false,
3926     false,
3927     false,
3928     false
3929   };
3930 \f
3931 /* A good reference for these can be found in chapter 9 of
3932    "ESA/390 Principles of Operation", IBM document number SA22-7201-01.
3933    An on-line version can be found here:
3934
3935    http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
3936 */
3937
3938 static void encode_i370_single PARAMS ((const struct real_format *fmt,
3939                                         long *, const REAL_VALUE_TYPE *));
3940 static void decode_i370_single PARAMS ((const struct real_format *,
3941                                         REAL_VALUE_TYPE *, const long *));
3942 static void encode_i370_double PARAMS ((const struct real_format *fmt,
3943                                         long *, const REAL_VALUE_TYPE *));
3944 static void decode_i370_double PARAMS ((const struct real_format *,
3945                                         REAL_VALUE_TYPE *, const long *));
3946
3947 static void
3948 encode_i370_single (fmt, buf, r)
3949      const struct real_format *fmt ATTRIBUTE_UNUSED;
3950      long *buf;
3951      const REAL_VALUE_TYPE *r;
3952 {
3953   unsigned long sign, exp, sig, image;
3954
3955   sign = r->sign << 31;
3956
3957   switch (r->class)
3958     {
3959     case rvc_zero:
3960       image = 0;
3961       break;
3962
3963     case rvc_inf:
3964     case rvc_nan:
3965       image = 0x7fffffff | sign;
3966       break;
3967
3968     case rvc_normal:
3969       sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0xffffff;
3970       exp = ((r->exp / 4) + 64) << 24;
3971       image = sign | exp | sig;
3972       break;
3973
3974     default:
3975       abort ();
3976     }
3977
3978   buf[0] = image;
3979 }
3980
3981 static void
3982 decode_i370_single (fmt, r, buf)
3983      const struct real_format *fmt ATTRIBUTE_UNUSED;
3984      REAL_VALUE_TYPE *r;
3985      const long *buf;
3986 {
3987   unsigned long sign, sig, image = buf[0];
3988   int exp;
3989
3990   sign = (image >> 31) & 1;
3991   exp = (image >> 24) & 0x7f;
3992   sig = image & 0xffffff;
3993
3994   memset (r, 0, sizeof (*r));
3995
3996   if (exp || sig)
3997     {
3998       r->class = rvc_normal;
3999       r->sign = sign;
4000       r->exp = (exp - 64) * 4;
4001       r->sig[SIGSZ-1] = sig << (HOST_BITS_PER_LONG - 24);
4002       normalize (r);
4003     }
4004 }
4005
4006 static void
4007 encode_i370_double (fmt, buf, r)
4008      const struct real_format *fmt ATTRIBUTE_UNUSED;
4009      long *buf;
4010      const REAL_VALUE_TYPE *r;
4011 {
4012   unsigned long sign, exp, image_hi, image_lo;
4013
4014   sign = r->sign << 31;
4015
4016   switch (r->class)
4017     {
4018     case rvc_zero:
4019       image_hi = image_lo = 0;
4020       break;
4021
4022     case rvc_inf:
4023     case rvc_nan:
4024       image_hi = 0x7fffffff | sign;
4025       image_lo = 0xffffffff;
4026       break;
4027
4028     case rvc_normal:
4029       if (HOST_BITS_PER_LONG == 64)
4030         {
4031           image_hi = r->sig[SIGSZ-1];
4032           image_lo = (image_hi >> (64 - 56)) & 0xffffffff;
4033           image_hi = (image_hi >> (64 - 56 + 1) >> 31) & 0xffffff;
4034         }
4035       else
4036         {
4037           image_hi = r->sig[SIGSZ-1];
4038           image_lo = r->sig[SIGSZ-2];
4039           image_lo = (image_lo >> 8) | (image_hi << 24);
4040           image_hi >>= 8;
4041         }
4042
4043       exp = ((r->exp / 4) + 64) << 24;
4044       image_hi |= sign | exp;
4045       break;
4046
4047     default:
4048       abort ();
4049     }
4050
4051   if (FLOAT_WORDS_BIG_ENDIAN)
4052     buf[0] = image_hi, buf[1] = image_lo;
4053   else
4054     buf[0] = image_lo, buf[1] = image_hi;
4055 }
4056
4057 static void
4058 decode_i370_double (fmt, r, buf)
4059      const struct real_format *fmt ATTRIBUTE_UNUSED;
4060      REAL_VALUE_TYPE *r;
4061      const long *buf;
4062 {
4063   unsigned long sign, image_hi, image_lo;
4064   int exp;
4065
4066   if (FLOAT_WORDS_BIG_ENDIAN)
4067     image_hi = buf[0], image_lo = buf[1];
4068   else
4069     image_lo = buf[0], image_hi = buf[1];
4070
4071   sign = (image_hi >> 31) & 1;
4072   exp = (image_hi >> 24) & 0x7f;
4073   image_hi &= 0xffffff;
4074   image_lo &= 0xffffffff;
4075
4076   memset (r, 0, sizeof (*r));
4077
4078   if (exp || image_hi || image_lo)
4079     {
4080       r->class = rvc_normal;
4081       r->sign = sign;
4082       r->exp = (exp - 64) * 4 + (SIGNIFICAND_BITS - 56);
4083
4084       if (HOST_BITS_PER_LONG == 32)
4085         {
4086           r->sig[0] = image_lo;
4087           r->sig[1] = image_hi;
4088         }
4089       else
4090         r->sig[0] = image_lo | (image_hi << 31 << 1);
4091
4092       normalize (r);
4093     }
4094 }
4095
4096 const struct real_format i370_single_format =
4097   {
4098     encode_i370_single,
4099     decode_i370_single,
4100     16,
4101     4,
4102     6,
4103     -64,
4104     63,
4105     31,
4106     false,
4107     false,
4108     false, /* ??? The encoding does allow for "unnormals".  */
4109     false, /* ??? The encoding does allow for "unnormals".  */
4110     false
4111   };
4112
4113 const struct real_format i370_double_format =
4114   {
4115     encode_i370_double,
4116     decode_i370_double,
4117     16,
4118     4,
4119     14,
4120     -64,
4121     63,
4122     63,
4123     false,
4124     false,
4125     false, /* ??? The encoding does allow for "unnormals".  */
4126     false, /* ??? The encoding does allow for "unnormals".  */
4127     false
4128   };
4129 \f
4130 /* The "twos-complement" c4x format is officially defined as
4131
4132         x = s(~s).f * 2**e
4133
4134    This is rather misleading.  One must remember that F is signed.
4135    A better description would be
4136
4137         x = -1**s * ((s + 1 + .f) * 2**e
4138
4139    So if we have a (4 bit) fraction of .1000 with a sign bit of 1,
4140    that's -1 * (1+1+(-.5)) == -1.5.  I think.
4141
4142    The constructions here are taken from Tables 5-1 and 5-2 of the
4143    TMS320C4x User's Guide wherein step-by-step instructions for
4144    conversion from IEEE are presented.  That's close enough to our
4145    internal representation so as to make things easy.
4146
4147    See http://www-s.ti.com/sc/psheets/spru063c/spru063c.pdf  */
4148
4149 static void encode_c4x_single PARAMS ((const struct real_format *fmt,
4150                                        long *, const REAL_VALUE_TYPE *));
4151 static void decode_c4x_single PARAMS ((const struct real_format *,
4152                                        REAL_VALUE_TYPE *, const long *));
4153 static void encode_c4x_extended PARAMS ((const struct real_format *fmt,
4154                                          long *, const REAL_VALUE_TYPE *));
4155 static void decode_c4x_extended PARAMS ((const struct real_format *,
4156                                          REAL_VALUE_TYPE *, const long *));
4157
4158 static void
4159 encode_c4x_single (fmt, buf, r)
4160      const struct real_format *fmt ATTRIBUTE_UNUSED;
4161      long *buf;
4162      const REAL_VALUE_TYPE *r;
4163 {
4164   unsigned long image, exp, sig;
4165   
4166   switch (r->class)
4167     {
4168     case rvc_zero:
4169       exp = -128;
4170       sig = 0;
4171       break;
4172
4173     case rvc_inf:
4174     case rvc_nan:
4175       exp = 127;
4176       sig = 0x800000 - r->sign;
4177       break;
4178
4179     case rvc_normal:
4180       exp = r->exp - 1;
4181       sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4182       if (r->sign)
4183         {
4184           if (sig)
4185             sig = -sig;
4186           else
4187             exp--;
4188           sig |= 0x800000;
4189         }
4190       break;
4191
4192     default:
4193       abort ();
4194     }
4195
4196   image = ((exp & 0xff) << 24) | (sig & 0xffffff);
4197   buf[0] = image;
4198 }
4199
4200 static void
4201 decode_c4x_single (fmt, r, buf)
4202      const struct real_format *fmt ATTRIBUTE_UNUSED;
4203      REAL_VALUE_TYPE *r;
4204      const long *buf;
4205 {
4206   unsigned long image = buf[0];
4207   unsigned long sig;
4208   int exp, sf;
4209
4210   exp = (((image >> 24) & 0xff) ^ 0x80) - 0x80;
4211   sf = ((image & 0xffffff) ^ 0x800000) - 0x800000;
4212
4213   memset (r, 0, sizeof (*r));
4214
4215   if (exp != -128)
4216     {
4217       r->class = rvc_normal;
4218
4219       sig = sf & 0x7fffff;
4220       if (sf < 0)
4221         {
4222           r->sign = 1;
4223           if (sig)
4224             sig = -sig;
4225           else
4226             exp++;
4227         }
4228       sig = (sig << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4229
4230       r->exp = exp + 1;
4231       r->sig[SIGSZ-1] = sig;
4232     }
4233 }
4234
4235 static void
4236 encode_c4x_extended (fmt, buf, r)
4237      const struct real_format *fmt ATTRIBUTE_UNUSED;
4238      long *buf;
4239      const REAL_VALUE_TYPE *r;
4240 {
4241   unsigned long exp, sig;
4242   
4243   switch (r->class)
4244     {
4245     case rvc_zero:
4246       exp = -128;
4247       sig = 0;
4248       break;
4249
4250     case rvc_inf:
4251     case rvc_nan:
4252       exp = 127;
4253       sig = 0x80000000 - r->sign;
4254       break;
4255
4256     case rvc_normal:
4257       exp = r->exp - 1;
4258
4259       sig = r->sig[SIGSZ-1];
4260       if (HOST_BITS_PER_LONG == 64)
4261         sig = sig >> 1 >> 31;
4262       sig &= 0x7fffffff;
4263
4264       if (r->sign)
4265         {
4266           if (sig)
4267             sig = -sig;
4268           else
4269             exp--;
4270           sig |= 0x80000000;
4271         }
4272       break;
4273
4274     default:
4275       abort ();
4276     }
4277
4278   exp = (exp & 0xff) << 24;
4279   sig &= 0xffffffff;
4280
4281   if (FLOAT_WORDS_BIG_ENDIAN)
4282     buf[0] = exp, buf[1] = sig;
4283   else
4284     buf[0] = sig, buf[0] = exp;
4285 }
4286
4287 static void
4288 decode_c4x_extended (fmt, r, buf)
4289      const struct real_format *fmt ATTRIBUTE_UNUSED;
4290      REAL_VALUE_TYPE *r;
4291      const long *buf;
4292 {
4293   unsigned long sig;
4294   int exp, sf;
4295
4296   if (FLOAT_WORDS_BIG_ENDIAN)
4297     exp = buf[0], sf = buf[1];
4298   else
4299     sf = buf[0], exp = buf[1];
4300
4301   exp = (((exp >> 24) & 0xff) & 0x80) - 0x80;
4302   sf = ((sf & 0xffffffff) ^ 0x80000000) - 0x80000000;
4303
4304   memset (r, 0, sizeof (*r));
4305
4306   if (exp != -128)
4307     {
4308       r->class = rvc_normal;
4309
4310       sig = sf & 0x7fffffff;
4311       if (sf < 0)
4312         {
4313           r->sign = 1;
4314           if (sig)
4315             sig = -sig;
4316           else
4317             exp++;
4318         }
4319       if (HOST_BITS_PER_LONG == 64)
4320         sig = sig << 1 << 31;
4321       sig |= SIG_MSB;
4322
4323       r->exp = exp + 1;
4324       r->sig[SIGSZ-1] = sig;
4325     }
4326 }
4327
4328 const struct real_format c4x_single_format = 
4329   {
4330     encode_c4x_single,
4331     decode_c4x_single,
4332     2,
4333     1,
4334     24,
4335     -126,
4336     128,
4337     -1,
4338     false,
4339     false,
4340     false,
4341     false,
4342     false
4343   };
4344
4345 const struct real_format c4x_extended_format = 
4346   {
4347     encode_c4x_extended,
4348     decode_c4x_extended,
4349     2,
4350     1,
4351     32,
4352     -126,
4353     128,
4354     -1,
4355     false,
4356     false,
4357     false,
4358     false,
4359     false
4360   };
4361
4362 \f
4363 /* A synthetic "format" for internal arithmetic.  It's the size of the
4364    internal significand minus the two bits needed for proper rounding.
4365    The encode and decode routines exist only to satisfy our paranoia
4366    harness.  */
4367
4368 static void encode_internal PARAMS ((const struct real_format *fmt,
4369                                      long *, const REAL_VALUE_TYPE *));
4370 static void decode_internal PARAMS ((const struct real_format *,
4371                                      REAL_VALUE_TYPE *, const long *));
4372
4373 static void
4374 encode_internal (fmt, buf, r)
4375      const struct real_format *fmt ATTRIBUTE_UNUSED;
4376      long *buf;
4377      const REAL_VALUE_TYPE *r;
4378 {
4379   memcpy (buf, r, sizeof (*r));
4380 }
4381
4382 static void
4383 decode_internal (fmt, r, buf)
4384      const struct real_format *fmt ATTRIBUTE_UNUSED;
4385      REAL_VALUE_TYPE *r;
4386      const long *buf;
4387 {
4388   memcpy (r, buf, sizeof (*r));
4389 }
4390
4391 const struct real_format real_internal_format = 
4392   {
4393     encode_internal,
4394     decode_internal,
4395     2,
4396     1,
4397     SIGNIFICAND_BITS - 2,
4398     -MAX_EXP,
4399     MAX_EXP,
4400     -1,
4401     true,
4402     true,
4403     false,
4404     true,
4405     true 
4406   };
4407 \f
4408 /* Set up default mode to format mapping for IEEE.  Everyone else has
4409    to set these values in OVERRIDE_OPTIONS.  */
4410
4411 const struct real_format *real_format_for_mode[TFmode - QFmode + 1] =
4412 {
4413   NULL,                         /* QFmode */
4414   NULL,                         /* HFmode */
4415   NULL,                         /* TQFmode */
4416   &ieee_single_format,          /* SFmode */
4417   &ieee_double_format,          /* DFmode */
4418
4419   /* We explicitly don't handle XFmode.  There are two formats,
4420      pretty much equally common.  Choose one in OVERRIDE_OPTIONS.  */
4421   NULL,                         /* XFmode */
4422   &ieee_quad_format             /* TFmode */
4423 };
4424
4425 \f
4426 /* Calculate the square root of X in mode MODE, and store the result
4427    in R.  Return TRUE if the operation does not raise an exception.
4428    For details see "High Precision Division and Square Root",
4429    Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4430    1993.  http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf.  */
4431
4432 bool
4433 real_sqrt (r, mode, x)
4434      REAL_VALUE_TYPE *r;
4435      enum machine_mode mode;
4436      const REAL_VALUE_TYPE *x;
4437 {
4438   static REAL_VALUE_TYPE halfthree;
4439   static REAL_VALUE_TYPE half;
4440   static bool init = false;
4441   REAL_VALUE_TYPE h, t, i;
4442   int iter, exp;
4443
4444   /* sqrt(-0.0) is -0.0.  */
4445   if (real_isnegzero (x))
4446     {
4447       *r = *x;
4448       return false;
4449     }
4450
4451   /* Negative arguments return NaN.  */
4452   if (real_isneg (x))
4453     {
4454       /* Mode is ignored for canonical NaN.  */
4455       real_nan (r, "", 1, SFmode);
4456       return false;
4457     }
4458
4459   /* Infinity and NaN return themselves.  */
4460   if (real_isinf (x) || real_isnan (x))
4461     {
4462       *r = *x;
4463       return false;
4464     }
4465
4466   if (!init)
4467     {
4468       real_arithmetic (&half, RDIV_EXPR, &dconst1, &dconst2);
4469       real_arithmetic (&halfthree, PLUS_EXPR, &dconst1, &half);
4470       init = true;
4471     }
4472
4473   /* Initial guess for reciprocal sqrt, i.  */
4474   exp = real_exponent (x);
4475   real_ldexp (&i, &dconst1, -exp/2);
4476
4477   /* Newton's iteration for reciprocal sqrt, i.  */
4478   for (iter = 0; iter < 16; iter++)
4479     {
4480       /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x).  */
4481       real_arithmetic (&t, MULT_EXPR, x, &i);
4482       real_arithmetic (&h, MULT_EXPR, &t, &i);
4483       real_arithmetic (&t, MULT_EXPR, &h, &half);
4484       real_arithmetic (&h, MINUS_EXPR, &halfthree, &t);
4485       real_arithmetic (&t, MULT_EXPR, &i, &h);
4486
4487       /* Check for early convergence.  */
4488       if (iter >= 6 && real_identical (&i, &t))
4489         break;
4490
4491       /* ??? Unroll loop to avoid copying.  */
4492       i = t;
4493     }
4494
4495   /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)).  */
4496   real_arithmetic (&t, MULT_EXPR, x, &i);
4497   real_arithmetic (&h, MULT_EXPR, &t, &i);
4498   real_arithmetic (&i, MINUS_EXPR, &dconst1, &h);
4499   real_arithmetic (&h, MULT_EXPR, &t, &i);
4500   real_arithmetic (&i, MULT_EXPR, &half, &h);
4501   real_arithmetic (&h, PLUS_EXPR, &t, &i);
4502
4503   /* ??? We need a Tuckerman test to get the last bit.  */
4504
4505   real_convert (r, mode, &h);
4506   return true;
4507 }
4508