OSDN Git Service

* real.c (real_to_decimal): Crop trailing zeros for DIGITS < 0.
[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 significant
1392    digits in the result.  If DIGITS <= 0, choose the maximum for the
1393    representation.  If DIGITS < 0, strip trailing zeros.  */
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   bool crop_trailing_zeros;
1409
1410   r = *r_orig;
1411   switch (r.class)
1412     {
1413     case rvc_zero:
1414       strcpy (str, (r.sign ? "-0.0" : "0.0"));
1415       return;
1416     case rvc_normal:
1417       break;
1418     case rvc_inf:
1419       strcpy (str, (r.sign ? "+Inf" : "-Inf"));
1420       return;
1421     case rvc_nan:
1422       /* ??? Print the significand as well, if not canonical?  */
1423       strcpy (str, (r.sign ? "+NaN" : "-NaN"));
1424       return;
1425     default:
1426       abort ();
1427     }
1428
1429   max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1430   crop_trailing_zeros = digits < 0;
1431   if (digits <= 0 || digits > max_digits)
1432     digits = max_digits;
1433
1434   one = real_digit (1);
1435   ten = ten_to_ptwo (0);
1436
1437   sign = r.sign;
1438   r.sign = 0;
1439
1440   /* Estimate the decimal exponent.  */
1441   dec_exp = r.exp * M_LOG10_2;
1442   
1443   /* Scale the number such that it is in [1, 10).  */
1444   if (dec_exp > 0)
1445     {
1446       int i;
1447       for (i = EXP_BITS - 1; i >= 0; --i)
1448         if (dec_exp & (1 << i))
1449           do_divide (&r, &r, ten_to_ptwo (i));
1450     }
1451   else if (dec_exp < 0)
1452     {
1453       int i, pos_exp = -(--dec_exp);
1454
1455       for (i = EXP_BITS - 1; i >= 0; --i)
1456         if (pos_exp & (1 << i))
1457           do_multiply (&r, &r, ten_to_ptwo (i));
1458     }
1459
1460   /* Assert that the number is in the proper range.  Round-off can
1461      prevent the above from working exactly.  */
1462   if (do_compare (&r, one, -1) < 0)
1463     {
1464       do_multiply (&r, &r, ten);
1465       dec_exp--;
1466     }
1467   else if (do_compare (&r, ten, 1) >= 0)
1468     {
1469       do_divide (&r, &r, ten);
1470       dec_exp++;
1471     }
1472
1473   p = str;
1474   if (sign)
1475     *p++ = '-';
1476   first = p++;
1477   while (1)
1478     {
1479       d = real_to_integer ((const REAL_VALUE_TYPE *) &r);
1480       do_add (&r, &r, real_digit (d), 1);
1481
1482       *p++ = d + '0';
1483       if (--digits == 0)
1484         break;
1485       do_multiply (&r, &r, ten);
1486     }
1487   last = p;
1488
1489   /* Round the result.  Compare R vs 0.5 by doing R*2 vs 1.0.  */
1490   r.exp += 1;
1491   cmp_half = do_compare (&r, one, -1);
1492   if (cmp_half == 0)
1493     /* Round to even.  */
1494     cmp_half += d & 1;
1495   if (cmp_half > 0)
1496     {
1497       while (p > first)
1498         {
1499           d = *--p;
1500           if (d == '9')
1501             *p = '0';
1502           else
1503             {
1504               *p = d + 1;
1505               break;
1506             }
1507         }
1508
1509       if (p == first)
1510         {
1511           first[1] = '1';
1512           dec_exp++;
1513         }
1514     }
1515   
1516   first[0] = first[1];
1517   first[1] = '.';
1518
1519   if (crop_trailing_zeros)
1520     while (last > first + 3 && last[-1] == '0')
1521       last--;
1522
1523   sprintf (last, "e%+d", dec_exp);
1524 }
1525
1526 /* Render R as a hexadecimal floating point constant.  Emit DIGITS
1527    significant digits in the result.  If DIGITS <= 0, choose the maximum
1528    for the representation.  If DIGITS < 0, strip trailing zeros.  */
1529
1530 void
1531 real_to_hexadecimal (str, r, digits)
1532      char *str;
1533      const REAL_VALUE_TYPE *r;
1534      int digits;
1535 {
1536   int i, j, exp = r->exp;
1537   char *p, *first;
1538   bool crop_trailing_zeros;
1539
1540   switch (r->class)
1541     {
1542     case rvc_zero:
1543       exp = 0;
1544       break;
1545     case rvc_normal:
1546       break;
1547     case rvc_inf:
1548       strcpy (str, (r->sign ? "+Inf" : "-Inf"));
1549       return;
1550     case rvc_nan:
1551       /* ??? Print the significand as well, if not canonical?  */
1552       strcpy (str, (r->sign ? "+NaN" : "-NaN"));
1553       return;
1554     default:
1555       abort ();
1556     }
1557
1558   crop_trailing_zeros = digits < 0;
1559   if (digits <= 0)
1560     digits = SIGNIFICAND_BITS / 4;
1561
1562   p = str;
1563   if (r->sign)
1564     *p++ = '-';
1565   *p++ = '0';
1566   *p++ = 'x';
1567   *p++ = '0';
1568   *p++ = '.';
1569   first = p;
1570
1571   for (i = SIGSZ - 1; i >= 0; --i)
1572     for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1573       {
1574         *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1575         if (--digits == 0)
1576           goto out;
1577       }
1578
1579  out:
1580   if (crop_trailing_zeros)
1581     while (p > first + 2 && p[-1] == '0')
1582       p--;
1583
1584   sprintf (p, "p%+d", exp);
1585 }
1586
1587 /* Initialize R from a decimal or hexadecimal string.  The string is
1588    assumed to have been syntax checked already.  */
1589
1590 void
1591 real_from_string (r, str)
1592      REAL_VALUE_TYPE *r;
1593      const char *str;
1594 {
1595   int exp = 0;
1596   bool sign = false;
1597
1598   get_zero (r, 0);
1599
1600   if (*str == '-')
1601     {
1602       sign = true;
1603       str++;
1604     }
1605   else if (*str == '+')
1606     str++;
1607
1608   if (str[0] == '0' && str[1] == 'x')
1609     {
1610       /* Hexadecimal floating point.  */
1611       int pos = SIGNIFICAND_BITS - 4, d;
1612
1613       str += 2;
1614
1615       while (*str == '0')
1616         str++;
1617       while (1)
1618         {
1619           d = hex_value (*str);
1620           if (d == _hex_bad)
1621             break;
1622           if (pos >= 0)
1623             {
1624               r->sig[pos / HOST_BITS_PER_LONG]
1625                 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1626               pos -= 4;
1627             }
1628           exp += 4;
1629           str++;
1630         }
1631       if (*str == '.')
1632         {
1633           str++;
1634           if (pos == SIGNIFICAND_BITS - 4)
1635             {
1636               while (*str == '0')
1637                 str++, exp -= 4;
1638             }
1639           while (1)
1640             {
1641               d = hex_value (*str);
1642               if (d == _hex_bad)
1643                 break;
1644               if (pos >= 0)
1645                 {
1646                   r->sig[pos / HOST_BITS_PER_LONG]
1647                     |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1648                   pos -= 4;
1649                 }
1650               str++;
1651             }
1652         }
1653       if (*str == 'p' || *str == 'P')
1654         {
1655           bool exp_neg = false;
1656
1657           str++;
1658           if (*str == '-')
1659             {
1660               exp_neg = true;
1661               str++;
1662             }
1663           else if (*str == '+')
1664             str++;
1665
1666           d = 0;
1667           while (ISDIGIT (*str))
1668             {
1669               d *= 10;
1670               d += *str - '0';
1671               if (d > MAX_EXP)
1672                 {
1673                   /* Overflowed the exponent.  */
1674                   if (exp_neg)
1675                     goto underflow;
1676                   else
1677                     goto overflow;
1678                 }
1679               str++;
1680             }
1681           if (exp_neg)
1682             d = -d;
1683
1684           exp += d;
1685         }
1686
1687       r->class = rvc_normal;
1688       r->exp = exp;
1689
1690       normalize (r);
1691     }
1692   else
1693     {
1694       /* Decimal floating point.  */
1695       const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
1696       int d;
1697
1698       while (*str == '0')
1699         str++;
1700       while (ISDIGIT (*str))
1701         {
1702           d = *str++ - '0';
1703           do_multiply (r, r, ten);
1704           if (d)
1705             do_add (r, r, real_digit (d), 0);
1706         }
1707       if (*str == '.')
1708         {
1709           str++;
1710           if (r->class == rvc_zero)
1711             {
1712               while (*str == '0')
1713                 str++, exp--;
1714             }
1715           while (ISDIGIT (*str))
1716             {
1717               d = *str++ - '0';
1718               do_multiply (r, r, ten);
1719               if (d)
1720                 do_add (r, r, real_digit (d), 0);
1721               exp--;
1722             }
1723         }
1724
1725       if (*str == 'e' || *str == 'E')
1726         {
1727           bool exp_neg = false;
1728
1729           str++;
1730           if (*str == '-')
1731             {
1732               exp_neg = true;
1733               str++;
1734             }
1735           else if (*str == '+')
1736             str++;
1737
1738           d = 0;
1739           while (ISDIGIT (*str))
1740             {
1741               d *= 10;
1742               d += *str - '0';
1743               if (d > MAX_EXP)
1744                 {
1745                   /* Overflowed the exponent.  */
1746                   if (exp_neg)
1747                     goto underflow;
1748                   else
1749                     goto overflow;
1750                 }
1751               str++;
1752             }
1753           if (exp_neg)
1754             d = -d;
1755           exp += d;
1756         }
1757
1758       if (exp < 0)
1759         {
1760           exp = -exp;
1761           for (d = 0; d < EXP_BITS; ++d)
1762             if (exp & (1 << d))
1763               do_divide (r, r, ten_to_ptwo (d));
1764         }
1765       else if (exp > 0)
1766         {
1767           for (d = 0; d < EXP_BITS; ++d)
1768             if (exp & (1 << d))
1769               do_multiply (r, r, ten_to_ptwo (d));
1770         }
1771     }
1772
1773   r->sign = sign;
1774   return;
1775
1776  underflow:
1777   get_zero (r, sign);
1778   return;
1779
1780  overflow:
1781   get_inf (r, sign);
1782   return;
1783 }
1784
1785 /* Legacy.  Similar, but return the result directly.  */
1786
1787 REAL_VALUE_TYPE
1788 real_from_string2 (s, mode)
1789      const char *s;
1790      enum machine_mode mode;
1791 {
1792   REAL_VALUE_TYPE r;
1793
1794   real_from_string (&r, s);
1795   if (mode != VOIDmode)
1796     real_convert (&r, mode, &r);
1797
1798   return r;
1799 }
1800
1801 /* Initialize R from the integer pair HIGH+LOW.  */
1802
1803 void
1804 real_from_integer (r, mode, low, high, unsigned_p)
1805      REAL_VALUE_TYPE *r;
1806      enum machine_mode mode;
1807      unsigned HOST_WIDE_INT low;
1808      HOST_WIDE_INT high;
1809      int unsigned_p;
1810 {
1811   if (low == 0 && high == 0)
1812     get_zero (r, 0);
1813   else
1814     {
1815       r->class = rvc_normal;
1816       r->sign = high < 0 && !unsigned_p;
1817       r->exp = 2 * HOST_BITS_PER_WIDE_INT;
1818
1819       if (r->sign)
1820         {
1821           high = ~high;
1822           if (low == 0)
1823             high += 1;
1824           else
1825             low = -low;
1826         }
1827
1828       if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
1829         {
1830           r->sig[SIGSZ-1] = high;
1831           r->sig[SIGSZ-2] = low;
1832           memset (r->sig, 0, sizeof(long)*(SIGSZ-2));
1833         }
1834       else if (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT)
1835         {
1836           r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
1837           r->sig[SIGSZ-2] = high;
1838           r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
1839           r->sig[SIGSZ-4] = low;
1840           if (SIGSZ > 4)
1841             memset (r->sig, 0, sizeof(long)*(SIGSZ-4));
1842         }
1843       else
1844         abort ();
1845
1846       normalize (r);
1847     }
1848
1849   if (mode != VOIDmode)
1850     real_convert (r, mode, r);
1851 }
1852
1853 /* Returns 10**2**n.  */
1854
1855 static const REAL_VALUE_TYPE *
1856 ten_to_ptwo (n)
1857      int n;
1858 {
1859   static REAL_VALUE_TYPE tens[EXP_BITS];
1860
1861   if (n < 0 || n >= EXP_BITS)
1862     abort ();
1863
1864   if (tens[n].class == rvc_zero)
1865     {
1866       if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
1867         {
1868           HOST_WIDE_INT t = 10;
1869           int i;
1870
1871           for (i = 0; i < n; ++i)
1872             t *= t;
1873
1874           real_from_integer (&tens[n], VOIDmode, t, 0, 1);
1875         }
1876       else
1877         {
1878           const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
1879           do_multiply (&tens[n], t, t);
1880         }
1881     }
1882
1883   return &tens[n];
1884 }
1885
1886 /* Returns N.  */
1887
1888 static const REAL_VALUE_TYPE *
1889 real_digit (n)
1890      int n;
1891 {
1892   static REAL_VALUE_TYPE num[10];
1893
1894   if (n < 0 || n > 9)
1895     abort ();
1896
1897   if (n > 0 && num[n].class == rvc_zero)
1898     real_from_integer (&num[n], VOIDmode, n, 0, 1);
1899
1900   return &num[n];
1901 }
1902
1903 /* Fills R with +Inf.  */
1904
1905 void
1906 real_inf (r)
1907      REAL_VALUE_TYPE *r;
1908 {
1909   get_inf (r, 0);
1910 }
1911
1912 /* Fills R with a NaN whose significand is described by STR.  If QUIET,
1913    we force a QNaN, else we force an SNaN.  The string, if not empty,
1914    is parsed as a number and placed in the significand.  Return true
1915    if the string was successfully parsed.  */
1916
1917 bool
1918 real_nan (r, str, quiet, mode)
1919      REAL_VALUE_TYPE *r;
1920      const char *str;
1921      int quiet;
1922      enum machine_mode mode;
1923 {
1924   const struct real_format *fmt;
1925
1926   fmt = real_format_for_mode[mode - QFmode];
1927   if (fmt == NULL)
1928     abort ();
1929
1930   if (*str == 0)
1931     {
1932       if (quiet)
1933         get_canonical_qnan (r, 0);
1934       else
1935         get_canonical_snan (r, 0);
1936     }
1937   else
1938     {
1939       int base = 10, d;
1940       bool neg = false;
1941
1942       memset (r, 0, sizeof (*r));
1943       r->class = rvc_nan;
1944
1945       /* Parse akin to strtol into the significand of R.  */
1946
1947       while (ISSPACE (*str))
1948         str++;
1949       if (*str == '-')
1950         str++, neg = true;
1951       else if (*str == '+')
1952         str++;
1953       if (*str == '0')
1954         {
1955           if (*++str == 'x')
1956             str++, base = 16;
1957           else
1958             base = 8;
1959         }
1960
1961       while ((d = hex_value (*str)) < base)
1962         {
1963           REAL_VALUE_TYPE u;
1964
1965           switch (base)
1966             {
1967             case 8:
1968               lshift_significand (r, r, 3);
1969               break;
1970             case 16:
1971               lshift_significand (r, r, 4);
1972               break;
1973             case 10:
1974               lshift_significand_1 (&u, r);
1975               lshift_significand (r, r, 3);
1976               add_significands (r, r, &u);
1977               break;
1978             default:
1979               abort ();
1980             }
1981
1982           get_zero (&u, 0);
1983           u.sig[0] = d;
1984           add_significands (r, r, &u);
1985
1986           str++;
1987         }
1988
1989       /* Must have consumed the entire string for success.  */
1990       if (*str != 0)
1991         return false;
1992
1993       /* Shift the significand into place such that the bits
1994          are in the most significant bits for the format.  */
1995       lshift_significand (r, r, SIGNIFICAND_BITS - fmt->p);
1996
1997       /* Our MSB is always unset for NaNs.  */
1998       r->sig[SIGSZ-1] &= ~SIG_MSB;
1999
2000       /* Force quiet or signalling NaN.  */
2001       if (quiet)
2002         r->sig[SIGSZ-1] |= SIG_MSB >> 1;
2003       else
2004         r->sig[SIGSZ-1] &= ~(SIG_MSB >> 1);
2005
2006       /* Force at least one bit of the significand set.  */
2007       for (d = 0; d < SIGSZ; ++d)
2008         if (r->sig[d])
2009           break;
2010       if (d == SIGSZ)
2011         r->sig[SIGSZ-1] |= SIG_MSB >> 2;
2012
2013       /* Our intermediate format forces QNaNs to have MSB-1 set.
2014          If the target format has QNaNs with the top bit unset,
2015          mirror the output routines and invert the top two bits.  */
2016       if (!fmt->qnan_msb_set)
2017         r->sig[SIGSZ-1] ^= (SIG_MSB >> 1) | (SIG_MSB >> 2);
2018     }
2019
2020   return true;
2021 }
2022
2023 /* Fills R with 2**N.  */
2024
2025 void
2026 real_2expN (r, n)
2027      REAL_VALUE_TYPE *r;
2028      int n;
2029 {
2030   memset (r, 0, sizeof (*r));
2031
2032   n++;
2033   if (n > MAX_EXP)
2034     r->class = rvc_inf;
2035   else if (n < -MAX_EXP)
2036     ;
2037   else
2038     {
2039       r->class = rvc_normal;
2040       r->exp = n;
2041       r->sig[SIGSZ-1] = SIG_MSB;
2042     }
2043 }
2044
2045 \f
2046 static void
2047 round_for_format (fmt, r)
2048      const struct real_format *fmt;
2049      REAL_VALUE_TYPE *r;
2050 {
2051   int p2, np2, i, w;
2052   unsigned long sticky;
2053   bool guard, lsb;
2054   int emin2m1, emax2;
2055
2056   p2 = fmt->p * fmt->log2_b;
2057   emin2m1 = (fmt->emin - 1) * fmt->log2_b;
2058   emax2 = fmt->emax * fmt->log2_b;
2059
2060   np2 = SIGNIFICAND_BITS - p2;
2061   switch (r->class)
2062     {
2063     underflow:
2064       get_zero (r, r->sign);
2065     case rvc_zero:
2066       if (!fmt->has_signed_zero)
2067         r->sign = 0;
2068       return;
2069
2070     overflow:
2071       get_inf (r, r->sign);
2072     case rvc_inf:
2073       return;
2074
2075     case rvc_nan:
2076       clear_significand_below (r, np2);
2077
2078       /* If we've cleared the entire significand, we need one bit
2079          set for this to continue to be a NaN.  */
2080       for (i = 0; i < SIGSZ; ++i)
2081         if (r->sig[i])
2082           break;
2083       if (i == SIGSZ)
2084         r->sig[SIGSZ-1] = SIG_MSB >> 2;
2085       return;
2086
2087     case rvc_normal:
2088       break;
2089
2090     default:
2091       abort ();
2092     }
2093
2094   /* If we're not base2, normalize the exponent to a multiple of
2095      the true base.  */
2096   if (fmt->log2_b != 1)
2097     {
2098       int shift = r->exp & (fmt->log2_b - 1);
2099       if (shift)
2100         {
2101           shift = fmt->log2_b - shift;
2102           sticky_rshift_significand (r, r, shift);
2103           r->exp += shift;
2104         }
2105     }
2106
2107   /* Check the range of the exponent.  If we're out of range,
2108      either underflow or overflow.  */
2109   if (r->exp > emax2)
2110     goto overflow;
2111   else if (r->exp <= emin2m1)
2112     {
2113       int diff;
2114
2115       if (!fmt->has_denorm)
2116         {
2117           /* Don't underflow completely until we've had a chance to round.  */
2118           if (r->exp < emin2m1)
2119             goto underflow;
2120         }
2121       else
2122         {
2123           diff = emin2m1 - r->exp + 1;
2124           if (diff > p2)
2125             goto underflow;
2126
2127           /* De-normalize the significand.  */
2128           sticky_rshift_significand (r, r, diff);
2129           r->exp += diff;
2130         }
2131     }
2132
2133   /* There are P2 true significand bits, followed by one guard bit,
2134      followed by one sticky bit, followed by stuff.  Fold nonzero
2135      stuff into the sticky bit.  */
2136
2137   sticky = 0;
2138   for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2139     sticky |= r->sig[i];
2140   sticky |=
2141     r->sig[w] & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2142
2143   guard = test_significand_bit (r, np2 - 1);
2144   lsb = test_significand_bit (r, np2);
2145
2146   /* Round to even.  */
2147   if (guard && (sticky || lsb))
2148     {
2149       REAL_VALUE_TYPE u;
2150       get_zero (&u, 0);
2151       set_significand_bit (&u, np2);
2152
2153       if (add_significands (r, r, &u))
2154         {
2155           /* Overflow.  Means the significand had been all ones, and
2156              is now all zeros.  Need to increase the exponent, and
2157              possibly re-normalize it.  */
2158           if (++r->exp > emax2)
2159             goto overflow;
2160           r->sig[SIGSZ-1] = SIG_MSB;
2161
2162           if (fmt->log2_b != 1)
2163             {
2164               int shift = r->exp & (fmt->log2_b - 1);
2165               if (shift)
2166                 {
2167                   shift = fmt->log2_b - shift;
2168                   sticky_rshift_significand (r, r, shift);
2169                   r->exp += shift;
2170                   if (r->exp > emax2)
2171                     goto overflow;
2172                 }
2173             }
2174         }
2175     }
2176
2177   /* Catch underflow that we deferred until after rounding.  */
2178   if (r->exp <= emin2m1)
2179     goto underflow;
2180
2181   /* Clear out trailing garbage.  */
2182   clear_significand_below (r, np2);
2183 }
2184
2185 /* Extend or truncate to a new mode.  */
2186
2187 void
2188 real_convert (r, mode, a)
2189      REAL_VALUE_TYPE *r;
2190      enum machine_mode mode;
2191      const REAL_VALUE_TYPE *a;
2192 {
2193   const struct real_format *fmt;
2194
2195   fmt = real_format_for_mode[mode - QFmode];
2196   if (fmt == NULL)
2197     abort ();
2198
2199   *r = *a;
2200   round_for_format (fmt, r);
2201
2202   /* round_for_format de-normalizes denormals.  Undo just that part.  */
2203   if (r->class == rvc_normal)
2204     normalize (r);
2205 }
2206
2207 /* Legacy.  Likewise, except return the struct directly.  */
2208
2209 REAL_VALUE_TYPE
2210 real_value_truncate (mode, a)
2211      enum machine_mode mode;
2212      REAL_VALUE_TYPE a;
2213 {
2214   REAL_VALUE_TYPE r;
2215   real_convert (&r, mode, &a);
2216   return r;
2217 }
2218
2219 /* Return true if truncating to MODE is exact.  */
2220
2221 bool
2222 exact_real_truncate (mode, a)
2223      enum machine_mode mode;
2224      const REAL_VALUE_TYPE *a;
2225 {
2226   REAL_VALUE_TYPE t;
2227   real_convert (&t, mode, a);
2228   return real_identical (&t, a);
2229 }
2230
2231 /* Write R to the given target format.  Place the words of the result
2232    in target word order in BUF.  There are always 32 bits in each
2233    long, no matter the size of the host long.
2234
2235    Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE.  */
2236
2237 long
2238 real_to_target_fmt (buf, r_orig, fmt)
2239      long *buf;
2240      const REAL_VALUE_TYPE *r_orig;
2241      const struct real_format *fmt;
2242 {
2243   REAL_VALUE_TYPE r;
2244   long buf1;
2245
2246   r = *r_orig;
2247   round_for_format (fmt, &r);
2248
2249   if (!buf)
2250     buf = &buf1;
2251   (*fmt->encode) (fmt, buf, &r);
2252
2253   return *buf;
2254 }
2255
2256 /* Similar, but look up the format from MODE.  */
2257
2258 long
2259 real_to_target (buf, r, mode)
2260      long *buf;
2261      const REAL_VALUE_TYPE *r;
2262      enum machine_mode mode;
2263 {
2264   const struct real_format *fmt;
2265
2266   fmt = real_format_for_mode[mode - QFmode];
2267   if (fmt == NULL)
2268     abort ();
2269
2270   return real_to_target_fmt (buf, r, fmt);
2271 }
2272
2273 /* Read R from the given target format.  Read the words of the result
2274    in target word order in BUF.  There are always 32 bits in each
2275    long, no matter the size of the host long.  */
2276
2277 void
2278 real_from_target_fmt (r, buf, fmt)
2279      REAL_VALUE_TYPE *r;
2280      const long *buf;
2281      const struct real_format *fmt;
2282 {
2283   (*fmt->decode) (fmt, r, buf);
2284 }     
2285
2286 /* Similar, but look up the format from MODE.  */
2287
2288 void
2289 real_from_target (r, buf, mode)
2290      REAL_VALUE_TYPE *r;
2291      const long *buf;
2292      enum machine_mode mode;
2293 {
2294   const struct real_format *fmt;
2295
2296   fmt = real_format_for_mode[mode - QFmode];
2297   if (fmt == NULL)
2298     abort ();
2299
2300   (*fmt->decode) (fmt, r, buf);
2301 }     
2302
2303 /* Return the number of bits in the significand for MODE.  */
2304 /* ??? Legacy.  Should get access to real_format directly.  */
2305
2306 int
2307 significand_size (mode)
2308      enum machine_mode mode;
2309 {
2310   const struct real_format *fmt;
2311
2312   fmt = real_format_for_mode[mode - QFmode];
2313   if (fmt == NULL)
2314     return 0;
2315
2316   return fmt->p * fmt->log2_b;
2317 }
2318
2319 /* Return a hash value for the given real value.  */
2320 /* ??? The "unsigned int" return value is intended to be hashval_t,
2321    but I didn't want to pull hashtab.h into real.h.  */
2322
2323 unsigned int
2324 real_hash (r)
2325      const REAL_VALUE_TYPE *r;
2326 {
2327   unsigned int h;
2328   size_t i;
2329
2330   h = r->class | (r->sign << 2);
2331   switch (r->class)
2332     {
2333     case rvc_zero:
2334     case rvc_inf:
2335       break;
2336
2337     case rvc_normal:
2338       h |= r->exp << 3;
2339       /* FALLTHRU */
2340
2341     case rvc_nan:
2342       if (sizeof(unsigned long) > sizeof(unsigned int))
2343         for (i = 0; i < SIGSZ; ++i)
2344           {
2345             unsigned long s = r->sig[i];
2346             h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2347           }
2348       else
2349         for (i = 0; i < SIGSZ; ++i)
2350           h ^= r->sig[i];
2351       break;
2352
2353     default:
2354       abort ();
2355     }
2356
2357   return h;
2358 }
2359 \f
2360 /* IEEE single-precision format.  */
2361
2362 static void encode_ieee_single PARAMS ((const struct real_format *fmt,
2363                                         long *, const REAL_VALUE_TYPE *));
2364 static void decode_ieee_single PARAMS ((const struct real_format *,
2365                                         REAL_VALUE_TYPE *, const long *));
2366
2367 static void
2368 encode_ieee_single (fmt, buf, r)
2369      const struct real_format *fmt;
2370      long *buf;
2371      const REAL_VALUE_TYPE *r;
2372 {
2373   unsigned long image, sig, exp;
2374   bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2375
2376   image = r->sign << 31;
2377   sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2378
2379   switch (r->class)
2380     {
2381     case rvc_zero:
2382       break;
2383
2384     case rvc_inf:
2385       if (fmt->has_inf)
2386         image |= 255 << 23;
2387       else
2388         image |= 0x7fffffff;
2389       break;
2390
2391     case rvc_nan:
2392       if (fmt->has_nans)
2393         {
2394           image |= 255 << 23;
2395           image |= sig;
2396           if (!fmt->qnan_msb_set)
2397             image ^= 1 << 23 | 1 << 22;
2398         }
2399       else
2400         image |= 0x7fffffff;
2401       break;
2402
2403     case rvc_normal:
2404       /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2405          whereas the intermediate representation is 0.F x 2**exp.
2406          Which means we're off by one.  */
2407       if (denormal)
2408         exp = 0;
2409       else
2410       exp = r->exp + 127 - 1;
2411       image |= exp << 23;
2412       image |= sig;
2413       break;
2414
2415     default:
2416       abort ();
2417     }
2418
2419   buf[0] = image;
2420 }
2421
2422 static void
2423 decode_ieee_single (fmt, r, buf)
2424      const struct real_format *fmt;
2425      REAL_VALUE_TYPE *r;
2426      const long *buf;
2427 {
2428   unsigned long image = buf[0] & 0xffffffff;
2429   bool sign = (image >> 31) & 1;
2430   int exp = (image >> 23) & 0xff;
2431
2432   memset (r, 0, sizeof (*r));
2433   image <<= HOST_BITS_PER_LONG - 24;
2434   image &= ~SIG_MSB;
2435
2436   if (exp == 0)
2437     {
2438       if (image && fmt->has_denorm)
2439         {
2440           r->class = rvc_normal;
2441           r->sign = sign;
2442           r->exp = -126;
2443           r->sig[SIGSZ-1] = image << 1;
2444           normalize (r);
2445         }
2446       else if (fmt->has_signed_zero)
2447         r->sign = sign;
2448     }
2449   else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2450     {
2451       if (image)
2452         {
2453           r->class = rvc_nan;
2454           r->sign = sign;
2455           if (!fmt->qnan_msb_set)
2456             image ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2457           r->sig[SIGSZ-1] = image;
2458         }
2459       else
2460         {
2461           r->class = rvc_inf;
2462           r->sign = sign;
2463         }
2464     }
2465   else
2466     {
2467       r->class = rvc_normal;
2468       r->sign = sign;
2469       r->exp = exp - 127 + 1;
2470       r->sig[SIGSZ-1] = image | SIG_MSB;
2471     }
2472 }
2473
2474 const struct real_format ieee_single_format = 
2475   {
2476     encode_ieee_single,
2477     decode_ieee_single,
2478     2,
2479     1,
2480     24,
2481     -125,
2482     128,
2483     true,
2484     true,
2485     true,
2486     true,
2487     true
2488   };
2489
2490 \f
2491 /* IEEE double-precision format.  */
2492
2493 static void encode_ieee_double PARAMS ((const struct real_format *fmt,
2494                                         long *, const REAL_VALUE_TYPE *));
2495 static void decode_ieee_double PARAMS ((const struct real_format *,
2496                                         REAL_VALUE_TYPE *, const long *));
2497
2498 static void
2499 encode_ieee_double (fmt, buf, r)
2500      const struct real_format *fmt;
2501      long *buf;
2502      const REAL_VALUE_TYPE *r;
2503 {
2504   unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
2505   bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2506
2507   image_hi = r->sign << 31;
2508   image_lo = 0;
2509
2510   if (HOST_BITS_PER_LONG == 64)
2511     {
2512       sig_hi = r->sig[SIGSZ-1];
2513       sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
2514       sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
2515     }
2516   else
2517     {
2518       sig_hi = r->sig[SIGSZ-1];
2519       sig_lo = r->sig[SIGSZ-2];
2520       sig_lo = (sig_hi << 21) | (sig_lo >> 11);
2521       sig_hi = (sig_hi >> 11) & 0xfffff;
2522     }
2523
2524   switch (r->class)
2525     {
2526     case rvc_zero:
2527       break;
2528
2529     case rvc_inf:
2530       if (fmt->has_inf)
2531         image_hi |= 2047 << 20;
2532       else
2533         {
2534           image_hi |= 0x7fffffff;
2535           image_lo = 0xffffffff;
2536         }
2537       break;
2538
2539     case rvc_nan:
2540       if (fmt->has_nans)
2541         {
2542           image_hi |= 2047 << 20;
2543           image_hi |= sig_hi;
2544           if (!fmt->qnan_msb_set)
2545             image_hi ^= 1 << 19 | 1 << 18;
2546           image_lo = sig_lo;
2547         }
2548       else
2549         {
2550           image_hi |= 0x7fffffff;
2551           image_lo = 0xffffffff;
2552         }
2553       break;
2554
2555     case rvc_normal:
2556       /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2557          whereas the intermediate representation is 0.F x 2**exp.
2558          Which means we're off by one.  */
2559       if (denormal)
2560         exp = 0;
2561       else
2562         exp = r->exp + 1023 - 1;
2563       image_hi |= exp << 20;
2564       image_hi |= sig_hi;
2565       image_lo = sig_lo;
2566       break;
2567
2568     default:
2569       abort ();
2570     }
2571
2572   if (FLOAT_WORDS_BIG_ENDIAN)
2573     buf[0] = image_hi, buf[1] = image_lo;
2574   else
2575     buf[0] = image_lo, buf[1] = image_hi;
2576 }
2577
2578 static void
2579 decode_ieee_double (fmt, r, buf)
2580      const struct real_format *fmt;
2581      REAL_VALUE_TYPE *r;
2582      const long *buf;
2583 {
2584   unsigned long image_hi, image_lo;
2585   bool sign;
2586   int exp;
2587
2588   if (FLOAT_WORDS_BIG_ENDIAN)
2589     image_hi = buf[0], image_lo = buf[1];
2590   else
2591     image_lo = buf[0], image_hi = buf[1];
2592   image_lo &= 0xffffffff;
2593   image_hi &= 0xffffffff;
2594
2595   sign = (image_hi >> 31) & 1;
2596   exp = (image_hi >> 20) & 0x7ff;
2597
2598   memset (r, 0, sizeof (*r));
2599
2600   image_hi <<= 32 - 21;
2601   image_hi |= image_lo >> 21;
2602   image_hi &= 0x7fffffff;
2603   image_lo <<= 32 - 21;
2604
2605   if (exp == 0)
2606     {
2607       if ((image_hi || image_lo) && fmt->has_denorm)
2608         {
2609           r->class = rvc_normal;
2610           r->sign = sign;
2611           r->exp = -1022;
2612           if (HOST_BITS_PER_LONG == 32)
2613             {
2614               image_hi = (image_hi << 1) | (image_lo >> 31);
2615               image_lo <<= 1;
2616               r->sig[SIGSZ-1] = image_hi;
2617               r->sig[SIGSZ-2] = image_lo;
2618             }
2619           else
2620             {
2621               image_hi = (image_hi << 31 << 2) | (image_lo << 1);
2622               r->sig[SIGSZ-1] = image_hi;
2623             }
2624           normalize (r);
2625         }
2626       else if (fmt->has_signed_zero)
2627         r->sign = sign;
2628     }
2629   else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
2630     {
2631       if (image_hi || image_lo)
2632         {
2633           r->class = rvc_nan;
2634           r->sign = sign;
2635           if (HOST_BITS_PER_LONG == 32)
2636             {
2637               r->sig[SIGSZ-1] = image_hi;
2638               r->sig[SIGSZ-2] = image_lo;
2639             }
2640           else
2641             r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
2642
2643           if (!fmt->qnan_msb_set)
2644             r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2645         }
2646       else
2647         {
2648           r->class = rvc_inf;
2649           r->sign = sign;
2650         }
2651     }
2652   else
2653     {
2654       r->class = rvc_normal;
2655       r->sign = sign;
2656       r->exp = exp - 1023 + 1;
2657       if (HOST_BITS_PER_LONG == 32)
2658         {
2659           r->sig[SIGSZ-1] = image_hi | SIG_MSB;
2660           r->sig[SIGSZ-2] = image_lo;
2661         }
2662       else
2663         r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
2664     }
2665 }
2666
2667 const struct real_format ieee_double_format = 
2668   {
2669     encode_ieee_double,
2670     decode_ieee_double,
2671     2,
2672     1,
2673     53,
2674     -1021,
2675     1024,
2676     true,
2677     true,
2678     true,
2679     true,
2680     true
2681   };
2682
2683 \f
2684 /* IEEE extended double precision format.  This comes in three
2685    flavours: Intel's as a 12 byte image, Intel's as a 16 byte image,
2686    and Motorola's.  */
2687
2688 static void encode_ieee_extended PARAMS ((const struct real_format *fmt,
2689                                           long *, const REAL_VALUE_TYPE *));
2690 static void decode_ieee_extended PARAMS ((const struct real_format *,
2691                                           REAL_VALUE_TYPE *, const long *));
2692
2693 static void encode_ieee_extended_128 PARAMS ((const struct real_format *fmt,
2694                                               long *,
2695                                               const REAL_VALUE_TYPE *));
2696 static void decode_ieee_extended_128 PARAMS ((const struct real_format *,
2697                                               REAL_VALUE_TYPE *,
2698                                               const long *));
2699
2700 static void
2701 encode_ieee_extended (fmt, buf, r)
2702      const struct real_format *fmt;
2703      long *buf;
2704      const REAL_VALUE_TYPE *r;
2705 {
2706   unsigned long image_hi, sig_hi, sig_lo;
2707   bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2708
2709   image_hi = r->sign << 15;
2710   sig_hi = sig_lo = 0;
2711
2712   switch (r->class)
2713     {
2714     case rvc_zero:
2715       break;
2716
2717     case rvc_inf:
2718       if (fmt->has_inf)
2719         {
2720           image_hi |= 32767;
2721
2722           /* Intel requires the explicit integer bit to be set, otherwise
2723              it considers the value a "pseudo-infinity".  Motorola docs
2724              say it doesn't care.  */
2725           sig_hi = 0x80000000;
2726         }
2727       else
2728         {
2729           image_hi |= 32767;
2730           sig_lo = sig_hi = 0xffffffff;
2731         }
2732       break;
2733
2734     case rvc_nan:
2735       if (fmt->has_nans)
2736         {
2737           image_hi |= 32767;
2738           if (HOST_BITS_PER_LONG == 32)
2739             {
2740               sig_hi = r->sig[SIGSZ-1];
2741               sig_lo = r->sig[SIGSZ-2];
2742             }
2743           else
2744             {
2745               sig_lo = r->sig[SIGSZ-1];
2746               sig_hi = sig_lo >> 31 >> 1;
2747               sig_lo &= 0xffffffff;
2748             }
2749           if (!fmt->qnan_msb_set)
2750             sig_hi ^= 1 << 30 | 1 << 29;
2751
2752           /* Intel requires the explicit integer bit to be set, otherwise
2753              it considers the value a "pseudo-nan".  Motorola docs say it
2754              doesn't care.  */
2755           sig_hi |= 0x80000000;
2756         }
2757       else
2758         {
2759           image_hi |= 32767;
2760           sig_lo = sig_hi = 0xffffffff;
2761         }
2762       break;
2763
2764     case rvc_normal:
2765       {
2766         int exp = r->exp;
2767
2768         /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2769            whereas the intermediate representation is 0.F x 2**exp.
2770            Which means we're off by one. 
2771
2772            Except for Motorola, which consider exp=0 and explicit
2773            integer bit set to continue to be normalized.  In theory
2774            this descrepency has been taken care of by the difference
2775            in fmt->emin in round_for_format.  */
2776
2777         if (denormal)
2778           exp = 0;
2779         else
2780           {
2781             exp += 16383 - 1;
2782             if (exp < 0)
2783               abort ();
2784           }
2785         image_hi |= exp;
2786
2787         if (HOST_BITS_PER_LONG == 32)
2788           {
2789             sig_hi = r->sig[SIGSZ-1];
2790             sig_lo = r->sig[SIGSZ-2];
2791           }
2792         else
2793           {
2794             sig_lo = r->sig[SIGSZ-1];
2795             sig_hi = sig_lo >> 31 >> 1;
2796             sig_lo &= 0xffffffff;
2797           }
2798       }
2799       break;
2800
2801     default:
2802       abort ();
2803     }
2804
2805   if (FLOAT_WORDS_BIG_ENDIAN)
2806     buf[0] = image_hi << 16, buf[1] = sig_hi, buf[2] = sig_lo;
2807   else
2808     buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
2809 }
2810
2811 static void
2812 encode_ieee_extended_128 (fmt, buf, r)
2813      const struct real_format *fmt;
2814      long *buf;
2815      const REAL_VALUE_TYPE *r;
2816 {
2817   buf[3 * !FLOAT_WORDS_BIG_ENDIAN] = 0;
2818   encode_ieee_extended (fmt, buf+!!FLOAT_WORDS_BIG_ENDIAN, r);
2819 }
2820
2821 static void
2822 decode_ieee_extended (fmt, r, buf)
2823      const struct real_format *fmt;
2824      REAL_VALUE_TYPE *r;
2825      const long *buf;
2826 {
2827   unsigned long image_hi, sig_hi, sig_lo;
2828   bool sign;
2829   int exp;
2830
2831   if (FLOAT_WORDS_BIG_ENDIAN)
2832     image_hi = buf[0] >> 16, sig_hi = buf[1], sig_lo = buf[2];
2833   else
2834     sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
2835   sig_lo &= 0xffffffff;
2836   sig_hi &= 0xffffffff;
2837   image_hi &= 0xffffffff;
2838
2839   sign = (image_hi >> 15) & 1;
2840   exp = image_hi & 0x7fff;
2841
2842   memset (r, 0, sizeof (*r));
2843
2844   if (exp == 0)
2845     {
2846       if ((sig_hi || sig_lo) && fmt->has_denorm)
2847         {
2848           r->class = rvc_normal;
2849           r->sign = sign;
2850
2851           /* When the IEEE format contains a hidden bit, we know that
2852              it's zero at this point, and so shift up the significand
2853              and decrease the exponent to match.  In this case, Motorola
2854              defines the explicit integer bit to be valid, so we don't
2855              know whether the msb is set or not.  */
2856           r->exp = fmt->emin;
2857           if (HOST_BITS_PER_LONG == 32)
2858             {
2859               r->sig[SIGSZ-1] = sig_hi;
2860               r->sig[SIGSZ-2] = sig_lo;
2861             }
2862           else
2863             r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
2864
2865           normalize (r);
2866         }
2867       else if (fmt->has_signed_zero)
2868         r->sign = sign;
2869     }
2870   else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
2871     {
2872       /* See above re "pseudo-infinities" and "pseudo-nans".
2873          Short summary is that the MSB will likely always be
2874          set, and that we don't care about it.  */
2875       sig_hi &= 0x7fffffff;
2876
2877       if (sig_hi || sig_lo)
2878         {
2879           r->class = rvc_nan;
2880           r->sign = sign;
2881           if (HOST_BITS_PER_LONG == 32)
2882             {
2883               r->sig[SIGSZ-1] = sig_hi;
2884               r->sig[SIGSZ-2] = sig_lo;
2885             }
2886           else
2887             r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
2888
2889           if (!fmt->qnan_msb_set)
2890             r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
2891         }
2892       else
2893         {
2894           r->class = rvc_inf;
2895           r->sign = sign;
2896         }
2897     }
2898   else
2899     {
2900       r->class = rvc_normal;
2901       r->sign = sign;
2902       r->exp = exp - 16383 + 1;
2903       if (HOST_BITS_PER_LONG == 32)
2904         {
2905           r->sig[SIGSZ-1] = sig_hi;
2906           r->sig[SIGSZ-2] = sig_lo;
2907         }
2908       else
2909         r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
2910     }
2911 }
2912
2913 static void
2914 decode_ieee_extended_128 (fmt, r, buf)
2915      const struct real_format *fmt;
2916      REAL_VALUE_TYPE *r;
2917      const long *buf;
2918 {
2919   decode_ieee_extended (fmt, r, buf+!!FLOAT_WORDS_BIG_ENDIAN);
2920 }
2921
2922 const struct real_format ieee_extended_motorola_format = 
2923   {
2924     encode_ieee_extended,
2925     decode_ieee_extended,
2926     2,
2927     1,
2928     64,
2929     -16382,
2930     16384,
2931     true,
2932     true,
2933     true,
2934     true,
2935     true
2936   };
2937
2938 const struct real_format ieee_extended_intel_96_format = 
2939   {
2940     encode_ieee_extended,
2941     decode_ieee_extended,
2942     2,
2943     1,
2944     64,
2945     -16381,
2946     16384,
2947     true,
2948     true,
2949     true,
2950     true,
2951     true
2952   };
2953
2954 const struct real_format ieee_extended_intel_128_format = 
2955   {
2956     encode_ieee_extended_128,
2957     decode_ieee_extended_128,
2958     2,
2959     1,
2960     64,
2961     -16381,
2962     16384,
2963     true,
2964     true,
2965     true,
2966     true,
2967     true
2968   };
2969
2970 \f
2971 /* IBM 128-bit extended precision format: a pair of IEEE double precision
2972    numbers whose sum is equal to the extended precision value.  The number
2973    with greater magnitude is first.  This format has the same magnitude
2974    range as an IEEE double precision value, but effectively 106 bits of
2975    significand precision.  Infinity and NaN are represented by their IEEE
2976    double precision value stored in the first number, the second number is
2977    ignored.  Zeroes, Infinities, and NaNs are set in both doubles
2978    due to precedent.  */
2979
2980 static void encode_ibm_extended PARAMS ((const struct real_format *fmt,
2981                                          long *, const REAL_VALUE_TYPE *));
2982 static void decode_ibm_extended PARAMS ((const struct real_format *,
2983                                          REAL_VALUE_TYPE *, const long *));
2984
2985 static void
2986 encode_ibm_extended (fmt, buf, r)
2987      const struct real_format *fmt ATTRIBUTE_UNUSED;
2988      long *buf;
2989      const REAL_VALUE_TYPE *r;
2990 {
2991   REAL_VALUE_TYPE u, v;
2992
2993   switch (r->class)
2994     {
2995     case rvc_zero:
2996       /* Both doubles have sign bit set.  */
2997       buf[0] = FLOAT_WORDS_BIG_ENDIAN ? r->sign << 31 : 0;
2998       buf[1] = FLOAT_WORDS_BIG_ENDIAN ? 0 : r->sign << 31;
2999       buf[2] = buf[0];
3000       buf[3] = buf[1];
3001       break;
3002
3003     case rvc_inf:
3004     case rvc_nan:
3005       /* Both doubles set to Inf / NaN.  */
3006       encode_ieee_double (&ieee_double_format, &buf[0], r);
3007       buf[2] = buf[0];
3008       buf[3] = buf[1];
3009       return;
3010       
3011     case rvc_normal:
3012       /* u = IEEE double precision portion of significand.  */
3013       u = *r;
3014       clear_significand_below (&u, SIGNIFICAND_BITS - 53);
3015
3016       /* v = remainder containing additional 53 bits of significand.  */
3017       do_add (&v, r, &u, 1);
3018
3019       encode_ieee_double (&ieee_double_format, &buf[0], &u);
3020       encode_ieee_double (&ieee_double_format, &buf[2], &v);
3021       break;
3022
3023     default:
3024       abort ();
3025     }
3026 }
3027
3028 static void
3029 decode_ibm_extended (fmt, r, buf)
3030      const struct real_format *fmt ATTRIBUTE_UNUSED;
3031      REAL_VALUE_TYPE *r;
3032      const long *buf;
3033 {
3034   REAL_VALUE_TYPE u, v;
3035
3036   decode_ieee_double (&ieee_double_format, &u, &buf[0]);
3037
3038   if (u.class != rvc_zero && u.class != rvc_inf && u.class != rvc_nan)
3039     {
3040       decode_ieee_double (&ieee_double_format, &v, &buf[2]);
3041       do_add (r, &u, &v, 0);
3042     }
3043   else
3044     *r = u;
3045 }
3046
3047 const struct real_format ibm_extended_format = 
3048   {
3049     encode_ibm_extended,
3050     decode_ibm_extended,
3051     2,
3052     1,
3053     53 + 53,
3054     -1021,
3055     1024,
3056     true,
3057     true,
3058     true,
3059     true,
3060     true
3061   };
3062
3063 \f
3064 /* IEEE quad precision format.  */
3065
3066 static void encode_ieee_quad PARAMS ((const struct real_format *fmt,
3067                                       long *, const REAL_VALUE_TYPE *));
3068 static void decode_ieee_quad PARAMS ((const struct real_format *,
3069                                       REAL_VALUE_TYPE *, const long *));
3070
3071 static void
3072 encode_ieee_quad (fmt, buf, r)
3073      const struct real_format *fmt;
3074      long *buf;
3075      const REAL_VALUE_TYPE *r;
3076 {
3077   unsigned long image3, image2, image1, image0, exp;
3078   bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3079   REAL_VALUE_TYPE u;
3080
3081   image3 = r->sign << 31;
3082   image2 = 0;
3083   image1 = 0;
3084   image0 = 0;
3085
3086   rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3087
3088   switch (r->class)
3089     {
3090     case rvc_zero:
3091       break;
3092
3093     case rvc_inf:
3094       if (fmt->has_inf)
3095         image3 |= 32767 << 16;
3096       else
3097         {
3098           image3 |= 0x7fffffff;
3099           image2 = 0xffffffff;
3100           image1 = 0xffffffff;
3101           image0 = 0xffffffff;
3102         }
3103       break;
3104
3105     case rvc_nan:
3106       if (fmt->has_nans)
3107         {
3108           image3 |= 32767 << 16;
3109
3110           if (HOST_BITS_PER_LONG == 32)
3111             {
3112               image0 = u.sig[0];
3113               image1 = u.sig[1];
3114               image2 = u.sig[2];
3115               image3 |= u.sig[3] & 0xffff;
3116             }
3117           else
3118             {
3119               image0 = u.sig[0];
3120               image1 = image0 >> 31 >> 1;
3121               image2 = u.sig[1];
3122               image3 |= (image2 >> 31 >> 1) & 0xffff;
3123               image0 &= 0xffffffff;
3124               image2 &= 0xffffffff;
3125             }
3126
3127           if (!fmt->qnan_msb_set)
3128             image3 ^= 1 << 15 | 1 << 14;
3129         }
3130       else
3131         {
3132           image3 |= 0x7fffffff;
3133           image2 = 0xffffffff;
3134           image1 = 0xffffffff;
3135           image0 = 0xffffffff;
3136         }
3137       break;
3138
3139     case rvc_normal:
3140       /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3141          whereas the intermediate representation is 0.F x 2**exp.
3142          Which means we're off by one.  */
3143       if (denormal)
3144         exp = 0;
3145       else
3146         exp = r->exp + 16383 - 1;
3147       image3 |= exp << 16;
3148
3149       if (HOST_BITS_PER_LONG == 32)
3150         {
3151           image0 = u.sig[0];
3152           image1 = u.sig[1];
3153           image2 = u.sig[2];
3154           image3 |= u.sig[3] & 0xffff;
3155         }
3156       else
3157         {
3158           image0 = u.sig[0];
3159           image1 = image0 >> 31 >> 1;
3160           image2 = u.sig[1];
3161           image3 |= (image2 >> 31 >> 1) & 0xffff;
3162           image0 &= 0xffffffff;
3163           image2 &= 0xffffffff;
3164         }
3165       break;
3166
3167     default:
3168       abort ();
3169     }
3170
3171   if (FLOAT_WORDS_BIG_ENDIAN)
3172     {
3173       buf[0] = image3;
3174       buf[1] = image2;
3175       buf[2] = image1;
3176       buf[3] = image0;
3177     }
3178   else
3179     {
3180       buf[0] = image0;
3181       buf[1] = image1;
3182       buf[2] = image2;
3183       buf[3] = image3;
3184     }
3185 }
3186
3187 static void
3188 decode_ieee_quad (fmt, r, buf)
3189      const struct real_format *fmt;
3190      REAL_VALUE_TYPE *r;
3191      const long *buf;
3192 {
3193   unsigned long image3, image2, image1, image0;
3194   bool sign;
3195   int exp;
3196
3197   if (FLOAT_WORDS_BIG_ENDIAN)
3198     {
3199       image3 = buf[0];
3200       image2 = buf[1];
3201       image1 = buf[2];
3202       image0 = buf[3];
3203     }
3204   else
3205     {
3206       image0 = buf[0];
3207       image1 = buf[1];
3208       image2 = buf[2];
3209       image3 = buf[3];
3210     }
3211   image0 &= 0xffffffff;
3212   image1 &= 0xffffffff;
3213   image2 &= 0xffffffff;
3214
3215   sign = (image3 >> 31) & 1;
3216   exp = (image3 >> 16) & 0x7fff;
3217   image3 &= 0xffff;
3218
3219   memset (r, 0, sizeof (*r));
3220
3221   if (exp == 0)
3222     {
3223       if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3224         {
3225           r->class = rvc_normal;
3226           r->sign = sign;
3227
3228           r->exp = -16382 + (SIGNIFICAND_BITS - 112);
3229           if (HOST_BITS_PER_LONG == 32)
3230             {
3231               r->sig[0] = image0;
3232               r->sig[1] = image1;
3233               r->sig[2] = image2;
3234               r->sig[3] = image3;
3235             }
3236           else
3237             {
3238               r->sig[0] = (image1 << 31 << 1) | image0;
3239               r->sig[1] = (image3 << 31 << 1) | image2;
3240             }
3241
3242           normalize (r);
3243         }
3244       else if (fmt->has_signed_zero)
3245         r->sign = sign;
3246     }
3247   else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3248     {
3249       if (image3 | image2 | image1 | image0)
3250         {
3251           r->class = rvc_nan;
3252           r->sign = sign;
3253
3254           if (HOST_BITS_PER_LONG == 32)
3255             {
3256               r->sig[0] = image0;
3257               r->sig[1] = image1;
3258               r->sig[2] = image2;
3259               r->sig[3] = image3;
3260             }
3261           else
3262             {
3263               r->sig[0] = (image1 << 31 << 1) | image0;
3264               r->sig[1] = (image3 << 31 << 1) | image2;
3265             }
3266           lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3267
3268           if (!fmt->qnan_msb_set)
3269             r->sig[SIGSZ-1] ^= (SIG_MSB >> 1 | SIG_MSB >> 2);
3270         }
3271       else
3272         {
3273           r->class = rvc_inf;
3274           r->sign = sign;
3275         }
3276     }
3277   else
3278     {
3279       r->class = rvc_normal;
3280       r->sign = sign;
3281       r->exp = exp - 16383 + 1;
3282
3283       if (HOST_BITS_PER_LONG == 32)
3284         {
3285           r->sig[0] = image0;
3286           r->sig[1] = image1;
3287           r->sig[2] = image2;
3288           r->sig[3] = image3;
3289         }
3290       else
3291         {
3292           r->sig[0] = (image1 << 31 << 1) | image0;
3293           r->sig[1] = (image3 << 31 << 1) | image2;
3294         }
3295       lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3296       r->sig[SIGSZ-1] |= SIG_MSB;
3297     }
3298 }
3299
3300 const struct real_format ieee_quad_format = 
3301   {
3302     encode_ieee_quad,
3303     decode_ieee_quad,
3304     2,
3305     1,
3306     113,
3307     -16381,
3308     16384,
3309     true,
3310     true,
3311     true,
3312     true,
3313     true
3314   };
3315 \f
3316 /* Descriptions of VAX floating point formats can be found beginning at
3317
3318    http://www.openvms.compaq.com:8000/73final/4515/4515pro_013.html#f_floating_point_format
3319
3320    The thing to remember is that they're almost IEEE, except for word
3321    order, exponent bias, and the lack of infinities, nans, and denormals.
3322
3323    We don't implement the H_floating format here, simply because neither
3324    the VAX or Alpha ports use it.  */
3325    
3326 static void encode_vax_f PARAMS ((const struct real_format *fmt,
3327                                   long *, const REAL_VALUE_TYPE *));
3328 static void decode_vax_f PARAMS ((const struct real_format *,
3329                                   REAL_VALUE_TYPE *, const long *));
3330 static void encode_vax_d PARAMS ((const struct real_format *fmt,
3331                                   long *, const REAL_VALUE_TYPE *));
3332 static void decode_vax_d PARAMS ((const struct real_format *,
3333                                   REAL_VALUE_TYPE *, const long *));
3334 static void encode_vax_g PARAMS ((const struct real_format *fmt,
3335                                   long *, const REAL_VALUE_TYPE *));
3336 static void decode_vax_g PARAMS ((const struct real_format *,
3337                                   REAL_VALUE_TYPE *, const long *));
3338
3339 static void
3340 encode_vax_f (fmt, buf, r)
3341      const struct real_format *fmt ATTRIBUTE_UNUSED;
3342      long *buf;
3343      const REAL_VALUE_TYPE *r;
3344 {
3345   unsigned long sign, exp, sig, image;
3346
3347   sign = r->sign << 15;
3348
3349   switch (r->class)
3350     {
3351     case rvc_zero:
3352       image = 0;
3353       break;
3354
3355     case rvc_inf:
3356     case rvc_nan:
3357       image = 0xffff7fff | sign;
3358       break;
3359
3360     case rvc_normal:
3361       sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3362       exp = r->exp + 128;
3363
3364       image = (sig << 16) & 0xffff0000;
3365       image |= sign;
3366       image |= exp << 7;
3367       image |= sig >> 16;
3368       break;
3369
3370     default:
3371       abort ();
3372     }
3373
3374   buf[0] = image;
3375 }
3376
3377 static void
3378 decode_vax_f (fmt, r, buf)
3379      const struct real_format *fmt ATTRIBUTE_UNUSED;
3380      REAL_VALUE_TYPE *r;
3381      const long *buf;
3382 {
3383   unsigned long image = buf[0] & 0xffffffff;
3384   int exp = (image >> 7) & 0xff;
3385
3386   memset (r, 0, sizeof (*r));
3387
3388   if (exp != 0)
3389     {
3390       r->class = rvc_normal;
3391       r->sign = (image >> 15) & 1;
3392       r->exp = exp - 128;
3393
3394       image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
3395       r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3396     }
3397 }
3398
3399 static void
3400 encode_vax_d (fmt, buf, r)
3401      const struct real_format *fmt ATTRIBUTE_UNUSED;
3402      long *buf;
3403      const REAL_VALUE_TYPE *r;
3404 {
3405   unsigned long image0, image1, sign = r->sign << 15;
3406
3407   switch (r->class)
3408     {
3409     case rvc_zero:
3410       image0 = image1 = 0;
3411       break;
3412
3413     case rvc_inf:
3414     case rvc_nan:
3415       image0 = 0xffff7fff | sign;
3416       image1 = 0xffffffff;
3417       break;
3418
3419     case rvc_normal:
3420       /* Extract the significand into straight hi:lo.  */
3421       if (HOST_BITS_PER_LONG == 64)
3422         {
3423           image0 = r->sig[SIGSZ-1];
3424           image1 = (image0 >> (64 - 56)) & 0xffffffff;
3425           image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
3426         }
3427       else
3428         {
3429           image0 = r->sig[SIGSZ-1];
3430           image1 = r->sig[SIGSZ-2];
3431           image1 = (image0 << 24) | (image1 >> 8);
3432           image0 = (image0 >> 8) & 0xffffff;
3433         }
3434
3435       /* Rearrange the half-words of the significand to match the
3436          external format.  */
3437       image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
3438       image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3439
3440       /* Add the sign and exponent.  */
3441       image0 |= sign;
3442       image0 |= (r->exp + 128) << 7;
3443       break;
3444
3445     default:
3446       abort ();
3447     }
3448
3449   if (FLOAT_WORDS_BIG_ENDIAN)
3450     buf[0] = image1, buf[1] = image0;
3451   else
3452     buf[0] = image0, buf[1] = image1;
3453 }
3454
3455 static void
3456 decode_vax_d (fmt, r, buf)
3457      const struct real_format *fmt ATTRIBUTE_UNUSED;
3458      REAL_VALUE_TYPE *r;
3459      const long *buf;
3460 {
3461   unsigned long image0, image1;
3462   int exp;
3463
3464   if (FLOAT_WORDS_BIG_ENDIAN)
3465     image1 = buf[0], image0 = buf[1];
3466   else
3467     image0 = buf[0], image1 = buf[1];
3468   image0 &= 0xffffffff;
3469   image1 &= 0xffffffff;
3470
3471   exp = (image0 >> 7) & 0x7f;
3472
3473   memset (r, 0, sizeof (*r));
3474
3475   if (exp != 0)
3476     {
3477       r->class = rvc_normal;
3478       r->sign = (image0 >> 15) & 1;
3479       r->exp = exp - 128;
3480
3481       /* Rearrange the half-words of the external format into
3482          proper ascending order.  */
3483       image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
3484       image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3485
3486       if (HOST_BITS_PER_LONG == 64)
3487         {
3488           image0 = (image0 << 31 << 1) | image1;
3489           image0 <<= 64 - 56;
3490           image0 |= SIG_MSB;
3491           r->sig[SIGSZ-1] = image0;
3492         }
3493       else
3494         {
3495           r->sig[SIGSZ-1] = image0;
3496           r->sig[SIGSZ-2] = image1;
3497           lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
3498           r->sig[SIGSZ-1] |= SIG_MSB;
3499         }
3500     }
3501 }
3502
3503 static void
3504 encode_vax_g (fmt, buf, r)
3505      const struct real_format *fmt ATTRIBUTE_UNUSED;
3506      long *buf;
3507      const REAL_VALUE_TYPE *r;
3508 {
3509   unsigned long image0, image1, sign = r->sign << 15;
3510
3511   switch (r->class)
3512     {
3513     case rvc_zero:
3514       image0 = image1 = 0;
3515       break;
3516
3517     case rvc_inf:
3518     case rvc_nan:
3519       image0 = 0xffff7fff | sign;
3520       image1 = 0xffffffff;
3521       break;
3522
3523     case rvc_normal:
3524       /* Extract the significand into straight hi:lo.  */
3525       if (HOST_BITS_PER_LONG == 64)
3526         {
3527           image0 = r->sig[SIGSZ-1];
3528           image1 = (image0 >> (64 - 53)) & 0xffffffff;
3529           image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
3530         }
3531       else
3532         {
3533           image0 = r->sig[SIGSZ-1];
3534           image1 = r->sig[SIGSZ-2];
3535           image1 = (image0 << 21) | (image1 >> 11);
3536           image0 = (image0 >> 11) & 0xfffff;
3537         }
3538
3539       /* Rearrange the half-words of the significand to match the
3540          external format.  */
3541       image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
3542       image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
3543
3544       /* Add the sign and exponent.  */
3545       image0 |= sign;
3546       image0 |= (r->exp + 1024) << 4;
3547       break;
3548
3549     default:
3550       abort ();
3551     }
3552
3553   if (FLOAT_WORDS_BIG_ENDIAN)
3554     buf[0] = image1, buf[1] = image0;
3555   else
3556     buf[0] = image0, buf[1] = image1;
3557 }
3558
3559 static void
3560 decode_vax_g (fmt, r, buf)
3561      const struct real_format *fmt ATTRIBUTE_UNUSED;
3562      REAL_VALUE_TYPE *r;
3563      const long *buf;
3564 {
3565   unsigned long image0, image1;
3566   int exp;
3567
3568   if (FLOAT_WORDS_BIG_ENDIAN)
3569     image1 = buf[0], image0 = buf[1];
3570   else
3571     image0 = buf[0], image1 = buf[1];
3572   image0 &= 0xffffffff;
3573   image1 &= 0xffffffff;
3574
3575   exp = (image0 >> 4) & 0x7ff;
3576
3577   memset (r, 0, sizeof (*r));
3578
3579   if (exp != 0)
3580     {
3581       r->class = rvc_normal;
3582       r->sign = (image0 >> 15) & 1;
3583       r->exp = exp - 1024;
3584
3585       /* Rearrange the half-words of the external format into
3586          proper ascending order.  */
3587       image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
3588       image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
3589
3590       if (HOST_BITS_PER_LONG == 64)
3591         {
3592           image0 = (image0 << 31 << 1) | image1;
3593           image0 <<= 64 - 53;
3594           image0 |= SIG_MSB;
3595           r->sig[SIGSZ-1] = image0;
3596         }
3597       else
3598         {
3599           r->sig[SIGSZ-1] = image0;
3600           r->sig[SIGSZ-2] = image1;
3601           lshift_significand (r, r, 64 - 53);
3602           r->sig[SIGSZ-1] |= SIG_MSB;
3603         }
3604     }
3605 }
3606
3607 const struct real_format vax_f_format = 
3608   {
3609     encode_vax_f,
3610     decode_vax_f,
3611     2,
3612     1,
3613     24,
3614     -127,
3615     127,
3616     false,
3617     false,
3618     false,
3619     false,
3620     false
3621   };
3622
3623 const struct real_format vax_d_format = 
3624   {
3625     encode_vax_d,
3626     decode_vax_d,
3627     2,
3628     1,
3629     56,
3630     -127,
3631     127,
3632     false,
3633     false,
3634     false,
3635     false,
3636     false
3637   };
3638
3639 const struct real_format vax_g_format = 
3640   {
3641     encode_vax_g,
3642     decode_vax_g,
3643     2,
3644     1,
3645     53,
3646     -1023,
3647     1023,
3648     false,
3649     false,
3650     false,
3651     false,
3652     false
3653   };
3654 \f
3655 /* A good reference for these can be found in chapter 9 of
3656    "ESA/390 Principles of Operation", IBM document number SA22-7201-01.
3657    An on-line version can be found here:
3658
3659    http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DZ9AR001/9.1?DT=19930923083613
3660 */
3661
3662 static void encode_i370_single PARAMS ((const struct real_format *fmt,
3663                                         long *, const REAL_VALUE_TYPE *));
3664 static void decode_i370_single PARAMS ((const struct real_format *,
3665                                         REAL_VALUE_TYPE *, const long *));
3666 static void encode_i370_double PARAMS ((const struct real_format *fmt,
3667                                         long *, const REAL_VALUE_TYPE *));
3668 static void decode_i370_double PARAMS ((const struct real_format *,
3669                                         REAL_VALUE_TYPE *, const long *));
3670
3671 static void
3672 encode_i370_single (fmt, buf, r)
3673      const struct real_format *fmt ATTRIBUTE_UNUSED;
3674      long *buf;
3675      const REAL_VALUE_TYPE *r;
3676 {
3677   unsigned long sign, exp, sig, image;
3678
3679   sign = r->sign << 31;
3680
3681   switch (r->class)
3682     {
3683     case rvc_zero:
3684       image = 0;
3685       break;
3686
3687     case rvc_inf:
3688     case rvc_nan:
3689       image = 0x7fffffff | sign;
3690       break;
3691
3692     case rvc_normal:
3693       sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0xffffff;
3694       exp = ((r->exp / 4) + 64) << 24;
3695       image = sign | exp | sig;
3696       break;
3697
3698     default:
3699       abort ();
3700     }
3701
3702   buf[0] = image;
3703 }
3704
3705 static void
3706 decode_i370_single (fmt, r, buf)
3707      const struct real_format *fmt ATTRIBUTE_UNUSED;
3708      REAL_VALUE_TYPE *r;
3709      const long *buf;
3710 {
3711   unsigned long sign, sig, image = buf[0];
3712   int exp;
3713
3714   sign = (image >> 31) & 1;
3715   exp = (image >> 24) & 0x7f;
3716   sig = image & 0xffffff;
3717
3718   memset (r, 0, sizeof (*r));
3719
3720   if (exp || sig)
3721     {
3722       r->class = rvc_normal;
3723       r->sign = sign;
3724       r->exp = (exp - 64) * 4;
3725       r->sig[SIGSZ-1] = sig << (HOST_BITS_PER_LONG - 24);
3726       normalize (r);
3727     }
3728 }
3729
3730 static void
3731 encode_i370_double (fmt, buf, r)
3732      const struct real_format *fmt ATTRIBUTE_UNUSED;
3733      long *buf;
3734      const REAL_VALUE_TYPE *r;
3735 {
3736   unsigned long sign, exp, image_hi, image_lo;
3737
3738   sign = r->sign << 31;
3739
3740   switch (r->class)
3741     {
3742     case rvc_zero:
3743       image_hi = image_lo = 0;
3744       break;
3745
3746     case rvc_inf:
3747     case rvc_nan:
3748       image_hi = 0x7fffffff | sign;
3749       image_lo = 0xffffffff;
3750       break;
3751
3752     case rvc_normal:
3753       if (HOST_BITS_PER_LONG == 64)
3754         {
3755           image_hi = r->sig[SIGSZ-1];
3756           image_lo = (image_hi >> (64 - 56)) & 0xffffffff;
3757           image_hi = (image_hi >> (64 - 56 + 1) >> 31) & 0xffffff;
3758         }
3759       else
3760         {
3761           image_hi = r->sig[SIGSZ-1];
3762           image_lo = r->sig[SIGSZ-2];
3763           image_lo = (image_lo >> 8) | (image_hi << 24);
3764           image_hi >>= 8;
3765         }
3766
3767       exp = ((r->exp / 4) + 64) << 24;
3768       image_hi |= sign | exp;
3769       break;
3770
3771     default:
3772       abort ();
3773     }
3774
3775   if (FLOAT_WORDS_BIG_ENDIAN)
3776     buf[0] = image_hi, buf[1] = image_lo;
3777   else
3778     buf[0] = image_lo, buf[1] = image_hi;
3779 }
3780
3781 static void
3782 decode_i370_double (fmt, r, buf)
3783      const struct real_format *fmt ATTRIBUTE_UNUSED;
3784      REAL_VALUE_TYPE *r;
3785      const long *buf;
3786 {
3787   unsigned long sign, image_hi, image_lo;
3788   int exp;
3789
3790   if (FLOAT_WORDS_BIG_ENDIAN)
3791     image_hi = buf[0], image_lo = buf[1];
3792   else
3793     image_lo = buf[0], image_hi = buf[1];
3794
3795   sign = (image_hi >> 31) & 1;
3796   exp = (image_hi >> 24) & 0x7f;
3797   image_hi &= 0xffffff;
3798   image_lo &= 0xffffffff;
3799
3800   memset (r, 0, sizeof (*r));
3801
3802   if (exp || image_hi || image_lo)
3803     {
3804       r->class = rvc_normal;
3805       r->sign = sign;
3806       r->exp = (exp - 64) * 4 + (SIGNIFICAND_BITS - 56);
3807
3808       if (HOST_BITS_PER_LONG == 32)
3809         {
3810           r->sig[0] = image_lo;
3811           r->sig[1] = image_hi;
3812         }
3813       else
3814         r->sig[0] = image_lo | (image_hi << 31 << 1);
3815
3816       normalize (r);
3817     }
3818 }
3819
3820 const struct real_format i370_single_format =
3821   {
3822     encode_i370_single,
3823     decode_i370_single,
3824     16,
3825     4,
3826     6,
3827     -64,
3828     63,
3829     false,
3830     false,
3831     false, /* ??? The encoding does allow for "unnormals".  */
3832     false, /* ??? The encoding does allow for "unnormals".  */
3833     false
3834   };
3835
3836 const struct real_format i370_double_format =
3837   {
3838     encode_i370_double,
3839     decode_i370_double,
3840     16,
3841     4,
3842     14,
3843     -64,
3844     63,
3845     false,
3846     false,
3847     false, /* ??? The encoding does allow for "unnormals".  */
3848     false, /* ??? The encoding does allow for "unnormals".  */
3849     false
3850   };
3851 \f
3852 /* The "twos-complement" c4x format is officially defined as
3853
3854         x = s(~s).f * 2**e
3855
3856    This is rather misleading.  One must remember that F is signed.
3857    A better description would be
3858
3859         x = -1**s * ((s + 1 + .f) * 2**e
3860
3861    So if we have a (4 bit) fraction of .1000 with a sign bit of 1,
3862    that's -1 * (1+1+(-.5)) == -1.5.  I think.
3863
3864    The constructions here are taken from Tables 5-1 and 5-2 of the
3865    TMS320C4x User's Guide wherein step-by-step instructions for
3866    conversion from IEEE are presented.  That's close enough to our
3867    internal representation so as to make things easy.
3868
3869    See http://www-s.ti.com/sc/psheets/spru063c/spru063c.pdf  */
3870
3871 static void encode_c4x_single PARAMS ((const struct real_format *fmt,
3872                                        long *, const REAL_VALUE_TYPE *));
3873 static void decode_c4x_single PARAMS ((const struct real_format *,
3874                                        REAL_VALUE_TYPE *, const long *));
3875 static void encode_c4x_extended PARAMS ((const struct real_format *fmt,
3876                                          long *, const REAL_VALUE_TYPE *));
3877 static void decode_c4x_extended PARAMS ((const struct real_format *,
3878                                          REAL_VALUE_TYPE *, const long *));
3879
3880 static void
3881 encode_c4x_single (fmt, buf, r)
3882      const struct real_format *fmt ATTRIBUTE_UNUSED;
3883      long *buf;
3884      const REAL_VALUE_TYPE *r;
3885 {
3886   unsigned long image, exp, sig;
3887   
3888   switch (r->class)
3889     {
3890     case rvc_zero:
3891       exp = -128;
3892       sig = 0;
3893       break;
3894
3895     case rvc_inf:
3896     case rvc_nan:
3897       exp = 127;
3898       sig = 0x800000 - r->sign;
3899       break;
3900
3901     case rvc_normal:
3902       exp = r->exp - 1;
3903       sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
3904       if (r->sign)
3905         {
3906           if (sig)
3907             sig = -sig;
3908           else
3909             exp--;
3910           sig |= 0x800000;
3911         }
3912       break;
3913
3914     default:
3915       abort ();
3916     }
3917
3918   image = ((exp & 0xff) << 24) | (sig & 0xffffff);
3919   buf[0] = image;
3920 }
3921
3922 static void
3923 decode_c4x_single (fmt, r, buf)
3924      const struct real_format *fmt ATTRIBUTE_UNUSED;
3925      REAL_VALUE_TYPE *r;
3926      const long *buf;
3927 {
3928   unsigned long image = buf[0];
3929   unsigned long sig;
3930   int exp, sf;
3931
3932   exp = (((image >> 24) & 0xff) ^ 0x80) - 0x80;
3933   sf = ((image & 0xffffff) ^ 0x800000) - 0x800000;
3934
3935   memset (r, 0, sizeof (*r));
3936
3937   if (exp != -128)
3938     {
3939       r->class = rvc_normal;
3940
3941       sig = sf & 0x7fffff;
3942       if (sf < 0)
3943         {
3944           r->sign = 1;
3945           if (sig)
3946             sig = -sig;
3947           else
3948             exp++;
3949         }
3950       sig = (sig << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
3951
3952       r->exp = exp + 1;
3953       r->sig[SIGSZ-1] = sig;
3954     }
3955 }
3956
3957 static void
3958 encode_c4x_extended (fmt, buf, r)
3959      const struct real_format *fmt ATTRIBUTE_UNUSED;
3960      long *buf;
3961      const REAL_VALUE_TYPE *r;
3962 {
3963   unsigned long exp, sig;
3964   
3965   switch (r->class)
3966     {
3967     case rvc_zero:
3968       exp = -128;
3969       sig = 0;
3970       break;
3971
3972     case rvc_inf:
3973     case rvc_nan:
3974       exp = 127;
3975       sig = 0x80000000 - r->sign;
3976       break;
3977
3978     case rvc_normal:
3979       exp = r->exp - 1;
3980
3981       sig = r->sig[SIGSZ-1];
3982       if (HOST_BITS_PER_LONG == 64)
3983         sig = sig >> 1 >> 31;
3984       sig &= 0x7fffffff;
3985
3986       if (r->sign)
3987         {
3988           if (sig)
3989             sig = -sig;
3990           else
3991             exp--;
3992           sig |= 0x80000000;
3993         }
3994       break;
3995
3996     default:
3997       abort ();
3998     }
3999
4000   exp = (exp & 0xff) << 24;
4001   sig &= 0xffffffff;
4002
4003   if (FLOAT_WORDS_BIG_ENDIAN)
4004     buf[0] = exp, buf[1] = sig;
4005   else
4006     buf[0] = sig, buf[0] = exp;
4007 }
4008
4009 static void
4010 decode_c4x_extended (fmt, r, buf)
4011      const struct real_format *fmt ATTRIBUTE_UNUSED;
4012      REAL_VALUE_TYPE *r;
4013      const long *buf;
4014 {
4015   unsigned long sig;
4016   int exp, sf;
4017
4018   if (FLOAT_WORDS_BIG_ENDIAN)
4019     exp = buf[0], sf = buf[1];
4020   else
4021     sf = buf[0], exp = buf[1];
4022
4023   exp = (((exp >> 24) & 0xff) & 0x80) - 0x80;
4024   sf = ((sf & 0xffffffff) ^ 0x80000000) - 0x80000000;
4025
4026   memset (r, 0, sizeof (*r));
4027
4028   if (exp != -128)
4029     {
4030       r->class = rvc_normal;
4031
4032       sig = sf & 0x7fffffff;
4033       if (sf < 0)
4034         {
4035           r->sign = 1;
4036           if (sig)
4037             sig = -sig;
4038           else
4039             exp++;
4040         }
4041       if (HOST_BITS_PER_LONG == 64)
4042         sig = sig << 1 << 31;
4043       sig |= SIG_MSB;
4044
4045       r->exp = exp + 1;
4046       r->sig[SIGSZ-1] = sig;
4047     }
4048 }
4049
4050 const struct real_format c4x_single_format = 
4051   {
4052     encode_c4x_single,
4053     decode_c4x_single,
4054     2,
4055     1,
4056     24,
4057     -126,
4058     128,
4059     false,
4060     false,
4061     false,
4062     false,
4063     false
4064   };
4065
4066 const struct real_format c4x_extended_format = 
4067   {
4068     encode_c4x_extended,
4069     decode_c4x_extended,
4070     2,
4071     1,
4072     32,
4073     -126,
4074     128,
4075     false,
4076     false,
4077     false,
4078     false,
4079     false
4080   };
4081 \f
4082 /* Set up default mode to format mapping for IEEE.  Everyone else has
4083    to set these values in OVERRIDE_OPTIONS.  */
4084
4085 const struct real_format *real_format_for_mode[TFmode - QFmode + 1] =
4086 {
4087   NULL,                         /* QFmode */
4088   NULL,                         /* HFmode */
4089   NULL,                         /* TQFmode */
4090   &ieee_single_format,          /* SFmode */
4091   &ieee_double_format,          /* DFmode */
4092
4093   /* We explicitly don't handle XFmode.  There are two formats,
4094      pretty much equally common.  Choose one in OVERRIDE_OPTIONS.  */
4095   NULL,                         /* XFmode */
4096   &ieee_quad_format             /* TFmode */
4097 };