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