OSDN Git Service

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