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