1 /* This is a software floating point library which can be used
2 for targets without hardware floating point.
3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003,
4 2004, 2005, 2008, 2009 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
27 /* This implements IEEE 754 format arithmetic, but does not provide a
28 mechanism for setting the rounding mode, or for generating or handling
31 The original code by Steve Chamberlain, hacked by Mark Eichin and Jim
32 Wilson, all of Cygnus Support. */
34 /* The intended way to use this file is to make two copies, add `#define FLOAT'
35 to one copy, then compile both copies and add them to libgcc.a. */
38 #include "coretypes.h"
40 #include "config/fp-bit.h"
42 /* The following macros can be defined to change the behavior of this file:
43 FLOAT: Implement a `float', aka SFmode, fp library. If this is not
44 defined, then this file implements a `double', aka DFmode, fp library.
45 FLOAT_ONLY: Used with FLOAT, to implement a `float' only library, i.e.
46 don't include float->double conversion which requires the double library.
47 This is useful only for machines which can't support doubles, e.g. some
49 CMPtype: Specify the type that floating point compares should return.
50 This defaults to SItype, aka int.
51 US_SOFTWARE_GOFAST: This makes all entry points use the same names as the
52 US Software goFast library.
53 _DEBUG_BITFLOAT: This makes debugging the code a little easier, by adding
54 two integers to the FLO_union_type.
55 NO_DENORMALS: Disable handling of denormals.
56 NO_NANS: Disable nan and infinity handling
57 SMALL_MACHINE: Useful when operations on QIs and HIs are faster
60 /* We don't currently support extended floats (long doubles) on machines
61 without hardware to deal with them.
63 These stubs are just to keep the linker from complaining about unresolved
64 references which can be pulled in from libio & libstdc++, even if the
65 user isn't using long doubles. However, they may generate an unresolved
66 external to abort if abort is not used by the function, and the stubs
67 are referenced from within libc, since libgcc goes before and after the
70 #ifdef DECLARE_LIBRARY_RENAMES
71 DECLARE_LIBRARY_RENAMES
74 #ifdef EXTENDED_FLOAT_STUBS
75 extern void abort (void);
76 void __extendsfxf2 (void) { abort(); }
77 void __extenddfxf2 (void) { abort(); }
78 void __truncxfdf2 (void) { abort(); }
79 void __truncxfsf2 (void) { abort(); }
80 void __fixxfsi (void) { abort(); }
81 void __floatsixf (void) { abort(); }
82 void __addxf3 (void) { abort(); }
83 void __subxf3 (void) { abort(); }
84 void __mulxf3 (void) { abort(); }
85 void __divxf3 (void) { abort(); }
86 void __negxf2 (void) { abort(); }
87 void __eqxf2 (void) { abort(); }
88 void __nexf2 (void) { abort(); }
89 void __gtxf2 (void) { abort(); }
90 void __gexf2 (void) { abort(); }
91 void __lexf2 (void) { abort(); }
92 void __ltxf2 (void) { abort(); }
94 void __extendsftf2 (void) { abort(); }
95 void __extenddftf2 (void) { abort(); }
96 void __trunctfdf2 (void) { abort(); }
97 void __trunctfsf2 (void) { abort(); }
98 void __fixtfsi (void) { abort(); }
99 void __floatsitf (void) { abort(); }
100 void __addtf3 (void) { abort(); }
101 void __subtf3 (void) { abort(); }
102 void __multf3 (void) { abort(); }
103 void __divtf3 (void) { abort(); }
104 void __negtf2 (void) { abort(); }
105 void __eqtf2 (void) { abort(); }
106 void __netf2 (void) { abort(); }
107 void __gttf2 (void) { abort(); }
108 void __getf2 (void) { abort(); }
109 void __letf2 (void) { abort(); }
110 void __lttf2 (void) { abort(); }
111 #else /* !EXTENDED_FLOAT_STUBS, rest of file */
113 /* IEEE "special" number predicates */
122 #if defined L_thenan_sf
123 const fp_number_type __thenan_sf
= { CLASS_SNAN
, 0, 0, {(fractype
) 0} };
124 #elif defined L_thenan_df
125 const fp_number_type __thenan_df
= { CLASS_SNAN
, 0, 0, {(fractype
) 0} };
126 #elif defined L_thenan_tf
127 const fp_number_type __thenan_tf
= { CLASS_SNAN
, 0, 0, {(fractype
) 0} };
129 extern const fp_number_type __thenan_tf
;
131 extern const fp_number_type __thenan_sf
;
133 extern const fp_number_type __thenan_df
;
137 static const fp_number_type
*
141 return & __thenan_tf
;
143 return & __thenan_sf
;
145 return & __thenan_df
;
151 isnan (const fp_number_type
*x
)
153 return __builtin_expect (x
->class == CLASS_SNAN
|| x
->class == CLASS_QNAN
,
159 isinf (const fp_number_type
* x
)
161 return __builtin_expect (x
->class == CLASS_INFINITY
, 0);
168 iszero (const fp_number_type
* x
)
170 return x
->class == CLASS_ZERO
;
175 flip_sign ( fp_number_type
* x
)
180 /* Count leading zeroes in N. */
185 extern int __clzsi2 (USItype
);
186 if (sizeof (USItype
) == sizeof (unsigned int))
187 return __builtin_clz (n
);
188 else if (sizeof (USItype
) == sizeof (unsigned long))
189 return __builtin_clzl (n
);
190 else if (sizeof (USItype
) == sizeof (unsigned long long))
191 return __builtin_clzll (n
);
196 extern FLO_type
pack_d (const fp_number_type
* );
198 #if defined(L_pack_df) || defined(L_pack_sf) || defined(L_pack_tf)
200 pack_d (const fp_number_type
*src
)
203 fractype fraction
= src
->fraction
.ll
; /* wasn't unsigned before? */
204 int sign
= src
->sign
;
207 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
) && (isnan (src
) || isinf (src
)))
209 /* We can't represent these values accurately. By using the
210 largest possible magnitude, we guarantee that the conversion
211 of infinity is at least as big as any finite number. */
213 fraction
= ((fractype
) 1 << FRACBITS
) - 1;
215 else if (isnan (src
))
218 if (src
->class == CLASS_QNAN
|| 1)
220 #ifdef QUIET_NAN_NEGATED
221 fraction
|= QUIET_NAN
- 1;
223 fraction
|= QUIET_NAN
;
227 else if (isinf (src
))
232 else if (iszero (src
))
237 else if (fraction
== 0)
243 if (__builtin_expect (src
->normal_exp
< NORMAL_EXPMIN
, 0))
246 /* Go straight to a zero representation if denormals are not
247 supported. The denormal handling would be harmless but
248 isn't unnecessary. */
251 #else /* NO_DENORMALS */
252 /* This number's exponent is too low to fit into the bits
253 available in the number, so we'll store 0 in the exponent and
254 shift the fraction to the right to make up for it. */
256 int shift
= NORMAL_EXPMIN
- src
->normal_exp
;
260 if (shift
> FRAC_NBITS
- NGARDS
)
262 /* No point shifting, since it's more that 64 out. */
267 int lowbit
= (fraction
& (((fractype
)1 << shift
) - 1)) ? 1 : 0;
268 fraction
= (fraction
>> shift
) | lowbit
;
270 if ((fraction
& GARDMASK
) == GARDMSB
)
272 if ((fraction
& (1 << NGARDS
)))
273 fraction
+= GARDROUND
+ 1;
277 /* Add to the guards to round up. */
278 fraction
+= GARDROUND
;
280 /* Perhaps the rounding means we now need to change the
281 exponent, because the fraction is no longer denormal. */
282 if (fraction
>= IMPLICIT_1
)
287 #endif /* NO_DENORMALS */
289 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
)
290 && __builtin_expect (src
->normal_exp
> EXPBIAS
, 0))
297 exp
= src
->normal_exp
+ EXPBIAS
;
298 if (!ROUND_TOWARDS_ZERO
)
300 /* IF the gard bits are the all zero, but the first, then we're
301 half way between two numbers, choose the one which makes the
302 lsb of the answer 0. */
303 if ((fraction
& GARDMASK
) == GARDMSB
)
305 if (fraction
& (1 << NGARDS
))
306 fraction
+= GARDROUND
+ 1;
310 /* Add a one to the guards to round up */
311 fraction
+= GARDROUND
;
313 if (fraction
>= IMPLICIT_2
)
321 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
) && exp
> EXPMAX
)
323 /* Saturate on overflow. */
325 fraction
= ((fractype
) 1 << FRACBITS
) - 1;
330 /* We previously used bitfields to store the number, but this doesn't
331 handle little/big endian systems conveniently, so use shifts and
333 #ifdef FLOAT_BIT_ORDER_MISMATCH
334 dst
.bits
.fraction
= fraction
;
336 dst
.bits
.sign
= sign
;
338 # if defined TFLOAT && defined HALFFRACBITS
340 halffractype high
, low
, unity
;
343 unity
= (halffractype
) 1 << HALFFRACBITS
;
345 /* Set HIGH to the high double's significand, masking out the implicit 1.
346 Set LOW to the low double's full significand. */
347 high
= (fraction
>> (FRACBITS
- HALFFRACBITS
)) & (unity
- 1);
348 low
= fraction
& (unity
* 2 - 1);
350 /* Get the initial sign and exponent of the low double. */
351 lowexp
= exp
- HALFFRACBITS
- 1;
354 /* HIGH should be rounded like a normal double, making |LOW| <=
355 0.5 ULP of HIGH. Assume round-to-nearest. */
357 if (low
> unity
|| (low
== unity
&& (high
& 1) == 1))
359 /* Round HIGH up and adjust LOW to match. */
363 /* May make it infinite, but that's OK. */
367 low
= unity
* 2 - low
;
371 high
|= (halffractype
) exp
<< HALFFRACBITS
;
372 high
|= (halffractype
) sign
<< (HALFFRACBITS
+ EXPBITS
);
374 if (exp
== EXPMAX
|| exp
== 0 || low
== 0)
378 while (lowexp
> 0 && low
< unity
)
386 halffractype roundmsb
, round
;
390 roundmsb
= (1 << (shift
- 1));
391 round
= low
& ((roundmsb
<< 1) - 1);
396 if (round
> roundmsb
|| (round
== roundmsb
&& (low
& 1) == 1))
400 /* LOW rounds up to the smallest normal number. */
406 low
|= (halffractype
) lowexp
<< HALFFRACBITS
;
407 low
|= (halffractype
) lowsign
<< (HALFFRACBITS
+ EXPBITS
);
409 dst
.value_raw
= ((fractype
) high
<< HALFSHIFT
) | low
;
412 dst
.value_raw
= fraction
& ((((fractype
)1) << FRACBITS
) - (fractype
)1);
413 dst
.value_raw
|= ((fractype
) (exp
& ((1 << EXPBITS
) - 1))) << FRACBITS
;
414 dst
.value_raw
|= ((fractype
) (sign
& 1)) << (FRACBITS
| EXPBITS
);
418 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
421 qrtrfractype tmp1
= dst
.words
[0];
422 qrtrfractype tmp2
= dst
.words
[1];
423 dst
.words
[0] = dst
.words
[3];
424 dst
.words
[1] = dst
.words
[2];
430 halffractype tmp
= dst
.words
[0];
431 dst
.words
[0] = dst
.words
[1];
441 #if defined(L_unpack_df) || defined(L_unpack_sf) || defined(L_unpack_tf)
443 unpack_d (FLO_union_type
* src
, fp_number_type
* dst
)
445 /* We previously used bitfields to store the number, but this doesn't
446 handle little/big endian systems conveniently, so use shifts and
452 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
453 FLO_union_type swapped
;
456 swapped
.words
[0] = src
->words
[3];
457 swapped
.words
[1] = src
->words
[2];
458 swapped
.words
[2] = src
->words
[1];
459 swapped
.words
[3] = src
->words
[0];
461 swapped
.words
[0] = src
->words
[1];
462 swapped
.words
[1] = src
->words
[0];
467 #ifdef FLOAT_BIT_ORDER_MISMATCH
468 fraction
= src
->bits
.fraction
;
470 sign
= src
->bits
.sign
;
472 # if defined TFLOAT && defined HALFFRACBITS
474 halffractype high
, low
;
476 high
= src
->value_raw
>> HALFSHIFT
;
477 low
= src
->value_raw
& (((fractype
)1 << HALFSHIFT
) - 1);
479 fraction
= high
& ((((fractype
)1) << HALFFRACBITS
) - 1);
480 fraction
<<= FRACBITS
- HALFFRACBITS
;
481 exp
= ((int)(high
>> HALFFRACBITS
)) & ((1 << EXPBITS
) - 1);
482 sign
= ((int)(high
>> (((HALFFRACBITS
+ EXPBITS
))))) & 1;
484 if (exp
!= EXPMAX
&& exp
!= 0 && low
!= 0)
486 int lowexp
= ((int)(low
>> HALFFRACBITS
)) & ((1 << EXPBITS
) - 1);
487 int lowsign
= ((int)(low
>> (((HALFFRACBITS
+ EXPBITS
))))) & 1;
491 xlow
= low
& ((((fractype
)1) << HALFFRACBITS
) - 1);
493 xlow
|= (((halffractype
)1) << HALFFRACBITS
);
496 shift
= (FRACBITS
- HALFFRACBITS
) - (exp
- lowexp
);
503 else if (fraction
>= xlow
)
507 /* The high part is a power of two but the full number is lower.
508 This code will leave the implicit 1 in FRACTION, but we'd
509 have added that below anyway. */
510 fraction
= (((fractype
) 1 << FRACBITS
) - xlow
) << 1;
516 fraction
= src
->value_raw
& ((((fractype
)1) << FRACBITS
) - 1);
517 exp
= ((int)(src
->value_raw
>> FRACBITS
)) & ((1 << EXPBITS
) - 1);
518 sign
= ((int)(src
->value_raw
>> (FRACBITS
+ EXPBITS
))) & 1;
525 /* Hmm. Looks like 0 */
532 /* tastes like zero */
533 dst
->class = CLASS_ZERO
;
537 /* Zero exponent with nonzero fraction - it's denormalized,
538 so there isn't a leading implicit one - we'll shift it so
540 dst
->normal_exp
= exp
- EXPBIAS
+ 1;
543 dst
->class = CLASS_NUMBER
;
545 while (fraction
< IMPLICIT_1
)
551 dst
->fraction
.ll
= fraction
;
554 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
)
555 && __builtin_expect (exp
== EXPMAX
, 0))
560 /* Attached to a zero fraction - means infinity */
561 dst
->class = CLASS_INFINITY
;
565 /* Nonzero fraction, means nan */
566 #ifdef QUIET_NAN_NEGATED
567 if ((fraction
& QUIET_NAN
) == 0)
569 if (fraction
& QUIET_NAN
)
572 dst
->class = CLASS_QNAN
;
576 dst
->class = CLASS_SNAN
;
578 /* Keep the fraction part as the nan number */
579 dst
->fraction
.ll
= fraction
;
584 /* Nothing strange about this number */
585 dst
->normal_exp
= exp
- EXPBIAS
;
586 dst
->class = CLASS_NUMBER
;
587 dst
->fraction
.ll
= (fraction
<< NGARDS
) | IMPLICIT_1
;
590 #endif /* L_unpack_df || L_unpack_sf */
592 #if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
593 static const fp_number_type
*
594 _fpadd_parts (fp_number_type
* a
,
596 fp_number_type
* tmp
)
600 /* Put commonly used fields in local variables. */
616 /* Adding infinities with opposite signs yields a NaN. */
617 if (isinf (b
) && a
->sign
!= b
->sign
)
630 tmp
->sign
= a
->sign
& b
->sign
;
640 /* Got two numbers. shift the smaller and increment the exponent till
646 a_normal_exp
= a
->normal_exp
;
647 b_normal_exp
= b
->normal_exp
;
648 a_fraction
= a
->fraction
.ll
;
649 b_fraction
= b
->fraction
.ll
;
651 diff
= a_normal_exp
- b_normal_exp
;
656 if (diff
< FRAC_NBITS
)
660 b_normal_exp
+= diff
;
661 LSHIFT (b_fraction
, diff
);
665 a_normal_exp
+= diff
;
666 LSHIFT (a_fraction
, diff
);
671 /* Somethings's up.. choose the biggest */
672 if (a_normal_exp
> b_normal_exp
)
674 b_normal_exp
= a_normal_exp
;
679 a_normal_exp
= b_normal_exp
;
685 if (a
->sign
!= b
->sign
)
689 tfraction
= -a_fraction
+ b_fraction
;
693 tfraction
= a_fraction
- b_fraction
;
698 tmp
->normal_exp
= a_normal_exp
;
699 tmp
->fraction
.ll
= tfraction
;
704 tmp
->normal_exp
= a_normal_exp
;
705 tmp
->fraction
.ll
= -tfraction
;
707 /* and renormalize it */
709 while (tmp
->fraction
.ll
< IMPLICIT_1
&& tmp
->fraction
.ll
)
711 tmp
->fraction
.ll
<<= 1;
718 tmp
->normal_exp
= a_normal_exp
;
719 tmp
->fraction
.ll
= a_fraction
+ b_fraction
;
721 tmp
->class = CLASS_NUMBER
;
722 /* Now the fraction is added, we have to shift down to renormalize the
725 if (tmp
->fraction
.ll
>= IMPLICIT_2
)
727 LSHIFT (tmp
->fraction
.ll
, 1);
734 add (FLO_type arg_a
, FLO_type arg_b
)
739 const fp_number_type
*res
;
740 FLO_union_type au
, bu
;
748 res
= _fpadd_parts (&a
, &b
, &tmp
);
754 sub (FLO_type arg_a
, FLO_type arg_b
)
759 const fp_number_type
*res
;
760 FLO_union_type au
, bu
;
770 res
= _fpadd_parts (&a
, &b
, &tmp
);
774 #endif /* L_addsub_sf || L_addsub_df */
776 #if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
777 static inline __attribute__ ((__always_inline__
)) const fp_number_type
*
778 _fpmul_parts ( fp_number_type
* a
,
780 fp_number_type
* tmp
)
787 a
->sign
= a
->sign
!= b
->sign
;
792 b
->sign
= a
->sign
!= b
->sign
;
799 a
->sign
= a
->sign
!= b
->sign
;
808 b
->sign
= a
->sign
!= b
->sign
;
813 a
->sign
= a
->sign
!= b
->sign
;
818 b
->sign
= a
->sign
!= b
->sign
;
822 /* Calculate the mantissa by multiplying both numbers to get a
823 twice-as-wide number. */
825 #if defined(NO_DI_MODE) || defined(TFLOAT)
827 fractype x
= a
->fraction
.ll
;
828 fractype ylow
= b
->fraction
.ll
;
832 /* ??? This does multiplies one bit at a time. Optimize. */
833 for (bit
= 0; bit
< FRAC_NBITS
; bit
++)
839 carry
= (low
+= ylow
) < ylow
;
840 high
+= yhigh
+ carry
;
852 /* Multiplying two USIs to get a UDI, we're safe. */
854 UDItype answer
= (UDItype
)a
->fraction
.ll
* (UDItype
)b
->fraction
.ll
;
856 high
= answer
>> BITS_PER_SI
;
860 /* fractype is DImode, but we need the result to be twice as wide.
861 Assuming a widening multiply from DImode to TImode is not
862 available, build one by hand. */
864 USItype nl
= a
->fraction
.ll
;
865 USItype nh
= a
->fraction
.ll
>> BITS_PER_SI
;
866 USItype ml
= b
->fraction
.ll
;
867 USItype mh
= b
->fraction
.ll
>> BITS_PER_SI
;
868 UDItype pp_ll
= (UDItype
) ml
* nl
;
869 UDItype pp_hl
= (UDItype
) mh
* nl
;
870 UDItype pp_lh
= (UDItype
) ml
* nh
;
871 UDItype pp_hh
= (UDItype
) mh
* nh
;
874 UDItype ps_hh__
= pp_hl
+ pp_lh
;
876 res2
+= (UDItype
)1 << BITS_PER_SI
;
877 pp_hl
= (UDItype
)(USItype
)ps_hh__
<< BITS_PER_SI
;
878 res0
= pp_ll
+ pp_hl
;
881 res2
+= (ps_hh__
>> BITS_PER_SI
) + pp_hh
;
888 tmp
->normal_exp
= a
->normal_exp
+ b
->normal_exp
889 + FRAC_NBITS
- (FRACBITS
+ NGARDS
);
890 tmp
->sign
= a
->sign
!= b
->sign
;
891 while (high
>= IMPLICIT_2
)
901 while (high
< IMPLICIT_1
)
911 if (!ROUND_TOWARDS_ZERO
&& (high
& GARDMASK
) == GARDMSB
)
913 if (high
& (1 << NGARDS
))
915 /* Because we're half way, we would round to even by adding
916 GARDROUND + 1, except that's also done in the packing
917 function, and rounding twice will lose precision and cause
918 the result to be too far off. Example: 32-bit floats with
919 bit patterns 0xfff * 0x3f800400 ~= 0xfff (less than 0.5ulp
920 off), not 0x1000 (more than 0.5ulp off). */
924 /* We're a further than half way by a small amount corresponding
925 to the bits set in "low". Knowing that, we round here and
926 not in pack_d, because there we don't have "low" available
928 high
+= GARDROUND
+ 1;
930 /* Avoid further rounding in pack_d. */
931 high
&= ~(fractype
) GARDMASK
;
934 tmp
->fraction
.ll
= high
;
935 tmp
->class = CLASS_NUMBER
;
940 multiply (FLO_type arg_a
, FLO_type arg_b
)
945 const fp_number_type
*res
;
946 FLO_union_type au
, bu
;
954 res
= _fpmul_parts (&a
, &b
, &tmp
);
958 #endif /* L_mul_sf || L_mul_df || L_mul_tf */
960 #if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
961 static inline __attribute__ ((__always_inline__
)) const fp_number_type
*
962 _fpdiv_parts (fp_number_type
* a
,
967 fractype denominator
;
979 a
->sign
= a
->sign
^ b
->sign
;
981 if (isinf (a
) || iszero (a
))
983 if (a
->class == b
->class)
996 a
->class = CLASS_INFINITY
;
1000 /* Calculate the mantissa by multiplying both 64bit numbers to get a
1004 ( numerator / denominator) * 2^(numerator exponent - denominator exponent)
1007 a
->normal_exp
= a
->normal_exp
- b
->normal_exp
;
1008 numerator
= a
->fraction
.ll
;
1009 denominator
= b
->fraction
.ll
;
1011 if (numerator
< denominator
)
1013 /* Fraction will be less than 1.0 */
1019 /* ??? Does divide one bit at a time. Optimize. */
1022 if (numerator
>= denominator
)
1025 numerator
-= denominator
;
1031 if (!ROUND_TOWARDS_ZERO
&& (quotient
& GARDMASK
) == GARDMSB
)
1033 if (quotient
& (1 << NGARDS
))
1035 /* Because we're half way, we would round to even by adding
1036 GARDROUND + 1, except that's also done in the packing
1037 function, and rounding twice will lose precision and cause
1038 the result to be too far off. */
1042 /* We're a further than half way by the small amount
1043 corresponding to the bits set in "numerator". Knowing
1044 that, we round here and not in pack_d, because there we
1045 don't have "numerator" available anymore. */
1046 quotient
+= GARDROUND
+ 1;
1048 /* Avoid further rounding in pack_d. */
1049 quotient
&= ~(fractype
) GARDMASK
;
1053 a
->fraction
.ll
= quotient
;
1059 divide (FLO_type arg_a
, FLO_type arg_b
)
1063 const fp_number_type
*res
;
1064 FLO_union_type au
, bu
;
1072 res
= _fpdiv_parts (&a
, &b
);
1074 return pack_d (res
);
1076 #endif /* L_div_sf || L_div_df */
1078 #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df) \
1079 || defined(L_fpcmp_parts_tf)
1080 /* according to the demo, fpcmp returns a comparison with 0... thus
1087 __fpcmp_parts (fp_number_type
* a
, fp_number_type
* b
)
1090 /* either nan -> unordered. Must be checked outside of this routine. */
1091 if (isnan (a
) && isnan (b
))
1093 return 1; /* still unordered! */
1097 if (isnan (a
) || isnan (b
))
1099 return 1; /* how to indicate unordered compare? */
1101 if (isinf (a
) && isinf (b
))
1103 /* +inf > -inf, but +inf != +inf */
1104 /* b \a| +inf(0)| -inf(1)
1105 ______\+--------+--------
1106 +inf(0)| a==b(0)| a<b(-1)
1107 -------+--------+--------
1108 -inf(1)| a>b(1) | a==b(0)
1109 -------+--------+--------
1110 So since unordered must be nonzero, just line up the columns...
1112 return b
->sign
- a
->sign
;
1114 /* but not both... */
1117 return a
->sign
? -1 : 1;
1121 return b
->sign
? 1 : -1;
1123 if (iszero (a
) && iszero (b
))
1129 return b
->sign
? 1 : -1;
1133 return a
->sign
? -1 : 1;
1135 /* now both are "normal". */
1136 if (a
->sign
!= b
->sign
)
1138 /* opposite signs */
1139 return a
->sign
? -1 : 1;
1141 /* same sign; exponents? */
1142 if (a
->normal_exp
> b
->normal_exp
)
1144 return a
->sign
? -1 : 1;
1146 if (a
->normal_exp
< b
->normal_exp
)
1148 return a
->sign
? 1 : -1;
1150 /* same exponents; check size. */
1151 if (a
->fraction
.ll
> b
->fraction
.ll
)
1153 return a
->sign
? -1 : 1;
1155 if (a
->fraction
.ll
< b
->fraction
.ll
)
1157 return a
->sign
? 1 : -1;
1159 /* after all that, they're equal. */
1164 #if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compoare_tf)
1166 compare (FLO_type arg_a
, FLO_type arg_b
)
1170 FLO_union_type au
, bu
;
1178 return __fpcmp_parts (&a
, &b
);
1180 #endif /* L_compare_sf || L_compare_df */
1182 #ifndef US_SOFTWARE_GOFAST
1184 /* These should be optimized for their specific tasks someday. */
1186 #if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
1188 _eq_f2 (FLO_type arg_a
, FLO_type arg_b
)
1192 FLO_union_type au
, bu
;
1200 if (isnan (&a
) || isnan (&b
))
1201 return 1; /* false, truth == 0 */
1203 return __fpcmp_parts (&a
, &b
) ;
1205 #endif /* L_eq_sf || L_eq_df */
1207 #if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
1209 _ne_f2 (FLO_type arg_a
, FLO_type arg_b
)
1213 FLO_union_type au
, bu
;
1221 if (isnan (&a
) || isnan (&b
))
1222 return 1; /* true, truth != 0 */
1224 return __fpcmp_parts (&a
, &b
) ;
1226 #endif /* L_ne_sf || L_ne_df */
1228 #if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
1230 _gt_f2 (FLO_type arg_a
, FLO_type arg_b
)
1234 FLO_union_type au
, bu
;
1242 if (isnan (&a
) || isnan (&b
))
1243 return -1; /* false, truth > 0 */
1245 return __fpcmp_parts (&a
, &b
);
1247 #endif /* L_gt_sf || L_gt_df */
1249 #if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
1251 _ge_f2 (FLO_type arg_a
, FLO_type arg_b
)
1255 FLO_union_type au
, bu
;
1263 if (isnan (&a
) || isnan (&b
))
1264 return -1; /* false, truth >= 0 */
1265 return __fpcmp_parts (&a
, &b
) ;
1267 #endif /* L_ge_sf || L_ge_df */
1269 #if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
1271 _lt_f2 (FLO_type arg_a
, FLO_type arg_b
)
1275 FLO_union_type au
, bu
;
1283 if (isnan (&a
) || isnan (&b
))
1284 return 1; /* false, truth < 0 */
1286 return __fpcmp_parts (&a
, &b
);
1288 #endif /* L_lt_sf || L_lt_df */
1290 #if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
1292 _le_f2 (FLO_type arg_a
, FLO_type arg_b
)
1296 FLO_union_type au
, bu
;
1304 if (isnan (&a
) || isnan (&b
))
1305 return 1; /* false, truth <= 0 */
1307 return __fpcmp_parts (&a
, &b
) ;
1309 #endif /* L_le_sf || L_le_df */
1311 #endif /* ! US_SOFTWARE_GOFAST */
1313 #if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
1315 _unord_f2 (FLO_type arg_a
, FLO_type arg_b
)
1319 FLO_union_type au
, bu
;
1327 return (isnan (&a
) || isnan (&b
));
1329 #endif /* L_unord_sf || L_unord_df */
1331 #if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
1333 si_to_float (SItype arg_a
)
1337 in
.class = CLASS_NUMBER
;
1338 in
.sign
= arg_a
< 0;
1341 in
.class = CLASS_ZERO
;
1347 in
.normal_exp
= FRACBITS
+ NGARDS
;
1350 /* Special case for minint, since there is no +ve integer
1351 representation for it */
1352 if (arg_a
== (- MAX_SI_INT
- 1))
1354 return (FLO_type
)(- MAX_SI_INT
- 1);
1361 in
.fraction
.ll
= uarg
;
1362 shift
= clzusi (uarg
) - (BITS_PER_SI
- 1 - FRACBITS
- NGARDS
);
1365 in
.fraction
.ll
<<= shift
;
1366 in
.normal_exp
-= shift
;
1369 return pack_d (&in
);
1371 #endif /* L_si_to_sf || L_si_to_df */
1373 #if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
1375 usi_to_float (USItype arg_a
)
1382 in
.class = CLASS_ZERO
;
1387 in
.class = CLASS_NUMBER
;
1388 in
.normal_exp
= FRACBITS
+ NGARDS
;
1389 in
.fraction
.ll
= arg_a
;
1391 shift
= clzusi (arg_a
) - (BITS_PER_SI
- 1 - FRACBITS
- NGARDS
);
1394 fractype guard
= in
.fraction
.ll
& (((fractype
)1 << -shift
) - 1);
1395 in
.fraction
.ll
>>= -shift
;
1396 in
.fraction
.ll
|= (guard
!= 0);
1397 in
.normal_exp
-= shift
;
1401 in
.fraction
.ll
<<= shift
;
1402 in
.normal_exp
-= shift
;
1405 return pack_d (&in
);
1409 #if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
1411 float_to_si (FLO_type arg_a
)
1424 /* get reasonable MAX_SI_INT... */
1426 return a
.sign
? (-MAX_SI_INT
)-1 : MAX_SI_INT
;
1427 /* it is a number, but a small one */
1428 if (a
.normal_exp
< 0)
1430 if (a
.normal_exp
> BITS_PER_SI
- 2)
1431 return a
.sign
? (-MAX_SI_INT
)-1 : MAX_SI_INT
;
1432 tmp
= a
.fraction
.ll
>> ((FRACBITS
+ NGARDS
) - a
.normal_exp
);
1433 return a
.sign
? (-tmp
) : (tmp
);
1435 #endif /* L_sf_to_si || L_df_to_si */
1437 #if defined(L_sf_to_usi) || defined(L_df_to_usi) || defined(L_tf_to_usi)
1438 #if defined US_SOFTWARE_GOFAST || defined(L_tf_to_usi)
1439 /* While libgcc2.c defines its own __fixunssfsi and __fixunsdfsi routines,
1440 we also define them for GOFAST because the ones in libgcc2.c have the
1441 wrong names and I'd rather define these here and keep GOFAST CYG-LOC's
1442 out of libgcc2.c. We can't define these here if not GOFAST because then
1443 there'd be duplicate copies. */
1446 float_to_usi (FLO_type arg_a
)
1458 /* it is a negative number */
1461 /* get reasonable MAX_USI_INT... */
1464 /* it is a number, but a small one */
1465 if (a
.normal_exp
< 0)
1467 if (a
.normal_exp
> BITS_PER_SI
- 1)
1469 else if (a
.normal_exp
> (FRACBITS
+ NGARDS
))
1470 return a
.fraction
.ll
<< (a
.normal_exp
- (FRACBITS
+ NGARDS
));
1472 return a
.fraction
.ll
>> ((FRACBITS
+ NGARDS
) - a
.normal_exp
);
1474 #endif /* US_SOFTWARE_GOFAST */
1475 #endif /* L_sf_to_usi || L_df_to_usi */
1477 #if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
1479 negate (FLO_type arg_a
)
1490 #endif /* L_negate_sf || L_negate_df */
1494 #if defined(L_make_sf)
1496 __make_fp(fp_class_type
class,
1505 in
.normal_exp
= exp
;
1506 in
.fraction
.ll
= frac
;
1507 return pack_d (&in
);
1509 #endif /* L_make_sf */
1513 /* This enables one to build an fp library that supports float but not double.
1514 Otherwise, we would get an undefined reference to __make_dp.
1515 This is needed for some 8-bit ports that can't handle well values that
1516 are 8-bytes in size, so we just don't support double for them at all. */
1518 #if defined(L_sf_to_df)
1520 sf_to_df (SFtype arg_a
)
1526 unpack_d (&au
, &in
);
1528 return __make_dp (in
.class, in
.sign
, in
.normal_exp
,
1529 ((UDItype
) in
.fraction
.ll
) << F_D_BITOFF
);
1531 #endif /* L_sf_to_df */
1533 #if defined(L_sf_to_tf) && defined(TMODES)
1535 sf_to_tf (SFtype arg_a
)
1541 unpack_d (&au
, &in
);
1543 return __make_tp (in
.class, in
.sign
, in
.normal_exp
,
1544 ((UTItype
) in
.fraction
.ll
) << F_T_BITOFF
);
1546 #endif /* L_sf_to_df */
1548 #endif /* ! FLOAT_ONLY */
1553 extern SFtype
__make_fp (fp_class_type
, unsigned int, int, USItype
);
1555 #if defined(L_make_df)
1557 __make_dp (fp_class_type
class, unsigned int sign
, int exp
, UDItype frac
)
1563 in
.normal_exp
= exp
;
1564 in
.fraction
.ll
= frac
;
1565 return pack_d (&in
);
1567 #endif /* L_make_df */
1569 #if defined(L_df_to_sf)
1571 df_to_sf (DFtype arg_a
)
1578 unpack_d (&au
, &in
);
1580 sffrac
= in
.fraction
.ll
>> F_D_BITOFF
;
1582 /* We set the lowest guard bit in SFFRAC if we discarded any non
1584 if ((in
.fraction
.ll
& (((USItype
) 1 << F_D_BITOFF
) - 1)) != 0)
1587 return __make_fp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1589 #endif /* L_df_to_sf */
1591 #if defined(L_df_to_tf) && defined(TMODES) \
1592 && !defined(FLOAT) && !defined(TFLOAT)
1594 df_to_tf (DFtype arg_a
)
1600 unpack_d (&au
, &in
);
1602 return __make_tp (in
.class, in
.sign
, in
.normal_exp
,
1603 ((UTItype
) in
.fraction
.ll
) << D_T_BITOFF
);
1605 #endif /* L_sf_to_df */
1608 #if defined(L_make_tf)
1610 __make_tp(fp_class_type
class,
1619 in
.normal_exp
= exp
;
1620 in
.fraction
.ll
= frac
;
1621 return pack_d (&in
);
1623 #endif /* L_make_tf */
1625 #if defined(L_tf_to_df)
1627 tf_to_df (TFtype arg_a
)
1634 unpack_d (&au
, &in
);
1636 sffrac
= in
.fraction
.ll
>> D_T_BITOFF
;
1638 /* We set the lowest guard bit in SFFRAC if we discarded any non
1640 if ((in
.fraction
.ll
& (((UTItype
) 1 << D_T_BITOFF
) - 1)) != 0)
1643 return __make_dp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1645 #endif /* L_tf_to_df */
1647 #if defined(L_tf_to_sf)
1649 tf_to_sf (TFtype arg_a
)
1656 unpack_d (&au
, &in
);
1658 sffrac
= in
.fraction
.ll
>> F_T_BITOFF
;
1660 /* We set the lowest guard bit in SFFRAC if we discarded any non
1662 if ((in
.fraction
.ll
& (((UTItype
) 1 << F_T_BITOFF
) - 1)) != 0)
1665 return __make_fp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1667 #endif /* L_tf_to_sf */
1670 #endif /* ! FLOAT */
1671 #endif /* !EXTENDED_FLOAT_STUBS */