OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / real.c
1 /* real.c - software floating point emulation.
2    Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2002, 2003, 2004, 2005, 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
1270 /* Return true if arithmetic on values in IMODE that were promoted
1271    from values in TMODE is equivalent to direct arithmetic on values
1272    in TMODE.  */
1273
1274 bool
1275 real_can_shorten_arithmetic (enum machine_mode imode, enum machine_mode tmode)
1276 {
1277   const struct real_format *tfmt, *ifmt;
1278   tfmt = REAL_MODE_FORMAT (tmode);
1279   ifmt = REAL_MODE_FORMAT (imode);
1280   /* These conditions are conservative rather than trying to catch the
1281      exact boundary conditions; the main case to allow is IEEE float
1282      and double.  */
1283   return (ifmt->b == tfmt->b
1284           && ifmt->p > 2 * tfmt->p
1285           && ifmt->emin < 2 * tfmt->emin - tfmt->p - 2
1286           && ifmt->emin < tfmt->emin - tfmt->emax - tfmt->p - 2
1287           && ifmt->emax > 2 * tfmt->emax + 2
1288           && ifmt->emax > tfmt->emax - tfmt->emin + tfmt->p + 2
1289           && ifmt->round_towards_zero == tfmt->round_towards_zero
1290           && (ifmt->has_sign_dependent_rounding
1291               == tfmt->has_sign_dependent_rounding)
1292           && ifmt->has_nans >= tfmt->has_nans
1293           && ifmt->has_inf >= tfmt->has_inf
1294           && ifmt->has_signed_zero >= tfmt->has_signed_zero
1295           && !MODE_COMPOSITE_P (tmode)
1296           && !MODE_COMPOSITE_P (imode));
1297 }
1298 \f
1299 /* Render R as an integer.  */
1300
1301 HOST_WIDE_INT
1302 real_to_integer (const REAL_VALUE_TYPE *r)
1303 {
1304   unsigned HOST_WIDE_INT i;
1305
1306   switch (r->cl)
1307     {
1308     case rvc_zero:
1309     underflow:
1310       return 0;
1311
1312     case rvc_inf:
1313     case rvc_nan:
1314     overflow:
1315       i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1316       if (!r->sign)
1317         i--;
1318       return i;
1319
1320     case rvc_normal:
1321       if (r->decimal)
1322         return decimal_real_to_integer (r);
1323
1324       if (REAL_EXP (r) <= 0)
1325         goto underflow;
1326       /* Only force overflow for unsigned overflow.  Signed overflow is
1327          undefined, so it doesn't matter what we return, and some callers
1328          expect to be able to use this routine for both signed and
1329          unsigned conversions.  */
1330       if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1331         goto overflow;
1332
1333       if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1334         i = r->sig[SIGSZ-1];
1335       else 
1336         {
1337           gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1338           i = r->sig[SIGSZ-1];
1339           i = i << (HOST_BITS_PER_LONG - 1) << 1;
1340           i |= r->sig[SIGSZ-2];
1341         }
1342
1343       i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1344
1345       if (r->sign)
1346         i = -i;
1347       return i;
1348
1349     default:
1350       gcc_unreachable ();
1351     }
1352 }
1353
1354 /* Likewise, but to an integer pair, HI+LOW.  */
1355
1356 void
1357 real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
1358                   const REAL_VALUE_TYPE *r)
1359 {
1360   REAL_VALUE_TYPE t;
1361   HOST_WIDE_INT low, high;
1362   int exp;
1363
1364   switch (r->cl)
1365     {
1366     case rvc_zero:
1367     underflow:
1368       low = high = 0;
1369       break;
1370
1371     case rvc_inf:
1372     case rvc_nan:
1373     overflow:
1374       high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1375       if (r->sign)
1376         low = 0;
1377       else
1378         {
1379           high--;
1380           low = -1;
1381         }
1382       break;
1383
1384     case rvc_normal:
1385       if (r->decimal)
1386         { 
1387           decimal_real_to_integer2 (plow, phigh, r);
1388           return;
1389         }
1390         
1391       exp = REAL_EXP (r);
1392       if (exp <= 0)
1393         goto underflow;
1394       /* Only force overflow for unsigned overflow.  Signed overflow is
1395          undefined, so it doesn't matter what we return, and some callers
1396          expect to be able to use this routine for both signed and
1397          unsigned conversions.  */
1398       if (exp > 2*HOST_BITS_PER_WIDE_INT)
1399         goto overflow;
1400
1401       rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp);
1402       if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1403         {
1404           high = t.sig[SIGSZ-1];
1405           low = t.sig[SIGSZ-2];
1406         }
1407       else 
1408         {
1409           gcc_assert (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG);
1410           high = t.sig[SIGSZ-1];
1411           high = high << (HOST_BITS_PER_LONG - 1) << 1;
1412           high |= t.sig[SIGSZ-2];
1413
1414           low = t.sig[SIGSZ-3];
1415           low = low << (HOST_BITS_PER_LONG - 1) << 1;
1416           low |= t.sig[SIGSZ-4];
1417         }
1418
1419       if (r->sign)
1420         {
1421           if (low == 0)
1422             high = -high;
1423           else
1424             low = -low, high = ~high;
1425         }
1426       break;
1427
1428     default:
1429       gcc_unreachable ();
1430     }
1431
1432   *plow = low;
1433   *phigh = high;
1434 }
1435
1436 /* A subroutine of real_to_decimal.  Compute the quotient and remainder
1437    of NUM / DEN.  Return the quotient and place the remainder in NUM.
1438    It is expected that NUM / DEN are close enough that the quotient is
1439    small.  */
1440
1441 static unsigned long
1442 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1443 {
1444   unsigned long q, msb;
1445   int expn = REAL_EXP (num), expd = REAL_EXP (den);
1446
1447   if (expn < expd)
1448     return 0;
1449
1450   q = msb = 0;
1451   goto start;
1452   do
1453     {
1454       msb = num->sig[SIGSZ-1] & SIG_MSB;
1455       q <<= 1;
1456       lshift_significand_1 (num, num);
1457     start:
1458       if (msb || cmp_significands (num, den) >= 0)
1459         {
1460           sub_significands (num, num, den, 0);
1461           q |= 1;
1462         }
1463     }
1464   while (--expn >= expd);
1465
1466   SET_REAL_EXP (num, expd);
1467   normalize (num);
1468
1469   return q;
1470 }
1471
1472 /* Render R as a decimal floating point constant.  Emit DIGITS significant
1473    digits in the result, bounded by BUF_SIZE.  If DIGITS is 0, choose the
1474    maximum for the representation.  If CROP_TRAILING_ZEROS, strip trailing
1475    zeros.  If MODE is VOIDmode, round to nearest value.  Otherwise, round
1476    to a string that, when parsed back in mode MODE, yields the same value.  */
1477
1478 #define M_LOG10_2       0.30102999566398119521
1479
1480 void
1481 real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig,
1482                           size_t buf_size, size_t digits,
1483                           int crop_trailing_zeros, enum machine_mode mode)
1484 {
1485   const struct real_format *fmt = NULL;
1486   const REAL_VALUE_TYPE *one, *ten;
1487   REAL_VALUE_TYPE r, pten, u, v;
1488   int dec_exp, cmp_one, digit;
1489   size_t max_digits;
1490   char *p, *first, *last;
1491   bool sign;
1492   bool round_up;
1493
1494   if (mode != VOIDmode)
1495    {
1496      fmt = REAL_MODE_FORMAT (mode);
1497      gcc_assert (fmt);
1498    }
1499
1500   r = *r_orig;
1501   switch (r.cl)
1502     {
1503     case rvc_zero:
1504       strcpy (str, (r.sign ? "-0.0" : "0.0"));
1505       return;
1506     case rvc_normal:
1507       break;
1508     case rvc_inf:
1509       strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1510       return;
1511     case rvc_nan:
1512       /* ??? Print the significand as well, if not canonical?  */
1513       sprintf (str, "%c%cNaN", (r_orig->sign ? '-' : '+'),
1514                (r_orig->signalling ? 'S' : 'Q'));
1515       return;
1516     default:
1517       gcc_unreachable ();
1518     }
1519
1520   if (r.decimal)
1521     {
1522       decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1523       return;
1524     }
1525
1526   /* Bound the number of digits printed by the size of the representation.  */
1527   max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1528   if (digits == 0 || digits > max_digits)
1529     digits = max_digits;
1530
1531   /* Estimate the decimal exponent, and compute the length of the string it
1532      will print as.  Be conservative and add one to account for possible
1533      overflow or rounding error.  */
1534   dec_exp = REAL_EXP (&r) * M_LOG10_2;
1535   for (max_digits = 1; dec_exp ; max_digits++)
1536     dec_exp /= 10;
1537
1538   /* Bound the number of digits printed by the size of the output buffer.  */
1539   max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1540   gcc_assert (max_digits <= buf_size);
1541   if (digits > max_digits)
1542     digits = max_digits;
1543
1544   one = real_digit (1);
1545   ten = ten_to_ptwo (0);
1546
1547   sign = r.sign;
1548   r.sign = 0;
1549
1550   dec_exp = 0;
1551   pten = *one;
1552
1553   cmp_one = do_compare (&r, one, 0);
1554   if (cmp_one > 0)
1555     {
1556       int m;
1557
1558       /* Number is greater than one.  Convert significand to an integer
1559          and strip trailing decimal zeros.  */
1560
1561       u = r;
1562       SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1563
1564       /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS.  */
1565       m = floor_log2 (max_digits);
1566
1567       /* Iterate over the bits of the possible powers of 10 that might
1568          be present in U and eliminate them.  That is, if we find that
1569          10**2**M divides U evenly, keep the division and increase
1570          DEC_EXP by 2**M.  */
1571       do
1572         {
1573           REAL_VALUE_TYPE t;
1574
1575           do_divide (&t, &u, ten_to_ptwo (m));
1576           do_fix_trunc (&v, &t);
1577           if (cmp_significands (&v, &t) == 0)
1578             {
1579               u = t;
1580               dec_exp += 1 << m;
1581             }
1582         }
1583       while (--m >= 0);
1584
1585       /* Revert the scaling to integer that we performed earlier.  */
1586       SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1587                     - (SIGNIFICAND_BITS - 1));
1588       r = u;
1589
1590       /* Find power of 10.  Do this by dividing out 10**2**M when
1591          this is larger than the current remainder.  Fill PTEN with
1592          the power of 10 that we compute.  */
1593       if (REAL_EXP (&r) > 0)
1594         {
1595           m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1596           do
1597             {
1598               const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1599               if (do_compare (&u, ptentwo, 0) >= 0)
1600                 {
1601                   do_divide (&u, &u, ptentwo);
1602                   do_multiply (&pten, &pten, ptentwo);
1603                   dec_exp += 1 << m;
1604                 }
1605             }
1606           while (--m >= 0);
1607         }
1608       else
1609         /* We managed to divide off enough tens in the above reduction
1610            loop that we've now got a negative exponent.  Fall into the
1611            less-than-one code to compute the proper value for PTEN.  */
1612         cmp_one = -1;
1613     }
1614   if (cmp_one < 0)
1615     {
1616       int m;
1617
1618       /* Number is less than one.  Pad significand with leading
1619          decimal zeros.  */
1620
1621       v = r;
1622       while (1)
1623         {
1624           /* Stop if we'd shift bits off the bottom.  */
1625           if (v.sig[0] & 7)
1626             break;
1627
1628           do_multiply (&u, &v, ten);
1629
1630           /* Stop if we're now >= 1.  */
1631           if (REAL_EXP (&u) > 0)
1632             break;
1633
1634           v = u;
1635           dec_exp -= 1;
1636         }
1637       r = v;
1638
1639       /* Find power of 10.  Do this by multiplying in P=10**2**M when
1640          the current remainder is smaller than 1/P.  Fill PTEN with the
1641          power of 10 that we compute.  */
1642       m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1643       do
1644         {
1645           const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1646           const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1647
1648           if (do_compare (&v, ptenmtwo, 0) <= 0)
1649             {
1650               do_multiply (&v, &v, ptentwo);
1651               do_multiply (&pten, &pten, ptentwo);
1652               dec_exp -= 1 << m;
1653             }
1654         }
1655       while (--m >= 0);
1656
1657       /* Invert the positive power of 10 that we've collected so far.  */
1658       do_divide (&pten, one, &pten);
1659     }
1660
1661   p = str;
1662   if (sign)
1663     *p++ = '-';
1664   first = p++;
1665
1666   /* At this point, PTEN should contain the nearest power of 10 smaller
1667      than R, such that this division produces the first digit.
1668
1669      Using a divide-step primitive that returns the complete integral
1670      remainder avoids the rounding error that would be produced if
1671      we were to use do_divide here and then simply multiply by 10 for
1672      each subsequent digit.  */
1673
1674   digit = rtd_divmod (&r, &pten);
1675
1676   /* Be prepared for error in that division via underflow ...  */
1677   if (digit == 0 && cmp_significand_0 (&r))
1678     {
1679       /* Multiply by 10 and try again.  */
1680       do_multiply (&r, &r, ten);
1681       digit = rtd_divmod (&r, &pten);
1682       dec_exp -= 1;
1683       gcc_assert (digit != 0);
1684     }
1685
1686   /* ... or overflow.  */
1687   if (digit == 10)
1688     {
1689       *p++ = '1';
1690       if (--digits > 0)
1691         *p++ = '0';
1692       dec_exp += 1;
1693     }
1694   else
1695     {
1696       gcc_assert (digit <= 10);
1697       *p++ = digit + '0';
1698     }
1699
1700   /* Generate subsequent digits.  */
1701   while (--digits > 0)
1702     {
1703       do_multiply (&r, &r, ten);
1704       digit = rtd_divmod (&r, &pten);
1705       *p++ = digit + '0';
1706     }
1707   last = p;
1708
1709   /* Generate one more digit with which to do rounding.  */
1710   do_multiply (&r, &r, ten);
1711   digit = rtd_divmod (&r, &pten);
1712
1713   /* Round the result.  */
1714   if (fmt && fmt->round_towards_zero)
1715     {
1716       /* If the format uses round towards zero when parsing the string
1717          back in, we need to always round away from zero here.  */
1718       if (cmp_significand_0 (&r))
1719         digit++;
1720       round_up = digit > 0;
1721     }
1722   else
1723     {
1724       if (digit == 5)
1725         {
1726           /* Round to nearest.  If R is nonzero there are additional
1727              nonzero digits to be extracted.  */
1728           if (cmp_significand_0 (&r))
1729             digit++;
1730           /* Round to even.  */
1731           else if ((p[-1] - '0') & 1)
1732             digit++;
1733         }
1734
1735       round_up = digit > 5;
1736     }
1737
1738   if (round_up)
1739     {
1740       while (p > first)
1741         {
1742           digit = *--p;
1743           if (digit == '9')
1744             *p = '0';
1745           else
1746             {
1747               *p = digit + 1;
1748               break;
1749             }
1750         }
1751
1752       /* Carry out of the first digit.  This means we had all 9's and
1753          now have all 0's.  "Prepend" a 1 by overwriting the first 0.  */
1754       if (p == first)
1755         {
1756           first[1] = '1';
1757           dec_exp++;
1758         }
1759     }
1760
1761   /* Insert the decimal point.  */
1762   first[0] = first[1];
1763   first[1] = '.';
1764
1765   /* If requested, drop trailing zeros.  Never crop past "1.0".  */
1766   if (crop_trailing_zeros)
1767     while (last > first + 3 && last[-1] == '0')
1768       last--;
1769
1770   /* Append the exponent.  */
1771   sprintf (last, "e%+d", dec_exp);
1772
1773 #ifdef ENABLE_CHECKING
1774   /* Verify that we can read the original value back in.  */
1775   if (mode != VOIDmode)
1776     {
1777       real_from_string (&r, str);
1778       real_convert (&r, mode, &r);
1779       gcc_assert (real_identical (&r, r_orig));
1780     }
1781 #endif
1782 }
1783
1784 /* Likewise, except always uses round-to-nearest.  */
1785
1786 void
1787 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1788                  size_t digits, int crop_trailing_zeros)
1789 {
1790   real_to_decimal_for_mode (str, r_orig, buf_size,
1791                             digits, crop_trailing_zeros, VOIDmode);
1792 }
1793
1794 /* Render R as a hexadecimal floating point constant.  Emit DIGITS
1795    significant digits in the result, bounded by BUF_SIZE.  If DIGITS is 0,
1796    choose the maximum for the representation.  If CROP_TRAILING_ZEROS,
1797    strip trailing zeros.  */
1798
1799 void
1800 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1801                      size_t digits, int crop_trailing_zeros)
1802 {
1803   int i, j, exp = REAL_EXP (r);
1804   char *p, *first;
1805   char exp_buf[16];
1806   size_t max_digits;
1807
1808   switch (r->cl)
1809     {
1810     case rvc_zero:
1811       exp = 0;
1812       break;
1813     case rvc_normal:
1814       break;
1815     case rvc_inf:
1816       strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1817       return;
1818     case rvc_nan:
1819       /* ??? Print the significand as well, if not canonical?  */
1820       sprintf (str, "%c%cNaN", (r->sign ? '-' : '+'),
1821                (r->signalling ? 'S' : 'Q'));
1822       return;
1823     default:
1824       gcc_unreachable ();
1825     }
1826
1827   if (r->decimal)
1828     {
1829       /* Hexadecimal format for decimal floats is not interesting. */
1830       strcpy (str, "N/A");
1831       return;
1832     }
1833
1834   if (digits == 0)
1835     digits = SIGNIFICAND_BITS / 4;
1836
1837   /* Bound the number of digits printed by the size of the output buffer.  */
1838
1839   sprintf (exp_buf, "p%+d", exp);
1840   max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1841   gcc_assert (max_digits <= buf_size);
1842   if (digits > max_digits)
1843     digits = max_digits;
1844
1845   p = str;
1846   if (r->sign)
1847     *p++ = '-';
1848   *p++ = '0';
1849   *p++ = 'x';
1850   *p++ = '0';
1851   *p++ = '.';
1852   first = p;
1853
1854   for (i = SIGSZ - 1; i >= 0; --i)
1855     for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1856       {
1857         *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1858         if (--digits == 0)
1859           goto out;
1860       }
1861
1862  out:
1863   if (crop_trailing_zeros)
1864     while (p > first + 1 && p[-1] == '0')
1865       p--;
1866
1867   sprintf (p, "p%+d", exp);
1868 }
1869
1870 /* Initialize R from a decimal or hexadecimal string.  The string is
1871    assumed to have been syntax checked already.  Return -1 if the
1872    value underflows, +1 if overflows, and 0 otherwise. */
1873
1874 int
1875 real_from_string (REAL_VALUE_TYPE *r, const char *str)
1876 {
1877   int exp = 0;
1878   bool sign = false;
1879
1880   get_zero (r, 0);
1881
1882   if (*str == '-')
1883     {
1884       sign = true;
1885       str++;
1886     }
1887   else if (*str == '+')
1888     str++;
1889
1890   if (!strncmp (str, "QNaN", 4))
1891     {
1892       get_canonical_qnan (r, sign);
1893       return 0;
1894     }
1895   else if (!strncmp (str, "SNaN", 4))
1896     {
1897       get_canonical_snan (r, sign);
1898       return 0;
1899     }
1900   else if (!strncmp (str, "Inf", 3))
1901     {
1902       get_inf (r, sign);
1903       return 0;
1904     }
1905
1906   if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1907     {
1908       /* Hexadecimal floating point.  */
1909       int pos = SIGNIFICAND_BITS - 4, d;
1910
1911       str += 2;
1912
1913       while (*str == '0')
1914         str++;
1915       while (1)
1916         {
1917           d = hex_value (*str);
1918           if (d == _hex_bad)
1919             break;
1920           if (pos >= 0)
1921             {
1922               r->sig[pos / HOST_BITS_PER_LONG]
1923                 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1924               pos -= 4;
1925             }
1926           else if (d)
1927             /* Ensure correct rounding by setting last bit if there is
1928                a subsequent nonzero digit.  */
1929             r->sig[0] |= 1;
1930           exp += 4;
1931           str++;
1932         }
1933       if (*str == '.')
1934         {
1935           str++;
1936           if (pos == SIGNIFICAND_BITS - 4)
1937             {
1938               while (*str == '0')
1939                 str++, exp -= 4;
1940             }
1941           while (1)
1942             {
1943               d = hex_value (*str);
1944               if (d == _hex_bad)
1945                 break;
1946               if (pos >= 0)
1947                 {
1948                   r->sig[pos / HOST_BITS_PER_LONG]
1949                     |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1950                   pos -= 4;
1951                 }
1952               else if (d)
1953                 /* Ensure correct rounding by setting last bit if there is
1954                    a subsequent nonzero digit.  */
1955                 r->sig[0] |= 1;
1956               str++;
1957             }
1958         }
1959
1960       /* If the mantissa is zero, ignore the exponent.  */
1961       if (!cmp_significand_0 (r))
1962         goto is_a_zero;
1963
1964       if (*str == 'p' || *str == 'P')
1965         {
1966           bool exp_neg = false;
1967
1968           str++;
1969           if (*str == '-')
1970             {
1971               exp_neg = true;
1972               str++;
1973             }
1974           else if (*str == '+')
1975             str++;
1976
1977           d = 0;
1978           while (ISDIGIT (*str))
1979             {
1980               d *= 10;
1981               d += *str - '0';
1982               if (d > MAX_EXP)
1983                 {
1984                   /* Overflowed the exponent.  */
1985                   if (exp_neg)
1986                     goto underflow;
1987                   else
1988                     goto overflow;
1989                 }
1990               str++;
1991             }
1992           if (exp_neg)
1993             d = -d;
1994
1995           exp += d;
1996         }
1997
1998       r->cl = rvc_normal;
1999       SET_REAL_EXP (r, exp);
2000
2001       normalize (r);
2002     }
2003   else
2004     {
2005       /* Decimal floating point.  */
2006       const REAL_VALUE_TYPE *ten = ten_to_ptwo (0);
2007       int d;
2008
2009       while (*str == '0')
2010         str++;
2011       while (ISDIGIT (*str))
2012         {
2013           d = *str++ - '0';
2014           do_multiply (r, r, ten);
2015           if (d)
2016             do_add (r, r, real_digit (d), 0);
2017         }
2018       if (*str == '.')
2019         {
2020           str++;
2021           if (r->cl == rvc_zero)
2022             {
2023               while (*str == '0')
2024                 str++, exp--;
2025             }
2026           while (ISDIGIT (*str))
2027             {
2028               d = *str++ - '0';
2029               do_multiply (r, r, ten);
2030               if (d)
2031                 do_add (r, r, real_digit (d), 0);
2032               exp--;
2033             }
2034         }
2035
2036       /* If the mantissa is zero, ignore the exponent.  */
2037       if (r->cl == rvc_zero)
2038         goto is_a_zero;
2039
2040       if (*str == 'e' || *str == 'E')
2041         {
2042           bool exp_neg = false;
2043
2044           str++;
2045           if (*str == '-')
2046             {
2047               exp_neg = true;
2048               str++;
2049             }
2050           else if (*str == '+')
2051             str++;
2052
2053           d = 0;
2054           while (ISDIGIT (*str))
2055             {
2056               d *= 10;
2057               d += *str - '0';
2058               if (d > MAX_EXP)
2059                 {
2060                   /* Overflowed the exponent.  */
2061                   if (exp_neg)
2062                     goto underflow;
2063                   else
2064                     goto overflow;
2065                 }
2066               str++;
2067             }
2068           if (exp_neg)
2069             d = -d;
2070           exp += d;
2071         }
2072
2073       if (exp)
2074         times_pten (r, exp);
2075     }
2076
2077   r->sign = sign;
2078   return 0;
2079
2080  is_a_zero:
2081   get_zero (r, sign);
2082   return 0;
2083
2084  underflow:
2085   get_zero (r, sign);
2086   return -1;
2087
2088  overflow:
2089   get_inf (r, sign);
2090   return 1;
2091 }
2092
2093 /* Legacy.  Similar, but return the result directly.  */
2094
2095 REAL_VALUE_TYPE
2096 real_from_string2 (const char *s, enum machine_mode mode)
2097 {
2098   REAL_VALUE_TYPE r;
2099
2100   real_from_string (&r, s);
2101   if (mode != VOIDmode)
2102     real_convert (&r, mode, &r);
2103
2104   return r;
2105 }
2106
2107 /* Initialize R from string S and desired MODE. */
2108
2109 void 
2110 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, enum machine_mode mode)
2111 {
2112   if (DECIMAL_FLOAT_MODE_P (mode))
2113     decimal_real_from_string (r, s);
2114   else
2115     real_from_string (r, s);
2116
2117   if (mode != VOIDmode)
2118     real_convert (r, mode, r);  
2119
2120
2121 /* Initialize R from the integer pair HIGH+LOW.  */
2122
2123 void
2124 real_from_integer (REAL_VALUE_TYPE *r, enum machine_mode mode,
2125                    unsigned HOST_WIDE_INT low, HOST_WIDE_INT high,
2126                    int unsigned_p)
2127 {
2128   if (low == 0 && high == 0)
2129     get_zero (r, 0);
2130   else
2131     {
2132       memset (r, 0, sizeof (*r));
2133       r->cl = rvc_normal;
2134       r->sign = high < 0 && !unsigned_p;
2135       SET_REAL_EXP (r, 2 * HOST_BITS_PER_WIDE_INT);
2136
2137       if (r->sign)
2138         {
2139           high = ~high;
2140           if (low == 0)
2141             high += 1;
2142           else
2143             low = -low;
2144         }
2145
2146       if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2147         {
2148           r->sig[SIGSZ-1] = high;
2149           r->sig[SIGSZ-2] = low;
2150         }
2151       else
2152         {
2153           gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2154           r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1;
2155           r->sig[SIGSZ-2] = high;
2156           r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1;
2157           r->sig[SIGSZ-4] = low;
2158         }
2159
2160       normalize (r);
2161     }
2162
2163   if (mode != VOIDmode)
2164     real_convert (r, mode, r);
2165 }
2166
2167 /* Returns 10**2**N.  */
2168
2169 static const REAL_VALUE_TYPE *
2170 ten_to_ptwo (int n)
2171 {
2172   static REAL_VALUE_TYPE tens[EXP_BITS];
2173
2174   gcc_assert (n >= 0);
2175   gcc_assert (n < EXP_BITS);
2176
2177   if (tens[n].cl == rvc_zero)
2178     {
2179       if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2180         {
2181           HOST_WIDE_INT t = 10;
2182           int i;
2183
2184           for (i = 0; i < n; ++i)
2185             t *= t;
2186
2187           real_from_integer (&tens[n], VOIDmode, t, 0, 1);
2188         }
2189       else
2190         {
2191           const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2192           do_multiply (&tens[n], t, t);
2193         }
2194     }
2195
2196   return &tens[n];
2197 }
2198
2199 /* Returns 10**(-2**N).  */
2200
2201 static const REAL_VALUE_TYPE *
2202 ten_to_mptwo (int n)
2203 {
2204   static REAL_VALUE_TYPE tens[EXP_BITS];
2205
2206   gcc_assert (n >= 0);
2207   gcc_assert (n < EXP_BITS);
2208
2209   if (tens[n].cl == rvc_zero)
2210     do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2211
2212   return &tens[n];
2213 }
2214
2215 /* Returns N.  */
2216
2217 static const REAL_VALUE_TYPE *
2218 real_digit (int n)
2219 {
2220   static REAL_VALUE_TYPE num[10];
2221
2222   gcc_assert (n >= 0);
2223   gcc_assert (n <= 9);
2224
2225   if (n > 0 && num[n].cl == rvc_zero)
2226     real_from_integer (&num[n], VOIDmode, n, 0, 1);
2227
2228   return &num[n];
2229 }
2230
2231 /* Multiply R by 10**EXP.  */
2232
2233 static void
2234 times_pten (REAL_VALUE_TYPE *r, int exp)
2235 {
2236   REAL_VALUE_TYPE pten, *rr;
2237   bool negative = (exp < 0);
2238   int i;
2239
2240   if (negative)
2241     {
2242       exp = -exp;
2243       pten = *real_digit (1);
2244       rr = &pten;
2245     }
2246   else
2247     rr = r;
2248
2249   for (i = 0; exp > 0; ++i, exp >>= 1)
2250     if (exp & 1)
2251       do_multiply (rr, rr, ten_to_ptwo (i));
2252
2253   if (negative)
2254     do_divide (r, r, &pten);
2255 }
2256
2257 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'.  */
2258
2259 const REAL_VALUE_TYPE *
2260 dconst_e_ptr (void)
2261 {
2262   static REAL_VALUE_TYPE value;
2263
2264   /* Initialize mathematical constants for constant folding builtins.
2265      These constants need to be given to at least 160 bits precision.  */
2266   if (value.cl == rvc_zero)
2267     {
2268       mpfr_t m;
2269       mpfr_init2 (m, SIGNIFICAND_BITS);
2270       mpfr_set_ui (m, 1, GMP_RNDN);
2271       mpfr_exp (m, m, GMP_RNDN);
2272       real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2273       mpfr_clear (m);
2274       
2275     }
2276   return &value;
2277 }
2278
2279 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3.  */
2280
2281 const REAL_VALUE_TYPE *
2282 dconst_third_ptr (void)
2283 {
2284   static REAL_VALUE_TYPE value;
2285
2286   /* Initialize mathematical constants for constant folding builtins.
2287      These constants need to be given to at least 160 bits precision.  */
2288   if (value.cl == rvc_zero)
2289     {
2290       real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (3));
2291     }
2292   return &value;
2293 }
2294
2295 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2).  */
2296
2297 const REAL_VALUE_TYPE *
2298 dconst_sqrt2_ptr (void)
2299 {
2300   static REAL_VALUE_TYPE value;
2301
2302   /* Initialize mathematical constants for constant folding builtins.
2303      These constants need to be given to at least 160 bits precision.  */
2304   if (value.cl == rvc_zero)
2305     {
2306       mpfr_t m;
2307       mpfr_init2 (m, SIGNIFICAND_BITS);
2308       mpfr_sqrt_ui (m, 2, GMP_RNDN);
2309       real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2310       mpfr_clear (m);
2311     }
2312   return &value;
2313 }
2314
2315 /* Fills R with +Inf.  */
2316
2317 void
2318 real_inf (REAL_VALUE_TYPE *r)
2319 {
2320   get_inf (r, 0);
2321 }
2322
2323 /* Fills R with a NaN whose significand is described by STR.  If QUIET,
2324    we force a QNaN, else we force an SNaN.  The string, if not empty,
2325    is parsed as a number and placed in the significand.  Return true
2326    if the string was successfully parsed.  */
2327
2328 bool
2329 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2330           enum machine_mode mode)
2331 {
2332   const struct real_format *fmt;
2333
2334   fmt = REAL_MODE_FORMAT (mode);
2335   gcc_assert (fmt);
2336
2337   if (*str == 0)
2338     {
2339       if (quiet)
2340         get_canonical_qnan (r, 0);
2341       else
2342         get_canonical_snan (r, 0);
2343     }
2344   else
2345     {
2346       int base = 10, d;
2347
2348       memset (r, 0, sizeof (*r));
2349       r->cl = rvc_nan;
2350
2351       /* Parse akin to strtol into the significand of R.  */
2352
2353       while (ISSPACE (*str))
2354         str++;
2355       if (*str == '-')
2356         str++;
2357       else if (*str == '+')
2358         str++;
2359       if (*str == '0')
2360         {
2361           str++;
2362           if (*str == 'x' || *str == 'X')
2363             {
2364               base = 16;
2365               str++;
2366             }
2367           else
2368             base = 8;
2369         }
2370
2371       while ((d = hex_value (*str)) < base)
2372         {
2373           REAL_VALUE_TYPE u;
2374
2375           switch (base)
2376             {
2377             case 8:
2378               lshift_significand (r, r, 3);
2379               break;
2380             case 16:
2381               lshift_significand (r, r, 4);
2382               break;
2383             case 10:
2384               lshift_significand_1 (&u, r);
2385               lshift_significand (r, r, 3);
2386               add_significands (r, r, &u);
2387               break;
2388             default:
2389               gcc_unreachable ();
2390             }
2391
2392           get_zero (&u, 0);
2393           u.sig[0] = d;
2394           add_significands (r, r, &u);
2395
2396           str++;
2397         }
2398
2399       /* Must have consumed the entire string for success.  */
2400       if (*str != 0)
2401         return false;
2402
2403       /* Shift the significand into place such that the bits
2404          are in the most significant bits for the format.  */
2405       lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2406
2407       /* Our MSB is always unset for NaNs.  */
2408       r->sig[SIGSZ-1] &= ~SIG_MSB;
2409
2410       /* Force quiet or signalling NaN.  */
2411       r->signalling = !quiet;
2412     }
2413
2414   return true;
2415 }
2416
2417 /* Fills R with the largest finite value representable in mode MODE.
2418    If SIGN is nonzero, R is set to the most negative finite value.  */
2419
2420 void
2421 real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
2422 {
2423   const struct real_format *fmt;
2424   int np2;
2425
2426   fmt = REAL_MODE_FORMAT (mode);
2427   gcc_assert (fmt);
2428   memset (r, 0, sizeof (*r));
2429   
2430   if (fmt->b == 10)
2431     decimal_real_maxval (r, sign, mode);
2432   else
2433     {
2434       r->cl = rvc_normal;
2435       r->sign = sign;
2436       SET_REAL_EXP (r, fmt->emax);
2437
2438       np2 = SIGNIFICAND_BITS - fmt->p;
2439       memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2440       clear_significand_below (r, np2);
2441
2442       if (fmt->pnan < fmt->p)
2443         /* This is an IBM extended double format made up of two IEEE
2444            doubles.  The value of the long double is the sum of the
2445            values of the two parts.  The most significant part is
2446            required to be the value of the long double rounded to the
2447            nearest double.  Rounding means we need a slightly smaller
2448            value for LDBL_MAX.  */
2449         clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1);
2450     }
2451 }
2452
2453 /* Fills R with 2**N.  */
2454
2455 void
2456 real_2expN (REAL_VALUE_TYPE *r, int n, enum machine_mode fmode)
2457 {
2458   memset (r, 0, sizeof (*r));
2459
2460   n++;
2461   if (n > MAX_EXP)
2462     r->cl = rvc_inf;
2463   else if (n < -MAX_EXP)
2464     ;
2465   else
2466     {
2467       r->cl = rvc_normal;
2468       SET_REAL_EXP (r, n);
2469       r->sig[SIGSZ-1] = SIG_MSB;
2470     }
2471   if (DECIMAL_FLOAT_MODE_P (fmode))
2472     decimal_real_convert (r, fmode, r);
2473 }
2474
2475 \f
2476 static void
2477 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2478 {
2479   int p2, np2, i, w;
2480   int emin2m1, emax2;
2481   bool round_up = false;
2482
2483   if (r->decimal)
2484     {
2485       if (fmt->b == 10)
2486         {
2487           decimal_round_for_format (fmt, r);
2488           return;
2489         }
2490       /* FIXME. We can come here via fp_easy_constant
2491          (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2492          investigated whether this convert needs to be here, or
2493          something else is missing. */
2494       decimal_real_convert (r, DFmode, r);
2495     }
2496
2497   p2 = fmt->p;
2498   emin2m1 = fmt->emin - 1;
2499   emax2 = fmt->emax;
2500
2501   np2 = SIGNIFICAND_BITS - p2;
2502   switch (r->cl)
2503     {
2504     underflow:
2505       get_zero (r, r->sign);
2506     case rvc_zero:
2507       if (!fmt->has_signed_zero)
2508         r->sign = 0;
2509       return;
2510
2511     overflow:
2512       get_inf (r, r->sign);
2513     case rvc_inf:
2514       return;
2515
2516     case rvc_nan:
2517       clear_significand_below (r, np2);
2518       return;
2519
2520     case rvc_normal:
2521       break;
2522
2523     default:
2524       gcc_unreachable ();
2525     }
2526
2527   /* Check the range of the exponent.  If we're out of range,
2528      either underflow or overflow.  */
2529   if (REAL_EXP (r) > emax2)
2530     goto overflow;
2531   else if (REAL_EXP (r) <= emin2m1)
2532     {
2533       int diff;
2534
2535       if (!fmt->has_denorm)
2536         {
2537           /* Don't underflow completely until we've had a chance to round.  */
2538           if (REAL_EXP (r) < emin2m1)
2539             goto underflow;
2540         }
2541       else
2542         {
2543           diff = emin2m1 - REAL_EXP (r) + 1;
2544           if (diff > p2)
2545             goto underflow;
2546
2547           /* De-normalize the significand.  */
2548           r->sig[0] |= sticky_rshift_significand (r, r, diff);
2549           SET_REAL_EXP (r, REAL_EXP (r) + diff);
2550         }
2551     }
2552
2553   if (!fmt->round_towards_zero)
2554     {
2555       /* There are P2 true significand bits, followed by one guard bit,
2556          followed by one sticky bit, followed by stuff.  Fold nonzero
2557          stuff into the sticky bit.  */
2558       unsigned long sticky;
2559       bool guard, lsb;
2560
2561       sticky = 0;
2562       for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2563         sticky |= r->sig[i];
2564       sticky |= r->sig[w]
2565                 & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2566
2567       guard = test_significand_bit (r, np2 - 1);
2568       lsb = test_significand_bit (r, np2);
2569
2570       /* Round to even.  */
2571       round_up = guard && (sticky || lsb);
2572     }
2573
2574   if (round_up)
2575     {
2576       REAL_VALUE_TYPE u;
2577       get_zero (&u, 0);
2578       set_significand_bit (&u, np2);
2579
2580       if (add_significands (r, r, &u))
2581         {
2582           /* Overflow.  Means the significand had been all ones, and
2583              is now all zeros.  Need to increase the exponent, and
2584              possibly re-normalize it.  */
2585           SET_REAL_EXP (r, REAL_EXP (r) + 1);
2586           if (REAL_EXP (r) > emax2)
2587             goto overflow;
2588           r->sig[SIGSZ-1] = SIG_MSB;
2589         }
2590     }
2591
2592   /* Catch underflow that we deferred until after rounding.  */
2593   if (REAL_EXP (r) <= emin2m1)
2594     goto underflow;
2595
2596   /* Clear out trailing garbage.  */
2597   clear_significand_below (r, np2);
2598 }
2599
2600 /* Extend or truncate to a new mode.  */
2601
2602 void
2603 real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
2604               const REAL_VALUE_TYPE *a)
2605 {
2606   const struct real_format *fmt;
2607
2608   fmt = REAL_MODE_FORMAT (mode);
2609   gcc_assert (fmt);
2610
2611   *r = *a;
2612
2613   if (a->decimal || fmt->b == 10)
2614     decimal_real_convert (r, mode, a);
2615
2616   round_for_format (fmt, r);
2617
2618   /* round_for_format de-normalizes denormals.  Undo just that part.  */
2619   if (r->cl == rvc_normal)
2620     normalize (r);
2621 }
2622
2623 /* Legacy.  Likewise, except return the struct directly.  */
2624
2625 REAL_VALUE_TYPE
2626 real_value_truncate (enum machine_mode mode, REAL_VALUE_TYPE a)
2627 {
2628   REAL_VALUE_TYPE r;
2629   real_convert (&r, mode, &a);
2630   return r;
2631 }
2632
2633 /* Return true if truncating to MODE is exact.  */
2634
2635 bool
2636 exact_real_truncate (enum machine_mode mode, const REAL_VALUE_TYPE *a)
2637 {
2638   const struct real_format *fmt;
2639   REAL_VALUE_TYPE t;
2640   int emin2m1;
2641
2642   fmt = REAL_MODE_FORMAT (mode);
2643   gcc_assert (fmt);
2644
2645   /* Don't allow conversion to denormals.  */
2646   emin2m1 = fmt->emin - 1;
2647   if (REAL_EXP (a) <= emin2m1)
2648     return false;
2649
2650   /* After conversion to the new mode, the value must be identical.  */
2651   real_convert (&t, mode, a);
2652   return real_identical (&t, a);
2653 }
2654
2655 /* Write R to the given target format.  Place the words of the result
2656    in target word order in BUF.  There are always 32 bits in each
2657    long, no matter the size of the host long.
2658
2659    Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE.  */
2660
2661 long
2662 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
2663                     const struct real_format *fmt)
2664 {
2665   REAL_VALUE_TYPE r;
2666   long buf1;
2667
2668   r = *r_orig;
2669   round_for_format (fmt, &r);
2670
2671   if (!buf)
2672     buf = &buf1;
2673   (*fmt->encode) (fmt, buf, &r);
2674
2675   return *buf;
2676 }
2677
2678 /* Similar, but look up the format from MODE.  */
2679
2680 long
2681 real_to_target (long *buf, const REAL_VALUE_TYPE *r, enum machine_mode mode)
2682 {
2683   const struct real_format *fmt;
2684
2685   fmt = REAL_MODE_FORMAT (mode);
2686   gcc_assert (fmt);
2687
2688   return real_to_target_fmt (buf, r, fmt);
2689 }
2690
2691 /* Read R from the given target format.  Read the words of the result
2692    in target word order in BUF.  There are always 32 bits in each
2693    long, no matter the size of the host long.  */
2694
2695 void
2696 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
2697                       const struct real_format *fmt)
2698 {
2699   (*fmt->decode) (fmt, r, buf);
2700 }
2701
2702 /* Similar, but look up the format from MODE.  */
2703
2704 void
2705 real_from_target (REAL_VALUE_TYPE *r, const long *buf, enum machine_mode mode)
2706 {
2707   const struct real_format *fmt;
2708
2709   fmt = REAL_MODE_FORMAT (mode);
2710   gcc_assert (fmt);
2711
2712   (*fmt->decode) (fmt, r, buf);
2713 }
2714
2715 /* Return the number of bits of the largest binary value that the
2716    significand of MODE will hold.  */
2717 /* ??? Legacy.  Should get access to real_format directly.  */
2718
2719 int
2720 significand_size (enum machine_mode mode)
2721 {
2722   const struct real_format *fmt;
2723
2724   fmt = REAL_MODE_FORMAT (mode);
2725   if (fmt == NULL)
2726     return 0;
2727
2728   if (fmt->b == 10)
2729     {
2730       /* Return the size in bits of the largest binary value that can be
2731          held by the decimal coefficient for this mode.  This is one more
2732          than the number of bits required to hold the largest coefficient
2733          of this mode.  */
2734       double log2_10 = 3.3219281;
2735       return fmt->p * log2_10;
2736     }
2737   return fmt->p;
2738 }
2739
2740 /* Return a hash value for the given real value.  */
2741 /* ??? The "unsigned int" return value is intended to be hashval_t,
2742    but I didn't want to pull hashtab.h into real.h.  */
2743
2744 unsigned int
2745 real_hash (const REAL_VALUE_TYPE *r)
2746 {
2747   unsigned int h;
2748   size_t i;
2749
2750   h = r->cl | (r->sign << 2);
2751   switch (r->cl)
2752     {
2753     case rvc_zero:
2754     case rvc_inf:
2755       return h;
2756
2757     case rvc_normal:
2758       h |= REAL_EXP (r) << 3;
2759       break;
2760
2761     case rvc_nan:
2762       if (r->signalling)
2763         h ^= (unsigned int)-1;
2764       if (r->canonical)
2765         return h;
2766       break;
2767
2768     default:
2769       gcc_unreachable ();
2770     }
2771
2772   if (sizeof(unsigned long) > sizeof(unsigned int))
2773     for (i = 0; i < SIGSZ; ++i)
2774       {
2775         unsigned long s = r->sig[i];
2776         h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2777       }
2778   else
2779     for (i = 0; i < SIGSZ; ++i)
2780       h ^= r->sig[i];
2781
2782   return h;
2783 }
2784 \f
2785 /* IEEE single-precision format.  */
2786
2787 static void encode_ieee_single (const struct real_format *fmt,
2788                                 long *, const REAL_VALUE_TYPE *);
2789 static void decode_ieee_single (const struct real_format *,
2790                                 REAL_VALUE_TYPE *, const long *);
2791
2792 static void
2793 encode_ieee_single (const struct real_format *fmt, long *buf,
2794                     const REAL_VALUE_TYPE *r)
2795 {
2796   unsigned long image, sig, exp;
2797   unsigned long sign = r->sign;
2798   bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2799
2800   image = sign << 31;
2801   sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2802
2803   switch (r->cl)
2804     {
2805     case rvc_zero:
2806       break;
2807
2808     case rvc_inf:
2809       if (fmt->has_inf)
2810         image |= 255 << 23;
2811       else
2812         image |= 0x7fffffff;
2813       break;
2814
2815     case rvc_nan:
2816       if (fmt->has_nans)
2817         {
2818           if (r->canonical)
2819             sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
2820           if (r->signalling == fmt->qnan_msb_set)
2821             sig &= ~(1 << 22);
2822           else
2823             sig |= 1 << 22;
2824           if (sig == 0)
2825             sig = 1 << 21;
2826
2827           image |= 255 << 23;
2828           image |= sig;
2829         }
2830       else
2831         image |= 0x7fffffff;
2832       break;
2833
2834     case rvc_normal:
2835       /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2836          whereas the intermediate representation is 0.F x 2**exp.
2837          Which means we're off by one.  */
2838       if (denormal)
2839         exp = 0;
2840       else
2841       exp = REAL_EXP (r) + 127 - 1;
2842       image |= exp << 23;
2843       image |= sig;
2844       break;
2845
2846     default:
2847       gcc_unreachable ();
2848     }
2849
2850   buf[0] = image;
2851 }
2852
2853 static void
2854 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2855                     const long *buf)
2856 {
2857   unsigned long image = buf[0] & 0xffffffff;
2858   bool sign = (image >> 31) & 1;
2859   int exp = (image >> 23) & 0xff;
2860
2861   memset (r, 0, sizeof (*r));
2862   image <<= HOST_BITS_PER_LONG - 24;
2863   image &= ~SIG_MSB;
2864
2865   if (exp == 0)
2866     {
2867       if (image && fmt->has_denorm)
2868         {
2869           r->cl = rvc_normal;
2870           r->sign = sign;
2871           SET_REAL_EXP (r, -126);
2872           r->sig[SIGSZ-1] = image << 1;
2873           normalize (r);
2874         }
2875       else if (fmt->has_signed_zero)
2876         r->sign = sign;
2877     }
2878   else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
2879     {
2880       if (image)
2881         {
2882           r->cl = rvc_nan;
2883           r->sign = sign;
2884           r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
2885                            ^ fmt->qnan_msb_set);
2886           r->sig[SIGSZ-1] = image;
2887         }
2888       else
2889         {
2890           r->cl = rvc_inf;
2891           r->sign = sign;
2892         }
2893     }
2894   else
2895     {
2896       r->cl = rvc_normal;
2897       r->sign = sign;
2898       SET_REAL_EXP (r, exp - 127 + 1);
2899       r->sig[SIGSZ-1] = image | SIG_MSB;
2900     }
2901 }
2902
2903 const struct real_format ieee_single_format =
2904   {
2905     encode_ieee_single,
2906     decode_ieee_single,
2907     2,
2908     24,
2909     24,
2910     -125,
2911     128,
2912     31,
2913     31,
2914     false,
2915     true,
2916     true,
2917     true,
2918     true,
2919     true,
2920     true,
2921     false
2922   };
2923
2924 const struct real_format mips_single_format =
2925   {
2926     encode_ieee_single,
2927     decode_ieee_single,
2928     2,
2929     24,
2930     24,
2931     -125,
2932     128,
2933     31,
2934     31,
2935     false,
2936     true,
2937     true,
2938     true,
2939     true,
2940     true,
2941     false,
2942     true
2943   };
2944
2945 const struct real_format motorola_single_format =
2946   {
2947     encode_ieee_single,
2948     decode_ieee_single,
2949     2,
2950     24,
2951     24,
2952     -125,
2953     128,
2954     31,
2955     31,
2956     false,
2957     true,
2958     true,
2959     true,
2960     true,
2961     true,
2962     true,
2963     true
2964   };
2965
2966 /*  SPU Single Precision (Extended-Range Mode) format is the same as IEEE
2967     single precision with the following differences:
2968       - Infinities are not supported.  Instead MAX_FLOAT or MIN_FLOAT
2969         are generated.
2970       - NaNs are not supported.
2971       - The range of non-zero numbers in binary is
2972         (001)[1.]000...000 to (255)[1.]111...111.
2973       - Denormals can be represented, but are treated as +0.0 when
2974         used as an operand and are never generated as a result.
2975       - -0.0 can be represented, but a zero result is always +0.0.
2976       - the only supported rounding mode is trunction (towards zero).  */
2977 const struct real_format spu_single_format =
2978   {
2979     encode_ieee_single,
2980     decode_ieee_single,
2981     2,
2982     24,
2983     24,
2984     -125,
2985     129,
2986     31,
2987     31,
2988     true,
2989     false,
2990     false,
2991     false,
2992     true,
2993     true,
2994     false,
2995     false
2996   };
2997 \f
2998 /* IEEE double-precision format.  */
2999
3000 static void encode_ieee_double (const struct real_format *fmt,
3001                                 long *, const REAL_VALUE_TYPE *);
3002 static void decode_ieee_double (const struct real_format *,
3003                                 REAL_VALUE_TYPE *, const long *);
3004
3005 static void
3006 encode_ieee_double (const struct real_format *fmt, long *buf,
3007                     const REAL_VALUE_TYPE *r)
3008 {
3009   unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
3010   bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3011
3012   image_hi = r->sign << 31;
3013   image_lo = 0;
3014
3015   if (HOST_BITS_PER_LONG == 64)
3016     {
3017       sig_hi = r->sig[SIGSZ-1];
3018       sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
3019       sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
3020     }
3021   else
3022     {
3023       sig_hi = r->sig[SIGSZ-1];
3024       sig_lo = r->sig[SIGSZ-2];
3025       sig_lo = (sig_hi << 21) | (sig_lo >> 11);
3026       sig_hi = (sig_hi >> 11) & 0xfffff;
3027     }
3028
3029   switch (r->cl)
3030     {
3031     case rvc_zero:
3032       break;
3033
3034     case rvc_inf:
3035       if (fmt->has_inf)
3036         image_hi |= 2047 << 20;
3037       else
3038         {
3039           image_hi |= 0x7fffffff;
3040           image_lo = 0xffffffff;
3041         }
3042       break;
3043
3044     case rvc_nan:
3045       if (fmt->has_nans)
3046         {
3047           if (r->canonical)
3048             {
3049               if (fmt->canonical_nan_lsbs_set)
3050                 {
3051                   sig_hi = (1 << 19) - 1;
3052                   sig_lo = 0xffffffff;
3053                 }
3054               else
3055                 {
3056                   sig_hi = 0;
3057                   sig_lo = 0;
3058                 }
3059             }
3060           if (r->signalling == fmt->qnan_msb_set)
3061             sig_hi &= ~(1 << 19);
3062           else
3063             sig_hi |= 1 << 19;
3064           if (sig_hi == 0 && sig_lo == 0)
3065             sig_hi = 1 << 18;
3066
3067           image_hi |= 2047 << 20;
3068           image_hi |= sig_hi;
3069           image_lo = sig_lo;
3070         }
3071       else
3072         {
3073           image_hi |= 0x7fffffff;
3074           image_lo = 0xffffffff;
3075         }
3076       break;
3077
3078     case rvc_normal:
3079       /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3080          whereas the intermediate representation is 0.F x 2**exp.
3081          Which means we're off by one.  */
3082       if (denormal)
3083         exp = 0;
3084       else
3085         exp = REAL_EXP (r) + 1023 - 1;
3086       image_hi |= exp << 20;
3087       image_hi |= sig_hi;
3088       image_lo = sig_lo;
3089       break;
3090
3091     default:
3092       gcc_unreachable ();
3093     }
3094
3095   if (FLOAT_WORDS_BIG_ENDIAN)
3096     buf[0] = image_hi, buf[1] = image_lo;
3097   else
3098     buf[0] = image_lo, buf[1] = image_hi;
3099 }
3100
3101 static void
3102 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3103                     const long *buf)
3104 {
3105   unsigned long image_hi, image_lo;
3106   bool sign;
3107   int exp;
3108
3109   if (FLOAT_WORDS_BIG_ENDIAN)
3110     image_hi = buf[0], image_lo = buf[1];
3111   else
3112     image_lo = buf[0], image_hi = buf[1];
3113   image_lo &= 0xffffffff;
3114   image_hi &= 0xffffffff;
3115
3116   sign = (image_hi >> 31) & 1;
3117   exp = (image_hi >> 20) & 0x7ff;
3118
3119   memset (r, 0, sizeof (*r));
3120
3121   image_hi <<= 32 - 21;
3122   image_hi |= image_lo >> 21;
3123   image_hi &= 0x7fffffff;
3124   image_lo <<= 32 - 21;
3125
3126   if (exp == 0)
3127     {
3128       if ((image_hi || image_lo) && fmt->has_denorm)
3129         {
3130           r->cl = rvc_normal;
3131           r->sign = sign;
3132           SET_REAL_EXP (r, -1022);
3133           if (HOST_BITS_PER_LONG == 32)
3134             {
3135               image_hi = (image_hi << 1) | (image_lo >> 31);
3136               image_lo <<= 1;
3137               r->sig[SIGSZ-1] = image_hi;
3138               r->sig[SIGSZ-2] = image_lo;
3139             }
3140           else
3141             {
3142               image_hi = (image_hi << 31 << 2) | (image_lo << 1);
3143               r->sig[SIGSZ-1] = image_hi;
3144             }
3145           normalize (r);
3146         }
3147       else if (fmt->has_signed_zero)
3148         r->sign = sign;
3149     }
3150   else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
3151     {
3152       if (image_hi || image_lo)
3153         {
3154           r->cl = rvc_nan;
3155           r->sign = sign;
3156           r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3157           if (HOST_BITS_PER_LONG == 32)
3158             {
3159               r->sig[SIGSZ-1] = image_hi;
3160               r->sig[SIGSZ-2] = image_lo;
3161             }
3162           else
3163             r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3164         }
3165       else
3166         {
3167           r->cl = rvc_inf;
3168           r->sign = sign;
3169         }
3170     }
3171   else
3172     {
3173       r->cl = rvc_normal;
3174       r->sign = sign;
3175       SET_REAL_EXP (r, exp - 1023 + 1);
3176       if (HOST_BITS_PER_LONG == 32)
3177         {
3178           r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3179           r->sig[SIGSZ-2] = image_lo;
3180         }
3181       else
3182         r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3183     }
3184 }
3185
3186 const struct real_format ieee_double_format =
3187   {
3188     encode_ieee_double,
3189     decode_ieee_double,
3190     2,
3191     53,
3192     53,
3193     -1021,
3194     1024,
3195     63,
3196     63,
3197     false,
3198     true,
3199     true,
3200     true,
3201     true,
3202     true,
3203     true,
3204     false
3205   };
3206
3207 const struct real_format mips_double_format =
3208   {
3209     encode_ieee_double,
3210     decode_ieee_double,
3211     2,
3212     53,
3213     53,
3214     -1021,
3215     1024,
3216     63,
3217     63,
3218     false,
3219     true,
3220     true,
3221     true,
3222     true,
3223     true,
3224     false,
3225     true
3226   };
3227
3228 const struct real_format motorola_double_format =
3229   {
3230     encode_ieee_double,
3231     decode_ieee_double,
3232     2,
3233     53,
3234     53,
3235     -1021,
3236     1024,
3237     63,
3238     63,
3239     false,
3240     true,
3241     true,
3242     true,
3243     true,
3244     true,
3245     true,
3246     true
3247   };
3248 \f
3249 /* IEEE extended real format.  This comes in three flavors: Intel's as
3250    a 12 byte image, Intel's as a 16 byte image, and Motorola's.  Intel
3251    12- and 16-byte images may be big- or little endian; Motorola's is
3252    always big endian.  */
3253
3254 /* Helper subroutine which converts from the internal format to the
3255    12-byte little-endian Intel format.  Functions below adjust this
3256    for the other possible formats.  */
3257 static void
3258 encode_ieee_extended (const struct real_format *fmt, long *buf,
3259                       const REAL_VALUE_TYPE *r)
3260 {
3261   unsigned long image_hi, sig_hi, sig_lo;
3262   bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3263
3264   image_hi = r->sign << 15;
3265   sig_hi = sig_lo = 0;
3266
3267   switch (r->cl)
3268     {
3269     case rvc_zero:
3270       break;
3271
3272     case rvc_inf:
3273       if (fmt->has_inf)
3274         {
3275           image_hi |= 32767;
3276
3277           /* Intel requires the explicit integer bit to be set, otherwise
3278              it considers the value a "pseudo-infinity".  Motorola docs
3279              say it doesn't care.  */
3280           sig_hi = 0x80000000;
3281         }
3282       else
3283         {
3284           image_hi |= 32767;
3285           sig_lo = sig_hi = 0xffffffff;
3286         }
3287       break;
3288
3289     case rvc_nan:
3290       if (fmt->has_nans)
3291         {
3292           image_hi |= 32767;
3293           if (r->canonical)
3294             {
3295               if (fmt->canonical_nan_lsbs_set)
3296                 {
3297                   sig_hi = (1 << 30) - 1;
3298                   sig_lo = 0xffffffff;
3299                 }
3300             }
3301           else if (HOST_BITS_PER_LONG == 32)
3302             {
3303               sig_hi = r->sig[SIGSZ-1];
3304               sig_lo = r->sig[SIGSZ-2];
3305             }
3306           else
3307             {
3308               sig_lo = r->sig[SIGSZ-1];
3309               sig_hi = sig_lo >> 31 >> 1;
3310               sig_lo &= 0xffffffff;
3311             }
3312           if (r->signalling == fmt->qnan_msb_set)
3313             sig_hi &= ~(1 << 30);
3314           else
3315             sig_hi |= 1 << 30;
3316           if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3317             sig_hi = 1 << 29;
3318
3319           /* Intel requires the explicit integer bit to be set, otherwise
3320              it considers the value a "pseudo-nan".  Motorola docs say it
3321              doesn't care.  */
3322           sig_hi |= 0x80000000;
3323         }
3324       else
3325         {
3326           image_hi |= 32767;
3327           sig_lo = sig_hi = 0xffffffff;
3328         }
3329       break;
3330
3331     case rvc_normal:
3332       {
3333         int exp = REAL_EXP (r);
3334
3335         /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3336            whereas the intermediate representation is 0.F x 2**exp.
3337            Which means we're off by one.
3338
3339            Except for Motorola, which consider exp=0 and explicit
3340            integer bit set to continue to be normalized.  In theory
3341            this discrepancy has been taken care of by the difference
3342            in fmt->emin in round_for_format.  */
3343
3344         if (denormal)
3345           exp = 0;
3346         else
3347           {
3348             exp += 16383 - 1;
3349             gcc_assert (exp >= 0);
3350           }
3351         image_hi |= exp;
3352
3353         if (HOST_BITS_PER_LONG == 32)
3354           {
3355             sig_hi = r->sig[SIGSZ-1];
3356             sig_lo = r->sig[SIGSZ-2];
3357           }
3358         else
3359           {
3360             sig_lo = r->sig[SIGSZ-1];
3361             sig_hi = sig_lo >> 31 >> 1;
3362             sig_lo &= 0xffffffff;
3363           }
3364       }
3365       break;
3366
3367     default:
3368       gcc_unreachable ();
3369     }
3370
3371   buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3372 }
3373
3374 /* Convert from the internal format to the 12-byte Motorola format
3375    for an IEEE extended real.  */
3376 static void
3377 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3378                                const REAL_VALUE_TYPE *r)
3379 {
3380   long intermed[3];
3381   encode_ieee_extended (fmt, intermed, r);
3382
3383   /* Motorola chips are assumed always to be big-endian.  Also, the
3384      padding in a Motorola extended real goes between the exponent and
3385      the mantissa.  At this point the mantissa is entirely within
3386      elements 0 and 1 of intermed, and the exponent entirely within
3387      element 2, so all we have to do is swap the order around, and
3388      shift element 2 left 16 bits.  */
3389   buf[0] = intermed[2] << 16;
3390   buf[1] = intermed[1];
3391   buf[2] = intermed[0];
3392 }
3393
3394 /* Convert from the internal format to the 12-byte Intel format for
3395    an IEEE extended real.  */
3396 static void
3397 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3398                                const REAL_VALUE_TYPE *r)
3399 {
3400   if (FLOAT_WORDS_BIG_ENDIAN)
3401     {
3402       /* All the padding in an Intel-format extended real goes at the high
3403          end, which in this case is after the mantissa, not the exponent.
3404          Therefore we must shift everything down 16 bits.  */
3405       long intermed[3];
3406       encode_ieee_extended (fmt, intermed, r);
3407       buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3408       buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3409       buf[2] =  (intermed[0] << 16);
3410     }
3411   else
3412     /* encode_ieee_extended produces what we want directly.  */
3413     encode_ieee_extended (fmt, buf, r);
3414 }
3415
3416 /* Convert from the internal format to the 16-byte Intel format for
3417    an IEEE extended real.  */
3418 static void
3419 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3420                                 const REAL_VALUE_TYPE *r)
3421 {
3422   /* All the padding in an Intel-format extended real goes at the high end.  */
3423   encode_ieee_extended_intel_96 (fmt, buf, r);
3424   buf[3] = 0;
3425 }
3426
3427 /* As above, we have a helper function which converts from 12-byte
3428    little-endian Intel format to internal format.  Functions below
3429    adjust for the other possible formats.  */
3430 static void
3431 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3432                       const long *buf)
3433 {
3434   unsigned long image_hi, sig_hi, sig_lo;
3435   bool sign;
3436   int exp;
3437
3438   sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3439   sig_lo &= 0xffffffff;
3440   sig_hi &= 0xffffffff;
3441   image_hi &= 0xffffffff;
3442
3443   sign = (image_hi >> 15) & 1;
3444   exp = image_hi & 0x7fff;
3445
3446   memset (r, 0, sizeof (*r));
3447
3448   if (exp == 0)
3449     {
3450       if ((sig_hi || sig_lo) && fmt->has_denorm)
3451         {
3452           r->cl = rvc_normal;
3453           r->sign = sign;
3454
3455           /* When the IEEE format contains a hidden bit, we know that
3456              it's zero at this point, and so shift up the significand
3457              and decrease the exponent to match.  In this case, Motorola
3458              defines the explicit integer bit to be valid, so we don't
3459              know whether the msb is set or not.  */
3460           SET_REAL_EXP (r, fmt->emin);
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           normalize (r);
3470         }
3471       else if (fmt->has_signed_zero)
3472         r->sign = sign;
3473     }
3474   else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3475     {
3476       /* See above re "pseudo-infinities" and "pseudo-nans".
3477          Short summary is that the MSB will likely always be
3478          set, and that we don't care about it.  */
3479       sig_hi &= 0x7fffffff;
3480
3481       if (sig_hi || sig_lo)
3482         {
3483           r->cl = rvc_nan;
3484           r->sign = sign;
3485           r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3486           if (HOST_BITS_PER_LONG == 32)
3487             {
3488               r->sig[SIGSZ-1] = sig_hi;
3489               r->sig[SIGSZ-2] = sig_lo;
3490             }
3491           else
3492             r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3493         }
3494       else
3495         {
3496           r->cl = rvc_inf;
3497           r->sign = sign;
3498         }
3499     }
3500   else
3501     {
3502       r->cl = rvc_normal;
3503       r->sign = sign;
3504       SET_REAL_EXP (r, exp - 16383 + 1);
3505       if (HOST_BITS_PER_LONG == 32)
3506         {
3507           r->sig[SIGSZ-1] = sig_hi;
3508           r->sig[SIGSZ-2] = sig_lo;
3509         }
3510       else
3511         r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3512     }
3513 }
3514
3515 /* Convert from the internal format to the 12-byte Motorola format
3516    for an IEEE extended real.  */
3517 static void
3518 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3519                                const long *buf)
3520 {
3521   long intermed[3];
3522
3523   /* Motorola chips are assumed always to be big-endian.  Also, the
3524      padding in a Motorola extended real goes between the exponent and
3525      the mantissa; remove it.  */
3526   intermed[0] = buf[2];
3527   intermed[1] = buf[1];
3528   intermed[2] = (unsigned long)buf[0] >> 16;
3529
3530   decode_ieee_extended (fmt, r, intermed);
3531 }
3532
3533 /* Convert from the internal format to the 12-byte Intel format for
3534    an IEEE extended real.  */
3535 static void
3536 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3537                                const long *buf)
3538 {
3539   if (FLOAT_WORDS_BIG_ENDIAN)
3540     {
3541       /* All the padding in an Intel-format extended real goes at the high
3542          end, which in this case is after the mantissa, not the exponent.
3543          Therefore we must shift everything up 16 bits.  */
3544       long intermed[3];
3545
3546       intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3547       intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3548       intermed[2] =  ((unsigned long)buf[0] >> 16);
3549
3550       decode_ieee_extended (fmt, r, intermed);
3551     }
3552   else
3553     /* decode_ieee_extended produces what we want directly.  */
3554     decode_ieee_extended (fmt, r, buf);
3555 }
3556
3557 /* Convert from the internal format to the 16-byte Intel format for
3558    an IEEE extended real.  */
3559 static void
3560 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3561                                 const long *buf)
3562 {
3563   /* All the padding in an Intel-format extended real goes at the high end.  */
3564   decode_ieee_extended_intel_96 (fmt, r, buf);
3565 }
3566
3567 const struct real_format ieee_extended_motorola_format =
3568   {
3569     encode_ieee_extended_motorola,
3570     decode_ieee_extended_motorola,
3571     2,
3572     64,
3573     64,
3574     -16382,
3575     16384,
3576     95,
3577     95,
3578     false,
3579     true,
3580     true,
3581     true,
3582     true,
3583     true,
3584     true,
3585     true
3586   };
3587
3588 const struct real_format ieee_extended_intel_96_format =
3589   {
3590     encode_ieee_extended_intel_96,
3591     decode_ieee_extended_intel_96,
3592     2,
3593     64,
3594     64,
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
3609 const struct real_format ieee_extended_intel_128_format =
3610   {
3611     encode_ieee_extended_intel_128,
3612     decode_ieee_extended_intel_128,
3613     2,
3614     64,
3615     64,
3616     -16381,
3617     16384,
3618     79,
3619     79,
3620     false,
3621     true,
3622     true,
3623     true,
3624     true,
3625     true,
3626     true,
3627     false
3628   };
3629
3630 /* The following caters to i386 systems that set the rounding precision
3631    to 53 bits instead of 64, e.g. FreeBSD.  */
3632 const struct real_format ieee_extended_intel_96_round_53_format =
3633   {
3634     encode_ieee_extended_intel_96,
3635     decode_ieee_extended_intel_96,
3636     2,
3637     53,
3638     53,
3639     -16381,
3640     16384,
3641     79,
3642     79,
3643     false,
3644     true,
3645     true,
3646     true,
3647     true,
3648     true,
3649     true,
3650     false
3651   };
3652 \f
3653 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3654    numbers whose sum is equal to the extended precision value.  The number
3655    with greater magnitude is first.  This format has the same magnitude
3656    range as an IEEE double precision value, but effectively 106 bits of
3657    significand precision.  Infinity and NaN are represented by their IEEE
3658    double precision value stored in the first number, the second number is
3659    +0.0 or -0.0 for Infinity and don't-care for NaN.  */
3660
3661 static void encode_ibm_extended (const struct real_format *fmt,
3662                                  long *, const REAL_VALUE_TYPE *);
3663 static void decode_ibm_extended (const struct real_format *,
3664                                  REAL_VALUE_TYPE *, const long *);
3665
3666 static void
3667 encode_ibm_extended (const struct real_format *fmt, long *buf,
3668                      const REAL_VALUE_TYPE *r)
3669 {
3670   REAL_VALUE_TYPE u, normr, v;
3671   const struct real_format *base_fmt;
3672
3673   base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3674
3675   /* Renormalize R before doing any arithmetic on it.  */
3676   normr = *r;
3677   if (normr.cl == rvc_normal)
3678     normalize (&normr);
3679
3680   /* u = IEEE double precision portion of significand.  */
3681   u = normr;
3682   round_for_format (base_fmt, &u);
3683   encode_ieee_double (base_fmt, &buf[0], &u);
3684
3685   if (u.cl == rvc_normal)
3686     {
3687       do_add (&v, &normr, &u, 1);
3688       /* Call round_for_format since we might need to denormalize.  */
3689       round_for_format (base_fmt, &v);
3690       encode_ieee_double (base_fmt, &buf[2], &v);
3691     }
3692   else
3693     {
3694       /* Inf, NaN, 0 are all representable as doubles, so the
3695          least-significant part can be 0.0.  */
3696       buf[2] = 0;
3697       buf[3] = 0;
3698     }
3699 }
3700
3701 static void
3702 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3703                      const long *buf)
3704 {
3705   REAL_VALUE_TYPE u, v;
3706   const struct real_format *base_fmt;
3707
3708   base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3709   decode_ieee_double (base_fmt, &u, &buf[0]);
3710
3711   if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3712     {
3713       decode_ieee_double (base_fmt, &v, &buf[2]);
3714       do_add (r, &u, &v, 0);
3715     }
3716   else
3717     *r = u;
3718 }
3719
3720 const struct real_format ibm_extended_format =
3721   {
3722     encode_ibm_extended,
3723     decode_ibm_extended,
3724     2,
3725     53 + 53,
3726     53,
3727     -1021 + 53,
3728     1024,
3729     127,
3730     -1,
3731     false,
3732     true,
3733     true,
3734     true,
3735     true,
3736     true,
3737     true,
3738     false
3739   };
3740
3741 const struct real_format mips_extended_format =
3742   {
3743     encode_ibm_extended,
3744     decode_ibm_extended,
3745     2,
3746     53 + 53,
3747     53,
3748     -1021 + 53,
3749     1024,
3750     127,
3751     -1,
3752     false,
3753     true,
3754     true,
3755     true,
3756     true,
3757     true,
3758     false,
3759     true
3760   };
3761
3762 \f
3763 /* IEEE quad precision format.  */
3764
3765 static void encode_ieee_quad (const struct real_format *fmt,
3766                               long *, const REAL_VALUE_TYPE *);
3767 static void decode_ieee_quad (const struct real_format *,
3768                               REAL_VALUE_TYPE *, const long *);
3769
3770 static void
3771 encode_ieee_quad (const struct real_format *fmt, long *buf,
3772                   const REAL_VALUE_TYPE *r)
3773 {
3774   unsigned long image3, image2, image1, image0, exp;
3775   bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3776   REAL_VALUE_TYPE u;
3777
3778   image3 = r->sign << 31;
3779   image2 = 0;
3780   image1 = 0;
3781   image0 = 0;
3782
3783   rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3784
3785   switch (r->cl)
3786     {
3787     case rvc_zero:
3788       break;
3789
3790     case rvc_inf:
3791       if (fmt->has_inf)
3792         image3 |= 32767 << 16;
3793       else
3794         {
3795           image3 |= 0x7fffffff;
3796           image2 = 0xffffffff;
3797           image1 = 0xffffffff;
3798           image0 = 0xffffffff;
3799         }
3800       break;
3801
3802     case rvc_nan:
3803       if (fmt->has_nans)
3804         {
3805           image3 |= 32767 << 16;
3806
3807           if (r->canonical)
3808             {
3809               if (fmt->canonical_nan_lsbs_set)
3810                 {
3811                   image3 |= 0x7fff;
3812                   image2 = image1 = image0 = 0xffffffff;
3813                 }
3814             }
3815           else if (HOST_BITS_PER_LONG == 32)
3816             {
3817               image0 = u.sig[0];
3818               image1 = u.sig[1];
3819               image2 = u.sig[2];
3820               image3 |= u.sig[3] & 0xffff;
3821             }
3822           else
3823             {
3824               image0 = u.sig[0];
3825               image1 = image0 >> 31 >> 1;
3826               image2 = u.sig[1];
3827               image3 |= (image2 >> 31 >> 1) & 0xffff;
3828               image0 &= 0xffffffff;
3829               image2 &= 0xffffffff;
3830             }
3831           if (r->signalling == fmt->qnan_msb_set)
3832             image3 &= ~0x8000;
3833           else
3834             image3 |= 0x8000;
3835           if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3836             image3 |= 0x4000;
3837         }
3838       else
3839         {
3840           image3 |= 0x7fffffff;
3841           image2 = 0xffffffff;
3842           image1 = 0xffffffff;
3843           image0 = 0xffffffff;
3844         }
3845       break;
3846
3847     case rvc_normal:
3848       /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3849          whereas the intermediate representation is 0.F x 2**exp.
3850          Which means we're off by one.  */
3851       if (denormal)
3852         exp = 0;
3853       else
3854         exp = REAL_EXP (r) + 16383 - 1;
3855       image3 |= exp << 16;
3856
3857       if (HOST_BITS_PER_LONG == 32)
3858         {
3859           image0 = u.sig[0];
3860           image1 = u.sig[1];
3861           image2 = u.sig[2];
3862           image3 |= u.sig[3] & 0xffff;
3863         }
3864       else
3865         {
3866           image0 = u.sig[0];
3867           image1 = image0 >> 31 >> 1;
3868           image2 = u.sig[1];
3869           image3 |= (image2 >> 31 >> 1) & 0xffff;
3870           image0 &= 0xffffffff;
3871           image2 &= 0xffffffff;
3872         }
3873       break;
3874
3875     default:
3876       gcc_unreachable ();
3877     }
3878
3879   if (FLOAT_WORDS_BIG_ENDIAN)
3880     {
3881       buf[0] = image3;
3882       buf[1] = image2;
3883       buf[2] = image1;
3884       buf[3] = image0;
3885     }
3886   else
3887     {
3888       buf[0] = image0;
3889       buf[1] = image1;
3890       buf[2] = image2;
3891       buf[3] = image3;
3892     }
3893 }
3894
3895 static void
3896 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3897                   const long *buf)
3898 {
3899   unsigned long image3, image2, image1, image0;
3900   bool sign;
3901   int exp;
3902
3903   if (FLOAT_WORDS_BIG_ENDIAN)
3904     {
3905       image3 = buf[0];
3906       image2 = buf[1];
3907       image1 = buf[2];
3908       image0 = buf[3];
3909     }
3910   else
3911     {
3912       image0 = buf[0];
3913       image1 = buf[1];
3914       image2 = buf[2];
3915       image3 = buf[3];
3916     }
3917   image0 &= 0xffffffff;
3918   image1 &= 0xffffffff;
3919   image2 &= 0xffffffff;
3920
3921   sign = (image3 >> 31) & 1;
3922   exp = (image3 >> 16) & 0x7fff;
3923   image3 &= 0xffff;
3924
3925   memset (r, 0, sizeof (*r));
3926
3927   if (exp == 0)
3928     {
3929       if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
3930         {
3931           r->cl = rvc_normal;
3932           r->sign = sign;
3933
3934           SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
3935           if (HOST_BITS_PER_LONG == 32)
3936             {
3937               r->sig[0] = image0;
3938               r->sig[1] = image1;
3939               r->sig[2] = image2;
3940               r->sig[3] = image3;
3941             }
3942           else
3943             {
3944               r->sig[0] = (image1 << 31 << 1) | image0;
3945               r->sig[1] = (image3 << 31 << 1) | image2;
3946             }
3947
3948           normalize (r);
3949         }
3950       else if (fmt->has_signed_zero)
3951         r->sign = sign;
3952     }
3953   else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3954     {
3955       if (image3 | image2 | image1 | image0)
3956         {
3957           r->cl = rvc_nan;
3958           r->sign = sign;
3959           r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
3960
3961           if (HOST_BITS_PER_LONG == 32)
3962             {
3963               r->sig[0] = image0;
3964               r->sig[1] = image1;
3965               r->sig[2] = image2;
3966               r->sig[3] = image3;
3967             }
3968           else
3969             {
3970               r->sig[0] = (image1 << 31 << 1) | image0;
3971               r->sig[1] = (image3 << 31 << 1) | image2;
3972             }
3973           lshift_significand (r, r, SIGNIFICAND_BITS - 113);
3974         }
3975       else
3976         {
3977           r->cl = rvc_inf;
3978           r->sign = sign;
3979         }
3980     }
3981   else
3982     {
3983       r->cl = rvc_normal;
3984       r->sign = sign;
3985       SET_REAL_EXP (r, exp - 16383 + 1);
3986
3987       if (HOST_BITS_PER_LONG == 32)
3988         {
3989           r->sig[0] = image0;
3990           r->sig[1] = image1;
3991           r->sig[2] = image2;
3992           r->sig[3] = image3;
3993         }
3994       else
3995         {
3996           r->sig[0] = (image1 << 31 << 1) | image0;
3997           r->sig[1] = (image3 << 31 << 1) | image2;
3998         }
3999       lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4000       r->sig[SIGSZ-1] |= SIG_MSB;
4001     }
4002 }
4003
4004 const struct real_format ieee_quad_format =
4005   {
4006     encode_ieee_quad,
4007     decode_ieee_quad,
4008     2,
4009     113,
4010     113,
4011     -16381,
4012     16384,
4013     127,
4014     127,
4015     false,
4016     true,
4017     true,
4018     true,
4019     true,
4020     true,
4021     true,
4022     false
4023   };
4024
4025 const struct real_format mips_quad_format =
4026   {
4027     encode_ieee_quad,
4028     decode_ieee_quad,
4029     2,
4030     113,
4031     113,
4032     -16381,
4033     16384,
4034     127,
4035     127,
4036     false,
4037     true,
4038     true,
4039     true,
4040     true,
4041     true,
4042     false,
4043     true
4044   };
4045 \f
4046 /* Descriptions of VAX floating point formats can be found beginning at
4047
4048    http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4049
4050    The thing to remember is that they're almost IEEE, except for word
4051    order, exponent bias, and the lack of infinities, nans, and denormals.
4052
4053    We don't implement the H_floating format here, simply because neither
4054    the VAX or Alpha ports use it.  */
4055
4056 static void encode_vax_f (const struct real_format *fmt,
4057                           long *, const REAL_VALUE_TYPE *);
4058 static void decode_vax_f (const struct real_format *,
4059                           REAL_VALUE_TYPE *, const long *);
4060 static void encode_vax_d (const struct real_format *fmt,
4061                           long *, const REAL_VALUE_TYPE *);
4062 static void decode_vax_d (const struct real_format *,
4063                           REAL_VALUE_TYPE *, const long *);
4064 static void encode_vax_g (const struct real_format *fmt,
4065                           long *, const REAL_VALUE_TYPE *);
4066 static void decode_vax_g (const struct real_format *,
4067                           REAL_VALUE_TYPE *, const long *);
4068
4069 static void
4070 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4071               const REAL_VALUE_TYPE *r)
4072 {
4073   unsigned long sign, exp, sig, image;
4074
4075   sign = r->sign << 15;
4076
4077   switch (r->cl)
4078     {
4079     case rvc_zero:
4080       image = 0;
4081       break;
4082
4083     case rvc_inf:
4084     case rvc_nan:
4085       image = 0xffff7fff | sign;
4086       break;
4087
4088     case rvc_normal:
4089       sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4090       exp = REAL_EXP (r) + 128;
4091
4092       image = (sig << 16) & 0xffff0000;
4093       image |= sign;
4094       image |= exp << 7;
4095       image |= sig >> 16;
4096       break;
4097
4098     default:
4099       gcc_unreachable ();
4100     }
4101
4102   buf[0] = image;
4103 }
4104
4105 static void
4106 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
4107               REAL_VALUE_TYPE *r, const long *buf)
4108 {
4109   unsigned long image = buf[0] & 0xffffffff;
4110   int exp = (image >> 7) & 0xff;
4111
4112   memset (r, 0, sizeof (*r));
4113
4114   if (exp != 0)
4115     {
4116       r->cl = rvc_normal;
4117       r->sign = (image >> 15) & 1;
4118       SET_REAL_EXP (r, exp - 128);
4119
4120       image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
4121       r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4122     }
4123 }
4124
4125 static void
4126 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4127               const REAL_VALUE_TYPE *r)
4128 {
4129   unsigned long image0, image1, sign = r->sign << 15;
4130
4131   switch (r->cl)
4132     {
4133     case rvc_zero:
4134       image0 = image1 = 0;
4135       break;
4136
4137     case rvc_inf:
4138     case rvc_nan:
4139       image0 = 0xffff7fff | sign;
4140       image1 = 0xffffffff;
4141       break;
4142
4143     case rvc_normal:
4144       /* Extract the significand into straight hi:lo.  */
4145       if (HOST_BITS_PER_LONG == 64)
4146         {
4147           image0 = r->sig[SIGSZ-1];
4148           image1 = (image0 >> (64 - 56)) & 0xffffffff;
4149           image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
4150         }
4151       else
4152         {
4153           image0 = r->sig[SIGSZ-1];
4154           image1 = r->sig[SIGSZ-2];
4155           image1 = (image0 << 24) | (image1 >> 8);
4156           image0 = (image0 >> 8) & 0xffffff;
4157         }
4158
4159       /* Rearrange the half-words of the significand to match the
4160          external format.  */
4161       image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
4162       image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4163
4164       /* Add the sign and exponent.  */
4165       image0 |= sign;
4166       image0 |= (REAL_EXP (r) + 128) << 7;
4167       break;
4168
4169     default:
4170       gcc_unreachable ();
4171     }
4172
4173   if (FLOAT_WORDS_BIG_ENDIAN)
4174     buf[0] = image1, buf[1] = image0;
4175   else
4176     buf[0] = image0, buf[1] = image1;
4177 }
4178
4179 static void
4180 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
4181               REAL_VALUE_TYPE *r, const long *buf)
4182 {
4183   unsigned long image0, image1;
4184   int exp;
4185
4186   if (FLOAT_WORDS_BIG_ENDIAN)
4187     image1 = buf[0], image0 = buf[1];
4188   else
4189     image0 = buf[0], image1 = buf[1];
4190   image0 &= 0xffffffff;
4191   image1 &= 0xffffffff;
4192
4193   exp = (image0 >> 7) & 0xff;
4194
4195   memset (r, 0, sizeof (*r));
4196
4197   if (exp != 0)
4198     {
4199       r->cl = rvc_normal;
4200       r->sign = (image0 >> 15) & 1;
4201       SET_REAL_EXP (r, exp - 128);
4202
4203       /* Rearrange the half-words of the external format into
4204          proper ascending order.  */
4205       image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4206       image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4207
4208       if (HOST_BITS_PER_LONG == 64)
4209         {
4210           image0 = (image0 << 31 << 1) | image1;
4211           image0 <<= 64 - 56;
4212           image0 |= SIG_MSB;
4213           r->sig[SIGSZ-1] = image0;
4214         }
4215       else
4216         {
4217           r->sig[SIGSZ-1] = image0;
4218           r->sig[SIGSZ-2] = image1;
4219           lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4220           r->sig[SIGSZ-1] |= SIG_MSB;
4221         }
4222     }
4223 }
4224
4225 static void
4226 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4227               const REAL_VALUE_TYPE *r)
4228 {
4229   unsigned long image0, image1, sign = r->sign << 15;
4230
4231   switch (r->cl)
4232     {
4233     case rvc_zero:
4234       image0 = image1 = 0;
4235       break;
4236
4237     case rvc_inf:
4238     case rvc_nan:
4239       image0 = 0xffff7fff | sign;
4240       image1 = 0xffffffff;
4241       break;
4242
4243     case rvc_normal:
4244       /* Extract the significand into straight hi:lo.  */
4245       if (HOST_BITS_PER_LONG == 64)
4246         {
4247           image0 = r->sig[SIGSZ-1];
4248           image1 = (image0 >> (64 - 53)) & 0xffffffff;
4249           image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4250         }
4251       else
4252         {
4253           image0 = r->sig[SIGSZ-1];
4254           image1 = r->sig[SIGSZ-2];
4255           image1 = (image0 << 21) | (image1 >> 11);
4256           image0 = (image0 >> 11) & 0xfffff;
4257         }
4258
4259       /* Rearrange the half-words of the significand to match the
4260          external format.  */
4261       image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4262       image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4263
4264       /* Add the sign and exponent.  */
4265       image0 |= sign;
4266       image0 |= (REAL_EXP (r) + 1024) << 4;
4267       break;
4268
4269     default:
4270       gcc_unreachable ();
4271     }
4272
4273   if (FLOAT_WORDS_BIG_ENDIAN)
4274     buf[0] = image1, buf[1] = image0;
4275   else
4276     buf[0] = image0, buf[1] = image1;
4277 }
4278
4279 static void
4280 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4281               REAL_VALUE_TYPE *r, const long *buf)
4282 {
4283   unsigned long image0, image1;
4284   int exp;
4285
4286   if (FLOAT_WORDS_BIG_ENDIAN)
4287     image1 = buf[0], image0 = buf[1];
4288   else
4289     image0 = buf[0], image1 = buf[1];
4290   image0 &= 0xffffffff;
4291   image1 &= 0xffffffff;
4292
4293   exp = (image0 >> 4) & 0x7ff;
4294
4295   memset (r, 0, sizeof (*r));
4296
4297   if (exp != 0)
4298     {
4299       r->cl = rvc_normal;
4300       r->sign = (image0 >> 15) & 1;
4301       SET_REAL_EXP (r, exp - 1024);
4302
4303       /* Rearrange the half-words of the external format into
4304          proper ascending order.  */
4305       image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4306       image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4307
4308       if (HOST_BITS_PER_LONG == 64)
4309         {
4310           image0 = (image0 << 31 << 1) | image1;
4311           image0 <<= 64 - 53;
4312           image0 |= SIG_MSB;
4313           r->sig[SIGSZ-1] = image0;
4314         }
4315       else
4316         {
4317           r->sig[SIGSZ-1] = image0;
4318           r->sig[SIGSZ-2] = image1;
4319           lshift_significand (r, r, 64 - 53);
4320           r->sig[SIGSZ-1] |= SIG_MSB;
4321         }
4322     }
4323 }
4324
4325 const struct real_format vax_f_format =
4326   {
4327     encode_vax_f,
4328     decode_vax_f,
4329     2,
4330     24,
4331     24,
4332     -127,
4333     127,
4334     15,
4335     15,
4336     false,
4337     false,
4338     false,
4339     false,
4340     false,
4341     false,
4342     false,
4343     false
4344   };
4345
4346 const struct real_format vax_d_format =
4347   {
4348     encode_vax_d,
4349     decode_vax_d,
4350     2,
4351     56,
4352     56,
4353     -127,
4354     127,
4355     15,
4356     15,
4357     false,
4358     false,
4359     false,
4360     false,
4361     false,
4362     false,
4363     false,
4364     false
4365   };
4366
4367 const struct real_format vax_g_format =
4368   {
4369     encode_vax_g,
4370     decode_vax_g,
4371     2,
4372     53,
4373     53,
4374     -1023,
4375     1023,
4376     15,
4377     15,
4378     false,
4379     false,
4380     false,
4381     false,
4382     false,
4383     false,
4384     false,
4385     false
4386   };
4387 \f
4388 /* Encode real R into a single precision DFP value in BUF.  */
4389 static void
4390 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4391                        long *buf ATTRIBUTE_UNUSED, 
4392                        const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4393 {
4394   encode_decimal32 (fmt, buf, r);
4395 }
4396
4397 /* Decode a single precision DFP value in BUF into a real R.  */
4398 static void 
4399 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4400                        REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED, 
4401                        const long *buf ATTRIBUTE_UNUSED)
4402 {
4403   decode_decimal32 (fmt, r, buf);
4404 }
4405
4406 /* Encode real R into a double precision DFP value in BUF.  */
4407 static void 
4408 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4409                        long *buf ATTRIBUTE_UNUSED, 
4410                        const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4411 {
4412   encode_decimal64 (fmt, buf, r);
4413 }
4414
4415 /* Decode a double precision DFP value in BUF into a real R.  */
4416 static void 
4417 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4418                        REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED, 
4419                        const long *buf ATTRIBUTE_UNUSED)
4420 {
4421   decode_decimal64 (fmt, r, buf);
4422 }
4423
4424 /* Encode real R into a quad precision DFP value in BUF.  */
4425 static void 
4426 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4427                      long *buf ATTRIBUTE_UNUSED,
4428                      const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4429 {
4430   encode_decimal128 (fmt, buf, r);
4431 }
4432
4433 /* Decode a quad precision DFP value in BUF into a real R.  */
4434 static void 
4435 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4436                      REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4437                      const long *buf ATTRIBUTE_UNUSED)
4438 {
4439   decode_decimal128 (fmt, r, buf);
4440 }
4441
4442 /* Single precision decimal floating point (IEEE 754). */
4443 const struct real_format decimal_single_format =
4444   {
4445     encode_decimal_single,
4446     decode_decimal_single,
4447     10, 
4448     7,
4449     7,
4450     -95,
4451     96,
4452     31,
4453     31,
4454     false,
4455     true,
4456     true,
4457     true,
4458     true,
4459     true, 
4460     true,
4461     false
4462   };
4463
4464 /* Double precision decimal floating point (IEEE 754). */
4465 const struct real_format decimal_double_format =
4466   {
4467     encode_decimal_double,
4468     decode_decimal_double,
4469     10,
4470     16,
4471     16,
4472     -383,
4473     384,
4474     63,
4475     63,
4476     false,
4477     true,
4478     true,
4479     true,
4480     true,
4481     true,
4482     true,
4483     false
4484   };
4485
4486 /* Quad precision decimal floating point (IEEE 754). */
4487 const struct real_format decimal_quad_format =
4488   {
4489     encode_decimal_quad,
4490     decode_decimal_quad,
4491     10,
4492     34,
4493     34,
4494     -6143,
4495     6144,
4496     127,
4497     127,
4498     false,
4499     true,
4500     true,
4501     true,
4502     true, 
4503     true, 
4504     true,
4505     false
4506   };
4507 \f
4508 /* A synthetic "format" for internal arithmetic.  It's the size of the
4509    internal significand minus the two bits needed for proper rounding.
4510    The encode and decode routines exist only to satisfy our paranoia
4511    harness.  */
4512
4513 static void encode_internal (const struct real_format *fmt,
4514                              long *, const REAL_VALUE_TYPE *);
4515 static void decode_internal (const struct real_format *,
4516                              REAL_VALUE_TYPE *, const long *);
4517
4518 static void
4519 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4520                  const REAL_VALUE_TYPE *r)
4521 {
4522   memcpy (buf, r, sizeof (*r));
4523 }
4524
4525 static void
4526 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
4527                  REAL_VALUE_TYPE *r, const long *buf)
4528 {
4529   memcpy (r, buf, sizeof (*r));
4530 }
4531
4532 const struct real_format real_internal_format =
4533   {
4534     encode_internal,
4535     decode_internal,
4536     2,
4537     SIGNIFICAND_BITS - 2,
4538     SIGNIFICAND_BITS - 2,
4539     -MAX_EXP,
4540     MAX_EXP,
4541     -1,
4542     -1,
4543     false,
4544     false,
4545     true,
4546     true,
4547     false,
4548     true,
4549     true,
4550     false
4551   };
4552 \f
4553 /* Calculate the square root of X in mode MODE, and store the result
4554    in R.  Return TRUE if the operation does not raise an exception.
4555    For details see "High Precision Division and Square Root",
4556    Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4557    1993.  http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf.  */
4558
4559 bool
4560 real_sqrt (REAL_VALUE_TYPE *r, enum machine_mode mode,
4561            const REAL_VALUE_TYPE *x)
4562 {
4563   static REAL_VALUE_TYPE halfthree;
4564   static bool init = false;
4565   REAL_VALUE_TYPE h, t, i;
4566   int iter, exp;
4567
4568   /* sqrt(-0.0) is -0.0.  */
4569   if (real_isnegzero (x))
4570     {
4571       *r = *x;
4572       return false;
4573     }
4574
4575   /* Negative arguments return NaN.  */
4576   if (real_isneg (x))
4577     {
4578       get_canonical_qnan (r, 0);
4579       return false;
4580     }
4581
4582   /* Infinity and NaN return themselves.  */
4583   if (!real_isfinite (x))
4584     {
4585       *r = *x;
4586       return false;
4587     }
4588
4589   if (!init)
4590     {
4591       do_add (&halfthree, &dconst1, &dconsthalf, 0);
4592       init = true;
4593     }
4594
4595   /* Initial guess for reciprocal sqrt, i.  */
4596   exp = real_exponent (x);
4597   real_ldexp (&i, &dconst1, -exp/2);
4598
4599   /* Newton's iteration for reciprocal sqrt, i.  */
4600   for (iter = 0; iter < 16; iter++)
4601     {
4602       /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x).  */
4603       do_multiply (&t, x, &i);
4604       do_multiply (&h, &t, &i);
4605       do_multiply (&t, &h, &dconsthalf);
4606       do_add (&h, &halfthree, &t, 1);
4607       do_multiply (&t, &i, &h);
4608
4609       /* Check for early convergence.  */
4610       if (iter >= 6 && real_identical (&i, &t))
4611         break;
4612
4613       /* ??? Unroll loop to avoid copying.  */
4614       i = t;
4615     }
4616
4617   /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)).  */
4618   do_multiply (&t, x, &i);
4619   do_multiply (&h, &t, &i);
4620   do_add (&i, &dconst1, &h, 1);
4621   do_multiply (&h, &t, &i);
4622   do_multiply (&i, &dconsthalf, &h);
4623   do_add (&h, &t, &i, 0);
4624
4625   /* ??? We need a Tuckerman test to get the last bit.  */
4626
4627   real_convert (r, mode, &h);
4628   return true;
4629 }
4630
4631 /* Calculate X raised to the integer exponent N in mode MODE and store
4632    the result in R.  Return true if the result may be inexact due to
4633    loss of precision.  The algorithm is the classic "left-to-right binary
4634    method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4635    Algorithms", "The Art of Computer Programming", Volume 2.  */
4636
4637 bool
4638 real_powi (REAL_VALUE_TYPE *r, enum machine_mode mode,
4639            const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4640 {
4641   unsigned HOST_WIDE_INT bit;
4642   REAL_VALUE_TYPE t;
4643   bool inexact = false;
4644   bool init = false;
4645   bool neg;
4646   int i;
4647
4648   if (n == 0)
4649     {
4650       *r = dconst1;
4651       return false;
4652     }
4653   else if (n < 0)
4654     {
4655       /* Don't worry about overflow, from now on n is unsigned.  */
4656       neg = true;
4657       n = -n;
4658     }
4659   else
4660     neg = false;
4661
4662   t = *x;
4663   bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
4664   for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4665     {
4666       if (init)
4667         {
4668           inexact |= do_multiply (&t, &t, &t);
4669           if (n & bit)
4670             inexact |= do_multiply (&t, &t, x);
4671         }
4672       else if (n & bit)
4673         init = true;
4674       bit >>= 1;
4675     }
4676
4677   if (neg)
4678     inexact |= do_divide (&t, &dconst1, &t);
4679
4680   real_convert (r, mode, &t);
4681   return inexact;
4682 }
4683
4684 /* Round X to the nearest integer not larger in absolute value, i.e.
4685    towards zero, placing the result in R in mode MODE.  */
4686
4687 void
4688 real_trunc (REAL_VALUE_TYPE *r, enum machine_mode mode,
4689             const REAL_VALUE_TYPE *x)
4690 {
4691   do_fix_trunc (r, x);
4692   if (mode != VOIDmode)
4693     real_convert (r, mode, r);
4694 }
4695
4696 /* Round X to the largest integer not greater in value, i.e. round
4697    down, placing the result in R in mode MODE.  */
4698
4699 void
4700 real_floor (REAL_VALUE_TYPE *r, enum machine_mode mode,
4701             const REAL_VALUE_TYPE *x)
4702 {
4703   REAL_VALUE_TYPE t;
4704
4705   do_fix_trunc (&t, x);
4706   if (! real_identical (&t, x) && x->sign)
4707     do_add (&t, &t, &dconstm1, 0);
4708   if (mode != VOIDmode)
4709     real_convert (r, mode, &t);
4710   else
4711     *r = t;
4712 }
4713
4714 /* Round X to the smallest integer not less then argument, i.e. round
4715    up, placing the result in R in mode MODE.  */
4716
4717 void
4718 real_ceil (REAL_VALUE_TYPE *r, enum machine_mode mode,
4719            const REAL_VALUE_TYPE *x)
4720 {
4721   REAL_VALUE_TYPE t;
4722
4723   do_fix_trunc (&t, x);
4724   if (! real_identical (&t, x) && ! x->sign)
4725     do_add (&t, &t, &dconst1, 0);
4726   if (mode != VOIDmode)
4727     real_convert (r, mode, &t);
4728   else
4729     *r = t;
4730 }
4731
4732 /* Round X to the nearest integer, but round halfway cases away from
4733    zero.  */
4734
4735 void
4736 real_round (REAL_VALUE_TYPE *r, enum machine_mode mode,
4737             const REAL_VALUE_TYPE *x)
4738 {
4739   do_add (r, x, &dconsthalf, x->sign);
4740   do_fix_trunc (r, r);
4741   if (mode != VOIDmode)
4742     real_convert (r, mode, r);
4743 }
4744
4745 /* Set the sign of R to the sign of X.  */
4746
4747 void
4748 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4749 {
4750   r->sign = x->sign;
4751 }
4752
4753 /* Convert from REAL_VALUE_TYPE to MPFR.  The caller is responsible
4754    for initializing and clearing the MPFR parameter.  */
4755
4756 void
4757 mpfr_from_real (mpfr_ptr m, const REAL_VALUE_TYPE *r, mp_rnd_t rndmode)
4758 {
4759   /* We use a string as an intermediate type.  */
4760   char buf[128];
4761   int ret;
4762
4763   /* Take care of Infinity and NaN.  */
4764   if (r->cl == rvc_inf)
4765     {
4766       mpfr_set_inf (m, r->sign == 1 ? -1 : 1);
4767       return;
4768     }
4769   
4770   if (r->cl == rvc_nan)
4771     {
4772       mpfr_set_nan (m);
4773       return;
4774     }
4775   
4776   real_to_hexadecimal (buf, r, sizeof (buf), 0, 1);
4777   /* mpfr_set_str() parses hexadecimal floats from strings in the same
4778      format that GCC will output them.  Nothing extra is needed.  */
4779   ret = mpfr_set_str (m, buf, 16, rndmode);
4780   gcc_assert (ret == 0);
4781 }
4782
4783 /* Convert from MPFR to REAL_VALUE_TYPE, for a given type TYPE and rounding
4784    mode RNDMODE.  TYPE is only relevant if M is a NaN.  */
4785
4786 void
4787 real_from_mpfr (REAL_VALUE_TYPE *r, mpfr_srcptr m, tree type, mp_rnd_t rndmode)
4788 {
4789   /* We use a string as an intermediate type.  */
4790   char buf[128], *rstr;
4791   mp_exp_t exp;
4792
4793   /* Take care of Infinity and NaN.  */
4794   if (mpfr_inf_p (m))
4795     {
4796       real_inf (r);
4797       if (mpfr_sgn (m) < 0)
4798         *r = REAL_VALUE_NEGATE (*r);
4799       return;
4800     }
4801
4802   if (mpfr_nan_p (m))
4803     {
4804       real_nan (r, "", 1, TYPE_MODE (type));
4805       return;
4806     }
4807
4808   rstr = mpfr_get_str (NULL, &exp, 16, 0, m, rndmode);
4809
4810   /* The additional 12 chars add space for the sprintf below.  This
4811      leaves 6 digits for the exponent which is supposedly enough.  */
4812   gcc_assert (rstr != NULL && strlen (rstr) < sizeof (buf) - 12);
4813
4814   /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp,
4815      mpfr_get_str returns the exponent for mantissa * 16**exp, adjust
4816      for that.  */
4817   exp *= 4;
4818
4819   if (rstr[0] == '-')
4820     sprintf (buf, "-0x.%sp%d", &rstr[1], (int) exp);
4821   else
4822     sprintf (buf, "0x.%sp%d", rstr, (int) exp);
4823
4824   mpfr_free_str (rstr);
4825   
4826   real_from_string (r, buf);
4827 }
4828
4829 /* Check whether the real constant value given is an integer.  */
4830
4831 bool
4832 real_isinteger (const REAL_VALUE_TYPE *c, enum machine_mode mode)
4833 {
4834   REAL_VALUE_TYPE cint;
4835
4836   real_trunc (&cint, mode, c);
4837   return real_identical (c, &cint);
4838 }
4839
4840 /* Write into BUF the maximum representable finite floating-point
4841    number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
4842    float string.  LEN is the size of BUF, and the buffer must be large
4843    enough to contain the resulting string.  */
4844
4845 void
4846 get_max_float (const struct real_format *fmt, char *buf, size_t len)
4847 {
4848   int i, n;
4849   char *p;
4850
4851   strcpy (buf, "0x0.");
4852   n = fmt->p;
4853   for (i = 0, p = buf + 4; i + 3 < n; i += 4)
4854     *p++ = 'f';
4855   if (i < n)
4856     *p++ = "08ce"[n - i];
4857   sprintf (p, "p%d", fmt->emax);
4858   if (fmt->pnan < fmt->p)
4859     {
4860       /* This is an IBM extended double format made up of two IEEE
4861          doubles.  The value of the long double is the sum of the
4862          values of the two parts.  The most significant part is
4863          required to be the value of the long double rounded to the
4864          nearest double.  Rounding means we need a slightly smaller
4865          value for LDBL_MAX.  */
4866       buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
4867     }
4868
4869   gcc_assert (strlen (buf) < len);
4870 }