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 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, 51 Franklin Street, Fifth Floor,
27 Boston, MA 02110-1301, 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"
49 #include "config/fp-bit.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 __builtin_expect (x
->class == CLASS_SNAN
|| x
->class == CLASS_QNAN
,
169 isinf ( fp_number_type
* x
)
171 return __builtin_expect (x
->class == CLASS_INFINITY
, 0);
178 iszero ( fp_number_type
* x
)
180 return x
->class == CLASS_ZERO
;
185 flip_sign ( fp_number_type
* x
)
190 /* Count leading zeroes in N. */
195 extern int __clzsi2 (USItype
);
196 if (sizeof (USItype
) == sizeof (unsigned int))
197 return __builtin_clz (n
);
198 else if (sizeof (USItype
) == sizeof (unsigned long))
199 return __builtin_clzl (n
);
200 else if (sizeof (USItype
) == sizeof (unsigned long long))
201 return __builtin_clzll (n
);
206 extern FLO_type
pack_d ( fp_number_type
* );
208 #if defined(L_pack_df) || defined(L_pack_sf) || defined(L_pack_tf)
210 pack_d ( fp_number_type
* src
)
213 fractype fraction
= src
->fraction
.ll
; /* wasn't unsigned before? */
214 int sign
= src
->sign
;
217 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
) && (isnan (src
) || isinf (src
)))
219 /* We can't represent these values accurately. By using the
220 largest possible magnitude, we guarantee that the conversion
221 of infinity is at least as big as any finite number. */
223 fraction
= ((fractype
) 1 << FRACBITS
) - 1;
225 else if (isnan (src
))
228 if (src
->class == CLASS_QNAN
|| 1)
230 #ifdef QUIET_NAN_NEGATED
231 fraction
|= QUIET_NAN
- 1;
233 fraction
|= QUIET_NAN
;
237 else if (isinf (src
))
242 else if (iszero (src
))
247 else if (fraction
== 0)
253 if (__builtin_expect (src
->normal_exp
< NORMAL_EXPMIN
, 0))
256 /* Go straight to a zero representation if denormals are not
257 supported. The denormal handling would be harmless but
258 isn't unnecessary. */
261 #else /* NO_DENORMALS */
262 /* This number's exponent is too low to fit into the bits
263 available in the number, so we'll store 0 in the exponent and
264 shift the fraction to the right to make up for it. */
266 int shift
= NORMAL_EXPMIN
- src
->normal_exp
;
270 if (shift
> FRAC_NBITS
- NGARDS
)
272 /* No point shifting, since it's more that 64 out. */
277 int lowbit
= (fraction
& (((fractype
)1 << shift
) - 1)) ? 1 : 0;
278 fraction
= (fraction
>> shift
) | lowbit
;
280 if ((fraction
& GARDMASK
) == GARDMSB
)
282 if ((fraction
& (1 << NGARDS
)))
283 fraction
+= GARDROUND
+ 1;
287 /* Add to the guards to round up. */
288 fraction
+= GARDROUND
;
290 /* Perhaps the rounding means we now need to change the
291 exponent, because the fraction is no longer denormal. */
292 if (fraction
>= IMPLICIT_1
)
297 #endif /* NO_DENORMALS */
299 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
)
300 && __builtin_expect (src
->normal_exp
> EXPBIAS
, 0))
307 exp
= src
->normal_exp
+ EXPBIAS
;
308 if (!ROUND_TOWARDS_ZERO
)
310 /* IF the gard bits are the all zero, but the first, then we're
311 half way between two numbers, choose the one which makes the
312 lsb of the answer 0. */
313 if ((fraction
& GARDMASK
) == GARDMSB
)
315 if (fraction
& (1 << NGARDS
))
316 fraction
+= GARDROUND
+ 1;
320 /* Add a one to the guards to round up */
321 fraction
+= GARDROUND
;
323 if (fraction
>= IMPLICIT_2
)
331 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
) && exp
> EXPMAX
)
333 /* Saturate on overflow. */
335 fraction
= ((fractype
) 1 << FRACBITS
) - 1;
340 /* We previously used bitfields to store the number, but this doesn't
341 handle little/big endian systems conveniently, so use shifts and
343 #ifdef FLOAT_BIT_ORDER_MISMATCH
344 dst
.bits
.fraction
= fraction
;
346 dst
.bits
.sign
= sign
;
348 # if defined TFLOAT && defined HALFFRACBITS
350 halffractype high
, low
, unity
;
353 unity
= (halffractype
) 1 << HALFFRACBITS
;
355 /* Set HIGH to the high double's significand, masking out the implicit 1.
356 Set LOW to the low double's full significand. */
357 high
= (fraction
>> (FRACBITS
- HALFFRACBITS
)) & (unity
- 1);
358 low
= fraction
& (unity
* 2 - 1);
360 /* Get the initial sign and exponent of the low double. */
361 lowexp
= exp
- HALFFRACBITS
- 1;
364 /* HIGH should be rounded like a normal double, making |LOW| <=
365 0.5 ULP of HIGH. Assume round-to-nearest. */
367 if (low
> unity
|| (low
== unity
&& (high
& 1) == 1))
369 /* Round HIGH up and adjust LOW to match. */
373 /* May make it infinite, but that's OK. */
377 low
= unity
* 2 - low
;
381 high
|= (halffractype
) exp
<< HALFFRACBITS
;
382 high
|= (halffractype
) sign
<< (HALFFRACBITS
+ EXPBITS
);
384 if (exp
== EXPMAX
|| exp
== 0 || low
== 0)
388 while (lowexp
> 0 && low
< unity
)
396 halffractype roundmsb
, round
;
400 roundmsb
= (1 << (shift
- 1));
401 round
= low
& ((roundmsb
<< 1) - 1);
406 if (round
> roundmsb
|| (round
== roundmsb
&& (low
& 1) == 1))
410 /* LOW rounds up to the smallest normal number. */
416 low
|= (halffractype
) lowexp
<< HALFFRACBITS
;
417 low
|= (halffractype
) lowsign
<< (HALFFRACBITS
+ EXPBITS
);
419 dst
.value_raw
= ((fractype
) high
<< HALFSHIFT
) | low
;
422 dst
.value_raw
= fraction
& ((((fractype
)1) << FRACBITS
) - (fractype
)1);
423 dst
.value_raw
|= ((fractype
) (exp
& ((1 << EXPBITS
) - 1))) << FRACBITS
;
424 dst
.value_raw
|= ((fractype
) (sign
& 1)) << (FRACBITS
| EXPBITS
);
428 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
431 qrtrfractype tmp1
= dst
.words
[0];
432 qrtrfractype tmp2
= dst
.words
[1];
433 dst
.words
[0] = dst
.words
[3];
434 dst
.words
[1] = dst
.words
[2];
440 halffractype tmp
= dst
.words
[0];
441 dst
.words
[0] = dst
.words
[1];
451 #if defined(L_unpack_df) || defined(L_unpack_sf) || defined(L_unpack_tf)
453 unpack_d (FLO_union_type
* src
, fp_number_type
* dst
)
455 /* We previously used bitfields to store the number, but this doesn't
456 handle little/big endian systems conveniently, so use shifts and
462 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
463 FLO_union_type swapped
;
466 swapped
.words
[0] = src
->words
[3];
467 swapped
.words
[1] = src
->words
[2];
468 swapped
.words
[2] = src
->words
[1];
469 swapped
.words
[3] = src
->words
[0];
471 swapped
.words
[0] = src
->words
[1];
472 swapped
.words
[1] = src
->words
[0];
477 #ifdef FLOAT_BIT_ORDER_MISMATCH
478 fraction
= src
->bits
.fraction
;
480 sign
= src
->bits
.sign
;
482 # if defined TFLOAT && defined HALFFRACBITS
484 halffractype high
, low
;
486 high
= src
->value_raw
>> HALFSHIFT
;
487 low
= src
->value_raw
& (((fractype
)1 << HALFSHIFT
) - 1);
489 fraction
= high
& ((((fractype
)1) << HALFFRACBITS
) - 1);
490 fraction
<<= FRACBITS
- HALFFRACBITS
;
491 exp
= ((int)(high
>> HALFFRACBITS
)) & ((1 << EXPBITS
) - 1);
492 sign
= ((int)(high
>> (((HALFFRACBITS
+ EXPBITS
))))) & 1;
494 if (exp
!= EXPMAX
&& exp
!= 0 && low
!= 0)
496 int lowexp
= ((int)(low
>> HALFFRACBITS
)) & ((1 << EXPBITS
) - 1);
497 int lowsign
= ((int)(low
>> (((HALFFRACBITS
+ EXPBITS
))))) & 1;
501 xlow
= low
& ((((fractype
)1) << HALFFRACBITS
) - 1);
503 xlow
|= (((halffractype
)1) << HALFFRACBITS
);
506 shift
= (FRACBITS
- HALFFRACBITS
) - (exp
- lowexp
);
513 else if (fraction
>= xlow
)
517 /* The high part is a power of two but the full number is lower.
518 This code will leave the implicit 1 in FRACTION, but we'd
519 have added that below anyway. */
520 fraction
= (((fractype
) 1 << FRACBITS
) - xlow
) << 1;
526 fraction
= src
->value_raw
& ((((fractype
)1) << FRACBITS
) - 1);
527 exp
= ((int)(src
->value_raw
>> FRACBITS
)) & ((1 << EXPBITS
) - 1);
528 sign
= ((int)(src
->value_raw
>> (FRACBITS
+ EXPBITS
))) & 1;
535 /* Hmm. Looks like 0 */
542 /* tastes like zero */
543 dst
->class = CLASS_ZERO
;
547 /* Zero exponent with nonzero fraction - it's denormalized,
548 so there isn't a leading implicit one - we'll shift it so
550 dst
->normal_exp
= exp
- EXPBIAS
+ 1;
553 dst
->class = CLASS_NUMBER
;
555 while (fraction
< IMPLICIT_1
)
561 dst
->fraction
.ll
= fraction
;
564 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
)
565 && __builtin_expect (exp
== EXPMAX
, 0))
570 /* Attached to a zero fraction - means infinity */
571 dst
->class = CLASS_INFINITY
;
575 /* Nonzero fraction, means nan */
576 #ifdef QUIET_NAN_NEGATED
577 if ((fraction
& QUIET_NAN
) == 0)
579 if (fraction
& QUIET_NAN
)
582 dst
->class = CLASS_QNAN
;
586 dst
->class = CLASS_SNAN
;
588 /* Keep the fraction part as the nan number */
589 dst
->fraction
.ll
= fraction
;
594 /* Nothing strange about this number */
595 dst
->normal_exp
= exp
- EXPBIAS
;
596 dst
->class = CLASS_NUMBER
;
597 dst
->fraction
.ll
= (fraction
<< NGARDS
) | IMPLICIT_1
;
600 #endif /* L_unpack_df || L_unpack_sf */
602 #if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
603 static fp_number_type
*
604 _fpadd_parts (fp_number_type
* a
,
606 fp_number_type
* tmp
)
610 /* Put commonly used fields in local variables. */
626 /* Adding infinities with opposite signs yields a NaN. */
627 if (isinf (b
) && a
->sign
!= b
->sign
)
640 tmp
->sign
= a
->sign
& b
->sign
;
650 /* Got two numbers. shift the smaller and increment the exponent till
656 a_normal_exp
= a
->normal_exp
;
657 b_normal_exp
= b
->normal_exp
;
658 a_fraction
= a
->fraction
.ll
;
659 b_fraction
= b
->fraction
.ll
;
661 diff
= a_normal_exp
- b_normal_exp
;
666 if (diff
< FRAC_NBITS
)
670 b_normal_exp
+= diff
;
671 LSHIFT (b_fraction
, diff
);
675 a_normal_exp
+= diff
;
676 LSHIFT (a_fraction
, diff
);
681 /* Somethings's up.. choose the biggest */
682 if (a_normal_exp
> b_normal_exp
)
684 b_normal_exp
= a_normal_exp
;
689 a_normal_exp
= b_normal_exp
;
695 if (a
->sign
!= b
->sign
)
699 tfraction
= -a_fraction
+ b_fraction
;
703 tfraction
= a_fraction
- b_fraction
;
708 tmp
->normal_exp
= a_normal_exp
;
709 tmp
->fraction
.ll
= tfraction
;
714 tmp
->normal_exp
= a_normal_exp
;
715 tmp
->fraction
.ll
= -tfraction
;
717 /* and renormalize it */
719 while (tmp
->fraction
.ll
< IMPLICIT_1
&& tmp
->fraction
.ll
)
721 tmp
->fraction
.ll
<<= 1;
728 tmp
->normal_exp
= a_normal_exp
;
729 tmp
->fraction
.ll
= a_fraction
+ b_fraction
;
731 tmp
->class = CLASS_NUMBER
;
732 /* Now the fraction is added, we have to shift down to renormalize the
735 if (tmp
->fraction
.ll
>= IMPLICIT_2
)
737 LSHIFT (tmp
->fraction
.ll
, 1);
745 add (FLO_type arg_a
, FLO_type arg_b
)
751 FLO_union_type au
, bu
;
759 res
= _fpadd_parts (&a
, &b
, &tmp
);
765 sub (FLO_type arg_a
, FLO_type arg_b
)
771 FLO_union_type au
, bu
;
781 res
= _fpadd_parts (&a
, &b
, &tmp
);
785 #endif /* L_addsub_sf || L_addsub_df */
787 #if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
788 static inline __attribute__ ((__always_inline__
)) fp_number_type
*
789 _fpmul_parts ( fp_number_type
* a
,
791 fp_number_type
* tmp
)
798 a
->sign
= a
->sign
!= b
->sign
;
803 b
->sign
= a
->sign
!= b
->sign
;
810 a
->sign
= a
->sign
!= b
->sign
;
819 b
->sign
= a
->sign
!= b
->sign
;
824 a
->sign
= a
->sign
!= b
->sign
;
829 b
->sign
= a
->sign
!= b
->sign
;
833 /* Calculate the mantissa by multiplying both numbers to get a
834 twice-as-wide number. */
836 #if defined(NO_DI_MODE) || defined(TFLOAT)
838 fractype x
= a
->fraction
.ll
;
839 fractype ylow
= b
->fraction
.ll
;
843 /* ??? This does multiplies one bit at a time. Optimize. */
844 for (bit
= 0; bit
< FRAC_NBITS
; bit
++)
850 carry
= (low
+= ylow
) < ylow
;
851 high
+= yhigh
+ carry
;
863 /* Multiplying two USIs to get a UDI, we're safe. */
865 UDItype answer
= (UDItype
)a
->fraction
.ll
* (UDItype
)b
->fraction
.ll
;
867 high
= answer
>> BITS_PER_SI
;
871 /* fractype is DImode, but we need the result to be twice as wide.
872 Assuming a widening multiply from DImode to TImode is not
873 available, build one by hand. */
875 USItype nl
= a
->fraction
.ll
;
876 USItype nh
= a
->fraction
.ll
>> BITS_PER_SI
;
877 USItype ml
= b
->fraction
.ll
;
878 USItype mh
= b
->fraction
.ll
>> BITS_PER_SI
;
879 UDItype pp_ll
= (UDItype
) ml
* nl
;
880 UDItype pp_hl
= (UDItype
) mh
* nl
;
881 UDItype pp_lh
= (UDItype
) ml
* nh
;
882 UDItype pp_hh
= (UDItype
) mh
* nh
;
885 UDItype ps_hh__
= pp_hl
+ pp_lh
;
887 res2
+= (UDItype
)1 << BITS_PER_SI
;
888 pp_hl
= (UDItype
)(USItype
)ps_hh__
<< BITS_PER_SI
;
889 res0
= pp_ll
+ pp_hl
;
892 res2
+= (ps_hh__
>> BITS_PER_SI
) + pp_hh
;
899 tmp
->normal_exp
= a
->normal_exp
+ b
->normal_exp
900 + FRAC_NBITS
- (FRACBITS
+ NGARDS
);
901 tmp
->sign
= a
->sign
!= b
->sign
;
902 while (high
>= IMPLICIT_2
)
912 while (high
< IMPLICIT_1
)
922 if (!ROUND_TOWARDS_ZERO
&& (high
& GARDMASK
) == GARDMSB
)
924 if (high
& (1 << NGARDS
))
926 /* Because we're half way, we would round to even by adding
927 GARDROUND + 1, except that's also done in the packing
928 function, and rounding twice will lose precision and cause
929 the result to be too far off. Example: 32-bit floats with
930 bit patterns 0xfff * 0x3f800400 ~= 0xfff (less than 0.5ulp
931 off), not 0x1000 (more than 0.5ulp off). */
935 /* We're a further than half way by a small amount corresponding
936 to the bits set in "low". Knowing that, we round here and
937 not in pack_d, because there we don't have "low" available
939 high
+= GARDROUND
+ 1;
941 /* Avoid further rounding in pack_d. */
942 high
&= ~(fractype
) GARDMASK
;
945 tmp
->fraction
.ll
= high
;
946 tmp
->class = CLASS_NUMBER
;
951 multiply (FLO_type arg_a
, FLO_type arg_b
)
957 FLO_union_type au
, bu
;
965 res
= _fpmul_parts (&a
, &b
, &tmp
);
969 #endif /* L_mul_sf || L_mul_df || L_mul_tf */
971 #if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
972 static inline __attribute__ ((__always_inline__
)) fp_number_type
*
973 _fpdiv_parts (fp_number_type
* a
,
978 fractype denominator
;
990 a
->sign
= a
->sign
^ b
->sign
;
992 if (isinf (a
) || iszero (a
))
994 if (a
->class == b
->class)
1007 a
->class = CLASS_INFINITY
;
1011 /* Calculate the mantissa by multiplying both 64bit numbers to get a
1015 ( numerator / denominator) * 2^(numerator exponent - denominator exponent)
1018 a
->normal_exp
= a
->normal_exp
- b
->normal_exp
;
1019 numerator
= a
->fraction
.ll
;
1020 denominator
= b
->fraction
.ll
;
1022 if (numerator
< denominator
)
1024 /* Fraction will be less than 1.0 */
1030 /* ??? Does divide one bit at a time. Optimize. */
1033 if (numerator
>= denominator
)
1036 numerator
-= denominator
;
1042 if (!ROUND_TOWARDS_ZERO
&& (quotient
& GARDMASK
) == GARDMSB
)
1044 if (quotient
& (1 << NGARDS
))
1046 /* Because we're half way, we would round to even by adding
1047 GARDROUND + 1, except that's also done in the packing
1048 function, and rounding twice will lose precision and cause
1049 the result to be too far off. */
1053 /* We're a further than half way by the small amount
1054 corresponding to the bits set in "numerator". Knowing
1055 that, we round here and not in pack_d, because there we
1056 don't have "numerator" available anymore. */
1057 quotient
+= GARDROUND
+ 1;
1059 /* Avoid further rounding in pack_d. */
1060 quotient
&= ~(fractype
) GARDMASK
;
1064 a
->fraction
.ll
= quotient
;
1070 divide (FLO_type arg_a
, FLO_type arg_b
)
1074 fp_number_type
*res
;
1075 FLO_union_type au
, bu
;
1083 res
= _fpdiv_parts (&a
, &b
);
1085 return pack_d (res
);
1087 #endif /* L_div_sf || L_div_df */
1089 #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df) \
1090 || defined(L_fpcmp_parts_tf)
1091 /* according to the demo, fpcmp returns a comparison with 0... thus
1098 __fpcmp_parts (fp_number_type
* a
, fp_number_type
* b
)
1101 /* either nan -> unordered. Must be checked outside of this routine. */
1102 if (isnan (a
) && isnan (b
))
1104 return 1; /* still unordered! */
1108 if (isnan (a
) || isnan (b
))
1110 return 1; /* how to indicate unordered compare? */
1112 if (isinf (a
) && isinf (b
))
1114 /* +inf > -inf, but +inf != +inf */
1115 /* b \a| +inf(0)| -inf(1)
1116 ______\+--------+--------
1117 +inf(0)| a==b(0)| a<b(-1)
1118 -------+--------+--------
1119 -inf(1)| a>b(1) | a==b(0)
1120 -------+--------+--------
1121 So since unordered must be nonzero, just line up the columns...
1123 return b
->sign
- a
->sign
;
1125 /* but not both... */
1128 return a
->sign
? -1 : 1;
1132 return b
->sign
? 1 : -1;
1134 if (iszero (a
) && iszero (b
))
1140 return b
->sign
? 1 : -1;
1144 return a
->sign
? -1 : 1;
1146 /* now both are "normal". */
1147 if (a
->sign
!= b
->sign
)
1149 /* opposite signs */
1150 return a
->sign
? -1 : 1;
1152 /* same sign; exponents? */
1153 if (a
->normal_exp
> b
->normal_exp
)
1155 return a
->sign
? -1 : 1;
1157 if (a
->normal_exp
< b
->normal_exp
)
1159 return a
->sign
? 1 : -1;
1161 /* same exponents; check size. */
1162 if (a
->fraction
.ll
> b
->fraction
.ll
)
1164 return a
->sign
? -1 : 1;
1166 if (a
->fraction
.ll
< b
->fraction
.ll
)
1168 return a
->sign
? 1 : -1;
1170 /* after all that, they're equal. */
1175 #if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compoare_tf)
1177 compare (FLO_type arg_a
, FLO_type arg_b
)
1181 FLO_union_type au
, bu
;
1189 return __fpcmp_parts (&a
, &b
);
1191 #endif /* L_compare_sf || L_compare_df */
1193 #ifndef US_SOFTWARE_GOFAST
1195 /* These should be optimized for their specific tasks someday. */
1197 #if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
1199 _eq_f2 (FLO_type arg_a
, FLO_type arg_b
)
1203 FLO_union_type au
, bu
;
1211 if (isnan (&a
) || isnan (&b
))
1212 return 1; /* false, truth == 0 */
1214 return __fpcmp_parts (&a
, &b
) ;
1216 #endif /* L_eq_sf || L_eq_df */
1218 #if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
1220 _ne_f2 (FLO_type arg_a
, FLO_type arg_b
)
1224 FLO_union_type au
, bu
;
1232 if (isnan (&a
) || isnan (&b
))
1233 return 1; /* true, truth != 0 */
1235 return __fpcmp_parts (&a
, &b
) ;
1237 #endif /* L_ne_sf || L_ne_df */
1239 #if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
1241 _gt_f2 (FLO_type arg_a
, FLO_type arg_b
)
1245 FLO_union_type au
, bu
;
1253 if (isnan (&a
) || isnan (&b
))
1254 return -1; /* false, truth > 0 */
1256 return __fpcmp_parts (&a
, &b
);
1258 #endif /* L_gt_sf || L_gt_df */
1260 #if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
1262 _ge_f2 (FLO_type arg_a
, FLO_type arg_b
)
1266 FLO_union_type au
, bu
;
1274 if (isnan (&a
) || isnan (&b
))
1275 return -1; /* false, truth >= 0 */
1276 return __fpcmp_parts (&a
, &b
) ;
1278 #endif /* L_ge_sf || L_ge_df */
1280 #if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
1282 _lt_f2 (FLO_type arg_a
, FLO_type arg_b
)
1286 FLO_union_type au
, bu
;
1294 if (isnan (&a
) || isnan (&b
))
1295 return 1; /* false, truth < 0 */
1297 return __fpcmp_parts (&a
, &b
);
1299 #endif /* L_lt_sf || L_lt_df */
1301 #if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
1303 _le_f2 (FLO_type arg_a
, FLO_type arg_b
)
1307 FLO_union_type au
, bu
;
1315 if (isnan (&a
) || isnan (&b
))
1316 return 1; /* false, truth <= 0 */
1318 return __fpcmp_parts (&a
, &b
) ;
1320 #endif /* L_le_sf || L_le_df */
1322 #endif /* ! US_SOFTWARE_GOFAST */
1324 #if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
1326 _unord_f2 (FLO_type arg_a
, FLO_type arg_b
)
1330 FLO_union_type au
, bu
;
1338 return (isnan (&a
) || isnan (&b
));
1340 #endif /* L_unord_sf || L_unord_df */
1342 #if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
1344 si_to_float (SItype arg_a
)
1348 in
.class = CLASS_NUMBER
;
1349 in
.sign
= arg_a
< 0;
1352 in
.class = CLASS_ZERO
;
1358 in
.normal_exp
= FRACBITS
+ NGARDS
;
1361 /* Special case for minint, since there is no +ve integer
1362 representation for it */
1363 if (arg_a
== (- MAX_SI_INT
- 1))
1365 return (FLO_type
)(- MAX_SI_INT
- 1);
1372 in
.fraction
.ll
= uarg
;
1373 shift
= clzusi (uarg
) - (BITS_PER_SI
- 1 - FRACBITS
- NGARDS
);
1376 in
.fraction
.ll
<<= shift
;
1377 in
.normal_exp
-= shift
;
1380 return pack_d (&in
);
1382 #endif /* L_si_to_sf || L_si_to_df */
1384 #if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
1386 usi_to_float (USItype arg_a
)
1393 in
.class = CLASS_ZERO
;
1398 in
.class = CLASS_NUMBER
;
1399 in
.normal_exp
= FRACBITS
+ NGARDS
;
1400 in
.fraction
.ll
= arg_a
;
1402 shift
= clzusi (arg_a
) - (BITS_PER_SI
- 1 - FRACBITS
- NGARDS
);
1405 fractype guard
= in
.fraction
.ll
& (((fractype
)1 << -shift
) - 1);
1406 in
.fraction
.ll
>>= -shift
;
1407 in
.fraction
.ll
|= (guard
!= 0);
1408 in
.normal_exp
-= shift
;
1412 in
.fraction
.ll
<<= shift
;
1413 in
.normal_exp
-= shift
;
1416 return pack_d (&in
);
1420 #if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
1422 float_to_si (FLO_type arg_a
)
1435 /* get reasonable MAX_SI_INT... */
1437 return a
.sign
? (-MAX_SI_INT
)-1 : MAX_SI_INT
;
1438 /* it is a number, but a small one */
1439 if (a
.normal_exp
< 0)
1441 if (a
.normal_exp
> BITS_PER_SI
- 2)
1442 return a
.sign
? (-MAX_SI_INT
)-1 : MAX_SI_INT
;
1443 tmp
= a
.fraction
.ll
>> ((FRACBITS
+ NGARDS
) - a
.normal_exp
);
1444 return a
.sign
? (-tmp
) : (tmp
);
1446 #endif /* L_sf_to_si || L_df_to_si */
1448 #if defined(L_sf_to_usi) || defined(L_df_to_usi) || defined(L_tf_to_usi)
1449 #if defined US_SOFTWARE_GOFAST || defined(L_tf_to_usi)
1450 /* While libgcc2.c defines its own __fixunssfsi and __fixunsdfsi routines,
1451 we also define them for GOFAST because the ones in libgcc2.c have the
1452 wrong names and I'd rather define these here and keep GOFAST CYG-LOC's
1453 out of libgcc2.c. We can't define these here if not GOFAST because then
1454 there'd be duplicate copies. */
1457 float_to_usi (FLO_type arg_a
)
1469 /* it is a negative number */
1472 /* get reasonable MAX_USI_INT... */
1475 /* it is a number, but a small one */
1476 if (a
.normal_exp
< 0)
1478 if (a
.normal_exp
> BITS_PER_SI
- 1)
1480 else if (a
.normal_exp
> (FRACBITS
+ NGARDS
))
1481 return a
.fraction
.ll
<< (a
.normal_exp
- (FRACBITS
+ NGARDS
));
1483 return a
.fraction
.ll
>> ((FRACBITS
+ NGARDS
) - a
.normal_exp
);
1485 #endif /* US_SOFTWARE_GOFAST */
1486 #endif /* L_sf_to_usi || L_df_to_usi */
1488 #if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
1490 negate (FLO_type arg_a
)
1501 #endif /* L_negate_sf || L_negate_df */
1505 #if defined(L_make_sf)
1507 __make_fp(fp_class_type
class,
1516 in
.normal_exp
= exp
;
1517 in
.fraction
.ll
= frac
;
1518 return pack_d (&in
);
1520 #endif /* L_make_sf */
1524 /* This enables one to build an fp library that supports float but not double.
1525 Otherwise, we would get an undefined reference to __make_dp.
1526 This is needed for some 8-bit ports that can't handle well values that
1527 are 8-bytes in size, so we just don't support double for them at all. */
1529 #if defined(L_sf_to_df)
1531 sf_to_df (SFtype arg_a
)
1537 unpack_d (&au
, &in
);
1539 return __make_dp (in
.class, in
.sign
, in
.normal_exp
,
1540 ((UDItype
) in
.fraction
.ll
) << F_D_BITOFF
);
1542 #endif /* L_sf_to_df */
1544 #if defined(L_sf_to_tf) && defined(TMODES)
1546 sf_to_tf (SFtype arg_a
)
1552 unpack_d (&au
, &in
);
1554 return __make_tp (in
.class, in
.sign
, in
.normal_exp
,
1555 ((UTItype
) in
.fraction
.ll
) << F_T_BITOFF
);
1557 #endif /* L_sf_to_df */
1559 #endif /* ! FLOAT_ONLY */
1564 extern SFtype
__make_fp (fp_class_type
, unsigned int, int, USItype
);
1566 #if defined(L_make_df)
1568 __make_dp (fp_class_type
class, unsigned int sign
, int exp
, UDItype frac
)
1574 in
.normal_exp
= exp
;
1575 in
.fraction
.ll
= frac
;
1576 return pack_d (&in
);
1578 #endif /* L_make_df */
1580 #if defined(L_df_to_sf)
1582 df_to_sf (DFtype arg_a
)
1589 unpack_d (&au
, &in
);
1591 sffrac
= in
.fraction
.ll
>> F_D_BITOFF
;
1593 /* We set the lowest guard bit in SFFRAC if we discarded any non
1595 if ((in
.fraction
.ll
& (((USItype
) 1 << F_D_BITOFF
) - 1)) != 0)
1598 return __make_fp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1600 #endif /* L_df_to_sf */
1602 #if defined(L_df_to_tf) && defined(TMODES) \
1603 && !defined(FLOAT) && !defined(TFLOAT)
1605 df_to_tf (DFtype arg_a
)
1611 unpack_d (&au
, &in
);
1613 return __make_tp (in
.class, in
.sign
, in
.normal_exp
,
1614 ((UTItype
) in
.fraction
.ll
) << D_T_BITOFF
);
1616 #endif /* L_sf_to_df */
1619 #if defined(L_make_tf)
1621 __make_tp(fp_class_type
class,
1630 in
.normal_exp
= exp
;
1631 in
.fraction
.ll
= frac
;
1632 return pack_d (&in
);
1634 #endif /* L_make_tf */
1636 #if defined(L_tf_to_df)
1638 tf_to_df (TFtype arg_a
)
1645 unpack_d (&au
, &in
);
1647 sffrac
= in
.fraction
.ll
>> D_T_BITOFF
;
1649 /* We set the lowest guard bit in SFFRAC if we discarded any non
1651 if ((in
.fraction
.ll
& (((UTItype
) 1 << D_T_BITOFF
) - 1)) != 0)
1654 return __make_dp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1656 #endif /* L_tf_to_df */
1658 #if defined(L_tf_to_sf)
1660 tf_to_sf (TFtype arg_a
)
1667 unpack_d (&au
, &in
);
1669 sffrac
= in
.fraction
.ll
>> F_T_BITOFF
;
1671 /* We set the lowest guard bit in SFFRAC if we discarded any non
1673 if ((in
.fraction
.ll
& (((UTItype
) 1 << F_T_BITOFF
) - 1)) != 0)
1676 return __make_fp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1678 #endif /* L_tf_to_sf */
1681 #endif /* ! FLOAT */
1682 #endif /* !EXTENDED_FLOAT_STUBS */