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