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