To...
[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 /* Return whether OP0 == OP1. */
1082
1083 bool
1084 real_equal (const REAL_VALUE_TYPE *op0, const REAL_VALUE_TYPE *op1)
1085 {
1086 return do_compare (op0, op1, -1) == 0;
1087 }
1088
1089 /* Return whether OP0 < OP1. */
1090
1091 bool
1092 real_less (const REAL_VALUE_TYPE *op0, const REAL_VALUE_TYPE *op1)
1093 {
1094 return do_compare (op0, op1, 1) < 0;
1095 }
1096
1097 bool
1098 real_compare (int icode, const REAL_VALUE_TYPE *op0,
1099 const REAL_VALUE_TYPE *op1)
1100 {
1101 enum tree_code code = (enum tree_code) icode;
1102
1103 switch (code)
1104 {
1105 case LT_EXPR:
1106 return real_less (op0, op1);
1107 case LE_EXPR:
1108 return do_compare (op0, op1, 1) <= 0;
1109 case GT_EXPR:
1110 return do_compare (op0, op1, -1) > 0;
1111 case GE_EXPR:
1112 return do_compare (op0, op1, -1) >= 0;
1113 case EQ_EXPR:
1114 return real_equal (op0, op1);
1115 case NE_EXPR:
1116 return do_compare (op0, op1, -1) != 0;
1117 case UNORDERED_EXPR:
1118 return op0->cl == rvc_nan || op1->cl == rvc_nan;
1119 case ORDERED_EXPR:
1120 return op0->cl != rvc_nan && op1->cl != rvc_nan;
1121 case UNLT_EXPR:
1122 return do_compare (op0, op1, -1) < 0;
1123 case UNLE_EXPR:
1124 return do_compare (op0, op1, -1) <= 0;
1125 case UNGT_EXPR:
1126 return do_compare (op0, op1, 1) > 0;
1127 case UNGE_EXPR:
1128 return do_compare (op0, op1, 1) >= 0;
1129 case UNEQ_EXPR:
1130 return do_compare (op0, op1, 0) == 0;
1131 case LTGT_EXPR:
1132 return do_compare (op0, op1, 0) != 0;
1133
1134 default:
1135 gcc_unreachable ();
1136 }
1137 }
1138
1139 /* Return floor log2(R). */
1140
1141 int
1142 real_exponent (const REAL_VALUE_TYPE *r)
1143 {
1144 switch (r->cl)
1145 {
1146 case rvc_zero:
1147 return 0;
1148 case rvc_inf:
1149 case rvc_nan:
1150 return (unsigned int)-1 >> 1;
1151 case rvc_normal:
1152 return REAL_EXP (r);
1153 default:
1154 gcc_unreachable ();
1155 }
1156 }
1157
1158 /* R = OP0 * 2**EXP. */
1159
1160 void
1161 real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
1162 {
1163 *r = *op0;
1164 switch (r->cl)
1165 {
1166 case rvc_zero:
1167 case rvc_inf:
1168 case rvc_nan:
1169 break;
1170
1171 case rvc_normal:
1172 exp += REAL_EXP (op0);
1173 if (exp > MAX_EXP)
1174 get_inf (r, r->sign);
1175 else if (exp < -MAX_EXP)
1176 get_zero (r, r->sign);
1177 else
1178 SET_REAL_EXP (r, exp);
1179 break;
1180
1181 default:
1182 gcc_unreachable ();
1183 }
1184 }
1185
1186 /* Determine whether a floating-point value X is infinite. */
1187
1188 bool
1189 real_isinf (const REAL_VALUE_TYPE *r)
1190 {
1191 return (r->cl == rvc_inf);
1192 }
1193
1194 /* Determine whether a floating-point value X is a NaN. */
1195
1196 bool
1197 real_isnan (const REAL_VALUE_TYPE *r)
1198 {
1199 return (r->cl == rvc_nan);
1200 }
1201
1202 /* Determine whether a floating-point value X is finite. */
1203
1204 bool
1205 real_isfinite (const REAL_VALUE_TYPE *r)
1206 {
1207 return (r->cl != rvc_nan) && (r->cl != rvc_inf);
1208 }
1209
1210 /* Determine whether a floating-point value X is negative. */
1211
1212 bool
1213 real_isneg (const REAL_VALUE_TYPE *r)
1214 {
1215 return r->sign;
1216 }
1217
1218 /* Determine whether a floating-point value X is minus zero. */
1219
1220 bool
1221 real_isnegzero (const REAL_VALUE_TYPE *r)
1222 {
1223 return r->sign && r->cl == rvc_zero;
1224 }
1225
1226 /* Compare two floating-point objects for bitwise identity. */
1227
1228 bool
1229 real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
1230 {
1231 int i;
1232
1233 if (a->cl != b->cl)
1234 return false;
1235 if (a->sign != b->sign)
1236 return false;
1237
1238 switch (a->cl)
1239 {
1240 case rvc_zero:
1241 case rvc_inf:
1242 return true;
1243
1244 case rvc_normal:
1245 if (a->decimal != b->decimal)
1246 return false;
1247 if (REAL_EXP (a) != REAL_EXP (b))
1248 return false;
1249 break;
1250
1251 case rvc_nan:
1252 if (a->signalling != b->signalling)
1253 return false;
1254 /* The significand is ignored for canonical NaNs. */
1255 if (a->canonical || b->canonical)
1256 return a->canonical == b->canonical;
1257 break;
1258
1259 default:
1260 gcc_unreachable ();
1261 }
1262
1263 for (i = 0; i < SIGSZ; ++i)
1264 if (a->sig[i] != b->sig[i])
1265 return false;
1266
1267 return true;
1268 }
1269
1270 /* Try to change R into its exact multiplicative inverse in machine
1271 mode MODE. Return true if successful. */
1272
1273 bool
1274 exact_real_inverse (machine_mode mode, REAL_VALUE_TYPE *r)
1275 {
1276 const REAL_VALUE_TYPE *one = real_digit (1);
1277 REAL_VALUE_TYPE u;
1278 int i;
1279
1280 if (r->cl != rvc_normal)
1281 return false;
1282
1283 /* Check for a power of two: all significand bits zero except the MSB. */
1284 for (i = 0; i < SIGSZ-1; ++i)
1285 if (r->sig[i] != 0)
1286 return false;
1287 if (r->sig[SIGSZ-1] != SIG_MSB)
1288 return false;
1289
1290 /* Find the inverse and truncate to the required mode. */
1291 do_divide (&u, one, r);
1292 real_convert (&u, mode, &u);
1293
1294 /* The rounding may have overflowed. */
1295 if (u.cl != rvc_normal)
1296 return false;
1297 for (i = 0; i < SIGSZ-1; ++i)
1298 if (u.sig[i] != 0)
1299 return false;
1300 if (u.sig[SIGSZ-1] != SIG_MSB)
1301 return false;
1302
1303 *r = u;
1304 return true;
1305 }
1306
1307 /* Return true if arithmetic on values in IMODE that were promoted
1308 from values in TMODE is equivalent to direct arithmetic on values
1309 in TMODE. */
1310
1311 bool
1312 real_can_shorten_arithmetic (machine_mode imode, machine_mode tmode)
1313 {
1314 const struct real_format *tfmt, *ifmt;
1315 tfmt = REAL_MODE_FORMAT (tmode);
1316 ifmt = REAL_MODE_FORMAT (imode);
1317 /* These conditions are conservative rather than trying to catch the
1318 exact boundary conditions; the main case to allow is IEEE float
1319 and double. */
1320 return (ifmt->b == tfmt->b
1321 && ifmt->p > 2 * tfmt->p
1322 && ifmt->emin < 2 * tfmt->emin - tfmt->p - 2
1323 && ifmt->emin < tfmt->emin - tfmt->emax - tfmt->p - 2
1324 && ifmt->emax > 2 * tfmt->emax + 2
1325 && ifmt->emax > tfmt->emax - tfmt->emin + tfmt->p + 2
1326 && ifmt->round_towards_zero == tfmt->round_towards_zero
1327 && (ifmt->has_sign_dependent_rounding
1328 == tfmt->has_sign_dependent_rounding)
1329 && ifmt->has_nans >= tfmt->has_nans
1330 && ifmt->has_inf >= tfmt->has_inf
1331 && ifmt->has_signed_zero >= tfmt->has_signed_zero
1332 && !MODE_COMPOSITE_P (tmode)
1333 && !MODE_COMPOSITE_P (imode));
1334 }
1335 \f
1336 /* Render R as an integer. */
1337
1338 HOST_WIDE_INT
1339 real_to_integer (const REAL_VALUE_TYPE *r)
1340 {
1341 unsigned HOST_WIDE_INT i;
1342
1343 switch (r->cl)
1344 {
1345 case rvc_zero:
1346 underflow:
1347 return 0;
1348
1349 case rvc_inf:
1350 case rvc_nan:
1351 overflow:
1352 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
1353 if (!r->sign)
1354 i--;
1355 return i;
1356
1357 case rvc_normal:
1358 if (r->decimal)
1359 return decimal_real_to_integer (r);
1360
1361 if (REAL_EXP (r) <= 0)
1362 goto underflow;
1363 /* Only force overflow for unsigned overflow. Signed overflow is
1364 undefined, so it doesn't matter what we return, and some callers
1365 expect to be able to use this routine for both signed and
1366 unsigned conversions. */
1367 if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
1368 goto overflow;
1369
1370 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1371 i = r->sig[SIGSZ-1];
1372 else
1373 {
1374 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1375 i = r->sig[SIGSZ-1];
1376 i = i << (HOST_BITS_PER_LONG - 1) << 1;
1377 i |= r->sig[SIGSZ-2];
1378 }
1379
1380 i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
1381
1382 if (r->sign)
1383 i = -i;
1384 return i;
1385
1386 default:
1387 gcc_unreachable ();
1388 }
1389 }
1390
1391 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1392 be represented in precision, *FAIL is set to TRUE. */
1393
1394 wide_int
1395 real_to_integer (const REAL_VALUE_TYPE *r, bool *fail, int precision)
1396 {
1397 HOST_WIDE_INT val[2 * WIDE_INT_MAX_ELTS];
1398 int exp;
1399 int words, w;
1400 wide_int result;
1401
1402 switch (r->cl)
1403 {
1404 case rvc_zero:
1405 underflow:
1406 return wi::zero (precision);
1407
1408 case rvc_inf:
1409 case rvc_nan:
1410 overflow:
1411 *fail = true;
1412
1413 if (r->sign)
1414 return wi::set_bit_in_zero (precision - 1, precision);
1415 else
1416 return ~wi::set_bit_in_zero (precision - 1, precision);
1417
1418 case rvc_normal:
1419 if (r->decimal)
1420 return decimal_real_to_integer (r, fail, precision);
1421
1422 exp = REAL_EXP (r);
1423 if (exp <= 0)
1424 goto underflow;
1425 /* Only force overflow for unsigned overflow. Signed overflow is
1426 undefined, so it doesn't matter what we return, and some callers
1427 expect to be able to use this routine for both signed and
1428 unsigned conversions. */
1429 if (exp > precision)
1430 goto overflow;
1431
1432 /* Put the significand into a wide_int that has precision W, which
1433 is the smallest HWI-multiple that has at least PRECISION bits.
1434 This ensures that the top bit of the significand is in the
1435 top bit of the wide_int. */
1436 words = (precision + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT;
1437 w = words * HOST_BITS_PER_WIDE_INT;
1438
1439 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1440 for (int i = 0; i < words; i++)
1441 {
1442 int j = SIGSZ - words + i;
1443 val[i] = (j < 0) ? 0 : r->sig[j];
1444 }
1445 #else
1446 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1447 for (int i = 0; i < words; i++)
1448 {
1449 int j = SIGSZ - (words * 2) + (i * 2);
1450 if (j < 0)
1451 val[i] = 0;
1452 else
1453 val[i] = r->sig[j];
1454 j += 1;
1455 if (j >= 0)
1456 val[i] |= (unsigned HOST_WIDE_INT) r->sig[j] << HOST_BITS_PER_LONG;
1457 }
1458 #endif
1459 /* Shift the value into place and truncate to the desired precision. */
1460 result = wide_int::from_array (val, words, w);
1461 result = wi::lrshift (result, w - exp);
1462 result = wide_int::from (result, precision, UNSIGNED);
1463
1464 if (r->sign)
1465 return -result;
1466 else
1467 return result;
1468
1469 default:
1470 gcc_unreachable ();
1471 }
1472 }
1473
1474 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1475 of NUM / DEN. Return the quotient and place the remainder in NUM.
1476 It is expected that NUM / DEN are close enough that the quotient is
1477 small. */
1478
1479 static unsigned long
1480 rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
1481 {
1482 unsigned long q, msb;
1483 int expn = REAL_EXP (num), expd = REAL_EXP (den);
1484
1485 if (expn < expd)
1486 return 0;
1487
1488 q = msb = 0;
1489 goto start;
1490 do
1491 {
1492 msb = num->sig[SIGSZ-1] & SIG_MSB;
1493 q <<= 1;
1494 lshift_significand_1 (num, num);
1495 start:
1496 if (msb || cmp_significands (num, den) >= 0)
1497 {
1498 sub_significands (num, num, den, 0);
1499 q |= 1;
1500 }
1501 }
1502 while (--expn >= expd);
1503
1504 SET_REAL_EXP (num, expd);
1505 normalize (num);
1506
1507 return q;
1508 }
1509
1510 /* Render R as a decimal floating point constant. Emit DIGITS significant
1511 digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
1512 maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
1513 zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
1514 to a string that, when parsed back in mode MODE, yields the same value. */
1515
1516 #define M_LOG10_2 0.30102999566398119521
1517
1518 void
1519 real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig,
1520 size_t buf_size, size_t digits,
1521 int crop_trailing_zeros, machine_mode mode)
1522 {
1523 const struct real_format *fmt = NULL;
1524 const REAL_VALUE_TYPE *one, *ten;
1525 REAL_VALUE_TYPE r, pten, u, v;
1526 int dec_exp, cmp_one, digit;
1527 size_t max_digits;
1528 char *p, *first, *last;
1529 bool sign;
1530 bool round_up;
1531
1532 if (mode != VOIDmode)
1533 {
1534 fmt = REAL_MODE_FORMAT (mode);
1535 gcc_assert (fmt);
1536 }
1537
1538 r = *r_orig;
1539 switch (r.cl)
1540 {
1541 case rvc_zero:
1542 strcpy (str, (r.sign ? "-0.0" : "0.0"));
1543 return;
1544 case rvc_normal:
1545 break;
1546 case rvc_inf:
1547 strcpy (str, (r.sign ? "-Inf" : "+Inf"));
1548 return;
1549 case rvc_nan:
1550 /* ??? Print the significand as well, if not canonical? */
1551 sprintf (str, "%c%cNaN", (r_orig->sign ? '-' : '+'),
1552 (r_orig->signalling ? 'S' : 'Q'));
1553 return;
1554 default:
1555 gcc_unreachable ();
1556 }
1557
1558 if (r.decimal)
1559 {
1560 decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
1561 return;
1562 }
1563
1564 /* Bound the number of digits printed by the size of the representation. */
1565 max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1566 if (digits == 0 || digits > max_digits)
1567 digits = max_digits;
1568
1569 /* Estimate the decimal exponent, and compute the length of the string it
1570 will print as. Be conservative and add one to account for possible
1571 overflow or rounding error. */
1572 dec_exp = REAL_EXP (&r) * M_LOG10_2;
1573 for (max_digits = 1; dec_exp ; max_digits++)
1574 dec_exp /= 10;
1575
1576 /* Bound the number of digits printed by the size of the output buffer. */
1577 max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
1578 gcc_assert (max_digits <= buf_size);
1579 if (digits > max_digits)
1580 digits = max_digits;
1581
1582 one = real_digit (1);
1583 ten = ten_to_ptwo (0);
1584
1585 sign = r.sign;
1586 r.sign = 0;
1587
1588 dec_exp = 0;
1589 pten = *one;
1590
1591 cmp_one = do_compare (&r, one, 0);
1592 if (cmp_one > 0)
1593 {
1594 int m;
1595
1596 /* Number is greater than one. Convert significand to an integer
1597 and strip trailing decimal zeros. */
1598
1599 u = r;
1600 SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
1601
1602 /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
1603 m = floor_log2 (max_digits);
1604
1605 /* Iterate over the bits of the possible powers of 10 that might
1606 be present in U and eliminate them. That is, if we find that
1607 10**2**M divides U evenly, keep the division and increase
1608 DEC_EXP by 2**M. */
1609 do
1610 {
1611 REAL_VALUE_TYPE t;
1612
1613 do_divide (&t, &u, ten_to_ptwo (m));
1614 do_fix_trunc (&v, &t);
1615 if (cmp_significands (&v, &t) == 0)
1616 {
1617 u = t;
1618 dec_exp += 1 << m;
1619 }
1620 }
1621 while (--m >= 0);
1622
1623 /* Revert the scaling to integer that we performed earlier. */
1624 SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
1625 - (SIGNIFICAND_BITS - 1));
1626 r = u;
1627
1628 /* Find power of 10. Do this by dividing out 10**2**M when
1629 this is larger than the current remainder. Fill PTEN with
1630 the power of 10 that we compute. */
1631 if (REAL_EXP (&r) > 0)
1632 {
1633 m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
1634 do
1635 {
1636 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1637 if (do_compare (&u, ptentwo, 0) >= 0)
1638 {
1639 do_divide (&u, &u, ptentwo);
1640 do_multiply (&pten, &pten, ptentwo);
1641 dec_exp += 1 << m;
1642 }
1643 }
1644 while (--m >= 0);
1645 }
1646 else
1647 /* We managed to divide off enough tens in the above reduction
1648 loop that we've now got a negative exponent. Fall into the
1649 less-than-one code to compute the proper value for PTEN. */
1650 cmp_one = -1;
1651 }
1652 if (cmp_one < 0)
1653 {
1654 int m;
1655
1656 /* Number is less than one. Pad significand with leading
1657 decimal zeros. */
1658
1659 v = r;
1660 while (1)
1661 {
1662 /* Stop if we'd shift bits off the bottom. */
1663 if (v.sig[0] & 7)
1664 break;
1665
1666 do_multiply (&u, &v, ten);
1667
1668 /* Stop if we're now >= 1. */
1669 if (REAL_EXP (&u) > 0)
1670 break;
1671
1672 v = u;
1673 dec_exp -= 1;
1674 }
1675 r = v;
1676
1677 /* Find power of 10. Do this by multiplying in P=10**2**M when
1678 the current remainder is smaller than 1/P. Fill PTEN with the
1679 power of 10 that we compute. */
1680 m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
1681 do
1682 {
1683 const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
1684 const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
1685
1686 if (do_compare (&v, ptenmtwo, 0) <= 0)
1687 {
1688 do_multiply (&v, &v, ptentwo);
1689 do_multiply (&pten, &pten, ptentwo);
1690 dec_exp -= 1 << m;
1691 }
1692 }
1693 while (--m >= 0);
1694
1695 /* Invert the positive power of 10 that we've collected so far. */
1696 do_divide (&pten, one, &pten);
1697 }
1698
1699 p = str;
1700 if (sign)
1701 *p++ = '-';
1702 first = p++;
1703
1704 /* At this point, PTEN should contain the nearest power of 10 smaller
1705 than R, such that this division produces the first digit.
1706
1707 Using a divide-step primitive that returns the complete integral
1708 remainder avoids the rounding error that would be produced if
1709 we were to use do_divide here and then simply multiply by 10 for
1710 each subsequent digit. */
1711
1712 digit = rtd_divmod (&r, &pten);
1713
1714 /* Be prepared for error in that division via underflow ... */
1715 if (digit == 0 && cmp_significand_0 (&r))
1716 {
1717 /* Multiply by 10 and try again. */
1718 do_multiply (&r, &r, ten);
1719 digit = rtd_divmod (&r, &pten);
1720 dec_exp -= 1;
1721 gcc_assert (digit != 0);
1722 }
1723
1724 /* ... or overflow. */
1725 if (digit == 10)
1726 {
1727 *p++ = '1';
1728 if (--digits > 0)
1729 *p++ = '0';
1730 dec_exp += 1;
1731 }
1732 else
1733 {
1734 gcc_assert (digit <= 10);
1735 *p++ = digit + '0';
1736 }
1737
1738 /* Generate subsequent digits. */
1739 while (--digits > 0)
1740 {
1741 do_multiply (&r, &r, ten);
1742 digit = rtd_divmod (&r, &pten);
1743 *p++ = digit + '0';
1744 }
1745 last = p;
1746
1747 /* Generate one more digit with which to do rounding. */
1748 do_multiply (&r, &r, ten);
1749 digit = rtd_divmod (&r, &pten);
1750
1751 /* Round the result. */
1752 if (fmt && fmt->round_towards_zero)
1753 {
1754 /* If the format uses round towards zero when parsing the string
1755 back in, we need to always round away from zero here. */
1756 if (cmp_significand_0 (&r))
1757 digit++;
1758 round_up = digit > 0;
1759 }
1760 else
1761 {
1762 if (digit == 5)
1763 {
1764 /* Round to nearest. If R is nonzero there are additional
1765 nonzero digits to be extracted. */
1766 if (cmp_significand_0 (&r))
1767 digit++;
1768 /* Round to even. */
1769 else if ((p[-1] - '0') & 1)
1770 digit++;
1771 }
1772
1773 round_up = digit > 5;
1774 }
1775
1776 if (round_up)
1777 {
1778 while (p > first)
1779 {
1780 digit = *--p;
1781 if (digit == '9')
1782 *p = '0';
1783 else
1784 {
1785 *p = digit + 1;
1786 break;
1787 }
1788 }
1789
1790 /* Carry out of the first digit. This means we had all 9's and
1791 now have all 0's. "Prepend" a 1 by overwriting the first 0. */
1792 if (p == first)
1793 {
1794 first[1] = '1';
1795 dec_exp++;
1796 }
1797 }
1798
1799 /* Insert the decimal point. */
1800 first[0] = first[1];
1801 first[1] = '.';
1802
1803 /* If requested, drop trailing zeros. Never crop past "1.0". */
1804 if (crop_trailing_zeros)
1805 while (last > first + 3 && last[-1] == '0')
1806 last--;
1807
1808 /* Append the exponent. */
1809 sprintf (last, "e%+d", dec_exp);
1810
1811 #ifdef ENABLE_CHECKING
1812 /* Verify that we can read the original value back in. */
1813 if (mode != VOIDmode)
1814 {
1815 real_from_string (&r, str);
1816 real_convert (&r, mode, &r);
1817 gcc_assert (real_identical (&r, r_orig));
1818 }
1819 #endif
1820 }
1821
1822 /* Likewise, except always uses round-to-nearest. */
1823
1824 void
1825 real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
1826 size_t digits, int crop_trailing_zeros)
1827 {
1828 real_to_decimal_for_mode (str, r_orig, buf_size,
1829 digits, crop_trailing_zeros, VOIDmode);
1830 }
1831
1832 /* Render R as a hexadecimal floating point constant. Emit DIGITS
1833 significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
1834 choose the maximum for the representation. If CROP_TRAILING_ZEROS,
1835 strip trailing zeros. */
1836
1837 void
1838 real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
1839 size_t digits, int crop_trailing_zeros)
1840 {
1841 int i, j, exp = REAL_EXP (r);
1842 char *p, *first;
1843 char exp_buf[16];
1844 size_t max_digits;
1845
1846 switch (r->cl)
1847 {
1848 case rvc_zero:
1849 exp = 0;
1850 break;
1851 case rvc_normal:
1852 break;
1853 case rvc_inf:
1854 strcpy (str, (r->sign ? "-Inf" : "+Inf"));
1855 return;
1856 case rvc_nan:
1857 /* ??? Print the significand as well, if not canonical? */
1858 sprintf (str, "%c%cNaN", (r->sign ? '-' : '+'),
1859 (r->signalling ? 'S' : 'Q'));
1860 return;
1861 default:
1862 gcc_unreachable ();
1863 }
1864
1865 if (r->decimal)
1866 {
1867 /* Hexadecimal format for decimal floats is not interesting. */
1868 strcpy (str, "N/A");
1869 return;
1870 }
1871
1872 if (digits == 0)
1873 digits = SIGNIFICAND_BITS / 4;
1874
1875 /* Bound the number of digits printed by the size of the output buffer. */
1876
1877 sprintf (exp_buf, "p%+d", exp);
1878 max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
1879 gcc_assert (max_digits <= buf_size);
1880 if (digits > max_digits)
1881 digits = max_digits;
1882
1883 p = str;
1884 if (r->sign)
1885 *p++ = '-';
1886 *p++ = '0';
1887 *p++ = 'x';
1888 *p++ = '0';
1889 *p++ = '.';
1890 first = p;
1891
1892 for (i = SIGSZ - 1; i >= 0; --i)
1893 for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
1894 {
1895 *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
1896 if (--digits == 0)
1897 goto out;
1898 }
1899
1900 out:
1901 if (crop_trailing_zeros)
1902 while (p > first + 1 && p[-1] == '0')
1903 p--;
1904
1905 sprintf (p, "p%+d", exp);
1906 }
1907
1908 /* Initialize R from a decimal or hexadecimal string. The string is
1909 assumed to have been syntax checked already. Return -1 if the
1910 value underflows, +1 if overflows, and 0 otherwise. */
1911
1912 int
1913 real_from_string (REAL_VALUE_TYPE *r, const char *str)
1914 {
1915 int exp = 0;
1916 bool sign = false;
1917
1918 get_zero (r, 0);
1919
1920 if (*str == '-')
1921 {
1922 sign = true;
1923 str++;
1924 }
1925 else if (*str == '+')
1926 str++;
1927
1928 if (!strncmp (str, "QNaN", 4))
1929 {
1930 get_canonical_qnan (r, sign);
1931 return 0;
1932 }
1933 else if (!strncmp (str, "SNaN", 4))
1934 {
1935 get_canonical_snan (r, sign);
1936 return 0;
1937 }
1938 else if (!strncmp (str, "Inf", 3))
1939 {
1940 get_inf (r, sign);
1941 return 0;
1942 }
1943
1944 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
1945 {
1946 /* Hexadecimal floating point. */
1947 int pos = SIGNIFICAND_BITS - 4, d;
1948
1949 str += 2;
1950
1951 while (*str == '0')
1952 str++;
1953 while (1)
1954 {
1955 d = hex_value (*str);
1956 if (d == _hex_bad)
1957 break;
1958 if (pos >= 0)
1959 {
1960 r->sig[pos / HOST_BITS_PER_LONG]
1961 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1962 pos -= 4;
1963 }
1964 else if (d)
1965 /* Ensure correct rounding by setting last bit if there is
1966 a subsequent nonzero digit. */
1967 r->sig[0] |= 1;
1968 exp += 4;
1969 str++;
1970 }
1971 if (*str == '.')
1972 {
1973 str++;
1974 if (pos == SIGNIFICAND_BITS - 4)
1975 {
1976 while (*str == '0')
1977 str++, exp -= 4;
1978 }
1979 while (1)
1980 {
1981 d = hex_value (*str);
1982 if (d == _hex_bad)
1983 break;
1984 if (pos >= 0)
1985 {
1986 r->sig[pos / HOST_BITS_PER_LONG]
1987 |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
1988 pos -= 4;
1989 }
1990 else if (d)
1991 /* Ensure correct rounding by setting last bit if there is
1992 a subsequent nonzero digit. */
1993 r->sig[0] |= 1;
1994 str++;
1995 }
1996 }
1997
1998 /* If the mantissa is zero, ignore the exponent. */
1999 if (!cmp_significand_0 (r))
2000 goto is_a_zero;
2001
2002 if (*str == 'p' || *str == 'P')
2003 {
2004 bool exp_neg = false;
2005
2006 str++;
2007 if (*str == '-')
2008 {
2009 exp_neg = true;
2010 str++;
2011 }
2012 else if (*str == '+')
2013 str++;
2014
2015 d = 0;
2016 while (ISDIGIT (*str))
2017 {
2018 d *= 10;
2019 d += *str - '0';
2020 if (d > MAX_EXP)
2021 {
2022 /* Overflowed the exponent. */
2023 if (exp_neg)
2024 goto underflow;
2025 else
2026 goto overflow;
2027 }
2028 str++;
2029 }
2030 if (exp_neg)
2031 d = -d;
2032
2033 exp += d;
2034 }
2035
2036 r->cl = rvc_normal;
2037 SET_REAL_EXP (r, exp);
2038
2039 normalize (r);
2040 }
2041 else
2042 {
2043 /* Decimal floating point. */
2044 const char *cstr = str;
2045 mpfr_t m;
2046 bool inexact;
2047
2048 while (*cstr == '0')
2049 cstr++;
2050 if (*cstr == '.')
2051 {
2052 cstr++;
2053 while (*cstr == '0')
2054 cstr++;
2055 }
2056
2057 /* If the mantissa is zero, ignore the exponent. */
2058 if (!ISDIGIT (*cstr))
2059 goto is_a_zero;
2060
2061 /* Nonzero value, possibly overflowing or underflowing. */
2062 mpfr_init2 (m, SIGNIFICAND_BITS);
2063 inexact = mpfr_strtofr (m, str, NULL, 10, GMP_RNDZ);
2064 /* The result should never be a NaN, and because the rounding is
2065 toward zero should never be an infinity. */
2066 gcc_assert (!mpfr_nan_p (m) && !mpfr_inf_p (m));
2067 if (mpfr_zero_p (m) || mpfr_get_exp (m) < -MAX_EXP + 4)
2068 {
2069 mpfr_clear (m);
2070 goto underflow;
2071 }
2072 else if (mpfr_get_exp (m) > MAX_EXP - 4)
2073 {
2074 mpfr_clear (m);
2075 goto overflow;
2076 }
2077 else
2078 {
2079 real_from_mpfr (r, m, NULL_TREE, GMP_RNDZ);
2080 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2081 because the hex digits used in real_from_mpfr did not
2082 start with a digit 8 to f, but the exponent bounds above
2083 should have avoided underflow or overflow. */
2084 gcc_assert (r->cl == rvc_normal);
2085 /* Set a sticky bit if mpfr_strtofr was inexact. */
2086 r->sig[0] |= inexact;
2087 mpfr_clear (m);
2088 }
2089 }
2090
2091 r->sign = sign;
2092 return 0;
2093
2094 is_a_zero:
2095 get_zero (r, sign);
2096 return 0;
2097
2098 underflow:
2099 get_zero (r, sign);
2100 return -1;
2101
2102 overflow:
2103 get_inf (r, sign);
2104 return 1;
2105 }
2106
2107 /* Legacy. Similar, but return the result directly. */
2108
2109 REAL_VALUE_TYPE
2110 real_from_string2 (const char *s, machine_mode mode)
2111 {
2112 REAL_VALUE_TYPE r;
2113
2114 real_from_string (&r, s);
2115 if (mode != VOIDmode)
2116 real_convert (&r, mode, &r);
2117
2118 return r;
2119 }
2120
2121 /* Initialize R from string S and desired MODE. */
2122
2123 void
2124 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, machine_mode mode)
2125 {
2126 if (DECIMAL_FLOAT_MODE_P (mode))
2127 decimal_real_from_string (r, s);
2128 else
2129 real_from_string (r, s);
2130
2131 if (mode != VOIDmode)
2132 real_convert (r, mode, r);
2133 }
2134
2135 /* Initialize R from the wide_int VAL_IN. The MODE is not VOIDmode,*/
2136
2137 void
2138 real_from_integer (REAL_VALUE_TYPE *r, machine_mode mode,
2139 const wide_int_ref &val_in, signop sgn)
2140 {
2141 if (val_in == 0)
2142 get_zero (r, 0);
2143 else
2144 {
2145 unsigned int len = val_in.get_precision ();
2146 int i, j, e = 0;
2147 int maxbitlen = MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT;
2148 const unsigned int realmax = (SIGNIFICAND_BITS / HOST_BITS_PER_WIDE_INT
2149 * HOST_BITS_PER_WIDE_INT);
2150
2151 memset (r, 0, sizeof (*r));
2152 r->cl = rvc_normal;
2153 r->sign = wi::neg_p (val_in, sgn);
2154
2155 /* We have to ensure we can negate the largest negative number. */
2156 wide_int val = wide_int::from (val_in, maxbitlen, sgn);
2157
2158 if (r->sign)
2159 val = -val;
2160
2161 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2162 won't work with precisions that are not a multiple of
2163 HOST_BITS_PER_WIDE_INT. */
2164 len += HOST_BITS_PER_WIDE_INT - 1;
2165
2166 /* Ensure we can represent the largest negative number. */
2167 len += 1;
2168
2169 len = len/HOST_BITS_PER_WIDE_INT * HOST_BITS_PER_WIDE_INT;
2170
2171 /* Cap the size to the size allowed by real.h. */
2172 if (len > realmax)
2173 {
2174 HOST_WIDE_INT cnt_l_z;
2175 cnt_l_z = wi::clz (val);
2176
2177 if (maxbitlen - cnt_l_z > realmax)
2178 {
2179 e = maxbitlen - cnt_l_z - realmax;
2180
2181 /* This value is too large, we must shift it right to
2182 preserve all the bits we can, and then bump the
2183 exponent up by that amount. */
2184 val = wi::lrshift (val, e);
2185 }
2186 len = realmax;
2187 }
2188
2189 /* Clear out top bits so elt will work with precisions that aren't
2190 a multiple of HOST_BITS_PER_WIDE_INT. */
2191 val = wide_int::from (val, len, sgn);
2192 len = len / HOST_BITS_PER_WIDE_INT;
2193
2194 SET_REAL_EXP (r, len * HOST_BITS_PER_WIDE_INT + e);
2195
2196 j = SIGSZ - 1;
2197 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2198 for (i = len - 1; i >= 0; i--)
2199 {
2200 r->sig[j--] = val.elt (i);
2201 if (j < 0)
2202 break;
2203 }
2204 else
2205 {
2206 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2207 for (i = len - 1; i >= 0; i--)
2208 {
2209 HOST_WIDE_INT e = val.elt (i);
2210 r->sig[j--] = e >> (HOST_BITS_PER_LONG - 1) >> 1;
2211 if (j < 0)
2212 break;
2213 r->sig[j--] = e;
2214 if (j < 0)
2215 break;
2216 }
2217 }
2218
2219 normalize (r);
2220 }
2221
2222 if (DECIMAL_FLOAT_MODE_P (mode))
2223 decimal_from_integer (r);
2224 else if (mode != VOIDmode)
2225 real_convert (r, mode, r);
2226 }
2227
2228 /* Render R, an integral value, as a floating point constant with no
2229 specified exponent. */
2230
2231 static void
2232 decimal_integer_string (char *str, const REAL_VALUE_TYPE *r_orig,
2233 size_t buf_size)
2234 {
2235 int dec_exp, digit, digits;
2236 REAL_VALUE_TYPE r, pten;
2237 char *p;
2238 bool sign;
2239
2240 r = *r_orig;
2241
2242 if (r.cl == rvc_zero)
2243 {
2244 strcpy (str, "0.");
2245 return;
2246 }
2247
2248 sign = r.sign;
2249 r.sign = 0;
2250
2251 dec_exp = REAL_EXP (&r) * M_LOG10_2;
2252 digits = dec_exp + 1;
2253 gcc_assert ((digits + 2) < (int)buf_size);
2254
2255 pten = *real_digit (1);
2256 times_pten (&pten, dec_exp);
2257
2258 p = str;
2259 if (sign)
2260 *p++ = '-';
2261
2262 digit = rtd_divmod (&r, &pten);
2263 gcc_assert (digit >= 0 && digit <= 9);
2264 *p++ = digit + '0';
2265 while (--digits > 0)
2266 {
2267 times_pten (&r, 1);
2268 digit = rtd_divmod (&r, &pten);
2269 *p++ = digit + '0';
2270 }
2271 *p++ = '.';
2272 *p++ = '\0';
2273 }
2274
2275 /* Convert a real with an integral value to decimal float. */
2276
2277 static void
2278 decimal_from_integer (REAL_VALUE_TYPE *r)
2279 {
2280 char str[256];
2281
2282 decimal_integer_string (str, r, sizeof (str) - 1);
2283 decimal_real_from_string (r, str);
2284 }
2285
2286 /* Returns 10**2**N. */
2287
2288 static const REAL_VALUE_TYPE *
2289 ten_to_ptwo (int n)
2290 {
2291 static REAL_VALUE_TYPE tens[EXP_BITS];
2292
2293 gcc_assert (n >= 0);
2294 gcc_assert (n < EXP_BITS);
2295
2296 if (tens[n].cl == rvc_zero)
2297 {
2298 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2299 {
2300 HOST_WIDE_INT t = 10;
2301 int i;
2302
2303 for (i = 0; i < n; ++i)
2304 t *= t;
2305
2306 real_from_integer (&tens[n], VOIDmode, t, UNSIGNED);
2307 }
2308 else
2309 {
2310 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2311 do_multiply (&tens[n], t, t);
2312 }
2313 }
2314
2315 return &tens[n];
2316 }
2317
2318 /* Returns 10**(-2**N). */
2319
2320 static const REAL_VALUE_TYPE *
2321 ten_to_mptwo (int n)
2322 {
2323 static REAL_VALUE_TYPE tens[EXP_BITS];
2324
2325 gcc_assert (n >= 0);
2326 gcc_assert (n < EXP_BITS);
2327
2328 if (tens[n].cl == rvc_zero)
2329 do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
2330
2331 return &tens[n];
2332 }
2333
2334 /* Returns N. */
2335
2336 static const REAL_VALUE_TYPE *
2337 real_digit (int n)
2338 {
2339 static REAL_VALUE_TYPE num[10];
2340
2341 gcc_assert (n >= 0);
2342 gcc_assert (n <= 9);
2343
2344 if (n > 0 && num[n].cl == rvc_zero)
2345 real_from_integer (&num[n], VOIDmode, n, UNSIGNED);
2346
2347 return &num[n];
2348 }
2349
2350 /* Multiply R by 10**EXP. */
2351
2352 static void
2353 times_pten (REAL_VALUE_TYPE *r, int exp)
2354 {
2355 REAL_VALUE_TYPE pten, *rr;
2356 bool negative = (exp < 0);
2357 int i;
2358
2359 if (negative)
2360 {
2361 exp = -exp;
2362 pten = *real_digit (1);
2363 rr = &pten;
2364 }
2365 else
2366 rr = r;
2367
2368 for (i = 0; exp > 0; ++i, exp >>= 1)
2369 if (exp & 1)
2370 do_multiply (rr, rr, ten_to_ptwo (i));
2371
2372 if (negative)
2373 do_divide (r, r, &pten);
2374 }
2375
2376 /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2377
2378 const REAL_VALUE_TYPE *
2379 dconst_e_ptr (void)
2380 {
2381 static REAL_VALUE_TYPE value;
2382
2383 /* Initialize mathematical constants for constant folding builtins.
2384 These constants need to be given to at least 160 bits precision. */
2385 if (value.cl == rvc_zero)
2386 {
2387 mpfr_t m;
2388 mpfr_init2 (m, SIGNIFICAND_BITS);
2389 mpfr_set_ui (m, 1, GMP_RNDN);
2390 mpfr_exp (m, m, GMP_RNDN);
2391 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2392 mpfr_clear (m);
2393
2394 }
2395 return &value;
2396 }
2397
2398 /* Returns a cached REAL_VALUE_TYPE corresponding to 1/n, for various n. */
2399
2400 #define CACHED_FRACTION(NAME, N) \
2401 const REAL_VALUE_TYPE * \
2402 NAME (void) \
2403 { \
2404 static REAL_VALUE_TYPE value; \
2405 \
2406 /* Initialize mathematical constants for constant folding builtins. \
2407 These constants need to be given to at least 160 bits \
2408 precision. */ \
2409 if (value.cl == rvc_zero) \
2410 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (N)); \
2411 return &value; \
2412 }
2413
2414 CACHED_FRACTION (dconst_third_ptr, 3)
2415 CACHED_FRACTION (dconst_quarter_ptr, 4)
2416 CACHED_FRACTION (dconst_sixth_ptr, 6)
2417 CACHED_FRACTION (dconst_ninth_ptr, 9)
2418
2419 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2420
2421 const REAL_VALUE_TYPE *
2422 dconst_sqrt2_ptr (void)
2423 {
2424 static REAL_VALUE_TYPE value;
2425
2426 /* Initialize mathematical constants for constant folding builtins.
2427 These constants need to be given to at least 160 bits precision. */
2428 if (value.cl == rvc_zero)
2429 {
2430 mpfr_t m;
2431 mpfr_init2 (m, SIGNIFICAND_BITS);
2432 mpfr_sqrt_ui (m, 2, GMP_RNDN);
2433 real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
2434 mpfr_clear (m);
2435 }
2436 return &value;
2437 }
2438
2439 /* Fills R with +Inf. */
2440
2441 void
2442 real_inf (REAL_VALUE_TYPE *r)
2443 {
2444 get_inf (r, 0);
2445 }
2446
2447 /* Fills R with a NaN whose significand is described by STR. If QUIET,
2448 we force a QNaN, else we force an SNaN. The string, if not empty,
2449 is parsed as a number and placed in the significand. Return true
2450 if the string was successfully parsed. */
2451
2452 bool
2453 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2454 machine_mode mode)
2455 {
2456 const struct real_format *fmt;
2457
2458 fmt = REAL_MODE_FORMAT (mode);
2459 gcc_assert (fmt);
2460
2461 if (*str == 0)
2462 {
2463 if (quiet)
2464 get_canonical_qnan (r, 0);
2465 else
2466 get_canonical_snan (r, 0);
2467 }
2468 else
2469 {
2470 int base = 10, d;
2471
2472 memset (r, 0, sizeof (*r));
2473 r->cl = rvc_nan;
2474
2475 /* Parse akin to strtol into the significand of R. */
2476
2477 while (ISSPACE (*str))
2478 str++;
2479 if (*str == '-')
2480 str++;
2481 else if (*str == '+')
2482 str++;
2483 if (*str == '0')
2484 {
2485 str++;
2486 if (*str == 'x' || *str == 'X')
2487 {
2488 base = 16;
2489 str++;
2490 }
2491 else
2492 base = 8;
2493 }
2494
2495 while ((d = hex_value (*str)) < base)
2496 {
2497 REAL_VALUE_TYPE u;
2498
2499 switch (base)
2500 {
2501 case 8:
2502 lshift_significand (r, r, 3);
2503 break;
2504 case 16:
2505 lshift_significand (r, r, 4);
2506 break;
2507 case 10:
2508 lshift_significand_1 (&u, r);
2509 lshift_significand (r, r, 3);
2510 add_significands (r, r, &u);
2511 break;
2512 default:
2513 gcc_unreachable ();
2514 }
2515
2516 get_zero (&u, 0);
2517 u.sig[0] = d;
2518 add_significands (r, r, &u);
2519
2520 str++;
2521 }
2522
2523 /* Must have consumed the entire string for success. */
2524 if (*str != 0)
2525 return false;
2526
2527 /* Shift the significand into place such that the bits
2528 are in the most significant bits for the format. */
2529 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2530
2531 /* Our MSB is always unset for NaNs. */
2532 r->sig[SIGSZ-1] &= ~SIG_MSB;
2533
2534 /* Force quiet or signalling NaN. */
2535 r->signalling = !quiet;
2536 }
2537
2538 return true;
2539 }
2540
2541 /* Fills R with the largest finite value representable in mode MODE.
2542 If SIGN is nonzero, R is set to the most negative finite value. */
2543
2544 void
2545 real_maxval (REAL_VALUE_TYPE *r, int sign, machine_mode mode)
2546 {
2547 const struct real_format *fmt;
2548 int np2;
2549
2550 fmt = REAL_MODE_FORMAT (mode);
2551 gcc_assert (fmt);
2552 memset (r, 0, sizeof (*r));
2553
2554 if (fmt->b == 10)
2555 decimal_real_maxval (r, sign, mode);
2556 else
2557 {
2558 r->cl = rvc_normal;
2559 r->sign = sign;
2560 SET_REAL_EXP (r, fmt->emax);
2561
2562 np2 = SIGNIFICAND_BITS - fmt->p;
2563 memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
2564 clear_significand_below (r, np2);
2565
2566 if (fmt->pnan < fmt->p)
2567 /* This is an IBM extended double format made up of two IEEE
2568 doubles. The value of the long double is the sum of the
2569 values of the two parts. The most significant part is
2570 required to be the value of the long double rounded to the
2571 nearest double. Rounding means we need a slightly smaller
2572 value for LDBL_MAX. */
2573 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1);
2574 }
2575 }
2576
2577 /* Fills R with 2**N. */
2578
2579 void
2580 real_2expN (REAL_VALUE_TYPE *r, int n, machine_mode fmode)
2581 {
2582 memset (r, 0, sizeof (*r));
2583
2584 n++;
2585 if (n > MAX_EXP)
2586 r->cl = rvc_inf;
2587 else if (n < -MAX_EXP)
2588 ;
2589 else
2590 {
2591 r->cl = rvc_normal;
2592 SET_REAL_EXP (r, n);
2593 r->sig[SIGSZ-1] = SIG_MSB;
2594 }
2595 if (DECIMAL_FLOAT_MODE_P (fmode))
2596 decimal_real_convert (r, fmode, r);
2597 }
2598
2599 \f
2600 static void
2601 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2602 {
2603 int p2, np2, i, w;
2604 int emin2m1, emax2;
2605 bool round_up = false;
2606
2607 if (r->decimal)
2608 {
2609 if (fmt->b == 10)
2610 {
2611 decimal_round_for_format (fmt, r);
2612 return;
2613 }
2614 /* FIXME. We can come here via fp_easy_constant
2615 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2616 investigated whether this convert needs to be here, or
2617 something else is missing. */
2618 decimal_real_convert (r, DFmode, r);
2619 }
2620
2621 p2 = fmt->p;
2622 emin2m1 = fmt->emin - 1;
2623 emax2 = fmt->emax;
2624
2625 np2 = SIGNIFICAND_BITS - p2;
2626 switch (r->cl)
2627 {
2628 underflow:
2629 get_zero (r, r->sign);
2630 case rvc_zero:
2631 if (!fmt->has_signed_zero)
2632 r->sign = 0;
2633 return;
2634
2635 overflow:
2636 get_inf (r, r->sign);
2637 case rvc_inf:
2638 return;
2639
2640 case rvc_nan:
2641 clear_significand_below (r, np2);
2642 return;
2643
2644 case rvc_normal:
2645 break;
2646
2647 default:
2648 gcc_unreachable ();
2649 }
2650
2651 /* Check the range of the exponent. If we're out of range,
2652 either underflow or overflow. */
2653 if (REAL_EXP (r) > emax2)
2654 goto overflow;
2655 else if (REAL_EXP (r) <= emin2m1)
2656 {
2657 int diff;
2658
2659 if (!fmt->has_denorm)
2660 {
2661 /* Don't underflow completely until we've had a chance to round. */
2662 if (REAL_EXP (r) < emin2m1)
2663 goto underflow;
2664 }
2665 else
2666 {
2667 diff = emin2m1 - REAL_EXP (r) + 1;
2668 if (diff > p2)
2669 goto underflow;
2670
2671 /* De-normalize the significand. */
2672 r->sig[0] |= sticky_rshift_significand (r, r, diff);
2673 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2674 }
2675 }
2676
2677 if (!fmt->round_towards_zero)
2678 {
2679 /* There are P2 true significand bits, followed by one guard bit,
2680 followed by one sticky bit, followed by stuff. Fold nonzero
2681 stuff into the sticky bit. */
2682 unsigned long sticky;
2683 bool guard, lsb;
2684
2685 sticky = 0;
2686 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2687 sticky |= r->sig[i];
2688 sticky |= r->sig[w]
2689 & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2690
2691 guard = test_significand_bit (r, np2 - 1);
2692 lsb = test_significand_bit (r, np2);
2693
2694 /* Round to even. */
2695 round_up = guard && (sticky || lsb);
2696 }
2697
2698 if (round_up)
2699 {
2700 REAL_VALUE_TYPE u;
2701 get_zero (&u, 0);
2702 set_significand_bit (&u, np2);
2703
2704 if (add_significands (r, r, &u))
2705 {
2706 /* Overflow. Means the significand had been all ones, and
2707 is now all zeros. Need to increase the exponent, and
2708 possibly re-normalize it. */
2709 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2710 if (REAL_EXP (r) > emax2)
2711 goto overflow;
2712 r->sig[SIGSZ-1] = SIG_MSB;
2713 }
2714 }
2715
2716 /* Catch underflow that we deferred until after rounding. */
2717 if (REAL_EXP (r) <= emin2m1)
2718 goto underflow;
2719
2720 /* Clear out trailing garbage. */
2721 clear_significand_below (r, np2);
2722 }
2723
2724 /* Extend or truncate to a new mode. */
2725
2726 void
2727 real_convert (REAL_VALUE_TYPE *r, machine_mode mode,
2728 const REAL_VALUE_TYPE *a)
2729 {
2730 const struct real_format *fmt;
2731
2732 fmt = REAL_MODE_FORMAT (mode);
2733 gcc_assert (fmt);
2734
2735 *r = *a;
2736
2737 if (a->decimal || fmt->b == 10)
2738 decimal_real_convert (r, mode, a);
2739
2740 round_for_format (fmt, r);
2741
2742 /* round_for_format de-normalizes denormals. Undo just that part. */
2743 if (r->cl == rvc_normal)
2744 normalize (r);
2745 }
2746
2747 /* Legacy. Likewise, except return the struct directly. */
2748
2749 REAL_VALUE_TYPE
2750 real_value_truncate (machine_mode mode, REAL_VALUE_TYPE a)
2751 {
2752 REAL_VALUE_TYPE r;
2753 real_convert (&r, mode, &a);
2754 return r;
2755 }
2756
2757 /* Return true if truncating to MODE is exact. */
2758
2759 bool
2760 exact_real_truncate (machine_mode mode, const REAL_VALUE_TYPE *a)
2761 {
2762 const struct real_format *fmt;
2763 REAL_VALUE_TYPE t;
2764 int emin2m1;
2765
2766 fmt = REAL_MODE_FORMAT (mode);
2767 gcc_assert (fmt);
2768
2769 /* Don't allow conversion to denormals. */
2770 emin2m1 = fmt->emin - 1;
2771 if (REAL_EXP (a) <= emin2m1)
2772 return false;
2773
2774 /* After conversion to the new mode, the value must be identical. */
2775 real_convert (&t, mode, a);
2776 return real_identical (&t, a);
2777 }
2778
2779 /* Write R to the given target format. Place the words of the result
2780 in target word order in BUF. There are always 32 bits in each
2781 long, no matter the size of the host long.
2782
2783 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2784
2785 long
2786 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
2787 const struct real_format *fmt)
2788 {
2789 REAL_VALUE_TYPE r;
2790 long buf1;
2791
2792 r = *r_orig;
2793 round_for_format (fmt, &r);
2794
2795 if (!buf)
2796 buf = &buf1;
2797 (*fmt->encode) (fmt, buf, &r);
2798
2799 return *buf;
2800 }
2801
2802 /* Similar, but look up the format from MODE. */
2803
2804 long
2805 real_to_target (long *buf, const REAL_VALUE_TYPE *r, machine_mode mode)
2806 {
2807 const struct real_format *fmt;
2808
2809 fmt = REAL_MODE_FORMAT (mode);
2810 gcc_assert (fmt);
2811
2812 return real_to_target_fmt (buf, r, fmt);
2813 }
2814
2815 /* Read R from the given target format. Read the words of the result
2816 in target word order in BUF. There are always 32 bits in each
2817 long, no matter the size of the host long. */
2818
2819 void
2820 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
2821 const struct real_format *fmt)
2822 {
2823 (*fmt->decode) (fmt, r, buf);
2824 }
2825
2826 /* Similar, but look up the format from MODE. */
2827
2828 void
2829 real_from_target (REAL_VALUE_TYPE *r, const long *buf, machine_mode mode)
2830 {
2831 const struct real_format *fmt;
2832
2833 fmt = REAL_MODE_FORMAT (mode);
2834 gcc_assert (fmt);
2835
2836 (*fmt->decode) (fmt, r, buf);
2837 }
2838
2839 /* Return the number of bits of the largest binary value that the
2840 significand of MODE will hold. */
2841 /* ??? Legacy. Should get access to real_format directly. */
2842
2843 int
2844 significand_size (machine_mode mode)
2845 {
2846 const struct real_format *fmt;
2847
2848 fmt = REAL_MODE_FORMAT (mode);
2849 if (fmt == NULL)
2850 return 0;
2851
2852 if (fmt->b == 10)
2853 {
2854 /* Return the size in bits of the largest binary value that can be
2855 held by the decimal coefficient for this mode. This is one more
2856 than the number of bits required to hold the largest coefficient
2857 of this mode. */
2858 double log2_10 = 3.3219281;
2859 return fmt->p * log2_10;
2860 }
2861 return fmt->p;
2862 }
2863
2864 /* Return a hash value for the given real value. */
2865 /* ??? The "unsigned int" return value is intended to be hashval_t,
2866 but I didn't want to pull hashtab.h into real.h. */
2867
2868 unsigned int
2869 real_hash (const REAL_VALUE_TYPE *r)
2870 {
2871 unsigned int h;
2872 size_t i;
2873
2874 h = r->cl | (r->sign << 2);
2875 switch (r->cl)
2876 {
2877 case rvc_zero:
2878 case rvc_inf:
2879 return h;
2880
2881 case rvc_normal:
2882 h |= REAL_EXP (r) << 3;
2883 break;
2884
2885 case rvc_nan:
2886 if (r->signalling)
2887 h ^= (unsigned int)-1;
2888 if (r->canonical)
2889 return h;
2890 break;
2891
2892 default:
2893 gcc_unreachable ();
2894 }
2895
2896 if (sizeof (unsigned long) > sizeof (unsigned int))
2897 for (i = 0; i < SIGSZ; ++i)
2898 {
2899 unsigned long s = r->sig[i];
2900 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2901 }
2902 else
2903 for (i = 0; i < SIGSZ; ++i)
2904 h ^= r->sig[i];
2905
2906 return h;
2907 }
2908 \f
2909 /* IEEE single-precision format. */
2910
2911 static void encode_ieee_single (const struct real_format *fmt,
2912 long *, const REAL_VALUE_TYPE *);
2913 static void decode_ieee_single (const struct real_format *,
2914 REAL_VALUE_TYPE *, const long *);
2915
2916 static void
2917 encode_ieee_single (const struct real_format *fmt, long *buf,
2918 const REAL_VALUE_TYPE *r)
2919 {
2920 unsigned long image, sig, exp;
2921 unsigned long sign = r->sign;
2922 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
2923
2924 image = sign << 31;
2925 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2926
2927 switch (r->cl)
2928 {
2929 case rvc_zero:
2930 break;
2931
2932 case rvc_inf:
2933 if (fmt->has_inf)
2934 image |= 255 << 23;
2935 else
2936 image |= 0x7fffffff;
2937 break;
2938
2939 case rvc_nan:
2940 if (fmt->has_nans)
2941 {
2942 if (r->canonical)
2943 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
2944 if (r->signalling == fmt->qnan_msb_set)
2945 sig &= ~(1 << 22);
2946 else
2947 sig |= 1 << 22;
2948 if (sig == 0)
2949 sig = 1 << 21;
2950
2951 image |= 255 << 23;
2952 image |= sig;
2953 }
2954 else
2955 image |= 0x7fffffff;
2956 break;
2957
2958 case rvc_normal:
2959 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
2960 whereas the intermediate representation is 0.F x 2**exp.
2961 Which means we're off by one. */
2962 if (denormal)
2963 exp = 0;
2964 else
2965 exp = REAL_EXP (r) + 127 - 1;
2966 image |= exp << 23;
2967 image |= sig;
2968 break;
2969
2970 default:
2971 gcc_unreachable ();
2972 }
2973
2974 buf[0] = image;
2975 }
2976
2977 static void
2978 decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
2979 const long *buf)
2980 {
2981 unsigned long image = buf[0] & 0xffffffff;
2982 bool sign = (image >> 31) & 1;
2983 int exp = (image >> 23) & 0xff;
2984
2985 memset (r, 0, sizeof (*r));
2986 image <<= HOST_BITS_PER_LONG - 24;
2987 image &= ~SIG_MSB;
2988
2989 if (exp == 0)
2990 {
2991 if (image && fmt->has_denorm)
2992 {
2993 r->cl = rvc_normal;
2994 r->sign = sign;
2995 SET_REAL_EXP (r, -126);
2996 r->sig[SIGSZ-1] = image << 1;
2997 normalize (r);
2998 }
2999 else if (fmt->has_signed_zero)
3000 r->sign = sign;
3001 }
3002 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
3003 {
3004 if (image)
3005 {
3006 r->cl = rvc_nan;
3007 r->sign = sign;
3008 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
3009 ^ fmt->qnan_msb_set);
3010 r->sig[SIGSZ-1] = image;
3011 }
3012 else
3013 {
3014 r->cl = rvc_inf;
3015 r->sign = sign;
3016 }
3017 }
3018 else
3019 {
3020 r->cl = rvc_normal;
3021 r->sign = sign;
3022 SET_REAL_EXP (r, exp - 127 + 1);
3023 r->sig[SIGSZ-1] = image | SIG_MSB;
3024 }
3025 }
3026
3027 const struct real_format ieee_single_format =
3028 {
3029 encode_ieee_single,
3030 decode_ieee_single,
3031 2,
3032 24,
3033 24,
3034 -125,
3035 128,
3036 31,
3037 31,
3038 false,
3039 true,
3040 true,
3041 true,
3042 true,
3043 true,
3044 true,
3045 false,
3046 "ieee_single"
3047 };
3048
3049 const struct real_format mips_single_format =
3050 {
3051 encode_ieee_single,
3052 decode_ieee_single,
3053 2,
3054 24,
3055 24,
3056 -125,
3057 128,
3058 31,
3059 31,
3060 false,
3061 true,
3062 true,
3063 true,
3064 true,
3065 true,
3066 false,
3067 true,
3068 "mips_single"
3069 };
3070
3071 const struct real_format motorola_single_format =
3072 {
3073 encode_ieee_single,
3074 decode_ieee_single,
3075 2,
3076 24,
3077 24,
3078 -125,
3079 128,
3080 31,
3081 31,
3082 false,
3083 true,
3084 true,
3085 true,
3086 true,
3087 true,
3088 true,
3089 true,
3090 "motorola_single"
3091 };
3092
3093 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3094 single precision with the following differences:
3095 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3096 are generated.
3097 - NaNs are not supported.
3098 - The range of non-zero numbers in binary is
3099 (001)[1.]000...000 to (255)[1.]111...111.
3100 - Denormals can be represented, but are treated as +0.0 when
3101 used as an operand and are never generated as a result.
3102 - -0.0 can be represented, but a zero result is always +0.0.
3103 - the only supported rounding mode is trunction (towards zero). */
3104 const struct real_format spu_single_format =
3105 {
3106 encode_ieee_single,
3107 decode_ieee_single,
3108 2,
3109 24,
3110 24,
3111 -125,
3112 129,
3113 31,
3114 31,
3115 true,
3116 false,
3117 false,
3118 false,
3119 true,
3120 true,
3121 false,
3122 false,
3123 "spu_single"
3124 };
3125 \f
3126 /* IEEE double-precision format. */
3127
3128 static void encode_ieee_double (const struct real_format *fmt,
3129 long *, const REAL_VALUE_TYPE *);
3130 static void decode_ieee_double (const struct real_format *,
3131 REAL_VALUE_TYPE *, const long *);
3132
3133 static void
3134 encode_ieee_double (const struct real_format *fmt, long *buf,
3135 const REAL_VALUE_TYPE *r)
3136 {
3137 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
3138 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3139
3140 image_hi = r->sign << 31;
3141 image_lo = 0;
3142
3143 if (HOST_BITS_PER_LONG == 64)
3144 {
3145 sig_hi = r->sig[SIGSZ-1];
3146 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
3147 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
3148 }
3149 else
3150 {
3151 sig_hi = r->sig[SIGSZ-1];
3152 sig_lo = r->sig[SIGSZ-2];
3153 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
3154 sig_hi = (sig_hi >> 11) & 0xfffff;
3155 }
3156
3157 switch (r->cl)
3158 {
3159 case rvc_zero:
3160 break;
3161
3162 case rvc_inf:
3163 if (fmt->has_inf)
3164 image_hi |= 2047 << 20;
3165 else
3166 {
3167 image_hi |= 0x7fffffff;
3168 image_lo = 0xffffffff;
3169 }
3170 break;
3171
3172 case rvc_nan:
3173 if (fmt->has_nans)
3174 {
3175 if (r->canonical)
3176 {
3177 if (fmt->canonical_nan_lsbs_set)
3178 {
3179 sig_hi = (1 << 19) - 1;
3180 sig_lo = 0xffffffff;
3181 }
3182 else
3183 {
3184 sig_hi = 0;
3185 sig_lo = 0;
3186 }
3187 }
3188 if (r->signalling == fmt->qnan_msb_set)
3189 sig_hi &= ~(1 << 19);
3190 else
3191 sig_hi |= 1 << 19;
3192 if (sig_hi == 0 && sig_lo == 0)
3193 sig_hi = 1 << 18;
3194
3195 image_hi |= 2047 << 20;
3196 image_hi |= sig_hi;
3197 image_lo = sig_lo;
3198 }
3199 else
3200 {
3201 image_hi |= 0x7fffffff;
3202 image_lo = 0xffffffff;
3203 }
3204 break;
3205
3206 case rvc_normal:
3207 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3208 whereas the intermediate representation is 0.F x 2**exp.
3209 Which means we're off by one. */
3210 if (denormal)
3211 exp = 0;
3212 else
3213 exp = REAL_EXP (r) + 1023 - 1;
3214 image_hi |= exp << 20;
3215 image_hi |= sig_hi;
3216 image_lo = sig_lo;
3217 break;
3218
3219 default:
3220 gcc_unreachable ();
3221 }
3222
3223 if (FLOAT_WORDS_BIG_ENDIAN)
3224 buf[0] = image_hi, buf[1] = image_lo;
3225 else
3226 buf[0] = image_lo, buf[1] = image_hi;
3227 }
3228
3229 static void
3230 decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3231 const long *buf)
3232 {
3233 unsigned long image_hi, image_lo;
3234 bool sign;
3235 int exp;
3236
3237 if (FLOAT_WORDS_BIG_ENDIAN)
3238 image_hi = buf[0], image_lo = buf[1];
3239 else
3240 image_lo = buf[0], image_hi = buf[1];
3241 image_lo &= 0xffffffff;
3242 image_hi &= 0xffffffff;
3243
3244 sign = (image_hi >> 31) & 1;
3245 exp = (image_hi >> 20) & 0x7ff;
3246
3247 memset (r, 0, sizeof (*r));
3248
3249 image_hi <<= 32 - 21;
3250 image_hi |= image_lo >> 21;
3251 image_hi &= 0x7fffffff;
3252 image_lo <<= 32 - 21;
3253
3254 if (exp == 0)
3255 {
3256 if ((image_hi || image_lo) && fmt->has_denorm)
3257 {
3258 r->cl = rvc_normal;
3259 r->sign = sign;
3260 SET_REAL_EXP (r, -1022);
3261 if (HOST_BITS_PER_LONG == 32)
3262 {
3263 image_hi = (image_hi << 1) | (image_lo >> 31);
3264 image_lo <<= 1;
3265 r->sig[SIGSZ-1] = image_hi;
3266 r->sig[SIGSZ-2] = image_lo;
3267 }
3268 else
3269 {
3270 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
3271 r->sig[SIGSZ-1] = image_hi;
3272 }
3273 normalize (r);
3274 }
3275 else if (fmt->has_signed_zero)
3276 r->sign = sign;
3277 }
3278 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
3279 {
3280 if (image_hi || image_lo)
3281 {
3282 r->cl = rvc_nan;
3283 r->sign = sign;
3284 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3285 if (HOST_BITS_PER_LONG == 32)
3286 {
3287 r->sig[SIGSZ-1] = image_hi;
3288 r->sig[SIGSZ-2] = image_lo;
3289 }
3290 else
3291 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3292 }
3293 else
3294 {
3295 r->cl = rvc_inf;
3296 r->sign = sign;
3297 }
3298 }
3299 else
3300 {
3301 r->cl = rvc_normal;
3302 r->sign = sign;
3303 SET_REAL_EXP (r, exp - 1023 + 1);
3304 if (HOST_BITS_PER_LONG == 32)
3305 {
3306 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3307 r->sig[SIGSZ-2] = image_lo;
3308 }
3309 else
3310 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3311 }
3312 }
3313
3314 const struct real_format ieee_double_format =
3315 {
3316 encode_ieee_double,
3317 decode_ieee_double,
3318 2,
3319 53,
3320 53,
3321 -1021,
3322 1024,
3323 63,
3324 63,
3325 false,
3326 true,
3327 true,
3328 true,
3329 true,
3330 true,
3331 true,
3332 false,
3333 "ieee_double"
3334 };
3335
3336 const struct real_format mips_double_format =
3337 {
3338 encode_ieee_double,
3339 decode_ieee_double,
3340 2,
3341 53,
3342 53,
3343 -1021,
3344 1024,
3345 63,
3346 63,
3347 false,
3348 true,
3349 true,
3350 true,
3351 true,
3352 true,
3353 false,
3354 true,
3355 "mips_double"
3356 };
3357
3358 const struct real_format motorola_double_format =
3359 {
3360 encode_ieee_double,
3361 decode_ieee_double,
3362 2,
3363 53,
3364 53,
3365 -1021,
3366 1024,
3367 63,
3368 63,
3369 false,
3370 true,
3371 true,
3372 true,
3373 true,
3374 true,
3375 true,
3376 true,
3377 "motorola_double"
3378 };
3379 \f
3380 /* IEEE extended real format. This comes in three flavors: Intel's as
3381 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3382 12- and 16-byte images may be big- or little endian; Motorola's is
3383 always big endian. */
3384
3385 /* Helper subroutine which converts from the internal format to the
3386 12-byte little-endian Intel format. Functions below adjust this
3387 for the other possible formats. */
3388 static void
3389 encode_ieee_extended (const struct real_format *fmt, long *buf,
3390 const REAL_VALUE_TYPE *r)
3391 {
3392 unsigned long image_hi, sig_hi, sig_lo;
3393 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3394
3395 image_hi = r->sign << 15;
3396 sig_hi = sig_lo = 0;
3397
3398 switch (r->cl)
3399 {
3400 case rvc_zero:
3401 break;
3402
3403 case rvc_inf:
3404 if (fmt->has_inf)
3405 {
3406 image_hi |= 32767;
3407
3408 /* Intel requires the explicit integer bit to be set, otherwise
3409 it considers the value a "pseudo-infinity". Motorola docs
3410 say it doesn't care. */
3411 sig_hi = 0x80000000;
3412 }
3413 else
3414 {
3415 image_hi |= 32767;
3416 sig_lo = sig_hi = 0xffffffff;
3417 }
3418 break;
3419
3420 case rvc_nan:
3421 if (fmt->has_nans)
3422 {
3423 image_hi |= 32767;
3424 if (r->canonical)
3425 {
3426 if (fmt->canonical_nan_lsbs_set)
3427 {
3428 sig_hi = (1 << 30) - 1;
3429 sig_lo = 0xffffffff;
3430 }
3431 }
3432 else if (HOST_BITS_PER_LONG == 32)
3433 {
3434 sig_hi = r->sig[SIGSZ-1];
3435 sig_lo = r->sig[SIGSZ-2];
3436 }
3437 else
3438 {
3439 sig_lo = r->sig[SIGSZ-1];
3440 sig_hi = sig_lo >> 31 >> 1;
3441 sig_lo &= 0xffffffff;
3442 }
3443 if (r->signalling == fmt->qnan_msb_set)
3444 sig_hi &= ~(1 << 30);
3445 else
3446 sig_hi |= 1 << 30;
3447 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3448 sig_hi = 1 << 29;
3449
3450 /* Intel requires the explicit integer bit to be set, otherwise
3451 it considers the value a "pseudo-nan". Motorola docs say it
3452 doesn't care. */
3453 sig_hi |= 0x80000000;
3454 }
3455 else
3456 {
3457 image_hi |= 32767;
3458 sig_lo = sig_hi = 0xffffffff;
3459 }
3460 break;
3461
3462 case rvc_normal:
3463 {
3464 int exp = REAL_EXP (r);
3465
3466 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3467 whereas the intermediate representation is 0.F x 2**exp.
3468 Which means we're off by one.
3469
3470 Except for Motorola, which consider exp=0 and explicit
3471 integer bit set to continue to be normalized. In theory
3472 this discrepancy has been taken care of by the difference
3473 in fmt->emin in round_for_format. */
3474
3475 if (denormal)
3476 exp = 0;
3477 else
3478 {
3479 exp += 16383 - 1;
3480 gcc_assert (exp >= 0);
3481 }
3482 image_hi |= exp;
3483
3484 if (HOST_BITS_PER_LONG == 32)
3485 {
3486 sig_hi = r->sig[SIGSZ-1];
3487 sig_lo = r->sig[SIGSZ-2];
3488 }
3489 else
3490 {
3491 sig_lo = r->sig[SIGSZ-1];
3492 sig_hi = sig_lo >> 31 >> 1;
3493 sig_lo &= 0xffffffff;
3494 }
3495 }
3496 break;
3497
3498 default:
3499 gcc_unreachable ();
3500 }
3501
3502 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3503 }
3504
3505 /* Convert from the internal format to the 12-byte Motorola format
3506 for an IEEE extended real. */
3507 static void
3508 encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3509 const REAL_VALUE_TYPE *r)
3510 {
3511 long intermed[3];
3512 encode_ieee_extended (fmt, intermed, r);
3513
3514 if (r->cl == rvc_inf)
3515 /* For infinity clear the explicit integer bit again, so that the
3516 format matches the canonical infinity generated by the FPU. */
3517 intermed[1] = 0;
3518
3519 /* Motorola chips are assumed always to be big-endian. Also, the
3520 padding in a Motorola extended real goes between the exponent and
3521 the mantissa. At this point the mantissa is entirely within
3522 elements 0 and 1 of intermed, and the exponent entirely within
3523 element 2, so all we have to do is swap the order around, and
3524 shift element 2 left 16 bits. */
3525 buf[0] = intermed[2] << 16;
3526 buf[1] = intermed[1];
3527 buf[2] = intermed[0];
3528 }
3529
3530 /* Convert from the internal format to the 12-byte Intel format for
3531 an IEEE extended real. */
3532 static void
3533 encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3534 const REAL_VALUE_TYPE *r)
3535 {
3536 if (FLOAT_WORDS_BIG_ENDIAN)
3537 {
3538 /* All the padding in an Intel-format extended real goes at the high
3539 end, which in this case is after the mantissa, not the exponent.
3540 Therefore we must shift everything down 16 bits. */
3541 long intermed[3];
3542 encode_ieee_extended (fmt, intermed, r);
3543 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3544 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3545 buf[2] = (intermed[0] << 16);
3546 }
3547 else
3548 /* encode_ieee_extended produces what we want directly. */
3549 encode_ieee_extended (fmt, buf, r);
3550 }
3551
3552 /* Convert from the internal format to the 16-byte Intel format for
3553 an IEEE extended real. */
3554 static void
3555 encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3556 const REAL_VALUE_TYPE *r)
3557 {
3558 /* All the padding in an Intel-format extended real goes at the high end. */
3559 encode_ieee_extended_intel_96 (fmt, buf, r);
3560 buf[3] = 0;
3561 }
3562
3563 /* As above, we have a helper function which converts from 12-byte
3564 little-endian Intel format to internal format. Functions below
3565 adjust for the other possible formats. */
3566 static void
3567 decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3568 const long *buf)
3569 {
3570 unsigned long image_hi, sig_hi, sig_lo;
3571 bool sign;
3572 int exp;
3573
3574 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3575 sig_lo &= 0xffffffff;
3576 sig_hi &= 0xffffffff;
3577 image_hi &= 0xffffffff;
3578
3579 sign = (image_hi >> 15) & 1;
3580 exp = image_hi & 0x7fff;
3581
3582 memset (r, 0, sizeof (*r));
3583
3584 if (exp == 0)
3585 {
3586 if ((sig_hi || sig_lo) && fmt->has_denorm)
3587 {
3588 r->cl = rvc_normal;
3589 r->sign = sign;
3590
3591 /* When the IEEE format contains a hidden bit, we know that
3592 it's zero at this point, and so shift up the significand
3593 and decrease the exponent to match. In this case, Motorola
3594 defines the explicit integer bit to be valid, so we don't
3595 know whether the msb is set or not. */
3596 SET_REAL_EXP (r, fmt->emin);
3597 if (HOST_BITS_PER_LONG == 32)
3598 {
3599 r->sig[SIGSZ-1] = sig_hi;
3600 r->sig[SIGSZ-2] = sig_lo;
3601 }
3602 else
3603 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3604
3605 normalize (r);
3606 }
3607 else if (fmt->has_signed_zero)
3608 r->sign = sign;
3609 }
3610 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3611 {
3612 /* See above re "pseudo-infinities" and "pseudo-nans".
3613 Short summary is that the MSB will likely always be
3614 set, and that we don't care about it. */
3615 sig_hi &= 0x7fffffff;
3616
3617 if (sig_hi || sig_lo)
3618 {
3619 r->cl = rvc_nan;
3620 r->sign = sign;
3621 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3622 if (HOST_BITS_PER_LONG == 32)
3623 {
3624 r->sig[SIGSZ-1] = sig_hi;
3625 r->sig[SIGSZ-2] = sig_lo;
3626 }
3627 else
3628 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3629 }
3630 else
3631 {
3632 r->cl = rvc_inf;
3633 r->sign = sign;
3634 }
3635 }
3636 else
3637 {
3638 r->cl = rvc_normal;
3639 r->sign = sign;
3640 SET_REAL_EXP (r, exp - 16383 + 1);
3641 if (HOST_BITS_PER_LONG == 32)
3642 {
3643 r->sig[SIGSZ-1] = sig_hi;
3644 r->sig[SIGSZ-2] = sig_lo;
3645 }
3646 else
3647 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3648 }
3649 }
3650
3651 /* Convert from the internal format to the 12-byte Motorola format
3652 for an IEEE extended real. */
3653 static void
3654 decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3655 const long *buf)
3656 {
3657 long intermed[3];
3658
3659 /* Motorola chips are assumed always to be big-endian. Also, the
3660 padding in a Motorola extended real goes between the exponent and
3661 the mantissa; remove it. */
3662 intermed[0] = buf[2];
3663 intermed[1] = buf[1];
3664 intermed[2] = (unsigned long)buf[0] >> 16;
3665
3666 decode_ieee_extended (fmt, r, intermed);
3667 }
3668
3669 /* Convert from the internal format to the 12-byte Intel format for
3670 an IEEE extended real. */
3671 static void
3672 decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3673 const long *buf)
3674 {
3675 if (FLOAT_WORDS_BIG_ENDIAN)
3676 {
3677 /* All the padding in an Intel-format extended real goes at the high
3678 end, which in this case is after the mantissa, not the exponent.
3679 Therefore we must shift everything up 16 bits. */
3680 long intermed[3];
3681
3682 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3683 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3684 intermed[2] = ((unsigned long)buf[0] >> 16);
3685
3686 decode_ieee_extended (fmt, r, intermed);
3687 }
3688 else
3689 /* decode_ieee_extended produces what we want directly. */
3690 decode_ieee_extended (fmt, r, buf);
3691 }
3692
3693 /* Convert from the internal format to the 16-byte Intel format for
3694 an IEEE extended real. */
3695 static void
3696 decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3697 const long *buf)
3698 {
3699 /* All the padding in an Intel-format extended real goes at the high end. */
3700 decode_ieee_extended_intel_96 (fmt, r, buf);
3701 }
3702
3703 const struct real_format ieee_extended_motorola_format =
3704 {
3705 encode_ieee_extended_motorola,
3706 decode_ieee_extended_motorola,
3707 2,
3708 64,
3709 64,
3710 -16382,
3711 16384,
3712 95,
3713 95,
3714 false,
3715 true,
3716 true,
3717 true,
3718 true,
3719 true,
3720 true,
3721 true,
3722 "ieee_extended_motorola"
3723 };
3724
3725 const struct real_format ieee_extended_intel_96_format =
3726 {
3727 encode_ieee_extended_intel_96,
3728 decode_ieee_extended_intel_96,
3729 2,
3730 64,
3731 64,
3732 -16381,
3733 16384,
3734 79,
3735 79,
3736 false,
3737 true,
3738 true,
3739 true,
3740 true,
3741 true,
3742 true,
3743 false,
3744 "ieee_extended_intel_96"
3745 };
3746
3747 const struct real_format ieee_extended_intel_128_format =
3748 {
3749 encode_ieee_extended_intel_128,
3750 decode_ieee_extended_intel_128,
3751 2,
3752 64,
3753 64,
3754 -16381,
3755 16384,
3756 79,
3757 79,
3758 false,
3759 true,
3760 true,
3761 true,
3762 true,
3763 true,
3764 true,
3765 false,
3766 "ieee_extended_intel_128"
3767 };
3768
3769 /* The following caters to i386 systems that set the rounding precision
3770 to 53 bits instead of 64, e.g. FreeBSD. */
3771 const struct real_format ieee_extended_intel_96_round_53_format =
3772 {
3773 encode_ieee_extended_intel_96,
3774 decode_ieee_extended_intel_96,
3775 2,
3776 53,
3777 53,
3778 -16381,
3779 16384,
3780 79,
3781 79,
3782 false,
3783 true,
3784 true,
3785 true,
3786 true,
3787 true,
3788 true,
3789 false,
3790 "ieee_extended_intel_96_round_53"
3791 };
3792 \f
3793 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3794 numbers whose sum is equal to the extended precision value. The number
3795 with greater magnitude is first. This format has the same magnitude
3796 range as an IEEE double precision value, but effectively 106 bits of
3797 significand precision. Infinity and NaN are represented by their IEEE
3798 double precision value stored in the first number, the second number is
3799 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3800
3801 static void encode_ibm_extended (const struct real_format *fmt,
3802 long *, const REAL_VALUE_TYPE *);
3803 static void decode_ibm_extended (const struct real_format *,
3804 REAL_VALUE_TYPE *, const long *);
3805
3806 static void
3807 encode_ibm_extended (const struct real_format *fmt, long *buf,
3808 const REAL_VALUE_TYPE *r)
3809 {
3810 REAL_VALUE_TYPE u, normr, v;
3811 const struct real_format *base_fmt;
3812
3813 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3814
3815 /* Renormalize R before doing any arithmetic on it. */
3816 normr = *r;
3817 if (normr.cl == rvc_normal)
3818 normalize (&normr);
3819
3820 /* u = IEEE double precision portion of significand. */
3821 u = normr;
3822 round_for_format (base_fmt, &u);
3823 encode_ieee_double (base_fmt, &buf[0], &u);
3824
3825 if (u.cl == rvc_normal)
3826 {
3827 do_add (&v, &normr, &u, 1);
3828 /* Call round_for_format since we might need to denormalize. */
3829 round_for_format (base_fmt, &v);
3830 encode_ieee_double (base_fmt, &buf[2], &v);
3831 }
3832 else
3833 {
3834 /* Inf, NaN, 0 are all representable as doubles, so the
3835 least-significant part can be 0.0. */
3836 buf[2] = 0;
3837 buf[3] = 0;
3838 }
3839 }
3840
3841 static void
3842 decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3843 const long *buf)
3844 {
3845 REAL_VALUE_TYPE u, v;
3846 const struct real_format *base_fmt;
3847
3848 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3849 decode_ieee_double (base_fmt, &u, &buf[0]);
3850
3851 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3852 {
3853 decode_ieee_double (base_fmt, &v, &buf[2]);
3854 do_add (r, &u, &v, 0);
3855 }
3856 else
3857 *r = u;
3858 }
3859
3860 const struct real_format ibm_extended_format =
3861 {
3862 encode_ibm_extended,
3863 decode_ibm_extended,
3864 2,
3865 53 + 53,
3866 53,
3867 -1021 + 53,
3868 1024,
3869 127,
3870 -1,
3871 false,
3872 true,
3873 true,
3874 true,
3875 true,
3876 true,
3877 true,
3878 false,
3879 "ibm_extended"
3880 };
3881
3882 const struct real_format mips_extended_format =
3883 {
3884 encode_ibm_extended,
3885 decode_ibm_extended,
3886 2,
3887 53 + 53,
3888 53,
3889 -1021 + 53,
3890 1024,
3891 127,
3892 -1,
3893 false,
3894 true,
3895 true,
3896 true,
3897 true,
3898 true,
3899 false,
3900 true,
3901 "mips_extended"
3902 };
3903
3904 \f
3905 /* IEEE quad precision format. */
3906
3907 static void encode_ieee_quad (const struct real_format *fmt,
3908 long *, const REAL_VALUE_TYPE *);
3909 static void decode_ieee_quad (const struct real_format *,
3910 REAL_VALUE_TYPE *, const long *);
3911
3912 static void
3913 encode_ieee_quad (const struct real_format *fmt, long *buf,
3914 const REAL_VALUE_TYPE *r)
3915 {
3916 unsigned long image3, image2, image1, image0, exp;
3917 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
3918 REAL_VALUE_TYPE u;
3919
3920 image3 = r->sign << 31;
3921 image2 = 0;
3922 image1 = 0;
3923 image0 = 0;
3924
3925 rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
3926
3927 switch (r->cl)
3928 {
3929 case rvc_zero:
3930 break;
3931
3932 case rvc_inf:
3933 if (fmt->has_inf)
3934 image3 |= 32767 << 16;
3935 else
3936 {
3937 image3 |= 0x7fffffff;
3938 image2 = 0xffffffff;
3939 image1 = 0xffffffff;
3940 image0 = 0xffffffff;
3941 }
3942 break;
3943
3944 case rvc_nan:
3945 if (fmt->has_nans)
3946 {
3947 image3 |= 32767 << 16;
3948
3949 if (r->canonical)
3950 {
3951 if (fmt->canonical_nan_lsbs_set)
3952 {
3953 image3 |= 0x7fff;
3954 image2 = image1 = image0 = 0xffffffff;
3955 }
3956 }
3957 else if (HOST_BITS_PER_LONG == 32)
3958 {
3959 image0 = u.sig[0];
3960 image1 = u.sig[1];
3961 image2 = u.sig[2];
3962 image3 |= u.sig[3] & 0xffff;
3963 }
3964 else
3965 {
3966 image0 = u.sig[0];
3967 image1 = image0 >> 31 >> 1;
3968 image2 = u.sig[1];
3969 image3 |= (image2 >> 31 >> 1) & 0xffff;
3970 image0 &= 0xffffffff;
3971 image2 &= 0xffffffff;
3972 }
3973 if (r->signalling == fmt->qnan_msb_set)
3974 image3 &= ~0x8000;
3975 else
3976 image3 |= 0x8000;
3977 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
3978 image3 |= 0x4000;
3979 }
3980 else
3981 {
3982 image3 |= 0x7fffffff;
3983 image2 = 0xffffffff;
3984 image1 = 0xffffffff;
3985 image0 = 0xffffffff;
3986 }
3987 break;
3988
3989 case rvc_normal:
3990 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3991 whereas the intermediate representation is 0.F x 2**exp.
3992 Which means we're off by one. */
3993 if (denormal)
3994 exp = 0;
3995 else
3996 exp = REAL_EXP (r) + 16383 - 1;
3997 image3 |= exp << 16;
3998
3999 if (HOST_BITS_PER_LONG == 32)
4000 {
4001 image0 = u.sig[0];
4002 image1 = u.sig[1];
4003 image2 = u.sig[2];
4004 image3 |= u.sig[3] & 0xffff;
4005 }
4006 else
4007 {
4008 image0 = u.sig[0];
4009 image1 = image0 >> 31 >> 1;
4010 image2 = u.sig[1];
4011 image3 |= (image2 >> 31 >> 1) & 0xffff;
4012 image0 &= 0xffffffff;
4013 image2 &= 0xffffffff;
4014 }
4015 break;
4016
4017 default:
4018 gcc_unreachable ();
4019 }
4020
4021 if (FLOAT_WORDS_BIG_ENDIAN)
4022 {
4023 buf[0] = image3;
4024 buf[1] = image2;
4025 buf[2] = image1;
4026 buf[3] = image0;
4027 }
4028 else
4029 {
4030 buf[0] = image0;
4031 buf[1] = image1;
4032 buf[2] = image2;
4033 buf[3] = image3;
4034 }
4035 }
4036
4037 static void
4038 decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4039 const long *buf)
4040 {
4041 unsigned long image3, image2, image1, image0;
4042 bool sign;
4043 int exp;
4044
4045 if (FLOAT_WORDS_BIG_ENDIAN)
4046 {
4047 image3 = buf[0];
4048 image2 = buf[1];
4049 image1 = buf[2];
4050 image0 = buf[3];
4051 }
4052 else
4053 {
4054 image0 = buf[0];
4055 image1 = buf[1];
4056 image2 = buf[2];
4057 image3 = buf[3];
4058 }
4059 image0 &= 0xffffffff;
4060 image1 &= 0xffffffff;
4061 image2 &= 0xffffffff;
4062
4063 sign = (image3 >> 31) & 1;
4064 exp = (image3 >> 16) & 0x7fff;
4065 image3 &= 0xffff;
4066
4067 memset (r, 0, sizeof (*r));
4068
4069 if (exp == 0)
4070 {
4071 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
4072 {
4073 r->cl = rvc_normal;
4074 r->sign = sign;
4075
4076 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
4077 if (HOST_BITS_PER_LONG == 32)
4078 {
4079 r->sig[0] = image0;
4080 r->sig[1] = image1;
4081 r->sig[2] = image2;
4082 r->sig[3] = image3;
4083 }
4084 else
4085 {
4086 r->sig[0] = (image1 << 31 << 1) | image0;
4087 r->sig[1] = (image3 << 31 << 1) | image2;
4088 }
4089
4090 normalize (r);
4091 }
4092 else if (fmt->has_signed_zero)
4093 r->sign = sign;
4094 }
4095 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
4096 {
4097 if (image3 | image2 | image1 | image0)
4098 {
4099 r->cl = rvc_nan;
4100 r->sign = sign;
4101 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
4102
4103 if (HOST_BITS_PER_LONG == 32)
4104 {
4105 r->sig[0] = image0;
4106 r->sig[1] = image1;
4107 r->sig[2] = image2;
4108 r->sig[3] = image3;
4109 }
4110 else
4111 {
4112 r->sig[0] = (image1 << 31 << 1) | image0;
4113 r->sig[1] = (image3 << 31 << 1) | image2;
4114 }
4115 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4116 }
4117 else
4118 {
4119 r->cl = rvc_inf;
4120 r->sign = sign;
4121 }
4122 }
4123 else
4124 {
4125 r->cl = rvc_normal;
4126 r->sign = sign;
4127 SET_REAL_EXP (r, exp - 16383 + 1);
4128
4129 if (HOST_BITS_PER_LONG == 32)
4130 {
4131 r->sig[0] = image0;
4132 r->sig[1] = image1;
4133 r->sig[2] = image2;
4134 r->sig[3] = image3;
4135 }
4136 else
4137 {
4138 r->sig[0] = (image1 << 31 << 1) | image0;
4139 r->sig[1] = (image3 << 31 << 1) | image2;
4140 }
4141 lshift_significand (r, r, SIGNIFICAND_BITS - 113);
4142 r->sig[SIGSZ-1] |= SIG_MSB;
4143 }
4144 }
4145
4146 const struct real_format ieee_quad_format =
4147 {
4148 encode_ieee_quad,
4149 decode_ieee_quad,
4150 2,
4151 113,
4152 113,
4153 -16381,
4154 16384,
4155 127,
4156 127,
4157 false,
4158 true,
4159 true,
4160 true,
4161 true,
4162 true,
4163 true,
4164 false,
4165 "ieee_quad"
4166 };
4167
4168 const struct real_format mips_quad_format =
4169 {
4170 encode_ieee_quad,
4171 decode_ieee_quad,
4172 2,
4173 113,
4174 113,
4175 -16381,
4176 16384,
4177 127,
4178 127,
4179 false,
4180 true,
4181 true,
4182 true,
4183 true,
4184 true,
4185 false,
4186 true,
4187 "mips_quad"
4188 };
4189 \f
4190 /* Descriptions of VAX floating point formats can be found beginning at
4191
4192 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4193
4194 The thing to remember is that they're almost IEEE, except for word
4195 order, exponent bias, and the lack of infinities, nans, and denormals.
4196
4197 We don't implement the H_floating format here, simply because neither
4198 the VAX or Alpha ports use it. */
4199
4200 static void encode_vax_f (const struct real_format *fmt,
4201 long *, const REAL_VALUE_TYPE *);
4202 static void decode_vax_f (const struct real_format *,
4203 REAL_VALUE_TYPE *, const long *);
4204 static void encode_vax_d (const struct real_format *fmt,
4205 long *, const REAL_VALUE_TYPE *);
4206 static void decode_vax_d (const struct real_format *,
4207 REAL_VALUE_TYPE *, const long *);
4208 static void encode_vax_g (const struct real_format *fmt,
4209 long *, const REAL_VALUE_TYPE *);
4210 static void decode_vax_g (const struct real_format *,
4211 REAL_VALUE_TYPE *, const long *);
4212
4213 static void
4214 encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4215 const REAL_VALUE_TYPE *r)
4216 {
4217 unsigned long sign, exp, sig, image;
4218
4219 sign = r->sign << 15;
4220
4221 switch (r->cl)
4222 {
4223 case rvc_zero:
4224 image = 0;
4225 break;
4226
4227 case rvc_inf:
4228 case rvc_nan:
4229 image = 0xffff7fff | sign;
4230 break;
4231
4232 case rvc_normal:
4233 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4234 exp = REAL_EXP (r) + 128;
4235
4236 image = (sig << 16) & 0xffff0000;
4237 image |= sign;
4238 image |= exp << 7;
4239 image |= sig >> 16;
4240 break;
4241
4242 default:
4243 gcc_unreachable ();
4244 }
4245
4246 buf[0] = image;
4247 }
4248
4249 static void
4250 decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
4251 REAL_VALUE_TYPE *r, const long *buf)
4252 {
4253 unsigned long image = buf[0] & 0xffffffff;
4254 int exp = (image >> 7) & 0xff;
4255
4256 memset (r, 0, sizeof (*r));
4257
4258 if (exp != 0)
4259 {
4260 r->cl = rvc_normal;
4261 r->sign = (image >> 15) & 1;
4262 SET_REAL_EXP (r, exp - 128);
4263
4264 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
4265 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4266 }
4267 }
4268
4269 static void
4270 encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4271 const REAL_VALUE_TYPE *r)
4272 {
4273 unsigned long image0, image1, sign = r->sign << 15;
4274
4275 switch (r->cl)
4276 {
4277 case rvc_zero:
4278 image0 = image1 = 0;
4279 break;
4280
4281 case rvc_inf:
4282 case rvc_nan:
4283 image0 = 0xffff7fff | sign;
4284 image1 = 0xffffffff;
4285 break;
4286
4287 case rvc_normal:
4288 /* Extract the significand into straight hi:lo. */
4289 if (HOST_BITS_PER_LONG == 64)
4290 {
4291 image0 = r->sig[SIGSZ-1];
4292 image1 = (image0 >> (64 - 56)) & 0xffffffff;
4293 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
4294 }
4295 else
4296 {
4297 image0 = r->sig[SIGSZ-1];
4298 image1 = r->sig[SIGSZ-2];
4299 image1 = (image0 << 24) | (image1 >> 8);
4300 image0 = (image0 >> 8) & 0xffffff;
4301 }
4302
4303 /* Rearrange the half-words of the significand to match the
4304 external format. */
4305 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
4306 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4307
4308 /* Add the sign and exponent. */
4309 image0 |= sign;
4310 image0 |= (REAL_EXP (r) + 128) << 7;
4311 break;
4312
4313 default:
4314 gcc_unreachable ();
4315 }
4316
4317 if (FLOAT_WORDS_BIG_ENDIAN)
4318 buf[0] = image1, buf[1] = image0;
4319 else
4320 buf[0] = image0, buf[1] = image1;
4321 }
4322
4323 static void
4324 decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
4325 REAL_VALUE_TYPE *r, const long *buf)
4326 {
4327 unsigned long image0, image1;
4328 int exp;
4329
4330 if (FLOAT_WORDS_BIG_ENDIAN)
4331 image1 = buf[0], image0 = buf[1];
4332 else
4333 image0 = buf[0], image1 = buf[1];
4334 image0 &= 0xffffffff;
4335 image1 &= 0xffffffff;
4336
4337 exp = (image0 >> 7) & 0xff;
4338
4339 memset (r, 0, sizeof (*r));
4340
4341 if (exp != 0)
4342 {
4343 r->cl = rvc_normal;
4344 r->sign = (image0 >> 15) & 1;
4345 SET_REAL_EXP (r, exp - 128);
4346
4347 /* Rearrange the half-words of the external format into
4348 proper ascending order. */
4349 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4350 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4351
4352 if (HOST_BITS_PER_LONG == 64)
4353 {
4354 image0 = (image0 << 31 << 1) | image1;
4355 image0 <<= 64 - 56;
4356 image0 |= SIG_MSB;
4357 r->sig[SIGSZ-1] = image0;
4358 }
4359 else
4360 {
4361 r->sig[SIGSZ-1] = image0;
4362 r->sig[SIGSZ-2] = image1;
4363 lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
4364 r->sig[SIGSZ-1] |= SIG_MSB;
4365 }
4366 }
4367 }
4368
4369 static void
4370 encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4371 const REAL_VALUE_TYPE *r)
4372 {
4373 unsigned long image0, image1, sign = r->sign << 15;
4374
4375 switch (r->cl)
4376 {
4377 case rvc_zero:
4378 image0 = image1 = 0;
4379 break;
4380
4381 case rvc_inf:
4382 case rvc_nan:
4383 image0 = 0xffff7fff | sign;
4384 image1 = 0xffffffff;
4385 break;
4386
4387 case rvc_normal:
4388 /* Extract the significand into straight hi:lo. */
4389 if (HOST_BITS_PER_LONG == 64)
4390 {
4391 image0 = r->sig[SIGSZ-1];
4392 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4393 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4394 }
4395 else
4396 {
4397 image0 = r->sig[SIGSZ-1];
4398 image1 = r->sig[SIGSZ-2];
4399 image1 = (image0 << 21) | (image1 >> 11);
4400 image0 = (image0 >> 11) & 0xfffff;
4401 }
4402
4403 /* Rearrange the half-words of the significand to match the
4404 external format. */
4405 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4406 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4407
4408 /* Add the sign and exponent. */
4409 image0 |= sign;
4410 image0 |= (REAL_EXP (r) + 1024) << 4;
4411 break;
4412
4413 default:
4414 gcc_unreachable ();
4415 }
4416
4417 if (FLOAT_WORDS_BIG_ENDIAN)
4418 buf[0] = image1, buf[1] = image0;
4419 else
4420 buf[0] = image0, buf[1] = image1;
4421 }
4422
4423 static void
4424 decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4425 REAL_VALUE_TYPE *r, const long *buf)
4426 {
4427 unsigned long image0, image1;
4428 int exp;
4429
4430 if (FLOAT_WORDS_BIG_ENDIAN)
4431 image1 = buf[0], image0 = buf[1];
4432 else
4433 image0 = buf[0], image1 = buf[1];
4434 image0 &= 0xffffffff;
4435 image1 &= 0xffffffff;
4436
4437 exp = (image0 >> 4) & 0x7ff;
4438
4439 memset (r, 0, sizeof (*r));
4440
4441 if (exp != 0)
4442 {
4443 r->cl = rvc_normal;
4444 r->sign = (image0 >> 15) & 1;
4445 SET_REAL_EXP (r, exp - 1024);
4446
4447 /* Rearrange the half-words of the external format into
4448 proper ascending order. */
4449 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4450 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4451
4452 if (HOST_BITS_PER_LONG == 64)
4453 {
4454 image0 = (image0 << 31 << 1) | image1;
4455 image0 <<= 64 - 53;
4456 image0 |= SIG_MSB;
4457 r->sig[SIGSZ-1] = image0;
4458 }
4459 else
4460 {
4461 r->sig[SIGSZ-1] = image0;
4462 r->sig[SIGSZ-2] = image1;
4463 lshift_significand (r, r, 64 - 53);
4464 r->sig[SIGSZ-1] |= SIG_MSB;
4465 }
4466 }
4467 }
4468
4469 const struct real_format vax_f_format =
4470 {
4471 encode_vax_f,
4472 decode_vax_f,
4473 2,
4474 24,
4475 24,
4476 -127,
4477 127,
4478 15,
4479 15,
4480 false,
4481 false,
4482 false,
4483 false,
4484 false,
4485 false,
4486 false,
4487 false,
4488 "vax_f"
4489 };
4490
4491 const struct real_format vax_d_format =
4492 {
4493 encode_vax_d,
4494 decode_vax_d,
4495 2,
4496 56,
4497 56,
4498 -127,
4499 127,
4500 15,
4501 15,
4502 false,
4503 false,
4504 false,
4505 false,
4506 false,
4507 false,
4508 false,
4509 false,
4510 "vax_d"
4511 };
4512
4513 const struct real_format vax_g_format =
4514 {
4515 encode_vax_g,
4516 decode_vax_g,
4517 2,
4518 53,
4519 53,
4520 -1023,
4521 1023,
4522 15,
4523 15,
4524 false,
4525 false,
4526 false,
4527 false,
4528 false,
4529 false,
4530 false,
4531 false,
4532 "vax_g"
4533 };
4534 \f
4535 /* Encode real R into a single precision DFP value in BUF. */
4536 static void
4537 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4538 long *buf ATTRIBUTE_UNUSED,
4539 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4540 {
4541 encode_decimal32 (fmt, buf, r);
4542 }
4543
4544 /* Decode a single precision DFP value in BUF into a real R. */
4545 static void
4546 decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4547 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4548 const long *buf ATTRIBUTE_UNUSED)
4549 {
4550 decode_decimal32 (fmt, r, buf);
4551 }
4552
4553 /* Encode real R into a double precision DFP value in BUF. */
4554 static void
4555 encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4556 long *buf ATTRIBUTE_UNUSED,
4557 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4558 {
4559 encode_decimal64 (fmt, buf, r);
4560 }
4561
4562 /* Decode a double precision DFP value in BUF into a real R. */
4563 static void
4564 decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4565 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4566 const long *buf ATTRIBUTE_UNUSED)
4567 {
4568 decode_decimal64 (fmt, r, buf);
4569 }
4570
4571 /* Encode real R into a quad precision DFP value in BUF. */
4572 static void
4573 encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4574 long *buf ATTRIBUTE_UNUSED,
4575 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4576 {
4577 encode_decimal128 (fmt, buf, r);
4578 }
4579
4580 /* Decode a quad precision DFP value in BUF into a real R. */
4581 static void
4582 decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4583 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4584 const long *buf ATTRIBUTE_UNUSED)
4585 {
4586 decode_decimal128 (fmt, r, buf);
4587 }
4588
4589 /* Single precision decimal floating point (IEEE 754). */
4590 const struct real_format decimal_single_format =
4591 {
4592 encode_decimal_single,
4593 decode_decimal_single,
4594 10,
4595 7,
4596 7,
4597 -94,
4598 97,
4599 31,
4600 31,
4601 false,
4602 true,
4603 true,
4604 true,
4605 true,
4606 true,
4607 true,
4608 false,
4609 "decimal_single"
4610 };
4611
4612 /* Double precision decimal floating point (IEEE 754). */
4613 const struct real_format decimal_double_format =
4614 {
4615 encode_decimal_double,
4616 decode_decimal_double,
4617 10,
4618 16,
4619 16,
4620 -382,
4621 385,
4622 63,
4623 63,
4624 false,
4625 true,
4626 true,
4627 true,
4628 true,
4629 true,
4630 true,
4631 false,
4632 "decimal_double"
4633 };
4634
4635 /* Quad precision decimal floating point (IEEE 754). */
4636 const struct real_format decimal_quad_format =
4637 {
4638 encode_decimal_quad,
4639 decode_decimal_quad,
4640 10,
4641 34,
4642 34,
4643 -6142,
4644 6145,
4645 127,
4646 127,
4647 false,
4648 true,
4649 true,
4650 true,
4651 true,
4652 true,
4653 true,
4654 false,
4655 "decimal_quad"
4656 };
4657 \f
4658 /* Encode half-precision floats. This routine is used both for the IEEE
4659 ARM alternative encodings. */
4660 static void
4661 encode_ieee_half (const struct real_format *fmt, long *buf,
4662 const REAL_VALUE_TYPE *r)
4663 {
4664 unsigned long image, sig, exp;
4665 unsigned long sign = r->sign;
4666 bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
4667
4668 image = sign << 15;
4669 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 11)) & 0x3ff;
4670
4671 switch (r->cl)
4672 {
4673 case rvc_zero:
4674 break;
4675
4676 case rvc_inf:
4677 if (fmt->has_inf)
4678 image |= 31 << 10;
4679 else
4680 image |= 0x7fff;
4681 break;
4682
4683 case rvc_nan:
4684 if (fmt->has_nans)
4685 {
4686 if (r->canonical)
4687 sig = (fmt->canonical_nan_lsbs_set ? (1 << 9) - 1 : 0);
4688 if (r->signalling == fmt->qnan_msb_set)
4689 sig &= ~(1 << 9);
4690 else
4691 sig |= 1 << 9;
4692 if (sig == 0)
4693 sig = 1 << 8;
4694
4695 image |= 31 << 10;
4696 image |= sig;
4697 }
4698 else
4699 image |= 0x3ff;
4700 break;
4701
4702 case rvc_normal:
4703 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4704 whereas the intermediate representation is 0.F x 2**exp.
4705 Which means we're off by one. */
4706 if (denormal)
4707 exp = 0;
4708 else
4709 exp = REAL_EXP (r) + 15 - 1;
4710 image |= exp << 10;
4711 image |= sig;
4712 break;
4713
4714 default:
4715 gcc_unreachable ();
4716 }
4717
4718 buf[0] = image;
4719 }
4720
4721 /* Decode half-precision floats. This routine is used both for the IEEE
4722 ARM alternative encodings. */
4723 static void
4724 decode_ieee_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4725 const long *buf)
4726 {
4727 unsigned long image = buf[0] & 0xffff;
4728 bool sign = (image >> 15) & 1;
4729 int exp = (image >> 10) & 0x1f;
4730
4731 memset (r, 0, sizeof (*r));
4732 image <<= HOST_BITS_PER_LONG - 11;
4733 image &= ~SIG_MSB;
4734
4735 if (exp == 0)
4736 {
4737 if (image && fmt->has_denorm)
4738 {
4739 r->cl = rvc_normal;
4740 r->sign = sign;
4741 SET_REAL_EXP (r, -14);
4742 r->sig[SIGSZ-1] = image << 1;
4743 normalize (r);
4744 }
4745 else if (fmt->has_signed_zero)
4746 r->sign = sign;
4747 }
4748 else if (exp == 31 && (fmt->has_nans || fmt->has_inf))
4749 {
4750 if (image)
4751 {
4752 r->cl = rvc_nan;
4753 r->sign = sign;
4754 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4755 ^ fmt->qnan_msb_set);
4756 r->sig[SIGSZ-1] = image;
4757 }
4758 else
4759 {
4760 r->cl = rvc_inf;
4761 r->sign = sign;
4762 }
4763 }
4764 else
4765 {
4766 r->cl = rvc_normal;
4767 r->sign = sign;
4768 SET_REAL_EXP (r, exp - 15 + 1);
4769 r->sig[SIGSZ-1] = image | SIG_MSB;
4770 }
4771 }
4772
4773 /* Half-precision format, as specified in IEEE 754R. */
4774 const struct real_format ieee_half_format =
4775 {
4776 encode_ieee_half,
4777 decode_ieee_half,
4778 2,
4779 11,
4780 11,
4781 -13,
4782 16,
4783 15,
4784 15,
4785 false,
4786 true,
4787 true,
4788 true,
4789 true,
4790 true,
4791 true,
4792 false,
4793 "ieee_half"
4794 };
4795
4796 /* ARM's alternative half-precision format, similar to IEEE but with
4797 no reserved exponent value for NaNs and infinities; rather, it just
4798 extends the range of exponents by one. */
4799 const struct real_format arm_half_format =
4800 {
4801 encode_ieee_half,
4802 decode_ieee_half,
4803 2,
4804 11,
4805 11,
4806 -13,
4807 17,
4808 15,
4809 15,
4810 false,
4811 true,
4812 false,
4813 false,
4814 true,
4815 true,
4816 false,
4817 false,
4818 "arm_half"
4819 };
4820 \f
4821 /* A synthetic "format" for internal arithmetic. It's the size of the
4822 internal significand minus the two bits needed for proper rounding.
4823 The encode and decode routines exist only to satisfy our paranoia
4824 harness. */
4825
4826 static void encode_internal (const struct real_format *fmt,
4827 long *, const REAL_VALUE_TYPE *);
4828 static void decode_internal (const struct real_format *,
4829 REAL_VALUE_TYPE *, const long *);
4830
4831 static void
4832 encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4833 const REAL_VALUE_TYPE *r)
4834 {
4835 memcpy (buf, r, sizeof (*r));
4836 }
4837
4838 static void
4839 decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
4840 REAL_VALUE_TYPE *r, const long *buf)
4841 {
4842 memcpy (r, buf, sizeof (*r));
4843 }
4844
4845 const struct real_format real_internal_format =
4846 {
4847 encode_internal,
4848 decode_internal,
4849 2,
4850 SIGNIFICAND_BITS - 2,
4851 SIGNIFICAND_BITS - 2,
4852 -MAX_EXP,
4853 MAX_EXP,
4854 -1,
4855 -1,
4856 false,
4857 false,
4858 true,
4859 true,
4860 false,
4861 true,
4862 true,
4863 false,
4864 "real_internal"
4865 };
4866 \f
4867 /* Calculate X raised to the integer exponent N in mode MODE and store
4868 the result in R. Return true if the result may be inexact due to
4869 loss of precision. The algorithm is the classic "left-to-right binary
4870 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4871 Algorithms", "The Art of Computer Programming", Volume 2. */
4872
4873 bool
4874 real_powi (REAL_VALUE_TYPE *r, machine_mode mode,
4875 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4876 {
4877 unsigned HOST_WIDE_INT bit;
4878 REAL_VALUE_TYPE t;
4879 bool inexact = false;
4880 bool init = false;
4881 bool neg;
4882 int i;
4883
4884 if (n == 0)
4885 {
4886 *r = dconst1;
4887 return false;
4888 }
4889 else if (n < 0)
4890 {
4891 /* Don't worry about overflow, from now on n is unsigned. */
4892 neg = true;
4893 n = -n;
4894 }
4895 else
4896 neg = false;
4897
4898 t = *x;
4899 bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
4900 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4901 {
4902 if (init)
4903 {
4904 inexact |= do_multiply (&t, &t, &t);
4905 if (n & bit)
4906 inexact |= do_multiply (&t, &t, x);
4907 }
4908 else if (n & bit)
4909 init = true;
4910 bit >>= 1;
4911 }
4912
4913 if (neg)
4914 inexact |= do_divide (&t, &dconst1, &t);
4915
4916 real_convert (r, mode, &t);
4917 return inexact;
4918 }
4919
4920 /* Round X to the nearest integer not larger in absolute value, i.e.
4921 towards zero, placing the result in R in mode MODE. */
4922
4923 void
4924 real_trunc (REAL_VALUE_TYPE *r, machine_mode mode,
4925 const REAL_VALUE_TYPE *x)
4926 {
4927 do_fix_trunc (r, x);
4928 if (mode != VOIDmode)
4929 real_convert (r, mode, r);
4930 }
4931
4932 /* Round X to the largest integer not greater in value, i.e. round
4933 down, placing the result in R in mode MODE. */
4934
4935 void
4936 real_floor (REAL_VALUE_TYPE *r, machine_mode mode,
4937 const REAL_VALUE_TYPE *x)
4938 {
4939 REAL_VALUE_TYPE t;
4940
4941 do_fix_trunc (&t, x);
4942 if (! real_identical (&t, x) && x->sign)
4943 do_add (&t, &t, &dconstm1, 0);
4944 if (mode != VOIDmode)
4945 real_convert (r, mode, &t);
4946 else
4947 *r = t;
4948 }
4949
4950 /* Round X to the smallest integer not less then argument, i.e. round
4951 up, placing the result in R in mode MODE. */
4952
4953 void
4954 real_ceil (REAL_VALUE_TYPE *r, machine_mode mode,
4955 const REAL_VALUE_TYPE *x)
4956 {
4957 REAL_VALUE_TYPE t;
4958
4959 do_fix_trunc (&t, x);
4960 if (! real_identical (&t, x) && ! x->sign)
4961 do_add (&t, &t, &dconst1, 0);
4962 if (mode != VOIDmode)
4963 real_convert (r, mode, &t);
4964 else
4965 *r = t;
4966 }
4967
4968 /* Round X to the nearest integer, but round halfway cases away from
4969 zero. */
4970
4971 void
4972 real_round (REAL_VALUE_TYPE *r, machine_mode mode,
4973 const REAL_VALUE_TYPE *x)
4974 {
4975 do_add (r, x, &dconsthalf, x->sign);
4976 do_fix_trunc (r, r);
4977 if (mode != VOIDmode)
4978 real_convert (r, mode, r);
4979 }
4980
4981 /* Set the sign of R to the sign of X. */
4982
4983 void
4984 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4985 {
4986 r->sign = x->sign;
4987 }
4988
4989 /* Check whether the real constant value given is an integer. */
4990
4991 bool
4992 real_isinteger (const REAL_VALUE_TYPE *c, machine_mode mode)
4993 {
4994 REAL_VALUE_TYPE cint;
4995
4996 real_trunc (&cint, mode, c);
4997 return real_identical (c, &cint);
4998 }
4999
5000 /* Check whether C is an integer that fits in a HOST_WIDE_INT,
5001 storing it in *INT_OUT if so. */
5002
5003 bool
5004 real_isinteger (const REAL_VALUE_TYPE *c, HOST_WIDE_INT *int_out)
5005 {
5006 REAL_VALUE_TYPE cint;
5007
5008 HOST_WIDE_INT n = real_to_integer (c);
5009 real_from_integer (&cint, VOIDmode, n, SIGNED);
5010 if (real_identical (c, &cint))
5011 {
5012 *int_out = n;
5013 return true;
5014 }
5015 return false;
5016 }
5017
5018 /* Write into BUF the maximum representable finite floating-point
5019 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5020 float string. LEN is the size of BUF, and the buffer must be large
5021 enough to contain the resulting string. */
5022
5023 void
5024 get_max_float (const struct real_format *fmt, char *buf, size_t len)
5025 {
5026 int i, n;
5027 char *p;
5028
5029 strcpy (buf, "0x0.");
5030 n = fmt->p;
5031 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
5032 *p++ = 'f';
5033 if (i < n)
5034 *p++ = "08ce"[n - i];
5035 sprintf (p, "p%d", fmt->emax);
5036 if (fmt->pnan < fmt->p)
5037 {
5038 /* This is an IBM extended double format made up of two IEEE
5039 doubles. The value of the long double is the sum of the
5040 values of the two parts. The most significant part is
5041 required to be the value of the long double rounded to the
5042 nearest double. Rounding means we need a slightly smaller
5043 value for LDBL_MAX. */
5044 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
5045 }
5046
5047 gcc_assert (strlen (buf) < len);
5048 }
5049
5050 /* True if mode M has a NaN representation and
5051 the treatment of NaN operands is important. */
5052
5053 bool
5054 HONOR_NANS (machine_mode m)
5055 {
5056 return MODE_HAS_NANS (m) && !flag_finite_math_only;
5057 }
5058
5059 bool
5060 HONOR_NANS (const_tree t)
5061 {
5062 return HONOR_NANS (element_mode (t));
5063 }
5064
5065 bool
5066 HONOR_NANS (const_rtx x)
5067 {
5068 return HONOR_NANS (GET_MODE (x));
5069 }
5070
5071 /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5072
5073 bool
5074 HONOR_SNANS (machine_mode m)
5075 {
5076 return flag_signaling_nans && HONOR_NANS (m);
5077 }
5078
5079 bool
5080 HONOR_SNANS (const_tree t)
5081 {
5082 return HONOR_SNANS (element_mode (t));
5083 }
5084
5085 bool
5086 HONOR_SNANS (const_rtx x)
5087 {
5088 return HONOR_SNANS (GET_MODE (x));
5089 }
5090
5091 /* As for HONOR_NANS, but true if the mode can represent infinity and
5092 the treatment of infinite values is important. */
5093
5094 bool
5095 HONOR_INFINITIES (machine_mode m)
5096 {
5097 return MODE_HAS_INFINITIES (m) && !flag_finite_math_only;
5098 }
5099
5100 bool
5101 HONOR_INFINITIES (const_tree t)
5102 {
5103 return HONOR_INFINITIES (element_mode (t));
5104 }
5105
5106 bool
5107 HONOR_INFINITIES (const_rtx x)
5108 {
5109 return HONOR_INFINITIES (GET_MODE (x));
5110 }
5111
5112 /* Like HONOR_NANS, but true if the given mode distinguishes between
5113 positive and negative zero, and the sign of zero is important. */
5114
5115 bool
5116 HONOR_SIGNED_ZEROS (machine_mode m)
5117 {
5118 return MODE_HAS_SIGNED_ZEROS (m) && flag_signed_zeros;
5119 }
5120
5121 bool
5122 HONOR_SIGNED_ZEROS (const_tree t)
5123 {
5124 return HONOR_SIGNED_ZEROS (element_mode (t));
5125 }
5126
5127 bool
5128 HONOR_SIGNED_ZEROS (const_rtx x)
5129 {
5130 return HONOR_SIGNED_ZEROS (GET_MODE (x));
5131 }
5132
5133 /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5134 and the rounding mode is important. */
5135
5136 bool
5137 HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m)
5138 {
5139 return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m) && flag_rounding_math;
5140 }
5141
5142 bool
5143 HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t)
5144 {
5145 return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t));
5146 }
5147
5148 bool
5149 HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x)
5150 {
5151 return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x));
5152 }