* real.c: Avoid parse error if FLOAT_WORDS_BIG_ENDIAN is
[official-gcc.git] / gcc / config / fp-bit.c
blob4253577acdf3812a538e093c85b4e39483887e43
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
4 Free Software Foundation, Inc.
6 This file is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file with other programs, and to distribute
14 those programs without any restriction coming from the use of this
15 file. (The General Public License restrictions do apply in other
16 respects; for example, they cover modification of the file, and
17 distribution when not linked into another program.)
19 This file is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; see the file COPYING. If not, write to
26 the Free Software Foundation, 59 Temple Place - Suite 330,
27 Boston, MA 02111-1307, USA. */
29 /* As a special exception, if you link this library with other files,
30 some of which are compiled with GCC, to produce an executable,
31 this library does not by itself cause the resulting executable
32 to be covered by the GNU General Public License.
33 This exception does not however invalidate any other reasons why
34 the executable file might be covered by the GNU General Public License. */
36 /* This implements IEEE 754 format arithmetic, but does not provide a
37 mechanism for setting the rounding mode, or for generating or handling
38 exceptions.
40 The original code by Steve Chamberlain, hacked by Mark Eichin and Jim
41 Wilson, all of Cygnus Support. */
43 /* The intended way to use this file is to make two copies, add `#define FLOAT'
44 to one copy, then compile both copies and add them to libgcc.a. */
46 #include "tconfig.h"
47 #include "fp-bit.h"
49 /* The following macros can be defined to change the behaviour of this file:
50 FLOAT: Implement a `float', aka SFmode, fp library. If this is not
51 defined, then this file implements a `double', aka DFmode, fp library.
52 FLOAT_ONLY: Used with FLOAT, to implement a `float' only library, i.e.
53 don't include float->double conversion which requires the double library.
54 This is useful only for machines which can't support doubles, e.g. some
55 8-bit processors.
56 CMPtype: Specify the type that floating point compares should return.
57 This defaults to SItype, aka int.
58 US_SOFTWARE_GOFAST: This makes all entry points use the same names as the
59 US Software goFast library.
60 _DEBUG_BITFLOAT: This makes debugging the code a little easier, by adding
61 two integers to the FLO_union_type.
62 NO_DENORMALS: Disable handling of denormals.
63 NO_NANS: Disable nan and infinity handling
64 SMALL_MACHINE: Useful when operations on QIs and HIs are faster
65 than on an SI */
67 /* We don't currently support extended floats (long doubles) on machines
68 without hardware to deal with them.
70 These stubs are just to keep the linker from complaining about unresolved
71 references which can be pulled in from libio & libstdc++, even if the
72 user isn't using long doubles. However, they may generate an unresolved
73 external to abort if abort is not used by the function, and the stubs
74 are referenced from within libc, since libgcc goes before and after the
75 system library. */
77 #ifdef DECLARE_LIBRARY_RENAMES
78 DECLARE_LIBRARY_RENAMES
79 #endif
81 #ifdef EXTENDED_FLOAT_STUBS
82 __truncxfsf2 (){ abort(); }
83 __extendsfxf2 (){ abort(); }
84 __addxf3 (){ abort(); }
85 __divxf3 (){ abort(); }
86 __eqxf2 (){ abort(); }
87 __extenddfxf2 (){ abort(); }
88 __gtxf2 (){ abort(); }
89 __lexf2 (){ abort(); }
90 __ltxf2 (){ abort(); }
91 __mulxf3 (){ abort(); }
92 __negxf2 (){ abort(); }
93 __nexf2 (){ abort(); }
94 __subxf3 (){ abort(); }
95 __truncxfdf2 (){ abort(); }
97 __trunctfsf2 (){ abort(); }
98 __extendsftf2 (){ abort(); }
99 __addtf3 (){ abort(); }
100 __divtf3 (){ abort(); }
101 __eqtf2 (){ abort(); }
102 __extenddftf2 (){ abort(); }
103 __gttf2 (){ abort(); }
104 __letf2 (){ abort(); }
105 __lttf2 (){ abort(); }
106 __multf3 (){ abort(); }
107 __negtf2 (){ abort(); }
108 __netf2 (){ abort(); }
109 __subtf3 (){ abort(); }
110 __trunctfdf2 (){ abort(); }
111 __gexf2 (){ abort(); }
112 __fixxfsi (){ abort(); }
113 __floatsixf (){ abort(); }
114 #else /* !EXTENDED_FLOAT_STUBS, rest of file */
116 /* IEEE "special" number predicates */
118 #ifdef NO_NANS
120 #define nan() 0
121 #define isnan(x) 0
122 #define isinf(x) 0
123 #else
125 #if defined L_thenan_sf
126 const fp_number_type __thenan_sf = { CLASS_SNAN, 0, 0, {(fractype) 0} };
127 #elif defined L_thenan_df
128 const fp_number_type __thenan_df = { CLASS_SNAN, 0, 0, {(fractype) 0} };
129 #elif defined FLOAT
130 extern const fp_number_type __thenan_sf;
131 #else
132 extern const fp_number_type __thenan_df;
133 #endif
135 INLINE
136 static fp_number_type *
137 nan (void)
139 /* Discard the const qualifier... */
140 #ifdef FLOAT
141 return (fp_number_type *) (& __thenan_sf);
142 #else
143 return (fp_number_type *) (& __thenan_df);
144 #endif
147 INLINE
148 static int
149 isnan ( fp_number_type * x)
151 return x->class == CLASS_SNAN || x->class == CLASS_QNAN;
154 INLINE
155 static int
156 isinf ( fp_number_type * x)
158 return x->class == CLASS_INFINITY;
161 #endif /* NO_NANS */
163 INLINE
164 static int
165 iszero ( fp_number_type * x)
167 return x->class == CLASS_ZERO;
170 INLINE
171 static void
172 flip_sign ( fp_number_type * x)
174 x->sign = !x->sign;
177 extern FLO_type pack_d ( fp_number_type * );
179 #if defined(L_pack_df) || defined(L_pack_sf)
180 FLO_type
181 pack_d ( fp_number_type * src)
183 FLO_union_type dst;
184 fractype fraction = src->fraction.ll; /* wasn't unsigned before? */
185 int sign = src->sign;
186 int exp = 0;
188 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && (isnan (src) || isinf (src)))
190 /* We can't represent these values accurately. By using the
191 largest possible magnitude, we guarantee that the conversion
192 of infinity is at least as big as any finite number. */
193 exp = EXPMAX;
194 fraction = ((fractype) 1 << FRACBITS) - 1;
196 else if (isnan (src))
198 exp = EXPMAX;
199 if (src->class == CLASS_QNAN || 1)
201 fraction |= QUIET_NAN;
204 else if (isinf (src))
206 exp = EXPMAX;
207 fraction = 0;
209 else if (iszero (src))
211 exp = 0;
212 fraction = 0;
214 else if (fraction == 0)
216 exp = 0;
218 else
220 if (src->normal_exp < NORMAL_EXPMIN)
222 #ifdef NO_DENORMALS
223 /* Go straight to a zero representation if denormals are not
224 supported. The denormal handling would be harmless but
225 isn't unnecessary. */
226 exp = 0;
227 fraction = 0;
228 #else /* NO_DENORMALS */
229 /* This number's exponent is too low to fit into the bits
230 available in the number, so we'll store 0 in the exponent and
231 shift the fraction to the right to make up for it. */
233 int shift = NORMAL_EXPMIN - src->normal_exp;
235 exp = 0;
237 if (shift > FRAC_NBITS - NGARDS)
239 /* No point shifting, since it's more that 64 out. */
240 fraction = 0;
242 else
244 int lowbit = (fraction & (((fractype)1 << shift) - 1)) ? 1 : 0;
245 fraction = (fraction >> shift) | lowbit;
247 if ((fraction & GARDMASK) == GARDMSB)
249 if ((fraction & (1 << NGARDS)))
250 fraction += GARDROUND + 1;
252 else
254 /* Add to the guards to round up. */
255 fraction += GARDROUND;
257 /* Perhaps the rounding means we now need to change the
258 exponent, because the fraction is no longer denormal. */
259 if (fraction >= IMPLICIT_1)
261 exp += 1;
263 fraction >>= NGARDS;
264 #endif /* NO_DENORMALS */
266 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS)
267 && src->normal_exp > EXPBIAS)
269 exp = EXPMAX;
270 fraction = 0;
272 else
274 exp = src->normal_exp + EXPBIAS;
275 if (!ROUND_TOWARDS_ZERO)
277 /* IF the gard bits are the all zero, but the first, then we're
278 half way between two numbers, choose the one which makes the
279 lsb of the answer 0. */
280 if ((fraction & GARDMASK) == GARDMSB)
282 if (fraction & (1 << NGARDS))
283 fraction += GARDROUND + 1;
285 else
287 /* Add a one to the guards to round up */
288 fraction += GARDROUND;
290 if (fraction >= IMPLICIT_2)
292 fraction >>= 1;
293 exp += 1;
296 fraction >>= NGARDS;
298 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && exp > EXPMAX)
300 /* Saturate on overflow. */
301 exp = EXPMAX;
302 fraction = ((fractype) 1 << FRACBITS) - 1;
307 /* We previously used bitfields to store the number, but this doesn't
308 handle little/big endian systems conveniently, so use shifts and
309 masks */
310 #ifdef FLOAT_BIT_ORDER_MISMATCH
311 dst.bits.fraction = fraction;
312 dst.bits.exp = exp;
313 dst.bits.sign = sign;
314 #else
315 dst.value_raw = fraction & ((((fractype)1) << FRACBITS) - (fractype)1);
316 dst.value_raw |= ((fractype) (exp & ((1 << EXPBITS) - 1))) << FRACBITS;
317 dst.value_raw |= ((fractype) (sign & 1)) << (FRACBITS | EXPBITS);
318 #endif
320 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
322 halffractype tmp = dst.words[0];
323 dst.words[0] = dst.words[1];
324 dst.words[1] = tmp;
326 #endif
328 return dst.value;
330 #endif
332 #if defined(L_unpack_df) || defined(L_unpack_sf)
333 void
334 unpack_d (FLO_union_type * src, fp_number_type * dst)
336 /* We previously used bitfields to store the number, but this doesn't
337 handle little/big endian systems conveniently, so use shifts and
338 masks */
339 fractype fraction;
340 int exp;
341 int sign;
343 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
344 FLO_union_type swapped;
346 swapped.words[0] = src->words[1];
347 swapped.words[1] = src->words[0];
348 src = &swapped;
349 #endif
351 #ifdef FLOAT_BIT_ORDER_MISMATCH
352 fraction = src->bits.fraction;
353 exp = src->bits.exp;
354 sign = src->bits.sign;
355 #else
356 fraction = src->value_raw & ((((fractype)1) << FRACBITS) - (fractype)1);
357 exp = ((int)(src->value_raw >> FRACBITS)) & ((1 << EXPBITS) - 1);
358 sign = ((int)(src->value_raw >> (FRACBITS + EXPBITS))) & 1;
359 #endif
361 dst->sign = sign;
362 if (exp == 0)
364 /* Hmm. Looks like 0 */
365 if (fraction == 0
366 #ifdef NO_DENORMALS
367 || 1
368 #endif
371 /* tastes like zero */
372 dst->class = CLASS_ZERO;
374 else
376 /* Zero exponent with non zero fraction - it's denormalized,
377 so there isn't a leading implicit one - we'll shift it so
378 it gets one. */
379 dst->normal_exp = exp - EXPBIAS + 1;
380 fraction <<= NGARDS;
382 dst->class = CLASS_NUMBER;
383 #if 1
384 while (fraction < IMPLICIT_1)
386 fraction <<= 1;
387 dst->normal_exp--;
389 #endif
390 dst->fraction.ll = fraction;
393 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && exp == EXPMAX)
395 /* Huge exponent*/
396 if (fraction == 0)
398 /* Attached to a zero fraction - means infinity */
399 dst->class = CLASS_INFINITY;
401 else
403 /* Non zero fraction, means nan */
404 if (fraction & QUIET_NAN)
406 dst->class = CLASS_QNAN;
408 else
410 dst->class = CLASS_SNAN;
412 /* Keep the fraction part as the nan number */
413 dst->fraction.ll = fraction;
416 else
418 /* Nothing strange about this number */
419 dst->normal_exp = exp - EXPBIAS;
420 dst->class = CLASS_NUMBER;
421 dst->fraction.ll = (fraction << NGARDS) | IMPLICIT_1;
424 #endif /* L_unpack_df || L_unpack_sf */
426 #if defined(L_addsub_sf) || defined(L_addsub_df)
427 static fp_number_type *
428 _fpadd_parts (fp_number_type * a,
429 fp_number_type * b,
430 fp_number_type * tmp)
432 intfrac tfraction;
434 /* Put commonly used fields in local variables. */
435 int a_normal_exp;
436 int b_normal_exp;
437 fractype a_fraction;
438 fractype b_fraction;
440 if (isnan (a))
442 return a;
444 if (isnan (b))
446 return b;
448 if (isinf (a))
450 /* Adding infinities with opposite signs yields a NaN. */
451 if (isinf (b) && a->sign != b->sign)
452 return nan ();
453 return a;
455 if (isinf (b))
457 return b;
459 if (iszero (b))
461 if (iszero (a))
463 *tmp = *a;
464 tmp->sign = a->sign & b->sign;
465 return tmp;
467 return a;
469 if (iszero (a))
471 return b;
474 /* Got two numbers. shift the smaller and increment the exponent till
475 they're the same */
477 int diff;
479 a_normal_exp = a->normal_exp;
480 b_normal_exp = b->normal_exp;
481 a_fraction = a->fraction.ll;
482 b_fraction = b->fraction.ll;
484 diff = a_normal_exp - b_normal_exp;
486 if (diff < 0)
487 diff = -diff;
488 if (diff < FRAC_NBITS)
490 /* ??? This does shifts one bit at a time. Optimize. */
491 while (a_normal_exp > b_normal_exp)
493 b_normal_exp++;
494 LSHIFT (b_fraction);
496 while (b_normal_exp > a_normal_exp)
498 a_normal_exp++;
499 LSHIFT (a_fraction);
502 else
504 /* Somethings's up.. choose the biggest */
505 if (a_normal_exp > b_normal_exp)
507 b_normal_exp = a_normal_exp;
508 b_fraction = 0;
510 else
512 a_normal_exp = b_normal_exp;
513 a_fraction = 0;
518 if (a->sign != b->sign)
520 if (a->sign)
522 tfraction = -a_fraction + b_fraction;
524 else
526 tfraction = a_fraction - b_fraction;
528 if (tfraction >= 0)
530 tmp->sign = 0;
531 tmp->normal_exp = a_normal_exp;
532 tmp->fraction.ll = tfraction;
534 else
536 tmp->sign = 1;
537 tmp->normal_exp = a_normal_exp;
538 tmp->fraction.ll = -tfraction;
540 /* and renormalize it */
542 while (tmp->fraction.ll < IMPLICIT_1 && tmp->fraction.ll)
544 tmp->fraction.ll <<= 1;
545 tmp->normal_exp--;
548 else
550 tmp->sign = a->sign;
551 tmp->normal_exp = a_normal_exp;
552 tmp->fraction.ll = a_fraction + b_fraction;
554 tmp->class = CLASS_NUMBER;
555 /* Now the fraction is added, we have to shift down to renormalize the
556 number */
558 if (tmp->fraction.ll >= IMPLICIT_2)
560 LSHIFT (tmp->fraction.ll);
561 tmp->normal_exp++;
563 return tmp;
567 FLO_type
568 add (FLO_type arg_a, FLO_type arg_b)
570 fp_number_type a;
571 fp_number_type b;
572 fp_number_type tmp;
573 fp_number_type *res;
574 FLO_union_type au, bu;
576 au.value = arg_a;
577 bu.value = arg_b;
579 unpack_d (&au, &a);
580 unpack_d (&bu, &b);
582 res = _fpadd_parts (&a, &b, &tmp);
584 return pack_d (res);
587 FLO_type
588 sub (FLO_type arg_a, FLO_type arg_b)
590 fp_number_type a;
591 fp_number_type b;
592 fp_number_type tmp;
593 fp_number_type *res;
594 FLO_union_type au, bu;
596 au.value = arg_a;
597 bu.value = arg_b;
599 unpack_d (&au, &a);
600 unpack_d (&bu, &b);
602 b.sign ^= 1;
604 res = _fpadd_parts (&a, &b, &tmp);
606 return pack_d (res);
608 #endif /* L_addsub_sf || L_addsub_df */
610 #if defined(L_mul_sf) || defined(L_mul_df)
611 static inline __attribute__ ((__always_inline__)) fp_number_type *
612 _fpmul_parts ( fp_number_type * a,
613 fp_number_type * b,
614 fp_number_type * tmp)
616 fractype low = 0;
617 fractype high = 0;
619 if (isnan (a))
621 a->sign = a->sign != b->sign;
622 return a;
624 if (isnan (b))
626 b->sign = a->sign != b->sign;
627 return b;
629 if (isinf (a))
631 if (iszero (b))
632 return nan ();
633 a->sign = a->sign != b->sign;
634 return a;
636 if (isinf (b))
638 if (iszero (a))
640 return nan ();
642 b->sign = a->sign != b->sign;
643 return b;
645 if (iszero (a))
647 a->sign = a->sign != b->sign;
648 return a;
650 if (iszero (b))
652 b->sign = a->sign != b->sign;
653 return b;
656 /* Calculate the mantissa by multiplying both numbers to get a
657 twice-as-wide number. */
659 #if defined(NO_DI_MODE)
661 fractype x = a->fraction.ll;
662 fractype ylow = b->fraction.ll;
663 fractype yhigh = 0;
664 int bit;
666 /* ??? This does multiplies one bit at a time. Optimize. */
667 for (bit = 0; bit < FRAC_NBITS; bit++)
669 int carry;
671 if (x & 1)
673 carry = (low += ylow) < ylow;
674 high += yhigh + carry;
676 yhigh <<= 1;
677 if (ylow & FRACHIGH)
679 yhigh |= 1;
681 ylow <<= 1;
682 x >>= 1;
685 #elif defined(FLOAT)
686 /* Multiplying two USIs to get a UDI, we're safe. */
688 UDItype answer = (UDItype)a->fraction.ll * (UDItype)b->fraction.ll;
690 high = answer >> BITS_PER_SI;
691 low = answer;
693 #else
694 /* fractype is DImode, but we need the result to be twice as wide.
695 Assuming a widening multiply from DImode to TImode is not
696 available, build one by hand. */
698 USItype nl = a->fraction.ll;
699 USItype nh = a->fraction.ll >> BITS_PER_SI;
700 USItype ml = b->fraction.ll;
701 USItype mh = b->fraction.ll >> BITS_PER_SI;
702 UDItype pp_ll = (UDItype) ml * nl;
703 UDItype pp_hl = (UDItype) mh * nl;
704 UDItype pp_lh = (UDItype) ml * nh;
705 UDItype pp_hh = (UDItype) mh * nh;
706 UDItype res2 = 0;
707 UDItype res0 = 0;
708 UDItype ps_hh__ = pp_hl + pp_lh;
709 if (ps_hh__ < pp_hl)
710 res2 += (UDItype)1 << BITS_PER_SI;
711 pp_hl = (UDItype)(USItype)ps_hh__ << BITS_PER_SI;
712 res0 = pp_ll + pp_hl;
713 if (res0 < pp_ll)
714 res2++;
715 res2 += (ps_hh__ >> BITS_PER_SI) + pp_hh;
716 high = res2;
717 low = res0;
719 #endif
722 tmp->normal_exp = a->normal_exp + b->normal_exp;
723 tmp->sign = a->sign != b->sign;
724 #ifdef FLOAT
725 tmp->normal_exp += 2; /* ??????????????? */
726 #else
727 tmp->normal_exp += 4; /* ??????????????? */
728 #endif
729 while (high >= IMPLICIT_2)
731 tmp->normal_exp++;
732 if (high & 1)
734 low >>= 1;
735 low |= FRACHIGH;
737 high >>= 1;
739 while (high < IMPLICIT_1)
741 tmp->normal_exp--;
743 high <<= 1;
744 if (low & FRACHIGH)
745 high |= 1;
746 low <<= 1;
748 /* rounding is tricky. if we only round if it won't make us round later. */
749 #if 0
750 if (low & FRACHIGH2)
752 if (((high & GARDMASK) != GARDMSB)
753 && (((high + 1) & GARDMASK) == GARDMSB))
755 /* don't round, it gets done again later. */
757 else
759 high++;
762 #endif
763 if (!ROUND_TOWARDS_ZERO && (high & GARDMASK) == GARDMSB)
765 if (high & (1 << NGARDS))
767 /* half way, so round to even */
768 high += GARDROUND + 1;
770 else if (low)
772 /* but we really weren't half way */
773 high += GARDROUND + 1;
776 tmp->fraction.ll = high;
777 tmp->class = CLASS_NUMBER;
778 return tmp;
781 FLO_type
782 multiply (FLO_type arg_a, FLO_type arg_b)
784 fp_number_type a;
785 fp_number_type b;
786 fp_number_type tmp;
787 fp_number_type *res;
788 FLO_union_type au, bu;
790 au.value = arg_a;
791 bu.value = arg_b;
793 unpack_d (&au, &a);
794 unpack_d (&bu, &b);
796 res = _fpmul_parts (&a, &b, &tmp);
798 return pack_d (res);
800 #endif /* L_mul_sf || L_mul_df */
802 #if defined(L_div_sf) || defined(L_div_df)
803 static inline __attribute__ ((__always_inline__)) fp_number_type *
804 _fpdiv_parts (fp_number_type * a,
805 fp_number_type * b)
807 fractype bit;
808 fractype numerator;
809 fractype denominator;
810 fractype quotient;
812 if (isnan (a))
814 return a;
816 if (isnan (b))
818 return b;
821 a->sign = a->sign ^ b->sign;
823 if (isinf (a) || iszero (a))
825 if (a->class == b->class)
826 return nan ();
827 return a;
830 if (isinf (b))
832 a->fraction.ll = 0;
833 a->normal_exp = 0;
834 return a;
836 if (iszero (b))
838 a->class = CLASS_INFINITY;
839 return a;
842 /* Calculate the mantissa by multiplying both 64bit numbers to get a
843 128 bit number */
845 /* quotient =
846 ( numerator / denominator) * 2^(numerator exponent - denominator exponent)
849 a->normal_exp = a->normal_exp - b->normal_exp;
850 numerator = a->fraction.ll;
851 denominator = b->fraction.ll;
853 if (numerator < denominator)
855 /* Fraction will be less than 1.0 */
856 numerator *= 2;
857 a->normal_exp--;
859 bit = IMPLICIT_1;
860 quotient = 0;
861 /* ??? Does divide one bit at a time. Optimize. */
862 while (bit)
864 if (numerator >= denominator)
866 quotient |= bit;
867 numerator -= denominator;
869 bit >>= 1;
870 numerator *= 2;
873 if (!ROUND_TOWARDS_ZERO && (quotient & GARDMASK) == GARDMSB)
875 if (quotient & (1 << NGARDS))
877 /* half way, so round to even */
878 quotient += GARDROUND + 1;
880 else if (numerator)
882 /* but we really weren't half way, more bits exist */
883 quotient += GARDROUND + 1;
887 a->fraction.ll = quotient;
888 return (a);
892 FLO_type
893 divide (FLO_type arg_a, FLO_type arg_b)
895 fp_number_type a;
896 fp_number_type b;
897 fp_number_type *res;
898 FLO_union_type au, bu;
900 au.value = arg_a;
901 bu.value = arg_b;
903 unpack_d (&au, &a);
904 unpack_d (&bu, &b);
906 res = _fpdiv_parts (&a, &b);
908 return pack_d (res);
910 #endif /* L_div_sf || L_div_df */
912 #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df)
913 /* according to the demo, fpcmp returns a comparison with 0... thus
914 a<b -> -1
915 a==b -> 0
916 a>b -> +1
920 __fpcmp_parts (fp_number_type * a, fp_number_type * b)
922 #if 0
923 /* either nan -> unordered. Must be checked outside of this routine. */
924 if (isnan (a) && isnan (b))
926 return 1; /* still unordered! */
928 #endif
930 if (isnan (a) || isnan (b))
932 return 1; /* how to indicate unordered compare? */
934 if (isinf (a) && isinf (b))
936 /* +inf > -inf, but +inf != +inf */
937 /* b \a| +inf(0)| -inf(1)
938 ______\+--------+--------
939 +inf(0)| a==b(0)| a<b(-1)
940 -------+--------+--------
941 -inf(1)| a>b(1) | a==b(0)
942 -------+--------+--------
943 So since unordered must be non zero, just line up the columns...
945 return b->sign - a->sign;
947 /* but not both... */
948 if (isinf (a))
950 return a->sign ? -1 : 1;
952 if (isinf (b))
954 return b->sign ? 1 : -1;
956 if (iszero (a) && iszero (b))
958 return 0;
960 if (iszero (a))
962 return b->sign ? 1 : -1;
964 if (iszero (b))
966 return a->sign ? -1 : 1;
968 /* now both are "normal". */
969 if (a->sign != b->sign)
971 /* opposite signs */
972 return a->sign ? -1 : 1;
974 /* same sign; exponents? */
975 if (a->normal_exp > b->normal_exp)
977 return a->sign ? -1 : 1;
979 if (a->normal_exp < b->normal_exp)
981 return a->sign ? 1 : -1;
983 /* same exponents; check size. */
984 if (a->fraction.ll > b->fraction.ll)
986 return a->sign ? -1 : 1;
988 if (a->fraction.ll < b->fraction.ll)
990 return a->sign ? 1 : -1;
992 /* after all that, they're equal. */
993 return 0;
995 #endif
997 #if defined(L_compare_sf) || defined(L_compare_df)
998 CMPtype
999 compare (FLO_type arg_a, FLO_type arg_b)
1001 fp_number_type a;
1002 fp_number_type b;
1003 FLO_union_type au, bu;
1005 au.value = arg_a;
1006 bu.value = arg_b;
1008 unpack_d (&au, &a);
1009 unpack_d (&bu, &b);
1011 return __fpcmp_parts (&a, &b);
1013 #endif /* L_compare_sf || L_compare_df */
1015 #ifndef US_SOFTWARE_GOFAST
1017 /* These should be optimized for their specific tasks someday. */
1019 #if defined(L_eq_sf) || defined(L_eq_df)
1020 CMPtype
1021 _eq_f2 (FLO_type arg_a, FLO_type arg_b)
1023 fp_number_type a;
1024 fp_number_type b;
1025 FLO_union_type au, bu;
1027 au.value = arg_a;
1028 bu.value = arg_b;
1030 unpack_d (&au, &a);
1031 unpack_d (&bu, &b);
1033 if (isnan (&a) || isnan (&b))
1034 return 1; /* false, truth == 0 */
1036 return __fpcmp_parts (&a, &b) ;
1038 #endif /* L_eq_sf || L_eq_df */
1040 #if defined(L_ne_sf) || defined(L_ne_df)
1041 CMPtype
1042 _ne_f2 (FLO_type arg_a, FLO_type arg_b)
1044 fp_number_type a;
1045 fp_number_type b;
1046 FLO_union_type au, bu;
1048 au.value = arg_a;
1049 bu.value = arg_b;
1051 unpack_d (&au, &a);
1052 unpack_d (&bu, &b);
1054 if (isnan (&a) || isnan (&b))
1055 return 1; /* true, truth != 0 */
1057 return __fpcmp_parts (&a, &b) ;
1059 #endif /* L_ne_sf || L_ne_df */
1061 #if defined(L_gt_sf) || defined(L_gt_df)
1062 CMPtype
1063 _gt_f2 (FLO_type arg_a, FLO_type arg_b)
1065 fp_number_type a;
1066 fp_number_type b;
1067 FLO_union_type au, bu;
1069 au.value = arg_a;
1070 bu.value = arg_b;
1072 unpack_d (&au, &a);
1073 unpack_d (&bu, &b);
1075 if (isnan (&a) || isnan (&b))
1076 return -1; /* false, truth > 0 */
1078 return __fpcmp_parts (&a, &b);
1080 #endif /* L_gt_sf || L_gt_df */
1082 #if defined(L_ge_sf) || defined(L_ge_df)
1083 CMPtype
1084 _ge_f2 (FLO_type arg_a, FLO_type arg_b)
1086 fp_number_type a;
1087 fp_number_type b;
1088 FLO_union_type au, bu;
1090 au.value = arg_a;
1091 bu.value = arg_b;
1093 unpack_d (&au, &a);
1094 unpack_d (&bu, &b);
1096 if (isnan (&a) || isnan (&b))
1097 return -1; /* false, truth >= 0 */
1098 return __fpcmp_parts (&a, &b) ;
1100 #endif /* L_ge_sf || L_ge_df */
1102 #if defined(L_lt_sf) || defined(L_lt_df)
1103 CMPtype
1104 _lt_f2 (FLO_type arg_a, FLO_type arg_b)
1106 fp_number_type a;
1107 fp_number_type b;
1108 FLO_union_type au, bu;
1110 au.value = arg_a;
1111 bu.value = arg_b;
1113 unpack_d (&au, &a);
1114 unpack_d (&bu, &b);
1116 if (isnan (&a) || isnan (&b))
1117 return 1; /* false, truth < 0 */
1119 return __fpcmp_parts (&a, &b);
1121 #endif /* L_lt_sf || L_lt_df */
1123 #if defined(L_le_sf) || defined(L_le_df)
1124 CMPtype
1125 _le_f2 (FLO_type arg_a, FLO_type arg_b)
1127 fp_number_type a;
1128 fp_number_type b;
1129 FLO_union_type au, bu;
1131 au.value = arg_a;
1132 bu.value = arg_b;
1134 unpack_d (&au, &a);
1135 unpack_d (&bu, &b);
1137 if (isnan (&a) || isnan (&b))
1138 return 1; /* false, truth <= 0 */
1140 return __fpcmp_parts (&a, &b) ;
1142 #endif /* L_le_sf || L_le_df */
1144 #endif /* ! US_SOFTWARE_GOFAST */
1146 #if defined(L_unord_sf) || defined(L_unord_df)
1147 CMPtype
1148 _unord_f2 (FLO_type arg_a, FLO_type arg_b)
1150 fp_number_type a;
1151 fp_number_type b;
1152 FLO_union_type au, bu;
1154 au.value = arg_a;
1155 bu.value = arg_b;
1157 unpack_d (&au, &a);
1158 unpack_d (&bu, &b);
1160 return (isnan (&a) || isnan (&b));
1162 #endif /* L_unord_sf || L_unord_df */
1164 #if defined(L_si_to_sf) || defined(L_si_to_df)
1165 FLO_type
1166 si_to_float (SItype arg_a)
1168 fp_number_type in;
1170 in.class = CLASS_NUMBER;
1171 in.sign = arg_a < 0;
1172 if (!arg_a)
1174 in.class = CLASS_ZERO;
1176 else
1178 in.normal_exp = FRACBITS + NGARDS;
1179 if (in.sign)
1181 /* Special case for minint, since there is no +ve integer
1182 representation for it */
1183 if (arg_a == (- MAX_SI_INT - 1))
1185 return (FLO_type)(- MAX_SI_INT - 1);
1187 in.fraction.ll = (-arg_a);
1189 else
1190 in.fraction.ll = arg_a;
1192 while (in.fraction.ll < (1LL << (FRACBITS + NGARDS)))
1194 in.fraction.ll <<= 1;
1195 in.normal_exp -= 1;
1198 return pack_d (&in);
1200 #endif /* L_si_to_sf || L_si_to_df */
1202 #if defined(L_usi_to_sf) || defined(L_usi_to_df)
1203 FLO_type
1204 usi_to_float (USItype arg_a)
1206 fp_number_type in;
1208 in.sign = 0;
1209 if (!arg_a)
1211 in.class = CLASS_ZERO;
1213 else
1215 in.class = CLASS_NUMBER;
1216 in.normal_exp = FRACBITS + NGARDS;
1217 in.fraction.ll = arg_a;
1219 while (in.fraction.ll > (1LL << (FRACBITS + NGARDS)))
1221 in.fraction.ll >>= 1;
1222 in.normal_exp += 1;
1224 while (in.fraction.ll < (1LL << (FRACBITS + NGARDS)))
1226 in.fraction.ll <<= 1;
1227 in.normal_exp -= 1;
1230 return pack_d (&in);
1232 #endif
1234 #if defined(L_sf_to_si) || defined(L_df_to_si)
1235 SItype
1236 float_to_si (FLO_type arg_a)
1238 fp_number_type a;
1239 SItype tmp;
1240 FLO_union_type au;
1242 au.value = arg_a;
1243 unpack_d (&au, &a);
1245 if (iszero (&a))
1246 return 0;
1247 if (isnan (&a))
1248 return 0;
1249 /* get reasonable MAX_SI_INT... */
1250 if (isinf (&a))
1251 return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1252 /* it is a number, but a small one */
1253 if (a.normal_exp < 0)
1254 return 0;
1255 if (a.normal_exp > BITS_PER_SI - 2)
1256 return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1257 tmp = a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1258 return a.sign ? (-tmp) : (tmp);
1260 #endif /* L_sf_to_si || L_df_to_si */
1262 #if defined(L_sf_to_usi) || defined(L_df_to_usi)
1263 #ifdef US_SOFTWARE_GOFAST
1264 /* While libgcc2.c defines its own __fixunssfsi and __fixunsdfsi routines,
1265 we also define them for GOFAST because the ones in libgcc2.c have the
1266 wrong names and I'd rather define these here and keep GOFAST CYG-LOC's
1267 out of libgcc2.c. We can't define these here if not GOFAST because then
1268 there'd be duplicate copies. */
1270 USItype
1271 float_to_usi (FLO_type arg_a)
1273 fp_number_type a;
1274 FLO_union_type au;
1276 au.value = arg_a;
1277 unpack_d (&au, &a);
1279 if (iszero (&a))
1280 return 0;
1281 if (isnan (&a))
1282 return 0;
1283 /* it is a negative number */
1284 if (a.sign)
1285 return 0;
1286 /* get reasonable MAX_USI_INT... */
1287 if (isinf (&a))
1288 return MAX_USI_INT;
1289 /* it is a number, but a small one */
1290 if (a.normal_exp < 0)
1291 return 0;
1292 if (a.normal_exp > BITS_PER_SI - 1)
1293 return MAX_USI_INT;
1294 else if (a.normal_exp > (FRACBITS + NGARDS))
1295 return a.fraction.ll << (a.normal_exp - (FRACBITS + NGARDS));
1296 else
1297 return a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1299 #endif /* US_SOFTWARE_GOFAST */
1300 #endif /* L_sf_to_usi || L_df_to_usi */
1302 #if defined(L_negate_sf) || defined(L_negate_df)
1303 FLO_type
1304 negate (FLO_type arg_a)
1306 fp_number_type a;
1307 FLO_union_type au;
1309 au.value = arg_a;
1310 unpack_d (&au, &a);
1312 flip_sign (&a);
1313 return pack_d (&a);
1315 #endif /* L_negate_sf || L_negate_df */
1317 #ifdef FLOAT
1319 #if defined(L_make_sf)
1320 SFtype
1321 __make_fp(fp_class_type class,
1322 unsigned int sign,
1323 int exp,
1324 USItype frac)
1326 fp_number_type in;
1328 in.class = class;
1329 in.sign = sign;
1330 in.normal_exp = exp;
1331 in.fraction.ll = frac;
1332 return pack_d (&in);
1334 #endif /* L_make_sf */
1336 #ifndef FLOAT_ONLY
1338 /* This enables one to build an fp library that supports float but not double.
1339 Otherwise, we would get an undefined reference to __make_dp.
1340 This is needed for some 8-bit ports that can't handle well values that
1341 are 8-bytes in size, so we just don't support double for them at all. */
1343 #if defined(L_sf_to_df)
1344 DFtype
1345 sf_to_df (SFtype arg_a)
1347 fp_number_type in;
1348 FLO_union_type au;
1350 au.value = arg_a;
1351 unpack_d (&au, &in);
1353 return __make_dp (in.class, in.sign, in.normal_exp,
1354 ((UDItype) in.fraction.ll) << F_D_BITOFF);
1356 #endif /* L_sf_to_df */
1358 #endif /* ! FLOAT_ONLY */
1359 #endif /* FLOAT */
1361 #ifndef FLOAT
1363 extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
1365 #if defined(L_make_df)
1366 DFtype
1367 __make_dp (fp_class_type class, unsigned int sign, int exp, UDItype frac)
1369 fp_number_type in;
1371 in.class = class;
1372 in.sign = sign;
1373 in.normal_exp = exp;
1374 in.fraction.ll = frac;
1375 return pack_d (&in);
1377 #endif /* L_make_df */
1379 #if defined(L_df_to_sf)
1380 SFtype
1381 df_to_sf (DFtype arg_a)
1383 fp_number_type in;
1384 USItype sffrac;
1385 FLO_union_type au;
1387 au.value = arg_a;
1388 unpack_d (&au, &in);
1390 sffrac = in.fraction.ll >> F_D_BITOFF;
1392 /* We set the lowest guard bit in SFFRAC if we discarded any non
1393 zero bits. */
1394 if ((in.fraction.ll & (((USItype) 1 << F_D_BITOFF) - 1)) != 0)
1395 sffrac |= 1;
1397 return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
1399 #endif /* L_df_to_sf */
1401 #endif /* ! FLOAT */
1402 #endif /* !EXTENDED_FLOAT_STUBS */