1 /* This is a software floating point library which can be used
2 for targets without hardware floating point.
3 Copyright (C) 1994-2018 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 /* This implements IEEE 754 format arithmetic, but does not provide a
27 mechanism for setting the rounding mode, or for generating or handling
30 The original code by Steve Chamberlain, hacked by Mark Eichin and Jim
31 Wilson, all of Cygnus Support. */
33 /* The intended way to use this file is to make two copies, add `#define FLOAT'
34 to one copy, then compile both copies and add them to libgcc.a. */
37 #include "coretypes.h"
39 #include "libgcc_tm.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 _DEBUG_BITFLOAT: This makes debugging the code a little easier, by adding
52 two integers to the FLO_union_type.
53 NO_DENORMALS: Disable handling of denormals.
54 NO_NANS: Disable nan and infinity handling
55 SMALL_MACHINE: Useful when operations on QIs and HIs are faster
58 /* We don't currently support extended floats (long doubles) on machines
59 without hardware to deal with them.
61 These stubs are just to keep the linker from complaining about unresolved
62 references which can be pulled in from libio & libstdc++, even if the
63 user isn't using long doubles. However, they may generate an unresolved
64 external to abort if abort is not used by the function, and the stubs
65 are referenced from within libc, since libgcc goes before and after the
68 #ifdef DECLARE_LIBRARY_RENAMES
69 DECLARE_LIBRARY_RENAMES
72 #ifdef EXTENDED_FLOAT_STUBS
73 extern void abort (void);
74 void __extendsfxf2 (void) { abort(); }
75 void __extenddfxf2 (void) { abort(); }
76 void __truncxfdf2 (void) { abort(); }
77 void __truncxfsf2 (void) { abort(); }
78 void __fixxfsi (void) { abort(); }
79 void __floatsixf (void) { abort(); }
80 void __addxf3 (void) { abort(); }
81 void __subxf3 (void) { abort(); }
82 void __mulxf3 (void) { abort(); }
83 void __divxf3 (void) { abort(); }
84 void __negxf2 (void) { abort(); }
85 void __eqxf2 (void) { abort(); }
86 void __nexf2 (void) { abort(); }
87 void __gtxf2 (void) { abort(); }
88 void __gexf2 (void) { abort(); }
89 void __lexf2 (void) { abort(); }
90 void __ltxf2 (void) { abort(); }
92 void __extendsftf2 (void) { abort(); }
93 void __extenddftf2 (void) { abort(); }
94 void __trunctfdf2 (void) { abort(); }
95 void __trunctfsf2 (void) { abort(); }
96 void __fixtfsi (void) { abort(); }
97 void __floatsitf (void) { abort(); }
98 void __addtf3 (void) { abort(); }
99 void __subtf3 (void) { abort(); }
100 void __multf3 (void) { abort(); }
101 void __divtf3 (void) { abort(); }
102 void __negtf2 (void) { abort(); }
103 void __eqtf2 (void) { abort(); }
104 void __netf2 (void) { abort(); }
105 void __gttf2 (void) { abort(); }
106 void __getf2 (void) { abort(); }
107 void __letf2 (void) { abort(); }
108 void __lttf2 (void) { abort(); }
109 #else /* !EXTENDED_FLOAT_STUBS, rest of file */
111 /* IEEE "special" number predicates */
120 #if defined L_thenan_sf
121 const fp_number_type __thenan_sf
= { CLASS_SNAN
, 0, 0, {(fractype
) 0} };
122 #elif defined L_thenan_df
123 const fp_number_type __thenan_df
= { CLASS_SNAN
, 0, 0, {(fractype
) 0} };
124 #elif defined L_thenan_tf
125 const fp_number_type __thenan_tf
= { CLASS_SNAN
, 0, 0, {(fractype
) 0} };
127 extern const fp_number_type __thenan_tf
;
129 extern const fp_number_type __thenan_sf
;
131 extern const fp_number_type __thenan_df
;
135 static const fp_number_type
*
139 return & __thenan_tf
;
141 return & __thenan_sf
;
143 return & __thenan_df
;
149 isnan (const fp_number_type
*x
)
151 return __builtin_expect (x
->class == CLASS_SNAN
|| x
->class == CLASS_QNAN
,
157 isinf (const fp_number_type
* x
)
159 return __builtin_expect (x
->class == CLASS_INFINITY
, 0);
166 iszero (const fp_number_type
* x
)
168 return x
->class == CLASS_ZERO
;
173 flip_sign ( fp_number_type
* x
)
178 /* Count leading zeroes in N. */
183 extern int __clzsi2 (USItype
);
184 if (sizeof (USItype
) == sizeof (unsigned int))
185 return __builtin_clz (n
);
186 else if (sizeof (USItype
) == sizeof (unsigned long))
187 return __builtin_clzl (n
);
188 else if (sizeof (USItype
) == sizeof (unsigned long long))
189 return __builtin_clzll (n
);
194 extern FLO_type
pack_d (const fp_number_type
* );
196 #if defined(L_pack_df) || defined(L_pack_sf) || defined(L_pack_tf)
198 pack_d (const fp_number_type
*src
)
201 fractype fraction
= src
->fraction
.ll
; /* wasn't unsigned before? */
202 int sign
= src
->sign
;
208 /* Restore the NaN's payload. */
210 fraction
&= QUIET_NAN
- 1;
211 if (src
->class == CLASS_QNAN
|| 1)
213 #ifdef QUIET_NAN_NEGATED
214 /* The quiet/signaling bit remains unset. */
215 /* Make sure the fraction has a non-zero value. */
217 fraction
|= QUIET_NAN
- 1;
219 /* Set the quiet/signaling bit. */
220 fraction
|= QUIET_NAN
;
224 else if (isinf (src
))
229 else if (iszero (src
))
234 else if (fraction
== 0)
240 if (__builtin_expect (src
->normal_exp
< NORMAL_EXPMIN
, 0))
243 /* Go straight to a zero representation if denormals are not
244 supported. The denormal handling would be harmless but
245 isn't unnecessary. */
248 #else /* NO_DENORMALS */
249 /* This number's exponent is too low to fit into the bits
250 available in the number, so we'll store 0 in the exponent and
251 shift the fraction to the right to make up for it. */
253 int shift
= NORMAL_EXPMIN
- src
->normal_exp
;
257 if (shift
> FRAC_NBITS
- NGARDS
)
259 /* No point shifting, since it's more that 64 out. */
264 int lowbit
= (fraction
& (((fractype
)1 << shift
) - 1)) ? 1 : 0;
265 fraction
= (fraction
>> shift
) | lowbit
;
267 if ((fraction
& GARDMASK
) == GARDMSB
)
269 if ((fraction
& (1 << NGARDS
)))
270 fraction
+= GARDROUND
+ 1;
274 /* Add to the guards to round up. */
275 fraction
+= GARDROUND
;
277 /* Perhaps the rounding means we now need to change the
278 exponent, because the fraction is no longer denormal. */
279 if (fraction
>= IMPLICIT_1
)
284 #endif /* NO_DENORMALS */
286 else if (__builtin_expect (src
->normal_exp
> EXPBIAS
, 0))
293 exp
= src
->normal_exp
+ EXPBIAS
;
294 /* IF the gard bits are the all zero, but the first, then we're
295 half way between two numbers, choose the one which makes the
296 lsb of the answer 0. */
297 if ((fraction
& GARDMASK
) == GARDMSB
)
299 if (fraction
& (1 << NGARDS
))
300 fraction
+= GARDROUND
+ 1;
304 /* Add a one to the guards to round up */
305 fraction
+= GARDROUND
;
307 if (fraction
>= IMPLICIT_2
)
316 /* We previously used bitfields to store the number, but this doesn't
317 handle little/big endian systems conveniently, so use shifts and
319 #ifdef FLOAT_BIT_ORDER_MISMATCH
320 dst
.bits
.fraction
= fraction
;
322 dst
.bits
.sign
= sign
;
324 # if defined TFLOAT && defined HALFFRACBITS
326 halffractype high
, low
, unity
;
329 unity
= (halffractype
) 1 << HALFFRACBITS
;
331 /* Set HIGH to the high double's significand, masking out the implicit 1.
332 Set LOW to the low double's full significand. */
333 high
= (fraction
>> (FRACBITS
- HALFFRACBITS
)) & (unity
- 1);
334 low
= fraction
& (unity
* 2 - 1);
336 /* Get the initial sign and exponent of the low double. */
337 lowexp
= exp
- HALFFRACBITS
- 1;
340 /* HIGH should be rounded like a normal double, making |LOW| <=
341 0.5 ULP of HIGH. Assume round-to-nearest. */
343 if (low
> unity
|| (low
== unity
&& (high
& 1) == 1))
345 /* Round HIGH up and adjust LOW to match. */
349 /* May make it infinite, but that's OK. */
353 low
= unity
* 2 - low
;
357 high
|= (halffractype
) exp
<< HALFFRACBITS
;
358 high
|= (halffractype
) sign
<< (HALFFRACBITS
+ EXPBITS
);
360 if (exp
== EXPMAX
|| exp
== 0 || low
== 0)
364 while (lowexp
> 0 && low
< unity
)
372 halffractype roundmsb
, round
;
376 roundmsb
= (1 << (shift
- 1));
377 round
= low
& ((roundmsb
<< 1) - 1);
382 if (round
> roundmsb
|| (round
== roundmsb
&& (low
& 1) == 1))
386 /* LOW rounds up to the smallest normal number. */
392 low
|= (halffractype
) lowexp
<< HALFFRACBITS
;
393 low
|= (halffractype
) lowsign
<< (HALFFRACBITS
+ EXPBITS
);
395 dst
.value_raw
= ((fractype
) high
<< HALFSHIFT
) | low
;
398 dst
.value_raw
= fraction
& ((((fractype
)1) << FRACBITS
) - (fractype
)1);
399 dst
.value_raw
|= ((fractype
) (exp
& ((1 << EXPBITS
) - 1))) << FRACBITS
;
400 dst
.value_raw
|= ((fractype
) (sign
& 1)) << (FRACBITS
| EXPBITS
);
404 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
407 qrtrfractype tmp1
= dst
.words
[0];
408 qrtrfractype tmp2
= dst
.words
[1];
409 dst
.words
[0] = dst
.words
[3];
410 dst
.words
[1] = dst
.words
[2];
416 halffractype tmp
= dst
.words
[0];
417 dst
.words
[0] = dst
.words
[1];
427 #if defined(L_unpack_df) || defined(L_unpack_sf) || defined(L_unpack_tf)
429 unpack_d (FLO_union_type
* src
, fp_number_type
* dst
)
431 /* We previously used bitfields to store the number, but this doesn't
432 handle little/big endian systems conveniently, so use shifts and
438 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
439 FLO_union_type swapped
;
442 swapped
.words
[0] = src
->words
[3];
443 swapped
.words
[1] = src
->words
[2];
444 swapped
.words
[2] = src
->words
[1];
445 swapped
.words
[3] = src
->words
[0];
447 swapped
.words
[0] = src
->words
[1];
448 swapped
.words
[1] = src
->words
[0];
453 #ifdef FLOAT_BIT_ORDER_MISMATCH
454 fraction
= src
->bits
.fraction
;
456 sign
= src
->bits
.sign
;
458 # if defined TFLOAT && defined HALFFRACBITS
460 halffractype high
, low
;
462 high
= src
->value_raw
>> HALFSHIFT
;
463 low
= src
->value_raw
& (((fractype
)1 << HALFSHIFT
) - 1);
465 fraction
= high
& ((((fractype
)1) << HALFFRACBITS
) - 1);
466 fraction
<<= FRACBITS
- HALFFRACBITS
;
467 exp
= ((int)(high
>> HALFFRACBITS
)) & ((1 << EXPBITS
) - 1);
468 sign
= ((int)(high
>> (((HALFFRACBITS
+ EXPBITS
))))) & 1;
470 if (exp
!= EXPMAX
&& exp
!= 0 && low
!= 0)
472 int lowexp
= ((int)(low
>> HALFFRACBITS
)) & ((1 << EXPBITS
) - 1);
473 int lowsign
= ((int)(low
>> (((HALFFRACBITS
+ EXPBITS
))))) & 1;
477 xlow
= low
& ((((fractype
)1) << HALFFRACBITS
) - 1);
479 xlow
|= (((halffractype
)1) << HALFFRACBITS
);
482 shift
= (FRACBITS
- HALFFRACBITS
) - (exp
- lowexp
);
489 else if (fraction
>= xlow
)
493 /* The high part is a power of two but the full number is lower.
494 This code will leave the implicit 1 in FRACTION, but we'd
495 have added that below anyway. */
496 fraction
= (((fractype
) 1 << FRACBITS
) - xlow
) << 1;
502 fraction
= src
->value_raw
& ((((fractype
)1) << FRACBITS
) - 1);
503 exp
= ((int)(src
->value_raw
>> FRACBITS
)) & ((1 << EXPBITS
) - 1);
504 sign
= ((int)(src
->value_raw
>> (FRACBITS
+ EXPBITS
))) & 1;
511 /* Hmm. Looks like 0 */
518 /* tastes like zero */
519 dst
->class = CLASS_ZERO
;
523 /* Zero exponent with nonzero fraction - it's denormalized,
524 so there isn't a leading implicit one - we'll shift it so
526 dst
->normal_exp
= exp
- EXPBIAS
+ 1;
529 dst
->class = CLASS_NUMBER
;
531 while (fraction
< IMPLICIT_1
)
537 dst
->fraction
.ll
= fraction
;
540 else if (__builtin_expect (exp
== EXPMAX
, 0))
545 /* Attached to a zero fraction - means infinity */
546 dst
->class = CLASS_INFINITY
;
550 /* Nonzero fraction, means nan */
551 #ifdef QUIET_NAN_NEGATED
552 if ((fraction
& QUIET_NAN
) == 0)
554 if (fraction
& QUIET_NAN
)
557 dst
->class = CLASS_QNAN
;
561 dst
->class = CLASS_SNAN
;
563 /* Now that we know which kind of NaN we got, discard the
564 quiet/signaling bit, but do preserve the NaN payload. */
565 fraction
&= ~QUIET_NAN
;
566 dst
->fraction
.ll
= fraction
<< NGARDS
;
571 /* Nothing strange about this number */
572 dst
->normal_exp
= exp
- EXPBIAS
;
573 dst
->class = CLASS_NUMBER
;
574 dst
->fraction
.ll
= (fraction
<< NGARDS
) | IMPLICIT_1
;
577 #endif /* L_unpack_df || L_unpack_sf */
579 #if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
580 static const fp_number_type
*
581 _fpadd_parts (fp_number_type
* a
,
583 fp_number_type
* tmp
)
587 /* Put commonly used fields in local variables. */
603 /* Adding infinities with opposite signs yields a NaN. */
604 if (isinf (b
) && a
->sign
!= b
->sign
)
617 tmp
->sign
= a
->sign
& b
->sign
;
627 /* Got two numbers. shift the smaller and increment the exponent till
633 a_normal_exp
= a
->normal_exp
;
634 b_normal_exp
= b
->normal_exp
;
635 a_fraction
= a
->fraction
.ll
;
636 b_fraction
= b
->fraction
.ll
;
638 diff
= a_normal_exp
- b_normal_exp
;
643 if (diff
< FRAC_NBITS
)
647 b_normal_exp
+= diff
;
648 LSHIFT (b_fraction
, diff
);
652 a_normal_exp
+= diff
;
653 LSHIFT (a_fraction
, diff
);
658 /* Somethings's up.. choose the biggest */
659 if (a_normal_exp
> b_normal_exp
)
661 b_normal_exp
= a_normal_exp
;
666 a_normal_exp
= b_normal_exp
;
672 if (a
->sign
!= b
->sign
)
676 tfraction
= -a_fraction
+ b_fraction
;
680 tfraction
= a_fraction
- b_fraction
;
685 tmp
->normal_exp
= a_normal_exp
;
686 tmp
->fraction
.ll
= tfraction
;
691 tmp
->normal_exp
= a_normal_exp
;
692 tmp
->fraction
.ll
= -tfraction
;
694 /* and renormalize it */
696 while (tmp
->fraction
.ll
< IMPLICIT_1
&& tmp
->fraction
.ll
)
698 tmp
->fraction
.ll
<<= 1;
705 tmp
->normal_exp
= a_normal_exp
;
706 tmp
->fraction
.ll
= a_fraction
+ b_fraction
;
708 tmp
->class = CLASS_NUMBER
;
709 /* Now the fraction is added, we have to shift down to renormalize the
712 if (tmp
->fraction
.ll
>= IMPLICIT_2
)
714 LSHIFT (tmp
->fraction
.ll
, 1);
721 add (FLO_type arg_a
, FLO_type arg_b
)
726 const fp_number_type
*res
;
727 FLO_union_type au
, bu
;
735 res
= _fpadd_parts (&a
, &b
, &tmp
);
741 sub (FLO_type arg_a
, FLO_type arg_b
)
746 const fp_number_type
*res
;
747 FLO_union_type au
, bu
;
757 res
= _fpadd_parts (&a
, &b
, &tmp
);
761 #endif /* L_addsub_sf || L_addsub_df */
763 #if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
764 static inline __attribute__ ((__always_inline__
)) const fp_number_type
*
765 _fpmul_parts ( fp_number_type
* a
,
767 fp_number_type
* tmp
)
774 a
->sign
= a
->sign
!= b
->sign
;
779 b
->sign
= a
->sign
!= b
->sign
;
786 a
->sign
= a
->sign
!= b
->sign
;
795 b
->sign
= a
->sign
!= b
->sign
;
800 a
->sign
= a
->sign
!= b
->sign
;
805 b
->sign
= a
->sign
!= b
->sign
;
809 /* Calculate the mantissa by multiplying both numbers to get a
810 twice-as-wide number. */
812 #if defined(NO_DI_MODE) || defined(TFLOAT)
814 fractype x
= a
->fraction
.ll
;
815 fractype ylow
= b
->fraction
.ll
;
819 /* ??? This does multiplies one bit at a time. Optimize. */
820 for (bit
= 0; bit
< FRAC_NBITS
; bit
++)
826 carry
= (low
+= ylow
) < ylow
;
827 high
+= yhigh
+ carry
;
839 /* Multiplying two USIs to get a UDI, we're safe. */
841 UDItype answer
= (UDItype
)a
->fraction
.ll
* (UDItype
)b
->fraction
.ll
;
843 high
= answer
>> BITS_PER_SI
;
847 /* fractype is DImode, but we need the result to be twice as wide.
848 Assuming a widening multiply from DImode to TImode is not
849 available, build one by hand. */
851 USItype nl
= a
->fraction
.ll
;
852 USItype nh
= a
->fraction
.ll
>> BITS_PER_SI
;
853 USItype ml
= b
->fraction
.ll
;
854 USItype mh
= b
->fraction
.ll
>> BITS_PER_SI
;
855 UDItype pp_ll
= (UDItype
) ml
* nl
;
856 UDItype pp_hl
= (UDItype
) mh
* nl
;
857 UDItype pp_lh
= (UDItype
) ml
* nh
;
858 UDItype pp_hh
= (UDItype
) mh
* nh
;
861 UDItype ps_hh__
= pp_hl
+ pp_lh
;
863 res2
+= (UDItype
)1 << BITS_PER_SI
;
864 pp_hl
= (UDItype
)(USItype
)ps_hh__
<< BITS_PER_SI
;
865 res0
= pp_ll
+ pp_hl
;
868 res2
+= (ps_hh__
>> BITS_PER_SI
) + pp_hh
;
875 tmp
->normal_exp
= a
->normal_exp
+ b
->normal_exp
876 + FRAC_NBITS
- (FRACBITS
+ NGARDS
);
877 tmp
->sign
= a
->sign
!= b
->sign
;
878 while (high
>= IMPLICIT_2
)
888 while (high
< IMPLICIT_1
)
898 if ((high
& GARDMASK
) == GARDMSB
)
900 if (high
& (1 << NGARDS
))
902 /* Because we're half way, we would round to even by adding
903 GARDROUND + 1, except that's also done in the packing
904 function, and rounding twice will lose precision and cause
905 the result to be too far off. Example: 32-bit floats with
906 bit patterns 0xfff * 0x3f800400 ~= 0xfff (less than 0.5ulp
907 off), not 0x1000 (more than 0.5ulp off). */
911 /* We're a further than half way by a small amount corresponding
912 to the bits set in "low". Knowing that, we round here and
913 not in pack_d, because there we don't have "low" available
915 high
+= GARDROUND
+ 1;
917 /* Avoid further rounding in pack_d. */
918 high
&= ~(fractype
) GARDMASK
;
921 tmp
->fraction
.ll
= high
;
922 tmp
->class = CLASS_NUMBER
;
927 multiply (FLO_type arg_a
, FLO_type arg_b
)
932 const fp_number_type
*res
;
933 FLO_union_type au
, bu
;
941 res
= _fpmul_parts (&a
, &b
, &tmp
);
945 #endif /* L_mul_sf || L_mul_df || L_mul_tf */
947 #if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
948 static inline __attribute__ ((__always_inline__
)) const fp_number_type
*
949 _fpdiv_parts (fp_number_type
* a
,
954 fractype denominator
;
966 a
->sign
= a
->sign
^ b
->sign
;
968 if (isinf (a
) || iszero (a
))
970 if (a
->class == b
->class)
983 a
->class = CLASS_INFINITY
;
987 /* Calculate the mantissa by multiplying both 64bit numbers to get a
991 ( numerator / denominator) * 2^(numerator exponent - denominator exponent)
994 a
->normal_exp
= a
->normal_exp
- b
->normal_exp
;
995 numerator
= a
->fraction
.ll
;
996 denominator
= b
->fraction
.ll
;
998 if (numerator
< denominator
)
1000 /* Fraction will be less than 1.0 */
1006 /* ??? Does divide one bit at a time. Optimize. */
1009 if (numerator
>= denominator
)
1012 numerator
-= denominator
;
1018 if ((quotient
& GARDMASK
) == GARDMSB
)
1020 if (quotient
& (1 << NGARDS
))
1022 /* Because we're half way, we would round to even by adding
1023 GARDROUND + 1, except that's also done in the packing
1024 function, and rounding twice will lose precision and cause
1025 the result to be too far off. */
1029 /* We're a further than half way by the small amount
1030 corresponding to the bits set in "numerator". Knowing
1031 that, we round here and not in pack_d, because there we
1032 don't have "numerator" available anymore. */
1033 quotient
+= GARDROUND
+ 1;
1035 /* Avoid further rounding in pack_d. */
1036 quotient
&= ~(fractype
) GARDMASK
;
1040 a
->fraction
.ll
= quotient
;
1046 divide (FLO_type arg_a
, FLO_type arg_b
)
1050 const fp_number_type
*res
;
1051 FLO_union_type au
, bu
;
1059 res
= _fpdiv_parts (&a
, &b
);
1061 return pack_d (res
);
1063 #endif /* L_div_sf || L_div_df */
1065 #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df) \
1066 || defined(L_fpcmp_parts_tf)
1067 /* according to the demo, fpcmp returns a comparison with 0... thus
1074 __fpcmp_parts (fp_number_type
* a
, fp_number_type
* b
)
1077 /* either nan -> unordered. Must be checked outside of this routine. */
1078 if (isnan (a
) && isnan (b
))
1080 return 1; /* still unordered! */
1084 if (isnan (a
) || isnan (b
))
1086 return 1; /* how to indicate unordered compare? */
1088 if (isinf (a
) && isinf (b
))
1090 /* +inf > -inf, but +inf != +inf */
1091 /* b \a| +inf(0)| -inf(1)
1092 ______\+--------+--------
1093 +inf(0)| a==b(0)| a<b(-1)
1094 -------+--------+--------
1095 -inf(1)| a>b(1) | a==b(0)
1096 -------+--------+--------
1097 So since unordered must be nonzero, just line up the columns...
1099 return b
->sign
- a
->sign
;
1101 /* but not both... */
1104 return a
->sign
? -1 : 1;
1108 return b
->sign
? 1 : -1;
1110 if (iszero (a
) && iszero (b
))
1116 return b
->sign
? 1 : -1;
1120 return a
->sign
? -1 : 1;
1122 /* now both are "normal". */
1123 if (a
->sign
!= b
->sign
)
1125 /* opposite signs */
1126 return a
->sign
? -1 : 1;
1128 /* same sign; exponents? */
1129 if (a
->normal_exp
> b
->normal_exp
)
1131 return a
->sign
? -1 : 1;
1133 if (a
->normal_exp
< b
->normal_exp
)
1135 return a
->sign
? 1 : -1;
1137 /* same exponents; check size. */
1138 if (a
->fraction
.ll
> b
->fraction
.ll
)
1140 return a
->sign
? -1 : 1;
1142 if (a
->fraction
.ll
< b
->fraction
.ll
)
1144 return a
->sign
? 1 : -1;
1146 /* after all that, they're equal. */
1151 #if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compoare_tf)
1153 compare (FLO_type arg_a
, FLO_type arg_b
)
1157 FLO_union_type au
, bu
;
1165 return __fpcmp_parts (&a
, &b
);
1167 #endif /* L_compare_sf || L_compare_df */
1169 /* These should be optimized for their specific tasks someday. */
1171 #if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
1173 _eq_f2 (FLO_type arg_a
, FLO_type arg_b
)
1177 FLO_union_type au
, bu
;
1185 if (isnan (&a
) || isnan (&b
))
1186 return 1; /* false, truth == 0 */
1188 return __fpcmp_parts (&a
, &b
) ;
1190 #endif /* L_eq_sf || L_eq_df */
1192 #if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
1194 _ne_f2 (FLO_type arg_a
, FLO_type arg_b
)
1198 FLO_union_type au
, bu
;
1206 if (isnan (&a
) || isnan (&b
))
1207 return 1; /* true, truth != 0 */
1209 return __fpcmp_parts (&a
, &b
) ;
1211 #endif /* L_ne_sf || L_ne_df */
1213 #if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
1215 _gt_f2 (FLO_type arg_a
, FLO_type arg_b
)
1219 FLO_union_type au
, bu
;
1227 if (isnan (&a
) || isnan (&b
))
1228 return -1; /* false, truth > 0 */
1230 return __fpcmp_parts (&a
, &b
);
1232 #endif /* L_gt_sf || L_gt_df */
1234 #if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
1236 _ge_f2 (FLO_type arg_a
, FLO_type arg_b
)
1240 FLO_union_type au
, bu
;
1248 if (isnan (&a
) || isnan (&b
))
1249 return -1; /* false, truth >= 0 */
1250 return __fpcmp_parts (&a
, &b
) ;
1252 #endif /* L_ge_sf || L_ge_df */
1254 #if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
1256 _lt_f2 (FLO_type arg_a
, FLO_type arg_b
)
1260 FLO_union_type au
, bu
;
1268 if (isnan (&a
) || isnan (&b
))
1269 return 1; /* false, truth < 0 */
1271 return __fpcmp_parts (&a
, &b
);
1273 #endif /* L_lt_sf || L_lt_df */
1275 #if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
1277 _le_f2 (FLO_type arg_a
, FLO_type arg_b
)
1281 FLO_union_type au
, bu
;
1289 if (isnan (&a
) || isnan (&b
))
1290 return 1; /* false, truth <= 0 */
1292 return __fpcmp_parts (&a
, &b
) ;
1294 #endif /* L_le_sf || L_le_df */
1296 #if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
1298 _unord_f2 (FLO_type arg_a
, FLO_type arg_b
)
1302 FLO_union_type au
, bu
;
1310 return (isnan (&a
) || isnan (&b
));
1312 #endif /* L_unord_sf || L_unord_df */
1314 #if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
1316 si_to_float (SItype arg_a
)
1320 in
.class = CLASS_NUMBER
;
1321 in
.sign
= arg_a
< 0;
1324 in
.class = CLASS_ZERO
;
1330 in
.normal_exp
= FRACBITS
+ NGARDS
;
1333 /* Special case for minint, since there is no +ve integer
1334 representation for it */
1335 if (arg_a
== (- MAX_SI_INT
- 1))
1337 return (FLO_type
)(- MAX_SI_INT
- 1);
1344 in
.fraction
.ll
= uarg
;
1345 shift
= clzusi (uarg
) - (BITS_PER_SI
- 1 - FRACBITS
- NGARDS
);
1348 in
.fraction
.ll
<<= shift
;
1349 in
.normal_exp
-= shift
;
1352 return pack_d (&in
);
1354 #endif /* L_si_to_sf || L_si_to_df */
1356 #if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
1358 usi_to_float (USItype arg_a
)
1365 in
.class = CLASS_ZERO
;
1370 in
.class = CLASS_NUMBER
;
1371 in
.normal_exp
= FRACBITS
+ NGARDS
;
1372 in
.fraction
.ll
= arg_a
;
1374 shift
= clzusi (arg_a
) - (BITS_PER_SI
- 1 - FRACBITS
- NGARDS
);
1377 fractype guard
= in
.fraction
.ll
& (((fractype
)1 << -shift
) - 1);
1378 in
.fraction
.ll
>>= -shift
;
1379 in
.fraction
.ll
|= (guard
!= 0);
1380 in
.normal_exp
-= shift
;
1384 in
.fraction
.ll
<<= shift
;
1385 in
.normal_exp
-= shift
;
1388 return pack_d (&in
);
1392 #if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
1394 float_to_si (FLO_type arg_a
)
1407 /* get reasonable MAX_SI_INT... */
1409 return a
.sign
? (-MAX_SI_INT
)-1 : MAX_SI_INT
;
1410 /* it is a number, but a small one */
1411 if (a
.normal_exp
< 0)
1413 if (a
.normal_exp
> BITS_PER_SI
- 2)
1414 return a
.sign
? (-MAX_SI_INT
)-1 : MAX_SI_INT
;
1415 tmp
= a
.fraction
.ll
>> ((FRACBITS
+ NGARDS
) - a
.normal_exp
);
1416 return a
.sign
? (-tmp
) : (tmp
);
1418 #endif /* L_sf_to_si || L_df_to_si */
1420 #if defined(L_tf_to_usi)
1422 float_to_usi (FLO_type arg_a
)
1434 /* it is a negative number */
1437 /* get reasonable MAX_USI_INT... */
1440 /* it is a number, but a small one */
1441 if (a
.normal_exp
< 0)
1443 if (a
.normal_exp
> BITS_PER_SI
- 1)
1445 else if (a
.normal_exp
> (FRACBITS
+ NGARDS
))
1446 return a
.fraction
.ll
<< (a
.normal_exp
- (FRACBITS
+ NGARDS
));
1448 return a
.fraction
.ll
>> ((FRACBITS
+ NGARDS
) - a
.normal_exp
);
1450 #endif /* L_tf_to_usi */
1452 #if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
1454 negate (FLO_type arg_a
)
1465 #endif /* L_negate_sf || L_negate_df */
1469 #if defined(L_make_sf)
1471 __make_fp(fp_class_type
class,
1480 in
.normal_exp
= exp
;
1481 in
.fraction
.ll
= frac
;
1482 return pack_d (&in
);
1484 #endif /* L_make_sf */
1488 /* This enables one to build an fp library that supports float but not double.
1489 Otherwise, we would get an undefined reference to __make_dp.
1490 This is needed for some 8-bit ports that can't handle well values that
1491 are 8-bytes in size, so we just don't support double for them at all. */
1493 #if defined(L_sf_to_df)
1495 sf_to_df (SFtype arg_a
)
1501 unpack_d (&au
, &in
);
1503 return __make_dp (in
.class, in
.sign
, in
.normal_exp
,
1504 ((UDItype
) in
.fraction
.ll
) << F_D_BITOFF
);
1506 #endif /* L_sf_to_df */
1508 #if defined(L_sf_to_tf) && defined(TMODES)
1510 sf_to_tf (SFtype arg_a
)
1516 unpack_d (&au
, &in
);
1518 return __make_tp (in
.class, in
.sign
, in
.normal_exp
,
1519 ((UTItype
) in
.fraction
.ll
) << F_T_BITOFF
);
1521 #endif /* L_sf_to_df */
1523 #endif /* ! FLOAT_ONLY */
1528 extern SFtype
__make_fp (fp_class_type
, unsigned int, int, USItype
);
1530 #if defined(L_make_df)
1532 __make_dp (fp_class_type
class, unsigned int sign
, int exp
, UDItype frac
)
1538 in
.normal_exp
= exp
;
1539 in
.fraction
.ll
= frac
;
1540 return pack_d (&in
);
1542 #endif /* L_make_df */
1544 #if defined(L_df_to_sf)
1546 df_to_sf (DFtype arg_a
)
1553 unpack_d (&au
, &in
);
1555 sffrac
= in
.fraction
.ll
>> F_D_BITOFF
;
1557 /* We set the lowest guard bit in SFFRAC if we discarded any non
1559 if ((in
.fraction
.ll
& (((USItype
) 1 << F_D_BITOFF
) - 1)) != 0)
1562 return __make_fp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1564 #endif /* L_df_to_sf */
1566 #if defined(L_df_to_tf) && defined(TMODES) \
1567 && !defined(FLOAT) && !defined(TFLOAT)
1569 df_to_tf (DFtype arg_a
)
1575 unpack_d (&au
, &in
);
1577 return __make_tp (in
.class, in
.sign
, in
.normal_exp
,
1578 ((UTItype
) in
.fraction
.ll
) << D_T_BITOFF
);
1580 #endif /* L_sf_to_df */
1583 #if defined(L_make_tf)
1585 __make_tp(fp_class_type
class,
1594 in
.normal_exp
= exp
;
1595 in
.fraction
.ll
= frac
;
1596 return pack_d (&in
);
1598 #endif /* L_make_tf */
1600 #if defined(L_tf_to_df)
1602 tf_to_df (TFtype arg_a
)
1609 unpack_d (&au
, &in
);
1611 sffrac
= in
.fraction
.ll
>> D_T_BITOFF
;
1613 /* We set the lowest guard bit in SFFRAC if we discarded any non
1615 if ((in
.fraction
.ll
& (((UTItype
) 1 << D_T_BITOFF
) - 1)) != 0)
1618 return __make_dp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1620 #endif /* L_tf_to_df */
1622 #if defined(L_tf_to_sf)
1624 tf_to_sf (TFtype arg_a
)
1631 unpack_d (&au
, &in
);
1633 sffrac
= in
.fraction
.ll
>> F_T_BITOFF
;
1635 /* We set the lowest guard bit in SFFRAC if we discarded any non
1637 if ((in
.fraction
.ll
& (((UTItype
) 1 << F_T_BITOFF
) - 1)) != 0)
1640 return __make_fp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1642 #endif /* L_tf_to_sf */
1645 #endif /* ! FLOAT */
1646 #endif /* !EXTENDED_FLOAT_STUBS */