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