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, 2010, 2011 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 "libgcc_tm.h"
43 /* The following macros can be defined to change the behavior of this file:
44 FLOAT: Implement a `float', aka SFmode, fp library. If this is not
45 defined, then this file implements a `double', aka DFmode, fp library.
46 FLOAT_ONLY: Used with FLOAT, to implement a `float' only library, i.e.
47 don't include float->double conversion which requires the double library.
48 This is useful only for machines which can't support doubles, e.g. some
50 CMPtype: Specify the type that floating point compares should return.
51 This defaults to SItype, aka int.
52 _DEBUG_BITFLOAT: This makes debugging the code a little easier, by adding
53 two integers to the FLO_union_type.
54 NO_DENORMALS: Disable handling of denormals.
55 NO_NANS: Disable nan and infinity handling
56 SMALL_MACHINE: Useful when operations on QIs and HIs are faster
59 /* We don't currently support extended floats (long doubles) on machines
60 without hardware to deal with them.
62 These stubs are just to keep the linker from complaining about unresolved
63 references which can be pulled in from libio & libstdc++, even if the
64 user isn't using long doubles. However, they may generate an unresolved
65 external to abort if abort is not used by the function, and the stubs
66 are referenced from within libc, since libgcc goes before and after the
69 #ifdef DECLARE_LIBRARY_RENAMES
70 DECLARE_LIBRARY_RENAMES
73 #ifdef EXTENDED_FLOAT_STUBS
74 extern void abort (void);
75 void __extendsfxf2 (void) { abort(); }
76 void __extenddfxf2 (void) { abort(); }
77 void __truncxfdf2 (void) { abort(); }
78 void __truncxfsf2 (void) { abort(); }
79 void __fixxfsi (void) { abort(); }
80 void __floatsixf (void) { abort(); }
81 void __addxf3 (void) { abort(); }
82 void __subxf3 (void) { abort(); }
83 void __mulxf3 (void) { abort(); }
84 void __divxf3 (void) { abort(); }
85 void __negxf2 (void) { abort(); }
86 void __eqxf2 (void) { abort(); }
87 void __nexf2 (void) { abort(); }
88 void __gtxf2 (void) { abort(); }
89 void __gexf2 (void) { abort(); }
90 void __lexf2 (void) { abort(); }
91 void __ltxf2 (void) { abort(); }
93 void __extendsftf2 (void) { abort(); }
94 void __extenddftf2 (void) { abort(); }
95 void __trunctfdf2 (void) { abort(); }
96 void __trunctfsf2 (void) { abort(); }
97 void __fixtfsi (void) { abort(); }
98 void __floatsitf (void) { abort(); }
99 void __addtf3 (void) { abort(); }
100 void __subtf3 (void) { abort(); }
101 void __multf3 (void) { abort(); }
102 void __divtf3 (void) { abort(); }
103 void __negtf2 (void) { abort(); }
104 void __eqtf2 (void) { abort(); }
105 void __netf2 (void) { abort(); }
106 void __gttf2 (void) { abort(); }
107 void __getf2 (void) { abort(); }
108 void __letf2 (void) { abort(); }
109 void __lttf2 (void) { abort(); }
110 #else /* !EXTENDED_FLOAT_STUBS, rest of file */
112 /* IEEE "special" number predicates */
121 #if defined L_thenan_sf
122 const fp_number_type __thenan_sf
= { CLASS_SNAN
, 0, 0, {(fractype
) 0} };
123 #elif defined L_thenan_df
124 const fp_number_type __thenan_df
= { CLASS_SNAN
, 0, 0, {(fractype
) 0} };
125 #elif defined L_thenan_tf
126 const fp_number_type __thenan_tf
= { CLASS_SNAN
, 0, 0, {(fractype
) 0} };
128 extern const fp_number_type __thenan_tf
;
130 extern const fp_number_type __thenan_sf
;
132 extern const fp_number_type __thenan_df
;
136 static const fp_number_type
*
140 return & __thenan_tf
;
142 return & __thenan_sf
;
144 return & __thenan_df
;
150 isnan (const fp_number_type
*x
)
152 return __builtin_expect (x
->class == CLASS_SNAN
|| x
->class == CLASS_QNAN
,
158 isinf (const fp_number_type
* x
)
160 return __builtin_expect (x
->class == CLASS_INFINITY
, 0);
167 iszero (const fp_number_type
* x
)
169 return x
->class == CLASS_ZERO
;
174 flip_sign ( fp_number_type
* x
)
179 /* Count leading zeroes in N. */
184 extern int __clzsi2 (USItype
);
185 if (sizeof (USItype
) == sizeof (unsigned int))
186 return __builtin_clz (n
);
187 else if (sizeof (USItype
) == sizeof (unsigned long))
188 return __builtin_clzl (n
);
189 else if (sizeof (USItype
) == sizeof (unsigned long long))
190 return __builtin_clzll (n
);
195 extern FLO_type
pack_d (const fp_number_type
* );
197 #if defined(L_pack_df) || defined(L_pack_sf) || defined(L_pack_tf)
199 pack_d (const fp_number_type
*src
)
202 fractype fraction
= src
->fraction
.ll
; /* wasn't unsigned before? */
203 int sign
= src
->sign
;
206 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
) && (isnan (src
) || isinf (src
)))
208 /* We can't represent these values accurately. By using the
209 largest possible magnitude, we guarantee that the conversion
210 of infinity is at least as big as any finite number. */
212 fraction
= ((fractype
) 1 << FRACBITS
) - 1;
214 else if (isnan (src
))
217 if (src
->class == CLASS_QNAN
|| 1)
219 #ifdef QUIET_NAN_NEGATED
220 fraction
|= QUIET_NAN
- 1;
222 fraction
|= QUIET_NAN
;
226 else if (isinf (src
))
231 else if (iszero (src
))
236 else if (fraction
== 0)
242 if (__builtin_expect (src
->normal_exp
< NORMAL_EXPMIN
, 0))
245 /* Go straight to a zero representation if denormals are not
246 supported. The denormal handling would be harmless but
247 isn't unnecessary. */
250 #else /* NO_DENORMALS */
251 /* This number's exponent is too low to fit into the bits
252 available in the number, so we'll store 0 in the exponent and
253 shift the fraction to the right to make up for it. */
255 int shift
= NORMAL_EXPMIN
- src
->normal_exp
;
259 if (shift
> FRAC_NBITS
- NGARDS
)
261 /* No point shifting, since it's more that 64 out. */
266 int lowbit
= (fraction
& (((fractype
)1 << shift
) - 1)) ? 1 : 0;
267 fraction
= (fraction
>> shift
) | lowbit
;
269 if ((fraction
& GARDMASK
) == GARDMSB
)
271 if ((fraction
& (1 << NGARDS
)))
272 fraction
+= GARDROUND
+ 1;
276 /* Add to the guards to round up. */
277 fraction
+= GARDROUND
;
279 /* Perhaps the rounding means we now need to change the
280 exponent, because the fraction is no longer denormal. */
281 if (fraction
>= IMPLICIT_1
)
286 #endif /* NO_DENORMALS */
288 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
)
289 && __builtin_expect (src
->normal_exp
> EXPBIAS
, 0))
296 exp
= src
->normal_exp
+ EXPBIAS
;
297 if (!ROUND_TOWARDS_ZERO
)
299 /* IF the gard bits are the all zero, but the first, then we're
300 half way between two numbers, choose the one which makes the
301 lsb of the answer 0. */
302 if ((fraction
& GARDMASK
) == GARDMSB
)
304 if (fraction
& (1 << NGARDS
))
305 fraction
+= GARDROUND
+ 1;
309 /* Add a one to the guards to round up */
310 fraction
+= GARDROUND
;
312 if (fraction
>= IMPLICIT_2
)
320 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
) && exp
> EXPMAX
)
322 /* Saturate on overflow. */
324 fraction
= ((fractype
) 1 << FRACBITS
) - 1;
329 /* We previously used bitfields to store the number, but this doesn't
330 handle little/big endian systems conveniently, so use shifts and
332 #ifdef FLOAT_BIT_ORDER_MISMATCH
333 dst
.bits
.fraction
= fraction
;
335 dst
.bits
.sign
= sign
;
337 # if defined TFLOAT && defined HALFFRACBITS
339 halffractype high
, low
, unity
;
342 unity
= (halffractype
) 1 << HALFFRACBITS
;
344 /* Set HIGH to the high double's significand, masking out the implicit 1.
345 Set LOW to the low double's full significand. */
346 high
= (fraction
>> (FRACBITS
- HALFFRACBITS
)) & (unity
- 1);
347 low
= fraction
& (unity
* 2 - 1);
349 /* Get the initial sign and exponent of the low double. */
350 lowexp
= exp
- HALFFRACBITS
- 1;
353 /* HIGH should be rounded like a normal double, making |LOW| <=
354 0.5 ULP of HIGH. Assume round-to-nearest. */
356 if (low
> unity
|| (low
== unity
&& (high
& 1) == 1))
358 /* Round HIGH up and adjust LOW to match. */
362 /* May make it infinite, but that's OK. */
366 low
= unity
* 2 - low
;
370 high
|= (halffractype
) exp
<< HALFFRACBITS
;
371 high
|= (halffractype
) sign
<< (HALFFRACBITS
+ EXPBITS
);
373 if (exp
== EXPMAX
|| exp
== 0 || low
== 0)
377 while (lowexp
> 0 && low
< unity
)
385 halffractype roundmsb
, round
;
389 roundmsb
= (1 << (shift
- 1));
390 round
= low
& ((roundmsb
<< 1) - 1);
395 if (round
> roundmsb
|| (round
== roundmsb
&& (low
& 1) == 1))
399 /* LOW rounds up to the smallest normal number. */
405 low
|= (halffractype
) lowexp
<< HALFFRACBITS
;
406 low
|= (halffractype
) lowsign
<< (HALFFRACBITS
+ EXPBITS
);
408 dst
.value_raw
= ((fractype
) high
<< HALFSHIFT
) | low
;
411 dst
.value_raw
= fraction
& ((((fractype
)1) << FRACBITS
) - (fractype
)1);
412 dst
.value_raw
|= ((fractype
) (exp
& ((1 << EXPBITS
) - 1))) << FRACBITS
;
413 dst
.value_raw
|= ((fractype
) (sign
& 1)) << (FRACBITS
| EXPBITS
);
417 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
420 qrtrfractype tmp1
= dst
.words
[0];
421 qrtrfractype tmp2
= dst
.words
[1];
422 dst
.words
[0] = dst
.words
[3];
423 dst
.words
[1] = dst
.words
[2];
429 halffractype tmp
= dst
.words
[0];
430 dst
.words
[0] = dst
.words
[1];
440 #if defined(L_unpack_df) || defined(L_unpack_sf) || defined(L_unpack_tf)
442 unpack_d (FLO_union_type
* src
, fp_number_type
* dst
)
444 /* We previously used bitfields to store the number, but this doesn't
445 handle little/big endian systems conveniently, so use shifts and
451 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
452 FLO_union_type swapped
;
455 swapped
.words
[0] = src
->words
[3];
456 swapped
.words
[1] = src
->words
[2];
457 swapped
.words
[2] = src
->words
[1];
458 swapped
.words
[3] = src
->words
[0];
460 swapped
.words
[0] = src
->words
[1];
461 swapped
.words
[1] = src
->words
[0];
466 #ifdef FLOAT_BIT_ORDER_MISMATCH
467 fraction
= src
->bits
.fraction
;
469 sign
= src
->bits
.sign
;
471 # if defined TFLOAT && defined HALFFRACBITS
473 halffractype high
, low
;
475 high
= src
->value_raw
>> HALFSHIFT
;
476 low
= src
->value_raw
& (((fractype
)1 << HALFSHIFT
) - 1);
478 fraction
= high
& ((((fractype
)1) << HALFFRACBITS
) - 1);
479 fraction
<<= FRACBITS
- HALFFRACBITS
;
480 exp
= ((int)(high
>> HALFFRACBITS
)) & ((1 << EXPBITS
) - 1);
481 sign
= ((int)(high
>> (((HALFFRACBITS
+ EXPBITS
))))) & 1;
483 if (exp
!= EXPMAX
&& exp
!= 0 && low
!= 0)
485 int lowexp
= ((int)(low
>> HALFFRACBITS
)) & ((1 << EXPBITS
) - 1);
486 int lowsign
= ((int)(low
>> (((HALFFRACBITS
+ EXPBITS
))))) & 1;
490 xlow
= low
& ((((fractype
)1) << HALFFRACBITS
) - 1);
492 xlow
|= (((halffractype
)1) << HALFFRACBITS
);
495 shift
= (FRACBITS
- HALFFRACBITS
) - (exp
- lowexp
);
502 else if (fraction
>= xlow
)
506 /* The high part is a power of two but the full number is lower.
507 This code will leave the implicit 1 in FRACTION, but we'd
508 have added that below anyway. */
509 fraction
= (((fractype
) 1 << FRACBITS
) - xlow
) << 1;
515 fraction
= src
->value_raw
& ((((fractype
)1) << FRACBITS
) - 1);
516 exp
= ((int)(src
->value_raw
>> FRACBITS
)) & ((1 << EXPBITS
) - 1);
517 sign
= ((int)(src
->value_raw
>> (FRACBITS
+ EXPBITS
))) & 1;
524 /* Hmm. Looks like 0 */
531 /* tastes like zero */
532 dst
->class = CLASS_ZERO
;
536 /* Zero exponent with nonzero fraction - it's denormalized,
537 so there isn't a leading implicit one - we'll shift it so
539 dst
->normal_exp
= exp
- EXPBIAS
+ 1;
542 dst
->class = CLASS_NUMBER
;
544 while (fraction
< IMPLICIT_1
)
550 dst
->fraction
.ll
= fraction
;
553 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
)
554 && __builtin_expect (exp
== EXPMAX
, 0))
559 /* Attached to a zero fraction - means infinity */
560 dst
->class = CLASS_INFINITY
;
564 /* Nonzero fraction, means nan */
565 #ifdef QUIET_NAN_NEGATED
566 if ((fraction
& QUIET_NAN
) == 0)
568 if (fraction
& QUIET_NAN
)
571 dst
->class = CLASS_QNAN
;
575 dst
->class = CLASS_SNAN
;
577 /* Keep the fraction part as the nan number */
578 dst
->fraction
.ll
= fraction
;
583 /* Nothing strange about this number */
584 dst
->normal_exp
= exp
- EXPBIAS
;
585 dst
->class = CLASS_NUMBER
;
586 dst
->fraction
.ll
= (fraction
<< NGARDS
) | IMPLICIT_1
;
589 #endif /* L_unpack_df || L_unpack_sf */
591 #if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
592 static const fp_number_type
*
593 _fpadd_parts (fp_number_type
* a
,
595 fp_number_type
* tmp
)
599 /* Put commonly used fields in local variables. */
615 /* Adding infinities with opposite signs yields a NaN. */
616 if (isinf (b
) && a
->sign
!= b
->sign
)
629 tmp
->sign
= a
->sign
& b
->sign
;
639 /* Got two numbers. shift the smaller and increment the exponent till
645 a_normal_exp
= a
->normal_exp
;
646 b_normal_exp
= b
->normal_exp
;
647 a_fraction
= a
->fraction
.ll
;
648 b_fraction
= b
->fraction
.ll
;
650 diff
= a_normal_exp
- b_normal_exp
;
655 if (diff
< FRAC_NBITS
)
659 b_normal_exp
+= diff
;
660 LSHIFT (b_fraction
, diff
);
664 a_normal_exp
+= diff
;
665 LSHIFT (a_fraction
, diff
);
670 /* Somethings's up.. choose the biggest */
671 if (a_normal_exp
> b_normal_exp
)
673 b_normal_exp
= a_normal_exp
;
678 a_normal_exp
= b_normal_exp
;
684 if (a
->sign
!= b
->sign
)
688 tfraction
= -a_fraction
+ b_fraction
;
692 tfraction
= a_fraction
- b_fraction
;
697 tmp
->normal_exp
= a_normal_exp
;
698 tmp
->fraction
.ll
= tfraction
;
703 tmp
->normal_exp
= a_normal_exp
;
704 tmp
->fraction
.ll
= -tfraction
;
706 /* and renormalize it */
708 while (tmp
->fraction
.ll
< IMPLICIT_1
&& tmp
->fraction
.ll
)
710 tmp
->fraction
.ll
<<= 1;
717 tmp
->normal_exp
= a_normal_exp
;
718 tmp
->fraction
.ll
= a_fraction
+ b_fraction
;
720 tmp
->class = CLASS_NUMBER
;
721 /* Now the fraction is added, we have to shift down to renormalize the
724 if (tmp
->fraction
.ll
>= IMPLICIT_2
)
726 LSHIFT (tmp
->fraction
.ll
, 1);
733 add (FLO_type arg_a
, FLO_type arg_b
)
738 const fp_number_type
*res
;
739 FLO_union_type au
, bu
;
747 res
= _fpadd_parts (&a
, &b
, &tmp
);
753 sub (FLO_type arg_a
, FLO_type arg_b
)
758 const fp_number_type
*res
;
759 FLO_union_type au
, bu
;
769 res
= _fpadd_parts (&a
, &b
, &tmp
);
773 #endif /* L_addsub_sf || L_addsub_df */
775 #if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
776 static inline __attribute__ ((__always_inline__
)) const fp_number_type
*
777 _fpmul_parts ( fp_number_type
* a
,
779 fp_number_type
* tmp
)
786 a
->sign
= a
->sign
!= b
->sign
;
791 b
->sign
= a
->sign
!= b
->sign
;
798 a
->sign
= a
->sign
!= b
->sign
;
807 b
->sign
= a
->sign
!= b
->sign
;
812 a
->sign
= a
->sign
!= b
->sign
;
817 b
->sign
= a
->sign
!= b
->sign
;
821 /* Calculate the mantissa by multiplying both numbers to get a
822 twice-as-wide number. */
824 #if defined(NO_DI_MODE) || defined(TFLOAT)
826 fractype x
= a
->fraction
.ll
;
827 fractype ylow
= b
->fraction
.ll
;
831 /* ??? This does multiplies one bit at a time. Optimize. */
832 for (bit
= 0; bit
< FRAC_NBITS
; bit
++)
838 carry
= (low
+= ylow
) < ylow
;
839 high
+= yhigh
+ carry
;
851 /* Multiplying two USIs to get a UDI, we're safe. */
853 UDItype answer
= (UDItype
)a
->fraction
.ll
* (UDItype
)b
->fraction
.ll
;
855 high
= answer
>> BITS_PER_SI
;
859 /* fractype is DImode, but we need the result to be twice as wide.
860 Assuming a widening multiply from DImode to TImode is not
861 available, build one by hand. */
863 USItype nl
= a
->fraction
.ll
;
864 USItype nh
= a
->fraction
.ll
>> BITS_PER_SI
;
865 USItype ml
= b
->fraction
.ll
;
866 USItype mh
= b
->fraction
.ll
>> BITS_PER_SI
;
867 UDItype pp_ll
= (UDItype
) ml
* nl
;
868 UDItype pp_hl
= (UDItype
) mh
* nl
;
869 UDItype pp_lh
= (UDItype
) ml
* nh
;
870 UDItype pp_hh
= (UDItype
) mh
* nh
;
873 UDItype ps_hh__
= pp_hl
+ pp_lh
;
875 res2
+= (UDItype
)1 << BITS_PER_SI
;
876 pp_hl
= (UDItype
)(USItype
)ps_hh__
<< BITS_PER_SI
;
877 res0
= pp_ll
+ pp_hl
;
880 res2
+= (ps_hh__
>> BITS_PER_SI
) + pp_hh
;
887 tmp
->normal_exp
= a
->normal_exp
+ b
->normal_exp
888 + FRAC_NBITS
- (FRACBITS
+ NGARDS
);
889 tmp
->sign
= a
->sign
!= b
->sign
;
890 while (high
>= IMPLICIT_2
)
900 while (high
< IMPLICIT_1
)
910 if (!ROUND_TOWARDS_ZERO
&& (high
& GARDMASK
) == GARDMSB
)
912 if (high
& (1 << NGARDS
))
914 /* Because we're half way, we would round to even by adding
915 GARDROUND + 1, except that's also done in the packing
916 function, and rounding twice will lose precision and cause
917 the result to be too far off. Example: 32-bit floats with
918 bit patterns 0xfff * 0x3f800400 ~= 0xfff (less than 0.5ulp
919 off), not 0x1000 (more than 0.5ulp off). */
923 /* We're a further than half way by a small amount corresponding
924 to the bits set in "low". Knowing that, we round here and
925 not in pack_d, because there we don't have "low" available
927 high
+= GARDROUND
+ 1;
929 /* Avoid further rounding in pack_d. */
930 high
&= ~(fractype
) GARDMASK
;
933 tmp
->fraction
.ll
= high
;
934 tmp
->class = CLASS_NUMBER
;
939 multiply (FLO_type arg_a
, FLO_type arg_b
)
944 const fp_number_type
*res
;
945 FLO_union_type au
, bu
;
953 res
= _fpmul_parts (&a
, &b
, &tmp
);
957 #endif /* L_mul_sf || L_mul_df || L_mul_tf */
959 #if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
960 static inline __attribute__ ((__always_inline__
)) const fp_number_type
*
961 _fpdiv_parts (fp_number_type
* a
,
966 fractype denominator
;
978 a
->sign
= a
->sign
^ b
->sign
;
980 if (isinf (a
) || iszero (a
))
982 if (a
->class == b
->class)
995 a
->class = CLASS_INFINITY
;
999 /* Calculate the mantissa by multiplying both 64bit numbers to get a
1003 ( numerator / denominator) * 2^(numerator exponent - denominator exponent)
1006 a
->normal_exp
= a
->normal_exp
- b
->normal_exp
;
1007 numerator
= a
->fraction
.ll
;
1008 denominator
= b
->fraction
.ll
;
1010 if (numerator
< denominator
)
1012 /* Fraction will be less than 1.0 */
1018 /* ??? Does divide one bit at a time. Optimize. */
1021 if (numerator
>= denominator
)
1024 numerator
-= denominator
;
1030 if (!ROUND_TOWARDS_ZERO
&& (quotient
& GARDMASK
) == GARDMSB
)
1032 if (quotient
& (1 << NGARDS
))
1034 /* Because we're half way, we would round to even by adding
1035 GARDROUND + 1, except that's also done in the packing
1036 function, and rounding twice will lose precision and cause
1037 the result to be too far off. */
1041 /* We're a further than half way by the small amount
1042 corresponding to the bits set in "numerator". Knowing
1043 that, we round here and not in pack_d, because there we
1044 don't have "numerator" available anymore. */
1045 quotient
+= GARDROUND
+ 1;
1047 /* Avoid further rounding in pack_d. */
1048 quotient
&= ~(fractype
) GARDMASK
;
1052 a
->fraction
.ll
= quotient
;
1058 divide (FLO_type arg_a
, FLO_type arg_b
)
1062 const fp_number_type
*res
;
1063 FLO_union_type au
, bu
;
1071 res
= _fpdiv_parts (&a
, &b
);
1073 return pack_d (res
);
1075 #endif /* L_div_sf || L_div_df */
1077 #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df) \
1078 || defined(L_fpcmp_parts_tf)
1079 /* according to the demo, fpcmp returns a comparison with 0... thus
1086 __fpcmp_parts (fp_number_type
* a
, fp_number_type
* b
)
1089 /* either nan -> unordered. Must be checked outside of this routine. */
1090 if (isnan (a
) && isnan (b
))
1092 return 1; /* still unordered! */
1096 if (isnan (a
) || isnan (b
))
1098 return 1; /* how to indicate unordered compare? */
1100 if (isinf (a
) && isinf (b
))
1102 /* +inf > -inf, but +inf != +inf */
1103 /* b \a| +inf(0)| -inf(1)
1104 ______\+--------+--------
1105 +inf(0)| a==b(0)| a<b(-1)
1106 -------+--------+--------
1107 -inf(1)| a>b(1) | a==b(0)
1108 -------+--------+--------
1109 So since unordered must be nonzero, just line up the columns...
1111 return b
->sign
- a
->sign
;
1113 /* but not both... */
1116 return a
->sign
? -1 : 1;
1120 return b
->sign
? 1 : -1;
1122 if (iszero (a
) && iszero (b
))
1128 return b
->sign
? 1 : -1;
1132 return a
->sign
? -1 : 1;
1134 /* now both are "normal". */
1135 if (a
->sign
!= b
->sign
)
1137 /* opposite signs */
1138 return a
->sign
? -1 : 1;
1140 /* same sign; exponents? */
1141 if (a
->normal_exp
> b
->normal_exp
)
1143 return a
->sign
? -1 : 1;
1145 if (a
->normal_exp
< b
->normal_exp
)
1147 return a
->sign
? 1 : -1;
1149 /* same exponents; check size. */
1150 if (a
->fraction
.ll
> b
->fraction
.ll
)
1152 return a
->sign
? -1 : 1;
1154 if (a
->fraction
.ll
< b
->fraction
.ll
)
1156 return a
->sign
? 1 : -1;
1158 /* after all that, they're equal. */
1163 #if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compoare_tf)
1165 compare (FLO_type arg_a
, FLO_type arg_b
)
1169 FLO_union_type au
, bu
;
1177 return __fpcmp_parts (&a
, &b
);
1179 #endif /* L_compare_sf || L_compare_df */
1181 /* These should be optimized for their specific tasks someday. */
1183 #if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
1185 _eq_f2 (FLO_type arg_a
, FLO_type arg_b
)
1189 FLO_union_type au
, bu
;
1197 if (isnan (&a
) || isnan (&b
))
1198 return 1; /* false, truth == 0 */
1200 return __fpcmp_parts (&a
, &b
) ;
1202 #endif /* L_eq_sf || L_eq_df */
1204 #if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
1206 _ne_f2 (FLO_type arg_a
, FLO_type arg_b
)
1210 FLO_union_type au
, bu
;
1218 if (isnan (&a
) || isnan (&b
))
1219 return 1; /* true, truth != 0 */
1221 return __fpcmp_parts (&a
, &b
) ;
1223 #endif /* L_ne_sf || L_ne_df */
1225 #if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
1227 _gt_f2 (FLO_type arg_a
, FLO_type arg_b
)
1231 FLO_union_type au
, bu
;
1239 if (isnan (&a
) || isnan (&b
))
1240 return -1; /* false, truth > 0 */
1242 return __fpcmp_parts (&a
, &b
);
1244 #endif /* L_gt_sf || L_gt_df */
1246 #if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
1248 _ge_f2 (FLO_type arg_a
, FLO_type arg_b
)
1252 FLO_union_type au
, bu
;
1260 if (isnan (&a
) || isnan (&b
))
1261 return -1; /* false, truth >= 0 */
1262 return __fpcmp_parts (&a
, &b
) ;
1264 #endif /* L_ge_sf || L_ge_df */
1266 #if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
1268 _lt_f2 (FLO_type arg_a
, FLO_type arg_b
)
1272 FLO_union_type au
, bu
;
1280 if (isnan (&a
) || isnan (&b
))
1281 return 1; /* false, truth < 0 */
1283 return __fpcmp_parts (&a
, &b
);
1285 #endif /* L_lt_sf || L_lt_df */
1287 #if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
1289 _le_f2 (FLO_type arg_a
, FLO_type arg_b
)
1293 FLO_union_type au
, bu
;
1301 if (isnan (&a
) || isnan (&b
))
1302 return 1; /* false, truth <= 0 */
1304 return __fpcmp_parts (&a
, &b
) ;
1306 #endif /* L_le_sf || L_le_df */
1308 #if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
1310 _unord_f2 (FLO_type arg_a
, FLO_type arg_b
)
1314 FLO_union_type au
, bu
;
1322 return (isnan (&a
) || isnan (&b
));
1324 #endif /* L_unord_sf || L_unord_df */
1326 #if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
1328 si_to_float (SItype arg_a
)
1332 in
.class = CLASS_NUMBER
;
1333 in
.sign
= arg_a
< 0;
1336 in
.class = CLASS_ZERO
;
1342 in
.normal_exp
= FRACBITS
+ NGARDS
;
1345 /* Special case for minint, since there is no +ve integer
1346 representation for it */
1347 if (arg_a
== (- MAX_SI_INT
- 1))
1349 return (FLO_type
)(- MAX_SI_INT
- 1);
1356 in
.fraction
.ll
= uarg
;
1357 shift
= clzusi (uarg
) - (BITS_PER_SI
- 1 - FRACBITS
- NGARDS
);
1360 in
.fraction
.ll
<<= shift
;
1361 in
.normal_exp
-= shift
;
1364 return pack_d (&in
);
1366 #endif /* L_si_to_sf || L_si_to_df */
1368 #if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
1370 usi_to_float (USItype arg_a
)
1377 in
.class = CLASS_ZERO
;
1382 in
.class = CLASS_NUMBER
;
1383 in
.normal_exp
= FRACBITS
+ NGARDS
;
1384 in
.fraction
.ll
= arg_a
;
1386 shift
= clzusi (arg_a
) - (BITS_PER_SI
- 1 - FRACBITS
- NGARDS
);
1389 fractype guard
= in
.fraction
.ll
& (((fractype
)1 << -shift
) - 1);
1390 in
.fraction
.ll
>>= -shift
;
1391 in
.fraction
.ll
|= (guard
!= 0);
1392 in
.normal_exp
-= shift
;
1396 in
.fraction
.ll
<<= shift
;
1397 in
.normal_exp
-= shift
;
1400 return pack_d (&in
);
1404 #if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
1406 float_to_si (FLO_type arg_a
)
1419 /* get reasonable MAX_SI_INT... */
1421 return a
.sign
? (-MAX_SI_INT
)-1 : MAX_SI_INT
;
1422 /* it is a number, but a small one */
1423 if (a
.normal_exp
< 0)
1425 if (a
.normal_exp
> BITS_PER_SI
- 2)
1426 return a
.sign
? (-MAX_SI_INT
)-1 : MAX_SI_INT
;
1427 tmp
= a
.fraction
.ll
>> ((FRACBITS
+ NGARDS
) - a
.normal_exp
);
1428 return a
.sign
? (-tmp
) : (tmp
);
1430 #endif /* L_sf_to_si || L_df_to_si */
1432 #if defined(L_tf_to_usi)
1434 float_to_usi (FLO_type arg_a
)
1446 /* it is a negative number */
1449 /* get reasonable MAX_USI_INT... */
1452 /* it is a number, but a small one */
1453 if (a
.normal_exp
< 0)
1455 if (a
.normal_exp
> BITS_PER_SI
- 1)
1457 else if (a
.normal_exp
> (FRACBITS
+ NGARDS
))
1458 return a
.fraction
.ll
<< (a
.normal_exp
- (FRACBITS
+ NGARDS
));
1460 return a
.fraction
.ll
>> ((FRACBITS
+ NGARDS
) - a
.normal_exp
);
1462 #endif /* L_tf_to_usi */
1464 #if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
1466 negate (FLO_type arg_a
)
1477 #endif /* L_negate_sf || L_negate_df */
1481 #if defined(L_make_sf)
1483 __make_fp(fp_class_type
class,
1492 in
.normal_exp
= exp
;
1493 in
.fraction
.ll
= frac
;
1494 return pack_d (&in
);
1496 #endif /* L_make_sf */
1500 /* This enables one to build an fp library that supports float but not double.
1501 Otherwise, we would get an undefined reference to __make_dp.
1502 This is needed for some 8-bit ports that can't handle well values that
1503 are 8-bytes in size, so we just don't support double for them at all. */
1505 #if defined(L_sf_to_df)
1507 sf_to_df (SFtype arg_a
)
1513 unpack_d (&au
, &in
);
1515 return __make_dp (in
.class, in
.sign
, in
.normal_exp
,
1516 ((UDItype
) in
.fraction
.ll
) << F_D_BITOFF
);
1518 #endif /* L_sf_to_df */
1520 #if defined(L_sf_to_tf) && defined(TMODES)
1522 sf_to_tf (SFtype arg_a
)
1528 unpack_d (&au
, &in
);
1530 return __make_tp (in
.class, in
.sign
, in
.normal_exp
,
1531 ((UTItype
) in
.fraction
.ll
) << F_T_BITOFF
);
1533 #endif /* L_sf_to_df */
1535 #endif /* ! FLOAT_ONLY */
1540 extern SFtype
__make_fp (fp_class_type
, unsigned int, int, USItype
);
1542 #if defined(L_make_df)
1544 __make_dp (fp_class_type
class, unsigned int sign
, int exp
, UDItype frac
)
1550 in
.normal_exp
= exp
;
1551 in
.fraction
.ll
= frac
;
1552 return pack_d (&in
);
1554 #endif /* L_make_df */
1556 #if defined(L_df_to_sf)
1558 df_to_sf (DFtype arg_a
)
1565 unpack_d (&au
, &in
);
1567 sffrac
= in
.fraction
.ll
>> F_D_BITOFF
;
1569 /* We set the lowest guard bit in SFFRAC if we discarded any non
1571 if ((in
.fraction
.ll
& (((USItype
) 1 << F_D_BITOFF
) - 1)) != 0)
1574 return __make_fp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1576 #endif /* L_df_to_sf */
1578 #if defined(L_df_to_tf) && defined(TMODES) \
1579 && !defined(FLOAT) && !defined(TFLOAT)
1581 df_to_tf (DFtype arg_a
)
1587 unpack_d (&au
, &in
);
1589 return __make_tp (in
.class, in
.sign
, in
.normal_exp
,
1590 ((UTItype
) in
.fraction
.ll
) << D_T_BITOFF
);
1592 #endif /* L_sf_to_df */
1595 #if defined(L_make_tf)
1597 __make_tp(fp_class_type
class,
1606 in
.normal_exp
= exp
;
1607 in
.fraction
.ll
= frac
;
1608 return pack_d (&in
);
1610 #endif /* L_make_tf */
1612 #if defined(L_tf_to_df)
1614 tf_to_df (TFtype arg_a
)
1621 unpack_d (&au
, &in
);
1623 sffrac
= in
.fraction
.ll
>> D_T_BITOFF
;
1625 /* We set the lowest guard bit in SFFRAC if we discarded any non
1627 if ((in
.fraction
.ll
& (((UTItype
) 1 << D_T_BITOFF
) - 1)) != 0)
1630 return __make_dp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1632 #endif /* L_tf_to_df */
1634 #if defined(L_tf_to_sf)
1636 tf_to_sf (TFtype arg_a
)
1643 unpack_d (&au
, &in
);
1645 sffrac
= in
.fraction
.ll
>> F_T_BITOFF
;
1647 /* We set the lowest guard bit in SFFRAC if we discarded any non
1649 if ((in
.fraction
.ll
& (((UTItype
) 1 << F_T_BITOFF
) - 1)) != 0)
1652 return __make_fp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1654 #endif /* L_tf_to_sf */
1657 #endif /* ! FLOAT */
1658 #endif /* !EXTENDED_FLOAT_STUBS */