OSDN Git Service

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