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