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