2010-04-01 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / config / fp-bit.c
blob1cdac6ebecac1708e9cad742eb72719ebdace5ec
1 /* This is a software floating point library which can be used
2 for targets without hardware floating point.
3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003,
4 2004, 2005, 2008, 2009 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
27 /* This implements IEEE 754 format arithmetic, but does not provide a
28 mechanism for setting the rounding mode, or for generating or handling
29 exceptions.
31 The original code by Steve Chamberlain, hacked by Mark Eichin and Jim
32 Wilson, all of Cygnus Support. */
34 /* The intended way to use this file is to make two copies, add `#define FLOAT'
35 to one copy, then compile both copies and add them to libgcc.a. */
37 #include "tconfig.h"
38 #include "coretypes.h"
39 #include "tm.h"
40 #include "config/fp-bit.h"
42 /* The following macros can be defined to change the behavior of this file:
43 FLOAT: Implement a `float', aka SFmode, fp library. If this is not
44 defined, then this file implements a `double', aka DFmode, fp library.
45 FLOAT_ONLY: Used with FLOAT, to implement a `float' only library, i.e.
46 don't include float->double conversion which requires the double library.
47 This is useful only for machines which can't support doubles, e.g. some
48 8-bit processors.
49 CMPtype: Specify the type that floating point compares should return.
50 This defaults to SItype, aka int.
51 US_SOFTWARE_GOFAST: This makes all entry points use the same names as the
52 US Software goFast library.
53 _DEBUG_BITFLOAT: This makes debugging the code a little easier, by adding
54 two integers to the FLO_union_type.
55 NO_DENORMALS: Disable handling of denormals.
56 NO_NANS: Disable nan and infinity handling
57 SMALL_MACHINE: Useful when operations on QIs and HIs are faster
58 than on an SI */
60 /* We don't currently support extended floats (long doubles) on machines
61 without hardware to deal with them.
63 These stubs are just to keep the linker from complaining about unresolved
64 references which can be pulled in from libio & libstdc++, even if the
65 user isn't using long doubles. However, they may generate an unresolved
66 external to abort if abort is not used by the function, and the stubs
67 are referenced from within libc, since libgcc goes before and after the
68 system library. */
70 #ifdef DECLARE_LIBRARY_RENAMES
71 DECLARE_LIBRARY_RENAMES
72 #endif
74 #ifdef EXTENDED_FLOAT_STUBS
75 extern void abort (void);
76 void __extendsfxf2 (void) { abort(); }
77 void __extenddfxf2 (void) { abort(); }
78 void __truncxfdf2 (void) { abort(); }
79 void __truncxfsf2 (void) { abort(); }
80 void __fixxfsi (void) { abort(); }
81 void __floatsixf (void) { abort(); }
82 void __addxf3 (void) { abort(); }
83 void __subxf3 (void) { abort(); }
84 void __mulxf3 (void) { abort(); }
85 void __divxf3 (void) { abort(); }
86 void __negxf2 (void) { abort(); }
87 void __eqxf2 (void) { abort(); }
88 void __nexf2 (void) { abort(); }
89 void __gtxf2 (void) { abort(); }
90 void __gexf2 (void) { abort(); }
91 void __lexf2 (void) { abort(); }
92 void __ltxf2 (void) { abort(); }
94 void __extendsftf2 (void) { abort(); }
95 void __extenddftf2 (void) { abort(); }
96 void __trunctfdf2 (void) { abort(); }
97 void __trunctfsf2 (void) { abort(); }
98 void __fixtfsi (void) { abort(); }
99 void __floatsitf (void) { abort(); }
100 void __addtf3 (void) { abort(); }
101 void __subtf3 (void) { abort(); }
102 void __multf3 (void) { abort(); }
103 void __divtf3 (void) { abort(); }
104 void __negtf2 (void) { abort(); }
105 void __eqtf2 (void) { abort(); }
106 void __netf2 (void) { abort(); }
107 void __gttf2 (void) { abort(); }
108 void __getf2 (void) { abort(); }
109 void __letf2 (void) { abort(); }
110 void __lttf2 (void) { abort(); }
111 #else /* !EXTENDED_FLOAT_STUBS, rest of file */
113 /* IEEE "special" number predicates */
115 #ifdef NO_NANS
117 #define nan() 0
118 #define isnan(x) 0
119 #define isinf(x) 0
120 #else
122 #if defined L_thenan_sf
123 const fp_number_type __thenan_sf = { CLASS_SNAN, 0, 0, {(fractype) 0} };
124 #elif defined L_thenan_df
125 const fp_number_type __thenan_df = { CLASS_SNAN, 0, 0, {(fractype) 0} };
126 #elif defined L_thenan_tf
127 const fp_number_type __thenan_tf = { CLASS_SNAN, 0, 0, {(fractype) 0} };
128 #elif defined TFLOAT
129 extern const fp_number_type __thenan_tf;
130 #elif defined FLOAT
131 extern const fp_number_type __thenan_sf;
132 #else
133 extern const fp_number_type __thenan_df;
134 #endif
136 INLINE
137 static const fp_number_type *
138 makenan (void)
140 #ifdef TFLOAT
141 return & __thenan_tf;
142 #elif defined FLOAT
143 return & __thenan_sf;
144 #else
145 return & __thenan_df;
146 #endif
149 INLINE
150 static int
151 isnan (const fp_number_type *x)
153 return __builtin_expect (x->class == CLASS_SNAN || x->class == CLASS_QNAN,
157 INLINE
158 static int
159 isinf (const fp_number_type * x)
161 return __builtin_expect (x->class == CLASS_INFINITY, 0);
164 #endif /* NO_NANS */
166 INLINE
167 static int
168 iszero (const fp_number_type * x)
170 return x->class == CLASS_ZERO;
173 INLINE
174 static void
175 flip_sign ( fp_number_type * x)
177 x->sign = !x->sign;
180 /* Count leading zeroes in N. */
181 INLINE
182 static int
183 clzusi (USItype n)
185 extern int __clzsi2 (USItype);
186 if (sizeof (USItype) == sizeof (unsigned int))
187 return __builtin_clz (n);
188 else if (sizeof (USItype) == sizeof (unsigned long))
189 return __builtin_clzl (n);
190 else if (sizeof (USItype) == sizeof (unsigned long long))
191 return __builtin_clzll (n);
192 else
193 return __clzsi2 (n);
196 extern FLO_type pack_d (const fp_number_type * );
198 #if defined(L_pack_df) || defined(L_pack_sf) || defined(L_pack_tf)
199 FLO_type
200 pack_d (const fp_number_type *src)
202 FLO_union_type dst;
203 fractype fraction = src->fraction.ll; /* wasn't unsigned before? */
204 int sign = src->sign;
205 int exp = 0;
207 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && (isnan (src) || isinf (src)))
209 /* We can't represent these values accurately. By using the
210 largest possible magnitude, we guarantee that the conversion
211 of infinity is at least as big as any finite number. */
212 exp = EXPMAX;
213 fraction = ((fractype) 1 << FRACBITS) - 1;
215 else if (isnan (src))
217 exp = EXPMAX;
218 if (src->class == CLASS_QNAN || 1)
220 #ifdef QUIET_NAN_NEGATED
221 fraction |= QUIET_NAN - 1;
222 #else
223 fraction |= QUIET_NAN;
224 #endif
227 else if (isinf (src))
229 exp = EXPMAX;
230 fraction = 0;
232 else if (iszero (src))
234 exp = 0;
235 fraction = 0;
237 else if (fraction == 0)
239 exp = 0;
241 else
243 if (__builtin_expect (src->normal_exp < NORMAL_EXPMIN, 0))
245 #ifdef NO_DENORMALS
246 /* Go straight to a zero representation if denormals are not
247 supported. The denormal handling would be harmless but
248 isn't unnecessary. */
249 exp = 0;
250 fraction = 0;
251 #else /* NO_DENORMALS */
252 /* This number's exponent is too low to fit into the bits
253 available in the number, so we'll store 0 in the exponent and
254 shift the fraction to the right to make up for it. */
256 int shift = NORMAL_EXPMIN - src->normal_exp;
258 exp = 0;
260 if (shift > FRAC_NBITS - NGARDS)
262 /* No point shifting, since it's more that 64 out. */
263 fraction = 0;
265 else
267 int lowbit = (fraction & (((fractype)1 << shift) - 1)) ? 1 : 0;
268 fraction = (fraction >> shift) | lowbit;
270 if ((fraction & GARDMASK) == GARDMSB)
272 if ((fraction & (1 << NGARDS)))
273 fraction += GARDROUND + 1;
275 else
277 /* Add to the guards to round up. */
278 fraction += GARDROUND;
280 /* Perhaps the rounding means we now need to change the
281 exponent, because the fraction is no longer denormal. */
282 if (fraction >= IMPLICIT_1)
284 exp += 1;
286 fraction >>= NGARDS;
287 #endif /* NO_DENORMALS */
289 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS)
290 && __builtin_expect (src->normal_exp > EXPBIAS, 0))
292 exp = EXPMAX;
293 fraction = 0;
295 else
297 exp = src->normal_exp + EXPBIAS;
298 if (!ROUND_TOWARDS_ZERO)
300 /* IF the gard bits are the all zero, but the first, then we're
301 half way between two numbers, choose the one which makes the
302 lsb of the answer 0. */
303 if ((fraction & GARDMASK) == GARDMSB)
305 if (fraction & (1 << NGARDS))
306 fraction += GARDROUND + 1;
308 else
310 /* Add a one to the guards to round up */
311 fraction += GARDROUND;
313 if (fraction >= IMPLICIT_2)
315 fraction >>= 1;
316 exp += 1;
319 fraction >>= NGARDS;
321 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && exp > EXPMAX)
323 /* Saturate on overflow. */
324 exp = EXPMAX;
325 fraction = ((fractype) 1 << FRACBITS) - 1;
330 /* We previously used bitfields to store the number, but this doesn't
331 handle little/big endian systems conveniently, so use shifts and
332 masks */
333 #ifdef FLOAT_BIT_ORDER_MISMATCH
334 dst.bits.fraction = fraction;
335 dst.bits.exp = exp;
336 dst.bits.sign = sign;
337 #else
338 # if defined TFLOAT && defined HALFFRACBITS
340 halffractype high, low, unity;
341 int lowsign, lowexp;
343 unity = (halffractype) 1 << HALFFRACBITS;
345 /* Set HIGH to the high double's significand, masking out the implicit 1.
346 Set LOW to the low double's full significand. */
347 high = (fraction >> (FRACBITS - HALFFRACBITS)) & (unity - 1);
348 low = fraction & (unity * 2 - 1);
350 /* Get the initial sign and exponent of the low double. */
351 lowexp = exp - HALFFRACBITS - 1;
352 lowsign = sign;
354 /* HIGH should be rounded like a normal double, making |LOW| <=
355 0.5 ULP of HIGH. Assume round-to-nearest. */
356 if (exp < EXPMAX)
357 if (low > unity || (low == unity && (high & 1) == 1))
359 /* Round HIGH up and adjust LOW to match. */
360 high++;
361 if (high == unity)
363 /* May make it infinite, but that's OK. */
364 high = 0;
365 exp++;
367 low = unity * 2 - low;
368 lowsign ^= 1;
371 high |= (halffractype) exp << HALFFRACBITS;
372 high |= (halffractype) sign << (HALFFRACBITS + EXPBITS);
374 if (exp == EXPMAX || exp == 0 || low == 0)
375 low = 0;
376 else
378 while (lowexp > 0 && low < unity)
380 low <<= 1;
381 lowexp--;
384 if (lowexp <= 0)
386 halffractype roundmsb, round;
387 int shift;
389 shift = 1 - lowexp;
390 roundmsb = (1 << (shift - 1));
391 round = low & ((roundmsb << 1) - 1);
393 low >>= shift;
394 lowexp = 0;
396 if (round > roundmsb || (round == roundmsb && (low & 1) == 1))
398 low++;
399 if (low == unity)
400 /* LOW rounds up to the smallest normal number. */
401 lowexp++;
405 low &= unity - 1;
406 low |= (halffractype) lowexp << HALFFRACBITS;
407 low |= (halffractype) lowsign << (HALFFRACBITS + EXPBITS);
409 dst.value_raw = ((fractype) high << HALFSHIFT) | low;
411 # else
412 dst.value_raw = fraction & ((((fractype)1) << FRACBITS) - (fractype)1);
413 dst.value_raw |= ((fractype) (exp & ((1 << EXPBITS) - 1))) << FRACBITS;
414 dst.value_raw |= ((fractype) (sign & 1)) << (FRACBITS | EXPBITS);
415 # endif
416 #endif
418 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
419 #ifdef TFLOAT
421 qrtrfractype tmp1 = dst.words[0];
422 qrtrfractype tmp2 = dst.words[1];
423 dst.words[0] = dst.words[3];
424 dst.words[1] = dst.words[2];
425 dst.words[2] = tmp2;
426 dst.words[3] = tmp1;
428 #else
430 halffractype tmp = dst.words[0];
431 dst.words[0] = dst.words[1];
432 dst.words[1] = tmp;
434 #endif
435 #endif
437 return dst.value;
439 #endif
441 #if defined(L_unpack_df) || defined(L_unpack_sf) || defined(L_unpack_tf)
442 void
443 unpack_d (FLO_union_type * src, fp_number_type * dst)
445 /* We previously used bitfields to store the number, but this doesn't
446 handle little/big endian systems conveniently, so use shifts and
447 masks */
448 fractype fraction;
449 int exp;
450 int sign;
452 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
453 FLO_union_type swapped;
455 #ifdef TFLOAT
456 swapped.words[0] = src->words[3];
457 swapped.words[1] = src->words[2];
458 swapped.words[2] = src->words[1];
459 swapped.words[3] = src->words[0];
460 #else
461 swapped.words[0] = src->words[1];
462 swapped.words[1] = src->words[0];
463 #endif
464 src = &swapped;
465 #endif
467 #ifdef FLOAT_BIT_ORDER_MISMATCH
468 fraction = src->bits.fraction;
469 exp = src->bits.exp;
470 sign = src->bits.sign;
471 #else
472 # if defined TFLOAT && defined HALFFRACBITS
474 halffractype high, low;
476 high = src->value_raw >> HALFSHIFT;
477 low = src->value_raw & (((fractype)1 << HALFSHIFT) - 1);
479 fraction = high & ((((fractype)1) << HALFFRACBITS) - 1);
480 fraction <<= FRACBITS - HALFFRACBITS;
481 exp = ((int)(high >> HALFFRACBITS)) & ((1 << EXPBITS) - 1);
482 sign = ((int)(high >> (((HALFFRACBITS + EXPBITS))))) & 1;
484 if (exp != EXPMAX && exp != 0 && low != 0)
486 int lowexp = ((int)(low >> HALFFRACBITS)) & ((1 << EXPBITS) - 1);
487 int lowsign = ((int)(low >> (((HALFFRACBITS + EXPBITS))))) & 1;
488 int shift;
489 fractype xlow;
491 xlow = low & ((((fractype)1) << HALFFRACBITS) - 1);
492 if (lowexp)
493 xlow |= (((halffractype)1) << HALFFRACBITS);
494 else
495 lowexp = 1;
496 shift = (FRACBITS - HALFFRACBITS) - (exp - lowexp);
497 if (shift > 0)
498 xlow <<= shift;
499 else if (shift < 0)
500 xlow >>= -shift;
501 if (sign == lowsign)
502 fraction += xlow;
503 else if (fraction >= xlow)
504 fraction -= xlow;
505 else
507 /* The high part is a power of two but the full number is lower.
508 This code will leave the implicit 1 in FRACTION, but we'd
509 have added that below anyway. */
510 fraction = (((fractype) 1 << FRACBITS) - xlow) << 1;
511 exp--;
515 # else
516 fraction = src->value_raw & ((((fractype)1) << FRACBITS) - 1);
517 exp = ((int)(src->value_raw >> FRACBITS)) & ((1 << EXPBITS) - 1);
518 sign = ((int)(src->value_raw >> (FRACBITS + EXPBITS))) & 1;
519 # endif
520 #endif
522 dst->sign = sign;
523 if (exp == 0)
525 /* Hmm. Looks like 0 */
526 if (fraction == 0
527 #ifdef NO_DENORMALS
528 || 1
529 #endif
532 /* tastes like zero */
533 dst->class = CLASS_ZERO;
535 else
537 /* Zero exponent with nonzero fraction - it's denormalized,
538 so there isn't a leading implicit one - we'll shift it so
539 it gets one. */
540 dst->normal_exp = exp - EXPBIAS + 1;
541 fraction <<= NGARDS;
543 dst->class = CLASS_NUMBER;
544 #if 1
545 while (fraction < IMPLICIT_1)
547 fraction <<= 1;
548 dst->normal_exp--;
550 #endif
551 dst->fraction.ll = fraction;
554 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS)
555 && __builtin_expect (exp == EXPMAX, 0))
557 /* Huge exponent*/
558 if (fraction == 0)
560 /* Attached to a zero fraction - means infinity */
561 dst->class = CLASS_INFINITY;
563 else
565 /* Nonzero fraction, means nan */
566 #ifdef QUIET_NAN_NEGATED
567 if ((fraction & QUIET_NAN) == 0)
568 #else
569 if (fraction & QUIET_NAN)
570 #endif
572 dst->class = CLASS_QNAN;
574 else
576 dst->class = CLASS_SNAN;
578 /* Keep the fraction part as the nan number */
579 dst->fraction.ll = fraction;
582 else
584 /* Nothing strange about this number */
585 dst->normal_exp = exp - EXPBIAS;
586 dst->class = CLASS_NUMBER;
587 dst->fraction.ll = (fraction << NGARDS) | IMPLICIT_1;
590 #endif /* L_unpack_df || L_unpack_sf */
592 #if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
593 static const fp_number_type *
594 _fpadd_parts (fp_number_type * a,
595 fp_number_type * b,
596 fp_number_type * tmp)
598 intfrac tfraction;
600 /* Put commonly used fields in local variables. */
601 int a_normal_exp;
602 int b_normal_exp;
603 fractype a_fraction;
604 fractype b_fraction;
606 if (isnan (a))
608 return a;
610 if (isnan (b))
612 return b;
614 if (isinf (a))
616 /* Adding infinities with opposite signs yields a NaN. */
617 if (isinf (b) && a->sign != b->sign)
618 return makenan ();
619 return a;
621 if (isinf (b))
623 return b;
625 if (iszero (b))
627 if (iszero (a))
629 *tmp = *a;
630 tmp->sign = a->sign & b->sign;
631 return tmp;
633 return a;
635 if (iszero (a))
637 return b;
640 /* Got two numbers. shift the smaller and increment the exponent till
641 they're the same */
643 int diff;
644 int sdiff;
646 a_normal_exp = a->normal_exp;
647 b_normal_exp = b->normal_exp;
648 a_fraction = a->fraction.ll;
649 b_fraction = b->fraction.ll;
651 diff = a_normal_exp - b_normal_exp;
652 sdiff = diff;
654 if (diff < 0)
655 diff = -diff;
656 if (diff < FRAC_NBITS)
658 if (sdiff > 0)
660 b_normal_exp += diff;
661 LSHIFT (b_fraction, diff);
663 else if (sdiff < 0)
665 a_normal_exp += diff;
666 LSHIFT (a_fraction, diff);
669 else
671 /* Somethings's up.. choose the biggest */
672 if (a_normal_exp > b_normal_exp)
674 b_normal_exp = a_normal_exp;
675 b_fraction = 0;
677 else
679 a_normal_exp = b_normal_exp;
680 a_fraction = 0;
685 if (a->sign != b->sign)
687 if (a->sign)
689 tfraction = -a_fraction + b_fraction;
691 else
693 tfraction = a_fraction - b_fraction;
695 if (tfraction >= 0)
697 tmp->sign = 0;
698 tmp->normal_exp = a_normal_exp;
699 tmp->fraction.ll = tfraction;
701 else
703 tmp->sign = 1;
704 tmp->normal_exp = a_normal_exp;
705 tmp->fraction.ll = -tfraction;
707 /* and renormalize it */
709 while (tmp->fraction.ll < IMPLICIT_1 && tmp->fraction.ll)
711 tmp->fraction.ll <<= 1;
712 tmp->normal_exp--;
715 else
717 tmp->sign = a->sign;
718 tmp->normal_exp = a_normal_exp;
719 tmp->fraction.ll = a_fraction + b_fraction;
721 tmp->class = CLASS_NUMBER;
722 /* Now the fraction is added, we have to shift down to renormalize the
723 number */
725 if (tmp->fraction.ll >= IMPLICIT_2)
727 LSHIFT (tmp->fraction.ll, 1);
728 tmp->normal_exp++;
730 return tmp;
733 FLO_type
734 add (FLO_type arg_a, FLO_type arg_b)
736 fp_number_type a;
737 fp_number_type b;
738 fp_number_type tmp;
739 const fp_number_type *res;
740 FLO_union_type au, bu;
742 au.value = arg_a;
743 bu.value = arg_b;
745 unpack_d (&au, &a);
746 unpack_d (&bu, &b);
748 res = _fpadd_parts (&a, &b, &tmp);
750 return pack_d (res);
753 FLO_type
754 sub (FLO_type arg_a, FLO_type arg_b)
756 fp_number_type a;
757 fp_number_type b;
758 fp_number_type tmp;
759 const fp_number_type *res;
760 FLO_union_type au, bu;
762 au.value = arg_a;
763 bu.value = arg_b;
765 unpack_d (&au, &a);
766 unpack_d (&bu, &b);
768 b.sign ^= 1;
770 res = _fpadd_parts (&a, &b, &tmp);
772 return pack_d (res);
774 #endif /* L_addsub_sf || L_addsub_df */
776 #if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
777 static inline __attribute__ ((__always_inline__)) const fp_number_type *
778 _fpmul_parts ( fp_number_type * a,
779 fp_number_type * b,
780 fp_number_type * tmp)
782 fractype low = 0;
783 fractype high = 0;
785 if (isnan (a))
787 a->sign = a->sign != b->sign;
788 return a;
790 if (isnan (b))
792 b->sign = a->sign != b->sign;
793 return b;
795 if (isinf (a))
797 if (iszero (b))
798 return makenan ();
799 a->sign = a->sign != b->sign;
800 return a;
802 if (isinf (b))
804 if (iszero (a))
806 return makenan ();
808 b->sign = a->sign != b->sign;
809 return b;
811 if (iszero (a))
813 a->sign = a->sign != b->sign;
814 return a;
816 if (iszero (b))
818 b->sign = a->sign != b->sign;
819 return b;
822 /* Calculate the mantissa by multiplying both numbers to get a
823 twice-as-wide number. */
825 #if defined(NO_DI_MODE) || defined(TFLOAT)
827 fractype x = a->fraction.ll;
828 fractype ylow = b->fraction.ll;
829 fractype yhigh = 0;
830 int bit;
832 /* ??? This does multiplies one bit at a time. Optimize. */
833 for (bit = 0; bit < FRAC_NBITS; bit++)
835 int carry;
837 if (x & 1)
839 carry = (low += ylow) < ylow;
840 high += yhigh + carry;
842 yhigh <<= 1;
843 if (ylow & FRACHIGH)
845 yhigh |= 1;
847 ylow <<= 1;
848 x >>= 1;
851 #elif defined(FLOAT)
852 /* Multiplying two USIs to get a UDI, we're safe. */
854 UDItype answer = (UDItype)a->fraction.ll * (UDItype)b->fraction.ll;
856 high = answer >> BITS_PER_SI;
857 low = answer;
859 #else
860 /* fractype is DImode, but we need the result to be twice as wide.
861 Assuming a widening multiply from DImode to TImode is not
862 available, build one by hand. */
864 USItype nl = a->fraction.ll;
865 USItype nh = a->fraction.ll >> BITS_PER_SI;
866 USItype ml = b->fraction.ll;
867 USItype mh = b->fraction.ll >> BITS_PER_SI;
868 UDItype pp_ll = (UDItype) ml * nl;
869 UDItype pp_hl = (UDItype) mh * nl;
870 UDItype pp_lh = (UDItype) ml * nh;
871 UDItype pp_hh = (UDItype) mh * nh;
872 UDItype res2 = 0;
873 UDItype res0 = 0;
874 UDItype ps_hh__ = pp_hl + pp_lh;
875 if (ps_hh__ < pp_hl)
876 res2 += (UDItype)1 << BITS_PER_SI;
877 pp_hl = (UDItype)(USItype)ps_hh__ << BITS_PER_SI;
878 res0 = pp_ll + pp_hl;
879 if (res0 < pp_ll)
880 res2++;
881 res2 += (ps_hh__ >> BITS_PER_SI) + pp_hh;
882 high = res2;
883 low = res0;
885 #endif
888 tmp->normal_exp = a->normal_exp + b->normal_exp
889 + FRAC_NBITS - (FRACBITS + NGARDS);
890 tmp->sign = a->sign != b->sign;
891 while (high >= IMPLICIT_2)
893 tmp->normal_exp++;
894 if (high & 1)
896 low >>= 1;
897 low |= FRACHIGH;
899 high >>= 1;
901 while (high < IMPLICIT_1)
903 tmp->normal_exp--;
905 high <<= 1;
906 if (low & FRACHIGH)
907 high |= 1;
908 low <<= 1;
911 if (!ROUND_TOWARDS_ZERO && (high & GARDMASK) == GARDMSB)
913 if (high & (1 << NGARDS))
915 /* Because we're half way, we would round to even by adding
916 GARDROUND + 1, except that's also done in the packing
917 function, and rounding twice will lose precision and cause
918 the result to be too far off. Example: 32-bit floats with
919 bit patterns 0xfff * 0x3f800400 ~= 0xfff (less than 0.5ulp
920 off), not 0x1000 (more than 0.5ulp off). */
922 else if (low)
924 /* We're a further than half way by a small amount corresponding
925 to the bits set in "low". Knowing that, we round here and
926 not in pack_d, because there we don't have "low" available
927 anymore. */
928 high += GARDROUND + 1;
930 /* Avoid further rounding in pack_d. */
931 high &= ~(fractype) GARDMASK;
934 tmp->fraction.ll = high;
935 tmp->class = CLASS_NUMBER;
936 return tmp;
939 FLO_type
940 multiply (FLO_type arg_a, FLO_type arg_b)
942 fp_number_type a;
943 fp_number_type b;
944 fp_number_type tmp;
945 const fp_number_type *res;
946 FLO_union_type au, bu;
948 au.value = arg_a;
949 bu.value = arg_b;
951 unpack_d (&au, &a);
952 unpack_d (&bu, &b);
954 res = _fpmul_parts (&a, &b, &tmp);
956 return pack_d (res);
958 #endif /* L_mul_sf || L_mul_df || L_mul_tf */
960 #if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
961 static inline __attribute__ ((__always_inline__)) const fp_number_type *
962 _fpdiv_parts (fp_number_type * a,
963 fp_number_type * b)
965 fractype bit;
966 fractype numerator;
967 fractype denominator;
968 fractype quotient;
970 if (isnan (a))
972 return a;
974 if (isnan (b))
976 return b;
979 a->sign = a->sign ^ b->sign;
981 if (isinf (a) || iszero (a))
983 if (a->class == b->class)
984 return makenan ();
985 return a;
988 if (isinf (b))
990 a->fraction.ll = 0;
991 a->normal_exp = 0;
992 return a;
994 if (iszero (b))
996 a->class = CLASS_INFINITY;
997 return a;
1000 /* Calculate the mantissa by multiplying both 64bit numbers to get a
1001 128 bit number */
1003 /* quotient =
1004 ( numerator / denominator) * 2^(numerator exponent - denominator exponent)
1007 a->normal_exp = a->normal_exp - b->normal_exp;
1008 numerator = a->fraction.ll;
1009 denominator = b->fraction.ll;
1011 if (numerator < denominator)
1013 /* Fraction will be less than 1.0 */
1014 numerator *= 2;
1015 a->normal_exp--;
1017 bit = IMPLICIT_1;
1018 quotient = 0;
1019 /* ??? Does divide one bit at a time. Optimize. */
1020 while (bit)
1022 if (numerator >= denominator)
1024 quotient |= bit;
1025 numerator -= denominator;
1027 bit >>= 1;
1028 numerator *= 2;
1031 if (!ROUND_TOWARDS_ZERO && (quotient & GARDMASK) == GARDMSB)
1033 if (quotient & (1 << NGARDS))
1035 /* Because we're half way, we would round to even by adding
1036 GARDROUND + 1, except that's also done in the packing
1037 function, and rounding twice will lose precision and cause
1038 the result to be too far off. */
1040 else if (numerator)
1042 /* We're a further than half way by the small amount
1043 corresponding to the bits set in "numerator". Knowing
1044 that, we round here and not in pack_d, because there we
1045 don't have "numerator" available anymore. */
1046 quotient += GARDROUND + 1;
1048 /* Avoid further rounding in pack_d. */
1049 quotient &= ~(fractype) GARDMASK;
1053 a->fraction.ll = quotient;
1054 return (a);
1058 FLO_type
1059 divide (FLO_type arg_a, FLO_type arg_b)
1061 fp_number_type a;
1062 fp_number_type b;
1063 const fp_number_type *res;
1064 FLO_union_type au, bu;
1066 au.value = arg_a;
1067 bu.value = arg_b;
1069 unpack_d (&au, &a);
1070 unpack_d (&bu, &b);
1072 res = _fpdiv_parts (&a, &b);
1074 return pack_d (res);
1076 #endif /* L_div_sf || L_div_df */
1078 #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df) \
1079 || defined(L_fpcmp_parts_tf)
1080 /* according to the demo, fpcmp returns a comparison with 0... thus
1081 a<b -> -1
1082 a==b -> 0
1083 a>b -> +1
1087 __fpcmp_parts (fp_number_type * a, fp_number_type * b)
1089 #if 0
1090 /* either nan -> unordered. Must be checked outside of this routine. */
1091 if (isnan (a) && isnan (b))
1093 return 1; /* still unordered! */
1095 #endif
1097 if (isnan (a) || isnan (b))
1099 return 1; /* how to indicate unordered compare? */
1101 if (isinf (a) && isinf (b))
1103 /* +inf > -inf, but +inf != +inf */
1104 /* b \a| +inf(0)| -inf(1)
1105 ______\+--------+--------
1106 +inf(0)| a==b(0)| a<b(-1)
1107 -------+--------+--------
1108 -inf(1)| a>b(1) | a==b(0)
1109 -------+--------+--------
1110 So since unordered must be nonzero, just line up the columns...
1112 return b->sign - a->sign;
1114 /* but not both... */
1115 if (isinf (a))
1117 return a->sign ? -1 : 1;
1119 if (isinf (b))
1121 return b->sign ? 1 : -1;
1123 if (iszero (a) && iszero (b))
1125 return 0;
1127 if (iszero (a))
1129 return b->sign ? 1 : -1;
1131 if (iszero (b))
1133 return a->sign ? -1 : 1;
1135 /* now both are "normal". */
1136 if (a->sign != b->sign)
1138 /* opposite signs */
1139 return a->sign ? -1 : 1;
1141 /* same sign; exponents? */
1142 if (a->normal_exp > b->normal_exp)
1144 return a->sign ? -1 : 1;
1146 if (a->normal_exp < b->normal_exp)
1148 return a->sign ? 1 : -1;
1150 /* same exponents; check size. */
1151 if (a->fraction.ll > b->fraction.ll)
1153 return a->sign ? -1 : 1;
1155 if (a->fraction.ll < b->fraction.ll)
1157 return a->sign ? 1 : -1;
1159 /* after all that, they're equal. */
1160 return 0;
1162 #endif
1164 #if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compoare_tf)
1165 CMPtype
1166 compare (FLO_type arg_a, FLO_type arg_b)
1168 fp_number_type a;
1169 fp_number_type b;
1170 FLO_union_type au, bu;
1172 au.value = arg_a;
1173 bu.value = arg_b;
1175 unpack_d (&au, &a);
1176 unpack_d (&bu, &b);
1178 return __fpcmp_parts (&a, &b);
1180 #endif /* L_compare_sf || L_compare_df */
1182 #ifndef US_SOFTWARE_GOFAST
1184 /* These should be optimized for their specific tasks someday. */
1186 #if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
1187 CMPtype
1188 _eq_f2 (FLO_type arg_a, FLO_type arg_b)
1190 fp_number_type a;
1191 fp_number_type b;
1192 FLO_union_type au, bu;
1194 au.value = arg_a;
1195 bu.value = arg_b;
1197 unpack_d (&au, &a);
1198 unpack_d (&bu, &b);
1200 if (isnan (&a) || isnan (&b))
1201 return 1; /* false, truth == 0 */
1203 return __fpcmp_parts (&a, &b) ;
1205 #endif /* L_eq_sf || L_eq_df */
1207 #if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
1208 CMPtype
1209 _ne_f2 (FLO_type arg_a, FLO_type arg_b)
1211 fp_number_type a;
1212 fp_number_type b;
1213 FLO_union_type au, bu;
1215 au.value = arg_a;
1216 bu.value = arg_b;
1218 unpack_d (&au, &a);
1219 unpack_d (&bu, &b);
1221 if (isnan (&a) || isnan (&b))
1222 return 1; /* true, truth != 0 */
1224 return __fpcmp_parts (&a, &b) ;
1226 #endif /* L_ne_sf || L_ne_df */
1228 #if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
1229 CMPtype
1230 _gt_f2 (FLO_type arg_a, FLO_type arg_b)
1232 fp_number_type a;
1233 fp_number_type b;
1234 FLO_union_type au, bu;
1236 au.value = arg_a;
1237 bu.value = arg_b;
1239 unpack_d (&au, &a);
1240 unpack_d (&bu, &b);
1242 if (isnan (&a) || isnan (&b))
1243 return -1; /* false, truth > 0 */
1245 return __fpcmp_parts (&a, &b);
1247 #endif /* L_gt_sf || L_gt_df */
1249 #if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
1250 CMPtype
1251 _ge_f2 (FLO_type arg_a, FLO_type arg_b)
1253 fp_number_type a;
1254 fp_number_type b;
1255 FLO_union_type au, bu;
1257 au.value = arg_a;
1258 bu.value = arg_b;
1260 unpack_d (&au, &a);
1261 unpack_d (&bu, &b);
1263 if (isnan (&a) || isnan (&b))
1264 return -1; /* false, truth >= 0 */
1265 return __fpcmp_parts (&a, &b) ;
1267 #endif /* L_ge_sf || L_ge_df */
1269 #if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
1270 CMPtype
1271 _lt_f2 (FLO_type arg_a, FLO_type arg_b)
1273 fp_number_type a;
1274 fp_number_type b;
1275 FLO_union_type au, bu;
1277 au.value = arg_a;
1278 bu.value = arg_b;
1280 unpack_d (&au, &a);
1281 unpack_d (&bu, &b);
1283 if (isnan (&a) || isnan (&b))
1284 return 1; /* false, truth < 0 */
1286 return __fpcmp_parts (&a, &b);
1288 #endif /* L_lt_sf || L_lt_df */
1290 #if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
1291 CMPtype
1292 _le_f2 (FLO_type arg_a, FLO_type arg_b)
1294 fp_number_type a;
1295 fp_number_type b;
1296 FLO_union_type au, bu;
1298 au.value = arg_a;
1299 bu.value = arg_b;
1301 unpack_d (&au, &a);
1302 unpack_d (&bu, &b);
1304 if (isnan (&a) || isnan (&b))
1305 return 1; /* false, truth <= 0 */
1307 return __fpcmp_parts (&a, &b) ;
1309 #endif /* L_le_sf || L_le_df */
1311 #endif /* ! US_SOFTWARE_GOFAST */
1313 #if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
1314 CMPtype
1315 _unord_f2 (FLO_type arg_a, FLO_type arg_b)
1317 fp_number_type a;
1318 fp_number_type b;
1319 FLO_union_type au, bu;
1321 au.value = arg_a;
1322 bu.value = arg_b;
1324 unpack_d (&au, &a);
1325 unpack_d (&bu, &b);
1327 return (isnan (&a) || isnan (&b));
1329 #endif /* L_unord_sf || L_unord_df */
1331 #if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
1332 FLO_type
1333 si_to_float (SItype arg_a)
1335 fp_number_type in;
1337 in.class = CLASS_NUMBER;
1338 in.sign = arg_a < 0;
1339 if (!arg_a)
1341 in.class = CLASS_ZERO;
1343 else
1345 USItype uarg;
1346 int shift;
1347 in.normal_exp = FRACBITS + NGARDS;
1348 if (in.sign)
1350 /* Special case for minint, since there is no +ve integer
1351 representation for it */
1352 if (arg_a == (- MAX_SI_INT - 1))
1354 return (FLO_type)(- MAX_SI_INT - 1);
1356 uarg = (-arg_a);
1358 else
1359 uarg = arg_a;
1361 in.fraction.ll = uarg;
1362 shift = clzusi (uarg) - (BITS_PER_SI - 1 - FRACBITS - NGARDS);
1363 if (shift > 0)
1365 in.fraction.ll <<= shift;
1366 in.normal_exp -= shift;
1369 return pack_d (&in);
1371 #endif /* L_si_to_sf || L_si_to_df */
1373 #if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
1374 FLO_type
1375 usi_to_float (USItype arg_a)
1377 fp_number_type in;
1379 in.sign = 0;
1380 if (!arg_a)
1382 in.class = CLASS_ZERO;
1384 else
1386 int shift;
1387 in.class = CLASS_NUMBER;
1388 in.normal_exp = FRACBITS + NGARDS;
1389 in.fraction.ll = arg_a;
1391 shift = clzusi (arg_a) - (BITS_PER_SI - 1 - FRACBITS - NGARDS);
1392 if (shift < 0)
1394 fractype guard = in.fraction.ll & (((fractype)1 << -shift) - 1);
1395 in.fraction.ll >>= -shift;
1396 in.fraction.ll |= (guard != 0);
1397 in.normal_exp -= shift;
1399 else if (shift > 0)
1401 in.fraction.ll <<= shift;
1402 in.normal_exp -= shift;
1405 return pack_d (&in);
1407 #endif
1409 #if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
1410 SItype
1411 float_to_si (FLO_type arg_a)
1413 fp_number_type a;
1414 SItype tmp;
1415 FLO_union_type au;
1417 au.value = arg_a;
1418 unpack_d (&au, &a);
1420 if (iszero (&a))
1421 return 0;
1422 if (isnan (&a))
1423 return 0;
1424 /* get reasonable MAX_SI_INT... */
1425 if (isinf (&a))
1426 return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1427 /* it is a number, but a small one */
1428 if (a.normal_exp < 0)
1429 return 0;
1430 if (a.normal_exp > BITS_PER_SI - 2)
1431 return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1432 tmp = a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1433 return a.sign ? (-tmp) : (tmp);
1435 #endif /* L_sf_to_si || L_df_to_si */
1437 #if defined(L_sf_to_usi) || defined(L_df_to_usi) || defined(L_tf_to_usi)
1438 #if defined US_SOFTWARE_GOFAST || defined(L_tf_to_usi)
1439 /* While libgcc2.c defines its own __fixunssfsi and __fixunsdfsi routines,
1440 we also define them for GOFAST because the ones in libgcc2.c have the
1441 wrong names and I'd rather define these here and keep GOFAST CYG-LOC's
1442 out of libgcc2.c. We can't define these here if not GOFAST because then
1443 there'd be duplicate copies. */
1445 USItype
1446 float_to_usi (FLO_type arg_a)
1448 fp_number_type a;
1449 FLO_union_type au;
1451 au.value = arg_a;
1452 unpack_d (&au, &a);
1454 if (iszero (&a))
1455 return 0;
1456 if (isnan (&a))
1457 return 0;
1458 /* it is a negative number */
1459 if (a.sign)
1460 return 0;
1461 /* get reasonable MAX_USI_INT... */
1462 if (isinf (&a))
1463 return MAX_USI_INT;
1464 /* it is a number, but a small one */
1465 if (a.normal_exp < 0)
1466 return 0;
1467 if (a.normal_exp > BITS_PER_SI - 1)
1468 return MAX_USI_INT;
1469 else if (a.normal_exp > (FRACBITS + NGARDS))
1470 return a.fraction.ll << (a.normal_exp - (FRACBITS + NGARDS));
1471 else
1472 return a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1474 #endif /* US_SOFTWARE_GOFAST */
1475 #endif /* L_sf_to_usi || L_df_to_usi */
1477 #if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
1478 FLO_type
1479 negate (FLO_type arg_a)
1481 fp_number_type a;
1482 FLO_union_type au;
1484 au.value = arg_a;
1485 unpack_d (&au, &a);
1487 flip_sign (&a);
1488 return pack_d (&a);
1490 #endif /* L_negate_sf || L_negate_df */
1492 #ifdef FLOAT
1494 #if defined(L_make_sf)
1495 SFtype
1496 __make_fp(fp_class_type class,
1497 unsigned int sign,
1498 int exp,
1499 USItype frac)
1501 fp_number_type in;
1503 in.class = class;
1504 in.sign = sign;
1505 in.normal_exp = exp;
1506 in.fraction.ll = frac;
1507 return pack_d (&in);
1509 #endif /* L_make_sf */
1511 #ifndef FLOAT_ONLY
1513 /* This enables one to build an fp library that supports float but not double.
1514 Otherwise, we would get an undefined reference to __make_dp.
1515 This is needed for some 8-bit ports that can't handle well values that
1516 are 8-bytes in size, so we just don't support double for them at all. */
1518 #if defined(L_sf_to_df)
1519 DFtype
1520 sf_to_df (SFtype arg_a)
1522 fp_number_type in;
1523 FLO_union_type au;
1525 au.value = arg_a;
1526 unpack_d (&au, &in);
1528 return __make_dp (in.class, in.sign, in.normal_exp,
1529 ((UDItype) in.fraction.ll) << F_D_BITOFF);
1531 #endif /* L_sf_to_df */
1533 #if defined(L_sf_to_tf) && defined(TMODES)
1534 TFtype
1535 sf_to_tf (SFtype arg_a)
1537 fp_number_type in;
1538 FLO_union_type au;
1540 au.value = arg_a;
1541 unpack_d (&au, &in);
1543 return __make_tp (in.class, in.sign, in.normal_exp,
1544 ((UTItype) in.fraction.ll) << F_T_BITOFF);
1546 #endif /* L_sf_to_df */
1548 #endif /* ! FLOAT_ONLY */
1549 #endif /* FLOAT */
1551 #ifndef FLOAT
1553 extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
1555 #if defined(L_make_df)
1556 DFtype
1557 __make_dp (fp_class_type class, unsigned int sign, int exp, UDItype frac)
1559 fp_number_type in;
1561 in.class = class;
1562 in.sign = sign;
1563 in.normal_exp = exp;
1564 in.fraction.ll = frac;
1565 return pack_d (&in);
1567 #endif /* L_make_df */
1569 #if defined(L_df_to_sf)
1570 SFtype
1571 df_to_sf (DFtype arg_a)
1573 fp_number_type in;
1574 USItype sffrac;
1575 FLO_union_type au;
1577 au.value = arg_a;
1578 unpack_d (&au, &in);
1580 sffrac = in.fraction.ll >> F_D_BITOFF;
1582 /* We set the lowest guard bit in SFFRAC if we discarded any non
1583 zero bits. */
1584 if ((in.fraction.ll & (((USItype) 1 << F_D_BITOFF) - 1)) != 0)
1585 sffrac |= 1;
1587 return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
1589 #endif /* L_df_to_sf */
1591 #if defined(L_df_to_tf) && defined(TMODES) \
1592 && !defined(FLOAT) && !defined(TFLOAT)
1593 TFtype
1594 df_to_tf (DFtype arg_a)
1596 fp_number_type in;
1597 FLO_union_type au;
1599 au.value = arg_a;
1600 unpack_d (&au, &in);
1602 return __make_tp (in.class, in.sign, in.normal_exp,
1603 ((UTItype) in.fraction.ll) << D_T_BITOFF);
1605 #endif /* L_sf_to_df */
1607 #ifdef TFLOAT
1608 #if defined(L_make_tf)
1609 TFtype
1610 __make_tp(fp_class_type class,
1611 unsigned int sign,
1612 int exp,
1613 UTItype frac)
1615 fp_number_type in;
1617 in.class = class;
1618 in.sign = sign;
1619 in.normal_exp = exp;
1620 in.fraction.ll = frac;
1621 return pack_d (&in);
1623 #endif /* L_make_tf */
1625 #if defined(L_tf_to_df)
1626 DFtype
1627 tf_to_df (TFtype arg_a)
1629 fp_number_type in;
1630 UDItype sffrac;
1631 FLO_union_type au;
1633 au.value = arg_a;
1634 unpack_d (&au, &in);
1636 sffrac = in.fraction.ll >> D_T_BITOFF;
1638 /* We set the lowest guard bit in SFFRAC if we discarded any non
1639 zero bits. */
1640 if ((in.fraction.ll & (((UTItype) 1 << D_T_BITOFF) - 1)) != 0)
1641 sffrac |= 1;
1643 return __make_dp (in.class, in.sign, in.normal_exp, sffrac);
1645 #endif /* L_tf_to_df */
1647 #if defined(L_tf_to_sf)
1648 SFtype
1649 tf_to_sf (TFtype arg_a)
1651 fp_number_type in;
1652 USItype sffrac;
1653 FLO_union_type au;
1655 au.value = arg_a;
1656 unpack_d (&au, &in);
1658 sffrac = in.fraction.ll >> F_T_BITOFF;
1660 /* We set the lowest guard bit in SFFRAC if we discarded any non
1661 zero bits. */
1662 if ((in.fraction.ll & (((UTItype) 1 << F_T_BITOFF) - 1)) != 0)
1663 sffrac |= 1;
1665 return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
1667 #endif /* L_tf_to_sf */
1668 #endif /* TFLOAT */
1670 #endif /* ! FLOAT */
1671 #endif /* !EXTENDED_FLOAT_STUBS */