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 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
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
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. */
47 #include "coretypes.h"
51 /* The following macros can be defined to change the behavior of this file:
52 FLOAT: Implement a `float', aka SFmode, fp library. If this is not
53 defined, then this file implements a `double', aka DFmode, fp library.
54 FLOAT_ONLY: Used with FLOAT, to implement a `float' only library, i.e.
55 don't include float->double conversion which requires the double library.
56 This is useful only for machines which can't support doubles, e.g. some
58 CMPtype: Specify the type that floating point compares should return.
59 This defaults to SItype, aka int.
60 US_SOFTWARE_GOFAST: This makes all entry points use the same names as the
61 US Software goFast library.
62 _DEBUG_BITFLOAT: This makes debugging the code a little easier, by adding
63 two integers to the FLO_union_type.
64 NO_DENORMALS: Disable handling of denormals.
65 NO_NANS: Disable nan and infinity handling
66 SMALL_MACHINE: Useful when operations on QIs and HIs are faster
69 /* We don't currently support extended floats (long doubles) on machines
70 without hardware to deal with them.
72 These stubs are just to keep the linker from complaining about unresolved
73 references which can be pulled in from libio & libstdc++, even if the
74 user isn't using long doubles. However, they may generate an unresolved
75 external to abort if abort is not used by the function, and the stubs
76 are referenced from within libc, since libgcc goes before and after the
79 #ifdef DECLARE_LIBRARY_RENAMES
80 DECLARE_LIBRARY_RENAMES
83 #ifdef EXTENDED_FLOAT_STUBS
84 extern void abort (void);
85 void __extendsfxf2 (void) { abort(); }
86 void __extenddfxf2 (void) { abort(); }
87 void __truncxfdf2 (void) { abort(); }
88 void __truncxfsf2 (void) { abort(); }
89 void __fixxfsi (void) { abort(); }
90 void __floatsixf (void) { abort(); }
91 void __addxf3 (void) { abort(); }
92 void __subxf3 (void) { abort(); }
93 void __mulxf3 (void) { abort(); }
94 void __divxf3 (void) { abort(); }
95 void __negxf2 (void) { abort(); }
96 void __eqxf2 (void) { abort(); }
97 void __nexf2 (void) { abort(); }
98 void __gtxf2 (void) { abort(); }
99 void __gexf2 (void) { abort(); }
100 void __lexf2 (void) { abort(); }
101 void __ltxf2 (void) { abort(); }
103 void __extendsftf2 (void) { abort(); }
104 void __extenddftf2 (void) { abort(); }
105 void __trunctfdf2 (void) { abort(); }
106 void __trunctfsf2 (void) { abort(); }
107 void __fixtfsi (void) { abort(); }
108 void __floatsitf (void) { abort(); }
109 void __addtf3 (void) { abort(); }
110 void __subtf3 (void) { abort(); }
111 void __multf3 (void) { abort(); }
112 void __divtf3 (void) { abort(); }
113 void __negtf2 (void) { abort(); }
114 void __eqtf2 (void) { abort(); }
115 void __netf2 (void) { abort(); }
116 void __gttf2 (void) { abort(); }
117 void __getf2 (void) { abort(); }
118 void __letf2 (void) { abort(); }
119 void __lttf2 (void) { abort(); }
120 #else /* !EXTENDED_FLOAT_STUBS, rest of file */
122 /* IEEE "special" number predicates */
131 #if defined L_thenan_sf
132 const fp_number_type __thenan_sf
= { CLASS_SNAN
, 0, 0, {(fractype
) 0} };
133 #elif defined L_thenan_df
134 const fp_number_type __thenan_df
= { CLASS_SNAN
, 0, 0, {(fractype
) 0} };
135 #elif defined L_thenan_tf
136 const fp_number_type __thenan_tf
= { CLASS_SNAN
, 0, 0, {(fractype
) 0} };
138 extern const fp_number_type __thenan_tf
;
140 extern const fp_number_type __thenan_sf
;
142 extern const fp_number_type __thenan_df
;
146 static fp_number_type
*
149 /* Discard the const qualifier... */
151 return (fp_number_type
*) (& __thenan_tf
);
153 return (fp_number_type
*) (& __thenan_sf
);
155 return (fp_number_type
*) (& __thenan_df
);
161 isnan ( fp_number_type
* x
)
163 return x
->class == CLASS_SNAN
|| x
->class == CLASS_QNAN
;
168 isinf ( fp_number_type
* x
)
170 return x
->class == CLASS_INFINITY
;
177 iszero ( fp_number_type
* x
)
179 return x
->class == CLASS_ZERO
;
184 flip_sign ( fp_number_type
* x
)
189 extern FLO_type
pack_d ( fp_number_type
* );
191 #if defined(L_pack_df) || defined(L_pack_sf) || defined(L_pack_tf)
193 pack_d ( fp_number_type
* src
)
196 fractype fraction
= src
->fraction
.ll
; /* wasn't unsigned before? */
197 int sign
= src
->sign
;
200 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
) && (isnan (src
) || isinf (src
)))
202 /* We can't represent these values accurately. By using the
203 largest possible magnitude, we guarantee that the conversion
204 of infinity is at least as big as any finite number. */
206 fraction
= ((fractype
) 1 << FRACBITS
) - 1;
208 else if (isnan (src
))
211 if (src
->class == CLASS_QNAN
|| 1)
213 #ifdef QUIET_NAN_NEGATED
214 fraction
|= QUIET_NAN
- 1;
216 fraction
|= QUIET_NAN
;
220 else if (isinf (src
))
225 else if (iszero (src
))
230 else if (fraction
== 0)
236 if (src
->normal_exp
< NORMAL_EXPMIN
)
239 /* Go straight to a zero representation if denormals are not
240 supported. The denormal handling would be harmless but
241 isn't unnecessary. */
244 #else /* NO_DENORMALS */
245 /* This number's exponent is too low to fit into the bits
246 available in the number, so we'll store 0 in the exponent and
247 shift the fraction to the right to make up for it. */
249 int shift
= NORMAL_EXPMIN
- src
->normal_exp
;
253 if (shift
> FRAC_NBITS
- NGARDS
)
255 /* No point shifting, since it's more that 64 out. */
260 int lowbit
= (fraction
& (((fractype
)1 << shift
) - 1)) ? 1 : 0;
261 fraction
= (fraction
>> shift
) | lowbit
;
263 if ((fraction
& GARDMASK
) == GARDMSB
)
265 if ((fraction
& (1 << NGARDS
)))
266 fraction
+= GARDROUND
+ 1;
270 /* Add to the guards to round up. */
271 fraction
+= GARDROUND
;
273 /* Perhaps the rounding means we now need to change the
274 exponent, because the fraction is no longer denormal. */
275 if (fraction
>= IMPLICIT_1
)
280 #endif /* NO_DENORMALS */
282 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
)
283 && src
->normal_exp
> EXPBIAS
)
290 exp
= src
->normal_exp
+ EXPBIAS
;
291 if (!ROUND_TOWARDS_ZERO
)
293 /* IF the gard bits are the all zero, but the first, then we're
294 half way between two numbers, choose the one which makes the
295 lsb of the answer 0. */
296 if ((fraction
& GARDMASK
) == GARDMSB
)
298 if (fraction
& (1 << NGARDS
))
299 fraction
+= GARDROUND
+ 1;
303 /* Add a one to the guards to round up */
304 fraction
+= GARDROUND
;
306 if (fraction
>= IMPLICIT_2
)
314 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
) && exp
> EXPMAX
)
316 /* Saturate on overflow. */
318 fraction
= ((fractype
) 1 << FRACBITS
) - 1;
323 /* We previously used bitfields to store the number, but this doesn't
324 handle little/big endian systems conveniently, so use shifts and
326 #ifdef FLOAT_BIT_ORDER_MISMATCH
327 dst
.bits
.fraction
= fraction
;
329 dst
.bits
.sign
= sign
;
331 # if defined TFLOAT && defined HALFFRACBITS
333 halffractype high
, low
;
335 high
= (fraction
>> (FRACBITS
- HALFFRACBITS
));
336 high
&= (((fractype
)1) << HALFFRACBITS
) - 1;
337 high
|= ((fractype
) (exp
& ((1 << EXPBITS
) - 1))) << HALFFRACBITS
;
338 high
|= ((fractype
) (sign
& 1)) << (HALFFRACBITS
| EXPBITS
);
340 low
= (halffractype
)fraction
&
341 ((((halffractype
)1) << (FRACBITS
- HALFFRACBITS
)) - 1);
343 if (exp
== EXPMAX
|| exp
== 0 || low
== 0)
347 exp
-= HALFFRACBITS
+ 1;
350 && low
< ((halffractype
)1 << HALFFRACBITS
))
358 halffractype roundmsb
, round
;
362 roundmsb
= (1 << (exp
- 1));
363 round
= low
& ((roundmsb
<< 1) - 1);
368 if (round
> roundmsb
|| (round
== roundmsb
&& (low
& 1)))
371 if (low
>= ((halffractype
)1 << HALFFRACBITS
))
372 /* We don't shift left, since it has just become the
373 smallest normal number, whose implicit 1 bit is
374 now indicated by the non-zero exponent. */
379 low
&= ((halffractype
)1 << HALFFRACBITS
) - 1;
380 low
|= ((fractype
) (exp
& ((1 << EXPBITS
) - 1))) << HALFFRACBITS
;
381 low
|= ((fractype
) (sign
& 1)) << (HALFFRACBITS
| EXPBITS
);
384 dst
.value_raw
= (((fractype
) high
) << HALFSHIFT
) | low
;
387 dst
.value_raw
= fraction
& ((((fractype
)1) << FRACBITS
) - (fractype
)1);
388 dst
.value_raw
|= ((fractype
) (exp
& ((1 << EXPBITS
) - 1))) << FRACBITS
;
389 dst
.value_raw
|= ((fractype
) (sign
& 1)) << (FRACBITS
| EXPBITS
);
393 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
396 qrtrfractype tmp1
= dst
.words
[0];
397 qrtrfractype tmp2
= dst
.words
[1];
398 dst
.words
[0] = dst
.words
[3];
399 dst
.words
[1] = dst
.words
[2];
405 halffractype tmp
= dst
.words
[0];
406 dst
.words
[0] = dst
.words
[1];
416 #if defined(L_unpack_df) || defined(L_unpack_sf) || defined(L_unpack_tf)
418 unpack_d (FLO_union_type
* src
, fp_number_type
* dst
)
420 /* We previously used bitfields to store the number, but this doesn't
421 handle little/big endian systems conveniently, so use shifts and
427 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
428 FLO_union_type swapped
;
431 swapped
.words
[0] = src
->words
[3];
432 swapped
.words
[1] = src
->words
[2];
433 swapped
.words
[2] = src
->words
[1];
434 swapped
.words
[3] = src
->words
[0];
436 swapped
.words
[0] = src
->words
[1];
437 swapped
.words
[1] = src
->words
[0];
442 #ifdef FLOAT_BIT_ORDER_MISMATCH
443 fraction
= src
->bits
.fraction
;
445 sign
= src
->bits
.sign
;
447 # if defined TFLOAT && defined HALFFRACBITS
449 halffractype high
, low
;
451 high
= src
->value_raw
>> HALFSHIFT
;
452 low
= src
->value_raw
& (((fractype
)1 << HALFSHIFT
) - 1);
454 fraction
= high
& ((((fractype
)1) << HALFFRACBITS
) - 1);
455 fraction
<<= FRACBITS
- HALFFRACBITS
;
456 exp
= ((int)(high
>> HALFFRACBITS
)) & ((1 << EXPBITS
) - 1);
457 sign
= ((int)(high
>> (((HALFFRACBITS
+ EXPBITS
))))) & 1;
459 if (exp
!= EXPMAX
&& exp
!= 0 && low
!= 0)
461 int lowexp
= ((int)(low
>> HALFFRACBITS
)) & ((1 << EXPBITS
) - 1);
462 int lowsign
= ((int)(low
>> (((HALFFRACBITS
+ EXPBITS
))))) & 1;
466 xlow
= low
& ((((fractype
)1) << HALFFRACBITS
) - 1);
468 xlow
|= (((halffractype
)1) << HALFFRACBITS
);
471 shift
= (FRACBITS
- HALFFRACBITS
) - (exp
- lowexp
);
483 fraction
= src
->value_raw
& ((((fractype
)1) << FRACBITS
) - 1);
484 exp
= ((int)(src
->value_raw
>> FRACBITS
)) & ((1 << EXPBITS
) - 1);
485 sign
= ((int)(src
->value_raw
>> (FRACBITS
+ EXPBITS
))) & 1;
492 /* Hmm. Looks like 0 */
499 /* tastes like zero */
500 dst
->class = CLASS_ZERO
;
504 /* Zero exponent with nonzero fraction - it's denormalized,
505 so there isn't a leading implicit one - we'll shift it so
507 dst
->normal_exp
= exp
- EXPBIAS
+ 1;
510 dst
->class = CLASS_NUMBER
;
512 while (fraction
< IMPLICIT_1
)
518 dst
->fraction
.ll
= fraction
;
521 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
) && exp
== EXPMAX
)
526 /* Attached to a zero fraction - means infinity */
527 dst
->class = CLASS_INFINITY
;
531 /* Nonzero fraction, means nan */
532 #ifdef QUIET_NAN_NEGATED
533 if ((fraction
& QUIET_NAN
) == 0)
535 if (fraction
& QUIET_NAN
)
538 dst
->class = CLASS_QNAN
;
542 dst
->class = CLASS_SNAN
;
544 /* Keep the fraction part as the nan number */
545 dst
->fraction
.ll
= fraction
;
550 /* Nothing strange about this number */
551 dst
->normal_exp
= exp
- EXPBIAS
;
552 dst
->class = CLASS_NUMBER
;
553 dst
->fraction
.ll
= (fraction
<< NGARDS
) | IMPLICIT_1
;
556 #endif /* L_unpack_df || L_unpack_sf */
558 #if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
559 static fp_number_type
*
560 _fpadd_parts (fp_number_type
* a
,
562 fp_number_type
* tmp
)
566 /* Put commonly used fields in local variables. */
582 /* Adding infinities with opposite signs yields a NaN. */
583 if (isinf (b
) && a
->sign
!= b
->sign
)
596 tmp
->sign
= a
->sign
& b
->sign
;
606 /* Got two numbers. shift the smaller and increment the exponent till
611 a_normal_exp
= a
->normal_exp
;
612 b_normal_exp
= b
->normal_exp
;
613 a_fraction
= a
->fraction
.ll
;
614 b_fraction
= b
->fraction
.ll
;
616 diff
= a_normal_exp
- b_normal_exp
;
620 if (diff
< FRAC_NBITS
)
622 /* ??? This does shifts one bit at a time. Optimize. */
623 while (a_normal_exp
> b_normal_exp
)
628 while (b_normal_exp
> a_normal_exp
)
636 /* Somethings's up.. choose the biggest */
637 if (a_normal_exp
> b_normal_exp
)
639 b_normal_exp
= a_normal_exp
;
644 a_normal_exp
= b_normal_exp
;
650 if (a
->sign
!= b
->sign
)
654 tfraction
= -a_fraction
+ b_fraction
;
658 tfraction
= a_fraction
- b_fraction
;
663 tmp
->normal_exp
= a_normal_exp
;
664 tmp
->fraction
.ll
= tfraction
;
669 tmp
->normal_exp
= a_normal_exp
;
670 tmp
->fraction
.ll
= -tfraction
;
672 /* and renormalize it */
674 while (tmp
->fraction
.ll
< IMPLICIT_1
&& tmp
->fraction
.ll
)
676 tmp
->fraction
.ll
<<= 1;
683 tmp
->normal_exp
= a_normal_exp
;
684 tmp
->fraction
.ll
= a_fraction
+ b_fraction
;
686 tmp
->class = CLASS_NUMBER
;
687 /* Now the fraction is added, we have to shift down to renormalize the
690 if (tmp
->fraction
.ll
>= IMPLICIT_2
)
692 LSHIFT (tmp
->fraction
.ll
);
700 add (FLO_type arg_a
, FLO_type arg_b
)
706 FLO_union_type au
, bu
;
714 res
= _fpadd_parts (&a
, &b
, &tmp
);
720 sub (FLO_type arg_a
, FLO_type arg_b
)
726 FLO_union_type au
, bu
;
736 res
= _fpadd_parts (&a
, &b
, &tmp
);
740 #endif /* L_addsub_sf || L_addsub_df */
742 #if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
743 static inline __attribute__ ((__always_inline__
)) fp_number_type
*
744 _fpmul_parts ( fp_number_type
* a
,
746 fp_number_type
* tmp
)
753 a
->sign
= a
->sign
!= b
->sign
;
758 b
->sign
= a
->sign
!= b
->sign
;
765 a
->sign
= a
->sign
!= b
->sign
;
774 b
->sign
= a
->sign
!= b
->sign
;
779 a
->sign
= a
->sign
!= b
->sign
;
784 b
->sign
= a
->sign
!= b
->sign
;
788 /* Calculate the mantissa by multiplying both numbers to get a
789 twice-as-wide number. */
791 #if defined(NO_DI_MODE) || defined(TFLOAT)
793 fractype x
= a
->fraction
.ll
;
794 fractype ylow
= b
->fraction
.ll
;
798 /* ??? This does multiplies one bit at a time. Optimize. */
799 for (bit
= 0; bit
< FRAC_NBITS
; bit
++)
805 carry
= (low
+= ylow
) < ylow
;
806 high
+= yhigh
+ carry
;
818 /* Multiplying two USIs to get a UDI, we're safe. */
820 UDItype answer
= (UDItype
)a
->fraction
.ll
* (UDItype
)b
->fraction
.ll
;
822 high
= answer
>> BITS_PER_SI
;
826 /* fractype is DImode, but we need the result to be twice as wide.
827 Assuming a widening multiply from DImode to TImode is not
828 available, build one by hand. */
830 USItype nl
= a
->fraction
.ll
;
831 USItype nh
= a
->fraction
.ll
>> BITS_PER_SI
;
832 USItype ml
= b
->fraction
.ll
;
833 USItype mh
= b
->fraction
.ll
>> BITS_PER_SI
;
834 UDItype pp_ll
= (UDItype
) ml
* nl
;
835 UDItype pp_hl
= (UDItype
) mh
* nl
;
836 UDItype pp_lh
= (UDItype
) ml
* nh
;
837 UDItype pp_hh
= (UDItype
) mh
* nh
;
840 UDItype ps_hh__
= pp_hl
+ pp_lh
;
842 res2
+= (UDItype
)1 << BITS_PER_SI
;
843 pp_hl
= (UDItype
)(USItype
)ps_hh__
<< BITS_PER_SI
;
844 res0
= pp_ll
+ pp_hl
;
847 res2
+= (ps_hh__
>> BITS_PER_SI
) + pp_hh
;
854 tmp
->normal_exp
= a
->normal_exp
+ b
->normal_exp
855 + FRAC_NBITS
- (FRACBITS
+ NGARDS
);
856 tmp
->sign
= a
->sign
!= b
->sign
;
857 while (high
>= IMPLICIT_2
)
867 while (high
< IMPLICIT_1
)
876 /* rounding is tricky. if we only round if it won't make us round later. */
880 if (((high
& GARDMASK
) != GARDMSB
)
881 && (((high
+ 1) & GARDMASK
) == GARDMSB
))
883 /* don't round, it gets done again later. */
891 if (!ROUND_TOWARDS_ZERO
&& (high
& GARDMASK
) == GARDMSB
)
893 if (high
& (1 << NGARDS
))
895 /* half way, so round to even */
896 high
+= GARDROUND
+ 1;
900 /* but we really weren't half way */
901 high
+= GARDROUND
+ 1;
904 tmp
->fraction
.ll
= high
;
905 tmp
->class = CLASS_NUMBER
;
910 multiply (FLO_type arg_a
, FLO_type arg_b
)
916 FLO_union_type au
, bu
;
924 res
= _fpmul_parts (&a
, &b
, &tmp
);
928 #endif /* L_mul_sf || L_mul_df */
930 #if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
931 static inline __attribute__ ((__always_inline__
)) fp_number_type
*
932 _fpdiv_parts (fp_number_type
* a
,
937 fractype denominator
;
949 a
->sign
= a
->sign
^ b
->sign
;
951 if (isinf (a
) || iszero (a
))
953 if (a
->class == b
->class)
966 a
->class = CLASS_INFINITY
;
970 /* Calculate the mantissa by multiplying both 64bit numbers to get a
974 ( numerator / denominator) * 2^(numerator exponent - denominator exponent)
977 a
->normal_exp
= a
->normal_exp
- b
->normal_exp
;
978 numerator
= a
->fraction
.ll
;
979 denominator
= b
->fraction
.ll
;
981 if (numerator
< denominator
)
983 /* Fraction will be less than 1.0 */
989 /* ??? Does divide one bit at a time. Optimize. */
992 if (numerator
>= denominator
)
995 numerator
-= denominator
;
1001 if (!ROUND_TOWARDS_ZERO
&& (quotient
& GARDMASK
) == GARDMSB
)
1003 if (quotient
& (1 << NGARDS
))
1005 /* half way, so round to even */
1006 quotient
+= GARDROUND
+ 1;
1010 /* but we really weren't half way, more bits exist */
1011 quotient
+= GARDROUND
+ 1;
1015 a
->fraction
.ll
= quotient
;
1021 divide (FLO_type arg_a
, FLO_type arg_b
)
1025 fp_number_type
*res
;
1026 FLO_union_type au
, bu
;
1034 res
= _fpdiv_parts (&a
, &b
);
1036 return pack_d (res
);
1038 #endif /* L_div_sf || L_div_df */
1040 #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df) \
1041 || defined(L_fpcmp_parts_tf)
1042 /* according to the demo, fpcmp returns a comparison with 0... thus
1049 __fpcmp_parts (fp_number_type
* a
, fp_number_type
* b
)
1052 /* either nan -> unordered. Must be checked outside of this routine. */
1053 if (isnan (a
) && isnan (b
))
1055 return 1; /* still unordered! */
1059 if (isnan (a
) || isnan (b
))
1061 return 1; /* how to indicate unordered compare? */
1063 if (isinf (a
) && isinf (b
))
1065 /* +inf > -inf, but +inf != +inf */
1066 /* b \a| +inf(0)| -inf(1)
1067 ______\+--------+--------
1068 +inf(0)| a==b(0)| a<b(-1)
1069 -------+--------+--------
1070 -inf(1)| a>b(1) | a==b(0)
1071 -------+--------+--------
1072 So since unordered must be nonzero, just line up the columns...
1074 return b
->sign
- a
->sign
;
1076 /* but not both... */
1079 return a
->sign
? -1 : 1;
1083 return b
->sign
? 1 : -1;
1085 if (iszero (a
) && iszero (b
))
1091 return b
->sign
? 1 : -1;
1095 return a
->sign
? -1 : 1;
1097 /* now both are "normal". */
1098 if (a
->sign
!= b
->sign
)
1100 /* opposite signs */
1101 return a
->sign
? -1 : 1;
1103 /* same sign; exponents? */
1104 if (a
->normal_exp
> b
->normal_exp
)
1106 return a
->sign
? -1 : 1;
1108 if (a
->normal_exp
< b
->normal_exp
)
1110 return a
->sign
? 1 : -1;
1112 /* same exponents; check size. */
1113 if (a
->fraction
.ll
> b
->fraction
.ll
)
1115 return a
->sign
? -1 : 1;
1117 if (a
->fraction
.ll
< b
->fraction
.ll
)
1119 return a
->sign
? 1 : -1;
1121 /* after all that, they're equal. */
1126 #if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compoare_tf)
1128 compare (FLO_type arg_a
, FLO_type arg_b
)
1132 FLO_union_type au
, bu
;
1140 return __fpcmp_parts (&a
, &b
);
1142 #endif /* L_compare_sf || L_compare_df */
1144 #ifndef US_SOFTWARE_GOFAST
1146 /* These should be optimized for their specific tasks someday. */
1148 #if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
1150 _eq_f2 (FLO_type arg_a
, FLO_type arg_b
)
1154 FLO_union_type au
, bu
;
1162 if (isnan (&a
) || isnan (&b
))
1163 return 1; /* false, truth == 0 */
1165 return __fpcmp_parts (&a
, &b
) ;
1167 #endif /* L_eq_sf || L_eq_df */
1169 #if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
1171 _ne_f2 (FLO_type arg_a
, FLO_type arg_b
)
1175 FLO_union_type au
, bu
;
1183 if (isnan (&a
) || isnan (&b
))
1184 return 1; /* true, truth != 0 */
1186 return __fpcmp_parts (&a
, &b
) ;
1188 #endif /* L_ne_sf || L_ne_df */
1190 #if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
1192 _gt_f2 (FLO_type arg_a
, FLO_type arg_b
)
1196 FLO_union_type au
, bu
;
1204 if (isnan (&a
) || isnan (&b
))
1205 return -1; /* false, truth > 0 */
1207 return __fpcmp_parts (&a
, &b
);
1209 #endif /* L_gt_sf || L_gt_df */
1211 #if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
1213 _ge_f2 (FLO_type arg_a
, FLO_type arg_b
)
1217 FLO_union_type au
, bu
;
1225 if (isnan (&a
) || isnan (&b
))
1226 return -1; /* false, truth >= 0 */
1227 return __fpcmp_parts (&a
, &b
) ;
1229 #endif /* L_ge_sf || L_ge_df */
1231 #if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
1233 _lt_f2 (FLO_type arg_a
, FLO_type arg_b
)
1237 FLO_union_type au
, bu
;
1245 if (isnan (&a
) || isnan (&b
))
1246 return 1; /* false, truth < 0 */
1248 return __fpcmp_parts (&a
, &b
);
1250 #endif /* L_lt_sf || L_lt_df */
1252 #if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
1254 _le_f2 (FLO_type arg_a
, FLO_type arg_b
)
1258 FLO_union_type au
, bu
;
1266 if (isnan (&a
) || isnan (&b
))
1267 return 1; /* false, truth <= 0 */
1269 return __fpcmp_parts (&a
, &b
) ;
1271 #endif /* L_le_sf || L_le_df */
1273 #endif /* ! US_SOFTWARE_GOFAST */
1275 #if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
1277 _unord_f2 (FLO_type arg_a
, FLO_type arg_b
)
1281 FLO_union_type au
, bu
;
1289 return (isnan (&a
) || isnan (&b
));
1291 #endif /* L_unord_sf || L_unord_df */
1293 #if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
1295 si_to_float (SItype arg_a
)
1299 in
.class = CLASS_NUMBER
;
1300 in
.sign
= arg_a
< 0;
1303 in
.class = CLASS_ZERO
;
1307 in
.normal_exp
= FRACBITS
+ NGARDS
;
1310 /* Special case for minint, since there is no +ve integer
1311 representation for it */
1312 if (arg_a
== (- MAX_SI_INT
- 1))
1314 return (FLO_type
)(- MAX_SI_INT
- 1);
1316 in
.fraction
.ll
= (-arg_a
);
1319 in
.fraction
.ll
= arg_a
;
1321 while (in
.fraction
.ll
< ((fractype
)1 << (FRACBITS
+ NGARDS
)))
1323 in
.fraction
.ll
<<= 1;
1327 return pack_d (&in
);
1329 #endif /* L_si_to_sf || L_si_to_df */
1331 #if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
1333 usi_to_float (USItype arg_a
)
1340 in
.class = CLASS_ZERO
;
1344 in
.class = CLASS_NUMBER
;
1345 in
.normal_exp
= FRACBITS
+ NGARDS
;
1346 in
.fraction
.ll
= arg_a
;
1348 while (in
.fraction
.ll
> ((fractype
)1 << (FRACBITS
+ NGARDS
)))
1350 in
.fraction
.ll
>>= 1;
1353 while (in
.fraction
.ll
< ((fractype
)1 << (FRACBITS
+ NGARDS
)))
1355 in
.fraction
.ll
<<= 1;
1359 return pack_d (&in
);
1363 #if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
1365 float_to_si (FLO_type arg_a
)
1378 /* get reasonable MAX_SI_INT... */
1380 return a
.sign
? (-MAX_SI_INT
)-1 : MAX_SI_INT
;
1381 /* it is a number, but a small one */
1382 if (a
.normal_exp
< 0)
1384 if (a
.normal_exp
> BITS_PER_SI
- 2)
1385 return a
.sign
? (-MAX_SI_INT
)-1 : MAX_SI_INT
;
1386 tmp
= a
.fraction
.ll
>> ((FRACBITS
+ NGARDS
) - a
.normal_exp
);
1387 return a
.sign
? (-tmp
) : (tmp
);
1389 #endif /* L_sf_to_si || L_df_to_si */
1391 #if defined(L_sf_to_usi) || defined(L_df_to_usi) || defined(L_tf_to_usi)
1392 #if defined US_SOFTWARE_GOFAST || defined(L_tf_to_usi)
1393 /* While libgcc2.c defines its own __fixunssfsi and __fixunsdfsi routines,
1394 we also define them for GOFAST because the ones in libgcc2.c have the
1395 wrong names and I'd rather define these here and keep GOFAST CYG-LOC's
1396 out of libgcc2.c. We can't define these here if not GOFAST because then
1397 there'd be duplicate copies. */
1400 float_to_usi (FLO_type arg_a
)
1412 /* it is a negative number */
1415 /* get reasonable MAX_USI_INT... */
1418 /* it is a number, but a small one */
1419 if (a
.normal_exp
< 0)
1421 if (a
.normal_exp
> BITS_PER_SI
- 1)
1423 else if (a
.normal_exp
> (FRACBITS
+ NGARDS
))
1424 return a
.fraction
.ll
<< (a
.normal_exp
- (FRACBITS
+ NGARDS
));
1426 return a
.fraction
.ll
>> ((FRACBITS
+ NGARDS
) - a
.normal_exp
);
1428 #endif /* US_SOFTWARE_GOFAST */
1429 #endif /* L_sf_to_usi || L_df_to_usi */
1431 #if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
1433 negate (FLO_type arg_a
)
1444 #endif /* L_negate_sf || L_negate_df */
1448 #if defined(L_make_sf)
1450 __make_fp(fp_class_type
class,
1459 in
.normal_exp
= exp
;
1460 in
.fraction
.ll
= frac
;
1461 return pack_d (&in
);
1463 #endif /* L_make_sf */
1467 /* This enables one to build an fp library that supports float but not double.
1468 Otherwise, we would get an undefined reference to __make_dp.
1469 This is needed for some 8-bit ports that can't handle well values that
1470 are 8-bytes in size, so we just don't support double for them at all. */
1472 #if defined(L_sf_to_df)
1474 sf_to_df (SFtype arg_a
)
1480 unpack_d (&au
, &in
);
1482 return __make_dp (in
.class, in
.sign
, in
.normal_exp
,
1483 ((UDItype
) in
.fraction
.ll
) << F_D_BITOFF
);
1485 #endif /* L_sf_to_df */
1487 #if defined(L_sf_to_tf) && defined(TMODES)
1489 sf_to_tf (SFtype arg_a
)
1495 unpack_d (&au
, &in
);
1497 return __make_tp (in
.class, in
.sign
, in
.normal_exp
,
1498 ((UTItype
) in
.fraction
.ll
) << F_T_BITOFF
);
1500 #endif /* L_sf_to_df */
1502 #endif /* ! FLOAT_ONLY */
1507 extern SFtype
__make_fp (fp_class_type
, unsigned int, int, USItype
);
1509 #if defined(L_make_df)
1511 __make_dp (fp_class_type
class, unsigned int sign
, int exp
, UDItype frac
)
1517 in
.normal_exp
= exp
;
1518 in
.fraction
.ll
= frac
;
1519 return pack_d (&in
);
1521 #endif /* L_make_df */
1523 #if defined(L_df_to_sf)
1525 df_to_sf (DFtype arg_a
)
1532 unpack_d (&au
, &in
);
1534 sffrac
= in
.fraction
.ll
>> F_D_BITOFF
;
1536 /* We set the lowest guard bit in SFFRAC if we discarded any non
1538 if ((in
.fraction
.ll
& (((USItype
) 1 << F_D_BITOFF
) - 1)) != 0)
1541 return __make_fp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1543 #endif /* L_df_to_sf */
1545 #if defined(L_df_to_tf) && defined(TMODES) \
1546 && !defined(FLOAT) && !defined(TFLOAT)
1548 df_to_tf (DFtype arg_a
)
1554 unpack_d (&au
, &in
);
1556 return __make_tp (in
.class, in
.sign
, in
.normal_exp
,
1557 ((UTItype
) in
.fraction
.ll
) << D_T_BITOFF
);
1559 #endif /* L_sf_to_df */
1562 #if defined(L_make_tf)
1564 __make_tp(fp_class_type
class,
1573 in
.normal_exp
= exp
;
1574 in
.fraction
.ll
= frac
;
1575 return pack_d (&in
);
1577 #endif /* L_make_tf */
1579 #if defined(L_tf_to_df)
1581 tf_to_df (TFtype arg_a
)
1588 unpack_d (&au
, &in
);
1590 sffrac
= in
.fraction
.ll
>> D_T_BITOFF
;
1592 /* We set the lowest guard bit in SFFRAC if we discarded any non
1594 if ((in
.fraction
.ll
& (((UTItype
) 1 << D_T_BITOFF
) - 1)) != 0)
1597 return __make_dp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1599 #endif /* L_tf_to_df */
1601 #if defined(L_tf_to_sf)
1603 tf_to_sf (TFtype arg_a
)
1610 unpack_d (&au
, &in
);
1612 sffrac
= in
.fraction
.ll
>> F_T_BITOFF
;
1614 /* We set the lowest guard bit in SFFRAC if we discarded any non
1616 if ((in
.fraction
.ll
& (((UTItype
) 1 << F_T_BITOFF
) - 1)) != 0)
1619 return __make_fp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1621 #endif /* L_tf_to_sf */
1624 #endif /* ! FLOAT */
1625 #endif /* !EXTENDED_FLOAT_STUBS */