Debian Codegraph @

gcc/real.cc

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