OSDN Git Service

cf72b4c73f2996d502c8ca29b26e2f08c1c17449
[pf3gnuchains/gcc-fork.git] / gcc / real.c
1 /* real.c - software floating point emulation.
2    Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2002 Free Software Foundation, Inc.
4    Contributed by Stephen L. Moshier (moshier@world.std.com).
5    Re-written by Richard Henderson  <rth@redhat.com>
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 "tree.h"
27 #include "toplev.h"
28 #include "real.h"
29 #include "tm_p.h"
30
31 /* The floating point model used internally is not exactly IEEE 754
32    compliant, and close to the description in the ISO C standard,
33    section 5.2.4.2.2 Characteristics of floating types.
34
35    Specifically
36
37         x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
38
39         where
40                 s = sign (+- 1)
41                 b = base or radix, here always 2
42                 e = exponent
43                 p = precision (the number of base-b digits in the significand)
44                 f_k = the digits of the significand.
45
46    We differ from typical IEEE 754 encodings in that the entire
47    significand is fractional.  Normalized significands are in the
48    range [0.5, 1.0).
49
50    A requirement of the model is that P be larger than than the 
51    largest supported target floating-point type by at least 2 bits.
52    This gives us proper rounding when we truncate to the target type.
53    In addition, E must be large enough to hold the smallest supported
54    denormal number in a normalized form.
55
56    Both of these requirements are easily satisfied.  The largest
57    target significand is 113 bits; we store 128.  The smallest
58    denormal number fits in 17 exponent bits; we store 29.
59
60    Target floating point models that use base 16 instead of base 2
61    (i.e. IBM 370), are handled during round_for_format, in which we
62    canonicalize the exponent to be a multiple of 4 (log2(16)), and
63    adjust the significand to match.  */
64
65
66 /* Enumerate the special cases of numbers that we encounter.  */
67 enum real_value_class {
68   rvc_zero,
69   rvc_normal,
70   rvc_inf,
71   rvc_nan
72 };
73
74 /* Used to classify two numbers simultaneously.  */
75 #define CLASS2(A, B)  ((A) << 2 | (B))
76
77 /* An expanded form of the represented number.  */
78
79 #define SIGNIFICAND_BITS        128
80 #define EXP_BITS                (32 - 3)
81 #define MAX_EXP                 ((1 << (EXP_BITS - 1)) - 1)
82 #define SIGSZ                   (SIGNIFICAND_BITS / HOST_BITS_PER_LONG)
83 #define SIG_MSB                 ((unsigned long)1 << (HOST_BITS_PER_LONG - 1))
84
85 #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
86  #error "Some constant folding done by hand to avoid shift count warnings"
87 #endif
88
89 struct real_value
90 {
91   enum real_value_class class : 2;
92   unsigned int sign : 1;
93   int exp : EXP_BITS;
94   unsigned long sig[SIGSZ];
95 };
96
97 /* Describes the properties of the specific target format in use.  */
98 struct real_format
99 {
100   /* Move to and from the target bytes.  */
101   void (*encode) (const struct real_format *, long *,
102                   const struct real_value *);
103   void (*decode) (const struct real_format *, struct real_value *,
104                   const long *);
105
106   /* The radix of the exponent and digits of the significand.  */
107   int b;
108
109   /* log2(b).  */
110   int log2_b;
111
112   /* Size of the significand in digits of radix B.  */
113   int p;
114
115   /* The minimum negative integer, x, such that b**(x-1) is normalized.  */
116   int emin;
117
118   /* The maximum integer, x, such that b**(x-1) is representable.  */
119   int emax;
120
121   /* Properties of the format.  */
122   bool has_nans;
123   bool has_inf;
124   bool has_denorm;
125   bool has_signed_zero;
126   bool qnan_msb_set;
127 };
128
129
130 static const struct real_format *fmt_for_mode[TFmode - QFmode + 1];
131
132
133 static void get_zero PARAMS ((struct real_value *, int));
134 static void get_canonical_qnan PARAMS ((struct real_value *, int));
135 static void get_canonical_snan PARAMS ((struct real_value *, int));
136 static void get_inf PARAMS ((struct real_value *, int));
137 static void sticky_rshift_significand PARAMS ((struct real_value *,
138                                                const struct real_value *,
139                                                unsigned int));
140 static void rshift_significand PARAMS ((struct real_value *,
141                                         const struct real_value *,
142                                         unsigned int));
143 static void lshift_significand PARAMS ((struct real_value *,
144                                         const struct real_value *,
145                                         unsigned int));
146 static void lshift_significand_1 PARAMS ((struct real_value *,
147                                           const struct real_value *));
148 static bool add_significands PARAMS ((struct real_value *r,
149                                       const struct real_value *,
150                                       const struct real_value *));
151 static bool sub_significands PARAMS ((struct real_value *,
152                                       const struct real_value *,
153                                       const struct real_value *));
154 static void neg_significand PARAMS ((struct real_value *,
155                                      const struct real_value *));
156 static int cmp_significands PARAMS ((const struct real_value *,
157                                      const struct real_value *));
158 static void set_significand_bit PARAMS ((struct real_value *, unsigned int));
159 static void clear_significand_bit PARAMS ((struct real_value *, unsigned int));
160 static bool test_significand_bit PARAMS ((struct real_value *, unsigned int));
161 static void clear_significand_below PARAMS ((struct real_value *,
162                                              unsigned int));
163 static bool div_significands PARAMS ((struct real_value *,
164                                       const struct real_value *,
165                                       const struct real_value *));
166 static void normalize PARAMS ((struct real_value *));
167
168 static void do_add PARAMS ((struct real_value *, const struct real_value *,
169                             const struct real_value *, int));
170 static void do_multiply PARAMS ((struct real_value *,
171                                  const struct real_value *,
172                                  const struct real_value *));
173 static void do_divide PARAMS ((struct real_value *, const struct real_value *,
174                                const struct real_value *));
175 static int do_compare PARAMS ((const struct real_value *,
176                                const struct real_value *, int));
177 static void do_fix_trunc PARAMS ((struct real_value *,
178                                   const struct real_value *));
179
180 static const struct real_value * ten_to_ptwo PARAMS ((int));
181 static const struct real_value * real_digit PARAMS ((int));
182
183 static void round_for_format PARAMS ((const struct real_format *,
184                                       struct real_value *));
185 \f
186 /* Initialize R with a positive zero.  */
187
188 static inline void
189 get_zero (r, sign)
190      struct real_value *r;
191      int sign;
192 {
193   memset (r, 0, sizeof (*r));
194   r->sign = sign;
195 }
196
197 /* Initialize R with the canonical quiet NaN.  */
198
199 static inline void
200 get_canonical_qnan (r, sign)
201      struct real_value *r;
202      int sign;
203 {
204   memset (r, 0, sizeof (*r));
205   r->class = rvc_nan;
206   r->sign = sign;
207   r->sig[SIGSZ-1] = SIG_MSB >> 1;
208 }
209
210 static inline void
211 get_canonical_snan (r, sign)
212      struct real_value *r;
213      int sign;
214 {
215   memset (r, 0, sizeof (*r));
216   r->class = rvc_nan;
217   r->sign = sign;
218   r->sig[SIGSZ-1] = SIG_MSB >> 2;
219 }
220
221 static inline void
222 get_inf (r, sign)
223      struct real_value *r;
224      int sign;
225 {
226   memset (r, 0, sizeof (*r));
227   r->class = rvc_inf;
228   r->sign = sign;
229 }
230
231 \f
232 /* Right-shift the significand of A by N bits; put the result in the
233    significand of R.  If any one bits are shifted out, set the least
234    significant bit of R.  */
235
236 static void
237 sticky_rshift_significand (r, a, n)
238      struct real_value *r;
239      const struct real_value *a;
240      unsigned int n;
241 {
242   unsigned long sticky = 0;
243   unsigned int i, ofs = 0;
244
245   if (n >= HOST_BITS_PER_LONG)
246     {
247       for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
248         sticky |= a->sig[i];
249       n -= ofs * HOST_BITS_PER_LONG;
250     }
251
252   if (n != 0)
253     {
254       sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
255       for (i = 0; i < SIGSZ; ++i)
256         {
257           r->sig[i]
258             = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
259                | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
260                   << (HOST_BITS_PER_LONG - n)));
261         }
262     }
263   else
264     {
265       for (i = 0; ofs + i < SIGSZ; ++i)
266         r->sig[i] = a->sig[ofs + i];
267       for (; i < SIGSZ; ++i)
268         r->sig[i] = 0;
269     }
270
271   r->sig[0] |= (sticky != 0);
272 }
273
274 /* Right-shift the significand of A by N bits; put the result in the
275    significand of R.  */
276
277 static void
278 rshift_significand (r, a, n)
279      struct real_value *r;
280      const struct real_value *a;
281      unsigned int n;
282 {
283   unsigned int i, ofs = n / HOST_BITS_PER_LONG;
284
285   n -= ofs * HOST_BITS_PER_LONG;
286   if (n != 0)
287     {
288       for (i = 0; i < SIGSZ; ++i)
289         {
290           r->sig[i]
291             = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
292                | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
293                   << (HOST_BITS_PER_LONG - n)));
294         }
295     }
296   else
297     {
298       for (i = 0; ofs + i < SIGSZ; ++i)
299         r->sig[i] = a->sig[ofs + i];
300       for (; i < SIGSZ; ++i)
301         r->sig[i] = 0;
302     }
303 }
304
305 /* Left-shift the significand of A by N bits; put the result in the
306    significand of R.  */
307
308 static void
309 lshift_significand (r, a, n)
310      struct real_value *r;
311      const struct real_value *a;
312      unsigned int n;
313 {
314   unsigned int i, ofs = n / HOST_BITS_PER_LONG;
315
316   n -= ofs * HOST_BITS_PER_LONG;
317   if (n == 0)
318     {
319       for (i = 0; ofs + i < SIGSZ; ++i)
320         r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
321       for (; i < SIGSZ; ++i)
322         r->sig[SIGSZ-1-i] = 0;
323     }
324   else
325     for (i = 0; i < SIGSZ; ++i)
326       {
327         r->sig[SIGSZ-1-i]
328           = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
329              | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
330                 >> (HOST_BITS_PER_LONG - n)));
331       }
332 }
333
334 /* Likewise, but N is specialized to 1.  */
335
336 static inline void
337 lshift_significand_1 (r, a)
338      struct real_value *r;
339      const struct real_value *a;
340 {
341   unsigned int i;
342
343   for (i = SIGSZ - 1; i > 0; --i)
344     r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
345   r->sig[0] = a->sig[0] << 1;
346 }
347
348 /* Add the significands of A and B, placing the result in R.  Return
349    true if there was carry out of the most significant word.  */
350
351 static inline bool
352 add_significands (r, a, b)
353      struct real_value *r;
354      const struct real_value *a, *b;
355 {
356   bool carry = false;
357   int i;
358
359   for (i = 0; i < SIGSZ; ++i)
360     {
361       unsigned long ai = a->sig[i];
362       unsigned long ri = ai + b->sig[i];
363
364       if (carry)
365         {
366           carry = ri < ai;
367           carry |= ++ri == 0;
368         }
369       else
370         carry = ri < ai;
371
372       r->sig[i] = ri;
373     }
374
375   return carry;
376 }
377
378 /* Subtract the significands of A and B, placing the result in R.
379    Return true if there was carry out of the most significant word.  */
380
381 static inline bool
382 sub_significands (r, a, b)
383      struct real_value *r;
384      const struct real_value *a, *b;
385 {
386   bool carry = false;
387   int i;
388
389   for (i = 0; i < SIGSZ; ++i)
390     {
391       unsigned long ai = a->sig[i];
392       unsigned long ri = ai - b->sig[i];
393
394       if (carry)
395         {
396           carry = ri > ai;
397           carry |= ~--ri == 0;
398         }
399       else
400         carry = ri > ai;
401
402       r->sig[i] = ri;
403     }
404
405   return carry;
406 }  
407
408 /* Negate the significand A, placing the result in R.  */
409
410 static inline void
411 neg_significand (r, a)
412      struct real_value *r;
413      const struct real_value *a;
414 {
415   bool carry = true;
416   int i;
417
418   for (i = 0; i < SIGSZ; ++i)
419     {
420       unsigned long ri, ai = a->sig[i];
421
422       if (carry)
423         {
424           if (ai)
425             {
426               ri = -ai;
427               carry = false;
428             }
429           else
430             ri = ai;
431         }
432       else
433         ri = ~ai;
434
435       r->sig[i] = ri;
436     }
437 }  
438
439 /* Compare significands.  Return tri-state vs zero.  */
440
441 static inline int 
442 cmp_significands (a, b)
443      const struct real_value *a, *b;
444 {
445   int i;
446
447   for (i = SIGSZ - 1; i >= 0; --i)
448     {
449       unsigned long ai = a->sig[i];
450       unsigned long bi = b->sig[i];
451
452       if (ai > bi)
453         return 1;
454       if (ai < bi)
455         return -1;
456     }
457
458   return 0;
459 }
460
461 /* Set bit N of the significand of R.  */
462
463 static inline void
464 set_significand_bit (r, n)
465      struct real_value *r;
466      unsigned int n;
467 {
468   r->sig[n / HOST_BITS_PER_LONG]
469     |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
470 }
471
472 /* Clear bit N of the significand of R.  */
473
474 static inline void
475 clear_significand_bit (r, n)
476      struct real_value *r;
477      unsigned int n;
478 {
479   r->sig[n / HOST_BITS_PER_LONG]
480     &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
481 }
482
483 /* Test bit N of the significand of R.  */
484
485 static inline bool
486 test_significand_bit (r, n)
487      struct real_value *r;
488      unsigned int n;
489 {
490   /* ??? Compiler bug here if we return this expression directly.
491      The conversion to bool strips the "&1" and we wind up testing
492      e.g. 2 != 0 -> true.  Seen in gcc version 3.2 20020520.  */
493   int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
494   return t;
495 }
496
497 /* Clear bits 0..N-1 of the significand of R.  */
498
499 static void
500 clear_significand_below (r, n)
501      struct real_value *r;
502      unsigned int n;
503 {
504   int i, w = n / HOST_BITS_PER_LONG;
505
506   for (i = 0; i < w; ++i)
507     r->sig[i] = 0;
508
509   r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
510 }
511
512 /* Divide the significands of A and B, placing the result in R.  Return
513    true if the division was inexact.  */
514
515 static inline bool
516 div_significands (r, a, b)
517      struct real_value *r;
518      const struct real_value *a, *b;
519 {
520   struct real_value u;
521   int bit = SIGNIFICAND_BITS - 1;
522   int i;
523   long inexact;
524
525   u = *a;
526   memset (r->sig, 0, sizeof (r->sig));
527
528   goto start;
529   do
530     {
531       if ((u.sig[SIGSZ-1] & SIG_MSB) == 0)
532         {
533           lshift_significand_1 (&u, &u);
534         start:
535           if (cmp_significands (&u, b) >= 0)
536             {
537               sub_significands (&u, &u, b);
538               set_significand_bit (r, bit);
539             }
540         }
541       else
542         {
543           /* We lose a bit here, and thus know the next quotient bit
544              will be one.  */
545           lshift_significand_1 (&u, &u);
546           sub_significands (&u, &u, b);
547           set_significand_bit (r, bit);
548         }
549     }
550   while (--bit >= 0);
551
552   for (i = 0, inexact = 0; i < SIGSZ; i++)
553     inexact |= u.sig[i];
554
555   return inexact != 0;
556 }
557
558 /* Adjust the exponent and significand of R such that the most
559    significant bit is set.  We underflow to zero and overflow to
560    infinity here, without denormals.  (The intermediate representation
561    exponent is large enough to handle target denormals normalized.)  */
562
563 static void
564 normalize (r)
565      struct real_value *r;
566 {
567   int shift = 0, exp;
568   int i, j;
569
570   /* Find the first word that is non-zero.  */
571   for (i = SIGSZ - 1; i >= 0; i--)
572     if (r->sig[i] == 0)
573       shift += HOST_BITS_PER_LONG;
574     else
575       break;
576
577   /* Zero significand flushes to zero.  */
578   if (i < 0)
579     {
580       r->class = rvc_zero;
581       r->exp = 0;
582       return;
583     }
584
585   /* Find the first bit that is non-zero.  */
586   for (j = 0; ; j++)
587     if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
588       break;
589   shift += j;
590
591   if (shift > 0)
592     {
593       exp = r->exp - shift;
594       if (exp > MAX_EXP)
595         get_inf (r, r->sign);
596       else if (exp < -MAX_EXP)
597         get_zero (r, r->sign);
598       else
599         {
600           r->exp = exp;
601           lshift_significand (r, r, shift);
602         }
603     }
604 }
605 \f
606 /* Return R = A + (SUBTRACT_P ? -B : B).  */
607
608 static void
609 do_add (r, a, b, subtract_p)
610      struct real_value *r;
611      const struct real_value *a, *b;
612      int subtract_p;
613 {
614   int dexp, sign, exp;
615   struct real_value t;
616
617   /* Determine if we need to add or subtract.  */
618   sign = a->sign;
619   subtract_p = (sign ^ b->sign) ^ subtract_p;
620
621   switch (CLASS2 (a->class, b->class))
622     {
623     case CLASS2 (rvc_zero, rvc_zero):
624       /* +-0 +/- +-0 = +0.  */
625       get_zero (r, 0);
626       return;
627
628     case CLASS2 (rvc_zero, rvc_normal):
629     case CLASS2 (rvc_zero, rvc_inf):
630     case CLASS2 (rvc_zero, rvc_nan):
631       /* 0 + ANY = ANY.  */
632     case CLASS2 (rvc_normal, rvc_nan):
633     case CLASS2 (rvc_inf, rvc_nan):
634     case CLASS2 (rvc_nan, rvc_nan):
635       /* ANY + NaN = NaN.  */
636     case CLASS2 (rvc_normal, rvc_inf):
637       /* R + Inf = Inf.  */
638       *r = *b;
639       r->sign = sign ^ subtract_p;
640       return;
641
642     case CLASS2 (rvc_normal, rvc_zero):
643     case CLASS2 (rvc_inf, rvc_zero):
644     case CLASS2 (rvc_nan, rvc_zero):
645       /* ANY + 0 = ANY.  */
646     case CLASS2 (rvc_nan, rvc_normal):
647     case CLASS2 (rvc_nan, rvc_inf):
648       /* NaN + ANY = NaN.  */
649     case CLASS2 (rvc_inf, rvc_normal):
650       /* Inf + R = Inf.  */
651       *r = *a;
652       return;
653
654     case CLASS2 (rvc_inf, rvc_inf):
655       if (subtract_p)
656         /* Inf - Inf = NaN.  */
657         get_canonical_qnan (r, 0);
658       else
659         /* Inf + Inf = Inf.  */
660         *r = *a;
661       return;
662
663     case CLASS2 (rvc_normal, rvc_normal):
664       break;
665
666     default:
667       abort ();
668     }
669
670   /* Swap the arguments such that A has the larger exponent.  */
671   dexp = a->exp - b->exp;
672   if (dexp < 0)
673     {
674       const struct real_value *t;
675       t = a, a = b, b = t;
676       dexp = -dexp;
677       sign ^= subtract_p;
678     }
679   exp = a->exp;
680
681   /* If the exponents are not identical, we need to shift the
682      significand of B down.  */
683   if (dexp > 0)
684     {
685       /* If the exponents are too far apart, the significands
686          do not overlap, which makes the subtraction a noop.  */
687       if (dexp >= SIGNIFICAND_BITS)
688         {
689           *r = *a;
690           r->sign = sign;
691           return;
692         }
693
694       sticky_rshift_significand (&t, b, dexp);
695       b = &t;
696     }
697
698   if (subtract_p)
699     {
700       if (sub_significands (r, a, b))
701         {
702           /* We got a borrow out of the subtraction.  That means that
703              A and B had the same exponent, and B had the larger
704              significand.  We need to swap the sign and negate the
705              significand.  */
706           sign ^= 1;
707           neg_significand (r, r);
708         }
709     }
710   else
711     {
712       if (add_significands (r, a, b))
713         {
714           /* We got carry out of the addition.  This means we need to
715              shift the significand back down one bit and increase the
716              exponent.  */
717           sticky_rshift_significand (r, r, 1);
718           r->sig[SIGSZ-1] |= SIG_MSB;
719           if (++exp > MAX_EXP)
720             {
721               get_inf (r, sign);
722               return;
723             }
724         }
725     }
726
727   r->class = rvc_normal;
728   r->sign = sign;
729   r->exp = exp;
730
731   /* Re-normalize the result.  */
732   normalize (r);
733
734   /* Special case: if the subtraction results in zero, the result
735      is positive.  */
736   if (r->class == rvc_zero)
737     r->sign = 0;
738 }
739
740 /* Return R = A * B.  */
741
742 static void
743 do_multiply (r, a, b)
744      struct real_value *r;
745      const struct real_value *a, *b;
746 {
747   struct real_value u, t, *rr;
748   unsigned int i, j, k;
749   int sign = a->sign ^ b->sign;
750
751   switch (CLASS2 (a->class, b->class))
752     {
753     case CLASS2 (rvc_zero, rvc_zero):
754     case CLASS2 (rvc_zero, rvc_normal):
755     case CLASS2 (rvc_normal, rvc_zero):
756       /* +-0 * ANY = 0 with appropriate sign.  */
757       get_zero (r, sign);
758       return;
759
760     case CLASS2 (rvc_zero, rvc_nan):
761     case CLASS2 (rvc_normal, rvc_nan):
762     case CLASS2 (rvc_inf, rvc_nan):
763     case CLASS2 (rvc_nan, rvc_nan):
764       /* ANY * NaN = NaN.  */
765       *r = *b;
766       r->sign = sign;
767       return;
768
769     case CLASS2 (rvc_nan, rvc_zero):
770     case CLASS2 (rvc_nan, rvc_normal):
771     case CLASS2 (rvc_nan, rvc_inf):
772       /* NaN * ANY = NaN.  */
773       *r = *a;
774       r->sign = sign;
775       return;
776
777     case CLASS2 (rvc_zero, rvc_inf):
778     case CLASS2 (rvc_inf, rvc_zero):
779       /* 0 * Inf = NaN */
780       get_canonical_qnan (r, sign);
781       return;
782
783     case CLASS2 (rvc_inf, rvc_inf):
784     case CLASS2 (rvc_normal, rvc_inf):
785     case CLASS2 (rvc_inf, rvc_normal):
786       /* Inf * Inf = Inf, R * Inf = Inf */
787     overflow:
788       get_inf (r, sign);
789       return;
790
791     case CLASS2 (rvc_normal, rvc_normal):
792       break;
793
794     default:
795       abort ();
796     }
797
798   if (r == a || r == b)
799     rr = &t;
800   else
801     rr = r;
802   get_zero (rr, 0);
803
804   u.class = rvc_normal;
805   u.sign = 0;
806
807   /* Collect all the partial products.  Since we don't have sure access
808      to a widening multiply, we split each long into two half-words.
809
810      Consider the long-hand form of a four half-word multiplication:
811
812                  A  B  C  D
813               *  E  F  G  H
814              --------------
815                 DE DF DG DH
816              CE CF CG CH
817           BE BF BG BH
818        AE AF AG AH
819
820      We construct partial products of the widened half-word products
821      that are known to not overlap, e.g. DF+DH.  Each such partial
822      product is given its proper exponent, which allows us to sum them
823      and obtain the finished product.  */
824
825   for (i = 0; i < SIGSZ * 2; ++i)
826     {
827       unsigned long ai = a->sig[i / 2];
828       if (i & 1)
829         ai >>= HOST_BITS_PER_LONG / 2;
830       else
831         ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
832
833       if (ai == 0)
834         continue;
835
836       for (j = 0; j < 2; ++j)
837         {
838           int exp = (a->exp - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
839                      + (b->exp - (1-j)*(HOST_BITS_PER_LONG/2)));
840
841           if (exp > MAX_EXP)
842             goto overflow;
843           if (exp < -MAX_EXP)
844             /* Would underflow to zero, which we shouldn't bother adding.  */
845             continue;
846
847           u.exp = exp;
848
849           for (k = j; k < SIGSZ * 2; k += 2)
850             {
851               unsigned long bi = b->sig[k / 2];
852               if (k & 1)
853                 bi >>= HOST_BITS_PER_LONG / 2;
854               else
855                 bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
856
857               u.sig[k / 2] = ai * bi;
858             }
859
860           do_add (rr, rr, &u, 0);
861         }
862     }
863
864   rr->sign = sign;
865   if (rr != r)
866     *r = t;
867 }
868
869 /* Return R = A / B.  */
870
871 static void
872 do_divide (r, a, b)
873      struct real_value *r;
874      const struct real_value *a, *b;
875 {
876   int exp, sign = a->sign ^ b->sign;
877   struct real_value t, *rr;
878   bool inexact;
879
880   switch (CLASS2 (a->class, b->class))
881     {
882     case CLASS2 (rvc_zero, rvc_zero):
883       /* 0 / 0 = NaN.  */
884     case CLASS2 (rvc_inf, rvc_zero):
885       /* Inf / 0 = NaN.  */
886     case CLASS2 (rvc_inf, rvc_inf):
887       /* Inf / Inf = NaN.  */
888       get_canonical_qnan (r, sign);
889       return;
890
891     case CLASS2 (rvc_zero, rvc_normal):
892     case CLASS2 (rvc_zero, rvc_inf):
893       /* 0 / ANY = 0.  */
894     case CLASS2 (rvc_normal, rvc_inf):
895       /* R / Inf = 0.  */
896     underflow:
897       get_zero (r, sign);
898       return;
899
900     case CLASS2 (rvc_normal, rvc_zero):
901       /* R / 0 = Inf.  */
902       get_inf (r, sign);
903       return;
904
905     case CLASS2 (rvc_zero, rvc_nan):
906     case CLASS2 (rvc_normal, rvc_nan):
907     case CLASS2 (rvc_inf, rvc_nan):
908     case CLASS2 (rvc_nan, rvc_nan):
909       /* ANY / NaN = NaN.  */
910       *r = *b;
911       r->sign = sign;
912       return;
913
914     case CLASS2 (rvc_nan, rvc_zero):
915     case CLASS2 (rvc_nan, rvc_normal):
916     case CLASS2 (rvc_nan, rvc_inf):
917       /* NaN / ANY = NaN.  */
918       *r = *a;
919       r->sign = sign;
920       return;
921
922     case CLASS2 (rvc_inf, rvc_normal):
923       /* Inf / R = Inf.  */
924     overflow:
925       get_inf (r, sign);
926       return;
927
928     case CLASS2 (rvc_normal, rvc_normal):
929       break;
930
931     default:
932       abort ();
933     }
934
935   if (r == a || r == b)
936     rr = &t;
937   else
938     rr = r;
939
940   rr->class = rvc_normal;
941   rr->sign = sign;
942
943   exp = a->exp - b->exp + 1;
944   if (exp > MAX_EXP)
945     goto overflow;
946   if (exp < -MAX_EXP)
947     goto underflow;
948   rr->exp = exp;
949
950   inexact = div_significands (rr, a, b);
951   rr->sig[0] |= inexact;
952
953   /* Re-normalize the result.  */
954   normalize (rr);
955
956   if (rr != r)
957     *r = t;
958 }
959
960 /* Return a tri-state comparison of A vs B.  Return NAN_RESULT if
961    one of the two operands is a NaN.  */
962
963 static int
964 do_compare (a, b, nan_result)
965      const struct real_value *a, *b;
966      int nan_result;
967 {
968   int ret;
969
970   switch (CLASS2 (a->class, b->class))
971     {
972     case CLASS2 (rvc_zero, rvc_zero):
973       /* Sign of zero doesn't matter for compares.  */
974       return 0;
975
976     case CLASS2 (rvc_inf, rvc_zero):
977     case CLASS2 (rvc_inf, rvc_normal):
978     case CLASS2 (rvc_normal, rvc_zero):
979       return (a->sign ? -1 : 1);
980
981     case CLASS2 (rvc_inf, rvc_inf):
982       return -a->sign - -b->sign;
983
984     case CLASS2 (rvc_zero, rvc_normal):
985     case CLASS2 (rvc_zero, rvc_inf):
986     case CLASS2 (rvc_normal, rvc_inf):
987       return (b->sign ? 1 : -1);
988
989     case CLASS2 (rvc_zero, rvc_nan):
990     case CLASS2 (rvc_normal, rvc_nan):
991     case CLASS2 (rvc_inf, rvc_nan):
992     case CLASS2 (rvc_nan, rvc_nan):
993     case CLASS2 (rvc_nan, rvc_zero):
994     case CLASS2 (rvc_nan, rvc_normal):
995     case CLASS2 (rvc_nan, rvc_inf):
996       return nan_result;
997
998     case CLASS2 (rvc_normal, rvc_normal):
999       break;
1000
1001     default:
1002       abort ();
1003     }
1004
1005   if (a->sign != b->sign)
1006     return -a->sign - -b->sign;
1007
1008   if (a->exp > b->exp)
1009     ret = 1;
1010   else if (a->exp < b->exp)
1011     ret = -1;
1012   else
1013     ret = cmp_significands (a, b);
1014
1015   return (a->sign ? -ret : ret);
1016 }
1017
1018 /* Return A truncated to an integral value toward zero.  */
1019
1020 void
1021 do_fix_trunc (r, a)
1022      struct real_value *r;
1023      const struct real_value *a;
1024 {
1025   *r = *a;
1026
1027   switch (a->class)
1028     {
1029     case rvc_zero:
1030     case rvc_inf:
1031     case rvc_nan:
1032       break;
1033
1034     case rvc_normal:
1035       if (r->exp <= 0)
1036         get_zero (r, r->sign);
1037       else if (r->exp < SIGNIFICAND_BITS)
1038         clear_significand_below (r, SIGNIFICAND_BITS - r->exp);
1039       break;
1040
1041     default:
1042       abort ();
1043     }
1044 }
1045
1046 /* Perform the binary or unary operation described by CODE.
1047    For a unary operation, leave OP1 NULL.  */
1048
1049 void
1050 real_arithmetic (tr, icode, top0, top1)
1051      REAL_VALUE_TYPE *tr;
1052      int icode;
1053      const REAL_VALUE_TYPE *top0, *top1;
1054 {
1055   struct real_value *r = (struct real_value *) tr;
1056   const struct real_value *op0 = (const struct real_value *) top0;
1057   const struct real_value *op1 = (const struct real_value *) top1;
1058   enum tree_code code = icode;
1059
1060   switch (code)
1061     {
1062     case PLUS_EXPR:
1063       do_add (r, op0, op1, 0);
1064       break;
1065
1066     case MINUS_EXPR:
1067       do_add (r, op0, op1, 1);
1068       break;
1069
1070     case MULT_EXPR:
1071       do_multiply (r, op0, op1);
1072       break;
1073
1074     case RDIV_EXPR:
1075       do_divide (r, op0, op1);
1076       break;
1077
1078     case MIN_EXPR:
1079       if (op1->class == rvc_nan)
1080         *r = *op1;
1081       else if (do_compare (op0, op1, -1) < 0)
1082         *r = *op0;
1083       else
1084         *r = *op1;
1085       break;
1086
1087     case MAX_EXPR:
1088       if (op1->class == rvc_nan)
1089         *r = *op1;
1090       else if (do_compare (op0, op1, 1) < 0)
1091         *r = *op1;
1092       else
1093         *r = *op0;
1094       break;
1095
1096     case NEGATE_EXPR:
1097       *r = *op0;
1098       r->sign ^= 1;
1099       break;
1100
1101     case ABS_EXPR:
1102       *r = *op0;
1103       r->sign = 0;
1104       break;
1105
1106     case FIX_TRUNC_EXPR:
1107       do_fix_trunc (r, op0);
1108       break;
1109
1110     default:
1111       abort ();
1112     }
1113 }
1114
1115 /* Legacy.  Similar, but return the result directly.  */
1116
1117 REAL_VALUE_TYPE
1118 real_arithmetic2 (icode, top0, top1)
1119      int icode;
1120      const REAL_VALUE_TYPE *top0, *top1;
1121 {
1122   REAL_VALUE_TYPE r;
1123   real_arithmetic (&r, icode, top0, top1);
1124   return r;
1125 }
1126
1127 bool
1128 real_compare (icode, top0, top1)
1129      int icode;
1130      const REAL_VALUE_TYPE *top0, *top1;
1131 {
1132   enum tree_code code = icode;
1133   const struct real_value *op0 = (const struct real_value *) top0;
1134   const struct real_value *op1 = (const struct real_value *) top1;
1135
1136   switch (code)
1137     {
1138     case LT_EXPR:
1139       return do_compare (op0, op1, 1) < 0;
1140     case LE_EXPR:
1141       return do_compare (op0, op1, 1) <= 0;
1142     case GT_EXPR:
1143       return do_compare (op0, op1, -1) > 0;
1144     case GE_EXPR:
1145       return do_compare (op0, op1, -1) >= 0;
1146     case EQ_EXPR:
1147       return do_compare (op0, op1, -1) == 0;
1148     case NE_EXPR:
1149       return do_compare (op0, op1, -1) != 0;
1150     case UNORDERED_EXPR:
1151       return op0->class == rvc_nan || op1->class == rvc_nan;
1152     case ORDERED_EXPR:
1153       return op0->class != rvc_nan && op1->class != rvc_nan;
1154     case UNLT_EXPR:
1155       return do_compare (op0, op1, -1) < 0;
1156     case UNLE_EXPR:
1157       return do_compare (op0, op1, -1) <= 0;
1158     case UNGT_EXPR:
1159       return do_compare (op0, op1, 1) > 0;
1160     case UNGE_EXPR:
1161       return do_compare (op0, op1, 1) >= 0;
1162     case UNEQ_EXPR:
1163       return do_compare (op0, op1, 0) == 0;
1164
1165     default:
1166       abort ();
1167     }
1168 }
1169
1170 /* Return floor log2(R).  */
1171
1172 int
1173 real_exponent (tr)
1174      const REAL_VALUE_TYPE *tr;
1175 {
1176   const struct real_value *r = (const struct real_value *) tr;
1177
1178   switch (r->class)
1179     {
1180     case rvc_zero:
1181       return 0;
1182     case rvc_inf:
1183     case rvc_nan:
1184       return (unsigned int)-1 >> 1;
1185     case rvc_normal:
1186       return r->exp;
1187     default:
1188       abort ();
1189     }
1190 }
1191
1192
1193 /* R = OP0 * 2**EXP.  */
1194
1195 void
1196 real_ldexp (tr, top0, exp)
1197      REAL_VALUE_TYPE *tr;
1198      const REAL_VALUE_TYPE *top0;
1199      int exp;
1200 {
1201   struct real_value *r = (struct real_value *) tr;
1202   const struct real_value *op0 = (const struct real_value *) top0;
1203
1204   *r = *op0;
1205   switch (r->class)
1206     {
1207     case rvc_zero:
1208     case rvc_inf:
1209     case rvc_nan:
1210       break;
1211
1212     case rvc_normal:
1213       exp += op0->exp;
1214       if (exp > MAX_EXP)
1215         get_inf (r, r->sign);
1216       else if (exp < -MAX_EXP)
1217         get_zero (r, r->sign);
1218       else
1219         r->exp = exp;
1220       break;
1221
1222     default:
1223       abort ();
1224     }
1225 }
1226
1227 /* Determine whether a floating-point value X is infinite.  */
1228
1229 bool
1230 real_isinf (tr)
1231      const REAL_VALUE_TYPE *tr;
1232 {
1233   const struct real_value *r = (const struct real_value *) tr;
1234   return (r->class == rvc_inf);
1235 }
1236
1237 /* Determine whether a floating-point value X is a NaN.  */
1238
1239 bool
1240 real_isnan (tr)
1241      const REAL_VALUE_TYPE *tr;
1242 {
1243   const struct real_value *r = (const struct real_value *) tr;
1244   return (r->class == rvc_nan);
1245 }
1246
1247 /* Determine whether a floating-point value X is negative.  */
1248
1249 bool
1250 real_isneg (tr)
1251      const REAL_VALUE_TYPE *tr;
1252 {
1253   const struct real_value *r = (const struct real_value *) tr;
1254   return r->sign;
1255 }
1256
1257 /* Determine whether a floating-point value X is minus zero.  */
1258
1259 bool
1260 real_isnegzero (tr)
1261      const REAL_VALUE_TYPE *tr;
1262 {
1263   const struct real_value *r = (const struct real_value *) tr;
1264   return r->sign && r->class == rvc_zero;
1265 }
1266
1267 /* Compare two floating-point objects for bitwise identity.  */
1268
1269 extern bool
1270 real_identical (ta, tb)
1271      const REAL_VALUE_TYPE *ta, *tb;
1272 {
1273   const struct real_value *a = (const struct real_value *) ta;
1274   const struct real_value *b = (const struct real_value *) tb;
1275   int i;
1276
1277   if (a->class != b->class)
1278     return false;
1279   if (a->sign != b->sign)
1280     return false;
1281
1282   switch (a->class)
1283     {
1284     case rvc_zero:
1285     case rvc_inf:
1286       break;
1287
1288     case rvc_normal:
1289       if (a->exp != b->exp)
1290         return false;
1291       /* FALLTHRU */
1292     case rvc_nan:
1293       for (i = 0; i < SIGSZ; ++i)
1294         if (a->sig[i] != b->sig[i])
1295           return false;
1296       break;
1297
1298     default:
1299       abort ();
1300     }
1301
1302   return true;
1303 }
1304
1305 /* Try to change R into its exact multiplicative inverse in machine
1306    mode MODE.  Return true if successful.  */
1307
1308 bool
1309 exact_real_inverse (mode, tr)
1310      enum machine_mode mode;
1311      REAL_VALUE_TYPE *tr;
1312 {
1313   const struct real_value *one = real_digit (1);
1314   struct real_value *r = (struct real_value *) tr;
1315   struct real_value u;
1316   int i;
1317   
1318   if (r->class != rvc_normal)
1319     return false;
1320
1321   /* Check for a power of two: all significand bits zero except the MSB.  */
1322   for (i = 0; i < SIGSZ-1; ++i)
1323     if (r->sig[i] != 0)
1324       return false;
1325   if (r->sig[SIGSZ-1] != SIG_MSB)
1326     return false;
1327
1328   /* Find the inverse and truncate to the required mode.  */
1329   do_divide (&u, one, r);
1330   real_convert ((REAL_VALUE_TYPE *)&u, mode, (REAL_VALUE_TYPE *)&u);
1331   
1332   /* The rounding may have overflowed.  */
1333   if (u.class != rvc_normal)
1334     return false;
1335   for (i = 0; i < SIGSZ-1; ++i)
1336     if (u.sig[i] != 0)
1337       return false;
1338   if (u.sig[SIGSZ-1] != SIG_MSB)
1339     return false;
1340
1341   *r = u;
1342   return true;
1343 }
1344 \f
1345 /* Render R as an integer.  */
1346
1347 HOST_WIDE_INT
1348 real_to_integer (tr)
1349      const REAL_VALUE_TYPE *tr;
1350 {
1351   const struct real_value *r = (const struct real_value *) tr;
1352   unsigned HOST_WIDE_INT i;
1353
1354   switch (r->class)
1355     {
1356     case rvc_zero:
1357     underflow:
1358       return 0;
1359
1360     case rvc_inf:
1361     case rvc_nan:
1362     overflow:
1363       i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1364       if (!r->sign)
1365         i--;
1366       return i;
1367
1368     case rvc_normal:
1369       if (r->exp <= 0)
1370         goto underflow;
1371       if (r->exp > HOST_BITS_PER_WIDE_INT)
1372         goto overflow;
1373
1374       if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1375         i = r->sig[SIGSZ-1];
1376       else if (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG)
1377         {
1378           i = r->sig[SIGSZ-1];
1379           i = i << (HOST_BITS_PER_LONG - 1) << 1;
1380           i |= r->sig[SIGSZ-2];
1381         }
1382       else
1383         abort ();
1384
1385       i >>= HOST_BITS_PER_WIDE_INT - r->exp;
1386
1387       if (r->sign)
1388         i = -i;
1389       return i;
1390
1391     default:
1392       abort ();
1393     }
1394 }
1395
1396 /* Likewise, but to an integer pair, HI+LOW.  */
1397
1398 void
1399 real_to_integer2 (plow, phigh, tr)
1400      HOST_WIDE_INT *plow, *phigh;
1401      const REAL_VALUE_TYPE *tr;
1402 {
1403   struct real_value r;
1404   HOST_WIDE_INT low, high;
1405   int exp;
1406
1407   r = *(const struct real_value *) tr;
1408   switch (r.class)
1409     {
1410     case rvc_zero:
1411     underflow:
1412       low = high = 0;
1413       break;
1414
1415     case rvc_inf:
1416     case rvc_nan:
1417     overflow:
1418       high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1419       if (r.sign)
1420         low = 0;
1421       else
1422         {
1423           high--;
1424           low = -1;
1425         }
1426       break;
1427
1428     case rvc_normal:
1429       exp = r.exp;
1430       if (exp <= 0)
1431         goto underflow;
1432       if (exp >= 2*HOST_BITS_PER_WIDE_INT)
1433         goto overflow;
1434
1435       rshift_significand (&r, &r, 2*HOST_BITS_PER_WIDE_INT - exp);
1436       if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1437         {
1438           high = r.sig[SIGSZ-1];
1439           low = r.sig[SIGSZ-2];
1440         }
1441       else if (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG)
1442         {
1443           high = r.sig[SIGSZ-1];
1444           high = high << (HOST_BITS_PER_LONG - 1) << 1;
1445           high |= r.sig[SIGSZ-2];
1446
1447           low = r.sig[SIGSZ-3];
1448           low = low << (HOST_BITS_PER_LONG - 1) << 1;
1449           low |= r.sig[SIGSZ-4];
1450         }
1451       else
1452         abort ();
1453
1454       if (r.sign)
1455         {
1456           if (low == 0)
1457             high = -high;
1458           else
1459             low = -low, high = ~high;
1460         }
1461       break;
1462
1463     default:
1464       abort ();
1465     }
1466
1467   *plow = low;
1468   *phigh = high;
1469 }
1470
1471 /* Render R as a decimal floating point constant.  Emit DIGITS
1472    significant digits in the result.  If DIGITS <= 0, choose the
1473    maximum for the representation.  */
1474
1475 #define M_LOG10_2       0.30102999566398119521
1476
1477 void
1478 real_to_decimal (str, r_orig, digits)
1479      char *str;
1480      const REAL_VALUE_TYPE *r_orig;
1481      int digits;
1482 {
1483   struct real_value r;
1484   const struct real_value *one, *ten;
1485   int dec_exp, max_digits, d, cmp_half;
1486   char *p, *first, *last;
1487   bool sign;
1488
1489   r = *(const struct real_value *)r_orig;
1490   switch (r.class)
1491     {
1492     case rvc_zero:
1493       strcpy (str, (r.sign ? "-0.0" : "0.0"));
1494       return;
1495     case rvc_normal:
1496       break;
1497     case rvc_inf:
1498       strcpy (str, (r.sign ? "+Inf" : "-Inf"));
1499       return;
1500     case rvc_nan:
1501       /* ??? Print the significand as well, if not canonical?  */
1502       strcpy (str, (r.sign ? "+NaN" : "-NaN"));
1503       return;
1504     default:
1505       abort ();
1506     }
1507
1508   max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1509   if (digits <= 0 || digits > max_digits)
1510     digits = max_digits;
1511
1512   one = real_digit (1);
1513   ten = ten_to_ptwo (0);
1514
1515   sign = r.sign;
1516   r.sign = 0;
1517
1518   /* Estimate the decimal exponent.  */
1519   dec_exp = r.exp * M_LOG10_2;
1520   
1521   /* Scale the number such that it is in [1, 10).  */
1522   if (dec_exp > 0)
1523     {
1524       int i;
1525       for (i = EXP_BITS - 1; i >= 0; --i)
1526         if (dec_exp & (1 << i))
1527           do_divide (&r, &r, ten_to_ptwo (i));
1528     }
1529   else if (dec_exp < 0)
1530     {
1531       int i, pos_exp = -(--dec_exp);
1532
1533       for (i = EXP_BITS - 1; i >= 0; --i)
1534         if (pos_exp & (1 << i))
1535           do_multiply (&r, &r, ten_to_ptwo (i));
1536     }
1537
1538   /* Assert that the number is in the proper range.  Round-off can
1539      prevent the above from working exactly.  */
1540   if (do_compare (&r, one, -1) < 0)
1541     {
1542       do_multiply (&r, &r, ten);
1543       dec_exp--;
1544     }
1545   else if (do_compare (&r, ten, 1) >= 0)
1546     {
1547       do_divide (&r, &r, ten);
1548       dec_exp++;
1549     }
1550
1551   p = str;
1552   if (sign)
1553     *p++ = '-';
1554   first = p++;
1555   while (1)
1556     {
1557       d = real_to_integer ((const REAL_VALUE_TYPE *) &r);
1558       do_add (&r, &r, real_digit (d), 1);
1559
1560       *p++ = d + '0';
1561       if (--digits == 0)
1562         break;
1563       do_multiply (&r, &r, ten);
1564     }
1565   last = p;
1566
1567   /* Round the result.  Compare R vs 0.5 by doing R*2 vs 1.0.  */
1568   r.exp += 1;
1569   cmp_half = do_compare (&r, one, -1);
1570   if (cmp_half == 0)
1571     /* Round to even.  */
1572     cmp_half += d & 1;
1573   if (cmp_half > 0)
1574     {
1575       while (p > first)
1576         {
1577           d = *--p;
1578           if (d == '9')
1579             *p = '0';
1580           else
1581             {
1582               *p = d + 1;
1583               break;
1584             }
1585         }
1586
1587       if (p == first)
1588         {
1589           first[1] = '1';
1590           dec_exp++;
1591         }
1592     }
1593   
1594   first[0] = first[1];
1595   first[1] = '.';
1596
1597   sprintf (last, "e%+d", dec_exp);
1598 }
1599
1600 /* Render R as a hexadecimal floating point constant.  Emit DIGITS
1601    significant digits in the result.  If DIGITS <= 0, choose the maximum
1602    for the representation.  */
1603
1604 void
1605 real_to_hexadecimal (str, tr, digits)
1606      char *str;
1607      const REAL_VALUE_TYPE *tr;
1608      int digits;
1609 {
1610   struct real_value r;
1611   int i, j;
1612   char *p;
1613
1614   r = *(const struct real_value *) tr;
1615
1616   switch (r.class)
1617     {
1618     case rvc_zero:
1619       r.exp = 0;
1620       break;
1621     case rvc_normal:
1622       break;
1623     case rvc_inf:
1624       strcpy (str, (r.sign ? "+Inf" : "-Inf"));
1625       return;
1626     case rvc_nan:
1627       /* ??? Print the significand as well, if not canonical?  */
1628       strcpy (str, (r.sign ? "+NaN" : "-NaN"));
1629       return;
1630     default:
1631       abort ();
1632     }
1633
1634   if (digits <= 0)
1635     digits = SIGNIFICAND_BITS / 4;
1636
1637   p = str;
1638   if (r.sign)
1639     *p++ = '-';
1640   *p++ = '0';
1641   *p++ = 'x';
1642   *p++ = '0';
1643   *p++ = '.';
1644
1645   for (i = SIGSZ - 1; i >= 0; --i)
1646     for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1647       {
1648         *p++ = "0123456789abcdef"[(r.sig[i] >> j) & 15];
1649         if (--digits == 0)
1650           goto out;
1651       }
1652  out:
1653   sprintf (p, "p%+d", r.exp);
1654 }
1655
1656 /* Initialize R from a decimal or hexadecimal string.  The string is
1657    assumed to have been syntax checked already.  */
1658
1659 void
1660 real_from_string (tr, str)
1661      REAL_VALUE_TYPE *tr;
1662      const char *str;
1663 {
1664   struct real_value *r = (struct real_value *) tr;
1665   int exp = 0;
1666
1667   get_zero (r, 0);
1668
1669   if (*str == '-')
1670     {
1671       r->sign = 1;
1672       str++;
1673     }
1674   else if (*str == '+')
1675     str++;
1676
1677   if (str[0] == '0' && str[1] == 'x')
1678     {
1679       /* Hexadecimal floating point.  */
1680       int pos = SIGNIFICAND_BITS - 4, d;
1681
1682       str += 2;
1683
1684       while (*str == '0')
1685         str++;
1686       while (1)
1687         {
1688           d = hex_value (*str);
1689           if (d == _hex_bad)
1690             break;
1691           if (pos >= 0)
1692             {
1693               r->sig[pos / HOST_BITS_PER_LONG]
1694                 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1695               pos -= 4;
1696             }
1697           exp += 4;
1698           str++;
1699         }
1700       if (*str == '.')
1701         {
1702           str++;
1703           while (1)
1704             {
1705               d = hex_value (*str);
1706               if (d == _hex_bad)
1707                 break;
1708               if (pos >= 0)
1709                 {
1710                   r->sig[pos / HOST_BITS_PER_LONG]
1711                     |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1712                   pos -= 4;
1713                 }
1714               str++;
1715             }
1716         }
1717       if (*str == 'p' || *str == 'P')
1718         {
1719           int exp_neg = 0;
1720
1721           str++;
1722           if (*str == '-')
1723             {
1724               exp_neg = 1;
1725               str++;
1726             }
1727           else if (*str == '+')
1728             str++;
1729
1730           d = 0;
1731           while (ISDIGIT (*str))
1732             {
1733               int t = d;
1734               d *= 10;
1735               d += *str - '0';
1736               if (d < t)
1737                 {
1738                   /* Overflowed the exponent.  */
1739                   if (exp_neg)
1740                     goto underflow;
1741                   else
1742                     goto overflow;
1743                 }
1744               str++;
1745             }
1746           if (exp_neg)
1747             d = -d;
1748
1749           exp += d;
1750         }
1751
1752       r->class = rvc_normal;
1753       r->exp = exp;
1754       if (r->exp != exp)
1755         {
1756           if (exp < 0)
1757             goto underflow;
1758           else
1759             goto overflow;
1760         }
1761
1762       normalize (r);
1763     }
1764   else
1765     {
1766       /* Decimal floating point.  */
1767       const struct real_value *ten = ten_to_ptwo (0);
1768       int d;
1769
1770       while (*str == '0')
1771         str++;
1772       while (ISDIGIT (*str))
1773         {
1774           d = *str++ - '0';
1775           do_multiply (r, r, ten);
1776           if (d)
1777             do_add (r, r, real_digit (d), 0);
1778         }
1779       if (*str == '.')
1780         {
1781           str++;
1782           while (ISDIGIT (*str))
1783             {
1784               d = *str++ - '0';
1785               do_multiply (r, r, ten);
1786               if (d)
1787                 do_add (r, r, real_digit (d), 0);
1788               exp--;
1789             }
1790         }
1791
1792       if (*str == 'e' || *str == 'E')
1793         {
1794           int exp_neg = 0;
1795
1796           str++;
1797           if (*str == '-')
1798             {
1799               exp_neg = 1;
1800               str++;
1801             }
1802           else if (*str == '+')
1803             str++;
1804
1805           d = 0;
1806           while (ISDIGIT (*str))
1807             {
1808               int t = d;
1809               d *= 10;
1810               d += *str - '0';
1811               if (d < t)
1812                 {
1813                   /* Overflowed the exponent.  */
1814                   if (exp_neg)
1815                     goto underflow;
1816                   else
1817                     goto overflow;
1818                 }
1819               str++;
1820             }
1821           if (exp_neg)
1822             d = -d;
1823           exp += d;
1824         }
1825
1826       if (exp < 0)
1827         {
1828           exp = -exp;
1829           for (d = 0; d < EXP_BITS; ++d)
1830             if (exp & (1 << d))
1831               do_divide (r, r, ten_to_ptwo (d));
1832         }
1833       else if (exp > 0)
1834         {
1835           for (d = 0; d < EXP_BITS; ++d)
1836             if (exp & (1 << d))
1837               do_multiply (r, r, ten_to_ptwo (d));
1838         }
1839     }
1840
1841   return;
1842
1843  underflow:
1844   get_zero (r, r->sign);
1845   return;
1846
1847  overflow:
1848   get_inf (r, r->sign);
1849   return;
1850 }
1851
1852 /* Legacy.  Similar, but return the result directly.  */
1853
1854 REAL_VALUE_TYPE
1855 real_from_string2 (s, mode)
1856      const char *s;
1857      enum machine_mode mode;
1858 {
1859   REAL_VALUE_TYPE r;
1860
1861   real_from_string (&r, s);
1862   if (mode != VOIDmode)
1863     real_convert (&r, mode, &r);
1864
1865   return r;
1866 }
1867
1868 /* Initialize R from the integer pair HIGH+LOW.  */
1869
1870 void
1871 real_from_integer (tr, mode, low, high, unsigned_p)
1872      REAL_VALUE_TYPE *tr;
1873      enum machine_mode mode;
1874      unsigned HOST_WIDE_INT low;
1875      HOST_WIDE_INT high;
1876      int unsigned_p;
1877 {
1878   struct real_value *r = (struct real_value *) tr;
1879
1880   if (low == 0 && high == 0)
1881     get_zero (r, 0);
1882   else
1883     {
1884       r->class = rvc_normal;
1885       r->sign = high < 0 && !unsigned_p;
1886       r->exp = 2 * HOST_BITS_PER_WIDE_INT;
1887
1888       if (r->sign)
1889         {
1890           high = ~high;
1891           if (low == 0)
1892             high += 1;
1893           else
1894             low = -low;
1895         }
1896
1897       if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
1898         {
1899           r->sig[SIGSZ-1] = high;
1900           r->sig[SIGSZ-2] = low;
1901           memset (r->sig, 0, sizeof(long)*(SIGSZ-2));
1902         }
1903       else if (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT)
1904         {
1905           r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
1906           r->sig[SIGSZ-2] = high;
1907           r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
1908           r->sig[SIGSZ-4] = low;
1909           if (SIGSZ > 4)
1910             memset (r->sig, 0, sizeof(long)*(SIGSZ-4));
1911         }
1912       else
1913         abort ();
1914
1915       normalize (r);
1916     }
1917
1918   if (mode != VOIDmode)
1919     real_convert (tr, mode, tr);
1920 }
1921
1922 /* Returns 10**2**n.  */
1923
1924 static const struct real_value *
1925 ten_to_ptwo (n)
1926      int n;
1927 {
1928   static struct real_value tens[EXP_BITS];
1929
1930   if (n < 0 || n >= EXP_BITS)
1931     abort ();
1932
1933   if (tens[n].class == rvc_zero)
1934     {
1935       if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
1936         {
1937           HOST_WIDE_INT t = 10;
1938           int i;
1939
1940           for (i = 0; i < n; ++i)
1941             t *= t;
1942
1943           real_from_integer ((REAL_VALUE_TYPE *) &tens[n], VOIDmode, t, 0, 1);
1944         }
1945       else
1946         {
1947           const struct real_value *t = ten_to_ptwo (n - 1);
1948           do_multiply (&tens[n], t, t);
1949         }
1950     }
1951
1952   return &tens[n];
1953 }
1954
1955 /* Returns N.  */
1956
1957 static const struct real_value *
1958 real_digit (n)
1959      int n;
1960 {
1961   static struct real_value num[10];
1962
1963   if (n < 0 || n > 9)
1964     abort ();
1965
1966   if (n > 0 && num[n].class == rvc_zero)
1967     real_from_integer ((REAL_VALUE_TYPE *) &num[n], VOIDmode, n, 0, 1);
1968
1969   return &num[n];
1970 }
1971
1972 /* Fills R with +Inf.  */
1973
1974 void
1975 real_inf (tr)
1976      REAL_VALUE_TYPE *tr;
1977 {
1978   get_inf ((struct real_value *)tr, 0);
1979 }
1980
1981 /* Fills R with a NaN whose significand is described by STR.  If QUIET,
1982    we force a QNaN, else we force an SNaN.  The string, if not empty,
1983    is parsed as a number and placed in the significand.  Return true
1984    if the string was successfully parsed.  */
1985
1986 bool
1987 real_nan (tr, str, quiet, mode)
1988      REAL_VALUE_TYPE *tr;
1989      const char *str;
1990      int quiet;
1991      enum machine_mode mode;
1992 {
1993   struct real_value *r = (struct real_value *) tr;
1994   const struct real_format *fmt;
1995
1996   fmt = fmt_for_mode[mode - QFmode];
1997   if (fmt == NULL)
1998     abort ();
1999
2000   if (*str == 0)
2001     {
2002       if (quiet)
2003         get_canonical_qnan (r, 0);
2004       else
2005         get_canonical_snan (r, 0);
2006     }
2007   else
2008     {
2009       int base = 10, d;
2010       bool neg = false;
2011
2012       memset (r, 0, sizeof (*r));
2013       r->class = rvc_nan;
2014
2015       /* Parse akin to strtol into the significand of R.  */
2016
2017       while (ISSPACE (*str))
2018         str++;
2019       if (*str == '-')
2020         str++, neg = true;
2021       else if (*str == '+')
2022         str++;
2023       if (*str == '0')
2024         {
2025           if (*++str == 'x')
2026             str++, base = 16;
2027           else
2028             base = 8;
2029         }
2030
2031       while ((d = hex_value (*str)) < base)
2032         {
2033           struct real_value u;
2034
2035           switch (base)
2036             {
2037             case 8:
2038               lshift_significand (r, r, 3);
2039               break;
2040             case 16:
2041               lshift_significand (r, r, 4);
2042               break;
2043             case 10:
2044               lshift_significand_1 (&u, r);
2045               lshift_significand (r, r, 3);
2046               add_significands (r, r, &u);
2047               break;
2048             default:
2049               abort ();
2050             }
2051
2052           get_zero (&u, 0);
2053           u.sig[0] = d;
2054           add_significands (r, r, &u);
2055
2056           str++;
2057         }
2058
2059       /* Must have consumed the entire string for success.  */
2060       if (*str != 0)
2061         return false;
2062
2063       /* Shift the significand into place such that the bits
2064          are in the most significant bits for the format.  */
2065       lshift_significand (r, r, SIGNIFICAND_BITS - fmt->p);
2066
2067       /* Our MSB is always unset for NaNs.  */
2068       r->sig[SIGSZ-1] &= ~SIG_MSB;
2069
2070       /* Force quiet or signalling NaN.  */
2071       if (quiet)
2072         r->sig[SIGSZ-1] |= SIG_MSB >> 1;
2073       else
2074         r->sig[SIGSZ-1] &= ~(SIG_MSB >> 1);
2075
2076       /* Force at least one bit of the significand set.  */
2077       for (d = 0; d < SIGSZ; ++d)
2078         if (r->sig[d])
2079           break;
2080       if (d == SIGSZ)
2081         r->sig[SIGSZ-1] |= SIG_MSB >> 2;
2082
2083       /* Our intermediate format forces QNaNs to have MSB-1 set.
2084          If the target format has QNaNs with the top bit unset,
2085          mirror the output routines and invert the top two bits.  */
2086       if (!fmt->qnan_msb_set)
2087         r->sig[SIGSZ-1] ^= (SIG_MSB >> 1) | (SIG_MSB >> 2);
2088     }
2089
2090   return true;
2091 }
2092
2093 /* Fills R with 2**N.  */
2094
2095 void
2096 real_2expN (tr, n)
2097      REAL_VALUE_TYPE *tr;
2098      int n;
2099 {
2100   struct real_value *r = (struct real_value *) tr;
2101
2102   memset (r, 0, sizeof (*r));
2103
2104   n++;
2105   if (n > MAX_EXP)
2106     r->class = rvc_inf;
2107   else if (n < -MAX_EXP)
2108     ;
2109   else
2110     {
2111       r->class = rvc_normal;
2112       r->exp = n;
2113       r->sig[SIGSZ-1] = SIG_MSB;
2114     }
2115 }
2116
2117 \f
2118 static void
2119 round_for_format (fmt, r)
2120      const struct real_format *fmt;
2121      struct real_value *r;
2122 {
2123   int p2, np2, i, w;
2124   bool sticky, guard, lsb;
2125   int emin2m1, emax2;
2126
2127   p2 = fmt->p * fmt->log2_b;
2128   emin2m1 = (fmt->emin - 1) * fmt->log2_b;
2129   emax2 = fmt->emax * fmt->log2_b;
2130
2131   np2 = SIGNIFICAND_BITS - p2;
2132   switch (r->class)
2133     {
2134     underflow:
2135       get_zero (r, r->sign);
2136     case rvc_zero:
2137       if (!fmt->has_signed_zero)
2138         r->sign = 0;
2139       return;
2140
2141     overflow:
2142       get_inf (r, r->sign);
2143     case rvc_inf:
2144       return;
2145
2146     case rvc_nan:
2147       clear_significand_below (r, np2);
2148
2149       /* If we've cleared the entire significand, we need one bit
2150          set for this to continue to be a NaN.  */
2151       for (i = 0; i < SIGSZ; ++i)
2152         if (r->sig[i])
2153           break;
2154       if (i == SIGSZ)
2155         r->sig[SIGSZ-1] = SIG_MSB >> 2;
2156       return;
2157
2158     case rvc_normal:
2159       break;
2160
2161     default:
2162       abort ();
2163     }
2164
2165   /* If we're not base2, normalize the exponent to a multiple of
2166      the true base.  */
2167   if (fmt->log2_b != 1)
2168     {
2169       int shift = r->exp & (fmt->log2_b - 1);
2170       if (shift)
2171         {
2172           shift = fmt->log2_b - shift;
2173           sticky_rshift_significand (r, r, shift);
2174           r->exp += shift;
2175         }
2176     }
2177
2178   /* Check the range of the exponent.  If we're out of range,
2179      either underflow or overflow.  */
2180   if (r->exp > emax2)
2181     goto overflow;
2182   else if (r->exp <= emin2m1)
2183     {
2184       int diff;
2185
2186       if (!fmt->has_denorm)
2187         {
2188           /* Don't underflow completely until we've had a chance to round.  */
2189           if (r->exp < emin2m1)
2190             goto underflow;
2191         }
2192       else
2193         {
2194           diff = emin2m1 - r->exp + 1;
2195           if (diff > p2)
2196             goto underflow;
2197
2198           /* De-normalize the significand.  */
2199           sticky_rshift_significand (r, r, diff);
2200           r->exp += diff;
2201         }
2202     }
2203
2204   /* There are P2 true significand bits, followed by one guard bit,
2205      followed by one sticky bit, followed by stuff.  Fold non-zero
2206      stuff into the sticky bit.  */
2207
2208   sticky = 0;
2209   for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2210     if (r->sig[i])
2211       sticky = 1;
2212   sticky |=
2213     r->sig[w] & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2214
2215   guard = test_significand_bit (r, np2 - 1);
2216   lsb = test_significand_bit (r, np2);
2217
2218   /* Round to even.  */
2219   if (guard && (sticky || lsb))
2220     {
2221       struct real_value u;
2222       get_zero (&u, 0);
2223       set_significand_bit (&u, np2);
2224
2225       if (add_significands (r, r, &u))
2226         {
2227           /* Overflow.  Means the significand had been all ones, and
2228              is now all zeros.  Need to increase the exponent, and
2229              possibly re-normalize it.  */
2230           if (++r->exp > emax2)
2231             goto overflow;
2232           r->sig[SIGSZ-1] = SIG_MSB;
2233
2234           if (fmt->log2_b != 1)
2235             {
2236               int shift = r->exp & (fmt->log2_b - 1);
2237               if (shift)
2238                 {
2239                   shift = fmt->log2_b - shift;
2240                   sticky_rshift_significand (r, r, shift);
2241                   r->exp += shift;
2242                   if (r->exp > emax2)
2243                     goto overflow;
2244                 }
2245             }
2246         }
2247     }
2248
2249   /* Catch underflow that we deferred until after rounding.  */
2250   if (r->exp <= emin2m1)
2251     goto underflow;
2252
2253   /* Clear out trailing garbage.  */
2254   clear_significand_below (r, np2);
2255 }
2256
2257 /* Extend or truncate to a new mode.  */
2258
2259 void
2260 real_convert (tr, mode, ta)
2261      REAL_VALUE_TYPE *tr;
2262      enum machine_mode mode;
2263      const REAL_VALUE_TYPE *ta;
2264 {
2265   struct real_value *r = (struct real_value *)tr;
2266   const struct real_value *a = (const struct real_value *)ta;
2267   const struct real_format *fmt;
2268
2269   fmt = fmt_for_mode[mode - QFmode];
2270   if (fmt == NULL)
2271     abort ();
2272
2273   *r = *a;
2274   round_for_format (fmt, r);
2275
2276   /* round_for_format de-normalizes denormals.  Undo just that part.  */
2277   if (r->class == rvc_normal)
2278     normalize (r);
2279 }
2280
2281 /* Legacy.  Likewise, except return the struct directly.  */
2282
2283 REAL_VALUE_TYPE
2284 real_value_truncate (mode, a)
2285      enum machine_mode mode;
2286      REAL_VALUE_TYPE a;
2287 {
2288   REAL_VALUE_TYPE r;
2289   real_convert (&r, mode, &a);
2290   return r;
2291 }
2292
2293 /* Return true if truncating to MODE is exact.  */
2294
2295 bool
2296 exact_real_truncate (mode, ta)
2297      enum machine_mode mode;
2298      const REAL_VALUE_TYPE *ta;
2299 {
2300   REAL_VALUE_TYPE t;
2301   real_convert (&t, mode, ta);
2302   return real_identical (&t, ta);
2303 }
2304
2305 /* Write R to the target format of MODE.  Place the words of the 
2306    result in target word order in BUF.  There are always 32 bits
2307    in each long, no matter the size of the host long.
2308
2309    Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE.  */
2310
2311 long
2312 real_to_target (buf, tr, mode)
2313      long *buf;
2314      const REAL_VALUE_TYPE *tr;
2315      enum machine_mode mode;
2316 {
2317   struct real_value r;
2318   const struct real_format *fmt;
2319   long buf1;
2320
2321   r = *(const struct real_value *) tr;
2322
2323   fmt = fmt_for_mode[mode - QFmode];
2324   if (fmt == NULL)
2325     abort ();
2326
2327   round_for_format (fmt, &r);
2328   if (!buf)
2329     buf = &buf1;
2330   (*fmt->encode) (fmt, buf, &r);
2331
2332   return *buf;
2333 }
2334
2335 /* Read R from the target format of MODE.  Read the words of the
2336    result in target word order in BUF.  There are always 32 bits
2337    in each long, no matter the size of the host long.  */
2338
2339 void
2340 real_from_target (tr, buf, mode)
2341      REAL_VALUE_TYPE *tr;
2342      const long *buf;
2343      enum machine_mode mode;
2344 {
2345   struct real_value *r = (struct real_value *) tr;
2346   const struct real_format *fmt;
2347
2348   fmt = fmt_for_mode[mode - QFmode];
2349   if (fmt == NULL)
2350     abort ();
2351
2352   (*fmt->decode) (fmt, r, buf);
2353 }     
2354
2355 /* Return the number of bits in the significand for MODE.  */
2356 /* ??? Legacy.  Should get access to real_format directly.  */
2357
2358 int
2359 significand_size (mode)
2360      enum machine_mode mode;
2361 {
2362   const struct real_format *fmt;
2363
2364   fmt = fmt_for_mode[mode - QFmode];
2365   if (fmt == NULL)
2366     return 0;
2367
2368   return fmt->p * fmt->log2_b;
2369 }
2370 \f
2371 /* IEEE single-precision format.  */
2372
2373 static void encode_ieee_single PARAMS ((const struct real_format *fmt,
2374                                         long *, const struct real_value *));
2375 static void decode_ieee_single PARAMS ((const struct real_format *,
2376                                         struct real_value *, const long *));
2377
2378 static void
2379 encode_ieee_single (fmt, buf, r)
2380      const struct real_format *fmt;
2381      long *buf;
2382      const struct real_value *r;
2383 {
2384   unsigned long image, sig, exp;
2385   bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2386
2387   image = r->sign << 31;
2388   sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2389
2390   switch (r->class)
2391     {
2392     case rvc_zero:
2393       break;
2394
2395     case rvc_inf:
2396       if (fmt->has_inf)
2397         image |= 255 << 23;
2398       else
2399         image |= 0x7fffffff;
2400       break;
2401
2402     case rvc_nan:
2403       if (fmt->has_nans)
2404         {
2405           image |= 255 << 23;
2406           image |= sig;
2407           if (!fmt->qnan_msb_set)
2408             image ^= 1 << 23 | 1 << 22;
2409         }
2410       else
2411         image |= 0x7fffffff;
2412       break;
2413
2414     case rvc_normal:
2415       /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2416          whereas the intermediate representation is 0.F x 2**exp.
2417          Which means we're off by one.  */
2418       if (denormal)
2419         exp = 0;
2420       else
2421       exp = r->exp + 127 - 1;
2422       image |= exp << 23;
2423       image |= sig;
2424       break;
2425     }
2426
2427   buf[0] = image;
2428 }
2429
2430 static void
2431 decode_ieee_single (fmt, r, buf)
2432      const struct real_format *fmt;
2433      struct real_value *r;
2434      const long *buf;
2435 {
2436   unsigned long image = buf[0] & 0xffffffff;
2437   bool sign = (image >> 31) & 1;
2438   int exp = (image >> 23) & 0xff;
2439
2440   memset (r, 0, sizeof (*r));
2441   image <<= HOST_BITS_PER_LONG - 24;
2442   image &= ~SIG_MSB;
2443
2444   if (exp == 0)
2445     {
2446       if (image && fmt->has_denorm)
2447         {
2448           r->class = rvc_normal;
2449           r->sign = sign;
2450           r->exp = -126;
2451           r->sig[SIGSZ-1] = image << 1;
2452           normalize (r);
2453         }
2454       else if (fmt->has_signed_zero)
2455         r->sign = sign;
2456     }
2457   else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2458     {
2459       if (image)
2460         {
2461           r->class = rvc_nan;
2462           r->sign = sign;
2463           if (!fmt->qnan_msb_set)
2464             image ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2465           r->sig[SIGSZ-1] = image;
2466         }
2467       else
2468         {
2469           r->class = rvc_inf;
2470           r->sign = sign;
2471         }
2472     }
2473   else
2474     {
2475       r->class = rvc_normal;
2476       r->sign = sign;
2477       r->exp = exp - 127 + 1;
2478       r->sig[SIGSZ-1] = image | SIG_MSB;
2479     }
2480 }
2481
2482 const struct real_format ieee_single = 
2483   {
2484     encode_ieee_single,
2485     decode_ieee_single,
2486     2,
2487     1,
2488     24,
2489     -125,
2490     128,
2491     true,
2492     true,
2493     true,
2494     true,
2495     true
2496   };
2497
2498 \f
2499 /* IEEE double-precision format.  */
2500
2501 static void encode_ieee_double PARAMS ((const struct real_format *fmt,
2502                                         long *, const struct real_value *));
2503 static void decode_ieee_double PARAMS ((const struct real_format *,
2504                                         struct real_value *, const long *));
2505
2506 static void
2507 encode_ieee_double (fmt, buf, r)
2508      const struct real_format *fmt;
2509      long *buf;
2510      const struct real_value *r;
2511 {
2512   unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
2513   bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2514
2515   image_hi = r->sign << 31;
2516   image_lo = 0;
2517
2518   if (HOST_BITS_PER_LONG == 64)
2519     {
2520       sig_hi = r->sig[SIGSZ-1];
2521       sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
2522       sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
2523     }
2524   else
2525     {
2526       sig_hi = r->sig[SIGSZ-1];
2527       sig_lo = r->sig[SIGSZ-2];
2528       sig_lo = (sig_hi << 21) | (sig_lo >> 11);
2529       sig_hi = (sig_hi >> 11) & 0xfffff;
2530     }
2531
2532   switch (r->class)
2533     {
2534     case rvc_zero:
2535       break;
2536
2537     case rvc_inf:
2538       if (fmt->has_inf)
2539         image_hi |= 2047 << 20;
2540       else
2541         {
2542           image_hi |= 0x7fffffff;
2543           image_lo = 0xffffffff;
2544         }
2545       break;
2546
2547     case rvc_nan:
2548       if (fmt->has_nans)
2549         {
2550           image_hi |= 2047 << 20;
2551           image_hi |= sig_hi;
2552           if (!fmt->qnan_msb_set)
2553             image_hi ^= 1 << 19 | 1 << 18;
2554           image_lo = sig_lo;
2555         }
2556       else
2557         {
2558           image_hi |= 0x7fffffff;
2559           image_lo = 0xffffffff;
2560         }
2561       break;
2562
2563     case rvc_normal:
2564       /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2565          whereas the intermediate representation is 0.F x 2**exp.
2566          Which means we're off by one.  */
2567       if (denormal)
2568         exp = 0;
2569       else
2570         exp = r->exp + 1023 - 1;
2571       image_hi |= exp << 20;
2572       image_hi |= sig_hi;
2573       image_lo = sig_lo;
2574       break;
2575     }
2576
2577   if (FLOAT_WORDS_BIG_ENDIAN)
2578     buf[0] = image_hi, buf[1] = image_lo;
2579   else
2580     buf[0] = image_lo, buf[1] = image_hi;
2581 }
2582
2583 static void
2584 decode_ieee_double (fmt, r, buf)
2585      const struct real_format *fmt;
2586      struct real_value *r;
2587      const long *buf;
2588 {
2589   unsigned long image_hi, image_lo;
2590   bool sign;
2591   int exp;
2592
2593   if (FLOAT_WORDS_BIG_ENDIAN)
2594     image_hi = buf[0], image_lo = buf[1];
2595   else
2596     image_lo = buf[0], image_hi = buf[1];
2597   image_lo &= 0xffffffff;
2598   image_hi &= 0xffffffff;
2599
2600   sign = (image_hi >> 31) & 1;
2601   exp = (image_hi >> 20) & 0x7ff;
2602
2603   memset (r, 0, sizeof (*r));
2604
2605   image_hi <<= 32 - 21;
2606   image_hi |= image_lo >> 21;
2607   image_hi &= 0x7fffffff;
2608   image_lo <<= 32 - 21;
2609
2610   if (exp == 0)
2611     {
2612       if ((image_hi || image_lo) && fmt->has_denorm)
2613         {
2614           r->class = rvc_normal;
2615           r->sign = sign;
2616           r->exp = -1022;
2617           if (HOST_BITS_PER_LONG == 32)
2618             {
2619               image_hi = (image_hi << 1) | (image_lo >> 31);
2620               image_lo <<= 1;
2621               r->sig[SIGSZ-1] = image_hi;
2622               r->sig[SIGSZ-2] = image_lo;
2623             }
2624           else
2625             {
2626               image_hi = (image_hi << 31 << 2) | (image_lo << 1);
2627               r->sig[SIGSZ-1] = image_hi;
2628             }
2629           normalize (r);
2630         }
2631       else if (fmt->has_signed_zero)
2632         r->sign = sign;
2633     }
2634   else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
2635     {
2636       if (image_hi || image_lo)
2637         {
2638           r->class = rvc_nan;
2639           r->sign = sign;
2640           if (HOST_BITS_PER_LONG == 32)
2641             {
2642               r->sig[SIGSZ-1] = image_hi;
2643               r->sig[SIGSZ-2] = image_lo;
2644             }
2645           else
2646             r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
2647
2648           if (!fmt->qnan_msb_set)
2649             r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2650         }
2651       else
2652         {
2653           r->class = rvc_inf;
2654           r->sign = sign;
2655         }
2656     }
2657   else
2658     {
2659       r->class = rvc_normal;
2660       r->sign = sign;
2661       r->exp = exp - 1023 + 1;
2662       if (HOST_BITS_PER_LONG == 32)
2663         {
2664           r->sig[SIGSZ-1] = image_hi | SIG_MSB;
2665           r->sig[SIGSZ-2] = image_lo;
2666         }
2667       else
2668         r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
2669     }
2670 }
2671
2672 const struct real_format ieee_double = 
2673   {
2674     encode_ieee_double,
2675     decode_ieee_double,
2676     2,
2677     1,
2678     53,
2679     -1021,
2680     1024,
2681     true,
2682     true,
2683     true,
2684     true,
2685     true
2686   };
2687
2688 \f
2689 /* IEEE extended double precision format.  This comes in three
2690    flavours: Intel's as a 12 byte image, Intel's as a 16 byte image,
2691    and Motorola's.  */
2692
2693 static void encode_ieee_extended PARAMS ((const struct real_format *fmt,
2694                                           long *, const struct real_value *));
2695 static void decode_ieee_extended PARAMS ((const struct real_format *,
2696                                           struct real_value *, const long *));
2697
2698 static void encode_ieee_extended_128 PARAMS ((const struct real_format *fmt,
2699                                               long *,
2700                                               const struct real_value *));
2701 static void decode_ieee_extended_128 PARAMS ((const struct real_format *,
2702                                               struct real_value *,
2703                                               const long *));
2704
2705 static void
2706 encode_ieee_extended (fmt, buf, r)
2707      const struct real_format *fmt;
2708      long *buf;
2709      const struct real_value *r;
2710 {
2711   unsigned long image_hi, sig_hi, sig_lo;
2712   bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2713
2714   image_hi = r->sign << 15;
2715   sig_hi = sig_lo = 0;
2716
2717   switch (r->class)
2718     {
2719     case rvc_zero:
2720       break;
2721
2722     case rvc_inf:
2723       if (fmt->has_inf)
2724         {
2725           image_hi |= 32767;
2726
2727           /* Intel requires the explicit integer bit to be set, otherwise
2728              it considers the value a "pseudo-infinity".  Motorola docs
2729              say it doesn't care.  */
2730           sig_hi = 0x80000000;
2731         }
2732       else
2733         {
2734           image_hi |= 32767;
2735           sig_lo = sig_hi = 0xffffffff;
2736         }
2737       break;
2738
2739     case rvc_nan:
2740       if (fmt->has_nans)
2741         {
2742           image_hi |= 32767;
2743           if (HOST_BITS_PER_LONG == 32)
2744             {
2745               sig_hi = r->sig[SIGSZ-1];
2746               sig_lo = r->sig[SIGSZ-2];
2747             }
2748           else
2749             {
2750               sig_lo = r->sig[SIGSZ-1];
2751               sig_hi = sig_lo >> 31 >> 1;
2752               sig_lo &= 0xffffffff;
2753             }
2754           if (!fmt->qnan_msb_set)
2755             sig_hi ^= 1 << 30 | 1 << 29;
2756
2757           /* Intel requires the explicit integer bit to be set, otherwise
2758              it considers the value a "pseudo-nan".  Motorola docs say it
2759              doesn't care.  */
2760           sig_hi |= 0x80000000;
2761         }
2762       else
2763         {
2764           image_hi |= 32767;
2765           sig_lo = sig_hi = 0xffffffff;
2766         }
2767       break;
2768
2769     case rvc_normal:
2770       {
2771         int exp = r->exp;
2772
2773         /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2774            whereas the intermediate representation is 0.F x 2**exp.
2775            Which means we're off by one. 
2776
2777            Except for Motorola, which consider exp=0 and explicit
2778            integer bit set to continue to be normalized.  In theory
2779            this descrepency has been taken care of by the difference
2780            in fmt->emin in round_for_format.  */
2781
2782         if (denormal)
2783           exp = 0;
2784         else
2785           {
2786             exp += 16383 - 1;
2787             if (exp < 0)
2788               abort ();
2789           }
2790         image_hi |= exp;
2791
2792         if (HOST_BITS_PER_LONG == 32)
2793           {
2794             sig_hi = r->sig[SIGSZ-1];
2795             sig_lo = r->sig[SIGSZ-2];
2796           }
2797         else
2798           {
2799             sig_lo = r->sig[SIGSZ-1];
2800             sig_hi = sig_lo >> 31 >> 1;
2801             sig_lo &= 0xffffffff;
2802           }
2803       }
2804       break;
2805     }
2806
2807   if (FLOAT_WORDS_BIG_ENDIAN)
2808     buf[0] = image_hi << 16, buf[1] = sig_hi, buf[2] = sig_lo;
2809   else
2810     buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
2811 }
2812
2813 static void
2814 encode_ieee_extended_128 (fmt, buf, r)
2815      const struct real_format *fmt;
2816      long *buf;
2817      const struct real_value *r;
2818 {
2819   buf[3 * !FLOAT_WORDS_BIG_ENDIAN] = 0;
2820   encode_ieee_extended (fmt, buf+!!FLOAT_WORDS_BIG_ENDIAN, r);
2821 }
2822
2823 static void
2824 decode_ieee_extended (fmt, r, buf)
2825      const struct real_format *fmt;
2826      struct real_value *r;
2827      const long *buf;
2828 {
2829   unsigned long image_hi, sig_hi, sig_lo;
2830   bool sign;
2831   int exp;
2832
2833   if (FLOAT_WORDS_BIG_ENDIAN)
2834     image_hi = buf[0] >> 16, sig_hi = buf[1], sig_lo = buf[2];
2835   else
2836     sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
2837   sig_lo &= 0xffffffff;
2838   sig_hi &= 0xffffffff;
2839   image_hi &= 0xffffffff;
2840
2841   sign = (image_hi >> 15) & 1;
2842   exp = image_hi & 0x7fff;
2843
2844   memset (r, 0, sizeof (*r));
2845
2846   if (exp == 0)
2847     {
2848       if ((sig_hi || sig_lo) && fmt->has_denorm)
2849         {
2850           r->class = rvc_normal;
2851           r->sign = sign;
2852
2853           /* When the IEEE format contains a hidden bit, we know that
2854              it's zero at this point, and so shift up the significand
2855              and decrease the exponent to match.  In this case, Motorola
2856              defines the explicit integer bit to be valid, so we don't
2857              know whether the msb is set or not.  */
2858           r->exp = fmt->emin;
2859           if (HOST_BITS_PER_LONG == 32)
2860             {
2861               r->sig[SIGSZ-1] = sig_hi;
2862               r->sig[SIGSZ-2] = sig_lo;
2863             }
2864           else
2865             r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
2866
2867           normalize (r);
2868         }
2869       else if (fmt->has_signed_zero)
2870         r->sign = sign;
2871     }
2872   else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
2873     {
2874       /* See above re "pseudo-infinities" and "pseudo-nans".
2875          Short summary is that the MSB will likely always be
2876          set, and that we don't care about it.  */
2877       sig_hi &= 0x7fffffff;
2878
2879       if (sig_hi || sig_lo)
2880         {
2881           r->class = rvc_nan;
2882           r->sign = sign;
2883           if (HOST_BITS_PER_LONG == 32)
2884             {
2885               r->sig[SIGSZ-1] = sig_hi;
2886               r->sig[SIGSZ-2] = sig_lo;
2887             }
2888           else
2889             r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
2890
2891           if (!fmt->qnan_msb_set)
2892             r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2893         }
2894       else
2895         {
2896           r->class = rvc_inf;
2897           r->sign = sign;
2898         }
2899     }
2900   else
2901     {
2902       r->class = rvc_normal;
2903       r->sign = sign;
2904       r->exp = exp - 16383 + 1;
2905       if (HOST_BITS_PER_LONG == 32)
2906         {
2907           r->sig[SIGSZ-1] = sig_hi;
2908           r->sig[SIGSZ-2] = sig_lo;
2909         }
2910       else
2911         r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
2912     }
2913 }
2914
2915 static void
2916 decode_ieee_extended_128 (fmt, r, buf)
2917      const struct real_format *fmt;
2918      struct real_value *r;
2919      const long *buf;
2920 {
2921   decode_ieee_extended (fmt, r, buf+!!FLOAT_WORDS_BIG_ENDIAN);
2922 }
2923
2924 const struct real_format ieee_extended_motorola = 
2925   {
2926     encode_ieee_extended,
2927     decode_ieee_extended,
2928     2,
2929     1,
2930     64,
2931     -16382,
2932     16384,
2933     true,
2934     true,
2935     true,
2936     true,
2937     true
2938   };
2939
2940 const struct real_format ieee_extended_intel_96 = 
2941   {
2942     encode_ieee_extended,
2943     decode_ieee_extended,
2944     2,
2945     1,
2946     64,
2947     -16381,
2948     16384,
2949     true,
2950     true,
2951     true,
2952     true,
2953     true
2954   };
2955
2956 const struct real_format ieee_extended_intel_128 = 
2957   {
2958     encode_ieee_extended_128,
2959     decode_ieee_extended_128,
2960     2,
2961     1,
2962     64,
2963     -16381,
2964     16384,
2965     true,
2966     true,
2967     true,
2968     true,
2969     true
2970   };
2971
2972 \f
2973 /* IEEE quad precision format.  */
2974
2975 static void encode_ieee_quad PARAMS ((const struct real_format *fmt,
2976                                       long *, const struct real_value *));
2977 static void decode_ieee_quad PARAMS ((const struct real_format *,
2978                                       struct real_value *, const long *));
2979
2980 static void
2981 encode_ieee_quad (fmt, buf, r)
2982      const struct real_format *fmt;
2983      long *buf;
2984      const struct real_value *r;
2985 {
2986   unsigned long image3, image2, image1, image0, exp;
2987   bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2988   struct real_value u;
2989
2990   image3 = r->sign << 31;
2991   image2 = 0;
2992   image1 = 0;
2993   image0 = 0;
2994
2995   rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
2996
2997   switch (r->class)
2998     {
2999     case rvc_zero:
3000       break;
3001
3002     case rvc_inf:
3003       if (fmt->has_inf)
3004         image3 |= 32767 << 16;
3005       else
3006         {
3007           image3 |= 0x7fffffff;
3008           image2 = 0xffffffff;
3009           image1 = 0xffffffff;
3010           image0 = 0xffffffff;
3011         }
3012       break;
3013
3014     case rvc_nan:
3015       if (fmt->has_nans)
3016         {
3017           image3 |= 32767 << 16;
3018
3019           if (HOST_BITS_PER_LONG == 32)
3020             {
3021               image0 = u.sig[0];
3022               image1 = u.sig[1];
3023               image2 = u.sig[2];
3024               image3 |= u.sig[3] & 0xffff;
3025             }
3026           else
3027             {
3028               image0 = u.sig[0];
3029               image1 = image0 >> 31 >> 1;
3030               image2 = u.sig[1];
3031               image3 |= (image2 >> 31 >> 1) & 0xffff;
3032               image0 &= 0xffffffff;
3033               image2 &= 0xffffffff;
3034             }
3035
3036           if (!fmt->qnan_msb_set)
3037             image3 ^= 1 << 15 | 1 << 14;
3038         }
3039       else
3040         {
3041           image3 |= 0x7fffffff;
3042           image2 = 0xffffffff;
3043           image1 = 0xffffffff;
3044           image0 = 0xffffffff;
3045         }
3046       break;
3047
3048     case rvc_normal:
3049       /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3050          whereas the intermediate representation is 0.F x 2**exp.
3051          Which means we're off by one.  */
3052       if (denormal)
3053         exp = 0;
3054       else
3055         exp = r->exp + 16383 - 1;
3056       image3 |= exp << 16;
3057
3058       if (HOST_BITS_PER_LONG == 32)
3059         {
3060           image0 = u.sig[0];
3061           image1 = u.sig[1];
3062           image2 = u.sig[2];
3063           image3 |= u.sig[3] & 0xffff;
3064         }
3065       else
3066         {
3067           image0 = u.sig[0];
3068           image1 = image0 >> 31 >> 1;
3069           image2 = u.sig[1];
3070           image3 |= (image2 >> 31 >> 1) & 0xffff;
3071           image0 &= 0xffffffff;
3072           image2 &= 0xffffffff;
3073         }
3074       break;
3075     }
3076
3077   if (FLOAT_WORDS_BIG_ENDIAN)
3078     {
3079       buf[0] = image3;
3080       buf[1] = image2;
3081       buf[2] = image1;
3082       buf[3] = image0;
3083     }
3084   else
3085     {
3086       buf[0] = image0;
3087       buf[1] = image1;
3088       buf[2] = image2;
3089       buf[3] = image3;
3090     }
3091 }
3092
3093 static void
3094 decode_ieee_quad (fmt, r, buf)
3095      const struct real_format *fmt;
3096      struct real_value *r;
3097      const long *buf;
3098 {
3099   unsigned long image3, image2, image1, image0;
3100   bool sign;
3101   int exp;
3102
3103   if (FLOAT_WORDS_BIG_ENDIAN)
3104     {
3105       image3 = buf[0];
3106       image2 = buf[1];
3107       image1 = buf[2];
3108       image0 = buf[3];
3109     }
3110   else
3111     {
3112       image0 = buf[0];
3113       image1 = buf[1];
3114       image2 = buf[2];
3115       image3 = buf[3];
3116     }
3117   image0 &= 0xffffffff;
3118   image1 &= 0xffffffff;
3119   image2 &= 0xffffffff;
3120
3121   sign = (image3 >> 31) & 1;
3122   exp = (image3 >> 16) & 0x7fff;
3123   image3 &= 0xffff;
3124
3125   memset (r, 0, sizeof (*r));
3126
3127   if (exp == 0)
3128     {
3129       if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3130         {
3131           r->class = rvc_normal;
3132           r->sign = sign;
3133
3134           r->exp = -16382 + (SIGNIFICAND_BITS - 112);
3135           if (HOST_BITS_PER_LONG == 32)
3136             {
3137               r->sig[0] = image0;
3138               r->sig[1] = image1;
3139               r->sig[2] = image2;
3140               r->sig[3] = image3;
3141             }
3142           else
3143             {
3144               r->sig[0] = (image1 << 31 << 1) | image0;
3145               r->sig[1] = (image3 << 31 << 1) | image2;
3146             }
3147
3148           normalize (r);
3149         }
3150       else if (fmt->has_signed_zero)
3151         r->sign = sign;
3152     }
3153   else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3154     {
3155       if (image3 | image2 | image1 | image0)
3156         {
3157           r->class = rvc_nan;
3158           r->sign = sign;
3159
3160           if (HOST_BITS_PER_LONG == 32)
3161             {
3162               r->sig[0] = image0;
3163               r->sig[1] = image1;
3164               r->sig[2] = image2;
3165               r->sig[3] = image3;
3166             }
3167           else
3168             {
3169               r->sig[0] = (image1 << 31 << 1) | image0;
3170               r->sig[1] = (image3 << 31 << 1) | image2;
3171             }
3172           lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3173
3174           if (!fmt->qnan_msb_set)
3175             r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
3176         }
3177       else
3178         {
3179           r->class = rvc_inf;
3180           r->sign = sign;
3181         }
3182     }
3183   else
3184     {
3185       r->class = rvc_normal;
3186       r->sign = sign;
3187       r->exp = exp - 16383 + 1;
3188
3189       if (HOST_BITS_PER_LONG == 32)
3190         {
3191           r->sig[0] = image0;
3192           r->sig[1] = image1;
3193           r->sig[2] = image2;
3194           r->sig[3] = image3;
3195         }
3196       else
3197         {
3198           r->sig[0] = (image1 << 31 << 1) | image0;
3199           r->sig[1] = (image3 << 31 << 1) | image2;
3200         }
3201       lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3202       r->sig[SIGSZ-1] |= SIG_MSB;
3203     }
3204 }
3205
3206 const struct real_format ieee_quad = 
3207   {
3208     encode_ieee_quad,
3209     decode_ieee_quad,
3210     2,
3211     1,
3212     113,
3213     -16382,
3214     16384,
3215     true,
3216     true,
3217     true,
3218     true,
3219     true
3220   };
3221
3222 \f
3223 /* The VAX floating point formats.  */
3224
3225 static void encode_vax_f PARAMS ((const struct real_format *fmt,
3226                                   long *, const struct real_value *));
3227 static void decode_vax_f PARAMS ((const struct real_format *,
3228                                   struct real_value *, const long *));
3229 static void encode_vax_d PARAMS ((const struct real_format *fmt,
3230                                   long *, const struct real_value *));
3231 static void decode_vax_d PARAMS ((const struct real_format *,
3232                                   struct real_value *, const long *));
3233 static void encode_vax_g PARAMS ((const struct real_format *fmt,
3234                                   long *, const struct real_value *));
3235 static void decode_vax_g PARAMS ((const struct real_format *,
3236                                   struct real_value *, const long *));
3237
3238 static void
3239 encode_vax_f (fmt, buf, r)
3240      const struct real_format *fmt ATTRIBUTE_UNUSED;
3241      long *buf;
3242      const struct real_value *r;
3243 {
3244   unsigned long sign, exp, sig, image;
3245
3246   sign = r->sign << 15;
3247
3248   switch (r->class)
3249     {
3250     case rvc_zero:
3251       image = 0;
3252       break;
3253
3254     case rvc_inf:
3255     case rvc_nan:
3256       image = 0xffff7fff | sign;
3257       break;
3258
3259     case rvc_normal:
3260       sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3261       exp = r->exp + 128;
3262
3263       image = (sig << 16) & 0xffff0000;
3264       image |= sign;
3265       image |= exp << 7;
3266       image |= sig >> 16;
3267       break;
3268     }
3269
3270   buf[0] = image;
3271 }
3272
3273 static void
3274 decode_vax_f (fmt, r, buf)
3275      const struct real_format *fmt ATTRIBUTE_UNUSED;
3276      struct real_value *r;
3277      const long *buf;
3278 {
3279   unsigned long image = buf[0] & 0xffffffff;
3280   int exp = (image >> 7) & 0xff;
3281
3282   memset (r, 0, sizeof (*r));
3283
3284   if (exp != 0)
3285     {
3286       r->class = rvc_normal;
3287       r->sign = (image >> 15) & 1;
3288       r->exp = exp - 128;
3289
3290       image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
3291       r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3292     }
3293 }
3294
3295 static void
3296 encode_vax_d (fmt, buf, r)
3297      const struct real_format *fmt ATTRIBUTE_UNUSED;
3298      long *buf;
3299      const struct real_value *r;
3300 {
3301   unsigned long image0, image1, sign = r->sign << 15;
3302
3303   switch (r->class)
3304     {
3305     case rvc_zero:
3306       image0 = image1 = 0;
3307       break;
3308
3309     case rvc_inf:
3310     case rvc_nan:
3311       image0 = 0xffff7fff | sign;
3312       image1 = 0xffffffff;
3313       break;
3314
3315     case rvc_normal:
3316       /* Extract the significand into straight hi:lo.  */
3317       if (HOST_BITS_PER_LONG == 64)
3318         {
3319           image0 = r->sig[SIGSZ-1];
3320           image1 = (image0 >> (64 - 56)) & 0xffffffff;
3321           image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
3322         }
3323       else
3324         {
3325           image0 = r->sig[SIGSZ-1];
3326           image1 = r->sig[SIGSZ-2];
3327           image1 = (image0 << 24) | (image1 >> 8);
3328           image0 = (image0 >> 8) & 0xffffff;
3329         }
3330
3331       /* Rearrange the half-words of the significand to match the
3332          external format.  */
3333       image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
3334       image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3335
3336       /* Add the sign and exponent.  */
3337       image0 |= sign;
3338       image0 |= (r->exp + 128) << 7;
3339       break;
3340     }
3341
3342   if (FLOAT_WORDS_BIG_ENDIAN)
3343     buf[0] = image1, buf[1] = image0;
3344   else
3345     buf[0] = image0, buf[1] = image1;
3346 }
3347
3348 static void
3349 decode_vax_d (fmt, r, buf)
3350      const struct real_format *fmt ATTRIBUTE_UNUSED;
3351      struct real_value *r;
3352      const long *buf;
3353 {
3354   unsigned long image0, image1;
3355   int exp;
3356
3357   if (FLOAT_WORDS_BIG_ENDIAN)
3358     image1 = buf[0], image0 = buf[1];
3359   else
3360     image0 = buf[0], image1 = buf[1];
3361   image0 &= 0xffffffff;
3362   image1 &= 0xffffffff;
3363
3364   exp = (image0 >> 7) & 0x7f;
3365
3366   memset (r, 0, sizeof (*r));
3367
3368   if (exp != 0)
3369     {
3370       r->class = rvc_normal;
3371       r->sign = (image0 >> 15) & 1;
3372       r->exp = exp - 128;
3373
3374       /* Rearrange the half-words of the external format into
3375          proper ascending order.  */
3376       image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
3377       image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3378
3379       if (HOST_BITS_PER_LONG == 64)
3380         {
3381           image0 = (image0 << 31 << 1) | image1;
3382           image0 <<= 64 - 56;
3383           image0 |= SIG_MSB;
3384           r->sig[SIGSZ-1] = image0;
3385         }
3386       else
3387         {
3388           r->sig[SIGSZ-1] = image0;
3389           r->sig[SIGSZ-2] = image1;
3390           lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
3391           r->sig[SIGSZ-1] |= SIG_MSB;
3392         }
3393     }
3394 }
3395
3396 static void
3397 encode_vax_g (fmt, buf, r)
3398      const struct real_format *fmt ATTRIBUTE_UNUSED;
3399      long *buf;
3400      const struct real_value *r;
3401 {
3402   unsigned long image0, image1, sign = r->sign << 15;
3403
3404   switch (r->class)
3405     {
3406     case rvc_zero:
3407       image0 = image1 = 0;
3408       break;
3409
3410     case rvc_inf:
3411     case rvc_nan:
3412       image0 = 0xffff7fff | sign;
3413       image1 = 0xffffffff;
3414       break;
3415
3416     case rvc_normal:
3417       /* Extract the significand into straight hi:lo.  */
3418       if (HOST_BITS_PER_LONG == 64)
3419         {
3420           image0 = r->sig[SIGSZ-1];
3421           image1 = (image0 >> (64 - 53)) & 0xffffffff;
3422           image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
3423         }
3424       else
3425         {
3426           image0 = r->sig[SIGSZ-1];
3427           image1 = r->sig[SIGSZ-2];
3428           image1 = (image0 << 21) | (image1 >> 11);
3429           image0 = (image0 >> 11) & 0xfffff;
3430         }
3431
3432       /* Rearrange the half-words of the significand to match the
3433          external format.  */
3434       image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
3435       image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3436
3437       /* Add the sign and exponent.  */
3438       image0 |= sign;
3439       image0 |= (r->exp + 1024) << 4;
3440       break;
3441     }
3442
3443   if (FLOAT_WORDS_BIG_ENDIAN)
3444     buf[0] = image1, buf[1] = image0;
3445   else
3446     buf[0] = image0, buf[1] = image1;
3447 }
3448
3449 static void
3450 decode_vax_g (fmt, r, buf)
3451      const struct real_format *fmt ATTRIBUTE_UNUSED;
3452      struct real_value *r;
3453      const long *buf;
3454 {
3455   unsigned long image0, image1;
3456   int exp;
3457
3458   if (FLOAT_WORDS_BIG_ENDIAN)
3459     image1 = buf[0], image0 = buf[1];
3460   else
3461     image0 = buf[0], image1 = buf[1];
3462   image0 &= 0xffffffff;
3463   image1 &= 0xffffffff;
3464
3465   exp = (image0 >> 4) & 0x7ff;
3466
3467   memset (r, 0, sizeof (*r));
3468
3469   if (exp != 0)
3470     {
3471       r->class = rvc_normal;
3472       r->sign = (image0 >> 15) & 1;
3473       r->exp = exp - 1024;
3474
3475       /* Rearrange the half-words of the external format into
3476          proper ascending order.  */
3477       image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
3478       image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3479
3480       if (HOST_BITS_PER_LONG == 64)
3481         {
3482           image0 = (image0 << 31 << 1) | image1;
3483           image0 <<= 64 - 53;
3484           image0 |= SIG_MSB;
3485           r->sig[SIGSZ-1] = image0;
3486         }
3487       else
3488         {
3489           r->sig[SIGSZ-1] = image0;
3490           r->sig[SIGSZ-2] = image1;
3491           lshift_significand (r, r, 64 - 53);
3492           r->sig[SIGSZ-1] |= SIG_MSB;
3493         }
3494     }
3495 }
3496
3497 const struct real_format vax_f_format = 
3498   {
3499     encode_vax_f,
3500     decode_vax_f,
3501     2,
3502     1,
3503     24,
3504     -127,
3505     127,
3506     false,
3507     false,
3508     false,
3509     false,
3510     false
3511   };
3512
3513 const struct real_format vax_d_format = 
3514   {
3515     encode_vax_d,
3516     decode_vax_d,
3517     2,
3518     1,
3519     56,
3520     -127,
3521     127,
3522     false,
3523     false,
3524     false,
3525     false,
3526     false
3527   };
3528
3529 const struct real_format vax_g_format = 
3530   {
3531     encode_vax_g,
3532     decode_vax_g,
3533     2,
3534     1,
3535     53,
3536     -1023,
3537     1023,
3538     false,
3539     false,
3540     false,
3541     false,
3542     false
3543   };
3544
3545 \f
3546 /* The IBM S/390 floating point formats.  A good reference for these can
3547    be found in chapter 9 of "ESA/390 Principles of Operation", IBM document
3548    number SA22-7201-01.  An on-line version can be found here:
3549
3550    http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
3551 */
3552
3553 static void encode_i370_single PARAMS ((const struct real_format *fmt,
3554                                         long *, const struct real_value *));
3555 static void decode_i370_single PARAMS ((const struct real_format *,
3556                                         struct real_value *, const long *));
3557 static void encode_i370_double PARAMS ((const struct real_format *fmt,
3558                                         long *, const struct real_value *));
3559 static void decode_i370_double PARAMS ((const struct real_format *,
3560                                         struct real_value *, const long *));
3561
3562 static void
3563 encode_i370_single (fmt, buf, r)
3564      const struct real_format *fmt ATTRIBUTE_UNUSED;
3565      long *buf;
3566      const struct real_value *r;
3567 {
3568   unsigned long sign, exp, sig, image;
3569
3570   sign = r->sign << 31;
3571
3572   switch (r->class)
3573     {
3574     case rvc_zero:
3575       image = 0;
3576       break;
3577
3578     case rvc_inf:
3579     case rvc_nan:
3580       image = 0x7fffffff | sign;
3581       break;
3582
3583     case rvc_normal:
3584       sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0xffffff;
3585       exp = ((r->exp / 4) + 64) << 24;
3586       image = sign | exp | sig;
3587       break;
3588     }
3589
3590   buf[0] = image;
3591 }
3592
3593 static void
3594 decode_i370_single (fmt, r, buf)
3595      const struct real_format *fmt ATTRIBUTE_UNUSED;
3596      struct real_value *r;
3597      const long *buf;
3598 {
3599   unsigned long sign, sig, image = buf[0];
3600   int exp;
3601
3602   sign = (image >> 31) & 1;
3603   exp = (image >> 24) & 0x7f;
3604   sig = image & 0xffffff;
3605
3606   memset (r, 0, sizeof (*r));
3607
3608   if (exp || sig)
3609     {
3610       r->class = rvc_normal;
3611       r->sign = sign;
3612       r->exp = (exp - 64) * 4;
3613       r->sig[SIGSZ-1] = sig << (HOST_BITS_PER_LONG - 24);
3614       normalize (r);
3615     }
3616 }
3617
3618 static void
3619 encode_i370_double (fmt, buf, r)
3620      const struct real_format *fmt ATTRIBUTE_UNUSED;
3621      long *buf;
3622      const struct real_value *r;
3623 {
3624   unsigned long sign, exp, image_hi, image_lo;
3625
3626   sign = r->sign << 31;
3627
3628   switch (r->class)
3629     {
3630     case rvc_zero:
3631       image_hi = image_lo = 0;
3632       break;
3633
3634     case rvc_inf:
3635     case rvc_nan:
3636       image_hi = 0x7fffffff | sign;
3637       image_lo = 0xffffffff;
3638       break;
3639
3640     case rvc_normal:
3641       if (HOST_BITS_PER_LONG == 64)
3642         {
3643           image_hi = r->sig[SIGSZ-1];
3644           image_lo = (image_hi >> (64 - 56)) & 0xffffffff;
3645           image_hi = (image_hi >> (64 - 56 + 1) >> 31) & 0xffffff;
3646         }
3647       else
3648         {
3649           image_hi = r->sig[SIGSZ-1];
3650           image_lo = r->sig[SIGSZ-2];
3651           image_lo = (image_lo >> 8) | (image_hi << 24);
3652           image_hi >>= 8;
3653         }
3654
3655       exp = ((r->exp / 4) + 64) << 24;
3656       image_hi |= sign | exp;
3657       break;
3658     }
3659
3660   if (FLOAT_WORDS_BIG_ENDIAN)
3661     buf[0] = image_hi, buf[1] = image_lo;
3662   else
3663     buf[0] = image_lo, buf[1] = image_hi;
3664 }
3665
3666 static void
3667 decode_i370_double (fmt, r, buf)
3668      const struct real_format *fmt ATTRIBUTE_UNUSED;
3669      struct real_value *r;
3670      const long *buf;
3671 {
3672   unsigned long sign, image_hi, image_lo;
3673   int exp;
3674
3675   if (FLOAT_WORDS_BIG_ENDIAN)
3676     image_hi = buf[0], image_lo = buf[1];
3677   else
3678     image_lo = buf[0], image_hi = buf[1];
3679
3680   sign = (image_hi >> 31) & 1;
3681   exp = (image_hi >> 24) & 0x7f;
3682   image_hi &= 0xffffff;
3683   image_lo &= 0xffffffff;
3684
3685   memset (r, 0, sizeof (*r));
3686
3687   if (exp || image_hi || image_lo)
3688     {
3689       r->class = rvc_normal;
3690       r->sign = sign;
3691       r->exp = (exp - 64) * 4 + (SIGNIFICAND_BITS - 56);
3692
3693       if (HOST_BITS_PER_LONG == 32)
3694         {
3695           r->sig[0] = image_lo;
3696           r->sig[1] = image_hi;
3697         }
3698       else
3699         r->sig[0] = image_lo | (image_hi << 31 << 1);
3700
3701       normalize (r);
3702     }
3703 }
3704
3705 const struct real_format i370_single =
3706   {
3707     encode_i370_single,
3708     decode_i370_single,
3709     16,
3710     4,
3711     6,
3712     -64,
3713     63,
3714     false,
3715     false,
3716     false, /* ??? The encoding does allow for "unnormals".  */
3717     false, /* ??? The encoding does allow for "unnormals".  */
3718     false
3719   };
3720
3721 const struct real_format i370_double =
3722   {
3723     encode_i370_double,
3724     decode_i370_double,
3725     16,
3726     4,
3727     14,
3728     -64,
3729     63,
3730     false,
3731     false,
3732     false, /* ??? The encoding does allow for "unnormals".  */
3733     false, /* ??? The encoding does allow for "unnormals".  */
3734     false
3735   };
3736
3737 \f
3738 /* TMS320C[34]x twos complement floating point format.  */
3739
3740 static void encode_c4x_single PARAMS ((const struct real_format *fmt,
3741                                        long *, const struct real_value *));
3742 static void decode_c4x_single PARAMS ((const struct real_format *,
3743                                        struct real_value *, const long *));
3744 static void encode_c4x_extended PARAMS ((const struct real_format *fmt,
3745                                          long *, const struct real_value *));
3746 static void decode_c4x_extended PARAMS ((const struct real_format *,
3747                                          struct real_value *, const long *));
3748
3749 static void
3750 encode_c4x_single (fmt, buf, r)
3751      const struct real_format *fmt ATTRIBUTE_UNUSED;
3752      long *buf;
3753      const struct real_value *r;
3754 {
3755   unsigned long image, exp, sig;
3756   
3757   switch (r->class)
3758     {
3759     case rvc_zero:
3760       exp = -128;
3761       sig = 0;
3762       break;
3763
3764     case rvc_inf:
3765     case rvc_nan:
3766       exp = 127;
3767       sig = 0x800000 - r->sign;
3768       break;
3769
3770     case rvc_normal:
3771       exp = r->exp - 1;
3772       sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3773       if (r->sign)
3774         {
3775           if (sig)
3776             sig = -sig;
3777           else
3778             exp--;
3779           sig |= 0x800000;
3780         }
3781       break;
3782     }
3783
3784   image = ((exp & 0xff) << 24) | (sig & 0xffffff);
3785   buf[0] = image;
3786 }
3787
3788 static void
3789 decode_c4x_single (fmt, r, buf)
3790      const struct real_format *fmt ATTRIBUTE_UNUSED;
3791      struct real_value *r;
3792      const long *buf;
3793 {
3794   unsigned long image = buf[0];
3795   unsigned long sig;
3796   int exp, sf;
3797
3798   exp = (((image >> 24) & 0xff) ^ 0x80) - 0x80;
3799   sf = ((image & 0xffffff) ^ 0x800000) - 0x800000;
3800
3801   memset (r, 0, sizeof (*r));
3802
3803   if (exp != -128)
3804     {
3805       r->class = rvc_normal;
3806
3807       sig = sf & 0x7fffff;
3808       if (sf < 0)
3809         {
3810           r->sign = 1;
3811           if (sig)
3812             sig = -sig;
3813           else
3814             exp++;
3815         }
3816       sig = (sig << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3817
3818       r->exp = exp + 1;
3819       r->sig[SIGSZ-1] = sig;
3820     }
3821 }
3822
3823 static void
3824 encode_c4x_extended (fmt, buf, r)
3825      const struct real_format *fmt ATTRIBUTE_UNUSED;
3826      long *buf;
3827      const struct real_value *r;
3828 {
3829   unsigned long exp, sig;
3830   
3831   switch (r->class)
3832     {
3833     case rvc_zero:
3834       exp = -128;
3835       sig = 0;
3836       break;
3837
3838     case rvc_inf:
3839     case rvc_nan:
3840       exp = 127;
3841       sig = 0x80000000 - r->sign;
3842       break;
3843
3844     case rvc_normal:
3845       exp = r->exp - 1;
3846
3847       sig = r->sig[SIGSZ-1];
3848       if (HOST_BITS_PER_LONG == 64)
3849         sig = sig >> 1 >> 31;
3850       sig &= 0x7fffffff;
3851
3852       if (r->sign)
3853         {
3854           if (sig)
3855             sig = -sig;
3856           else
3857             exp--;
3858           sig |= 0x80000000;
3859         }
3860       break;
3861     }
3862
3863   exp = (exp & 0xff) << 24;
3864   sig &= 0xffffffff;
3865
3866   if (FLOAT_WORDS_BIG_ENDIAN)
3867     buf[0] = exp, buf[1] = sig;
3868   else
3869     buf[0] = sig, buf[0] = exp;
3870 }
3871
3872 static void
3873 decode_c4x_extended (fmt, r, buf)
3874      const struct real_format *fmt ATTRIBUTE_UNUSED;
3875      struct real_value *r;
3876      const long *buf;
3877 {
3878   unsigned long sig;
3879   int exp, sf;
3880
3881   if (FLOAT_WORDS_BIG_ENDIAN)
3882     exp = buf[0], sf = buf[1];
3883   else
3884     sf = buf[0], exp = buf[1];
3885
3886   exp = (((exp >> 24) & 0xff) & 0x80) - 0x80;
3887   sf = ((sf & 0xffffffff) ^ 0x80000000) - 0x80000000;
3888
3889   memset (r, 0, sizeof (*r));
3890
3891   if (exp != -128)
3892     {
3893       r->class = rvc_normal;
3894
3895       sig = sf & 0x7fffffff;
3896       if (sf < 0)
3897         {
3898           r->sign = 1;
3899           if (sig)
3900             sig = -sig;
3901           else
3902             exp++;
3903         }
3904       if (HOST_BITS_PER_LONG == 64)
3905         sig = sig << 1 << 31;
3906       sig |= SIG_MSB;
3907
3908       r->exp = exp + 1;
3909       r->sig[SIGSZ-1] = sig;
3910     }
3911 }
3912
3913 const struct real_format c4x_single = 
3914   {
3915     encode_c4x_single,
3916     decode_c4x_single,
3917     2,
3918     1,
3919     24,
3920     -126,
3921     128,
3922     false,
3923     false,
3924     false,
3925     false,
3926     false
3927   };
3928
3929 const struct real_format c4x_extended = 
3930   {
3931     encode_c4x_extended,
3932     decode_c4x_extended,
3933     2,
3934     1,
3935     32,
3936     -126,
3937     128,
3938     false,
3939     false,
3940     false,
3941     false,
3942     false
3943   };
3944
3945 \f
3946 /* Initialize things at start of compilation.  */
3947
3948 static const struct real_format * format_for_size PARAMS ((int));
3949
3950 static const struct real_format *
3951 format_for_size (size)
3952      int size;
3953 {
3954 #ifndef TARGET_G_FORMAT
3955 #define TARGET_G_FORMAT 0
3956 #endif
3957
3958   switch (TARGET_FLOAT_FORMAT)
3959     {
3960     case IEEE_FLOAT_FORMAT:
3961       switch (size)
3962         {
3963         case 32:
3964           return &ieee_single;
3965
3966         case 64:
3967           return &ieee_double;
3968
3969         case 96:
3970           if (!INTEL_EXTENDED_IEEE_FORMAT)
3971             return &ieee_extended_motorola;
3972           else
3973             return &ieee_extended_intel_96;
3974
3975         case 128:
3976           if (!INTEL_EXTENDED_IEEE_FORMAT)
3977             return &ieee_quad;
3978           else
3979             return &ieee_extended_intel_128;
3980         }
3981       break;
3982
3983     case VAX_FLOAT_FORMAT:
3984       switch (size)
3985         {
3986         case 32:
3987           return &vax_f_format;
3988
3989         case 64:
3990           if (TARGET_G_FORMAT)
3991             return &vax_g_format;
3992           else
3993             return &vax_d_format;
3994         }
3995       break;
3996
3997     case IBM_FLOAT_FORMAT:
3998       switch (size)
3999         {
4000         case 32:
4001           return &i370_single;
4002         case 64:
4003           return &i370_double;
4004         }
4005       break;
4006
4007     case C4X_FLOAT_FORMAT:
4008       switch (size)
4009         {
4010         case 32:
4011           return &c4x_single;
4012         case 64:
4013           return &c4x_extended;
4014         }
4015       break;
4016     }
4017
4018   abort ();
4019 }
4020
4021 void
4022 init_real_once ()
4023 {
4024   int i;
4025
4026   /* Set up the mode->format table.  */
4027   for (i = 0; i < 3; ++i)
4028     {
4029       enum machine_mode mode;
4030       int size;
4031
4032       if (i == 0)
4033         size = FLOAT_TYPE_SIZE;
4034       else if (i == 1)
4035         size = DOUBLE_TYPE_SIZE;
4036       else
4037         size = LONG_DOUBLE_TYPE_SIZE;
4038
4039       mode = mode_for_size (size, MODE_FLOAT, 0);
4040       if (mode == BLKmode)
4041         abort ();
4042
4043       fmt_for_mode[mode - QFmode] = format_for_size (size);
4044     }
4045 }