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