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