1/* real.cc - software floating point emulation.
2 Copyright (C) 1993-2023 Free Software Foundation, Inc.
3 Contributed by Stephen L. Moshier (moshier@world.std.com).
4 Re-written by Richard Henderson <rth@redhat.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
26#include "rtl.h"
27#include "tree.h"
28#include "realmpfr.h"
29#include "dfp.h"
30
31/* The floating point model used internally is not exactly IEEE 754
32 compliant, and close to the description in the ISO C99 standard,
33 section 5.2.4.2.2 Characteristics of floating types.
34
35 Specifically
36
37 x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
38
39 where
40 s = sign (+- 1)
41 b = base or radix, here always 2
42 e = exponent
43 p = precision (the number of base-b digits in the significand)
44 f_k = the digits of the significand.
45
46 We differ from typical IEEE 754 encodings in that the entire
47 significand is fractional. Normalized significands are in the
48 range [0.5, 1.0).
49
50 A requirement of the model is that P be larger than the largest
51 supported target floating-point type by at least 2 bits. This gives
52 us proper rounding when we truncate to the target type. In addition,
53 E must be large enough to hold the smallest supported denormal number
54 in a normalized form.
55
56 Both of these requirements are easily satisfied. The largest target
57 significand is 113 bits; we store at least 160. The smallest
58 denormal number fits in 17 exponent bits; we store 26. */
59
60
61/* Used to classify two numbers simultaneously. */
62#define CLASS2(A, B) ((A) << 2 | (B))
63
64#if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
65 #error "Some constant folding done by hand to avoid shift count warnings"
66#endif
67
68static void get_zero (REAL_VALUE_TYPE *, int);
69static void get_canonical_qnan (REAL_VALUE_TYPE *, int);
70static void get_canonical_snan (REAL_VALUE_TYPE *, int);
71static void get_inf (REAL_VALUE_TYPE *, int);
72static bool sticky_rshift_significand (REAL_VALUE_TYPE *,
73 const REAL_VALUE_TYPE *, unsigned int);
74static void rshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
75 unsigned int);
76static void lshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
77 unsigned int);
78static void lshift_significand_1 (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
79static bool add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *,
80 const REAL_VALUE_TYPE *);
81static bool sub_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
82 const REAL_VALUE_TYPE *, int);
83static void neg_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
84static int cmp_significands (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
85static int cmp_significand_0 (const REAL_VALUE_TYPE *);
86static void set_significand_bit (REAL_VALUE_TYPE *, unsigned int);
87static void clear_significand_bit (REAL_VALUE_TYPE *, unsigned int);
88static bool test_significand_bit (REAL_VALUE_TYPE *, unsigned int);
89static void clear_significand_below (REAL_VALUE_TYPE *, unsigned int);
90static bool div_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
91 const REAL_VALUE_TYPE *);
92static void normalize (REAL_VALUE_TYPE *);
93
94static bool do_add (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
95 const REAL_VALUE_TYPE *, int);
96static bool do_multiply (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
97 const REAL_VALUE_TYPE *);
98static bool do_divide (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
99 const REAL_VALUE_TYPE *);
100static int do_compare (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int);
101static void do_fix_trunc (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
102
103static unsigned long rtd_divmod (REAL_VALUE_TYPE *, REAL_VALUE_TYPE *);
104static void decimal_from_integer (REAL_VALUE_TYPE *);
105static void decimal_integer_string (char *, const REAL_VALUE_TYPE *,
106 size_t);
107
108static const REAL_VALUE_TYPE * ten_to_ptwo (int);
109static const REAL_VALUE_TYPE * ten_to_mptwo (int);
110static const REAL_VALUE_TYPE * real_digit (int);
111static void times_pten (REAL_VALUE_TYPE *, int);
112
113static void round_for_format (const struct real_format *, REAL_VALUE_TYPE *);
114
115/* 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
119static inline bool
120real_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
127static inline void
128get_zero (REAL_VALUE_TYPE *r, int sign)
129{
130 memset (s: r, c: 0, n: sizeof (*r));
131 r->sign = sign;
132}
133
134/* Initialize R with the canonical quiet NaN. */
135
136static inline void
137get_canonical_qnan (REAL_VALUE_TYPE *r, int sign)
138{
139 memset (s: r, c: 0, n: sizeof (*r));
140 r->cl = rvc_nan;
141 r->sign = sign;
142 r->canonical = 1;
143}
144
145static inline void
146get_canonical_snan (REAL_VALUE_TYPE *r, int sign)
147{
148 memset (s: r, c: 0, n: sizeof (*r));
149 r->cl = rvc_nan;
150 r->sign = sign;
151 r->signalling = 1;
152 r->canonical = 1;
153}
154
155static inline void
156get_inf (REAL_VALUE_TYPE *r, int sign)
157{
158 memset (s: r, c: 0, n: 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
167static bool
168sticky_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
206static void
207rshift_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
235static void
236lshift_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
261static inline void
262lshift_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
274static inline bool
275add_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
304static inline bool
305sub_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
331static inline void
332neg_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
360static inline int
361cmp_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
381static inline int
382cmp_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
395static inline void
396set_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
404static inline void
405clear_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
413static inline bool
414test_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
425static void
426clear_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
442static inline bool
443div_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 (s: r->sig, c: 0, n: 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 (r: &u, a: &u);
459 start:
460 if (msb || cmp_significands (a: &u, b) >= 0)
461 {
462 sub_significands (r: &u, a: &u, b, carry: 0);
463 set_significand_bit (r, n: 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
479static void
480normalize (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, sign: r->sign);
514 else if (exp < -MAX_EXP)
515 get_zero (r, sign: r->sign);
516 else
517 {
518 SET_REAL_EXP (r, exp);
519 lshift_significand (r, a: r, n: 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
527static bool
528do_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: 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, sign: 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 (r: &t, a: b, n: dexp);
621 b = &t;
622 }
623
624 if (subtract_p)
625 {
626 if (sub_significands (r, a, b, carry: 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, a: 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, a: r, n: 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
676static bool
677do_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 (r: rr, sign: 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 (s: &u, c: 0, n: 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 (r: &u);
807 inexact |= do_add (r: rr, a: rr, b: &u, subtract_p: 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
820static bool
821do_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 (r: rr, sign: 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 (r: rr, a, b);
913
914 /* Re-normalize the result. */
915 normalize (r: 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
927static int
928do_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
994static void
995do_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, sign: 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
1031bool
1032real_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 (s: r, c: '\0', n: sizeof (*r));
1047 return do_add (r, a: op0, b: op1, subtract_p: 0);
1048
1049 case MINUS_EXPR:
1050 if (r != op0 && r != op1)
1051 memset (s: r, c: '\0', n: sizeof (*r));
1052 return do_add (r, a: op0, b: op1, subtract_p: 1);
1053
1054 case MULT_EXPR:
1055 if (r != op0 && r != op1)
1056 memset (s: r, c: '\0', n: sizeof (*r));
1057 return do_multiply (r, a: op0, b: op1);
1058
1059 case RDIV_EXPR:
1060 if (r != op0 && r != op1)
1061 memset (s: r, c: '\0', n: sizeof (*r));
1062 return do_divide (r, a: op0, b: 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 (a: op0, b: op1, nan_result: -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 (a: op0, b: op1, nan_result: 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, a: op0);
1106 break;
1107
1108 default:
1109 gcc_unreachable ();
1110 }
1111 return false;
1112}
1113
1114REAL_VALUE_TYPE
1115real_value_negate (const REAL_VALUE_TYPE *op0)
1116{
1117 REAL_VALUE_TYPE r;
1118 real_arithmetic (r: &r, icode: NEGATE_EXPR, op0, NULL);
1119 return r;
1120}
1121
1122REAL_VALUE_TYPE
1123real_value_abs (const REAL_VALUE_TYPE *op0)
1124{
1125 REAL_VALUE_TYPE r;
1126 real_arithmetic (r: &r, icode: ABS_EXPR, op0, NULL);
1127 return r;
1128}
1129
1130/* Return whether OP0 == OP1. */
1131
1132bool
1133real_equal (const REAL_VALUE_TYPE *op0, const REAL_VALUE_TYPE *op1)
1134{
1135 return do_compare (a: op0, b: op1, nan_result: -1) == 0;
1136}
1137
1138/* Return whether OP0 < OP1. */
1139
1140bool
1141real_less (const REAL_VALUE_TYPE *op0, const REAL_VALUE_TYPE *op1)
1142{
1143 return do_compare (a: op0, b: op1, nan_result: 1) < 0;
1144}
1145
1146bool
1147real_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 (a: op0, b: op1, nan_result: 1) <= 0;
1158 case GT_EXPR:
1159 return do_compare (a: op0, b: op1, nan_result: -1) > 0;
1160 case GE_EXPR:
1161 return do_compare (a: op0, b: op1, nan_result: -1) >= 0;
1162 case EQ_EXPR:
1163 return real_equal (op0, op1);
1164 case NE_EXPR:
1165 return do_compare (a: op0, b: op1, nan_result: -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 (a: op0, b: op1, nan_result: -1) < 0;
1172 case UNLE_EXPR:
1173 return do_compare (a: op0, b: op1, nan_result: -1) <= 0;
1174 case UNGT_EXPR:
1175 return do_compare (a: op0, b: op1, nan_result: 1) > 0;
1176 case UNGE_EXPR:
1177 return do_compare (a: op0, b: op1, nan_result: 1) >= 0;
1178 case UNEQ_EXPR:
1179 return do_compare (a: op0, b: op1, nan_result: 0) == 0;
1180 case LTGT_EXPR:
1181 return do_compare (a: op0, b: op1, nan_result: 0) != 0;
1182
1183 default:
1184 gcc_unreachable ();
1185 }
1186}
1187
1188/* Return floor log2(R). */
1189
1190int
1191real_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
1209void
1210real_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, sign: r->sign);
1228 else if (exp < -MAX_EXP)
1229 get_zero (r, sign: 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
1241bool
1242real_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
1249bool
1250real_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
1257bool
1258real_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. */
1264bool 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
1271bool
1272real_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
1279bool
1280real_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
1287bool
1288real_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
1295bool
1296real_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
1303bool
1304real_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
1311bool
1312real_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
1356bool
1357exact_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 (r: &u, a: one, b: 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
1394bool
1395real_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
1421HOST_WIDE_INT
1422real_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
1477wide_int
1478real_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 (bit: precision - 1, precision);
1498 else
1499 return ~wi::set_bit_in_zero (bit: 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, len: words, precision: w);
1548 result = wi::lrshift (x: result, y: w - exp);
1549 result = wide_int::from (x: result, precision, sgn: 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
1566static unsigned long
1567rtd_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 (r: num, a: num);
1582 start:
1583 if (msb || cmp_significands (a: num, b: den) >= 0)
1584 {
1585 sub_significands (r: num, a: num, b: den, carry: 0);
1586 q |= 1;
1587 }
1588 }
1589 while (--expn >= expd);
1590
1591 SET_REAL_EXP (num, expd);
1592 normalize (r: 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
1605void
1606real_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 (dest: str, src: (r.sign ? "-0.0" : "0.0"));
1630 return;
1631 case rvc_normal:
1632 break;
1633 case rvc_inf:
1634 strcpy (dest: str, src: (r.sign ? "-Inf" : "+Inf"));
1635 return;
1636 case rvc_nan:
1637 /* ??? Print the significand as well, if not canonical? */
1638 sprintf (s: str, format: "%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 (a: &r, b: one, nan_result: 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 (x: 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 (r: &t, a: &u, b: ten_to_ptwo (m));
1701 do_fix_trunc (r: &v, a: &t);
1702 if (cmp_significands (a: &v, b: &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 (x: (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 (a: &u, b: ptentwo, nan_result: 0) >= 0)
1725 {
1726 do_divide (r: &u, a: &u, b: ptentwo);
1727 do_multiply (r: &pten, a: &pten, b: 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 (r: &u, a: &v, b: 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 (x: (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 (a: &v, b: ptenmtwo, nan_result: 0) <= 0)
1774 {
1775 do_multiply (r: &v, a: &v, b: ptentwo);
1776 do_multiply (r: &pten, a: &pten, b: 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 (r: &pten, a: one, b: &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 (num: &r, den: &pten);
1800
1801 /* Be prepared for error in that division via underflow ... */
1802 if (digit == 0 && cmp_significand_0 (a: &r))
1803 {
1804 /* Multiply by 10 and try again. */
1805 do_multiply (r: &r, a: &r, b: ten);
1806 digit = rtd_divmod (num: &r, den: &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, a: &r, b: ten);
1829 digit = rtd_divmod (num: &r, den: &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, a: &r, b: ten);
1836 digit = rtd_divmod (num: &r, den: &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 (a: &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 (a: &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 (s: last, format: "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
1909void
1910real_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
1917DEBUG_FUNCTION void
1918debug (const REAL_VALUE_TYPE &r)
1919{
1920 char s[60];
1921 real_to_hexadecimal (s, &r, sizeof (s), 0, 1);
1922 fprintf (stderr, format: "%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
1930void
1931real_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 (dest: str, src: (r->sign ? "-Inf" : "+Inf"));
1948 return;
1949 case rvc_nan:
1950 /* ??? Print the significand as well, if not canonical? */
1951 sprintf (s: str, format: "%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 (dest: str, src: "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 (s: exp_buf, format: "p%+d", exp);
1971 max_digits = buf_size - strlen (s: 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 (s: p, format: "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
2005int
2006real_from_string (REAL_VALUE_TYPE *r, const char *str)
2007{
2008 int exp = 0;
2009 bool sign = false;
2010
2011 get_zero (r, sign: 0);
2012
2013 if (*str == '-')
2014 {
2015 sign = true;
2016 str++;
2017 }
2018 else if (*str == '+')
2019 str++;
2020
2021 if (startswith (str, prefix: "QNaN"))
2022 {
2023 get_canonical_qnan (r, sign);
2024 return 0;
2025 }
2026 else if (startswith (str, prefix: "SNaN"))
2027 {
2028 get_canonical_snan (r, sign);
2029 return 0;
2030 }
2031 else if (startswith (str, prefix: "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 (a: 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
2194REAL_VALUE_TYPE
2195real_from_string2 (const char *s, format_helper fmt)
2196{
2197 REAL_VALUE_TYPE r;
2198
2199 real_from_string (r: &r, str: 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
2208void
2209real_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, str: 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
2223void
2224real_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, sign: 0);
2229 else
2230 {
2231 unsigned int len = val_in.get_precision ();
2232 int i, j, e = 0;
2233 int maxbitlen = MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT;
2234 const unsigned int realmax = (SIGNIFICAND_BITS / HOST_BITS_PER_WIDE_INT
2235 * HOST_BITS_PER_WIDE_INT);
2236
2237 memset (s: r, c: 0, n: sizeof (*r));
2238 r->cl = rvc_normal;
2239 r->sign = wi::neg_p (x: val_in, sgn);
2240
2241 /* We have to ensure we can negate the largest negative number. */
2242 wide_int val = wide_int::from (x: val_in, precision: maxbitlen, sgn);
2243
2244 if (r->sign)
2245 val = -val;
2246
2247 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2248 won't work with precisions that are not a multiple of
2249 HOST_BITS_PER_WIDE_INT. */
2250 len += HOST_BITS_PER_WIDE_INT - 1;
2251
2252 /* Ensure we can represent the largest negative number. */
2253 len += 1;
2254
2255 len = len/HOST_BITS_PER_WIDE_INT * HOST_BITS_PER_WIDE_INT;
2256
2257 /* Cap the size to the size allowed by real.h. */
2258 if (len > realmax)
2259 {
2260 HOST_WIDE_INT cnt_l_z;
2261 cnt_l_z = wi::clz (val);
2262
2263 if (maxbitlen - cnt_l_z > realmax)
2264 {
2265 e = maxbitlen - cnt_l_z - realmax;
2266
2267 /* This value is too large, we must shift it right to
2268 preserve all the bits we can, and then bump the
2269 exponent up by that amount. */
2270 val = wi::lrshift (x: val, y: e);
2271 }
2272 len = realmax;
2273 }
2274
2275 /* Clear out top bits so elt will work with precisions that aren't
2276 a multiple of HOST_BITS_PER_WIDE_INT. */
2277 val = wide_int::from (x: val, precision: len, sgn);
2278 len = len / HOST_BITS_PER_WIDE_INT;
2279
2280 SET_REAL_EXP (r, len * HOST_BITS_PER_WIDE_INT + e);
2281
2282 j = SIGSZ - 1;
2283 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2284 for (i = len - 1; i >= 0; i--)
2285 {
2286 r->sig[j--] = val.elt (i);
2287 if (j < 0)
2288 break;
2289 }
2290 else
2291 {
2292 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2293 for (i = len - 1; i >= 0; i--)
2294 {
2295 HOST_WIDE_INT e = val.elt (i);
2296 r->sig[j--] = e >> (HOST_BITS_PER_LONG - 1) >> 1;
2297 if (j < 0)
2298 break;
2299 r->sig[j--] = e;
2300 if (j < 0)
2301 break;
2302 }
2303 }
2304
2305 normalize (r);
2306 }
2307
2308 if (fmt.decimal_p ())
2309 decimal_from_integer (r);
2310 if (fmt)
2311 real_convert (r, fmt, r);
2312}
2313
2314/* Render R, an integral value, as a floating point constant with no
2315 specified exponent. */
2316
2317static void
2318decimal_integer_string (char *str, const REAL_VALUE_TYPE *r_orig,
2319 size_t buf_size)
2320{
2321 int dec_exp, digit, digits;
2322 REAL_VALUE_TYPE r, pten;
2323 char *p;
2324 bool sign;
2325
2326 r = *r_orig;
2327
2328 if (r.cl == rvc_zero)
2329 {
2330 strcpy (dest: str, src: "0.");
2331 return;
2332 }
2333
2334 sign = r.sign;
2335 r.sign = 0;
2336
2337 dec_exp = REAL_EXP (&r) * M_LOG10_2;
2338 digits = dec_exp + 1;
2339 gcc_assert ((digits + 2) < (int)buf_size);
2340
2341 pten = *real_digit (1);
2342 times_pten (&pten, dec_exp);
2343
2344 p = str;
2345 if (sign)
2346 *p++ = '-';
2347
2348 digit = rtd_divmod (num: &r, den: &pten);
2349 gcc_assert (digit >= 0 && digit <= 9);
2350 *p++ = digit + '0';
2351 while (--digits > 0)
2352 {
2353 times_pten (&r, 1);
2354 digit = rtd_divmod (num: &r, den: &pten);
2355 *p++ = digit + '0';
2356 }
2357 *p++ = '.';
2358 *p++ = '\0';
2359}
2360
2361/* Convert a real with an integral value to decimal float. */
2362
2363static void
2364decimal_from_integer (REAL_VALUE_TYPE *r)
2365{
2366 char str[256];
2367
2368 decimal_integer_string (str, r_orig: r, buf_size: sizeof (str) - 1);
2369 decimal_real_from_string (r, str);
2370}
2371
2372/* Returns 10**2**N. */
2373
2374static const REAL_VALUE_TYPE *
2375ten_to_ptwo (int n)
2376{
2377 static REAL_VALUE_TYPE tens[EXP_BITS];
2378
2379 gcc_assert (n >= 0);
2380 gcc_assert (n < EXP_BITS);
2381
2382 if (tens[n].cl == rvc_zero)
2383 {
2384 if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
2385 {
2386 HOST_WIDE_INT t = 10;
2387 int i;
2388
2389 for (i = 0; i < n; ++i)
2390 t *= t;
2391
2392 real_from_integer (r: &tens[n], VOIDmode, val_in: t, sgn: UNSIGNED);
2393 }
2394 else
2395 {
2396 const REAL_VALUE_TYPE *t = ten_to_ptwo (n: n - 1);
2397 do_multiply (r: &tens[n], a: t, b: t);
2398 }
2399 }
2400
2401 return &tens[n];
2402}
2403
2404/* Returns 10**(-2**N). */
2405
2406static const REAL_VALUE_TYPE *
2407ten_to_mptwo (int n)
2408{
2409 static REAL_VALUE_TYPE tens[EXP_BITS];
2410
2411 gcc_assert (n >= 0);
2412 gcc_assert (n < EXP_BITS);
2413
2414 if (tens[n].cl == rvc_zero)
2415 do_divide (r: &tens[n], a: real_digit (1), b: ten_to_ptwo (n));
2416
2417 return &tens[n];
2418}
2419
2420/* Returns N. */
2421
2422static const REAL_VALUE_TYPE *
2423real_digit (int n)
2424{
2425 static REAL_VALUE_TYPE num[10];
2426
2427 gcc_assert (n >= 0);
2428 gcc_assert (n <= 9);
2429
2430 if (n > 0 && num[n].cl == rvc_zero)
2431 real_from_integer (r: &num[n], VOIDmode, val_in: n, sgn: UNSIGNED);
2432
2433 return &num[n];
2434}
2435
2436/* Multiply R by 10**EXP. */
2437
2438static void
2439times_pten (REAL_VALUE_TYPE *r, int exp)
2440{
2441 REAL_VALUE_TYPE pten, *rr;
2442 bool negative = (exp < 0);
2443 int i;
2444
2445 if (negative)
2446 {
2447 exp = -exp;
2448 pten = *real_digit (n: 1);
2449 rr = &pten;
2450 }
2451 else
2452 rr = r;
2453
2454 for (i = 0; exp > 0; ++i, exp >>= 1)
2455 if (exp & 1)
2456 do_multiply (r: rr, a: rr, b: ten_to_ptwo (n: i));
2457
2458 if (negative)
2459 do_divide (r, a: r, b: &pten);
2460}
2461
2462/* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
2463
2464const REAL_VALUE_TYPE *
2465dconst_e_ptr (void)
2466{
2467 static REAL_VALUE_TYPE value;
2468
2469 /* Initialize mathematical constants for constant folding builtins.
2470 These constants need to be given to at least 160 bits precision. */
2471 if (value.cl == rvc_zero)
2472 {
2473 auto_mpfr m (SIGNIFICAND_BITS);
2474 mpfr_set_ui (m, 1, MPFR_RNDN);
2475 mpfr_exp (m, m, MPFR_RNDN);
2476 real_from_mpfr (&value, m, NULL_TREE, MPFR_RNDN);
2477
2478 }
2479 return &value;
2480}
2481
2482/* Returns the special REAL_VALUE_TYPE corresponding to 'pi'. */
2483
2484const REAL_VALUE_TYPE *
2485dconst_pi_ptr (void)
2486{
2487 static REAL_VALUE_TYPE value;
2488
2489 /* Initialize mathematical constants for constant folding builtins.
2490 These constants need to be given to at least 160 bits precision. */
2491 if (value.cl == rvc_zero)
2492 {
2493 auto_mpfr m (SIGNIFICAND_BITS);
2494 mpfr_set_si (m, -1, MPFR_RNDN);
2495 mpfr_acos (m, m, MPFR_RNDN);
2496 real_from_mpfr (&value, m, NULL_TREE, MPFR_RNDN);
2497
2498 }
2499 return &value;
2500}
2501
2502/* Returns a cached REAL_VALUE_TYPE corresponding to 1/n, for various n. */
2503
2504#define CACHED_FRACTION(NAME, N) \
2505 const REAL_VALUE_TYPE * \
2506 NAME (void) \
2507 { \
2508 static REAL_VALUE_TYPE value; \
2509 \
2510 /* Initialize mathematical constants for constant folding builtins. \
2511 These constants need to be given to at least 160 bits \
2512 precision. */ \
2513 if (value.cl == rvc_zero) \
2514 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (N)); \
2515 return &value; \
2516 }
2517
2518CACHED_FRACTION (dconst_third_ptr, 3)
2519CACHED_FRACTION (dconst_quarter_ptr, 4)
2520CACHED_FRACTION (dconst_sixth_ptr, 6)
2521CACHED_FRACTION (dconst_ninth_ptr, 9)
2522
2523/* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2524
2525const REAL_VALUE_TYPE *
2526dconst_sqrt2_ptr (void)
2527{
2528 static REAL_VALUE_TYPE value;
2529
2530 /* Initialize mathematical constants for constant folding builtins.
2531 These constants need to be given to at least 160 bits precision. */
2532 if (value.cl == rvc_zero)
2533 {
2534 auto_mpfr m (SIGNIFICAND_BITS);
2535 mpfr_sqrt_ui (m, 2, MPFR_RNDN);
2536 real_from_mpfr (&value, m, NULL_TREE, MPFR_RNDN);
2537 }
2538 return &value;
2539}
2540
2541/* Fills R with Inf with SIGN. */
2542
2543void
2544real_inf (REAL_VALUE_TYPE *r, bool sign)
2545{
2546 get_inf (r, sign);
2547}
2548
2549/* Fills R with a NaN whose significand is described by STR. If QUIET,
2550 we force a QNaN, else we force an SNaN. The string, if not empty,
2551 is parsed as a number and placed in the significand. Return true
2552 if the string was successfully parsed. */
2553
2554bool
2555real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2556 format_helper fmt)
2557{
2558 if (*str == 0)
2559 {
2560 if (quiet)
2561 get_canonical_qnan (r, sign: 0);
2562 else
2563 get_canonical_snan (r, sign: 0);
2564 }
2565 else
2566 {
2567 int base = 10, d;
2568
2569 memset (s: r, c: 0, n: sizeof (*r));
2570 r->cl = rvc_nan;
2571
2572 /* Parse akin to strtol into the significand of R. */
2573
2574 while (ISSPACE (*str))
2575 str++;
2576 if (*str == '-')
2577 str++;
2578 else if (*str == '+')
2579 str++;
2580 if (*str == '0')
2581 {
2582 str++;
2583 if (*str == 'x' || *str == 'X')
2584 {
2585 base = 16;
2586 str++;
2587 }
2588 else
2589 base = 8;
2590 }
2591
2592 while ((d = hex_value (*str)) < base)
2593 {
2594 REAL_VALUE_TYPE u;
2595
2596 switch (base)
2597 {
2598 case 8:
2599 lshift_significand (r, a: r, n: 3);
2600 break;
2601 case 16:
2602 lshift_significand (r, a: r, n: 4);
2603 break;
2604 case 10:
2605 lshift_significand_1 (r: &u, a: r);
2606 lshift_significand (r, a: r, n: 3);
2607 add_significands (r, a: r, b: &u);
2608 break;
2609 default:
2610 gcc_unreachable ();
2611 }
2612
2613 get_zero (r: &u, sign: 0);
2614 u.sig[0] = d;
2615 add_significands (r, a: r, b: &u);
2616
2617 str++;
2618 }
2619
2620 /* Must have consumed the entire string for success. */
2621 if (*str != 0)
2622 return false;
2623
2624 /* Shift the significand into place such that the bits
2625 are in the most significant bits for the format. */
2626 lshift_significand (r, a: r, SIGNIFICAND_BITS - fmt->pnan);
2627
2628 /* Our MSB is always unset for NaNs. */
2629 r->sig[SIGSZ-1] &= ~SIG_MSB;
2630
2631 /* Force quiet or signaling NaN. */
2632 r->signalling = !quiet;
2633 }
2634
2635 return true;
2636}
2637
2638/* Fills R with the largest finite value representable in mode MODE.
2639 If SIGN is nonzero, R is set to the most negative finite value. */
2640
2641void
2642real_maxval (REAL_VALUE_TYPE *r, int sign, machine_mode mode)
2643{
2644 const struct real_format *fmt;
2645 int np2;
2646
2647 fmt = REAL_MODE_FORMAT (mode);
2648 gcc_assert (fmt);
2649 memset (s: r, c: 0, n: sizeof (*r));
2650
2651 if (fmt->b == 10)
2652 decimal_real_maxval (r, sign, mode);
2653 else
2654 {
2655 r->cl = rvc_normal;
2656 r->sign = sign;
2657 SET_REAL_EXP (r, fmt->emax);
2658
2659 np2 = SIGNIFICAND_BITS - fmt->p;
2660 memset (s: r->sig, c: -1, SIGSZ * sizeof (unsigned long));
2661 clear_significand_below (r, n: np2);
2662
2663 if (fmt->pnan < fmt->p)
2664 /* This is an IBM extended double format made up of two IEEE
2665 doubles. The value of the long double is the sum of the
2666 values of the two parts. The most significant part is
2667 required to be the value of the long double rounded to the
2668 nearest double. Rounding means we need a slightly smaller
2669 value for LDBL_MAX. */
2670 clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1);
2671 }
2672}
2673
2674/* Fills R with 2**N. */
2675
2676void
2677real_2expN (REAL_VALUE_TYPE *r, int n, format_helper fmt)
2678{
2679 memset (s: r, c: 0, n: sizeof (*r));
2680
2681 n++;
2682 if (n > MAX_EXP)
2683 r->cl = rvc_inf;
2684 else if (n < -MAX_EXP)
2685 ;
2686 else
2687 {
2688 r->cl = rvc_normal;
2689 SET_REAL_EXP (r, n);
2690 r->sig[SIGSZ-1] = SIG_MSB;
2691 }
2692 if (fmt.decimal_p ())
2693 decimal_real_convert (r, fmt, r);
2694}
2695
2696
2697static void
2698round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2699{
2700 int p2, np2, i, w;
2701 int emin2m1, emax2;
2702 bool round_up = false;
2703
2704 if (r->decimal)
2705 {
2706 if (fmt->b == 10)
2707 {
2708 decimal_round_for_format (fmt, r);
2709 return;
2710 }
2711 /* FIXME. We can come here via fp_easy_constant
2712 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2713 investigated whether this convert needs to be here, or
2714 something else is missing. */
2715 decimal_real_convert (r, REAL_MODE_FORMAT (DFmode), r);
2716 }
2717
2718 p2 = fmt->p;
2719 emin2m1 = fmt->emin - 1;
2720 emax2 = fmt->emax;
2721
2722 np2 = SIGNIFICAND_BITS - p2;
2723 switch (r->cl)
2724 {
2725 underflow:
2726 get_zero (r, sign: r->sign);
2727 /* FALLTHRU */
2728 case rvc_zero:
2729 if (!fmt->has_signed_zero)
2730 r->sign = 0;
2731 return;
2732
2733 overflow:
2734 get_inf (r, sign: r->sign);
2735 case rvc_inf:
2736 return;
2737
2738 case rvc_nan:
2739 clear_significand_below (r, n: np2);
2740 return;
2741
2742 case rvc_normal:
2743 break;
2744
2745 default:
2746 gcc_unreachable ();
2747 }
2748
2749 /* Check the range of the exponent. If we're out of range,
2750 either underflow or overflow. */
2751 if (REAL_EXP (r) > emax2)
2752 goto overflow;
2753 else if (REAL_EXP (r) <= emin2m1)
2754 {
2755 int diff;
2756
2757 if (!fmt->has_denorm)
2758 {
2759 /* Don't underflow completely until we've had a chance to round. */
2760 if (REAL_EXP (r) < emin2m1)
2761 goto underflow;
2762 }
2763 else
2764 {
2765 diff = emin2m1 - REAL_EXP (r) + 1;
2766 if (diff > p2)
2767 goto underflow;
2768
2769 /* De-normalize the significand. */
2770 r->sig[0] |= sticky_rshift_significand (r, a: r, n: diff);
2771 SET_REAL_EXP (r, REAL_EXP (r) + diff);
2772 }
2773 }
2774
2775 if (!fmt->round_towards_zero)
2776 {
2777 /* There are P2 true significand bits, followed by one guard bit,
2778 followed by one sticky bit, followed by stuff. Fold nonzero
2779 stuff into the sticky bit. */
2780 unsigned long sticky;
2781 bool guard, lsb;
2782
2783 sticky = 0;
2784 for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
2785 sticky |= r->sig[i];
2786 sticky |= r->sig[w]
2787 & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
2788
2789 guard = test_significand_bit (r, n: np2 - 1);
2790 lsb = test_significand_bit (r, n: np2);
2791
2792 /* Round to even. */
2793 round_up = guard && (sticky || lsb);
2794 }
2795
2796 if (round_up)
2797 {
2798 REAL_VALUE_TYPE u;
2799 get_zero (r: &u, sign: 0);
2800 set_significand_bit (r: &u, n: np2);
2801
2802 if (add_significands (r, a: r, b: &u))
2803 {
2804 /* Overflow. Means the significand had been all ones, and
2805 is now all zeros. Need to increase the exponent, and
2806 possibly re-normalize it. */
2807 SET_REAL_EXP (r, REAL_EXP (r) + 1);
2808 if (REAL_EXP (r) > emax2)
2809 goto overflow;
2810 r->sig[SIGSZ-1] = SIG_MSB;
2811 }
2812 }
2813
2814 /* Catch underflow that we deferred until after rounding. */
2815 if (REAL_EXP (r) <= emin2m1)
2816 goto underflow;
2817
2818 /* Clear out trailing garbage. */
2819 clear_significand_below (r, n: np2);
2820}
2821
2822/* Extend or truncate to a new format. */
2823
2824void
2825real_convert (REAL_VALUE_TYPE *r, format_helper fmt,
2826 const REAL_VALUE_TYPE *a)
2827{
2828 *r = *a;
2829
2830 if (a->decimal || fmt->b == 10)
2831 decimal_real_convert (r, fmt, a);
2832
2833 round_for_format (fmt, r);
2834
2835 /* Make resulting NaN value to be qNaN. The caller has the
2836 responsibility to avoid the operation if flag_signaling_nans
2837 is on. */
2838 if (r->cl == rvc_nan)
2839 r->signalling = 0;
2840
2841 /* round_for_format de-normalizes denormals. Undo just that part. */
2842 if (r->cl == rvc_normal)
2843 normalize (r);
2844}
2845
2846/* Legacy. Likewise, except return the struct directly. */
2847
2848REAL_VALUE_TYPE
2849real_value_truncate (format_helper fmt, REAL_VALUE_TYPE a)
2850{
2851 REAL_VALUE_TYPE r;
2852 real_convert (r: &r, fmt, a: &a);
2853 return r;
2854}
2855
2856/* Return true if truncating to FMT is exact. */
2857
2858bool
2859exact_real_truncate (format_helper fmt, const REAL_VALUE_TYPE *a)
2860{
2861 REAL_VALUE_TYPE t;
2862 int emin2m1;
2863
2864 /* Don't allow conversion to denormals. */
2865 emin2m1 = fmt->emin - 1;
2866 if (REAL_EXP (a) <= emin2m1)
2867 return false;
2868
2869 /* After conversion to the new format, the value must be identical. */
2870 real_convert (r: &t, fmt, a);
2871 return real_identical (a: &t, b: a);
2872}
2873
2874/* Write R to the given target format. Place the words of the result
2875 in target word order in BUF. There are always 32 bits in each
2876 long, no matter the size of the host long.
2877
2878 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2879
2880long
2881real_to_target (long *buf, const REAL_VALUE_TYPE *r_orig,
2882 format_helper fmt)
2883{
2884 REAL_VALUE_TYPE r;
2885 long buf1;
2886
2887 r = *r_orig;
2888 round_for_format (fmt, r: &r);
2889
2890 if (!buf)
2891 buf = &buf1;
2892 (*fmt->encode) (fmt, buf, &r);
2893
2894 return *buf;
2895}
2896
2897/* Read R from the given target format. Read the words of the result
2898 in target word order in BUF. There are always 32 bits in each
2899 long, no matter the size of the host long. */
2900
2901void
2902real_from_target (REAL_VALUE_TYPE *r, const long *buf, format_helper fmt)
2903{
2904 (*fmt->decode) (fmt, r, buf);
2905}
2906
2907/* Return the number of bits of the largest binary value that the
2908 significand of FMT will hold. */
2909/* ??? Legacy. Should get access to real_format directly. */
2910
2911int
2912significand_size (format_helper fmt)
2913{
2914 if (fmt == NULL)
2915 return 0;
2916
2917 if (fmt->b == 10)
2918 {
2919 /* Return the size in bits of the largest binary value that can be
2920 held by the decimal coefficient for this format. This is one more
2921 than the number of bits required to hold the largest coefficient
2922 of this format. */
2923 double log2_10 = 3.3219281;
2924 return fmt->p * log2_10;
2925 }
2926 return fmt->p;
2927}
2928
2929/* Return a hash value for the given real value. */
2930/* ??? The "unsigned int" return value is intended to be hashval_t,
2931 but I didn't want to pull hashtab.h into real.h. */
2932
2933unsigned int
2934real_hash (const REAL_VALUE_TYPE *r)
2935{
2936 unsigned int h;
2937 size_t i;
2938
2939 h = r->cl | (r->sign << 2);
2940 switch (r->cl)
2941 {
2942 case rvc_zero:
2943 case rvc_inf:
2944 return h;
2945
2946 case rvc_normal:
2947 h |= (unsigned int)REAL_EXP (r) << 3;
2948 break;
2949
2950 case rvc_nan:
2951 if (r->signalling)
2952 h ^= (unsigned int)-1;
2953 if (r->canonical)
2954 return h;
2955 break;
2956
2957 default:
2958 gcc_unreachable ();
2959 }
2960
2961 if (sizeof (unsigned long) > sizeof (unsigned int))
2962 for (i = 0; i < SIGSZ; ++i)
2963 {
2964 unsigned long s = r->sig[i];
2965 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2966 }
2967 else
2968 for (i = 0; i < SIGSZ; ++i)
2969 h ^= r->sig[i];
2970
2971 return h;
2972}
2973
2974/* IEEE single-precision format. */
2975
2976static void encode_ieee_single (const struct real_format *fmt,
2977 long *, const REAL_VALUE_TYPE *);
2978static void decode_ieee_single (const struct real_format *,
2979 REAL_VALUE_TYPE *, const long *);
2980
2981static void
2982encode_ieee_single (const struct real_format *fmt, long *buf,
2983 const REAL_VALUE_TYPE *r)
2984{
2985 unsigned long image, sig, exp;
2986 unsigned long sign = r->sign;
2987
2988 image = sign << 31;
2989 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
2990
2991 switch (r->cl)
2992 {
2993 case rvc_zero:
2994 break;
2995
2996 case rvc_inf:
2997 if (fmt->has_inf)
2998 image |= 255 << 23;
2999 else
3000 image |= 0x7fffffff;
3001 break;
3002
3003 case rvc_nan:
3004 if (fmt->has_nans)
3005 {
3006 if (r->canonical)
3007 sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
3008 if (r->signalling == fmt->qnan_msb_set)
3009 sig &= ~(1 << 22);
3010 else
3011 sig |= 1 << 22;
3012 if (sig == 0)
3013 sig = 1 << 21;
3014
3015 image |= 255 << 23;
3016 image |= sig;
3017 }
3018 else
3019 image |= 0x7fffffff;
3020 break;
3021
3022 case rvc_normal:
3023 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3024 whereas the intermediate representation is 0.F x 2**exp.
3025 Which means we're off by one. */
3026 if (real_isdenormal (r))
3027 exp = 0;
3028 else
3029 exp = REAL_EXP (r) + 127 - 1;
3030 image |= exp << 23;
3031 image |= sig;
3032 break;
3033
3034 default:
3035 gcc_unreachable ();
3036 }
3037
3038 buf[0] = image;
3039}
3040
3041static void
3042decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3043 const long *buf)
3044{
3045 unsigned long image = buf[0] & 0xffffffff;
3046 bool sign = (image >> 31) & 1;
3047 int exp = (image >> 23) & 0xff;
3048
3049 memset (s: r, c: 0, n: sizeof (*r));
3050 image <<= HOST_BITS_PER_LONG - 24;
3051 image &= ~SIG_MSB;
3052
3053 if (exp == 0)
3054 {
3055 if (image && fmt->has_denorm)
3056 {
3057 r->cl = rvc_normal;
3058 r->sign = sign;
3059 SET_REAL_EXP (r, -126);
3060 r->sig[SIGSZ-1] = image << 1;
3061 normalize (r);
3062 }
3063 else if (fmt->has_signed_zero)
3064 r->sign = sign;
3065 }
3066 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
3067 {
3068 if (image)
3069 {
3070 r->cl = rvc_nan;
3071 r->sign = sign;
3072 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
3073 ^ fmt->qnan_msb_set);
3074 r->sig[SIGSZ-1] = image;
3075 }
3076 else
3077 {
3078 r->cl = rvc_inf;
3079 r->sign = sign;
3080 }
3081 }
3082 else
3083 {
3084 r->cl = rvc_normal;
3085 r->sign = sign;
3086 SET_REAL_EXP (r, exp - 127 + 1);
3087 r->sig[SIGSZ-1] = image | SIG_MSB;
3088 }
3089}
3090
3091const struct real_format ieee_single_format =
3092 {
3093 .encode: encode_ieee_single,
3094 .decode: decode_ieee_single,
3095 .b: 2,
3096 .p: 24,
3097 .pnan: 24,
3098 .emin: -125,
3099 .emax: 128,
3100 .signbit_ro: 31,
3101 .signbit_rw: 31,
3102 .ieee_bits: 32,
3103 .round_towards_zero: false,
3104 .has_sign_dependent_rounding: true,
3105 .has_nans: true,
3106 .has_inf: true,
3107 .has_denorm: true,
3108 .has_signed_zero: true,
3109 .qnan_msb_set: true,
3110 .canonical_nan_lsbs_set: false,
3111 .name: "ieee_single"
3112 };
3113
3114const struct real_format mips_single_format =
3115 {
3116 .encode: encode_ieee_single,
3117 .decode: decode_ieee_single,
3118 .b: 2,
3119 .p: 24,
3120 .pnan: 24,
3121 .emin: -125,
3122 .emax: 128,
3123 .signbit_ro: 31,
3124 .signbit_rw: 31,
3125 .ieee_bits: 32,
3126 .round_towards_zero: false,
3127 .has_sign_dependent_rounding: true,
3128 .has_nans: true,
3129 .has_inf: true,
3130 .has_denorm: true,
3131 .has_signed_zero: true,
3132 .qnan_msb_set: false,
3133 .canonical_nan_lsbs_set: true,
3134 .name: "mips_single"
3135 };
3136
3137const struct real_format motorola_single_format =
3138 {
3139 .encode: encode_ieee_single,
3140 .decode: decode_ieee_single,
3141 .b: 2,
3142 .p: 24,
3143 .pnan: 24,
3144 .emin: -125,
3145 .emax: 128,
3146 .signbit_ro: 31,
3147 .signbit_rw: 31,
3148 .ieee_bits: 32,
3149 .round_towards_zero: false,
3150 .has_sign_dependent_rounding: true,
3151 .has_nans: true,
3152 .has_inf: true,
3153 .has_denorm: true,
3154 .has_signed_zero: true,
3155 .qnan_msb_set: true,
3156 .canonical_nan_lsbs_set: true,
3157 .name: "motorola_single"
3158 };
3159
3160/* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3161 single precision with the following differences:
3162 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3163 are generated.
3164 - NaNs are not supported.
3165 - The range of non-zero numbers in binary is
3166 (001)[1.]000...000 to (255)[1.]111...111.
3167 - Denormals can be represented, but are treated as +0.0 when
3168 used as an operand and are never generated as a result.
3169 - -0.0 can be represented, but a zero result is always +0.0.
3170 - the only supported rounding mode is trunction (towards zero). */
3171const struct real_format spu_single_format =
3172 {
3173 .encode: encode_ieee_single,
3174 .decode: decode_ieee_single,
3175 .b: 2,
3176 .p: 24,
3177 .pnan: 24,
3178 .emin: -125,
3179 .emax: 129,
3180 .signbit_ro: 31,
3181 .signbit_rw: 31,
3182 .ieee_bits: 0,
3183 .round_towards_zero: true,
3184 .has_sign_dependent_rounding: false,
3185 .has_nans: false,
3186 .has_inf: false,
3187 .has_denorm: true,
3188 .has_signed_zero: true,
3189 .qnan_msb_set: false,
3190 .canonical_nan_lsbs_set: false,
3191 .name: "spu_single"
3192 };
3193
3194/* IEEE double-precision format. */
3195
3196static void encode_ieee_double (const struct real_format *fmt,
3197 long *, const REAL_VALUE_TYPE *);
3198static void decode_ieee_double (const struct real_format *,
3199 REAL_VALUE_TYPE *, const long *);
3200
3201static void
3202encode_ieee_double (const struct real_format *fmt, long *buf,
3203 const REAL_VALUE_TYPE *r)
3204{
3205 unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
3206 unsigned long sign = r->sign;
3207
3208 image_hi = sign << 31;
3209 image_lo = 0;
3210
3211 if (HOST_BITS_PER_LONG == 64)
3212 {
3213 sig_hi = r->sig[SIGSZ-1];
3214 sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
3215 sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
3216 }
3217 else
3218 {
3219 sig_hi = r->sig[SIGSZ-1];
3220 sig_lo = r->sig[SIGSZ-2];
3221 sig_lo = (sig_hi << 21) | (sig_lo >> 11);
3222 sig_hi = (sig_hi >> 11) & 0xfffff;
3223 }
3224
3225 switch (r->cl)
3226 {
3227 case rvc_zero:
3228 break;
3229
3230 case rvc_inf:
3231 if (fmt->has_inf)
3232 image_hi |= 2047 << 20;
3233 else
3234 {
3235 image_hi |= 0x7fffffff;
3236 image_lo = 0xffffffff;
3237 }
3238 break;
3239
3240 case rvc_nan:
3241 if (fmt->has_nans)
3242 {
3243 if (r->canonical)
3244 {
3245 if (fmt->canonical_nan_lsbs_set)
3246 {
3247 sig_hi = (1 << 19) - 1;
3248 sig_lo = 0xffffffff;
3249 }
3250 else
3251 {
3252 sig_hi = 0;
3253 sig_lo = 0;
3254 }
3255 }
3256 if (r->signalling == fmt->qnan_msb_set)
3257 sig_hi &= ~(1 << 19);
3258 else
3259 sig_hi |= 1 << 19;
3260 if (sig_hi == 0 && sig_lo == 0)
3261 sig_hi = 1 << 18;
3262
3263 image_hi |= 2047 << 20;
3264 image_hi |= sig_hi;
3265 image_lo = sig_lo;
3266 }
3267 else
3268 {
3269 image_hi |= 0x7fffffff;
3270 image_lo = 0xffffffff;
3271 }
3272 break;
3273
3274 case rvc_normal:
3275 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3276 whereas the intermediate representation is 0.F x 2**exp.
3277 Which means we're off by one. */
3278 if (real_isdenormal (r))
3279 exp = 0;
3280 else
3281 exp = REAL_EXP (r) + 1023 - 1;
3282 image_hi |= exp << 20;
3283 image_hi |= sig_hi;
3284 image_lo = sig_lo;
3285 break;
3286
3287 default:
3288 gcc_unreachable ();
3289 }
3290
3291 if (FLOAT_WORDS_BIG_ENDIAN)
3292 buf[0] = image_hi, buf[1] = image_lo;
3293 else
3294 buf[0] = image_lo, buf[1] = image_hi;
3295}
3296
3297static void
3298decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3299 const long *buf)
3300{
3301 unsigned long image_hi, image_lo;
3302 bool sign;
3303 int exp;
3304
3305 if (FLOAT_WORDS_BIG_ENDIAN)
3306 image_hi = buf[0], image_lo = buf[1];
3307 else
3308 image_lo = buf[0], image_hi = buf[1];
3309 image_lo &= 0xffffffff;
3310 image_hi &= 0xffffffff;
3311
3312 sign = (image_hi >> 31) & 1;
3313 exp = (image_hi >> 20) & 0x7ff;
3314
3315 memset (s: r, c: 0, n: sizeof (*r));
3316
3317 image_hi <<= 32 - 21;
3318 image_hi |= image_lo >> 21;
3319 image_hi &= 0x7fffffff;
3320 image_lo <<= 32 - 21;
3321
3322 if (exp == 0)
3323 {
3324 if ((image_hi || image_lo) && fmt->has_denorm)
3325 {
3326 r->cl = rvc_normal;
3327 r->sign = sign;
3328 SET_REAL_EXP (r, -1022);
3329 if (HOST_BITS_PER_LONG == 32)
3330 {
3331 image_hi = (image_hi << 1) | (image_lo >> 31);
3332 image_lo <<= 1;
3333 r->sig[SIGSZ-1] = image_hi;
3334 r->sig[SIGSZ-2] = image_lo;
3335 }
3336 else
3337 {
3338 image_hi = (image_hi << 31 << 2) | (image_lo << 1);
3339 r->sig[SIGSZ-1] = image_hi;
3340 }
3341 normalize (r);
3342 }
3343 else if (fmt->has_signed_zero)
3344 r->sign = sign;
3345 }
3346 else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
3347 {
3348 if (image_hi || image_lo)
3349 {
3350 r->cl = rvc_nan;
3351 r->sign = sign;
3352 r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3353 if (HOST_BITS_PER_LONG == 32)
3354 {
3355 r->sig[SIGSZ-1] = image_hi;
3356 r->sig[SIGSZ-2] = image_lo;
3357 }
3358 else
3359 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
3360 }
3361 else
3362 {
3363 r->cl = rvc_inf;
3364 r->sign = sign;
3365 }
3366 }
3367 else
3368 {
3369 r->cl = rvc_normal;
3370 r->sign = sign;
3371 SET_REAL_EXP (r, exp - 1023 + 1);
3372 if (HOST_BITS_PER_LONG == 32)
3373 {
3374 r->sig[SIGSZ-1] = image_hi | SIG_MSB;
3375 r->sig[SIGSZ-2] = image_lo;
3376 }
3377 else
3378 r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
3379 }
3380}
3381
3382const struct real_format ieee_double_format =
3383 {
3384 .encode: encode_ieee_double,
3385 .decode: decode_ieee_double,
3386 .b: 2,
3387 .p: 53,
3388 .pnan: 53,
3389 .emin: -1021,
3390 .emax: 1024,
3391 .signbit_ro: 63,
3392 .signbit_rw: 63,
3393 .ieee_bits: 64,
3394 .round_towards_zero: false,
3395 .has_sign_dependent_rounding: true,
3396 .has_nans: true,
3397 .has_inf: true,
3398 .has_denorm: true,
3399 .has_signed_zero: true,
3400 .qnan_msb_set: true,
3401 .canonical_nan_lsbs_set: false,
3402 .name: "ieee_double"
3403 };
3404
3405const struct real_format mips_double_format =
3406 {
3407 .encode: encode_ieee_double,
3408 .decode: decode_ieee_double,
3409 .b: 2,
3410 .p: 53,
3411 .pnan: 53,
3412 .emin: -1021,
3413 .emax: 1024,
3414 .signbit_ro: 63,
3415 .signbit_rw: 63,
3416 .ieee_bits: 64,
3417 .round_towards_zero: false,
3418 .has_sign_dependent_rounding: true,
3419 .has_nans: true,
3420 .has_inf: true,
3421 .has_denorm: true,
3422 .has_signed_zero: true,
3423 .qnan_msb_set: false,
3424 .canonical_nan_lsbs_set: true,
3425 .name: "mips_double"
3426 };
3427
3428const struct real_format motorola_double_format =
3429 {
3430 .encode: encode_ieee_double,
3431 .decode: decode_ieee_double,
3432 .b: 2,
3433 .p: 53,
3434 .pnan: 53,
3435 .emin: -1021,
3436 .emax: 1024,
3437 .signbit_ro: 63,
3438 .signbit_rw: 63,
3439 .ieee_bits: 64,
3440 .round_towards_zero: false,
3441 .has_sign_dependent_rounding: true,
3442 .has_nans: true,
3443 .has_inf: true,
3444 .has_denorm: true,
3445 .has_signed_zero: true,
3446 .qnan_msb_set: true,
3447 .canonical_nan_lsbs_set: true,
3448 .name: "motorola_double"
3449 };
3450
3451/* IEEE extended real format. This comes in three flavors: Intel's as
3452 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3453 12- and 16-byte images may be big- or little endian; Motorola's is
3454 always big endian. */
3455
3456/* Helper subroutine which converts from the internal format to the
3457 12-byte little-endian Intel format. Functions below adjust this
3458 for the other possible formats. */
3459static void
3460encode_ieee_extended (const struct real_format *fmt, long *buf,
3461 const REAL_VALUE_TYPE *r)
3462{
3463 unsigned long image_hi, sig_hi, sig_lo;
3464
3465 image_hi = r->sign << 15;
3466 sig_hi = sig_lo = 0;
3467
3468 switch (r->cl)
3469 {
3470 case rvc_zero:
3471 break;
3472
3473 case rvc_inf:
3474 if (fmt->has_inf)
3475 {
3476 image_hi |= 32767;
3477
3478 /* Intel requires the explicit integer bit to be set, otherwise
3479 it considers the value a "pseudo-infinity". Motorola docs
3480 say it doesn't care. */
3481 sig_hi = 0x80000000;
3482 }
3483 else
3484 {
3485 image_hi |= 32767;
3486 sig_lo = sig_hi = 0xffffffff;
3487 }
3488 break;
3489
3490 case rvc_nan:
3491 if (fmt->has_nans)
3492 {
3493 image_hi |= 32767;
3494 if (r->canonical)
3495 {
3496 if (fmt->canonical_nan_lsbs_set)
3497 {
3498 sig_hi = (1 << 30) - 1;
3499 sig_lo = 0xffffffff;
3500 }
3501 }
3502 else if (HOST_BITS_PER_LONG == 32)
3503 {
3504 sig_hi = r->sig[SIGSZ-1];
3505 sig_lo = r->sig[SIGSZ-2];
3506 }
3507 else
3508 {
3509 sig_lo = r->sig[SIGSZ-1];
3510 sig_hi = sig_lo >> 31 >> 1;
3511 sig_lo &= 0xffffffff;
3512 }
3513 if (r->signalling == fmt->qnan_msb_set)
3514 sig_hi &= ~(1 << 30);
3515 else
3516 sig_hi |= 1 << 30;
3517 if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
3518 sig_hi = 1 << 29;
3519
3520 /* Intel requires the explicit integer bit to be set, otherwise
3521 it considers the value a "pseudo-nan". Motorola docs say it
3522 doesn't care. */
3523 sig_hi |= 0x80000000;
3524 }
3525 else
3526 {
3527 image_hi |= 32767;
3528 sig_lo = sig_hi = 0xffffffff;
3529 }
3530 break;
3531
3532 case rvc_normal:
3533 {
3534 int exp = REAL_EXP (r);
3535
3536 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
3537 whereas the intermediate representation is 0.F x 2**exp.
3538 Which means we're off by one.
3539
3540 Except for Motorola, which consider exp=0 and explicit
3541 integer bit set to continue to be normalized. In theory
3542 this discrepancy has been taken care of by the difference
3543 in fmt->emin in round_for_format. */
3544
3545 if (real_isdenormal (r))
3546 exp = 0;
3547 else
3548 {
3549 exp += 16383 - 1;
3550 gcc_assert (exp >= 0);
3551 }
3552 image_hi |= exp;
3553
3554 if (HOST_BITS_PER_LONG == 32)
3555 {
3556 sig_hi = r->sig[SIGSZ-1];
3557 sig_lo = r->sig[SIGSZ-2];
3558 }
3559 else
3560 {
3561 sig_lo = r->sig[SIGSZ-1];
3562 sig_hi = sig_lo >> 31 >> 1;
3563 sig_lo &= 0xffffffff;
3564 }
3565 }
3566 break;
3567
3568 default:
3569 gcc_unreachable ();
3570 }
3571
3572 buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
3573}
3574
3575/* Convert from the internal format to the 12-byte Motorola format
3576 for an IEEE extended real. */
3577static void
3578encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
3579 const REAL_VALUE_TYPE *r)
3580{
3581 long intermed[3];
3582 encode_ieee_extended (fmt, buf: intermed, r);
3583
3584 if (r->cl == rvc_inf)
3585 /* For infinity clear the explicit integer bit again, so that the
3586 format matches the canonical infinity generated by the FPU. */
3587 intermed[1] = 0;
3588
3589 /* Motorola chips are assumed always to be big-endian. Also, the
3590 padding in a Motorola extended real goes between the exponent and
3591 the mantissa. At this point the mantissa is entirely within
3592 elements 0 and 1 of intermed, and the exponent entirely within
3593 element 2, so all we have to do is swap the order around, and
3594 shift element 2 left 16 bits. */
3595 buf[0] = intermed[2] << 16;
3596 buf[1] = intermed[1];
3597 buf[2] = intermed[0];
3598}
3599
3600/* Convert from the internal format to the 12-byte Intel format for
3601 an IEEE extended real. */
3602static void
3603encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
3604 const REAL_VALUE_TYPE *r)
3605{
3606 if (FLOAT_WORDS_BIG_ENDIAN)
3607 {
3608 /* All the padding in an Intel-format extended real goes at the high
3609 end, which in this case is after the mantissa, not the exponent.
3610 Therefore we must shift everything down 16 bits. */
3611 long intermed[3];
3612 encode_ieee_extended (fmt, buf: intermed, r);
3613 buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
3614 buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
3615 buf[2] = (intermed[0] << 16);
3616 }
3617 else
3618 /* encode_ieee_extended produces what we want directly. */
3619 encode_ieee_extended (fmt, buf, r);
3620}
3621
3622/* Convert from the internal format to the 16-byte Intel format for
3623 an IEEE extended real. */
3624static void
3625encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
3626 const REAL_VALUE_TYPE *r)
3627{
3628 /* All the padding in an Intel-format extended real goes at the high end. */
3629 encode_ieee_extended_intel_96 (fmt, buf, r);
3630 buf[3] = 0;
3631}
3632
3633/* As above, we have a helper function which converts from 12-byte
3634 little-endian Intel format to internal format. Functions below
3635 adjust for the other possible formats. */
3636static void
3637decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3638 const long *buf)
3639{
3640 unsigned long image_hi, sig_hi, sig_lo;
3641 bool sign;
3642 int exp;
3643
3644 sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
3645 sig_lo &= 0xffffffff;
3646 sig_hi &= 0xffffffff;
3647 image_hi &= 0xffffffff;
3648
3649 sign = (image_hi >> 15) & 1;
3650 exp = image_hi & 0x7fff;
3651
3652 memset (s: r, c: 0, n: sizeof (*r));
3653
3654 if (exp == 0)
3655 {
3656 if ((sig_hi || sig_lo) && fmt->has_denorm)
3657 {
3658 r->cl = rvc_normal;
3659 r->sign = sign;
3660
3661 /* When the IEEE format contains a hidden bit, we know that
3662 it's zero at this point, and so shift up the significand
3663 and decrease the exponent to match. In this case, Motorola
3664 defines the explicit integer bit to be valid, so we don't
3665 know whether the msb is set or not. */
3666 SET_REAL_EXP (r, fmt->emin);
3667 if (HOST_BITS_PER_LONG == 32)
3668 {
3669 r->sig[SIGSZ-1] = sig_hi;
3670 r->sig[SIGSZ-2] = sig_lo;
3671 }
3672 else
3673 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3674
3675 normalize (r);
3676 }
3677 else if (fmt->has_signed_zero)
3678 r->sign = sign;
3679 }
3680 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
3681 {
3682 /* See above re "pseudo-infinities" and "pseudo-nans".
3683 Short summary is that the MSB will likely always be
3684 set, and that we don't care about it. */
3685 sig_hi &= 0x7fffffff;
3686
3687 if (sig_hi || sig_lo)
3688 {
3689 r->cl = rvc_nan;
3690 r->sign = sign;
3691 r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
3692 if (HOST_BITS_PER_LONG == 32)
3693 {
3694 r->sig[SIGSZ-1] = sig_hi;
3695 r->sig[SIGSZ-2] = sig_lo;
3696 }
3697 else
3698 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3699 }
3700 else
3701 {
3702 r->cl = rvc_inf;
3703 r->sign = sign;
3704 }
3705 }
3706 else
3707 {
3708 r->cl = rvc_normal;
3709 r->sign = sign;
3710 SET_REAL_EXP (r, exp - 16383 + 1);
3711 if (HOST_BITS_PER_LONG == 32)
3712 {
3713 r->sig[SIGSZ-1] = sig_hi;
3714 r->sig[SIGSZ-2] = sig_lo;
3715 }
3716 else
3717 r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
3718 }
3719}
3720
3721/* Convert from the internal format to the 12-byte Motorola format
3722 for an IEEE extended real. */
3723static void
3724decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3725 const long *buf)
3726{
3727 long intermed[3];
3728
3729 /* Motorola chips are assumed always to be big-endian. Also, the
3730 padding in a Motorola extended real goes between the exponent and
3731 the mantissa; remove it. */
3732 intermed[0] = buf[2];
3733 intermed[1] = buf[1];
3734 intermed[2] = (unsigned long)buf[0] >> 16;
3735
3736 decode_ieee_extended (fmt, r, buf: intermed);
3737}
3738
3739/* Convert from the internal format to the 12-byte Intel format for
3740 an IEEE extended real. */
3741static void
3742decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3743 const long *buf)
3744{
3745 if (FLOAT_WORDS_BIG_ENDIAN)
3746 {
3747 /* All the padding in an Intel-format extended real goes at the high
3748 end, which in this case is after the mantissa, not the exponent.
3749 Therefore we must shift everything up 16 bits. */
3750 long intermed[3];
3751
3752 intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
3753 intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
3754 intermed[2] = ((unsigned long)buf[0] >> 16);
3755
3756 decode_ieee_extended (fmt, r, buf: intermed);
3757 }
3758 else
3759 /* decode_ieee_extended produces what we want directly. */
3760 decode_ieee_extended (fmt, r, buf);
3761}
3762
3763/* Convert from the internal format to the 16-byte Intel format for
3764 an IEEE extended real. */
3765static void
3766decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
3767 const long *buf)
3768{
3769 /* All the padding in an Intel-format extended real goes at the high end. */
3770 decode_ieee_extended_intel_96 (fmt, r, buf);
3771}
3772
3773const struct real_format ieee_extended_motorola_format =
3774 {
3775 .encode: encode_ieee_extended_motorola,
3776 .decode: decode_ieee_extended_motorola,
3777 .b: 2,
3778 .p: 64,
3779 .pnan: 64,
3780 .emin: -16382,
3781 .emax: 16384,
3782 .signbit_ro: 95,
3783 .signbit_rw: 95,
3784 .ieee_bits: 0,
3785 .round_towards_zero: false,
3786 .has_sign_dependent_rounding: true,
3787 .has_nans: true,
3788 .has_inf: true,
3789 .has_denorm: true,
3790 .has_signed_zero: true,
3791 .qnan_msb_set: true,
3792 .canonical_nan_lsbs_set: true,
3793 .name: "ieee_extended_motorola"
3794 };
3795
3796const struct real_format ieee_extended_intel_96_format =
3797 {
3798 .encode: encode_ieee_extended_intel_96,
3799 .decode: decode_ieee_extended_intel_96,
3800 .b: 2,
3801 .p: 64,
3802 .pnan: 64,
3803 .emin: -16381,
3804 .emax: 16384,
3805 .signbit_ro: 79,
3806 .signbit_rw: 79,
3807 .ieee_bits: 65,
3808 .round_towards_zero: false,
3809 .has_sign_dependent_rounding: true,
3810 .has_nans: true,
3811 .has_inf: true,
3812 .has_denorm: true,
3813 .has_signed_zero: true,
3814 .qnan_msb_set: true,
3815 .canonical_nan_lsbs_set: false,
3816 .name: "ieee_extended_intel_96"
3817 };
3818
3819const struct real_format ieee_extended_intel_128_format =
3820 {
3821 .encode: encode_ieee_extended_intel_128,
3822 .decode: decode_ieee_extended_intel_128,
3823 .b: 2,
3824 .p: 64,
3825 .pnan: 64,
3826 .emin: -16381,
3827 .emax: 16384,
3828 .signbit_ro: 79,
3829 .signbit_rw: 79,
3830 .ieee_bits: 65,
3831 .round_towards_zero: false,
3832 .has_sign_dependent_rounding: true,
3833 .has_nans: true,
3834 .has_inf: true,
3835 .has_denorm: true,
3836 .has_signed_zero: true,
3837 .qnan_msb_set: true,
3838 .canonical_nan_lsbs_set: false,
3839 .name: "ieee_extended_intel_128"
3840 };
3841
3842/* The following caters to i386 systems that set the rounding precision
3843 to 53 bits instead of 64, e.g. FreeBSD. */
3844const struct real_format ieee_extended_intel_96_round_53_format =
3845 {
3846 .encode: encode_ieee_extended_intel_96,
3847 .decode: decode_ieee_extended_intel_96,
3848 .b: 2,
3849 .p: 53,
3850 .pnan: 53,
3851 .emin: -16381,
3852 .emax: 16384,
3853 .signbit_ro: 79,
3854 .signbit_rw: 79,
3855 .ieee_bits: 33,
3856 .round_towards_zero: false,
3857 .has_sign_dependent_rounding: true,
3858 .has_nans: true,
3859 .has_inf: true,
3860 .has_denorm: true,
3861 .has_signed_zero: true,
3862 .qnan_msb_set: true,
3863 .canonical_nan_lsbs_set: false,
3864 .name: "ieee_extended_intel_96_round_53"
3865 };
3866
3867/* IBM 128-bit extended precision format: a pair of IEEE double precision
3868 numbers whose sum is equal to the extended precision value. The number
3869 with greater magnitude is first. This format has the same magnitude
3870 range as an IEEE double precision value, but effectively 106 bits of
3871 significand precision. Infinity and NaN are represented by their IEEE
3872 double precision value stored in the first number, the second number is
3873 +0.0 or -0.0 for Infinity and don't-care for NaN. */
3874
3875static void encode_ibm_extended (const struct real_format *fmt,
3876 long *, const REAL_VALUE_TYPE *);
3877static void decode_ibm_extended (const struct real_format *,
3878 REAL_VALUE_TYPE *, const long *);
3879
3880static void
3881encode_ibm_extended (const struct real_format *fmt, long *buf,
3882 const REAL_VALUE_TYPE *r)
3883{
3884 REAL_VALUE_TYPE u, normr, v;
3885 const struct real_format *base_fmt;
3886
3887 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3888
3889 /* Renormalize R before doing any arithmetic on it. */
3890 normr = *r;
3891 if (normr.cl == rvc_normal)
3892 normalize (r: &normr);
3893
3894 /* u = IEEE double precision portion of significand. */
3895 u = normr;
3896 round_for_format (fmt: base_fmt, r: &u);
3897 encode_ieee_double (fmt: base_fmt, buf: &buf[0], r: &u);
3898
3899 if (u.cl == rvc_normal)
3900 {
3901 do_add (r: &v, a: &normr, b: &u, subtract_p: 1);
3902 /* Call round_for_format since we might need to denormalize. */
3903 round_for_format (fmt: base_fmt, r: &v);
3904 encode_ieee_double (fmt: base_fmt, buf: &buf[2], r: &v);
3905 }
3906 else
3907 {
3908 /* Inf, NaN, 0 are all representable as doubles, so the
3909 least-significant part can be 0.0. */
3910 buf[2] = 0;
3911 buf[3] = 0;
3912 }
3913}
3914
3915static void
3916decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
3917 const long *buf)
3918{
3919 REAL_VALUE_TYPE u, v;
3920 const struct real_format *base_fmt;
3921
3922 base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
3923 decode_ieee_double (fmt: base_fmt, r: &u, buf: &buf[0]);
3924
3925 if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
3926 {
3927 decode_ieee_double (fmt: base_fmt, r: &v, buf: &buf[2]);
3928 do_add (r, a: &u, b: &v, subtract_p: 0);
3929 }
3930 else
3931 *r = u;
3932}
3933
3934const struct real_format ibm_extended_format =
3935 {
3936 .encode: encode_ibm_extended,
3937 .decode: decode_ibm_extended,
3938 .b: 2,
3939 .p: 53 + 53,
3940 .pnan: 53,
3941 .emin: -1021 + 53,
3942 .emax: 1024,
3943 .signbit_ro: 127,
3944 .signbit_rw: -1,
3945 .ieee_bits: 0,
3946 .round_towards_zero: false,
3947 .has_sign_dependent_rounding: true,
3948 .has_nans: true,
3949 .has_inf: true,
3950 .has_denorm: true,
3951 .has_signed_zero: true,
3952 .qnan_msb_set: true,
3953 .canonical_nan_lsbs_set: false,
3954 .name: "ibm_extended"
3955 };
3956
3957const struct real_format mips_extended_format =
3958 {
3959 .encode: encode_ibm_extended,
3960 .decode: decode_ibm_extended,
3961 .b: 2,
3962 .p: 53 + 53,
3963 .pnan: 53,
3964 .emin: -1021 + 53,
3965 .emax: 1024,
3966 .signbit_ro: 127,
3967 .signbit_rw: -1,
3968 .ieee_bits: 0,
3969 .round_towards_zero: false,
3970 .has_sign_dependent_rounding: true,
3971 .has_nans: true,
3972 .has_inf: true,
3973 .has_denorm: true,
3974 .has_signed_zero: true,
3975 .qnan_msb_set: false,
3976 .canonical_nan_lsbs_set: true,
3977 .name: "mips_extended"
3978 };
3979
3980
3981/* IEEE quad precision format. */
3982
3983static void encode_ieee_quad (const struct real_format *fmt,
3984 long *, const REAL_VALUE_TYPE *);
3985static void decode_ieee_quad (const struct real_format *,
3986 REAL_VALUE_TYPE *, const long *);
3987
3988static void
3989encode_ieee_quad (const struct real_format *fmt, long *buf,
3990 const REAL_VALUE_TYPE *r)
3991{
3992 unsigned long image3, image2, image1, image0, exp;
3993 unsigned long sign = r->sign;
3994 REAL_VALUE_TYPE u;
3995
3996 image3 = sign << 31;
3997 image2 = 0;
3998 image1 = 0;
3999 image0 = 0;
4000
4001 rshift_significand (r: &u, a: r, SIGNIFICAND_BITS - 113);
4002
4003 switch (r->cl)
4004 {
4005 case rvc_zero:
4006 break;
4007
4008 case rvc_inf:
4009 if (fmt->has_inf)
4010 image3 |= 32767 << 16;
4011 else
4012 {
4013 image3 |= 0x7fffffff;
4014 image2 = 0xffffffff;
4015 image1 = 0xffffffff;
4016 image0 = 0xffffffff;
4017 }
4018 break;
4019
4020 case rvc_nan:
4021 if (fmt->has_nans)
4022 {
4023 image3 |= 32767 << 16;
4024
4025 if (r->canonical)
4026 {
4027 if (fmt->canonical_nan_lsbs_set)
4028 {
4029 image3 |= 0x7fff;
4030 image2 = image1 = image0 = 0xffffffff;
4031 }
4032 }
4033 else if (HOST_BITS_PER_LONG == 32)
4034 {
4035 image0 = u.sig[0];
4036 image1 = u.sig[1];
4037 image2 = u.sig[2];
4038 image3 |= u.sig[3] & 0xffff;
4039 }
4040 else
4041 {
4042 image0 = u.sig[0];
4043 image1 = image0 >> 31 >> 1;
4044 image2 = u.sig[1];
4045 image3 |= (image2 >> 31 >> 1) & 0xffff;
4046 image0 &= 0xffffffff;
4047 image2 &= 0xffffffff;
4048 }
4049 if (r->signalling == fmt->qnan_msb_set)
4050 image3 &= ~0x8000;
4051 else
4052 image3 |= 0x8000;
4053 if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
4054 image3 |= 0x4000;
4055 }
4056 else
4057 {
4058 image3 |= 0x7fffffff;
4059 image2 = 0xffffffff;
4060 image1 = 0xffffffff;
4061 image0 = 0xffffffff;
4062 }
4063 break;
4064
4065 case rvc_normal:
4066 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4067 whereas the intermediate representation is 0.F x 2**exp.
4068 Which means we're off by one. */
4069 if (real_isdenormal (r))
4070 exp = 0;
4071 else
4072 exp = REAL_EXP (r) + 16383 - 1;
4073 image3 |= exp << 16;
4074
4075 if (HOST_BITS_PER_LONG == 32)
4076 {
4077 image0 = u.sig[0];
4078 image1 = u.sig[1];
4079 image2 = u.sig[2];
4080 image3 |= u.sig[3] & 0xffff;
4081 }
4082 else
4083 {
4084 image0 = u.sig[0];
4085 image1 = image0 >> 31 >> 1;
4086 image2 = u.sig[1];
4087 image3 |= (image2 >> 31 >> 1) & 0xffff;
4088 image0 &= 0xffffffff;
4089 image2 &= 0xffffffff;
4090 }
4091 break;
4092
4093 default:
4094 gcc_unreachable ();
4095 }
4096
4097 if (FLOAT_WORDS_BIG_ENDIAN)
4098 {
4099 buf[0] = image3;
4100 buf[1] = image2;
4101 buf[2] = image1;
4102 buf[3] = image0;
4103 }
4104 else
4105 {
4106 buf[0] = image0;
4107 buf[1] = image1;
4108 buf[2] = image2;
4109 buf[3] = image3;
4110 }
4111}
4112
4113static void
4114decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4115 const long *buf)
4116{
4117 unsigned long image3, image2, image1, image0;
4118 bool sign;
4119 int exp;
4120
4121 if (FLOAT_WORDS_BIG_ENDIAN)
4122 {
4123 image3 = buf[0];
4124 image2 = buf[1];
4125 image1 = buf[2];
4126 image0 = buf[3];
4127 }
4128 else
4129 {
4130 image0 = buf[0];
4131 image1 = buf[1];
4132 image2 = buf[2];
4133 image3 = buf[3];
4134 }
4135 image0 &= 0xffffffff;
4136 image1 &= 0xffffffff;
4137 image2 &= 0xffffffff;
4138
4139 sign = (image3 >> 31) & 1;
4140 exp = (image3 >> 16) & 0x7fff;
4141 image3 &= 0xffff;
4142
4143 memset (s: r, c: 0, n: sizeof (*r));
4144
4145 if (exp == 0)
4146 {
4147 if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
4148 {
4149 r->cl = rvc_normal;
4150 r->sign = sign;
4151
4152 SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
4153 if (HOST_BITS_PER_LONG == 32)
4154 {
4155 r->sig[0] = image0;
4156 r->sig[1] = image1;
4157 r->sig[2] = image2;
4158 r->sig[3] = image3;
4159 }
4160 else
4161 {
4162 r->sig[0] = (image1 << 31 << 1) | image0;
4163 r->sig[1] = (image3 << 31 << 1) | image2;
4164 }
4165
4166 normalize (r);
4167 }
4168 else if (fmt->has_signed_zero)
4169 r->sign = sign;
4170 }
4171 else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
4172 {
4173 if (image3 | image2 | image1 | image0)
4174 {
4175 r->cl = rvc_nan;
4176 r->sign = sign;
4177 r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
4178
4179 if (HOST_BITS_PER_LONG == 32)
4180 {
4181 r->sig[0] = image0;
4182 r->sig[1] = image1;
4183 r->sig[2] = image2;
4184 r->sig[3] = image3;
4185 }
4186 else
4187 {
4188 r->sig[0] = (image1 << 31 << 1) | image0;
4189 r->sig[1] = (image3 << 31 << 1) | image2;
4190 }
4191 lshift_significand (r, a: r, SIGNIFICAND_BITS - 113);
4192 }
4193 else
4194 {
4195 r->cl = rvc_inf;
4196 r->sign = sign;
4197 }
4198 }
4199 else
4200 {
4201 r->cl = rvc_normal;
4202 r->sign = sign;
4203 SET_REAL_EXP (r, exp - 16383 + 1);
4204
4205 if (HOST_BITS_PER_LONG == 32)
4206 {
4207 r->sig[0] = image0;
4208 r->sig[1] = image1;
4209 r->sig[2] = image2;
4210 r->sig[3] = image3;
4211 }
4212 else
4213 {
4214 r->sig[0] = (image1 << 31 << 1) | image0;
4215 r->sig[1] = (image3 << 31 << 1) | image2;
4216 }
4217 lshift_significand (r, a: r, SIGNIFICAND_BITS - 113);
4218 r->sig[SIGSZ-1] |= SIG_MSB;
4219 }
4220}
4221
4222const struct real_format ieee_quad_format =
4223 {
4224 .encode: encode_ieee_quad,
4225 .decode: decode_ieee_quad,
4226 .b: 2,
4227 .p: 113,
4228 .pnan: 113,
4229 .emin: -16381,
4230 .emax: 16384,
4231 .signbit_ro: 127,
4232 .signbit_rw: 127,
4233 .ieee_bits: 128,
4234 .round_towards_zero: false,
4235 .has_sign_dependent_rounding: true,
4236 .has_nans: true,
4237 .has_inf: true,
4238 .has_denorm: true,
4239 .has_signed_zero: true,
4240 .qnan_msb_set: true,
4241 .canonical_nan_lsbs_set: false,
4242 .name: "ieee_quad"
4243 };
4244
4245const struct real_format mips_quad_format =
4246 {
4247 .encode: encode_ieee_quad,
4248 .decode: decode_ieee_quad,
4249 .b: 2,
4250 .p: 113,
4251 .pnan: 113,
4252 .emin: -16381,
4253 .emax: 16384,
4254 .signbit_ro: 127,
4255 .signbit_rw: 127,
4256 .ieee_bits: 128,
4257 .round_towards_zero: false,
4258 .has_sign_dependent_rounding: true,
4259 .has_nans: true,
4260 .has_inf: true,
4261 .has_denorm: true,
4262 .has_signed_zero: true,
4263 .qnan_msb_set: false,
4264 .canonical_nan_lsbs_set: true,
4265 .name: "mips_quad"
4266 };
4267
4268/* Descriptions of VAX floating point formats can be found beginning at
4269
4270 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4271
4272 The thing to remember is that they're almost IEEE, except for word
4273 order, exponent bias, and the lack of infinities, nans, and denormals.
4274
4275 We don't implement the H_floating format here, simply because neither
4276 the VAX or Alpha ports use it. */
4277
4278static void encode_vax_f (const struct real_format *fmt,
4279 long *, const REAL_VALUE_TYPE *);
4280static void decode_vax_f (const struct real_format *,
4281 REAL_VALUE_TYPE *, const long *);
4282static void encode_vax_d (const struct real_format *fmt,
4283 long *, const REAL_VALUE_TYPE *);
4284static void decode_vax_d (const struct real_format *,
4285 REAL_VALUE_TYPE *, const long *);
4286static void encode_vax_g (const struct real_format *fmt,
4287 long *, const REAL_VALUE_TYPE *);
4288static void decode_vax_g (const struct real_format *,
4289 REAL_VALUE_TYPE *, const long *);
4290
4291static void
4292encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4293 const REAL_VALUE_TYPE *r)
4294{
4295 unsigned long sign, exp, sig, image;
4296
4297 sign = r->sign << 15;
4298
4299 switch (r->cl)
4300 {
4301 case rvc_zero:
4302 image = 0;
4303 break;
4304
4305 case rvc_inf:
4306 case rvc_nan:
4307 image = 0xffff7fff | sign;
4308 break;
4309
4310 case rvc_normal:
4311 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
4312 exp = REAL_EXP (r) + 128;
4313
4314 image = (sig << 16) & 0xffff0000;
4315 image |= sign;
4316 image |= exp << 7;
4317 image |= sig >> 16;
4318 break;
4319
4320 default:
4321 gcc_unreachable ();
4322 }
4323
4324 buf[0] = image;
4325}
4326
4327static void
4328decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
4329 REAL_VALUE_TYPE *r, const long *buf)
4330{
4331 unsigned long image = buf[0] & 0xffffffff;
4332 int exp = (image >> 7) & 0xff;
4333
4334 memset (s: r, c: 0, n: sizeof (*r));
4335
4336 if (exp != 0)
4337 {
4338 r->cl = rvc_normal;
4339 r->sign = (image >> 15) & 1;
4340 SET_REAL_EXP (r, exp - 128);
4341
4342 image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
4343 r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
4344 }
4345}
4346
4347static void
4348encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4349 const REAL_VALUE_TYPE *r)
4350{
4351 unsigned long image0, image1, sign = r->sign << 15;
4352
4353 switch (r->cl)
4354 {
4355 case rvc_zero:
4356 image0 = image1 = 0;
4357 break;
4358
4359 case rvc_inf:
4360 case rvc_nan:
4361 image0 = 0xffff7fff | sign;
4362 image1 = 0xffffffff;
4363 break;
4364
4365 case rvc_normal:
4366 /* Extract the significand into straight hi:lo. */
4367 if (HOST_BITS_PER_LONG == 64)
4368 {
4369 image0 = r->sig[SIGSZ-1];
4370 image1 = (image0 >> (64 - 56)) & 0xffffffff;
4371 image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
4372 }
4373 else
4374 {
4375 image0 = r->sig[SIGSZ-1];
4376 image1 = r->sig[SIGSZ-2];
4377 image1 = (image0 << 24) | (image1 >> 8);
4378 image0 = (image0 >> 8) & 0xffffff;
4379 }
4380
4381 /* Rearrange the half-words of the significand to match the
4382 external format. */
4383 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
4384 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4385
4386 /* Add the sign and exponent. */
4387 image0 |= sign;
4388 image0 |= (REAL_EXP (r) + 128) << 7;
4389 break;
4390
4391 default:
4392 gcc_unreachable ();
4393 }
4394
4395 if (FLOAT_WORDS_BIG_ENDIAN)
4396 buf[0] = image1, buf[1] = image0;
4397 else
4398 buf[0] = image0, buf[1] = image1;
4399}
4400
4401static void
4402decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
4403 REAL_VALUE_TYPE *r, const long *buf)
4404{
4405 unsigned long image0, image1;
4406 int exp;
4407
4408 if (FLOAT_WORDS_BIG_ENDIAN)
4409 image1 = buf[0], image0 = buf[1];
4410 else
4411 image0 = buf[0], image1 = buf[1];
4412 image0 &= 0xffffffff;
4413 image1 &= 0xffffffff;
4414
4415 exp = (image0 >> 7) & 0xff;
4416
4417 memset (s: r, c: 0, n: sizeof (*r));
4418
4419 if (exp != 0)
4420 {
4421 r->cl = rvc_normal;
4422 r->sign = (image0 >> 15) & 1;
4423 SET_REAL_EXP (r, exp - 128);
4424
4425 /* Rearrange the half-words of the external format into
4426 proper ascending order. */
4427 image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
4428 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4429
4430 if (HOST_BITS_PER_LONG == 64)
4431 {
4432 image0 = (image0 << 31 << 1) | image1;
4433 image0 <<= 64 - 56;
4434 image0 |= SIG_MSB;
4435 r->sig[SIGSZ-1] = image0;
4436 }
4437 else
4438 {
4439 r->sig[SIGSZ-1] = image0;
4440 r->sig[SIGSZ-2] = image1;
4441 lshift_significand (r, a: r, n: 2*HOST_BITS_PER_LONG - 56);
4442 r->sig[SIGSZ-1] |= SIG_MSB;
4443 }
4444 }
4445}
4446
4447static void
4448encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
4449 const REAL_VALUE_TYPE *r)
4450{
4451 unsigned long image0, image1, sign = r->sign << 15;
4452
4453 switch (r->cl)
4454 {
4455 case rvc_zero:
4456 image0 = image1 = 0;
4457 break;
4458
4459 case rvc_inf:
4460 case rvc_nan:
4461 image0 = 0xffff7fff | sign;
4462 image1 = 0xffffffff;
4463 break;
4464
4465 case rvc_normal:
4466 /* Extract the significand into straight hi:lo. */
4467 if (HOST_BITS_PER_LONG == 64)
4468 {
4469 image0 = r->sig[SIGSZ-1];
4470 image1 = (image0 >> (64 - 53)) & 0xffffffff;
4471 image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
4472 }
4473 else
4474 {
4475 image0 = r->sig[SIGSZ-1];
4476 image1 = r->sig[SIGSZ-2];
4477 image1 = (image0 << 21) | (image1 >> 11);
4478 image0 = (image0 >> 11) & 0xfffff;
4479 }
4480
4481 /* Rearrange the half-words of the significand to match the
4482 external format. */
4483 image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
4484 image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
4485
4486 /* Add the sign and exponent. */
4487 image0 |= sign;
4488 image0 |= (REAL_EXP (r) + 1024) << 4;
4489 break;
4490
4491 default:
4492 gcc_unreachable ();
4493 }
4494
4495 if (FLOAT_WORDS_BIG_ENDIAN)
4496 buf[0] = image1, buf[1] = image0;
4497 else
4498 buf[0] = image0, buf[1] = image1;
4499}
4500
4501static void
4502decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
4503 REAL_VALUE_TYPE *r, const long *buf)
4504{
4505 unsigned long image0, image1;
4506 int exp;
4507
4508 if (FLOAT_WORDS_BIG_ENDIAN)
4509 image1 = buf[0], image0 = buf[1];
4510 else
4511 image0 = buf[0], image1 = buf[1];
4512 image0 &= 0xffffffff;
4513 image1 &= 0xffffffff;
4514
4515 exp = (image0 >> 4) & 0x7ff;
4516
4517 memset (s: r, c: 0, n: sizeof (*r));
4518
4519 if (exp != 0)
4520 {
4521 r->cl = rvc_normal;
4522 r->sign = (image0 >> 15) & 1;
4523 SET_REAL_EXP (r, exp - 1024);
4524
4525 /* Rearrange the half-words of the external format into
4526 proper ascending order. */
4527 image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
4528 image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
4529
4530 if (HOST_BITS_PER_LONG == 64)
4531 {
4532 image0 = (image0 << 31 << 1) | image1;
4533 image0 <<= 64 - 53;
4534 image0 |= SIG_MSB;
4535 r->sig[SIGSZ-1] = image0;
4536 }
4537 else
4538 {
4539 r->sig[SIGSZ-1] = image0;
4540 r->sig[SIGSZ-2] = image1;
4541 lshift_significand (r, a: r, n: 64 - 53);
4542 r->sig[SIGSZ-1] |= SIG_MSB;
4543 }
4544 }
4545}
4546
4547const struct real_format vax_f_format =
4548 {
4549 .encode: encode_vax_f,
4550 .decode: decode_vax_f,
4551 .b: 2,
4552 .p: 24,
4553 .pnan: 24,
4554 .emin: -127,
4555 .emax: 127,
4556 .signbit_ro: 15,
4557 .signbit_rw: 15,
4558 .ieee_bits: 0,
4559 .round_towards_zero: false,
4560 .has_sign_dependent_rounding: false,
4561 .has_nans: false,
4562 .has_inf: false,
4563 .has_denorm: false,
4564 .has_signed_zero: false,
4565 .qnan_msb_set: false,
4566 .canonical_nan_lsbs_set: false,
4567 .name: "vax_f"
4568 };
4569
4570const struct real_format vax_d_format =
4571 {
4572 .encode: encode_vax_d,
4573 .decode: decode_vax_d,
4574 .b: 2,
4575 .p: 56,
4576 .pnan: 56,
4577 .emin: -127,
4578 .emax: 127,
4579 .signbit_ro: 15,
4580 .signbit_rw: 15,
4581 .ieee_bits: 0,
4582 .round_towards_zero: false,
4583 .has_sign_dependent_rounding: false,
4584 .has_nans: false,
4585 .has_inf: false,
4586 .has_denorm: false,
4587 .has_signed_zero: false,
4588 .qnan_msb_set: false,
4589 .canonical_nan_lsbs_set: false,
4590 .name: "vax_d"
4591 };
4592
4593const struct real_format vax_g_format =
4594 {
4595 .encode: encode_vax_g,
4596 .decode: decode_vax_g,
4597 .b: 2,
4598 .p: 53,
4599 .pnan: 53,
4600 .emin: -1023,
4601 .emax: 1023,
4602 .signbit_ro: 15,
4603 .signbit_rw: 15,
4604 .ieee_bits: 0,
4605 .round_towards_zero: false,
4606 .has_sign_dependent_rounding: false,
4607 .has_nans: false,
4608 .has_inf: false,
4609 .has_denorm: false,
4610 .has_signed_zero: false,
4611 .qnan_msb_set: false,
4612 .canonical_nan_lsbs_set: false,
4613 .name: "vax_g"
4614 };
4615
4616/* Encode real R into a single precision DFP value in BUF. */
4617static void
4618encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4619 long *buf ATTRIBUTE_UNUSED,
4620 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4621{
4622 encode_decimal32 (fmt, buf, r);
4623}
4624
4625/* Decode a single precision DFP value in BUF into a real R. */
4626static void
4627decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4628 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4629 const long *buf ATTRIBUTE_UNUSED)
4630{
4631 decode_decimal32 (fmt, r, buf);
4632}
4633
4634/* Encode real R into a double precision DFP value in BUF. */
4635static void
4636encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4637 long *buf ATTRIBUTE_UNUSED,
4638 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4639{
4640 encode_decimal64 (fmt, buf, r);
4641}
4642
4643/* Decode a double precision DFP value in BUF into a real R. */
4644static void
4645decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
4646 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4647 const long *buf ATTRIBUTE_UNUSED)
4648{
4649 decode_decimal64 (fmt, r, buf);
4650}
4651
4652/* Encode real R into a quad precision DFP value in BUF. */
4653static void
4654encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4655 long *buf ATTRIBUTE_UNUSED,
4656 const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
4657{
4658 encode_decimal128 (fmt, buf, r);
4659}
4660
4661/* Decode a quad precision DFP value in BUF into a real R. */
4662static void
4663decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
4664 REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
4665 const long *buf ATTRIBUTE_UNUSED)
4666{
4667 decode_decimal128 (fmt, r, buf);
4668}
4669
4670/* Single precision decimal floating point (IEEE 754). */
4671const struct real_format decimal_single_format =
4672 {
4673 .encode: encode_decimal_single,
4674 .decode: decode_decimal_single,
4675 .b: 10,
4676 .p: 7,
4677 .pnan: 7,
4678 .emin: -94,
4679 .emax: 97,
4680 .signbit_ro: 31,
4681 .signbit_rw: 31,
4682 .ieee_bits: 32,
4683 .round_towards_zero: false,
4684 .has_sign_dependent_rounding: true,
4685 .has_nans: true,
4686 .has_inf: true,
4687 .has_denorm: true,
4688 .has_signed_zero: true,
4689 .qnan_msb_set: true,
4690 .canonical_nan_lsbs_set: false,
4691 .name: "decimal_single"
4692 };
4693
4694/* Double precision decimal floating point (IEEE 754). */
4695const struct real_format decimal_double_format =
4696 {
4697 .encode: encode_decimal_double,
4698 .decode: decode_decimal_double,
4699 .b: 10,
4700 .p: 16,
4701 .pnan: 16,
4702 .emin: -382,
4703 .emax: 385,
4704 .signbit_ro: 63,
4705 .signbit_rw: 63,
4706 .ieee_bits: 64,
4707 .round_towards_zero: false,
4708 .has_sign_dependent_rounding: true,
4709 .has_nans: true,
4710 .has_inf: true,
4711 .has_denorm: true,
4712 .has_signed_zero: true,
4713 .qnan_msb_set: true,
4714 .canonical_nan_lsbs_set: false,
4715 .name: "decimal_double"
4716 };
4717
4718/* Quad precision decimal floating point (IEEE 754). */
4719const struct real_format decimal_quad_format =
4720 {
4721 .encode: encode_decimal_quad,
4722 .decode: decode_decimal_quad,
4723 .b: 10,
4724 .p: 34,
4725 .pnan: 34,
4726 .emin: -6142,
4727 .emax: 6145,
4728 .signbit_ro: 127,
4729 .signbit_rw: 127,
4730 .ieee_bits: 128,
4731 .round_towards_zero: false,
4732 .has_sign_dependent_rounding: true,
4733 .has_nans: true,
4734 .has_inf: true,
4735 .has_denorm: true,
4736 .has_signed_zero: true,
4737 .qnan_msb_set: true,
4738 .canonical_nan_lsbs_set: false,
4739 .name: "decimal_quad"
4740 };
4741
4742/* Encode half-precision floats. This routine is used both for the IEEE
4743 ARM alternative encodings. */
4744static void
4745encode_ieee_half (const struct real_format *fmt, long *buf,
4746 const REAL_VALUE_TYPE *r)
4747{
4748 unsigned long image, sig, exp;
4749 unsigned long sign = r->sign;
4750
4751 image = sign << 15;
4752 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 11)) & 0x3ff;
4753
4754 switch (r->cl)
4755 {
4756 case rvc_zero:
4757 break;
4758
4759 case rvc_inf:
4760 if (fmt->has_inf)
4761 image |= 31 << 10;
4762 else
4763 image |= 0x7fff;
4764 break;
4765
4766 case rvc_nan:
4767 if (fmt->has_nans)
4768 {
4769 if (r->canonical)
4770 sig = (fmt->canonical_nan_lsbs_set ? (1 << 9) - 1 : 0);
4771 if (r->signalling == fmt->qnan_msb_set)
4772 sig &= ~(1 << 9);
4773 else
4774 sig |= 1 << 9;
4775 if (sig == 0)
4776 sig = 1 << 8;
4777
4778 image |= 31 << 10;
4779 image |= sig;
4780 }
4781 else
4782 image |= 0x3ff;
4783 break;
4784
4785 case rvc_normal:
4786 /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
4787 whereas the intermediate representation is 0.F x 2**exp.
4788 Which means we're off by one. */
4789 if (real_isdenormal (r))
4790 exp = 0;
4791 else
4792 exp = REAL_EXP (r) + 15 - 1;
4793 image |= exp << 10;
4794 image |= sig;
4795 break;
4796
4797 default:
4798 gcc_unreachable ();
4799 }
4800
4801 buf[0] = image;
4802}
4803
4804/* Decode half-precision floats. This routine is used both for the IEEE
4805 ARM alternative encodings. */
4806static void
4807decode_ieee_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4808 const long *buf)
4809{
4810 unsigned long image = buf[0] & 0xffff;
4811 bool sign = (image >> 15) & 1;
4812 int exp = (image >> 10) & 0x1f;
4813
4814 memset (s: r, c: 0, n: sizeof (*r));
4815 image <<= HOST_BITS_PER_LONG - 11;
4816 image &= ~SIG_MSB;
4817
4818 if (exp == 0)
4819 {
4820 if (image && fmt->has_denorm)
4821 {
4822 r->cl = rvc_normal;
4823 r->sign = sign;
4824 SET_REAL_EXP (r, -14);
4825 r->sig[SIGSZ-1] = image << 1;
4826 normalize (r);
4827 }
4828 else if (fmt->has_signed_zero)
4829 r->sign = sign;
4830 }
4831 else if (exp == 31 && (fmt->has_nans || fmt->has_inf))
4832 {
4833 if (image)
4834 {
4835 r->cl = rvc_nan;
4836 r->sign = sign;
4837 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4838 ^ fmt->qnan_msb_set);
4839 r->sig[SIGSZ-1] = image;
4840 }
4841 else
4842 {
4843 r->cl = rvc_inf;
4844 r->sign = sign;
4845 }
4846 }
4847 else
4848 {
4849 r->cl = rvc_normal;
4850 r->sign = sign;
4851 SET_REAL_EXP (r, exp - 15 + 1);
4852 r->sig[SIGSZ-1] = image | SIG_MSB;
4853 }
4854}
4855
4856/* Encode arm_bfloat types. */
4857static void
4858encode_arm_bfloat_half (const struct real_format *fmt, long *buf,
4859 const REAL_VALUE_TYPE *r)
4860{
4861 unsigned long image, sig, exp;
4862 unsigned long sign = r->sign;
4863
4864 image = sign << 15;
4865 sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 8)) & 0x7f;
4866
4867 switch (r->cl)
4868 {
4869 case rvc_zero:
4870 break;
4871
4872 case rvc_inf:
4873 if (fmt->has_inf)
4874 image |= 255 << 7;
4875 else
4876 image |= 0x7fff;
4877 break;
4878
4879 case rvc_nan:
4880 if (fmt->has_nans)
4881 {
4882 if (r->canonical)
4883 sig = (fmt->canonical_nan_lsbs_set ? (1 << 6) - 1 : 0);
4884 if (r->signalling == fmt->qnan_msb_set)
4885 sig &= ~(1 << 6);
4886 else
4887 sig |= 1 << 6;
4888 if (sig == 0)
4889 sig = 1 << 5;
4890
4891 image |= 255 << 7;
4892 image |= sig;
4893 }
4894 else
4895 image |= 0x7fff;
4896 break;
4897
4898 case rvc_normal:
4899 if (real_isdenormal (r))
4900 exp = 0;
4901 else
4902 exp = REAL_EXP (r) + 127 - 1;
4903 image |= exp << 7;
4904 image |= sig;
4905 break;
4906
4907 default:
4908 gcc_unreachable ();
4909 }
4910
4911 buf[0] = image;
4912}
4913
4914/* Decode arm_bfloat types. */
4915static void
4916decode_arm_bfloat_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
4917 const long *buf)
4918{
4919 unsigned long image = buf[0] & 0xffff;
4920 bool sign = (image >> 15) & 1;
4921 int exp = (image >> 7) & 0xff;
4922
4923 memset (s: r, c: 0, n: sizeof (*r));
4924 image <<= HOST_BITS_PER_LONG - 8;
4925 image &= ~SIG_MSB;
4926
4927 if (exp == 0)
4928 {
4929 if (image && fmt->has_denorm)
4930 {
4931 r->cl = rvc_normal;
4932 r->sign = sign;
4933 SET_REAL_EXP (r, -126);
4934 r->sig[SIGSZ-1] = image << 1;
4935 normalize (r);
4936 }
4937 else if (fmt->has_signed_zero)
4938 r->sign = sign;
4939 }
4940 else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
4941 {
4942 if (image)
4943 {
4944 r->cl = rvc_nan;
4945 r->sign = sign;
4946 r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
4947 ^ fmt->qnan_msb_set);
4948 r->sig[SIGSZ-1] = image;
4949 }
4950 else
4951 {
4952 r->cl = rvc_inf;
4953 r->sign = sign;
4954 }
4955 }
4956 else
4957 {
4958 r->cl = rvc_normal;
4959 r->sign = sign;
4960 SET_REAL_EXP (r, exp - 127 + 1);
4961 r->sig[SIGSZ-1] = image | SIG_MSB;
4962 }
4963}
4964
4965/* Half-precision format, as specified in IEEE 754R. */
4966const struct real_format ieee_half_format =
4967 {
4968 .encode: encode_ieee_half,
4969 .decode: decode_ieee_half,
4970 .b: 2,
4971 .p: 11,
4972 .pnan: 11,
4973 .emin: -13,
4974 .emax: 16,
4975 .signbit_ro: 15,
4976 .signbit_rw: 15,
4977 .ieee_bits: 16,
4978 .round_towards_zero: false,
4979 .has_sign_dependent_rounding: true,
4980 .has_nans: true,
4981 .has_inf: true,
4982 .has_denorm: true,
4983 .has_signed_zero: true,
4984 .qnan_msb_set: true,
4985 .canonical_nan_lsbs_set: false,
4986 .name: "ieee_half"
4987 };
4988
4989/* ARM's alternative half-precision format, similar to IEEE but with
4990 no reserved exponent value for NaNs and infinities; rather, it just
4991 extends the range of exponents by one. */
4992const struct real_format arm_half_format =
4993 {
4994 .encode: encode_ieee_half,
4995 .decode: decode_ieee_half,
4996 .b: 2,
4997 .p: 11,
4998 .pnan: 11,
4999 .emin: -13,
5000 .emax: 17,
5001 .signbit_ro: 15,
5002 .signbit_rw: 15,
5003 .ieee_bits: 0,
5004 .round_towards_zero: false,
5005 .has_sign_dependent_rounding: true,
5006 .has_nans: false,
5007 .has_inf: false,
5008 .has_denorm: true,
5009 .has_signed_zero: true,
5010 .qnan_msb_set: false,
5011 .canonical_nan_lsbs_set: false,
5012 .name: "arm_half"
5013 };
5014
5015/* ARM Bfloat half-precision format. This format resembles a truncated
5016 (16-bit) version of the 32-bit IEEE 754 single-precision floating-point
5017 format. */
5018const struct real_format arm_bfloat_half_format =
5019 {
5020 .encode: encode_arm_bfloat_half,
5021 .decode: decode_arm_bfloat_half,
5022 .b: 2,
5023 .p: 8,
5024 .pnan: 8,
5025 .emin: -125,
5026 .emax: 128,
5027 .signbit_ro: 15,
5028 .signbit_rw: 15,
5029 .ieee_bits: 0,
5030 .round_towards_zero: false,
5031 .has_sign_dependent_rounding: true,
5032 .has_nans: true,
5033 .has_inf: true,
5034 .has_denorm: true,
5035 .has_signed_zero: true,
5036 .qnan_msb_set: true,
5037 .canonical_nan_lsbs_set: false,
5038 .name: "arm_bfloat_half"
5039 };
5040
5041
5042/* A synthetic "format" for internal arithmetic. It's the size of the
5043 internal significand minus the two bits needed for proper rounding.
5044 The encode and decode routines exist only to satisfy our paranoia
5045 harness. */
5046
5047static void encode_internal (const struct real_format *fmt,
5048 long *, const REAL_VALUE_TYPE *);
5049static void decode_internal (const struct real_format *,
5050 REAL_VALUE_TYPE *, const long *);
5051
5052static void
5053encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
5054 const REAL_VALUE_TYPE *r)
5055{
5056 memcpy (dest: buf, src: r, n: sizeof (*r));
5057}
5058
5059static void
5060decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
5061 REAL_VALUE_TYPE *r, const long *buf)
5062{
5063 memcpy (dest: r, src: buf, n: sizeof (*r));
5064}
5065
5066const struct real_format real_internal_format =
5067 {
5068 .encode: encode_internal,
5069 .decode: decode_internal,
5070 .b: 2,
5071 SIGNIFICAND_BITS - 2,
5072 SIGNIFICAND_BITS - 2,
5073 .emin: -MAX_EXP,
5074 MAX_EXP,
5075 .signbit_ro: -1,
5076 .signbit_rw: -1,
5077 .ieee_bits: 0,
5078 .round_towards_zero: false,
5079 .has_sign_dependent_rounding: false,
5080 .has_nans: true,
5081 .has_inf: true,
5082 .has_denorm: false,
5083 .has_signed_zero: true,
5084 .qnan_msb_set: true,
5085 .canonical_nan_lsbs_set: false,
5086 .name: "real_internal"
5087 };
5088
5089/* Calculate X raised to the integer exponent N in format FMT and store
5090 the result in R. Return true if the result may be inexact due to
5091 loss of precision. The algorithm is the classic "left-to-right binary
5092 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
5093 Algorithms", "The Art of Computer Programming", Volume 2. */
5094
5095bool
5096real_powi (REAL_VALUE_TYPE *r, format_helper fmt,
5097 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
5098{
5099 unsigned HOST_WIDE_INT bit;
5100 REAL_VALUE_TYPE t;
5101 bool inexact = false;
5102 bool init = false;
5103 bool neg;
5104 int i;
5105
5106 if (n == 0)
5107 {
5108 *r = dconst1;
5109 return false;
5110 }
5111 else if (n < 0)
5112 {
5113 /* Don't worry about overflow, from now on n is unsigned. */
5114 neg = true;
5115 n = -n;
5116 }
5117 else
5118 neg = false;
5119
5120 t = *x;
5121 bit = HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1);
5122 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
5123 {
5124 if (init)
5125 {
5126 inexact |= do_multiply (r: &t, a: &t, b: &t);
5127 if (n & bit)
5128 inexact |= do_multiply (r: &t, a: &t, b: x);
5129 }
5130 else if (n & bit)
5131 init = true;
5132 bit >>= 1;
5133 }
5134
5135 if (neg)
5136 inexact |= do_divide (r: &t, a: &dconst1, b: &t);
5137
5138 real_convert (r, fmt, a: &t);
5139 return inexact;
5140}
5141
5142/* Round X to the nearest integer not larger in absolute value, i.e.
5143 towards zero, placing the result in R in format FMT. */
5144
5145void
5146real_trunc (REAL_VALUE_TYPE *r, format_helper fmt,
5147 const REAL_VALUE_TYPE *x)
5148{
5149 do_fix_trunc (r, a: x);
5150 if (fmt)
5151 real_convert (r, fmt, a: r);
5152}
5153
5154/* Round X to the largest integer not greater in value, i.e. round
5155 down, placing the result in R in format FMT. */
5156
5157void
5158real_floor (REAL_VALUE_TYPE *r, format_helper fmt,
5159 const REAL_VALUE_TYPE *x)
5160{
5161 REAL_VALUE_TYPE t;
5162
5163 do_fix_trunc (r: &t, a: x);
5164 if (! real_identical (a: &t, b: x) && x->sign)
5165 do_add (r: &t, a: &t, b: &dconstm1, subtract_p: 0);
5166 if (fmt)
5167 real_convert (r, fmt, a: &t);
5168 else
5169 *r = t;
5170}
5171
5172/* Round X to the smallest integer not less then argument, i.e. round
5173 up, placing the result in R in format FMT. */
5174
5175void
5176real_ceil (REAL_VALUE_TYPE *r, format_helper fmt,
5177 const REAL_VALUE_TYPE *x)
5178{
5179 REAL_VALUE_TYPE t;
5180
5181 do_fix_trunc (r: &t, a: x);
5182 if (! real_identical (a: &t, b: x) && ! x->sign)
5183 do_add (r: &t, a: &t, b: &dconst1, subtract_p: 0);
5184 if (fmt)
5185 real_convert (r, fmt, a: &t);
5186 else
5187 *r = t;
5188}
5189
5190/* Round X to the nearest integer, but round halfway cases away from
5191 zero. */
5192
5193void
5194real_round (REAL_VALUE_TYPE *r, format_helper fmt,
5195 const REAL_VALUE_TYPE *x)
5196{
5197 do_add (r, a: x, b: &dconsthalf, subtract_p: x->sign);
5198 do_fix_trunc (r, a: r);
5199 if (fmt)
5200 real_convert (r, fmt, a: r);
5201}
5202
5203/* Return true (including 0) if integer part of R is even, else return
5204 false. The function is not valid for rvc_inf and rvc_nan classes. */
5205
5206static bool
5207is_even (REAL_VALUE_TYPE *r)
5208{
5209 gcc_assert (r->cl != rvc_inf);
5210 gcc_assert (r->cl != rvc_nan);
5211
5212 if (r->cl == rvc_zero)
5213 return true;
5214
5215 /* For (-1,1), number is even. */
5216 if (REAL_EXP (r) <= 0)
5217 return true;
5218
5219 /* Check lowest bit, if not set, return true. */
5220 else if (REAL_EXP (r) <= SIGNIFICAND_BITS)
5221 {
5222 unsigned int n = SIGNIFICAND_BITS - REAL_EXP (r);
5223 int w = n / HOST_BITS_PER_LONG;
5224
5225 unsigned long num = ((unsigned long)1 << (n % HOST_BITS_PER_LONG));
5226
5227 if ((r->sig[w] & num) == 0)
5228 return true;
5229 }
5230 else
5231 return true;
5232
5233 return false;
5234}
5235
5236/* Return true if R is halfway between two integers, else return
5237 false. */
5238
5239static bool
5240is_halfway_below (const REAL_VALUE_TYPE *r)
5241{
5242 if (r->cl != rvc_normal)
5243 return false;
5244
5245 /* For numbers (-0.5,0) and (0,0.5). */
5246 if (REAL_EXP (r) < 0)
5247 return false;
5248
5249 else if (REAL_EXP (r) < SIGNIFICAND_BITS)
5250 {
5251 unsigned int n = SIGNIFICAND_BITS - REAL_EXP (r) - 1;
5252 int w = n / HOST_BITS_PER_LONG;
5253
5254 for (int i = 0; i < w; ++i)
5255 if (r->sig[i] != 0)
5256 return false;
5257
5258 unsigned long num = 1UL << (n % HOST_BITS_PER_LONG);
5259
5260 if ((r->sig[w] & num) != 0 && (r->sig[w] & (num - 1)) == 0)
5261 return true;
5262 }
5263 return false;
5264}
5265
5266/* Round X to nearest integer, rounding halfway cases towards even. */
5267
5268void
5269real_roundeven (REAL_VALUE_TYPE *r, format_helper fmt,
5270 const REAL_VALUE_TYPE *x)
5271{
5272 if (is_halfway_below (r: x))
5273 {
5274 /* Special case as -0.5 rounds to -0.0 and
5275 similarly +0.5 rounds to +0.0. */
5276 if (REAL_EXP (x) == 0)
5277 {
5278 *r = *x;
5279 clear_significand_below (r, SIGNIFICAND_BITS);
5280 }
5281 else
5282 {
5283 do_add (r, a: x, b: &dconsthalf, subtract_p: x->sign);
5284 if (!is_even (r))
5285 do_add (r, a: r, b: &dconstm1, subtract_p: x->sign);
5286 }
5287 if (fmt)
5288 real_convert (r, fmt, a: r);
5289 }
5290 else
5291 real_round (r, fmt, x);
5292}
5293
5294/* Set the sign of R to the sign of X. */
5295
5296void
5297real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
5298{
5299 r->sign = x->sign;
5300}
5301
5302/* Check whether the real constant value given is an integer.
5303 Returns false for signaling NaN. */
5304
5305bool
5306real_isinteger (const REAL_VALUE_TYPE *c, format_helper fmt)
5307{
5308 REAL_VALUE_TYPE cint;
5309
5310 real_trunc (r: &cint, fmt, x: c);
5311 return real_identical (a: c, b: &cint);
5312}
5313
5314/* Check whether C is an integer that fits in a HOST_WIDE_INT,
5315 storing it in *INT_OUT if so. */
5316
5317bool
5318real_isinteger (const REAL_VALUE_TYPE *c, HOST_WIDE_INT *int_out)
5319{
5320 REAL_VALUE_TYPE cint;
5321
5322 HOST_WIDE_INT n = real_to_integer (r: c);
5323 real_from_integer (r: &cint, VOIDmode, val_in: n, sgn: SIGNED);
5324 if (real_identical (a: c, b: &cint))
5325 {
5326 *int_out = n;
5327 return true;
5328 }
5329 return false;
5330}
5331
5332/* Calculate nextafter (X, Y) or nexttoward (X, Y). Return true if
5333 underflow or overflow needs to be raised. */
5334
5335bool
5336real_nextafter (REAL_VALUE_TYPE *r, format_helper fmt,
5337 const REAL_VALUE_TYPE *x, const REAL_VALUE_TYPE *y)
5338{
5339 int cmp = do_compare (a: x, b: y, nan_result: 2);
5340 /* If either operand is NaN, return qNaN. */
5341 if (cmp == 2)
5342 {
5343 get_canonical_qnan (r, sign: 0);
5344 return false;
5345 }
5346 /* If x == y, return y cast to target type. */
5347 if (cmp == 0)
5348 {
5349 real_convert (r, fmt, a: y);
5350 return false;
5351 }
5352
5353 if (x->cl == rvc_zero)
5354 {
5355 get_zero (r, sign: y->sign);
5356 r->cl = rvc_normal;
5357 SET_REAL_EXP (r, fmt->emin - fmt->p + 1);
5358 r->sig[SIGSZ - 1] = SIG_MSB;
5359 return false;
5360 }
5361
5362 int np2 = SIGNIFICAND_BITS - fmt->p;
5363 /* For denormals adjust np2 correspondingly. */
5364 if (x->cl == rvc_normal && REAL_EXP (x) < fmt->emin)
5365 np2 += fmt->emin - REAL_EXP (x);
5366
5367 REAL_VALUE_TYPE u;
5368 get_zero (r, sign: x->sign);
5369 get_zero (r: &u, sign: 0);
5370 set_significand_bit (r: &u, n: np2);
5371 r->cl = rvc_normal;
5372 SET_REAL_EXP (r, REAL_EXP (x));
5373
5374 if (x->cl == rvc_inf)
5375 {
5376 bool borrow = sub_significands (r, a: r, b: &u, carry: 0);
5377 gcc_assert (borrow);
5378 SET_REAL_EXP (r, fmt->emax);
5379 }
5380 else if (cmp == (x->sign ? 1 : -1))
5381 {
5382 if (add_significands (r, a: x, b: &u))
5383 {
5384 /* Overflow. Means the significand had been all ones, and
5385 is now all zeros. Need to increase the exponent, and
5386 possibly re-normalize it. */
5387 SET_REAL_EXP (r, REAL_EXP (r) + 1);
5388 if (REAL_EXP (r) > fmt->emax)
5389 {
5390 get_inf (r, sign: x->sign);
5391 return true;
5392 }
5393 r->sig[SIGSZ - 1] = SIG_MSB;
5394 }
5395 }
5396 else
5397 {
5398 if (REAL_EXP (x) > fmt->emin && x->sig[SIGSZ - 1] == SIG_MSB)
5399 {
5400 int i;
5401 for (i = SIGSZ - 2; i >= 0; i--)
5402 if (x->sig[i])
5403 break;
5404 if (i < 0)
5405 {
5406 /* When mantissa is 1.0, we need to subtract only
5407 half of u: nextafter (1.0, 0.0) is 1.0 - __DBL_EPSILON__ / 2
5408 rather than 1.0 - __DBL_EPSILON__. */
5409 clear_significand_bit (r: &u, n: np2);
5410 np2--;
5411 set_significand_bit (r: &u, n: np2);
5412 }
5413 }
5414 sub_significands (r, a: x, b: &u, carry: 0);
5415 }
5416
5417 /* Clear out trailing garbage. */
5418 clear_significand_below (r, n: np2);
5419 normalize (r);
5420 if (REAL_EXP (r) <= fmt->emin - fmt->p)
5421 {
5422 get_zero (r, sign: x->sign);
5423 return true;
5424 }
5425 return r->cl == rvc_zero || REAL_EXP (r) < fmt->emin;
5426}
5427
5428/* Write into BUF the maximum representable finite floating-point
5429 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5430 float string. LEN is the size of BUF, and the buffer must be large
5431 enough to contain the resulting string. If NORM_MAX, instead write
5432 the maximum representable finite normalized floating-point number,
5433 defined to be such that all choices of digits for that exponent are
5434 representable in the format (this only makes a difference for IBM
5435 long double). */
5436
5437void
5438get_max_float (const struct real_format *fmt, char *buf, size_t len,
5439 bool norm_max)
5440{
5441 int i, n;
5442 char *p;
5443 bool is_ibm_extended = fmt->pnan < fmt->p;
5444
5445 strcpy (dest: buf, src: "0x0.");
5446 n = fmt->p;
5447 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
5448 *p++ = 'f';
5449 if (i < n)
5450 *p++ = "08ce"[n - i];
5451 sprintf (s: p, format: "p%d",
5452 (is_ibm_extended && norm_max) ? fmt->emax - 1 : fmt->emax);
5453 if (is_ibm_extended && !norm_max)
5454 {
5455 /* This is an IBM extended double format made up of two IEEE
5456 doubles. The value of the long double is the sum of the
5457 values of the two parts. The most significant part is
5458 required to be the value of the long double rounded to the
5459 nearest double. Rounding means we need a slightly smaller
5460 value for LDBL_MAX. */
5461 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
5462 }
5463
5464 gcc_assert (strlen (buf) < len);
5465}
5466
5467/* True if all values of integral type can be represented
5468 by this floating-point type exactly. */
5469
5470bool format_helper::can_represent_integral_type_p (tree type) const
5471{
5472 gcc_assert (! decimal_p () && INTEGRAL_TYPE_P (type));
5473
5474 /* INT?_MIN is power-of-two so it takes
5475 only one mantissa bit. */
5476 bool signed_p = TYPE_SIGN (type) == SIGNED;
5477 return TYPE_PRECISION (type) - signed_p <= significand_size (fmt: *this);
5478}
5479
5480/* True if mode M has a NaN representation and
5481 the treatment of NaN operands is important. */
5482
5483bool
5484HONOR_NANS (machine_mode m)
5485{
5486 return MODE_HAS_NANS (m) && !flag_finite_math_only;
5487}
5488
5489bool
5490HONOR_NANS (const_tree t)
5491{
5492 return HONOR_NANS (m: element_mode (t));
5493}
5494
5495bool
5496HONOR_NANS (const_rtx x)
5497{
5498 return HONOR_NANS (GET_MODE (x));
5499}
5500
5501/* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5502
5503bool
5504HONOR_SNANS (machine_mode m)
5505{
5506 return flag_signaling_nans && HONOR_NANS (m);
5507}
5508
5509bool
5510HONOR_SNANS (const_tree t)
5511{
5512 return HONOR_SNANS (m: element_mode (t));
5513}
5514
5515bool
5516HONOR_SNANS (const_rtx x)
5517{
5518 return HONOR_SNANS (GET_MODE (x));
5519}
5520
5521/* As for HONOR_NANS, but true if the mode can represent infinity and
5522 the treatment of infinite values is important. */
5523
5524bool
5525HONOR_INFINITIES (machine_mode m)
5526{
5527 return MODE_HAS_INFINITIES (m) && !flag_finite_math_only;
5528}
5529
5530bool
5531HONOR_INFINITIES (const_tree t)
5532{
5533 return HONOR_INFINITIES (m: element_mode (t));
5534}
5535
5536bool
5537HONOR_INFINITIES (const_rtx x)
5538{
5539 return HONOR_INFINITIES (GET_MODE (x));
5540}
5541
5542/* Like HONOR_NANS, but true if the given mode distinguishes between
5543 positive and negative zero, and the sign of zero is important. */
5544
5545bool
5546HONOR_SIGNED_ZEROS (machine_mode m)
5547{
5548 return MODE_HAS_SIGNED_ZEROS (m) && flag_signed_zeros;
5549}
5550
5551bool
5552HONOR_SIGNED_ZEROS (const_tree t)
5553{
5554 return HONOR_SIGNED_ZEROS (m: element_mode (t));
5555}
5556
5557bool
5558HONOR_SIGNED_ZEROS (const_rtx x)
5559{
5560 return HONOR_SIGNED_ZEROS (GET_MODE (x));
5561}
5562
5563/* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5564 and the rounding mode is important. */
5565
5566bool
5567HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m)
5568{
5569 return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m) && flag_rounding_math;
5570}
5571
5572bool
5573HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t)
5574{
5575 return HONOR_SIGN_DEPENDENT_ROUNDING (m: element_mode (t));
5576}
5577
5578bool
5579HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x)
5580{
5581 return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x));
5582}
5583
5584/* Fills r with the largest value such that 1 + r*r won't overflow.
5585 This is used in both sin (atan (x)) and cos (atan(x)) optimizations. */
5586
5587void
5588build_sinatan_real (REAL_VALUE_TYPE * r, tree type)
5589{
5590 REAL_VALUE_TYPE maxval;
5591 mpfr_t mpfr_const1, mpfr_c, mpfr_maxval;
5592 machine_mode mode = TYPE_MODE (type);
5593 const struct real_format * fmt = REAL_MODE_FORMAT (mode);
5594
5595 real_maxval (r: &maxval, sign: 0, mode);
5596
5597 mpfr_inits (mpfr_const1, mpfr_c, mpfr_maxval, NULL);
5598
5599 mpfr_from_real (mpfr_const1, &dconst1, MPFR_RNDN);
5600 mpfr_from_real (mpfr_maxval, &maxval, MPFR_RNDN);
5601
5602 mpfr_sub (mpfr_c, mpfr_maxval, mpfr_const1, MPFR_RNDN);
5603 mpfr_sqrt (mpfr_c, mpfr_c, MPFR_RNDZ);
5604
5605 real_from_mpfr (r, mpfr_c, fmt, MPFR_RNDZ);
5606
5607 mpfr_clears (mpfr_const1, mpfr_c, mpfr_maxval, NULL);
5608}
5609

source code of gcc/real.cc