OSDN Git Service

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