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