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 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 2, or (at your option) any later
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combine
22 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
23 WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 You should have received a copy of the GNU General Public License
28 along with GCC; see the file COPYING. If not, write to the Free
29 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
32 /* This implements IEEE 754 format arithmetic, but does not provide a
33 mechanism for setting the rounding mode, or for generating or handling
36 The original code by Steve Chamberlain, hacked by Mark Eichin and Jim
37 Wilson, all of Cygnus Support. */
39 /* The intended way to use this file is to make two copies, add `#define FLOAT'
40 to one copy, then compile both copies and add them to libgcc.a. */
43 #include "coretypes.h"
45 #include "config/fp-bit.h"
47 /* The following macros can be defined to change the behavior of this file:
48 FLOAT: Implement a `float', aka SFmode, fp library. If this is not
49 defined, then this file implements a `double', aka DFmode, fp library.
50 FLOAT_ONLY: Used with FLOAT, to implement a `float' only library, i.e.
51 don't include float->double conversion which requires the double library.
52 This is useful only for machines which can't support doubles, e.g. some
54 CMPtype: Specify the type that floating point compares should return.
55 This defaults to SItype, aka int.
56 US_SOFTWARE_GOFAST: This makes all entry points use the same names as the
57 US Software goFast library.
58 _DEBUG_BITFLOAT: This makes debugging the code a little easier, by adding
59 two integers to the FLO_union_type.
60 NO_DENORMALS: Disable handling of denormals.
61 NO_NANS: Disable nan and infinity handling
62 SMALL_MACHINE: Useful when operations on QIs and HIs are faster
65 /* We don't currently support extended floats (long doubles) on machines
66 without hardware to deal with them.
68 These stubs are just to keep the linker from complaining about unresolved
69 references which can be pulled in from libio & libstdc++, even if the
70 user isn't using long doubles. However, they may generate an unresolved
71 external to abort if abort is not used by the function, and the stubs
72 are referenced from within libc, since libgcc goes before and after the
75 #ifdef DECLARE_LIBRARY_RENAMES
76 DECLARE_LIBRARY_RENAMES
79 #ifdef EXTENDED_FLOAT_STUBS
80 extern void abort (void);
81 void __extendsfxf2 (void) { abort(); }
82 void __extenddfxf2 (void) { abort(); }
83 void __truncxfdf2 (void) { abort(); }
84 void __truncxfsf2 (void) { abort(); }
85 void __fixxfsi (void) { abort(); }
86 void __floatsixf (void) { abort(); }
87 void __addxf3 (void) { abort(); }
88 void __subxf3 (void) { abort(); }
89 void __mulxf3 (void) { abort(); }
90 void __divxf3 (void) { abort(); }
91 void __negxf2 (void) { abort(); }
92 void __eqxf2 (void) { abort(); }
93 void __nexf2 (void) { abort(); }
94 void __gtxf2 (void) { abort(); }
95 void __gexf2 (void) { abort(); }
96 void __lexf2 (void) { abort(); }
97 void __ltxf2 (void) { abort(); }
99 void __extendsftf2 (void) { abort(); }
100 void __extenddftf2 (void) { abort(); }
101 void __trunctfdf2 (void) { abort(); }
102 void __trunctfsf2 (void) { abort(); }
103 void __fixtfsi (void) { abort(); }
104 void __floatsitf (void) { abort(); }
105 void __addtf3 (void) { abort(); }
106 void __subtf3 (void) { abort(); }
107 void __multf3 (void) { abort(); }
108 void __divtf3 (void) { abort(); }
109 void __negtf2 (void) { abort(); }
110 void __eqtf2 (void) { abort(); }
111 void __netf2 (void) { abort(); }
112 void __gttf2 (void) { abort(); }
113 void __getf2 (void) { abort(); }
114 void __letf2 (void) { abort(); }
115 void __lttf2 (void) { abort(); }
116 #else /* !EXTENDED_FLOAT_STUBS, rest of file */
118 /* IEEE "special" number predicates */
127 #if defined L_thenan_sf
128 const fp_number_type __thenan_sf
= { CLASS_SNAN
, 0, 0, {(fractype
) 0} };
129 #elif defined L_thenan_df
130 const fp_number_type __thenan_df
= { CLASS_SNAN
, 0, 0, {(fractype
) 0} };
131 #elif defined L_thenan_tf
132 const fp_number_type __thenan_tf
= { CLASS_SNAN
, 0, 0, {(fractype
) 0} };
134 extern const fp_number_type __thenan_tf
;
136 extern const fp_number_type __thenan_sf
;
138 extern const fp_number_type __thenan_df
;
142 static fp_number_type
*
145 /* Discard the const qualifier... */
147 return (fp_number_type
*) (& __thenan_tf
);
149 return (fp_number_type
*) (& __thenan_sf
);
151 return (fp_number_type
*) (& __thenan_df
);
157 isnan ( fp_number_type
* x
)
159 return __builtin_expect (x
->class == CLASS_SNAN
|| x
->class == CLASS_QNAN
,
165 isinf ( fp_number_type
* x
)
167 return __builtin_expect (x
->class == CLASS_INFINITY
, 0);
174 iszero ( fp_number_type
* x
)
176 return x
->class == CLASS_ZERO
;
181 flip_sign ( fp_number_type
* x
)
186 /* Count leading zeroes in N. */
191 extern int __clzsi2 (USItype
);
192 if (sizeof (USItype
) == sizeof (unsigned int))
193 return __builtin_clz (n
);
194 else if (sizeof (USItype
) == sizeof (unsigned long))
195 return __builtin_clzl (n
);
196 else if (sizeof (USItype
) == sizeof (unsigned long long))
197 return __builtin_clzll (n
);
202 extern FLO_type
pack_d ( fp_number_type
* );
204 #if defined(L_pack_df) || defined(L_pack_sf) || defined(L_pack_tf)
206 pack_d ( fp_number_type
* src
)
209 fractype fraction
= src
->fraction
.ll
; /* wasn't unsigned before? */
210 int sign
= src
->sign
;
213 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
) && (isnan (src
) || isinf (src
)))
215 /* We can't represent these values accurately. By using the
216 largest possible magnitude, we guarantee that the conversion
217 of infinity is at least as big as any finite number. */
219 fraction
= ((fractype
) 1 << FRACBITS
) - 1;
221 else if (isnan (src
))
224 if (src
->class == CLASS_QNAN
|| 1)
226 #ifdef QUIET_NAN_NEGATED
227 fraction
|= QUIET_NAN
- 1;
229 fraction
|= QUIET_NAN
;
233 else if (isinf (src
))
238 else if (iszero (src
))
243 else if (fraction
== 0)
249 if (__builtin_expect (src
->normal_exp
< NORMAL_EXPMIN
, 0))
252 /* Go straight to a zero representation if denormals are not
253 supported. The denormal handling would be harmless but
254 isn't unnecessary. */
257 #else /* NO_DENORMALS */
258 /* This number's exponent is too low to fit into the bits
259 available in the number, so we'll store 0 in the exponent and
260 shift the fraction to the right to make up for it. */
262 int shift
= NORMAL_EXPMIN
- src
->normal_exp
;
266 if (shift
> FRAC_NBITS
- NGARDS
)
268 /* No point shifting, since it's more that 64 out. */
273 int lowbit
= (fraction
& (((fractype
)1 << shift
) - 1)) ? 1 : 0;
274 fraction
= (fraction
>> shift
) | lowbit
;
276 if ((fraction
& GARDMASK
) == GARDMSB
)
278 if ((fraction
& (1 << NGARDS
)))
279 fraction
+= GARDROUND
+ 1;
283 /* Add to the guards to round up. */
284 fraction
+= GARDROUND
;
286 /* Perhaps the rounding means we now need to change the
287 exponent, because the fraction is no longer denormal. */
288 if (fraction
>= IMPLICIT_1
)
293 #endif /* NO_DENORMALS */
295 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
)
296 && __builtin_expect (src
->normal_exp
> EXPBIAS
, 0))
303 exp
= src
->normal_exp
+ EXPBIAS
;
304 if (!ROUND_TOWARDS_ZERO
)
306 /* IF the gard bits are the all zero, but the first, then we're
307 half way between two numbers, choose the one which makes the
308 lsb of the answer 0. */
309 if ((fraction
& GARDMASK
) == GARDMSB
)
311 if (fraction
& (1 << NGARDS
))
312 fraction
+= GARDROUND
+ 1;
316 /* Add a one to the guards to round up */
317 fraction
+= GARDROUND
;
319 if (fraction
>= IMPLICIT_2
)
327 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
) && exp
> EXPMAX
)
329 /* Saturate on overflow. */
331 fraction
= ((fractype
) 1 << FRACBITS
) - 1;
336 /* We previously used bitfields to store the number, but this doesn't
337 handle little/big endian systems conveniently, so use shifts and
339 #ifdef FLOAT_BIT_ORDER_MISMATCH
340 dst
.bits
.fraction
= fraction
;
342 dst
.bits
.sign
= sign
;
344 # if defined TFLOAT && defined HALFFRACBITS
346 halffractype high
, low
, unity
;
349 unity
= (halffractype
) 1 << HALFFRACBITS
;
351 /* Set HIGH to the high double's significand, masking out the implicit 1.
352 Set LOW to the low double's full significand. */
353 high
= (fraction
>> (FRACBITS
- HALFFRACBITS
)) & (unity
- 1);
354 low
= fraction
& (unity
* 2 - 1);
356 /* Get the initial sign and exponent of the low double. */
357 lowexp
= exp
- HALFFRACBITS
- 1;
360 /* HIGH should be rounded like a normal double, making |LOW| <=
361 0.5 ULP of HIGH. Assume round-to-nearest. */
363 if (low
> unity
|| (low
== unity
&& (high
& 1) == 1))
365 /* Round HIGH up and adjust LOW to match. */
369 /* May make it infinite, but that's OK. */
373 low
= unity
* 2 - low
;
377 high
|= (halffractype
) exp
<< HALFFRACBITS
;
378 high
|= (halffractype
) sign
<< (HALFFRACBITS
+ EXPBITS
);
380 if (exp
== EXPMAX
|| exp
== 0 || low
== 0)
384 while (lowexp
> 0 && low
< unity
)
392 halffractype roundmsb
, round
;
396 roundmsb
= (1 << (shift
- 1));
397 round
= low
& ((roundmsb
<< 1) - 1);
402 if (round
> roundmsb
|| (round
== roundmsb
&& (low
& 1) == 1))
406 /* LOW rounds up to the smallest normal number. */
412 low
|= (halffractype
) lowexp
<< HALFFRACBITS
;
413 low
|= (halffractype
) lowsign
<< (HALFFRACBITS
+ EXPBITS
);
415 dst
.value_raw
= ((fractype
) high
<< HALFSHIFT
) | low
;
418 dst
.value_raw
= fraction
& ((((fractype
)1) << FRACBITS
) - (fractype
)1);
419 dst
.value_raw
|= ((fractype
) (exp
& ((1 << EXPBITS
) - 1))) << FRACBITS
;
420 dst
.value_raw
|= ((fractype
) (sign
& 1)) << (FRACBITS
| EXPBITS
);
424 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
427 qrtrfractype tmp1
= dst
.words
[0];
428 qrtrfractype tmp2
= dst
.words
[1];
429 dst
.words
[0] = dst
.words
[3];
430 dst
.words
[1] = dst
.words
[2];
436 halffractype tmp
= dst
.words
[0];
437 dst
.words
[0] = dst
.words
[1];
447 #if defined(L_unpack_df) || defined(L_unpack_sf) || defined(L_unpack_tf)
449 unpack_d (FLO_union_type
* src
, fp_number_type
* dst
)
451 /* We previously used bitfields to store the number, but this doesn't
452 handle little/big endian systems conveniently, so use shifts and
458 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
459 FLO_union_type swapped
;
462 swapped
.words
[0] = src
->words
[3];
463 swapped
.words
[1] = src
->words
[2];
464 swapped
.words
[2] = src
->words
[1];
465 swapped
.words
[3] = src
->words
[0];
467 swapped
.words
[0] = src
->words
[1];
468 swapped
.words
[1] = src
->words
[0];
473 #ifdef FLOAT_BIT_ORDER_MISMATCH
474 fraction
= src
->bits
.fraction
;
476 sign
= src
->bits
.sign
;
478 # if defined TFLOAT && defined HALFFRACBITS
480 halffractype high
, low
;
482 high
= src
->value_raw
>> HALFSHIFT
;
483 low
= src
->value_raw
& (((fractype
)1 << HALFSHIFT
) - 1);
485 fraction
= high
& ((((fractype
)1) << HALFFRACBITS
) - 1);
486 fraction
<<= FRACBITS
- HALFFRACBITS
;
487 exp
= ((int)(high
>> HALFFRACBITS
)) & ((1 << EXPBITS
) - 1);
488 sign
= ((int)(high
>> (((HALFFRACBITS
+ EXPBITS
))))) & 1;
490 if (exp
!= EXPMAX
&& exp
!= 0 && low
!= 0)
492 int lowexp
= ((int)(low
>> HALFFRACBITS
)) & ((1 << EXPBITS
) - 1);
493 int lowsign
= ((int)(low
>> (((HALFFRACBITS
+ EXPBITS
))))) & 1;
497 xlow
= low
& ((((fractype
)1) << HALFFRACBITS
) - 1);
499 xlow
|= (((halffractype
)1) << HALFFRACBITS
);
502 shift
= (FRACBITS
- HALFFRACBITS
) - (exp
- lowexp
);
509 else if (fraction
>= xlow
)
513 /* The high part is a power of two but the full number is lower.
514 This code will leave the implicit 1 in FRACTION, but we'd
515 have added that below anyway. */
516 fraction
= (((fractype
) 1 << FRACBITS
) - xlow
) << 1;
522 fraction
= src
->value_raw
& ((((fractype
)1) << FRACBITS
) - 1);
523 exp
= ((int)(src
->value_raw
>> FRACBITS
)) & ((1 << EXPBITS
) - 1);
524 sign
= ((int)(src
->value_raw
>> (FRACBITS
+ EXPBITS
))) & 1;
531 /* Hmm. Looks like 0 */
538 /* tastes like zero */
539 dst
->class = CLASS_ZERO
;
543 /* Zero exponent with nonzero fraction - it's denormalized,
544 so there isn't a leading implicit one - we'll shift it so
546 dst
->normal_exp
= exp
- EXPBIAS
+ 1;
549 dst
->class = CLASS_NUMBER
;
551 while (fraction
< IMPLICIT_1
)
557 dst
->fraction
.ll
= fraction
;
560 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
)
561 && __builtin_expect (exp
== EXPMAX
, 0))
566 /* Attached to a zero fraction - means infinity */
567 dst
->class = CLASS_INFINITY
;
571 /* Nonzero fraction, means nan */
572 #ifdef QUIET_NAN_NEGATED
573 if ((fraction
& QUIET_NAN
) == 0)
575 if (fraction
& QUIET_NAN
)
578 dst
->class = CLASS_QNAN
;
582 dst
->class = CLASS_SNAN
;
584 /* Keep the fraction part as the nan number */
585 dst
->fraction
.ll
= fraction
;
590 /* Nothing strange about this number */
591 dst
->normal_exp
= exp
- EXPBIAS
;
592 dst
->class = CLASS_NUMBER
;
593 dst
->fraction
.ll
= (fraction
<< NGARDS
) | IMPLICIT_1
;
596 #endif /* L_unpack_df || L_unpack_sf */
598 #if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
599 static fp_number_type
*
600 _fpadd_parts (fp_number_type
* a
,
602 fp_number_type
* tmp
)
606 /* Put commonly used fields in local variables. */
622 /* Adding infinities with opposite signs yields a NaN. */
623 if (isinf (b
) && a
->sign
!= b
->sign
)
636 tmp
->sign
= a
->sign
& b
->sign
;
646 /* Got two numbers. shift the smaller and increment the exponent till
652 a_normal_exp
= a
->normal_exp
;
653 b_normal_exp
= b
->normal_exp
;
654 a_fraction
= a
->fraction
.ll
;
655 b_fraction
= b
->fraction
.ll
;
657 diff
= a_normal_exp
- b_normal_exp
;
662 if (diff
< FRAC_NBITS
)
666 b_normal_exp
+= diff
;
667 LSHIFT (b_fraction
, diff
);
671 a_normal_exp
+= diff
;
672 LSHIFT (a_fraction
, diff
);
677 /* Somethings's up.. choose the biggest */
678 if (a_normal_exp
> b_normal_exp
)
680 b_normal_exp
= a_normal_exp
;
685 a_normal_exp
= b_normal_exp
;
691 if (a
->sign
!= b
->sign
)
695 tfraction
= -a_fraction
+ b_fraction
;
699 tfraction
= a_fraction
- b_fraction
;
704 tmp
->normal_exp
= a_normal_exp
;
705 tmp
->fraction
.ll
= tfraction
;
710 tmp
->normal_exp
= a_normal_exp
;
711 tmp
->fraction
.ll
= -tfraction
;
713 /* and renormalize it */
715 while (tmp
->fraction
.ll
< IMPLICIT_1
&& tmp
->fraction
.ll
)
717 tmp
->fraction
.ll
<<= 1;
724 tmp
->normal_exp
= a_normal_exp
;
725 tmp
->fraction
.ll
= a_fraction
+ b_fraction
;
727 tmp
->class = CLASS_NUMBER
;
728 /* Now the fraction is added, we have to shift down to renormalize the
731 if (tmp
->fraction
.ll
>= IMPLICIT_2
)
733 LSHIFT (tmp
->fraction
.ll
, 1);
741 add (FLO_type arg_a
, FLO_type arg_b
)
747 FLO_union_type au
, bu
;
755 res
= _fpadd_parts (&a
, &b
, &tmp
);
761 sub (FLO_type arg_a
, FLO_type arg_b
)
767 FLO_union_type au
, bu
;
777 res
= _fpadd_parts (&a
, &b
, &tmp
);
781 #endif /* L_addsub_sf || L_addsub_df */
783 #if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
784 static inline __attribute__ ((__always_inline__
)) fp_number_type
*
785 _fpmul_parts ( fp_number_type
* a
,
787 fp_number_type
* tmp
)
794 a
->sign
= a
->sign
!= b
->sign
;
799 b
->sign
= a
->sign
!= b
->sign
;
806 a
->sign
= a
->sign
!= b
->sign
;
815 b
->sign
= a
->sign
!= b
->sign
;
820 a
->sign
= a
->sign
!= b
->sign
;
825 b
->sign
= a
->sign
!= b
->sign
;
829 /* Calculate the mantissa by multiplying both numbers to get a
830 twice-as-wide number. */
832 #if defined(NO_DI_MODE) || defined(TFLOAT)
834 fractype x
= a
->fraction
.ll
;
835 fractype ylow
= b
->fraction
.ll
;
839 /* ??? This does multiplies one bit at a time. Optimize. */
840 for (bit
= 0; bit
< FRAC_NBITS
; bit
++)
846 carry
= (low
+= ylow
) < ylow
;
847 high
+= yhigh
+ carry
;
859 /* Multiplying two USIs to get a UDI, we're safe. */
861 UDItype answer
= (UDItype
)a
->fraction
.ll
* (UDItype
)b
->fraction
.ll
;
863 high
= answer
>> BITS_PER_SI
;
867 /* fractype is DImode, but we need the result to be twice as wide.
868 Assuming a widening multiply from DImode to TImode is not
869 available, build one by hand. */
871 USItype nl
= a
->fraction
.ll
;
872 USItype nh
= a
->fraction
.ll
>> BITS_PER_SI
;
873 USItype ml
= b
->fraction
.ll
;
874 USItype mh
= b
->fraction
.ll
>> BITS_PER_SI
;
875 UDItype pp_ll
= (UDItype
) ml
* nl
;
876 UDItype pp_hl
= (UDItype
) mh
* nl
;
877 UDItype pp_lh
= (UDItype
) ml
* nh
;
878 UDItype pp_hh
= (UDItype
) mh
* nh
;
881 UDItype ps_hh__
= pp_hl
+ pp_lh
;
883 res2
+= (UDItype
)1 << BITS_PER_SI
;
884 pp_hl
= (UDItype
)(USItype
)ps_hh__
<< BITS_PER_SI
;
885 res0
= pp_ll
+ pp_hl
;
888 res2
+= (ps_hh__
>> BITS_PER_SI
) + pp_hh
;
895 tmp
->normal_exp
= a
->normal_exp
+ b
->normal_exp
896 + FRAC_NBITS
- (FRACBITS
+ NGARDS
);
897 tmp
->sign
= a
->sign
!= b
->sign
;
898 while (high
>= IMPLICIT_2
)
908 while (high
< IMPLICIT_1
)
918 if (!ROUND_TOWARDS_ZERO
&& (high
& GARDMASK
) == GARDMSB
)
920 if (high
& (1 << NGARDS
))
922 /* Because we're half way, we would round to even by adding
923 GARDROUND + 1, except that's also done in the packing
924 function, and rounding twice will lose precision and cause
925 the result to be too far off. Example: 32-bit floats with
926 bit patterns 0xfff * 0x3f800400 ~= 0xfff (less than 0.5ulp
927 off), not 0x1000 (more than 0.5ulp off). */
931 /* We're a further than half way by a small amount corresponding
932 to the bits set in "low". Knowing that, we round here and
933 not in pack_d, because there we don't have "low" available
935 high
+= GARDROUND
+ 1;
937 /* Avoid further rounding in pack_d. */
938 high
&= ~(fractype
) GARDMASK
;
941 tmp
->fraction
.ll
= high
;
942 tmp
->class = CLASS_NUMBER
;
947 multiply (FLO_type arg_a
, FLO_type arg_b
)
953 FLO_union_type au
, bu
;
961 res
= _fpmul_parts (&a
, &b
, &tmp
);
965 #endif /* L_mul_sf || L_mul_df || L_mul_tf */
967 #if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
968 static inline __attribute__ ((__always_inline__
)) fp_number_type
*
969 _fpdiv_parts (fp_number_type
* a
,
974 fractype denominator
;
986 a
->sign
= a
->sign
^ b
->sign
;
988 if (isinf (a
) || iszero (a
))
990 if (a
->class == b
->class)
1003 a
->class = CLASS_INFINITY
;
1007 /* Calculate the mantissa by multiplying both 64bit numbers to get a
1011 ( numerator / denominator) * 2^(numerator exponent - denominator exponent)
1014 a
->normal_exp
= a
->normal_exp
- b
->normal_exp
;
1015 numerator
= a
->fraction
.ll
;
1016 denominator
= b
->fraction
.ll
;
1018 if (numerator
< denominator
)
1020 /* Fraction will be less than 1.0 */
1026 /* ??? Does divide one bit at a time. Optimize. */
1029 if (numerator
>= denominator
)
1032 numerator
-= denominator
;
1038 if (!ROUND_TOWARDS_ZERO
&& (quotient
& GARDMASK
) == GARDMSB
)
1040 if (quotient
& (1 << NGARDS
))
1042 /* Because we're half way, we would round to even by adding
1043 GARDROUND + 1, except that's also done in the packing
1044 function, and rounding twice will lose precision and cause
1045 the result to be too far off. */
1049 /* We're a further than half way by the small amount
1050 corresponding to the bits set in "numerator". Knowing
1051 that, we round here and not in pack_d, because there we
1052 don't have "numerator" available anymore. */
1053 quotient
+= GARDROUND
+ 1;
1055 /* Avoid further rounding in pack_d. */
1056 quotient
&= ~(fractype
) GARDMASK
;
1060 a
->fraction
.ll
= quotient
;
1066 divide (FLO_type arg_a
, FLO_type arg_b
)
1070 fp_number_type
*res
;
1071 FLO_union_type au
, bu
;
1079 res
= _fpdiv_parts (&a
, &b
);
1081 return pack_d (res
);
1083 #endif /* L_div_sf || L_div_df */
1085 #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df) \
1086 || defined(L_fpcmp_parts_tf)
1087 /* according to the demo, fpcmp returns a comparison with 0... thus
1094 __fpcmp_parts (fp_number_type
* a
, fp_number_type
* b
)
1097 /* either nan -> unordered. Must be checked outside of this routine. */
1098 if (isnan (a
) && isnan (b
))
1100 return 1; /* still unordered! */
1104 if (isnan (a
) || isnan (b
))
1106 return 1; /* how to indicate unordered compare? */
1108 if (isinf (a
) && isinf (b
))
1110 /* +inf > -inf, but +inf != +inf */
1111 /* b \a| +inf(0)| -inf(1)
1112 ______\+--------+--------
1113 +inf(0)| a==b(0)| a<b(-1)
1114 -------+--------+--------
1115 -inf(1)| a>b(1) | a==b(0)
1116 -------+--------+--------
1117 So since unordered must be nonzero, just line up the columns...
1119 return b
->sign
- a
->sign
;
1121 /* but not both... */
1124 return a
->sign
? -1 : 1;
1128 return b
->sign
? 1 : -1;
1130 if (iszero (a
) && iszero (b
))
1136 return b
->sign
? 1 : -1;
1140 return a
->sign
? -1 : 1;
1142 /* now both are "normal". */
1143 if (a
->sign
!= b
->sign
)
1145 /* opposite signs */
1146 return a
->sign
? -1 : 1;
1148 /* same sign; exponents? */
1149 if (a
->normal_exp
> b
->normal_exp
)
1151 return a
->sign
? -1 : 1;
1153 if (a
->normal_exp
< b
->normal_exp
)
1155 return a
->sign
? 1 : -1;
1157 /* same exponents; check size. */
1158 if (a
->fraction
.ll
> b
->fraction
.ll
)
1160 return a
->sign
? -1 : 1;
1162 if (a
->fraction
.ll
< b
->fraction
.ll
)
1164 return a
->sign
? 1 : -1;
1166 /* after all that, they're equal. */
1171 #if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compoare_tf)
1173 compare (FLO_type arg_a
, FLO_type arg_b
)
1177 FLO_union_type au
, bu
;
1185 return __fpcmp_parts (&a
, &b
);
1187 #endif /* L_compare_sf || L_compare_df */
1189 #ifndef US_SOFTWARE_GOFAST
1191 /* These should be optimized for their specific tasks someday. */
1193 #if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
1195 _eq_f2 (FLO_type arg_a
, FLO_type arg_b
)
1199 FLO_union_type au
, bu
;
1207 if (isnan (&a
) || isnan (&b
))
1208 return 1; /* false, truth == 0 */
1210 return __fpcmp_parts (&a
, &b
) ;
1212 #endif /* L_eq_sf || L_eq_df */
1214 #if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
1216 _ne_f2 (FLO_type arg_a
, FLO_type arg_b
)
1220 FLO_union_type au
, bu
;
1228 if (isnan (&a
) || isnan (&b
))
1229 return 1; /* true, truth != 0 */
1231 return __fpcmp_parts (&a
, &b
) ;
1233 #endif /* L_ne_sf || L_ne_df */
1235 #if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
1237 _gt_f2 (FLO_type arg_a
, FLO_type arg_b
)
1241 FLO_union_type au
, bu
;
1249 if (isnan (&a
) || isnan (&b
))
1250 return -1; /* false, truth > 0 */
1252 return __fpcmp_parts (&a
, &b
);
1254 #endif /* L_gt_sf || L_gt_df */
1256 #if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
1258 _ge_f2 (FLO_type arg_a
, FLO_type arg_b
)
1262 FLO_union_type au
, bu
;
1270 if (isnan (&a
) || isnan (&b
))
1271 return -1; /* false, truth >= 0 */
1272 return __fpcmp_parts (&a
, &b
) ;
1274 #endif /* L_ge_sf || L_ge_df */
1276 #if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
1278 _lt_f2 (FLO_type arg_a
, FLO_type arg_b
)
1282 FLO_union_type au
, bu
;
1290 if (isnan (&a
) || isnan (&b
))
1291 return 1; /* false, truth < 0 */
1293 return __fpcmp_parts (&a
, &b
);
1295 #endif /* L_lt_sf || L_lt_df */
1297 #if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
1299 _le_f2 (FLO_type arg_a
, FLO_type arg_b
)
1303 FLO_union_type au
, bu
;
1311 if (isnan (&a
) || isnan (&b
))
1312 return 1; /* false, truth <= 0 */
1314 return __fpcmp_parts (&a
, &b
) ;
1316 #endif /* L_le_sf || L_le_df */
1318 #endif /* ! US_SOFTWARE_GOFAST */
1320 #if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
1322 _unord_f2 (FLO_type arg_a
, FLO_type arg_b
)
1326 FLO_union_type au
, bu
;
1334 return (isnan (&a
) || isnan (&b
));
1336 #endif /* L_unord_sf || L_unord_df */
1338 #if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
1340 si_to_float (SItype arg_a
)
1344 in
.class = CLASS_NUMBER
;
1345 in
.sign
= arg_a
< 0;
1348 in
.class = CLASS_ZERO
;
1354 in
.normal_exp
= FRACBITS
+ NGARDS
;
1357 /* Special case for minint, since there is no +ve integer
1358 representation for it */
1359 if (arg_a
== (- MAX_SI_INT
- 1))
1361 return (FLO_type
)(- MAX_SI_INT
- 1);
1368 in
.fraction
.ll
= uarg
;
1369 shift
= clzusi (uarg
) - (BITS_PER_SI
- 1 - FRACBITS
- NGARDS
);
1372 in
.fraction
.ll
<<= shift
;
1373 in
.normal_exp
-= shift
;
1376 return pack_d (&in
);
1378 #endif /* L_si_to_sf || L_si_to_df */
1380 #if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
1382 usi_to_float (USItype arg_a
)
1389 in
.class = CLASS_ZERO
;
1394 in
.class = CLASS_NUMBER
;
1395 in
.normal_exp
= FRACBITS
+ NGARDS
;
1396 in
.fraction
.ll
= arg_a
;
1398 shift
= clzusi (arg_a
) - (BITS_PER_SI
- 1 - FRACBITS
- NGARDS
);
1401 fractype guard
= in
.fraction
.ll
& (((fractype
)1 << -shift
) - 1);
1402 in
.fraction
.ll
>>= -shift
;
1403 in
.fraction
.ll
|= (guard
!= 0);
1404 in
.normal_exp
-= shift
;
1408 in
.fraction
.ll
<<= shift
;
1409 in
.normal_exp
-= shift
;
1412 return pack_d (&in
);
1416 #if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
1418 float_to_si (FLO_type arg_a
)
1431 /* get reasonable MAX_SI_INT... */
1433 return a
.sign
? (-MAX_SI_INT
)-1 : MAX_SI_INT
;
1434 /* it is a number, but a small one */
1435 if (a
.normal_exp
< 0)
1437 if (a
.normal_exp
> BITS_PER_SI
- 2)
1438 return a
.sign
? (-MAX_SI_INT
)-1 : MAX_SI_INT
;
1439 tmp
= a
.fraction
.ll
>> ((FRACBITS
+ NGARDS
) - a
.normal_exp
);
1440 return a
.sign
? (-tmp
) : (tmp
);
1442 #endif /* L_sf_to_si || L_df_to_si */
1444 #if defined(L_sf_to_usi) || defined(L_df_to_usi) || defined(L_tf_to_usi)
1445 #if defined US_SOFTWARE_GOFAST || defined(L_tf_to_usi)
1446 /* While libgcc2.c defines its own __fixunssfsi and __fixunsdfsi routines,
1447 we also define them for GOFAST because the ones in libgcc2.c have the
1448 wrong names and I'd rather define these here and keep GOFAST CYG-LOC's
1449 out of libgcc2.c. We can't define these here if not GOFAST because then
1450 there'd be duplicate copies. */
1453 float_to_usi (FLO_type arg_a
)
1465 /* it is a negative number */
1468 /* get reasonable MAX_USI_INT... */
1471 /* it is a number, but a small one */
1472 if (a
.normal_exp
< 0)
1474 if (a
.normal_exp
> BITS_PER_SI
- 1)
1476 else if (a
.normal_exp
> (FRACBITS
+ NGARDS
))
1477 return a
.fraction
.ll
<< (a
.normal_exp
- (FRACBITS
+ NGARDS
));
1479 return a
.fraction
.ll
>> ((FRACBITS
+ NGARDS
) - a
.normal_exp
);
1481 #endif /* US_SOFTWARE_GOFAST */
1482 #endif /* L_sf_to_usi || L_df_to_usi */
1484 #if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
1486 negate (FLO_type arg_a
)
1497 #endif /* L_negate_sf || L_negate_df */
1501 #if defined(L_make_sf)
1503 __make_fp(fp_class_type
class,
1512 in
.normal_exp
= exp
;
1513 in
.fraction
.ll
= frac
;
1514 return pack_d (&in
);
1516 #endif /* L_make_sf */
1520 /* This enables one to build an fp library that supports float but not double.
1521 Otherwise, we would get an undefined reference to __make_dp.
1522 This is needed for some 8-bit ports that can't handle well values that
1523 are 8-bytes in size, so we just don't support double for them at all. */
1525 #if defined(L_sf_to_df)
1527 sf_to_df (SFtype arg_a
)
1533 unpack_d (&au
, &in
);
1535 return __make_dp (in
.class, in
.sign
, in
.normal_exp
,
1536 ((UDItype
) in
.fraction
.ll
) << F_D_BITOFF
);
1538 #endif /* L_sf_to_df */
1540 #if defined(L_sf_to_tf) && defined(TMODES)
1542 sf_to_tf (SFtype arg_a
)
1548 unpack_d (&au
, &in
);
1550 return __make_tp (in
.class, in
.sign
, in
.normal_exp
,
1551 ((UTItype
) in
.fraction
.ll
) << F_T_BITOFF
);
1553 #endif /* L_sf_to_df */
1555 #endif /* ! FLOAT_ONLY */
1560 extern SFtype
__make_fp (fp_class_type
, unsigned int, int, USItype
);
1562 #if defined(L_make_df)
1564 __make_dp (fp_class_type
class, unsigned int sign
, int exp
, UDItype frac
)
1570 in
.normal_exp
= exp
;
1571 in
.fraction
.ll
= frac
;
1572 return pack_d (&in
);
1574 #endif /* L_make_df */
1576 #if defined(L_df_to_sf)
1578 df_to_sf (DFtype arg_a
)
1585 unpack_d (&au
, &in
);
1587 sffrac
= in
.fraction
.ll
>> F_D_BITOFF
;
1589 /* We set the lowest guard bit in SFFRAC if we discarded any non
1591 if ((in
.fraction
.ll
& (((USItype
) 1 << F_D_BITOFF
) - 1)) != 0)
1594 return __make_fp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1596 #endif /* L_df_to_sf */
1598 #if defined(L_df_to_tf) && defined(TMODES) \
1599 && !defined(FLOAT) && !defined(TFLOAT)
1601 df_to_tf (DFtype arg_a
)
1607 unpack_d (&au
, &in
);
1609 return __make_tp (in
.class, in
.sign
, in
.normal_exp
,
1610 ((UTItype
) in
.fraction
.ll
) << D_T_BITOFF
);
1612 #endif /* L_sf_to_df */
1615 #if defined(L_make_tf)
1617 __make_tp(fp_class_type
class,
1626 in
.normal_exp
= exp
;
1627 in
.fraction
.ll
= frac
;
1628 return pack_d (&in
);
1630 #endif /* L_make_tf */
1632 #if defined(L_tf_to_df)
1634 tf_to_df (TFtype arg_a
)
1641 unpack_d (&au
, &in
);
1643 sffrac
= in
.fraction
.ll
>> D_T_BITOFF
;
1645 /* We set the lowest guard bit in SFFRAC if we discarded any non
1647 if ((in
.fraction
.ll
& (((UTItype
) 1 << D_T_BITOFF
) - 1)) != 0)
1650 return __make_dp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1652 #endif /* L_tf_to_df */
1654 #if defined(L_tf_to_sf)
1656 tf_to_sf (TFtype arg_a
)
1663 unpack_d (&au
, &in
);
1665 sffrac
= in
.fraction
.ll
>> F_T_BITOFF
;
1667 /* We set the lowest guard bit in SFFRAC if we discarded any non
1669 if ((in
.fraction
.ll
& (((UTItype
) 1 << F_T_BITOFF
) - 1)) != 0)
1672 return __make_fp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1674 #endif /* L_tf_to_sf */
1677 #endif /* ! FLOAT */
1678 #endif /* !EXTENDED_FLOAT_STUBS */