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