* config/i386/netbsd-elf.h (TARGET_OS_CPP_BUILTINS): Define.
[official-gcc.git] / gcc / config / fp-bit.c
blob5da7a8efe13884839f39fa7cfba028507dda67a6
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 behaviour 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 EXTENDED_FLOAT_STUBS
78 __truncxfsf2 (){ abort(); }
79 __extendsfxf2 (){ abort(); }
80 __addxf3 (){ abort(); }
81 __divxf3 (){ abort(); }
82 __eqxf2 (){ abort(); }
83 __extenddfxf2 (){ abort(); }
84 __gtxf2 (){ abort(); }
85 __lexf2 (){ abort(); }
86 __ltxf2 (){ abort(); }
87 __mulxf3 (){ abort(); }
88 __negxf2 (){ abort(); }
89 __nexf2 (){ abort(); }
90 __subxf3 (){ abort(); }
91 __truncxfdf2 (){ abort(); }
93 __trunctfsf2 (){ abort(); }
94 __extendsftf2 (){ abort(); }
95 __addtf3 (){ abort(); }
96 __divtf3 (){ abort(); }
97 __eqtf2 (){ abort(); }
98 __extenddftf2 (){ abort(); }
99 __gttf2 (){ abort(); }
100 __letf2 (){ abort(); }
101 __lttf2 (){ abort(); }
102 __multf3 (){ abort(); }
103 __negtf2 (){ abort(); }
104 __netf2 (){ abort(); }
105 __subtf3 (){ abort(); }
106 __trunctfdf2 (){ abort(); }
107 __gexf2 (){ abort(); }
108 __fixxfsi (){ abort(); }
109 __floatsixf (){ abort(); }
110 #else /* !EXTENDED_FLOAT_STUBS, rest of file */
112 /* IEEE "special" number predicates */
114 #ifdef NO_NANS
116 #define nan() 0
117 #define isnan(x) 0
118 #define isinf(x) 0
119 #else
121 #if defined L_thenan_sf
122 const fp_number_type __thenan_sf = { CLASS_SNAN, 0, 0, {(fractype) 0} };
123 #elif defined L_thenan_df
124 const fp_number_type __thenan_df = { CLASS_SNAN, 0, 0, {(fractype) 0} };
125 #elif defined FLOAT
126 extern const fp_number_type __thenan_sf;
127 #else
128 extern const fp_number_type __thenan_df;
129 #endif
131 INLINE
132 static fp_number_type *
133 nan (void)
135 /* Discard the const qualifier... */
136 #ifdef FLOAT
137 return (fp_number_type *) (& __thenan_sf);
138 #else
139 return (fp_number_type *) (& __thenan_df);
140 #endif
143 INLINE
144 static int
145 isnan ( fp_number_type * x)
147 return x->class == CLASS_SNAN || x->class == CLASS_QNAN;
150 INLINE
151 static int
152 isinf ( fp_number_type * x)
154 return x->class == CLASS_INFINITY;
157 #endif /* NO_NANS */
159 INLINE
160 static int
161 iszero ( fp_number_type * x)
163 return x->class == CLASS_ZERO;
166 INLINE
167 static void
168 flip_sign ( fp_number_type * x)
170 x->sign = !x->sign;
173 extern FLO_type pack_d ( fp_number_type * );
175 #if defined(L_pack_df) || defined(L_pack_sf)
176 FLO_type
177 pack_d ( fp_number_type * src)
179 FLO_union_type dst;
180 fractype fraction = src->fraction.ll; /* wasn't unsigned before? */
181 int sign = src->sign;
182 int exp = 0;
184 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && (isnan (src) || isinf (src)))
186 /* We can't represent these values accurately. By using the
187 largest possible magnitude, we guarantee that the conversion
188 of infinity is at least as big as any finite number. */
189 exp = EXPMAX;
190 fraction = ((fractype) 1 << FRACBITS) - 1;
192 else if (isnan (src))
194 exp = EXPMAX;
195 if (src->class == CLASS_QNAN || 1)
197 fraction |= QUIET_NAN;
200 else if (isinf (src))
202 exp = EXPMAX;
203 fraction = 0;
205 else if (iszero (src))
207 exp = 0;
208 fraction = 0;
210 else if (fraction == 0)
212 exp = 0;
214 else
216 if (src->normal_exp < NORMAL_EXPMIN)
218 #ifdef NO_DENORMALS
219 /* Go straight to a zero representation if denormals are not
220 supported. The denormal handling would be harmless but
221 isn't unnecessary. */
222 exp = 0;
223 fraction = 0;
224 #else /* NO_DENORMALS */
225 /* This number's exponent is too low to fit into the bits
226 available in the number, so we'll store 0 in the exponent and
227 shift the fraction to the right to make up for it. */
229 int shift = NORMAL_EXPMIN - src->normal_exp;
231 exp = 0;
233 if (shift > FRAC_NBITS - NGARDS)
235 /* No point shifting, since it's more that 64 out. */
236 fraction = 0;
238 else
240 int lowbit = (fraction & (((fractype)1 << shift) - 1)) ? 1 : 0;
241 fraction = (fraction >> shift) | lowbit;
243 if ((fraction & GARDMASK) == GARDMSB)
245 if ((fraction & (1 << NGARDS)))
246 fraction += GARDROUND + 1;
248 else
250 /* Add to the guards to round up. */
251 fraction += GARDROUND;
253 /* Perhaps the rounding means we now need to change the
254 exponent, because the fraction is no longer denormal. */
255 if (fraction >= IMPLICIT_1)
257 exp += 1;
259 fraction >>= NGARDS;
260 #endif /* NO_DENORMALS */
262 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS)
263 && src->normal_exp > EXPBIAS)
265 exp = EXPMAX;
266 fraction = 0;
268 else
270 exp = src->normal_exp + EXPBIAS;
271 if (!ROUND_TOWARDS_ZERO)
273 /* IF the gard bits are the all zero, but the first, then we're
274 half way between two numbers, choose the one which makes the
275 lsb of the answer 0. */
276 if ((fraction & GARDMASK) == GARDMSB)
278 if (fraction & (1 << NGARDS))
279 fraction += GARDROUND + 1;
281 else
283 /* Add a one to the guards to round up */
284 fraction += GARDROUND;
286 if (fraction >= IMPLICIT_2)
288 fraction >>= 1;
289 exp += 1;
292 fraction >>= NGARDS;
294 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && exp > EXPMAX)
296 /* Saturate on overflow. */
297 exp = EXPMAX;
298 fraction = ((fractype) 1 << FRACBITS) - 1;
303 /* We previously used bitfields to store the number, but this doesn't
304 handle little/big endian systems conveniently, so use shifts and
305 masks */
306 #ifdef FLOAT_BIT_ORDER_MISMATCH
307 dst.bits.fraction = fraction;
308 dst.bits.exp = exp;
309 dst.bits.sign = sign;
310 #else
311 dst.value_raw = fraction & ((((fractype)1) << FRACBITS) - (fractype)1);
312 dst.value_raw |= ((fractype) (exp & ((1 << EXPBITS) - 1))) << FRACBITS;
313 dst.value_raw |= ((fractype) (sign & 1)) << (FRACBITS | EXPBITS);
314 #endif
316 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
318 halffractype tmp = dst.words[0];
319 dst.words[0] = dst.words[1];
320 dst.words[1] = tmp;
322 #endif
324 return dst.value;
326 #endif
328 #if defined(L_unpack_df) || defined(L_unpack_sf)
329 void
330 unpack_d (FLO_union_type * src, fp_number_type * dst)
332 /* We previously used bitfields to store the number, but this doesn't
333 handle little/big endian systems conveniently, so use shifts and
334 masks */
335 fractype fraction;
336 int exp;
337 int sign;
339 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
340 FLO_union_type swapped;
342 swapped.words[0] = src->words[1];
343 swapped.words[1] = src->words[0];
344 src = &swapped;
345 #endif
347 #ifdef FLOAT_BIT_ORDER_MISMATCH
348 fraction = src->bits.fraction;
349 exp = src->bits.exp;
350 sign = src->bits.sign;
351 #else
352 fraction = src->value_raw & ((((fractype)1) << FRACBITS) - (fractype)1);
353 exp = ((int)(src->value_raw >> FRACBITS)) & ((1 << EXPBITS) - 1);
354 sign = ((int)(src->value_raw >> (FRACBITS + EXPBITS))) & 1;
355 #endif
357 dst->sign = sign;
358 if (exp == 0)
360 /* Hmm. Looks like 0 */
361 if (fraction == 0
362 #ifdef NO_DENORMALS
363 || 1
364 #endif
367 /* tastes like zero */
368 dst->class = CLASS_ZERO;
370 else
372 /* Zero exponent with non zero fraction - it's denormalized,
373 so there isn't a leading implicit one - we'll shift it so
374 it gets one. */
375 dst->normal_exp = exp - EXPBIAS + 1;
376 fraction <<= NGARDS;
378 dst->class = CLASS_NUMBER;
379 #if 1
380 while (fraction < IMPLICIT_1)
382 fraction <<= 1;
383 dst->normal_exp--;
385 #endif
386 dst->fraction.ll = fraction;
389 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && exp == EXPMAX)
391 /* Huge exponent*/
392 if (fraction == 0)
394 /* Attached to a zero fraction - means infinity */
395 dst->class = CLASS_INFINITY;
397 else
399 /* Non zero fraction, means nan */
400 if (fraction & QUIET_NAN)
402 dst->class = CLASS_QNAN;
404 else
406 dst->class = CLASS_SNAN;
408 /* Keep the fraction part as the nan number */
409 dst->fraction.ll = fraction;
412 else
414 /* Nothing strange about this number */
415 dst->normal_exp = exp - EXPBIAS;
416 dst->class = CLASS_NUMBER;
417 dst->fraction.ll = (fraction << NGARDS) | IMPLICIT_1;
420 #endif /* L_unpack_df || L_unpack_sf */
422 #if defined(L_addsub_sf) || defined(L_addsub_df)
423 static fp_number_type *
424 _fpadd_parts (fp_number_type * a,
425 fp_number_type * b,
426 fp_number_type * tmp)
428 intfrac tfraction;
430 /* Put commonly used fields in local variables. */
431 int a_normal_exp;
432 int b_normal_exp;
433 fractype a_fraction;
434 fractype b_fraction;
436 if (isnan (a))
438 return a;
440 if (isnan (b))
442 return b;
444 if (isinf (a))
446 /* Adding infinities with opposite signs yields a NaN. */
447 if (isinf (b) && a->sign != b->sign)
448 return nan ();
449 return a;
451 if (isinf (b))
453 return b;
455 if (iszero (b))
457 if (iszero (a))
459 *tmp = *a;
460 tmp->sign = a->sign & b->sign;
461 return tmp;
463 return a;
465 if (iszero (a))
467 return b;
470 /* Got two numbers. shift the smaller and increment the exponent till
471 they're the same */
473 int diff;
475 a_normal_exp = a->normal_exp;
476 b_normal_exp = b->normal_exp;
477 a_fraction = a->fraction.ll;
478 b_fraction = b->fraction.ll;
480 diff = a_normal_exp - b_normal_exp;
482 if (diff < 0)
483 diff = -diff;
484 if (diff < FRAC_NBITS)
486 /* ??? This does shifts one bit at a time. Optimize. */
487 while (a_normal_exp > b_normal_exp)
489 b_normal_exp++;
490 LSHIFT (b_fraction);
492 while (b_normal_exp > a_normal_exp)
494 a_normal_exp++;
495 LSHIFT (a_fraction);
498 else
500 /* Somethings's up.. choose the biggest */
501 if (a_normal_exp > b_normal_exp)
503 b_normal_exp = a_normal_exp;
504 b_fraction = 0;
506 else
508 a_normal_exp = b_normal_exp;
509 a_fraction = 0;
514 if (a->sign != b->sign)
516 if (a->sign)
518 tfraction = -a_fraction + b_fraction;
520 else
522 tfraction = a_fraction - b_fraction;
524 if (tfraction >= 0)
526 tmp->sign = 0;
527 tmp->normal_exp = a_normal_exp;
528 tmp->fraction.ll = tfraction;
530 else
532 tmp->sign = 1;
533 tmp->normal_exp = a_normal_exp;
534 tmp->fraction.ll = -tfraction;
536 /* and renormalize it */
538 while (tmp->fraction.ll < IMPLICIT_1 && tmp->fraction.ll)
540 tmp->fraction.ll <<= 1;
541 tmp->normal_exp--;
544 else
546 tmp->sign = a->sign;
547 tmp->normal_exp = a_normal_exp;
548 tmp->fraction.ll = a_fraction + b_fraction;
550 tmp->class = CLASS_NUMBER;
551 /* Now the fraction is added, we have to shift down to renormalize the
552 number */
554 if (tmp->fraction.ll >= IMPLICIT_2)
556 LSHIFT (tmp->fraction.ll);
557 tmp->normal_exp++;
559 return tmp;
563 FLO_type
564 add (FLO_type arg_a, FLO_type arg_b)
566 fp_number_type a;
567 fp_number_type b;
568 fp_number_type tmp;
569 fp_number_type *res;
570 FLO_union_type au, bu;
572 au.value = arg_a;
573 bu.value = arg_b;
575 unpack_d (&au, &a);
576 unpack_d (&bu, &b);
578 res = _fpadd_parts (&a, &b, &tmp);
580 return pack_d (res);
583 FLO_type
584 sub (FLO_type arg_a, FLO_type arg_b)
586 fp_number_type a;
587 fp_number_type b;
588 fp_number_type tmp;
589 fp_number_type *res;
590 FLO_union_type au, bu;
592 au.value = arg_a;
593 bu.value = arg_b;
595 unpack_d (&au, &a);
596 unpack_d (&bu, &b);
598 b.sign ^= 1;
600 res = _fpadd_parts (&a, &b, &tmp);
602 return pack_d (res);
604 #endif /* L_addsub_sf || L_addsub_df */
606 #if defined(L_mul_sf) || defined(L_mul_df)
607 static INLINE fp_number_type *
608 _fpmul_parts ( fp_number_type * a,
609 fp_number_type * b,
610 fp_number_type * tmp)
612 fractype low = 0;
613 fractype high = 0;
615 if (isnan (a))
617 a->sign = a->sign != b->sign;
618 return a;
620 if (isnan (b))
622 b->sign = a->sign != b->sign;
623 return b;
625 if (isinf (a))
627 if (iszero (b))
628 return nan ();
629 a->sign = a->sign != b->sign;
630 return a;
632 if (isinf (b))
634 if (iszero (a))
636 return nan ();
638 b->sign = a->sign != b->sign;
639 return b;
641 if (iszero (a))
643 a->sign = a->sign != b->sign;
644 return a;
646 if (iszero (b))
648 b->sign = a->sign != b->sign;
649 return b;
652 /* Calculate the mantissa by multiplying both numbers to get a
653 twice-as-wide number. */
655 #if defined(NO_DI_MODE)
657 fractype x = a->fraction.ll;
658 fractype ylow = b->fraction.ll;
659 fractype yhigh = 0;
660 int bit;
662 /* ??? This does multiplies one bit at a time. Optimize. */
663 for (bit = 0; bit < FRAC_NBITS; bit++)
665 int carry;
667 if (x & 1)
669 carry = (low += ylow) < ylow;
670 high += yhigh + carry;
672 yhigh <<= 1;
673 if (ylow & FRACHIGH)
675 yhigh |= 1;
677 ylow <<= 1;
678 x >>= 1;
681 #elif defined(FLOAT)
682 /* Multiplying two USIs to get a UDI, we're safe. */
684 UDItype answer = (UDItype)a->fraction.ll * (UDItype)b->fraction.ll;
686 high = answer >> BITS_PER_SI;
687 low = answer;
689 #else
690 /* fractype is DImode, but we need the result to be twice as wide.
691 Assuming a widening multiply from DImode to TImode is not
692 available, build one by hand. */
694 USItype nl = a->fraction.ll;
695 USItype nh = a->fraction.ll >> BITS_PER_SI;
696 USItype ml = b->fraction.ll;
697 USItype mh = b->fraction.ll >> BITS_PER_SI;
698 UDItype pp_ll = (UDItype) ml * nl;
699 UDItype pp_hl = (UDItype) mh * nl;
700 UDItype pp_lh = (UDItype) ml * nh;
701 UDItype pp_hh = (UDItype) mh * nh;
702 UDItype res2 = 0;
703 UDItype res0 = 0;
704 UDItype ps_hh__ = pp_hl + pp_lh;
705 if (ps_hh__ < pp_hl)
706 res2 += (UDItype)1 << BITS_PER_SI;
707 pp_hl = (UDItype)(USItype)ps_hh__ << BITS_PER_SI;
708 res0 = pp_ll + pp_hl;
709 if (res0 < pp_ll)
710 res2++;
711 res2 += (ps_hh__ >> BITS_PER_SI) + pp_hh;
712 high = res2;
713 low = res0;
715 #endif
718 tmp->normal_exp = a->normal_exp + b->normal_exp;
719 tmp->sign = a->sign != b->sign;
720 #ifdef FLOAT
721 tmp->normal_exp += 2; /* ??????????????? */
722 #else
723 tmp->normal_exp += 4; /* ??????????????? */
724 #endif
725 while (high >= IMPLICIT_2)
727 tmp->normal_exp++;
728 if (high & 1)
730 low >>= 1;
731 low |= FRACHIGH;
733 high >>= 1;
735 while (high < IMPLICIT_1)
737 tmp->normal_exp--;
739 high <<= 1;
740 if (low & FRACHIGH)
741 high |= 1;
742 low <<= 1;
744 /* rounding is tricky. if we only round if it won't make us round later. */
745 #if 0
746 if (low & FRACHIGH2)
748 if (((high & GARDMASK) != GARDMSB)
749 && (((high + 1) & GARDMASK) == GARDMSB))
751 /* don't round, it gets done again later. */
753 else
755 high++;
758 #endif
759 if (!ROUND_TOWARDS_ZERO && (high & GARDMASK) == GARDMSB)
761 if (high & (1 << NGARDS))
763 /* half way, so round to even */
764 high += GARDROUND + 1;
766 else if (low)
768 /* but we really weren't half way */
769 high += GARDROUND + 1;
772 tmp->fraction.ll = high;
773 tmp->class = CLASS_NUMBER;
774 return tmp;
777 FLO_type
778 multiply (FLO_type arg_a, FLO_type arg_b)
780 fp_number_type a;
781 fp_number_type b;
782 fp_number_type tmp;
783 fp_number_type *res;
784 FLO_union_type au, bu;
786 au.value = arg_a;
787 bu.value = arg_b;
789 unpack_d (&au, &a);
790 unpack_d (&bu, &b);
792 res = _fpmul_parts (&a, &b, &tmp);
794 return pack_d (res);
796 #endif /* L_mul_sf || L_mul_df */
798 #if defined(L_div_sf) || defined(L_div_df)
799 static INLINE fp_number_type *
800 _fpdiv_parts (fp_number_type * a,
801 fp_number_type * b)
803 fractype bit;
804 fractype numerator;
805 fractype denominator;
806 fractype quotient;
808 if (isnan (a))
810 return a;
812 if (isnan (b))
814 return b;
817 a->sign = a->sign ^ b->sign;
819 if (isinf (a) || iszero (a))
821 if (a->class == b->class)
822 return nan ();
823 return a;
826 if (isinf (b))
828 a->fraction.ll = 0;
829 a->normal_exp = 0;
830 return a;
832 if (iszero (b))
834 a->class = CLASS_INFINITY;
835 return a;
838 /* Calculate the mantissa by multiplying both 64bit numbers to get a
839 128 bit number */
841 /* quotient =
842 ( numerator / denominator) * 2^(numerator exponent - denominator exponent)
845 a->normal_exp = a->normal_exp - b->normal_exp;
846 numerator = a->fraction.ll;
847 denominator = b->fraction.ll;
849 if (numerator < denominator)
851 /* Fraction will be less than 1.0 */
852 numerator *= 2;
853 a->normal_exp--;
855 bit = IMPLICIT_1;
856 quotient = 0;
857 /* ??? Does divide one bit at a time. Optimize. */
858 while (bit)
860 if (numerator >= denominator)
862 quotient |= bit;
863 numerator -= denominator;
865 bit >>= 1;
866 numerator *= 2;
869 if (!ROUND_TOWARDS_ZERO && (quotient & GARDMASK) == GARDMSB)
871 if (quotient & (1 << NGARDS))
873 /* half way, so round to even */
874 quotient += GARDROUND + 1;
876 else if (numerator)
878 /* but we really weren't half way, more bits exist */
879 quotient += GARDROUND + 1;
883 a->fraction.ll = quotient;
884 return (a);
888 FLO_type
889 divide (FLO_type arg_a, FLO_type arg_b)
891 fp_number_type a;
892 fp_number_type b;
893 fp_number_type *res;
894 FLO_union_type au, bu;
896 au.value = arg_a;
897 bu.value = arg_b;
899 unpack_d (&au, &a);
900 unpack_d (&bu, &b);
902 res = _fpdiv_parts (&a, &b);
904 return pack_d (res);
906 #endif /* L_div_sf || L_div_df */
908 #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df)
909 /* according to the demo, fpcmp returns a comparison with 0... thus
910 a<b -> -1
911 a==b -> 0
912 a>b -> +1
916 __fpcmp_parts (fp_number_type * a, fp_number_type * b)
918 #if 0
919 /* either nan -> unordered. Must be checked outside of this routine. */
920 if (isnan (a) && isnan (b))
922 return 1; /* still unordered! */
924 #endif
926 if (isnan (a) || isnan (b))
928 return 1; /* how to indicate unordered compare? */
930 if (isinf (a) && isinf (b))
932 /* +inf > -inf, but +inf != +inf */
933 /* b \a| +inf(0)| -inf(1)
934 ______\+--------+--------
935 +inf(0)| a==b(0)| a<b(-1)
936 -------+--------+--------
937 -inf(1)| a>b(1) | a==b(0)
938 -------+--------+--------
939 So since unordered must be non zero, just line up the columns...
941 return b->sign - a->sign;
943 /* but not both... */
944 if (isinf (a))
946 return a->sign ? -1 : 1;
948 if (isinf (b))
950 return b->sign ? 1 : -1;
952 if (iszero (a) && iszero (b))
954 return 0;
956 if (iszero (a))
958 return b->sign ? 1 : -1;
960 if (iszero (b))
962 return a->sign ? -1 : 1;
964 /* now both are "normal". */
965 if (a->sign != b->sign)
967 /* opposite signs */
968 return a->sign ? -1 : 1;
970 /* same sign; exponents? */
971 if (a->normal_exp > b->normal_exp)
973 return a->sign ? -1 : 1;
975 if (a->normal_exp < b->normal_exp)
977 return a->sign ? 1 : -1;
979 /* same exponents; check size. */
980 if (a->fraction.ll > b->fraction.ll)
982 return a->sign ? -1 : 1;
984 if (a->fraction.ll < b->fraction.ll)
986 return a->sign ? 1 : -1;
988 /* after all that, they're equal. */
989 return 0;
991 #endif
993 #if defined(L_compare_sf) || defined(L_compare_df)
994 CMPtype
995 compare (FLO_type arg_a, FLO_type arg_b)
997 fp_number_type a;
998 fp_number_type b;
999 FLO_union_type au, bu;
1001 au.value = arg_a;
1002 bu.value = arg_b;
1004 unpack_d (&au, &a);
1005 unpack_d (&bu, &b);
1007 return __fpcmp_parts (&a, &b);
1009 #endif /* L_compare_sf || L_compare_df */
1011 #ifndef US_SOFTWARE_GOFAST
1013 /* These should be optimized for their specific tasks someday. */
1015 #if defined(L_eq_sf) || defined(L_eq_df)
1016 CMPtype
1017 _eq_f2 (FLO_type arg_a, FLO_type arg_b)
1019 fp_number_type a;
1020 fp_number_type b;
1021 FLO_union_type au, bu;
1023 au.value = arg_a;
1024 bu.value = arg_b;
1026 unpack_d (&au, &a);
1027 unpack_d (&bu, &b);
1029 if (isnan (&a) || isnan (&b))
1030 return 1; /* false, truth == 0 */
1032 return __fpcmp_parts (&a, &b) ;
1034 #endif /* L_eq_sf || L_eq_df */
1036 #if defined(L_ne_sf) || defined(L_ne_df)
1037 CMPtype
1038 _ne_f2 (FLO_type arg_a, FLO_type arg_b)
1040 fp_number_type a;
1041 fp_number_type b;
1042 FLO_union_type au, bu;
1044 au.value = arg_a;
1045 bu.value = arg_b;
1047 unpack_d (&au, &a);
1048 unpack_d (&bu, &b);
1050 if (isnan (&a) || isnan (&b))
1051 return 1; /* true, truth != 0 */
1053 return __fpcmp_parts (&a, &b) ;
1055 #endif /* L_ne_sf || L_ne_df */
1057 #if defined(L_gt_sf) || defined(L_gt_df)
1058 CMPtype
1059 _gt_f2 (FLO_type arg_a, FLO_type arg_b)
1061 fp_number_type a;
1062 fp_number_type b;
1063 FLO_union_type au, bu;
1065 au.value = arg_a;
1066 bu.value = arg_b;
1068 unpack_d (&au, &a);
1069 unpack_d (&bu, &b);
1071 if (isnan (&a) || isnan (&b))
1072 return -1; /* false, truth > 0 */
1074 return __fpcmp_parts (&a, &b);
1076 #endif /* L_gt_sf || L_gt_df */
1078 #if defined(L_ge_sf) || defined(L_ge_df)
1079 CMPtype
1080 _ge_f2 (FLO_type arg_a, FLO_type arg_b)
1082 fp_number_type a;
1083 fp_number_type b;
1084 FLO_union_type au, bu;
1086 au.value = arg_a;
1087 bu.value = arg_b;
1089 unpack_d (&au, &a);
1090 unpack_d (&bu, &b);
1092 if (isnan (&a) || isnan (&b))
1093 return -1; /* false, truth >= 0 */
1094 return __fpcmp_parts (&a, &b) ;
1096 #endif /* L_ge_sf || L_ge_df */
1098 #if defined(L_lt_sf) || defined(L_lt_df)
1099 CMPtype
1100 _lt_f2 (FLO_type arg_a, FLO_type arg_b)
1102 fp_number_type a;
1103 fp_number_type b;
1104 FLO_union_type au, bu;
1106 au.value = arg_a;
1107 bu.value = arg_b;
1109 unpack_d (&au, &a);
1110 unpack_d (&bu, &b);
1112 if (isnan (&a) || isnan (&b))
1113 return 1; /* false, truth < 0 */
1115 return __fpcmp_parts (&a, &b);
1117 #endif /* L_lt_sf || L_lt_df */
1119 #if defined(L_le_sf) || defined(L_le_df)
1120 CMPtype
1121 _le_f2 (FLO_type arg_a, FLO_type arg_b)
1123 fp_number_type a;
1124 fp_number_type b;
1125 FLO_union_type au, bu;
1127 au.value = arg_a;
1128 bu.value = arg_b;
1130 unpack_d (&au, &a);
1131 unpack_d (&bu, &b);
1133 if (isnan (&a) || isnan (&b))
1134 return 1; /* false, truth <= 0 */
1136 return __fpcmp_parts (&a, &b) ;
1138 #endif /* L_le_sf || L_le_df */
1140 #endif /* ! US_SOFTWARE_GOFAST */
1142 #if defined(L_unord_sf) || defined(L_unord_df)
1143 CMPtype
1144 _unord_f2 (FLO_type arg_a, FLO_type arg_b)
1146 fp_number_type a;
1147 fp_number_type b;
1148 FLO_union_type au, bu;
1150 au.value = arg_a;
1151 bu.value = arg_b;
1153 unpack_d (&au, &a);
1154 unpack_d (&bu, &b);
1156 return (isnan (&a) || isnan (&b));
1158 #endif /* L_unord_sf || L_unord_df */
1160 #if defined(L_si_to_sf) || defined(L_si_to_df)
1161 FLO_type
1162 si_to_float (SItype arg_a)
1164 fp_number_type in;
1166 in.class = CLASS_NUMBER;
1167 in.sign = arg_a < 0;
1168 if (!arg_a)
1170 in.class = CLASS_ZERO;
1172 else
1174 in.normal_exp = FRACBITS + NGARDS;
1175 if (in.sign)
1177 /* Special case for minint, since there is no +ve integer
1178 representation for it */
1179 if (arg_a == (- MAX_SI_INT - 1))
1181 return (FLO_type)(- MAX_SI_INT - 1);
1183 in.fraction.ll = (-arg_a);
1185 else
1186 in.fraction.ll = arg_a;
1188 while (in.fraction.ll < (1LL << (FRACBITS + NGARDS)))
1190 in.fraction.ll <<= 1;
1191 in.normal_exp -= 1;
1194 return pack_d (&in);
1196 #endif /* L_si_to_sf || L_si_to_df */
1198 #if defined(L_usi_to_sf) || defined(L_usi_to_df)
1199 FLO_type
1200 usi_to_float (USItype arg_a)
1202 fp_number_type in;
1204 in.sign = 0;
1205 if (!arg_a)
1207 in.class = CLASS_ZERO;
1209 else
1211 in.class = CLASS_NUMBER;
1212 in.normal_exp = FRACBITS + NGARDS;
1213 in.fraction.ll = arg_a;
1215 while (in.fraction.ll > (1LL << (FRACBITS + NGARDS)))
1217 in.fraction.ll >>= 1;
1218 in.normal_exp += 1;
1220 while (in.fraction.ll < (1LL << (FRACBITS + NGARDS)))
1222 in.fraction.ll <<= 1;
1223 in.normal_exp -= 1;
1226 return pack_d (&in);
1228 #endif
1230 #if defined(L_sf_to_si) || defined(L_df_to_si)
1231 SItype
1232 float_to_si (FLO_type arg_a)
1234 fp_number_type a;
1235 SItype tmp;
1236 FLO_union_type au;
1238 au.value = arg_a;
1239 unpack_d (&au, &a);
1241 if (iszero (&a))
1242 return 0;
1243 if (isnan (&a))
1244 return 0;
1245 /* get reasonable MAX_SI_INT... */
1246 if (isinf (&a))
1247 return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1248 /* it is a number, but a small one */
1249 if (a.normal_exp < 0)
1250 return 0;
1251 if (a.normal_exp > BITS_PER_SI - 2)
1252 return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1253 tmp = a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1254 return a.sign ? (-tmp) : (tmp);
1256 #endif /* L_sf_to_si || L_df_to_si */
1258 #if defined(L_sf_to_usi) || defined(L_df_to_usi)
1259 #ifdef US_SOFTWARE_GOFAST
1260 /* While libgcc2.c defines its own __fixunssfsi and __fixunsdfsi routines,
1261 we also define them for GOFAST because the ones in libgcc2.c have the
1262 wrong names and I'd rather define these here and keep GOFAST CYG-LOC's
1263 out of libgcc2.c. We can't define these here if not GOFAST because then
1264 there'd be duplicate copies. */
1266 USItype
1267 float_to_usi (FLO_type arg_a)
1269 fp_number_type a;
1270 FLO_union_type au;
1272 au.value = arg_a;
1273 unpack_d (&au, &a);
1275 if (iszero (&a))
1276 return 0;
1277 if (isnan (&a))
1278 return 0;
1279 /* it is a negative number */
1280 if (a.sign)
1281 return 0;
1282 /* get reasonable MAX_USI_INT... */
1283 if (isinf (&a))
1284 return MAX_USI_INT;
1285 /* it is a number, but a small one */
1286 if (a.normal_exp < 0)
1287 return 0;
1288 if (a.normal_exp > BITS_PER_SI - 1)
1289 return MAX_USI_INT;
1290 else if (a.normal_exp > (FRACBITS + NGARDS))
1291 return a.fraction.ll << (a.normal_exp - (FRACBITS + NGARDS));
1292 else
1293 return a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1295 #endif /* US_SOFTWARE_GOFAST */
1296 #endif /* L_sf_to_usi || L_df_to_usi */
1298 #if defined(L_negate_sf) || defined(L_negate_df)
1299 FLO_type
1300 negate (FLO_type arg_a)
1302 fp_number_type a;
1303 FLO_union_type au;
1305 au.value = arg_a;
1306 unpack_d (&au, &a);
1308 flip_sign (&a);
1309 return pack_d (&a);
1311 #endif /* L_negate_sf || L_negate_df */
1313 #ifdef FLOAT
1315 #if defined(L_make_sf)
1316 SFtype
1317 __make_fp(fp_class_type class,
1318 unsigned int sign,
1319 int exp,
1320 USItype frac)
1322 fp_number_type in;
1324 in.class = class;
1325 in.sign = sign;
1326 in.normal_exp = exp;
1327 in.fraction.ll = frac;
1328 return pack_d (&in);
1330 #endif /* L_make_sf */
1332 #ifndef FLOAT_ONLY
1334 /* This enables one to build an fp library that supports float but not double.
1335 Otherwise, we would get an undefined reference to __make_dp.
1336 This is needed for some 8-bit ports that can't handle well values that
1337 are 8-bytes in size, so we just don't support double for them at all. */
1339 #if defined(L_sf_to_df)
1340 DFtype
1341 sf_to_df (SFtype arg_a)
1343 fp_number_type in;
1344 FLO_union_type au;
1346 au.value = arg_a;
1347 unpack_d (&au, &in);
1349 return __make_dp (in.class, in.sign, in.normal_exp,
1350 ((UDItype) in.fraction.ll) << F_D_BITOFF);
1352 #endif /* L_sf_to_df */
1354 #endif /* ! FLOAT_ONLY */
1355 #endif /* FLOAT */
1357 #ifndef FLOAT
1359 extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
1361 #if defined(L_make_df)
1362 DFtype
1363 __make_dp (fp_class_type class, unsigned int sign, int exp, UDItype frac)
1365 fp_number_type in;
1367 in.class = class;
1368 in.sign = sign;
1369 in.normal_exp = exp;
1370 in.fraction.ll = frac;
1371 return pack_d (&in);
1373 #endif /* L_make_df */
1375 #if defined(L_df_to_sf)
1376 SFtype
1377 df_to_sf (DFtype arg_a)
1379 fp_number_type in;
1380 USItype sffrac;
1381 FLO_union_type au;
1383 au.value = arg_a;
1384 unpack_d (&au, &in);
1386 sffrac = in.fraction.ll >> F_D_BITOFF;
1388 /* We set the lowest guard bit in SFFRAC if we discarded any non
1389 zero bits. */
1390 if ((in.fraction.ll & (((USItype) 1 << F_D_BITOFF) - 1)) != 0)
1391 sffrac |= 1;
1393 return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
1395 #endif /* L_df_to_sf */
1397 #endif /* ! FLOAT */
1398 #endif /* !EXTENDED_FLOAT_STUBS */