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