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