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 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 _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
;
205 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
) && (isnan (src
) || isinf (src
)))
207 /* We can't represent these values accurately. By using the
208 largest possible magnitude, we guarantee that the conversion
209 of infinity is at least as big as any finite number. */
211 fraction
= ((fractype
) 1 << FRACBITS
) - 1;
213 else if (isnan (src
))
216 if (src
->class == CLASS_QNAN
|| 1)
218 #ifdef QUIET_NAN_NEGATED
219 fraction
|= QUIET_NAN
- 1;
221 fraction
|= QUIET_NAN
;
225 else if (isinf (src
))
230 else if (iszero (src
))
235 else if (fraction
== 0)
241 if (__builtin_expect (src
->normal_exp
< NORMAL_EXPMIN
, 0))
244 /* Go straight to a zero representation if denormals are not
245 supported. The denormal handling would be harmless but
246 isn't unnecessary. */
249 #else /* NO_DENORMALS */
250 /* This number's exponent is too low to fit into the bits
251 available in the number, so we'll store 0 in the exponent and
252 shift the fraction to the right to make up for it. */
254 int shift
= NORMAL_EXPMIN
- src
->normal_exp
;
258 if (shift
> FRAC_NBITS
- NGARDS
)
260 /* No point shifting, since it's more that 64 out. */
265 int lowbit
= (fraction
& (((fractype
)1 << shift
) - 1)) ? 1 : 0;
266 fraction
= (fraction
>> shift
) | lowbit
;
268 if ((fraction
& GARDMASK
) == GARDMSB
)
270 if ((fraction
& (1 << NGARDS
)))
271 fraction
+= GARDROUND
+ 1;
275 /* Add to the guards to round up. */
276 fraction
+= GARDROUND
;
278 /* Perhaps the rounding means we now need to change the
279 exponent, because the fraction is no longer denormal. */
280 if (fraction
>= IMPLICIT_1
)
285 #endif /* NO_DENORMALS */
287 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
)
288 && __builtin_expect (src
->normal_exp
> EXPBIAS
, 0))
295 exp
= src
->normal_exp
+ EXPBIAS
;
296 if (!ROUND_TOWARDS_ZERO
)
298 /* IF the gard bits are the all zero, but the first, then we're
299 half way between two numbers, choose the one which makes the
300 lsb of the answer 0. */
301 if ((fraction
& GARDMASK
) == GARDMSB
)
303 if (fraction
& (1 << NGARDS
))
304 fraction
+= GARDROUND
+ 1;
308 /* Add a one to the guards to round up */
309 fraction
+= GARDROUND
;
311 if (fraction
>= IMPLICIT_2
)
319 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
) && exp
> EXPMAX
)
321 /* Saturate on overflow. */
323 fraction
= ((fractype
) 1 << FRACBITS
) - 1;
328 /* We previously used bitfields to store the number, but this doesn't
329 handle little/big endian systems conveniently, so use shifts and
331 #ifdef FLOAT_BIT_ORDER_MISMATCH
332 dst
.bits
.fraction
= fraction
;
334 dst
.bits
.sign
= sign
;
336 # if defined TFLOAT && defined HALFFRACBITS
338 halffractype high
, low
, unity
;
341 unity
= (halffractype
) 1 << HALFFRACBITS
;
343 /* Set HIGH to the high double's significand, masking out the implicit 1.
344 Set LOW to the low double's full significand. */
345 high
= (fraction
>> (FRACBITS
- HALFFRACBITS
)) & (unity
- 1);
346 low
= fraction
& (unity
* 2 - 1);
348 /* Get the initial sign and exponent of the low double. */
349 lowexp
= exp
- HALFFRACBITS
- 1;
352 /* HIGH should be rounded like a normal double, making |LOW| <=
353 0.5 ULP of HIGH. Assume round-to-nearest. */
355 if (low
> unity
|| (low
== unity
&& (high
& 1) == 1))
357 /* Round HIGH up and adjust LOW to match. */
361 /* May make it infinite, but that's OK. */
365 low
= unity
* 2 - low
;
369 high
|= (halffractype
) exp
<< HALFFRACBITS
;
370 high
|= (halffractype
) sign
<< (HALFFRACBITS
+ EXPBITS
);
372 if (exp
== EXPMAX
|| exp
== 0 || low
== 0)
376 while (lowexp
> 0 && low
< unity
)
384 halffractype roundmsb
, round
;
388 roundmsb
= (1 << (shift
- 1));
389 round
= low
& ((roundmsb
<< 1) - 1);
394 if (round
> roundmsb
|| (round
== roundmsb
&& (low
& 1) == 1))
398 /* LOW rounds up to the smallest normal number. */
404 low
|= (halffractype
) lowexp
<< HALFFRACBITS
;
405 low
|= (halffractype
) lowsign
<< (HALFFRACBITS
+ EXPBITS
);
407 dst
.value_raw
= ((fractype
) high
<< HALFSHIFT
) | low
;
410 dst
.value_raw
= fraction
& ((((fractype
)1) << FRACBITS
) - (fractype
)1);
411 dst
.value_raw
|= ((fractype
) (exp
& ((1 << EXPBITS
) - 1))) << FRACBITS
;
412 dst
.value_raw
|= ((fractype
) (sign
& 1)) << (FRACBITS
| EXPBITS
);
416 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
419 qrtrfractype tmp1
= dst
.words
[0];
420 qrtrfractype tmp2
= dst
.words
[1];
421 dst
.words
[0] = dst
.words
[3];
422 dst
.words
[1] = dst
.words
[2];
428 halffractype tmp
= dst
.words
[0];
429 dst
.words
[0] = dst
.words
[1];
439 #if defined(L_unpack_df) || defined(L_unpack_sf) || defined(L_unpack_tf)
441 unpack_d (FLO_union_type
* src
, fp_number_type
* dst
)
443 /* We previously used bitfields to store the number, but this doesn't
444 handle little/big endian systems conveniently, so use shifts and
450 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
451 FLO_union_type swapped
;
454 swapped
.words
[0] = src
->words
[3];
455 swapped
.words
[1] = src
->words
[2];
456 swapped
.words
[2] = src
->words
[1];
457 swapped
.words
[3] = src
->words
[0];
459 swapped
.words
[0] = src
->words
[1];
460 swapped
.words
[1] = src
->words
[0];
465 #ifdef FLOAT_BIT_ORDER_MISMATCH
466 fraction
= src
->bits
.fraction
;
468 sign
= src
->bits
.sign
;
470 # if defined TFLOAT && defined HALFFRACBITS
472 halffractype high
, low
;
474 high
= src
->value_raw
>> HALFSHIFT
;
475 low
= src
->value_raw
& (((fractype
)1 << HALFSHIFT
) - 1);
477 fraction
= high
& ((((fractype
)1) << HALFFRACBITS
) - 1);
478 fraction
<<= FRACBITS
- HALFFRACBITS
;
479 exp
= ((int)(high
>> HALFFRACBITS
)) & ((1 << EXPBITS
) - 1);
480 sign
= ((int)(high
>> (((HALFFRACBITS
+ EXPBITS
))))) & 1;
482 if (exp
!= EXPMAX
&& exp
!= 0 && low
!= 0)
484 int lowexp
= ((int)(low
>> HALFFRACBITS
)) & ((1 << EXPBITS
) - 1);
485 int lowsign
= ((int)(low
>> (((HALFFRACBITS
+ EXPBITS
))))) & 1;
489 xlow
= low
& ((((fractype
)1) << HALFFRACBITS
) - 1);
491 xlow
|= (((halffractype
)1) << HALFFRACBITS
);
494 shift
= (FRACBITS
- HALFFRACBITS
) - (exp
- lowexp
);
501 else if (fraction
>= xlow
)
505 /* The high part is a power of two but the full number is lower.
506 This code will leave the implicit 1 in FRACTION, but we'd
507 have added that below anyway. */
508 fraction
= (((fractype
) 1 << FRACBITS
) - xlow
) << 1;
514 fraction
= src
->value_raw
& ((((fractype
)1) << FRACBITS
) - 1);
515 exp
= ((int)(src
->value_raw
>> FRACBITS
)) & ((1 << EXPBITS
) - 1);
516 sign
= ((int)(src
->value_raw
>> (FRACBITS
+ EXPBITS
))) & 1;
523 /* Hmm. Looks like 0 */
530 /* tastes like zero */
531 dst
->class = CLASS_ZERO
;
535 /* Zero exponent with nonzero fraction - it's denormalized,
536 so there isn't a leading implicit one - we'll shift it so
538 dst
->normal_exp
= exp
- EXPBIAS
+ 1;
541 dst
->class = CLASS_NUMBER
;
543 while (fraction
< IMPLICIT_1
)
549 dst
->fraction
.ll
= fraction
;
552 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS
)
553 && __builtin_expect (exp
== EXPMAX
, 0))
558 /* Attached to a zero fraction - means infinity */
559 dst
->class = CLASS_INFINITY
;
563 /* Nonzero fraction, means nan */
564 #ifdef QUIET_NAN_NEGATED
565 if ((fraction
& QUIET_NAN
) == 0)
567 if (fraction
& QUIET_NAN
)
570 dst
->class = CLASS_QNAN
;
574 dst
->class = CLASS_SNAN
;
576 /* Keep the fraction part as the nan number */
577 dst
->fraction
.ll
= fraction
;
582 /* Nothing strange about this number */
583 dst
->normal_exp
= exp
- EXPBIAS
;
584 dst
->class = CLASS_NUMBER
;
585 dst
->fraction
.ll
= (fraction
<< NGARDS
) | IMPLICIT_1
;
588 #endif /* L_unpack_df || L_unpack_sf */
590 #if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
591 static const fp_number_type
*
592 _fpadd_parts (fp_number_type
* a
,
594 fp_number_type
* tmp
)
598 /* Put commonly used fields in local variables. */
614 /* Adding infinities with opposite signs yields a NaN. */
615 if (isinf (b
) && a
->sign
!= b
->sign
)
628 tmp
->sign
= a
->sign
& b
->sign
;
638 /* Got two numbers. shift the smaller and increment the exponent till
644 a_normal_exp
= a
->normal_exp
;
645 b_normal_exp
= b
->normal_exp
;
646 a_fraction
= a
->fraction
.ll
;
647 b_fraction
= b
->fraction
.ll
;
649 diff
= a_normal_exp
- b_normal_exp
;
654 if (diff
< FRAC_NBITS
)
658 b_normal_exp
+= diff
;
659 LSHIFT (b_fraction
, diff
);
663 a_normal_exp
+= diff
;
664 LSHIFT (a_fraction
, diff
);
669 /* Somethings's up.. choose the biggest */
670 if (a_normal_exp
> b_normal_exp
)
672 b_normal_exp
= a_normal_exp
;
677 a_normal_exp
= b_normal_exp
;
683 if (a
->sign
!= b
->sign
)
687 tfraction
= -a_fraction
+ b_fraction
;
691 tfraction
= a_fraction
- b_fraction
;
696 tmp
->normal_exp
= a_normal_exp
;
697 tmp
->fraction
.ll
= tfraction
;
702 tmp
->normal_exp
= a_normal_exp
;
703 tmp
->fraction
.ll
= -tfraction
;
705 /* and renormalize it */
707 while (tmp
->fraction
.ll
< IMPLICIT_1
&& tmp
->fraction
.ll
)
709 tmp
->fraction
.ll
<<= 1;
716 tmp
->normal_exp
= a_normal_exp
;
717 tmp
->fraction
.ll
= a_fraction
+ b_fraction
;
719 tmp
->class = CLASS_NUMBER
;
720 /* Now the fraction is added, we have to shift down to renormalize the
723 if (tmp
->fraction
.ll
>= IMPLICIT_2
)
725 LSHIFT (tmp
->fraction
.ll
, 1);
732 add (FLO_type arg_a
, FLO_type arg_b
)
737 const fp_number_type
*res
;
738 FLO_union_type au
, bu
;
746 res
= _fpadd_parts (&a
, &b
, &tmp
);
752 sub (FLO_type arg_a
, FLO_type arg_b
)
757 const fp_number_type
*res
;
758 FLO_union_type au
, bu
;
768 res
= _fpadd_parts (&a
, &b
, &tmp
);
772 #endif /* L_addsub_sf || L_addsub_df */
774 #if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
775 static inline __attribute__ ((__always_inline__
)) const fp_number_type
*
776 _fpmul_parts ( fp_number_type
* a
,
778 fp_number_type
* tmp
)
785 a
->sign
= a
->sign
!= b
->sign
;
790 b
->sign
= a
->sign
!= b
->sign
;
797 a
->sign
= a
->sign
!= b
->sign
;
806 b
->sign
= a
->sign
!= b
->sign
;
811 a
->sign
= a
->sign
!= b
->sign
;
816 b
->sign
= a
->sign
!= b
->sign
;
820 /* Calculate the mantissa by multiplying both numbers to get a
821 twice-as-wide number. */
823 #if defined(NO_DI_MODE) || defined(TFLOAT)
825 fractype x
= a
->fraction
.ll
;
826 fractype ylow
= b
->fraction
.ll
;
830 /* ??? This does multiplies one bit at a time. Optimize. */
831 for (bit
= 0; bit
< FRAC_NBITS
; bit
++)
837 carry
= (low
+= ylow
) < ylow
;
838 high
+= yhigh
+ carry
;
850 /* Multiplying two USIs to get a UDI, we're safe. */
852 UDItype answer
= (UDItype
)a
->fraction
.ll
* (UDItype
)b
->fraction
.ll
;
854 high
= answer
>> BITS_PER_SI
;
858 /* fractype is DImode, but we need the result to be twice as wide.
859 Assuming a widening multiply from DImode to TImode is not
860 available, build one by hand. */
862 USItype nl
= a
->fraction
.ll
;
863 USItype nh
= a
->fraction
.ll
>> BITS_PER_SI
;
864 USItype ml
= b
->fraction
.ll
;
865 USItype mh
= b
->fraction
.ll
>> BITS_PER_SI
;
866 UDItype pp_ll
= (UDItype
) ml
* nl
;
867 UDItype pp_hl
= (UDItype
) mh
* nl
;
868 UDItype pp_lh
= (UDItype
) ml
* nh
;
869 UDItype pp_hh
= (UDItype
) mh
* nh
;
872 UDItype ps_hh__
= pp_hl
+ pp_lh
;
874 res2
+= (UDItype
)1 << BITS_PER_SI
;
875 pp_hl
= (UDItype
)(USItype
)ps_hh__
<< BITS_PER_SI
;
876 res0
= pp_ll
+ pp_hl
;
879 res2
+= (ps_hh__
>> BITS_PER_SI
) + pp_hh
;
886 tmp
->normal_exp
= a
->normal_exp
+ b
->normal_exp
887 + FRAC_NBITS
- (FRACBITS
+ NGARDS
);
888 tmp
->sign
= a
->sign
!= b
->sign
;
889 while (high
>= IMPLICIT_2
)
899 while (high
< IMPLICIT_1
)
909 if (!ROUND_TOWARDS_ZERO
&& (high
& GARDMASK
) == GARDMSB
)
911 if (high
& (1 << NGARDS
))
913 /* Because we're half way, we would round to even by adding
914 GARDROUND + 1, except that's also done in the packing
915 function, and rounding twice will lose precision and cause
916 the result to be too far off. Example: 32-bit floats with
917 bit patterns 0xfff * 0x3f800400 ~= 0xfff (less than 0.5ulp
918 off), not 0x1000 (more than 0.5ulp off). */
922 /* We're a further than half way by a small amount corresponding
923 to the bits set in "low". Knowing that, we round here and
924 not in pack_d, because there we don't have "low" available
926 high
+= GARDROUND
+ 1;
928 /* Avoid further rounding in pack_d. */
929 high
&= ~(fractype
) GARDMASK
;
932 tmp
->fraction
.ll
= high
;
933 tmp
->class = CLASS_NUMBER
;
938 multiply (FLO_type arg_a
, FLO_type arg_b
)
943 const fp_number_type
*res
;
944 FLO_union_type au
, bu
;
952 res
= _fpmul_parts (&a
, &b
, &tmp
);
956 #endif /* L_mul_sf || L_mul_df || L_mul_tf */
958 #if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
959 static inline __attribute__ ((__always_inline__
)) const fp_number_type
*
960 _fpdiv_parts (fp_number_type
* a
,
965 fractype denominator
;
977 a
->sign
= a
->sign
^ b
->sign
;
979 if (isinf (a
) || iszero (a
))
981 if (a
->class == b
->class)
994 a
->class = CLASS_INFINITY
;
998 /* Calculate the mantissa by multiplying both 64bit numbers to get a
1002 ( numerator / denominator) * 2^(numerator exponent - denominator exponent)
1005 a
->normal_exp
= a
->normal_exp
- b
->normal_exp
;
1006 numerator
= a
->fraction
.ll
;
1007 denominator
= b
->fraction
.ll
;
1009 if (numerator
< denominator
)
1011 /* Fraction will be less than 1.0 */
1017 /* ??? Does divide one bit at a time. Optimize. */
1020 if (numerator
>= denominator
)
1023 numerator
-= denominator
;
1029 if (!ROUND_TOWARDS_ZERO
&& (quotient
& GARDMASK
) == GARDMSB
)
1031 if (quotient
& (1 << NGARDS
))
1033 /* Because we're half way, we would round to even by adding
1034 GARDROUND + 1, except that's also done in the packing
1035 function, and rounding twice will lose precision and cause
1036 the result to be too far off. */
1040 /* We're a further than half way by the small amount
1041 corresponding to the bits set in "numerator". Knowing
1042 that, we round here and not in pack_d, because there we
1043 don't have "numerator" available anymore. */
1044 quotient
+= GARDROUND
+ 1;
1046 /* Avoid further rounding in pack_d. */
1047 quotient
&= ~(fractype
) GARDMASK
;
1051 a
->fraction
.ll
= quotient
;
1057 divide (FLO_type arg_a
, FLO_type arg_b
)
1061 const fp_number_type
*res
;
1062 FLO_union_type au
, bu
;
1070 res
= _fpdiv_parts (&a
, &b
);
1072 return pack_d (res
);
1074 #endif /* L_div_sf || L_div_df */
1076 #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df) \
1077 || defined(L_fpcmp_parts_tf)
1078 /* according to the demo, fpcmp returns a comparison with 0... thus
1085 __fpcmp_parts (fp_number_type
* a
, fp_number_type
* b
)
1088 /* either nan -> unordered. Must be checked outside of this routine. */
1089 if (isnan (a
) && isnan (b
))
1091 return 1; /* still unordered! */
1095 if (isnan (a
) || isnan (b
))
1097 return 1; /* how to indicate unordered compare? */
1099 if (isinf (a
) && isinf (b
))
1101 /* +inf > -inf, but +inf != +inf */
1102 /* b \a| +inf(0)| -inf(1)
1103 ______\+--------+--------
1104 +inf(0)| a==b(0)| a<b(-1)
1105 -------+--------+--------
1106 -inf(1)| a>b(1) | a==b(0)
1107 -------+--------+--------
1108 So since unordered must be nonzero, just line up the columns...
1110 return b
->sign
- a
->sign
;
1112 /* but not both... */
1115 return a
->sign
? -1 : 1;
1119 return b
->sign
? 1 : -1;
1121 if (iszero (a
) && iszero (b
))
1127 return b
->sign
? 1 : -1;
1131 return a
->sign
? -1 : 1;
1133 /* now both are "normal". */
1134 if (a
->sign
!= b
->sign
)
1136 /* opposite signs */
1137 return a
->sign
? -1 : 1;
1139 /* same sign; exponents? */
1140 if (a
->normal_exp
> b
->normal_exp
)
1142 return a
->sign
? -1 : 1;
1144 if (a
->normal_exp
< b
->normal_exp
)
1146 return a
->sign
? 1 : -1;
1148 /* same exponents; check size. */
1149 if (a
->fraction
.ll
> b
->fraction
.ll
)
1151 return a
->sign
? -1 : 1;
1153 if (a
->fraction
.ll
< b
->fraction
.ll
)
1155 return a
->sign
? 1 : -1;
1157 /* after all that, they're equal. */
1162 #if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compoare_tf)
1164 compare (FLO_type arg_a
, FLO_type arg_b
)
1168 FLO_union_type au
, bu
;
1176 return __fpcmp_parts (&a
, &b
);
1178 #endif /* L_compare_sf || L_compare_df */
1180 /* These should be optimized for their specific tasks someday. */
1182 #if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
1184 _eq_f2 (FLO_type arg_a
, FLO_type arg_b
)
1188 FLO_union_type au
, bu
;
1196 if (isnan (&a
) || isnan (&b
))
1197 return 1; /* false, truth == 0 */
1199 return __fpcmp_parts (&a
, &b
) ;
1201 #endif /* L_eq_sf || L_eq_df */
1203 #if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
1205 _ne_f2 (FLO_type arg_a
, FLO_type arg_b
)
1209 FLO_union_type au
, bu
;
1217 if (isnan (&a
) || isnan (&b
))
1218 return 1; /* true, truth != 0 */
1220 return __fpcmp_parts (&a
, &b
) ;
1222 #endif /* L_ne_sf || L_ne_df */
1224 #if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
1226 _gt_f2 (FLO_type arg_a
, FLO_type arg_b
)
1230 FLO_union_type au
, bu
;
1238 if (isnan (&a
) || isnan (&b
))
1239 return -1; /* false, truth > 0 */
1241 return __fpcmp_parts (&a
, &b
);
1243 #endif /* L_gt_sf || L_gt_df */
1245 #if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
1247 _ge_f2 (FLO_type arg_a
, FLO_type arg_b
)
1251 FLO_union_type au
, bu
;
1259 if (isnan (&a
) || isnan (&b
))
1260 return -1; /* false, truth >= 0 */
1261 return __fpcmp_parts (&a
, &b
) ;
1263 #endif /* L_ge_sf || L_ge_df */
1265 #if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
1267 _lt_f2 (FLO_type arg_a
, FLO_type arg_b
)
1271 FLO_union_type au
, bu
;
1279 if (isnan (&a
) || isnan (&b
))
1280 return 1; /* false, truth < 0 */
1282 return __fpcmp_parts (&a
, &b
);
1284 #endif /* L_lt_sf || L_lt_df */
1286 #if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
1288 _le_f2 (FLO_type arg_a
, FLO_type arg_b
)
1292 FLO_union_type au
, bu
;
1300 if (isnan (&a
) || isnan (&b
))
1301 return 1; /* false, truth <= 0 */
1303 return __fpcmp_parts (&a
, &b
) ;
1305 #endif /* L_le_sf || L_le_df */
1307 #if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
1309 _unord_f2 (FLO_type arg_a
, FLO_type arg_b
)
1313 FLO_union_type au
, bu
;
1321 return (isnan (&a
) || isnan (&b
));
1323 #endif /* L_unord_sf || L_unord_df */
1325 #if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
1327 si_to_float (SItype arg_a
)
1331 in
.class = CLASS_NUMBER
;
1332 in
.sign
= arg_a
< 0;
1335 in
.class = CLASS_ZERO
;
1341 in
.normal_exp
= FRACBITS
+ NGARDS
;
1344 /* Special case for minint, since there is no +ve integer
1345 representation for it */
1346 if (arg_a
== (- MAX_SI_INT
- 1))
1348 return (FLO_type
)(- MAX_SI_INT
- 1);
1355 in
.fraction
.ll
= uarg
;
1356 shift
= clzusi (uarg
) - (BITS_PER_SI
- 1 - FRACBITS
- NGARDS
);
1359 in
.fraction
.ll
<<= shift
;
1360 in
.normal_exp
-= shift
;
1363 return pack_d (&in
);
1365 #endif /* L_si_to_sf || L_si_to_df */
1367 #if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
1369 usi_to_float (USItype arg_a
)
1376 in
.class = CLASS_ZERO
;
1381 in
.class = CLASS_NUMBER
;
1382 in
.normal_exp
= FRACBITS
+ NGARDS
;
1383 in
.fraction
.ll
= arg_a
;
1385 shift
= clzusi (arg_a
) - (BITS_PER_SI
- 1 - FRACBITS
- NGARDS
);
1388 fractype guard
= in
.fraction
.ll
& (((fractype
)1 << -shift
) - 1);
1389 in
.fraction
.ll
>>= -shift
;
1390 in
.fraction
.ll
|= (guard
!= 0);
1391 in
.normal_exp
-= shift
;
1395 in
.fraction
.ll
<<= shift
;
1396 in
.normal_exp
-= shift
;
1399 return pack_d (&in
);
1403 #if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
1405 float_to_si (FLO_type arg_a
)
1418 /* get reasonable MAX_SI_INT... */
1420 return a
.sign
? (-MAX_SI_INT
)-1 : MAX_SI_INT
;
1421 /* it is a number, but a small one */
1422 if (a
.normal_exp
< 0)
1424 if (a
.normal_exp
> BITS_PER_SI
- 2)
1425 return a
.sign
? (-MAX_SI_INT
)-1 : MAX_SI_INT
;
1426 tmp
= a
.fraction
.ll
>> ((FRACBITS
+ NGARDS
) - a
.normal_exp
);
1427 return a
.sign
? (-tmp
) : (tmp
);
1429 #endif /* L_sf_to_si || L_df_to_si */
1431 #if defined(L_tf_to_usi)
1433 float_to_usi (FLO_type arg_a
)
1445 /* it is a negative number */
1448 /* get reasonable MAX_USI_INT... */
1451 /* it is a number, but a small one */
1452 if (a
.normal_exp
< 0)
1454 if (a
.normal_exp
> BITS_PER_SI
- 1)
1456 else if (a
.normal_exp
> (FRACBITS
+ NGARDS
))
1457 return a
.fraction
.ll
<< (a
.normal_exp
- (FRACBITS
+ NGARDS
));
1459 return a
.fraction
.ll
>> ((FRACBITS
+ NGARDS
) - a
.normal_exp
);
1461 #endif /* L_tf_to_usi */
1463 #if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
1465 negate (FLO_type arg_a
)
1476 #endif /* L_negate_sf || L_negate_df */
1480 #if defined(L_make_sf)
1482 __make_fp(fp_class_type
class,
1491 in
.normal_exp
= exp
;
1492 in
.fraction
.ll
= frac
;
1493 return pack_d (&in
);
1495 #endif /* L_make_sf */
1499 /* This enables one to build an fp library that supports float but not double.
1500 Otherwise, we would get an undefined reference to __make_dp.
1501 This is needed for some 8-bit ports that can't handle well values that
1502 are 8-bytes in size, so we just don't support double for them at all. */
1504 #if defined(L_sf_to_df)
1506 sf_to_df (SFtype arg_a
)
1512 unpack_d (&au
, &in
);
1514 return __make_dp (in
.class, in
.sign
, in
.normal_exp
,
1515 ((UDItype
) in
.fraction
.ll
) << F_D_BITOFF
);
1517 #endif /* L_sf_to_df */
1519 #if defined(L_sf_to_tf) && defined(TMODES)
1521 sf_to_tf (SFtype arg_a
)
1527 unpack_d (&au
, &in
);
1529 return __make_tp (in
.class, in
.sign
, in
.normal_exp
,
1530 ((UTItype
) in
.fraction
.ll
) << F_T_BITOFF
);
1532 #endif /* L_sf_to_df */
1534 #endif /* ! FLOAT_ONLY */
1539 extern SFtype
__make_fp (fp_class_type
, unsigned int, int, USItype
);
1541 #if defined(L_make_df)
1543 __make_dp (fp_class_type
class, unsigned int sign
, int exp
, UDItype frac
)
1549 in
.normal_exp
= exp
;
1550 in
.fraction
.ll
= frac
;
1551 return pack_d (&in
);
1553 #endif /* L_make_df */
1555 #if defined(L_df_to_sf)
1557 df_to_sf (DFtype arg_a
)
1564 unpack_d (&au
, &in
);
1566 sffrac
= in
.fraction
.ll
>> F_D_BITOFF
;
1568 /* We set the lowest guard bit in SFFRAC if we discarded any non
1570 if ((in
.fraction
.ll
& (((USItype
) 1 << F_D_BITOFF
) - 1)) != 0)
1573 return __make_fp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1575 #endif /* L_df_to_sf */
1577 #if defined(L_df_to_tf) && defined(TMODES) \
1578 && !defined(FLOAT) && !defined(TFLOAT)
1580 df_to_tf (DFtype arg_a
)
1586 unpack_d (&au
, &in
);
1588 return __make_tp (in
.class, in
.sign
, in
.normal_exp
,
1589 ((UTItype
) in
.fraction
.ll
) << D_T_BITOFF
);
1591 #endif /* L_sf_to_df */
1594 #if defined(L_make_tf)
1596 __make_tp(fp_class_type
class,
1605 in
.normal_exp
= exp
;
1606 in
.fraction
.ll
= frac
;
1607 return pack_d (&in
);
1609 #endif /* L_make_tf */
1611 #if defined(L_tf_to_df)
1613 tf_to_df (TFtype arg_a
)
1620 unpack_d (&au
, &in
);
1622 sffrac
= in
.fraction
.ll
>> D_T_BITOFF
;
1624 /* We set the lowest guard bit in SFFRAC if we discarded any non
1626 if ((in
.fraction
.ll
& (((UTItype
) 1 << D_T_BITOFF
) - 1)) != 0)
1629 return __make_dp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1631 #endif /* L_tf_to_df */
1633 #if defined(L_tf_to_sf)
1635 tf_to_sf (TFtype arg_a
)
1642 unpack_d (&au
, &in
);
1644 sffrac
= in
.fraction
.ll
>> F_T_BITOFF
;
1646 /* We set the lowest guard bit in SFFRAC if we discarded any non
1648 if ((in
.fraction
.ll
& (((UTItype
) 1 << F_T_BITOFF
) - 1)) != 0)
1651 return __make_fp (in
.class, in
.sign
, in
.normal_exp
, sffrac
);
1653 #endif /* L_tf_to_sf */
1656 #endif /* ! FLOAT */
1657 #endif /* !EXTENDED_FLOAT_STUBS */