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