1 /* This is a software floating point library which can be used instead of
2 the floating point routines in libgcc1.c for targets without hardware
5 This implements IEEE 754 format arithmetic, but does not provide a
6 mechanism for setting the rounding mode, or for generating or handling
9 The original code by Steve Chamberlain, hacked by Mark Eichin and Jim
10 Wilson, all of Cygnus Support.
12 This file is in the public domain. */
14 /* The intended way to use this file is to make two copies, add `#define FLOAT'
15 to one copy, then compile both copies and add them to libgcc.a. */
17 /* The following macros can be defined to change the behaviour of this file:
18 FLOAT: Implement a `float', aka SFmode, fp library. If this is not
19 defined, then this file implements a `double', aka DFmode, fp library.
20 FLOAT_ONLY: Used with FLOAT, to implement a `float' only library, i.e.
21 don't include float->double conversion which requires the double library.
22 This is useful only for machines which can't support doubles, e.g. some
24 CMPtype: Specify the type that floating point compares should return.
25 This defaults to SItype, aka int.
26 US_SOFTWARE_GOFAST: This makes all entry points use the same names as the
27 US Software goFast library. If this is not defined, the entry points use
28 the same names as libgcc1.c.
29 _DEBUG_BITFLOAT: This makes debugging the code a little easier, by adding
30 two integers to the FLO_union_type.
31 NO_NANS: Disable nan and infinity handling
32 SMALL_MACHINE: Useful when operations on QIs and HIs are faster
35 typedef SFtype __attribute__ ((mode (SF)));
36 typedef DFtype __attribute__ ((mode (DF)));
38 typedef int HItype __attribute__ ((mode (HI)));
39 typedef int SItype __attribute__ ((mode (SI)));
40 typedef int DItype __attribute__ ((mode (DI)));
42 /* The type of the result of a fp compare */
44 #define CMPtype SItype
47 typedef unsigned int UHItype __attribute__ ((mode (HI)));
48 typedef unsigned int USItype __attribute__ ((mode (SI)));
49 typedef unsigned int UDItype __attribute__ ((mode (DI)));
51 #define MAX_SI_INT ((SItype) ((unsigned) (~0)>>1))
52 #define MAX_USI_INT ((USItype) ~0)
61 # define GARDROUND 0x3f
62 # define GARDMASK 0x7f
67 # define EXPMAX (0xff)
68 # define QUIET_NAN 0x100000L
69 # define FRAC_NBITS 32
70 # define FRACHIGH 0x80000000L
71 # define FRACHIGH2 0xc0000000L
72 typedef USItype fractype;
73 typedef UHItype halffractype;
74 typedef SFtype FLO_type;
75 typedef SItype intfrac;
78 # define PREFIXFPDP dp
79 # define PREFIXSFDF df
81 # define GARDROUND 0x7f
82 # define GARDMASK 0xff
87 # define EXPMAX (0x7ff)
88 # define QUIET_NAN 0x8000000000000LL
89 # define FRAC_NBITS 64
90 # define FRACHIGH 0x8000000000000000LL
91 # define FRACHIGH2 0xc000000000000000LL
92 typedef UDItype fractype;
93 typedef USItype halffractype;
94 typedef DFtype FLO_type;
95 typedef DItype intfrac;
98 #ifdef US_SOFTWARE_GOFAST
102 # define multiply fpmul
103 # define divide fpdiv
104 # define compare fpcmp
105 # define si_to_float sitofp
106 # define float_to_si fptosi
107 # define float_to_usi fptoui
108 # define negate __negsf2
109 # define sf_to_df fptodp
110 # define dptofp dptofp
114 # define multiply dpmul
115 # define divide dpdiv
116 # define compare dpcmp
117 # define si_to_float litodp
118 # define float_to_si dptoli
119 # define float_to_usi dptoul
120 # define negate __negdf2
121 # define df_to_sf dptofp
125 # define add __addsf3
126 # define sub __subsf3
127 # define multiply __mulsf3
128 # define divide __divsf3
129 # define compare __cmpsf2
130 # define _eq_f2 __eqsf2
131 # define _ne_f2 __nesf2
132 # define _gt_f2 __gtsf2
133 # define _ge_f2 __gesf2
134 # define _lt_f2 __ltsf2
135 # define _le_f2 __lesf2
136 # define si_to_float __floatsisf
137 # define float_to_si __fixsfsi
138 # define float_to_usi __fixunssfsi
139 # define negate __negsf2
140 # define sf_to_df __extendsfdf2
142 # define add __adddf3
143 # define sub __subdf3
144 # define multiply __muldf3
145 # define divide __divdf3
146 # define compare __cmpdf2
147 # define _eq_f2 __eqdf2
148 # define _ne_f2 __nedf2
149 # define _gt_f2 __gtdf2
150 # define _ge_f2 __gedf2
151 # define _lt_f2 __ltdf2
152 # define _le_f2 __ledf2
153 # define si_to_float __floatsidf
154 # define float_to_si __fixdfsi
155 # define float_to_usi __fixunsdfsi
156 # define negate __negdf2
157 # define df_to_sf __truncdfsf2
162 #define INLINE __inline__
164 /* Preserve the sticky-bit when shifting fractions to the right. */
165 #define LSHIFT(a) { a = (a & 1) | (a >> 1); }
167 /* numeric parameters */
168 /* F_D_BITOFF is the number of bits offset between the MSB of the mantissa
169 of a float and of a double. Assumes there are only two float types.
170 (double::FRAC_BITS+double::NGARGS-(float::FRAC_BITS-float::NGARDS))
172 #define F_D_BITOFF (52+8-(23+7))
175 #define NORMAL_EXPMIN (-(EXPBIAS)+1)
176 #define IMPLICIT_1 (1LL<<(FRACBITS+NGARDS))
177 #define IMPLICIT_2 (1LL<<(FRACBITS+1+NGARDS))
213 #ifdef _DEBUG_BITFLOAT
218 #ifndef FLOAT_BIT_ORDER_MISMATCH
220 unsigned int exp:EXPBITS;
221 fractype fraction:FRACBITS;
223 fractype fraction:FRACBITS;
224 unsigned int exp:EXPBITS;
236 /* IEEE "special" number predicates */
246 static fp_number_type *
249 static fp_number_type thenan;
256 isnan ( fp_number_type * x)
258 return x->class == CLASS_SNAN || x->class == CLASS_QNAN;
263 isinf ( fp_number_type * x)
265 return x->class == CLASS_INFINITY;
272 iszero ( fp_number_type * x)
274 return x->class == CLASS_ZERO;
279 flip_sign ( fp_number_type * x)
285 pack_d ( fp_number_type * src)
288 fractype fraction = src->fraction.ll; /* wasn't unsigned before? */
290 dst.bits.sign = src->sign;
294 dst.bits.exp = EXPMAX;
295 dst.bits.fraction = src->fraction.ll;
296 if (src->class == CLASS_QNAN || 1)
298 dst.bits.fraction |= QUIET_NAN;
301 else if (isinf (src))
303 dst.bits.exp = EXPMAX;
304 dst.bits.fraction = 0;
306 else if (iszero (src))
309 dst.bits.fraction = 0;
311 else if (fraction == 0)
317 if (src->normal_exp < NORMAL_EXPMIN)
319 /* This number's exponent is too low to fit into the bits
320 available in the number, so we'll store 0 in the exponent and
321 shift the fraction to the right to make up for it. */
323 int shift = NORMAL_EXPMIN - src->normal_exp;
327 if (shift > FRAC_NBITS - NGARDS)
329 /* No point shifting, since it's more that 64 out. */
334 /* Shift by the value */
338 dst.bits.fraction = fraction;
340 else if (src->normal_exp > EXPBIAS)
342 dst.bits.exp = EXPMAX;
343 dst.bits.fraction = 0;
347 dst.bits.exp = src->normal_exp + EXPBIAS;
348 /* IF the gard bits are the all zero, but the first, then we're
349 half way between two numbers, choose the one which makes the
350 lsb of the answer 0. */
351 if ((fraction & GARDMASK) == GARDMSB)
353 if (fraction & (1 << NGARDS))
354 fraction += GARDROUND + 1;
358 /* Add a one to the guards to round up */
359 fraction += GARDROUND;
361 if (fraction >= IMPLICIT_2)
367 dst.bits.fraction = fraction;
374 unpack_d (FLO_union_type * src, fp_number_type * dst)
376 fractype fraction = src->bits.fraction;
378 dst->sign = src->bits.sign;
379 if (src->bits.exp == 0)
381 /* Hmm. Looks like 0 */
384 /* tastes like zero */
385 dst->class = CLASS_ZERO;
389 /* Zero exponent with non zero fraction - it's denormalized,
390 so there isn't a leading implicit one - we'll shift it so
392 dst->normal_exp = src->bits.exp - EXPBIAS + 1;
395 dst->class = CLASS_NUMBER;
397 while (fraction < IMPLICIT_1)
403 dst->fraction.ll = fraction;
406 else if (src->bits.exp == EXPMAX)
411 /* Attatched to a zero fraction - means infinity */
412 dst->class = CLASS_INFINITY;
416 /* Non zero fraction, means nan */
419 dst->class = CLASS_SNAN;
423 dst->class = CLASS_QNAN;
425 /* Keep the fraction part as the nan number */
426 dst->fraction.ll = fraction;
431 /* Nothing strange about this number */
432 dst->normal_exp = src->bits.exp - EXPBIAS;
433 dst->class = CLASS_NUMBER;
434 dst->fraction.ll = (fraction << NGARDS) | IMPLICIT_1;
438 static fp_number_type *
439 _fpadd_parts (fp_number_type * a,
441 fp_number_type * tmp)
445 /* Put commonly used fields in local variables. */
476 /* Got two numbers. shift the smaller and increment the exponent till
481 a_normal_exp = a->normal_exp;
482 b_normal_exp = b->normal_exp;
483 a_fraction = a->fraction.ll;
484 b_fraction = b->fraction.ll;
486 diff = a_normal_exp - b_normal_exp;
490 if (diff < FRAC_NBITS)
492 /* ??? This does shifts one bit at a time. Optimize. */
493 while (a_normal_exp > b_normal_exp)
498 while (b_normal_exp > a_normal_exp)
506 /* Somethings's up.. choose the biggest */
507 if (a_normal_exp > b_normal_exp)
509 b_normal_exp = a_normal_exp;
514 a_normal_exp = b_normal_exp;
520 if (a->sign != b->sign)
524 tfraction = -a_fraction + b_fraction;
528 tfraction = a_fraction - b_fraction;
533 tmp->normal_exp = a_normal_exp;
534 tmp->fraction.ll = tfraction;
539 tmp->normal_exp = a_normal_exp;
540 tmp->fraction.ll = -tfraction;
542 /* and renomalize it */
544 while (tmp->fraction.ll < IMPLICIT_1 && tmp->fraction.ll)
546 tmp->fraction.ll <<= 1;
553 tmp->normal_exp = a_normal_exp;
554 tmp->fraction.ll = a_fraction + b_fraction;
556 tmp->class = CLASS_NUMBER;
557 /* Now the fraction is added, we have to shift down to renormalize the
560 if (tmp->fraction.ll >= IMPLICIT_2)
562 LSHIFT (tmp->fraction.ll);
570 add (FLO_type arg_a, FLO_type arg_b)
577 unpack_d ((FLO_union_type *) & arg_a, &a);
578 unpack_d ((FLO_union_type *) & arg_b, &b);
580 res = _fpadd_parts (&a, &b, &tmp);
586 sub (FLO_type arg_a, FLO_type arg_b)
593 unpack_d ((FLO_union_type *) & arg_a, &a);
594 unpack_d ((FLO_union_type *) & arg_b, &b);
598 res = _fpadd_parts (&a, &b, &tmp);
603 static fp_number_type *
604 _fpmul_parts ( fp_number_type * a,
606 fp_number_type * tmp)
613 a->sign = a->sign != b->sign;
618 b->sign = a->sign != b->sign;
625 a->sign = a->sign != b->sign;
634 b->sign = a->sign != b->sign;
639 a->sign = a->sign != b->sign;
644 b->sign = a->sign != b->sign;
648 /* Calculate the mantissa by multiplying both 64bit numbers to get a
651 fractype x = a->fraction.ll;
652 fractype ylow = b->fraction.ll;
656 #if defined(NO_DI_MODE)
658 /* ??? This does multiplies one bit at a time. Optimize. */
659 for (bit = 0; bit < FRAC_NBITS; bit++)
665 carry = (low += ylow) < ylow;
666 high += yhigh + carry;
679 /* Multiplying two 32 bit numbers to get a 64 bit number on
680 a machine with DI, so we're safe */
682 DItype answer = (DItype)(a->fraction.ll) * (DItype)(b->fraction.ll);
688 /* Doing a 64*64 to 128 */
690 UDItype nl = a->fraction.ll;
691 UDItype nh = a->fraction.ll >> 32;
692 UDItype ml = b->fraction.ll;
693 UDItype mh = b->fraction.ll >>32;
694 UDItype pp_ll = ml * nl;
695 UDItype pp_hl = mh * nl;
696 UDItype pp_lh = ml * nh;
697 UDItype pp_hh = mh * nh;
700 UDItype ps_hh__ = pp_hl + pp_lh;
702 res2 += 0x100000000LL;
703 pp_hl = (ps_hh__ << 32) & 0xffffffff00000000LL;
704 res0 = pp_ll + pp_hl;
707 res2 += ((ps_hh__ >> 32) & 0xffffffffL) + pp_hh;
714 tmp->normal_exp = a->normal_exp + b->normal_exp;
715 tmp->sign = a->sign != b->sign;
717 tmp->normal_exp += 2; /* ??????????????? */
719 tmp->normal_exp += 4; /* ??????????????? */
721 while (high >= IMPLICIT_2)
731 while (high < IMPLICIT_1)
740 /* rounding is tricky. if we only round if it won't make us round later. */
744 if (((high & GARDMASK) != GARDMSB)
745 && (((high + 1) & GARDMASK) == GARDMSB))
747 /* don't round, it gets done again later. */
755 if ((high & GARDMASK) == GARDMSB)
757 if (high & (1 << NGARDS))
759 /* half way, so round to even */
760 high += GARDROUND + 1;
764 /* but we really weren't half way */
765 high += GARDROUND + 1;
768 tmp->fraction.ll = high;
769 tmp->class = CLASS_NUMBER;
774 multiply (FLO_type arg_a, FLO_type arg_b)
781 unpack_d ((FLO_union_type *) & arg_a, &a);
782 unpack_d ((FLO_union_type *) & arg_b, &b);
784 res = _fpmul_parts (&a, &b, &tmp);
789 static fp_number_type *
790 _fpdiv_parts (fp_number_type * a,
792 fp_number_type * tmp)
796 fractype r0, r1, y0, y1, bit;
799 fractype denominator;
811 if (isinf (a) || iszero (a))
813 if (a->class == b->class)
817 a->sign = a->sign ^ b->sign;
827 a->class = CLASS_INFINITY;
831 /* Calculate the mantissa by multiplying both 64bit numbers to get a
835 intfrac d0, d1; /* weren't unsigned before ??? */
838 ( numerator / denominator) * 2^(numerator exponent - denominator exponent)
841 a->normal_exp = a->normal_exp - b->normal_exp;
842 numerator = a->fraction.ll;
843 denominator = b->fraction.ll;
845 if (numerator < denominator)
847 /* Fraction will be less than 1.0 */
853 /* ??? Does divide one bit at a time. Optimize. */
856 if (numerator >= denominator)
859 numerator -= denominator;
865 if ((quotient & GARDMASK) == GARDMSB)
867 if (quotient & (1 << NGARDS))
869 /* half way, so round to even */
870 quotient += GARDROUND + 1;
874 /* but we really weren't half way, more bits exist */
875 quotient += GARDROUND + 1;
879 a->fraction.ll = quotient;
885 divide (FLO_type arg_a, FLO_type arg_b)
892 unpack_d ((FLO_union_type *) & arg_a, &a);
893 unpack_d ((FLO_union_type *) & arg_b, &b);
895 res = _fpdiv_parts (&a, &b, &tmp);
900 /* according to the demo, fpcmp returns a comparison with 0... thus
907 _fpcmp_parts (fp_number_type * a, fp_number_type * b)
910 /* either nan -> unordered. Must be checked outside of this routine. */
911 if (isnan (a) && isnan (b))
913 return 1; /* still unordered! */
917 if (isnan (a) || isnan (b))
919 return 1; /* how to indicate unordered compare? */
921 if (isinf (a) && isinf (b))
923 /* +inf > -inf, but +inf != +inf */
924 /* b \a| +inf(0)| -inf(1)
925 ______\+--------+--------
926 +inf(0)| a==b(0)| a<b(-1)
927 -------+--------+--------
928 -inf(1)| a>b(1) | a==b(0)
929 -------+--------+--------
930 So since unordered must be non zero, just line up the columns...
932 return b->sign - a->sign;
934 /* but not both... */
937 return a->sign ? -1 : 1;
941 return b->sign ? 1 : -1;
943 if (iszero (a) && iszero (b))
949 return b->sign ? 1 : -1;
953 return a->sign ? -1 : 1;
955 /* now both are "normal". */
956 if (a->sign != b->sign)
959 return a->sign ? -1 : 1;
961 /* same sign; exponents? */
962 if (a->normal_exp > b->normal_exp)
964 return a->sign ? -1 : 1;
966 if (a->normal_exp < b->normal_exp)
968 return a->sign ? 1 : -1;
970 /* same exponents; check size. */
971 if (a->fraction.ll > b->fraction.ll)
973 return a->sign ? -1 : 1;
975 if (a->fraction.ll < b->fraction.ll)
977 return a->sign ? 1 : -1;
979 /* after all that, they're equal. */
984 compare (FLO_type arg_a, FLO_type arg_b)
989 unpack_d ((FLO_union_type *) & arg_a, &a);
990 unpack_d ((FLO_union_type *) & arg_b, &b);
992 return _fpcmp_parts (&a, &b);
995 #ifndef US_SOFTWARE_GOFAST
997 /* These should be optimized for their specific tasks someday. */
1000 _eq_f2 (FLO_type arg_a, FLO_type arg_b)
1005 unpack_d ((FLO_union_type *) & arg_a, &a);
1006 unpack_d ((FLO_union_type *) & arg_b, &b);
1008 if (isnan (&a) || isnan (&b))
1009 return 1; /* false, truth == 0 */
1011 return _fpcmp_parts (&a, &b) ;
1015 _ne_f2 (FLO_type arg_a, FLO_type arg_b)
1020 unpack_d ((FLO_union_type *) & arg_a, &a);
1021 unpack_d ((FLO_union_type *) & arg_b, &b);
1023 if (isnan (&a) || isnan (&b))
1024 return 1; /* true, truth != 0 */
1026 return _fpcmp_parts (&a, &b) ;
1030 _gt_f2 (FLO_type arg_a, FLO_type arg_b)
1035 unpack_d ((FLO_union_type *) & arg_a, &a);
1036 unpack_d ((FLO_union_type *) & arg_b, &b);
1038 if (isnan (&a) || isnan (&b))
1039 return -1; /* false, truth > 0 */
1041 return _fpcmp_parts (&a, &b);
1045 _ge_f2 (FLO_type arg_a, FLO_type arg_b)
1050 unpack_d ((FLO_union_type *) & arg_a, &a);
1051 unpack_d ((FLO_union_type *) & arg_b, &b);
1053 if (isnan (&a) || isnan (&b))
1054 return -1; /* false, truth >= 0 */
1055 return _fpcmp_parts (&a, &b) ;
1059 _lt_f2 (FLO_type arg_a, FLO_type arg_b)
1064 unpack_d ((FLO_union_type *) & arg_a, &a);
1065 unpack_d ((FLO_union_type *) & arg_b, &b);
1067 if (isnan (&a) || isnan (&b))
1068 return 1; /* false, truth < 0 */
1070 return _fpcmp_parts (&a, &b);
1074 _le_f2 (FLO_type arg_a, FLO_type arg_b)
1079 unpack_d ((FLO_union_type *) & arg_a, &a);
1080 unpack_d ((FLO_union_type *) & arg_b, &b);
1082 if (isnan (&a) || isnan (&b))
1083 return 1; /* false, truth <= 0 */
1085 return _fpcmp_parts (&a, &b) ;
1088 #endif /* ! US_SOFTWARE_GOFAST */
1091 si_to_float (SItype arg_a)
1095 in.class = CLASS_NUMBER;
1096 in.sign = arg_a < 0;
1099 in.class = CLASS_ZERO;
1103 in.normal_exp = FRACBITS + NGARDS;
1106 /* Special case for minint, since there is no +ve integer
1107 representation for it */
1108 if (arg_a == 0x80000000)
1110 return -2147483648.0;
1112 in.fraction.ll = (-arg_a);
1115 in.fraction.ll = arg_a;
1117 while (in.fraction.ll < (1LL << (FRACBITS + NGARDS)))
1119 in.fraction.ll <<= 1;
1123 return pack_d (&in);
1127 float_to_si (FLO_type arg_a)
1132 unpack_d ((FLO_union_type *) & arg_a, &a);
1137 /* get reasonable MAX_SI_INT... */
1139 return a.sign ? MAX_SI_INT : (-MAX_SI_INT)-1;
1140 /* it is a number, but a small one */
1141 if (a.normal_exp < 0)
1143 if (a.normal_exp > 30)
1144 return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1145 tmp = a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1146 return a.sign ? (-tmp) : (tmp);
1149 #ifdef US_SOFTWARE_GOFAST
1150 /* While libgcc2.c defines its own __fixunssfsi and __fixunsdfsi routines,
1151 we also define them for GOFAST because the ones in libgcc2.c have the
1152 wrong names and I'd rather define these here and keep GOFAST CYG-LOC's
1153 out of libgcc2.c. We can't define these here if not GOFAST because then
1154 there'd be duplicate copies. */
1157 float_to_usi (FLO_type arg_a)
1161 unpack_d ((FLO_union_type *) & arg_a, &a);
1166 /* get reasonable MAX_USI_INT... */
1168 return a.sign ? MAX_USI_INT : 0;
1169 /* it is a negative number */
1172 /* it is a number, but a small one */
1173 if (a.normal_exp < 0)
1175 if (a.normal_exp > 31)
1177 else if (a.normal_exp > (FRACBITS + NGARDS))
1178 return a.fraction.ll << ((FRACBITS + NGARDS) - a.normal_exp);
1180 return a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1185 negate (FLO_type arg_a)
1189 unpack_d ((FLO_union_type *) & arg_a, &a);
1197 __make_fp(fp_class_type class,
1206 in.normal_exp = exp;
1207 in.fraction.ll = frac;
1208 return pack_d (&in);
1213 /* This enables one to build an fp library that supports float but not double.
1214 Otherwise, we would get an undefined reference to __make_dp.
1215 This is needed for some 8-bit ports that can't handle well values that
1216 are 8-bytes in size, so we just don't support double for them at all. */
1218 extern DFtype __make_dp (fp_class_type, unsigned int, int, UDItype frac);
1221 sf_to_df (SFtype arg_a)
1225 unpack_d ((FLO_union_type *) & arg_a, &in);
1226 return __make_dp (in.class, in.sign, in.normal_exp,
1227 ((UDItype) in.fraction.ll) << F_D_BITOFF);
1235 extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
1238 __make_dp (fp_class_type class, unsigned int sign, int exp, UDItype frac)
1244 in.normal_exp = exp;
1245 in.fraction.ll = frac;
1246 return pack_d (&in);
1250 df_to_sf (DFtype arg_a)
1254 unpack_d ((FLO_union_type *) & arg_a, &in);
1255 return __make_fp (in.class, in.sign, in.normal_exp,
1256 in.fraction.ll >> F_D_BITOFF);