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