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