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