PR target/7856
[official-gcc.git] / gcc / config / fp-bit.c
blob7ec20ecf9f21fb3d2c4e44fab174a98e8027f266
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
4 Free Software Foundation, Inc.
6 This file is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file with other programs, and to distribute
14 those programs without any restriction coming from the use of this
15 file. (The General Public License restrictions do apply in other
16 respects; for example, they cover modification of the file, and
17 distribution when not linked into another program.)
19 This file is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; see the file COPYING. If not, write to
26 the Free Software Foundation, 59 Temple Place - Suite 330,
27 Boston, MA 02111-1307, USA. */
29 /* As a special exception, if you link this library with other files,
30 some of which are compiled with GCC, to produce an executable,
31 this library does not by itself cause the resulting executable
32 to be covered by the GNU General Public License.
33 This exception does not however invalidate any other reasons why
34 the executable file might be covered by the GNU General Public License. */
36 /* This implements IEEE 754 format arithmetic, but does not provide a
37 mechanism for setting the rounding mode, or for generating or handling
38 exceptions.
40 The original code by Steve Chamberlain, hacked by Mark Eichin and Jim
41 Wilson, all of Cygnus Support. */
43 /* The intended way to use this file is to make two copies, add `#define FLOAT'
44 to one copy, then compile both copies and add them to libgcc.a. */
46 #include "tconfig.h"
47 #include "fp-bit.h"
49 /* The following macros can be defined to change the behavior of this file:
50 FLOAT: Implement a `float', aka SFmode, fp library. If this is not
51 defined, then this file implements a `double', aka DFmode, fp library.
52 FLOAT_ONLY: Used with FLOAT, to implement a `float' only library, i.e.
53 don't include float->double conversion which requires the double library.
54 This is useful only for machines which can't support doubles, e.g. some
55 8-bit processors.
56 CMPtype: Specify the type that floating point compares should return.
57 This defaults to SItype, aka int.
58 US_SOFTWARE_GOFAST: This makes all entry points use the same names as the
59 US Software goFast library.
60 _DEBUG_BITFLOAT: This makes debugging the code a little easier, by adding
61 two integers to the FLO_union_type.
62 NO_DENORMALS: Disable handling of denormals.
63 NO_NANS: Disable nan and infinity handling
64 SMALL_MACHINE: Useful when operations on QIs and HIs are faster
65 than on an SI */
67 /* We don't currently support extended floats (long doubles) on machines
68 without hardware to deal with them.
70 These stubs are just to keep the linker from complaining about unresolved
71 references which can be pulled in from libio & libstdc++, even if the
72 user isn't using long doubles. However, they may generate an unresolved
73 external to abort if abort is not used by the function, and the stubs
74 are referenced from within libc, since libgcc goes before and after the
75 system library. */
77 #ifdef DECLARE_LIBRARY_RENAMES
78 DECLARE_LIBRARY_RENAMES
79 #endif
81 #ifdef EXTENDED_FLOAT_STUBS
82 extern void abort (void);
83 void __extendsfxf2 (void) { abort(); }
84 void __extenddfxf2 (void) { abort(); }
85 void __truncxfdf2 (void) { abort(); }
86 void __truncxfsf2 (void) { abort(); }
87 void __fixxfsi (void) { abort(); }
88 void __floatsixf (void) { abort(); }
89 void __addxf3 (void) { abort(); }
90 void __subxf3 (void) { abort(); }
91 void __mulxf3 (void) { abort(); }
92 void __divxf3 (void) { abort(); }
93 void __negxf2 (void) { abort(); }
94 void __eqxf2 (void) { abort(); }
95 void __nexf2 (void) { abort(); }
96 void __gtxf2 (void) { abort(); }
97 void __gexf2 (void) { abort(); }
98 void __lexf2 (void) { abort(); }
99 void __ltxf2 (void) { abort(); }
101 void __extendsftf2 (void) { abort(); }
102 void __extenddftf2 (void) { abort(); }
103 void __trunctfdf2 (void) { abort(); }
104 void __trunctfsf2 (void) { abort(); }
105 void __fixtfsi (void) { abort(); }
106 void __floatsitf (void) { abort(); }
107 void __addtf3 (void) { abort(); }
108 void __subtf3 (void) { abort(); }
109 void __multf3 (void) { abort(); }
110 void __divtf3 (void) { abort(); }
111 void __negtf2 (void) { abort(); }
112 void __eqtf2 (void) { abort(); }
113 void __netf2 (void) { abort(); }
114 void __gttf2 (void) { abort(); }
115 void __getf2 (void) { abort(); }
116 void __letf2 (void) { abort(); }
117 void __lttf2 (void) { abort(); }
118 #else /* !EXTENDED_FLOAT_STUBS, rest of file */
120 /* IEEE "special" number predicates */
122 #ifdef NO_NANS
124 #define nan() 0
125 #define isnan(x) 0
126 #define isinf(x) 0
127 #else
129 #if defined L_thenan_sf
130 const fp_number_type __thenan_sf = { CLASS_SNAN, 0, 0, {(fractype) 0} };
131 #elif defined L_thenan_df
132 const fp_number_type __thenan_df = { CLASS_SNAN, 0, 0, {(fractype) 0} };
133 #elif defined FLOAT
134 extern const fp_number_type __thenan_sf;
135 #else
136 extern const fp_number_type __thenan_df;
137 #endif
139 INLINE
140 static fp_number_type *
141 nan (void)
143 /* Discard the const qualifier... */
144 #ifdef FLOAT
145 return (fp_number_type *) (& __thenan_sf);
146 #else
147 return (fp_number_type *) (& __thenan_df);
148 #endif
151 INLINE
152 static int
153 isnan ( fp_number_type * x)
155 return x->class == CLASS_SNAN || x->class == CLASS_QNAN;
158 INLINE
159 static int
160 isinf ( fp_number_type * x)
162 return x->class == CLASS_INFINITY;
165 #endif /* NO_NANS */
167 INLINE
168 static int
169 iszero ( fp_number_type * x)
171 return x->class == CLASS_ZERO;
174 INLINE
175 static void
176 flip_sign ( fp_number_type * x)
178 x->sign = !x->sign;
181 extern FLO_type pack_d ( fp_number_type * );
183 #if defined(L_pack_df) || defined(L_pack_sf)
184 FLO_type
185 pack_d ( fp_number_type * src)
187 FLO_union_type dst;
188 fractype fraction = src->fraction.ll; /* wasn't unsigned before? */
189 int sign = src->sign;
190 int exp = 0;
192 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && (isnan (src) || isinf (src)))
194 /* We can't represent these values accurately. By using the
195 largest possible magnitude, we guarantee that the conversion
196 of infinity is at least as big as any finite number. */
197 exp = EXPMAX;
198 fraction = ((fractype) 1 << FRACBITS) - 1;
200 else if (isnan (src))
202 exp = EXPMAX;
203 if (src->class == CLASS_QNAN || 1)
205 fraction |= QUIET_NAN;
208 else if (isinf (src))
210 exp = EXPMAX;
211 fraction = 0;
213 else if (iszero (src))
215 exp = 0;
216 fraction = 0;
218 else if (fraction == 0)
220 exp = 0;
222 else
224 if (src->normal_exp < NORMAL_EXPMIN)
226 #ifdef NO_DENORMALS
227 /* Go straight to a zero representation if denormals are not
228 supported. The denormal handling would be harmless but
229 isn't unnecessary. */
230 exp = 0;
231 fraction = 0;
232 #else /* NO_DENORMALS */
233 /* This number's exponent is too low to fit into the bits
234 available in the number, so we'll store 0 in the exponent and
235 shift the fraction to the right to make up for it. */
237 int shift = NORMAL_EXPMIN - src->normal_exp;
239 exp = 0;
241 if (shift > FRAC_NBITS - NGARDS)
243 /* No point shifting, since it's more that 64 out. */
244 fraction = 0;
246 else
248 int lowbit = (fraction & (((fractype)1 << shift) - 1)) ? 1 : 0;
249 fraction = (fraction >> shift) | lowbit;
251 if ((fraction & GARDMASK) == GARDMSB)
253 if ((fraction & (1 << NGARDS)))
254 fraction += GARDROUND + 1;
256 else
258 /* Add to the guards to round up. */
259 fraction += GARDROUND;
261 /* Perhaps the rounding means we now need to change the
262 exponent, because the fraction is no longer denormal. */
263 if (fraction >= IMPLICIT_1)
265 exp += 1;
267 fraction >>= NGARDS;
268 #endif /* NO_DENORMALS */
270 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS)
271 && src->normal_exp > EXPBIAS)
273 exp = EXPMAX;
274 fraction = 0;
276 else
278 exp = src->normal_exp + EXPBIAS;
279 if (!ROUND_TOWARDS_ZERO)
281 /* IF the gard bits are the all zero, but the first, then we're
282 half way between two numbers, choose the one which makes the
283 lsb of the answer 0. */
284 if ((fraction & GARDMASK) == GARDMSB)
286 if (fraction & (1 << NGARDS))
287 fraction += GARDROUND + 1;
289 else
291 /* Add a one to the guards to round up */
292 fraction += GARDROUND;
294 if (fraction >= IMPLICIT_2)
296 fraction >>= 1;
297 exp += 1;
300 fraction >>= NGARDS;
302 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && exp > EXPMAX)
304 /* Saturate on overflow. */
305 exp = EXPMAX;
306 fraction = ((fractype) 1 << FRACBITS) - 1;
311 /* We previously used bitfields to store the number, but this doesn't
312 handle little/big endian systems conveniently, so use shifts and
313 masks */
314 #ifdef FLOAT_BIT_ORDER_MISMATCH
315 dst.bits.fraction = fraction;
316 dst.bits.exp = exp;
317 dst.bits.sign = sign;
318 #else
319 dst.value_raw = fraction & ((((fractype)1) << FRACBITS) - (fractype)1);
320 dst.value_raw |= ((fractype) (exp & ((1 << EXPBITS) - 1))) << FRACBITS;
321 dst.value_raw |= ((fractype) (sign & 1)) << (FRACBITS | EXPBITS);
322 #endif
324 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
326 halffractype tmp = dst.words[0];
327 dst.words[0] = dst.words[1];
328 dst.words[1] = tmp;
330 #endif
332 return dst.value;
334 #endif
336 #if defined(L_unpack_df) || defined(L_unpack_sf)
337 void
338 unpack_d (FLO_union_type * src, fp_number_type * dst)
340 /* We previously used bitfields to store the number, but this doesn't
341 handle little/big endian systems conveniently, so use shifts and
342 masks */
343 fractype fraction;
344 int exp;
345 int sign;
347 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
348 FLO_union_type swapped;
350 swapped.words[0] = src->words[1];
351 swapped.words[1] = src->words[0];
352 src = &swapped;
353 #endif
355 #ifdef FLOAT_BIT_ORDER_MISMATCH
356 fraction = src->bits.fraction;
357 exp = src->bits.exp;
358 sign = src->bits.sign;
359 #else
360 fraction = src->value_raw & ((((fractype)1) << FRACBITS) - (fractype)1);
361 exp = ((int)(src->value_raw >> FRACBITS)) & ((1 << EXPBITS) - 1);
362 sign = ((int)(src->value_raw >> (FRACBITS + EXPBITS))) & 1;
363 #endif
365 dst->sign = sign;
366 if (exp == 0)
368 /* Hmm. Looks like 0 */
369 if (fraction == 0
370 #ifdef NO_DENORMALS
371 || 1
372 #endif
375 /* tastes like zero */
376 dst->class = CLASS_ZERO;
378 else
380 /* Zero exponent with nonzero fraction - it's denormalized,
381 so there isn't a leading implicit one - we'll shift it so
382 it gets one. */
383 dst->normal_exp = exp - EXPBIAS + 1;
384 fraction <<= NGARDS;
386 dst->class = CLASS_NUMBER;
387 #if 1
388 while (fraction < IMPLICIT_1)
390 fraction <<= 1;
391 dst->normal_exp--;
393 #endif
394 dst->fraction.ll = fraction;
397 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && exp == EXPMAX)
399 /* Huge exponent*/
400 if (fraction == 0)
402 /* Attached to a zero fraction - means infinity */
403 dst->class = CLASS_INFINITY;
405 else
407 /* Nonzero fraction, means nan */
408 if (fraction & QUIET_NAN)
410 dst->class = CLASS_QNAN;
412 else
414 dst->class = CLASS_SNAN;
416 /* Keep the fraction part as the nan number */
417 dst->fraction.ll = fraction;
420 else
422 /* Nothing strange about this number */
423 dst->normal_exp = exp - EXPBIAS;
424 dst->class = CLASS_NUMBER;
425 dst->fraction.ll = (fraction << NGARDS) | IMPLICIT_1;
428 #endif /* L_unpack_df || L_unpack_sf */
430 #if defined(L_addsub_sf) || defined(L_addsub_df)
431 static fp_number_type *
432 _fpadd_parts (fp_number_type * a,
433 fp_number_type * b,
434 fp_number_type * tmp)
436 intfrac tfraction;
438 /* Put commonly used fields in local variables. */
439 int a_normal_exp;
440 int b_normal_exp;
441 fractype a_fraction;
442 fractype b_fraction;
444 if (isnan (a))
446 return a;
448 if (isnan (b))
450 return b;
452 if (isinf (a))
454 /* Adding infinities with opposite signs yields a NaN. */
455 if (isinf (b) && a->sign != b->sign)
456 return nan ();
457 return a;
459 if (isinf (b))
461 return b;
463 if (iszero (b))
465 if (iszero (a))
467 *tmp = *a;
468 tmp->sign = a->sign & b->sign;
469 return tmp;
471 return a;
473 if (iszero (a))
475 return b;
478 /* Got two numbers. shift the smaller and increment the exponent till
479 they're the same */
481 int diff;
483 a_normal_exp = a->normal_exp;
484 b_normal_exp = b->normal_exp;
485 a_fraction = a->fraction.ll;
486 b_fraction = b->fraction.ll;
488 diff = a_normal_exp - b_normal_exp;
490 if (diff < 0)
491 diff = -diff;
492 if (diff < FRAC_NBITS)
494 /* ??? This does shifts one bit at a time. Optimize. */
495 while (a_normal_exp > b_normal_exp)
497 b_normal_exp++;
498 LSHIFT (b_fraction);
500 while (b_normal_exp > a_normal_exp)
502 a_normal_exp++;
503 LSHIFT (a_fraction);
506 else
508 /* Somethings's up.. choose the biggest */
509 if (a_normal_exp > b_normal_exp)
511 b_normal_exp = a_normal_exp;
512 b_fraction = 0;
514 else
516 a_normal_exp = b_normal_exp;
517 a_fraction = 0;
522 if (a->sign != b->sign)
524 if (a->sign)
526 tfraction = -a_fraction + b_fraction;
528 else
530 tfraction = a_fraction - b_fraction;
532 if (tfraction >= 0)
534 tmp->sign = 0;
535 tmp->normal_exp = a_normal_exp;
536 tmp->fraction.ll = tfraction;
538 else
540 tmp->sign = 1;
541 tmp->normal_exp = a_normal_exp;
542 tmp->fraction.ll = -tfraction;
544 /* and renormalize it */
546 while (tmp->fraction.ll < IMPLICIT_1 && tmp->fraction.ll)
548 tmp->fraction.ll <<= 1;
549 tmp->normal_exp--;
552 else
554 tmp->sign = a->sign;
555 tmp->normal_exp = a_normal_exp;
556 tmp->fraction.ll = a_fraction + b_fraction;
558 tmp->class = CLASS_NUMBER;
559 /* Now the fraction is added, we have to shift down to renormalize the
560 number */
562 if (tmp->fraction.ll >= IMPLICIT_2)
564 LSHIFT (tmp->fraction.ll);
565 tmp->normal_exp++;
567 return tmp;
571 FLO_type
572 add (FLO_type arg_a, FLO_type arg_b)
574 fp_number_type a;
575 fp_number_type b;
576 fp_number_type tmp;
577 fp_number_type *res;
578 FLO_union_type au, bu;
580 au.value = arg_a;
581 bu.value = arg_b;
583 unpack_d (&au, &a);
584 unpack_d (&bu, &b);
586 res = _fpadd_parts (&a, &b, &tmp);
588 return pack_d (res);
591 FLO_type
592 sub (FLO_type arg_a, FLO_type arg_b)
594 fp_number_type a;
595 fp_number_type b;
596 fp_number_type tmp;
597 fp_number_type *res;
598 FLO_union_type au, bu;
600 au.value = arg_a;
601 bu.value = arg_b;
603 unpack_d (&au, &a);
604 unpack_d (&bu, &b);
606 b.sign ^= 1;
608 res = _fpadd_parts (&a, &b, &tmp);
610 return pack_d (res);
612 #endif /* L_addsub_sf || L_addsub_df */
614 #if defined(L_mul_sf) || defined(L_mul_df)
615 static inline __attribute__ ((__always_inline__)) fp_number_type *
616 _fpmul_parts ( fp_number_type * a,
617 fp_number_type * b,
618 fp_number_type * tmp)
620 fractype low = 0;
621 fractype high = 0;
623 if (isnan (a))
625 a->sign = a->sign != b->sign;
626 return a;
628 if (isnan (b))
630 b->sign = a->sign != b->sign;
631 return b;
633 if (isinf (a))
635 if (iszero (b))
636 return nan ();
637 a->sign = a->sign != b->sign;
638 return a;
640 if (isinf (b))
642 if (iszero (a))
644 return nan ();
646 b->sign = a->sign != b->sign;
647 return b;
649 if (iszero (a))
651 a->sign = a->sign != b->sign;
652 return a;
654 if (iszero (b))
656 b->sign = a->sign != b->sign;
657 return b;
660 /* Calculate the mantissa by multiplying both numbers to get a
661 twice-as-wide number. */
663 #if defined(NO_DI_MODE)
665 fractype x = a->fraction.ll;
666 fractype ylow = b->fraction.ll;
667 fractype yhigh = 0;
668 int bit;
670 /* ??? This does multiplies one bit at a time. Optimize. */
671 for (bit = 0; bit < FRAC_NBITS; bit++)
673 int carry;
675 if (x & 1)
677 carry = (low += ylow) < ylow;
678 high += yhigh + carry;
680 yhigh <<= 1;
681 if (ylow & FRACHIGH)
683 yhigh |= 1;
685 ylow <<= 1;
686 x >>= 1;
689 #elif defined(FLOAT)
690 /* Multiplying two USIs to get a UDI, we're safe. */
692 UDItype answer = (UDItype)a->fraction.ll * (UDItype)b->fraction.ll;
694 high = answer >> BITS_PER_SI;
695 low = answer;
697 #else
698 /* fractype is DImode, but we need the result to be twice as wide.
699 Assuming a widening multiply from DImode to TImode is not
700 available, build one by hand. */
702 USItype nl = a->fraction.ll;
703 USItype nh = a->fraction.ll >> BITS_PER_SI;
704 USItype ml = b->fraction.ll;
705 USItype mh = b->fraction.ll >> BITS_PER_SI;
706 UDItype pp_ll = (UDItype) ml * nl;
707 UDItype pp_hl = (UDItype) mh * nl;
708 UDItype pp_lh = (UDItype) ml * nh;
709 UDItype pp_hh = (UDItype) mh * nh;
710 UDItype res2 = 0;
711 UDItype res0 = 0;
712 UDItype ps_hh__ = pp_hl + pp_lh;
713 if (ps_hh__ < pp_hl)
714 res2 += (UDItype)1 << BITS_PER_SI;
715 pp_hl = (UDItype)(USItype)ps_hh__ << BITS_PER_SI;
716 res0 = pp_ll + pp_hl;
717 if (res0 < pp_ll)
718 res2++;
719 res2 += (ps_hh__ >> BITS_PER_SI) + pp_hh;
720 high = res2;
721 low = res0;
723 #endif
726 tmp->normal_exp = a->normal_exp + b->normal_exp;
727 tmp->sign = a->sign != b->sign;
728 #ifdef FLOAT
729 tmp->normal_exp += 2; /* ??????????????? */
730 #else
731 tmp->normal_exp += 4; /* ??????????????? */
732 #endif
733 while (high >= IMPLICIT_2)
735 tmp->normal_exp++;
736 if (high & 1)
738 low >>= 1;
739 low |= FRACHIGH;
741 high >>= 1;
743 while (high < IMPLICIT_1)
745 tmp->normal_exp--;
747 high <<= 1;
748 if (low & FRACHIGH)
749 high |= 1;
750 low <<= 1;
752 /* rounding is tricky. if we only round if it won't make us round later. */
753 #if 0
754 if (low & FRACHIGH2)
756 if (((high & GARDMASK) != GARDMSB)
757 && (((high + 1) & GARDMASK) == GARDMSB))
759 /* don't round, it gets done again later. */
761 else
763 high++;
766 #endif
767 if (!ROUND_TOWARDS_ZERO && (high & GARDMASK) == GARDMSB)
769 if (high & (1 << NGARDS))
771 /* half way, so round to even */
772 high += GARDROUND + 1;
774 else if (low)
776 /* but we really weren't half way */
777 high += GARDROUND + 1;
780 tmp->fraction.ll = high;
781 tmp->class = CLASS_NUMBER;
782 return tmp;
785 FLO_type
786 multiply (FLO_type arg_a, FLO_type arg_b)
788 fp_number_type a;
789 fp_number_type b;
790 fp_number_type tmp;
791 fp_number_type *res;
792 FLO_union_type au, bu;
794 au.value = arg_a;
795 bu.value = arg_b;
797 unpack_d (&au, &a);
798 unpack_d (&bu, &b);
800 res = _fpmul_parts (&a, &b, &tmp);
802 return pack_d (res);
804 #endif /* L_mul_sf || L_mul_df */
806 #if defined(L_div_sf) || defined(L_div_df)
807 static inline __attribute__ ((__always_inline__)) fp_number_type *
808 _fpdiv_parts (fp_number_type * a,
809 fp_number_type * b)
811 fractype bit;
812 fractype numerator;
813 fractype denominator;
814 fractype quotient;
816 if (isnan (a))
818 return a;
820 if (isnan (b))
822 return b;
825 a->sign = a->sign ^ b->sign;
827 if (isinf (a) || iszero (a))
829 if (a->class == b->class)
830 return nan ();
831 return a;
834 if (isinf (b))
836 a->fraction.ll = 0;
837 a->normal_exp = 0;
838 return a;
840 if (iszero (b))
842 a->class = CLASS_INFINITY;
843 return a;
846 /* Calculate the mantissa by multiplying both 64bit numbers to get a
847 128 bit number */
849 /* quotient =
850 ( numerator / denominator) * 2^(numerator exponent - denominator exponent)
853 a->normal_exp = a->normal_exp - b->normal_exp;
854 numerator = a->fraction.ll;
855 denominator = b->fraction.ll;
857 if (numerator < denominator)
859 /* Fraction will be less than 1.0 */
860 numerator *= 2;
861 a->normal_exp--;
863 bit = IMPLICIT_1;
864 quotient = 0;
865 /* ??? Does divide one bit at a time. Optimize. */
866 while (bit)
868 if (numerator >= denominator)
870 quotient |= bit;
871 numerator -= denominator;
873 bit >>= 1;
874 numerator *= 2;
877 if (!ROUND_TOWARDS_ZERO && (quotient & GARDMASK) == GARDMSB)
879 if (quotient & (1 << NGARDS))
881 /* half way, so round to even */
882 quotient += GARDROUND + 1;
884 else if (numerator)
886 /* but we really weren't half way, more bits exist */
887 quotient += GARDROUND + 1;
891 a->fraction.ll = quotient;
892 return (a);
896 FLO_type
897 divide (FLO_type arg_a, FLO_type arg_b)
899 fp_number_type a;
900 fp_number_type b;
901 fp_number_type *res;
902 FLO_union_type au, bu;
904 au.value = arg_a;
905 bu.value = arg_b;
907 unpack_d (&au, &a);
908 unpack_d (&bu, &b);
910 res = _fpdiv_parts (&a, &b);
912 return pack_d (res);
914 #endif /* L_div_sf || L_div_df */
916 #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df)
917 /* according to the demo, fpcmp returns a comparison with 0... thus
918 a<b -> -1
919 a==b -> 0
920 a>b -> +1
924 __fpcmp_parts (fp_number_type * a, fp_number_type * b)
926 #if 0
927 /* either nan -> unordered. Must be checked outside of this routine. */
928 if (isnan (a) && isnan (b))
930 return 1; /* still unordered! */
932 #endif
934 if (isnan (a) || isnan (b))
936 return 1; /* how to indicate unordered compare? */
938 if (isinf (a) && isinf (b))
940 /* +inf > -inf, but +inf != +inf */
941 /* b \a| +inf(0)| -inf(1)
942 ______\+--------+--------
943 +inf(0)| a==b(0)| a<b(-1)
944 -------+--------+--------
945 -inf(1)| a>b(1) | a==b(0)
946 -------+--------+--------
947 So since unordered must be nonzero, just line up the columns...
949 return b->sign - a->sign;
951 /* but not both... */
952 if (isinf (a))
954 return a->sign ? -1 : 1;
956 if (isinf (b))
958 return b->sign ? 1 : -1;
960 if (iszero (a) && iszero (b))
962 return 0;
964 if (iszero (a))
966 return b->sign ? 1 : -1;
968 if (iszero (b))
970 return a->sign ? -1 : 1;
972 /* now both are "normal". */
973 if (a->sign != b->sign)
975 /* opposite signs */
976 return a->sign ? -1 : 1;
978 /* same sign; exponents? */
979 if (a->normal_exp > b->normal_exp)
981 return a->sign ? -1 : 1;
983 if (a->normal_exp < b->normal_exp)
985 return a->sign ? 1 : -1;
987 /* same exponents; check size. */
988 if (a->fraction.ll > b->fraction.ll)
990 return a->sign ? -1 : 1;
992 if (a->fraction.ll < b->fraction.ll)
994 return a->sign ? 1 : -1;
996 /* after all that, they're equal. */
997 return 0;
999 #endif
1001 #if defined(L_compare_sf) || defined(L_compare_df)
1002 CMPtype
1003 compare (FLO_type arg_a, FLO_type arg_b)
1005 fp_number_type a;
1006 fp_number_type b;
1007 FLO_union_type au, bu;
1009 au.value = arg_a;
1010 bu.value = arg_b;
1012 unpack_d (&au, &a);
1013 unpack_d (&bu, &b);
1015 return __fpcmp_parts (&a, &b);
1017 #endif /* L_compare_sf || L_compare_df */
1019 #ifndef US_SOFTWARE_GOFAST
1021 /* These should be optimized for their specific tasks someday. */
1023 #if defined(L_eq_sf) || defined(L_eq_df)
1024 CMPtype
1025 _eq_f2 (FLO_type arg_a, FLO_type arg_b)
1027 fp_number_type a;
1028 fp_number_type b;
1029 FLO_union_type au, bu;
1031 au.value = arg_a;
1032 bu.value = arg_b;
1034 unpack_d (&au, &a);
1035 unpack_d (&bu, &b);
1037 if (isnan (&a) || isnan (&b))
1038 return 1; /* false, truth == 0 */
1040 return __fpcmp_parts (&a, &b) ;
1042 #endif /* L_eq_sf || L_eq_df */
1044 #if defined(L_ne_sf) || defined(L_ne_df)
1045 CMPtype
1046 _ne_f2 (FLO_type arg_a, FLO_type arg_b)
1048 fp_number_type a;
1049 fp_number_type b;
1050 FLO_union_type au, bu;
1052 au.value = arg_a;
1053 bu.value = arg_b;
1055 unpack_d (&au, &a);
1056 unpack_d (&bu, &b);
1058 if (isnan (&a) || isnan (&b))
1059 return 1; /* true, truth != 0 */
1061 return __fpcmp_parts (&a, &b) ;
1063 #endif /* L_ne_sf || L_ne_df */
1065 #if defined(L_gt_sf) || defined(L_gt_df)
1066 CMPtype
1067 _gt_f2 (FLO_type arg_a, FLO_type arg_b)
1069 fp_number_type a;
1070 fp_number_type b;
1071 FLO_union_type au, bu;
1073 au.value = arg_a;
1074 bu.value = arg_b;
1076 unpack_d (&au, &a);
1077 unpack_d (&bu, &b);
1079 if (isnan (&a) || isnan (&b))
1080 return -1; /* false, truth > 0 */
1082 return __fpcmp_parts (&a, &b);
1084 #endif /* L_gt_sf || L_gt_df */
1086 #if defined(L_ge_sf) || defined(L_ge_df)
1087 CMPtype
1088 _ge_f2 (FLO_type arg_a, FLO_type arg_b)
1090 fp_number_type a;
1091 fp_number_type b;
1092 FLO_union_type au, bu;
1094 au.value = arg_a;
1095 bu.value = arg_b;
1097 unpack_d (&au, &a);
1098 unpack_d (&bu, &b);
1100 if (isnan (&a) || isnan (&b))
1101 return -1; /* false, truth >= 0 */
1102 return __fpcmp_parts (&a, &b) ;
1104 #endif /* L_ge_sf || L_ge_df */
1106 #if defined(L_lt_sf) || defined(L_lt_df)
1107 CMPtype
1108 _lt_f2 (FLO_type arg_a, FLO_type arg_b)
1110 fp_number_type a;
1111 fp_number_type b;
1112 FLO_union_type au, bu;
1114 au.value = arg_a;
1115 bu.value = arg_b;
1117 unpack_d (&au, &a);
1118 unpack_d (&bu, &b);
1120 if (isnan (&a) || isnan (&b))
1121 return 1; /* false, truth < 0 */
1123 return __fpcmp_parts (&a, &b);
1125 #endif /* L_lt_sf || L_lt_df */
1127 #if defined(L_le_sf) || defined(L_le_df)
1128 CMPtype
1129 _le_f2 (FLO_type arg_a, FLO_type arg_b)
1131 fp_number_type a;
1132 fp_number_type b;
1133 FLO_union_type au, bu;
1135 au.value = arg_a;
1136 bu.value = arg_b;
1138 unpack_d (&au, &a);
1139 unpack_d (&bu, &b);
1141 if (isnan (&a) || isnan (&b))
1142 return 1; /* false, truth <= 0 */
1144 return __fpcmp_parts (&a, &b) ;
1146 #endif /* L_le_sf || L_le_df */
1148 #endif /* ! US_SOFTWARE_GOFAST */
1150 #if defined(L_unord_sf) || defined(L_unord_df)
1151 CMPtype
1152 _unord_f2 (FLO_type arg_a, FLO_type arg_b)
1154 fp_number_type a;
1155 fp_number_type b;
1156 FLO_union_type au, bu;
1158 au.value = arg_a;
1159 bu.value = arg_b;
1161 unpack_d (&au, &a);
1162 unpack_d (&bu, &b);
1164 return (isnan (&a) || isnan (&b));
1166 #endif /* L_unord_sf || L_unord_df */
1168 #if defined(L_si_to_sf) || defined(L_si_to_df)
1169 FLO_type
1170 si_to_float (SItype arg_a)
1172 fp_number_type in;
1174 in.class = CLASS_NUMBER;
1175 in.sign = arg_a < 0;
1176 if (!arg_a)
1178 in.class = CLASS_ZERO;
1180 else
1182 in.normal_exp = FRACBITS + NGARDS;
1183 if (in.sign)
1185 /* Special case for minint, since there is no +ve integer
1186 representation for it */
1187 if (arg_a == (- MAX_SI_INT - 1))
1189 return (FLO_type)(- MAX_SI_INT - 1);
1191 in.fraction.ll = (-arg_a);
1193 else
1194 in.fraction.ll = arg_a;
1196 while (in.fraction.ll < (1LL << (FRACBITS + NGARDS)))
1198 in.fraction.ll <<= 1;
1199 in.normal_exp -= 1;
1202 return pack_d (&in);
1204 #endif /* L_si_to_sf || L_si_to_df */
1206 #if defined(L_usi_to_sf) || defined(L_usi_to_df)
1207 FLO_type
1208 usi_to_float (USItype arg_a)
1210 fp_number_type in;
1212 in.sign = 0;
1213 if (!arg_a)
1215 in.class = CLASS_ZERO;
1217 else
1219 in.class = CLASS_NUMBER;
1220 in.normal_exp = FRACBITS + NGARDS;
1221 in.fraction.ll = arg_a;
1223 while (in.fraction.ll > (1LL << (FRACBITS + NGARDS)))
1225 in.fraction.ll >>= 1;
1226 in.normal_exp += 1;
1228 while (in.fraction.ll < (1LL << (FRACBITS + NGARDS)))
1230 in.fraction.ll <<= 1;
1231 in.normal_exp -= 1;
1234 return pack_d (&in);
1236 #endif
1238 #if defined(L_sf_to_si) || defined(L_df_to_si)
1239 SItype
1240 float_to_si (FLO_type arg_a)
1242 fp_number_type a;
1243 SItype tmp;
1244 FLO_union_type au;
1246 au.value = arg_a;
1247 unpack_d (&au, &a);
1249 if (iszero (&a))
1250 return 0;
1251 if (isnan (&a))
1252 return 0;
1253 /* get reasonable MAX_SI_INT... */
1254 if (isinf (&a))
1255 return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1256 /* it is a number, but a small one */
1257 if (a.normal_exp < 0)
1258 return 0;
1259 if (a.normal_exp > BITS_PER_SI - 2)
1260 return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1261 tmp = a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1262 return a.sign ? (-tmp) : (tmp);
1264 #endif /* L_sf_to_si || L_df_to_si */
1266 #if defined(L_sf_to_usi) || defined(L_df_to_usi)
1267 #ifdef US_SOFTWARE_GOFAST
1268 /* While libgcc2.c defines its own __fixunssfsi and __fixunsdfsi routines,
1269 we also define them for GOFAST because the ones in libgcc2.c have the
1270 wrong names and I'd rather define these here and keep GOFAST CYG-LOC's
1271 out of libgcc2.c. We can't define these here if not GOFAST because then
1272 there'd be duplicate copies. */
1274 USItype
1275 float_to_usi (FLO_type arg_a)
1277 fp_number_type a;
1278 FLO_union_type au;
1280 au.value = arg_a;
1281 unpack_d (&au, &a);
1283 if (iszero (&a))
1284 return 0;
1285 if (isnan (&a))
1286 return 0;
1287 /* it is a negative number */
1288 if (a.sign)
1289 return 0;
1290 /* get reasonable MAX_USI_INT... */
1291 if (isinf (&a))
1292 return MAX_USI_INT;
1293 /* it is a number, but a small one */
1294 if (a.normal_exp < 0)
1295 return 0;
1296 if (a.normal_exp > BITS_PER_SI - 1)
1297 return MAX_USI_INT;
1298 else if (a.normal_exp > (FRACBITS + NGARDS))
1299 return a.fraction.ll << (a.normal_exp - (FRACBITS + NGARDS));
1300 else
1301 return a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1303 #endif /* US_SOFTWARE_GOFAST */
1304 #endif /* L_sf_to_usi || L_df_to_usi */
1306 #if defined(L_negate_sf) || defined(L_negate_df)
1307 FLO_type
1308 negate (FLO_type arg_a)
1310 fp_number_type a;
1311 FLO_union_type au;
1313 au.value = arg_a;
1314 unpack_d (&au, &a);
1316 flip_sign (&a);
1317 return pack_d (&a);
1319 #endif /* L_negate_sf || L_negate_df */
1321 #ifdef FLOAT
1323 #if defined(L_make_sf)
1324 SFtype
1325 __make_fp(fp_class_type class,
1326 unsigned int sign,
1327 int exp,
1328 USItype frac)
1330 fp_number_type in;
1332 in.class = class;
1333 in.sign = sign;
1334 in.normal_exp = exp;
1335 in.fraction.ll = frac;
1336 return pack_d (&in);
1338 #endif /* L_make_sf */
1340 #ifndef FLOAT_ONLY
1342 /* This enables one to build an fp library that supports float but not double.
1343 Otherwise, we would get an undefined reference to __make_dp.
1344 This is needed for some 8-bit ports that can't handle well values that
1345 are 8-bytes in size, so we just don't support double for them at all. */
1347 #if defined(L_sf_to_df)
1348 DFtype
1349 sf_to_df (SFtype arg_a)
1351 fp_number_type in;
1352 FLO_union_type au;
1354 au.value = arg_a;
1355 unpack_d (&au, &in);
1357 return __make_dp (in.class, in.sign, in.normal_exp,
1358 ((UDItype) in.fraction.ll) << F_D_BITOFF);
1360 #endif /* L_sf_to_df */
1362 #endif /* ! FLOAT_ONLY */
1363 #endif /* FLOAT */
1365 #ifndef FLOAT
1367 extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
1369 #if defined(L_make_df)
1370 DFtype
1371 __make_dp (fp_class_type class, unsigned int sign, int exp, UDItype frac)
1373 fp_number_type in;
1375 in.class = class;
1376 in.sign = sign;
1377 in.normal_exp = exp;
1378 in.fraction.ll = frac;
1379 return pack_d (&in);
1381 #endif /* L_make_df */
1383 #if defined(L_df_to_sf)
1384 SFtype
1385 df_to_sf (DFtype arg_a)
1387 fp_number_type in;
1388 USItype sffrac;
1389 FLO_union_type au;
1391 au.value = arg_a;
1392 unpack_d (&au, &in);
1394 sffrac = in.fraction.ll >> F_D_BITOFF;
1396 /* We set the lowest guard bit in SFFRAC if we discarded any non
1397 zero bits. */
1398 if ((in.fraction.ll & (((USItype) 1 << F_D_BITOFF) - 1)) != 0)
1399 sffrac |= 1;
1401 return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
1403 #endif /* L_df_to_sf */
1405 #endif /* ! FLOAT */
1406 #endif /* !EXTENDED_FLOAT_STUBS */