OSDN Git Service

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