OSDN Git Service

* fp-bit.h: New file.
[pf3gnuchains/gcc-fork.git] / gcc / config / fp-bit.c
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
3    floating point. 
4    Copyright (C) 1994, 1995, 1996, 1997, 1998,
5    2000 Free Software Foundation, Inc.
6
7 This file is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file with other programs, and to distribute
15 those programs without any restriction coming from the use of this
16 file.  (The General Public License restrictions do apply in other
17 respects; for example, they cover modification of the file, and
18 distribution when not linked into another program.)
19
20 This file is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23 General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; see the file COPYING.  If not, write to
27 the Free Software Foundation, 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA.  */
29
30 /* As a special exception, if you link this library with other files,
31    some of which are compiled with GCC, to produce an executable,
32    this library does not by itself cause the resulting executable
33    to be covered by the GNU General Public License.
34    This exception does not however invalidate any other reasons why
35    the executable file might be covered by the GNU General Public License.  */
36
37 /* This implements IEEE 754 format arithmetic, but does not provide a
38    mechanism for setting the rounding mode, or for generating or handling
39    exceptions.
40
41    The original code by Steve Chamberlain, hacked by Mark Eichin and Jim
42    Wilson, all of Cygnus Support.  */
43
44 /* The intended way to use this file is to make two copies, add `#define FLOAT'
45    to one copy, then compile both copies and add them to libgcc.a.  */
46
47 #include "fp-bit.h"
48
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.  If this is not defined, the entry points use
60      the same names as libgcc1.c.
61    _DEBUG_BITFLOAT: This makes debugging the code a little easier, by adding
62      two integers to the FLO_union_type.
63    NO_DENORMALS: Disable handling of denormals.
64    NO_NANS: Disable nan and infinity handling
65    SMALL_MACHINE: Useful when operations on QIs and HIs are faster
66      than on an SI */
67
68 /* We don't currently support extended floats (long doubles) on machines
69    without hardware to deal with them.
70
71    These stubs are just to keep the linker from complaining about unresolved
72    references which can be pulled in from libio & libstdc++, even if the
73    user isn't using long doubles.  However, they may generate an unresolved
74    external to abort if abort is not used by the function, and the stubs
75    are referenced from within libc, since libgcc goes before and after the
76    system library.  */
77
78 #ifdef EXTENDED_FLOAT_STUBS
79 __truncxfsf2 (){ abort(); }
80 __extendsfxf2 (){ abort(); }
81 __addxf3 (){ abort(); }
82 __divxf3 (){ abort(); }
83 __eqxf2 (){ abort(); }
84 __extenddfxf2 (){ abort(); }
85 __gtxf2 (){ abort(); }
86 __lexf2 (){ abort(); }
87 __ltxf2 (){ abort(); }
88 __mulxf3 (){ abort(); }
89 __negxf2 (){ abort(); }
90 __nexf2 (){ abort(); }
91 __subxf3 (){ abort(); }
92 __truncxfdf2 (){ abort(); }
93
94 __trunctfsf2 (){ abort(); }
95 __extendsftf2 (){ abort(); }
96 __addtf3 (){ abort(); }
97 __divtf3 (){ abort(); }
98 __eqtf2 (){ abort(); }
99 __extenddftf2 (){ abort(); }
100 __gttf2 (){ abort(); }
101 __letf2 (){ abort(); }
102 __lttf2 (){ abort(); }
103 __multf3 (){ abort(); }
104 __negtf2 (){ abort(); }
105 __netf2 (){ abort(); }
106 __subtf3 (){ abort(); }
107 __trunctfdf2 (){ abort(); }
108 __gexf2 (){ abort(); }
109 __fixxfsi (){ abort(); }
110 __floatsixf (){ abort(); }
111 #else   /* !EXTENDED_FLOAT_STUBS, rest of file */
112
113 /* IEEE "special" number predicates */
114
115 #ifdef NO_NANS
116
117 #define nan() 0
118 #define isnan(x) 0
119 #define isinf(x) 0
120 #else
121
122 #if   defined L_thenan_sf
123 const fp_number_type __thenan_sf = { CLASS_SNAN, 0, 0, {(fractype) 0} };
124 #elif defined L_thenan_df
125 const fp_number_type __thenan_df = { CLASS_SNAN, 0, 0, {(fractype) 0} };
126 #elif defined FLOAT
127 extern const fp_number_type __thenan_sf;
128 #else
129 extern const fp_number_type __thenan_df;
130 #endif
131
132 INLINE
133 static fp_number_type *
134 nan (void)
135 {
136   /* Discard the const qualifier... */
137 #ifdef FLOAT  
138   return (fp_number_type *) (& __thenan_sf);
139 #else
140   return (fp_number_type *) (& __thenan_df);
141 #endif
142 }
143
144 INLINE
145 static int
146 isnan ( fp_number_type *  x)
147 {
148   return x->class == CLASS_SNAN || x->class == CLASS_QNAN;
149 }
150
151 INLINE
152 static int
153 isinf ( fp_number_type *  x)
154 {
155   return x->class == CLASS_INFINITY;
156 }
157
158 #endif /* NO_NANS */
159
160 INLINE
161 static int
162 iszero ( fp_number_type *  x)
163 {
164   return x->class == CLASS_ZERO;
165 }
166
167 INLINE 
168 static void
169 flip_sign ( fp_number_type *  x)
170 {
171   x->sign = !x->sign;
172 }
173
174 extern FLO_type pack_d ( fp_number_type * );
175
176 #if defined(L_pack_df) || defined(L_pack_sf)
177 FLO_type
178 pack_d ( fp_number_type *  src)
179 {
180   FLO_union_type dst;
181   fractype fraction = src->fraction.ll; /* wasn't unsigned before? */
182   int sign = src->sign;
183   int exp = 0;
184
185   if (isnan (src))
186     {
187       exp = EXPMAX;
188       if (src->class == CLASS_QNAN || 1)
189         {
190           fraction |= QUIET_NAN;
191         }
192     }
193   else if (isinf (src))
194     {
195       exp = EXPMAX;
196       fraction = 0;
197     }
198   else if (iszero (src))
199     {
200       exp = 0;
201       fraction = 0;
202     }
203   else if (fraction == 0)
204     {
205       exp = 0;
206     }
207   else
208     {
209       if (src->normal_exp < NORMAL_EXPMIN)
210         {
211           /* This number's exponent is too low to fit into the bits
212              available in the number, so we'll store 0 in the exponent and
213              shift the fraction to the right to make up for it.  */
214
215           int shift = NORMAL_EXPMIN - src->normal_exp;
216
217           exp = 0;
218
219           if (shift > FRAC_NBITS - NGARDS)
220             {
221               /* No point shifting, since it's more that 64 out.  */
222               fraction = 0;
223             }
224           else
225             {
226               int lowbit = (fraction & ((1 << shift) - 1)) ? 1 : 0;
227               fraction = (fraction >> shift) | lowbit;
228             }
229           if ((fraction & GARDMASK) == GARDMSB)
230             {
231               if ((fraction & (1 << NGARDS)))
232                 fraction += GARDROUND + 1;
233             }
234           else
235             {
236               /* Add to the guards to round up.  */
237               fraction += GARDROUND;
238             }
239           /* Perhaps the rounding means we now need to change the
240              exponent, because the fraction is no longer denormal.  */
241           if (fraction >= IMPLICIT_1)
242             {
243               exp += 1;
244             }
245           fraction >>= NGARDS;
246         }
247       else if (src->normal_exp > EXPBIAS)
248         {
249           exp = EXPMAX;
250           fraction = 0;
251         }
252       else
253         {
254           exp = src->normal_exp + EXPBIAS;
255           /* IF the gard bits are the all zero, but the first, then we're
256              half way between two numbers, choose the one which makes the
257              lsb of the answer 0.  */
258           if ((fraction & GARDMASK) == GARDMSB)
259             {
260               if (fraction & (1 << NGARDS))
261                 fraction += GARDROUND + 1;
262             }
263           else
264             {
265               /* Add a one to the guards to round up */
266               fraction += GARDROUND;
267             }
268           if (fraction >= IMPLICIT_2)
269             {
270               fraction >>= 1;
271               exp += 1;
272             }
273           fraction >>= NGARDS;
274         }
275     }
276
277   /* We previously used bitfields to store the number, but this doesn't
278      handle little/big endian systems conveniently, so use shifts and
279      masks */
280 #ifdef FLOAT_BIT_ORDER_MISMATCH
281   dst.bits.fraction = fraction;
282   dst.bits.exp = exp;
283   dst.bits.sign = sign;
284 #else
285   dst.value_raw = fraction & ((((fractype)1) << FRACBITS) - (fractype)1);
286   dst.value_raw |= ((fractype) (exp & ((1 << EXPBITS) - 1))) << FRACBITS;
287   dst.value_raw |= ((fractype) (sign & 1)) << (FRACBITS | EXPBITS);
288 #endif
289
290 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
291   {
292     halffractype tmp = dst.words[0];
293     dst.words[0] = dst.words[1];
294     dst.words[1] = tmp;
295   }
296 #endif
297
298   return dst.value;
299 }
300 #endif
301
302 #if defined(L_unpack_df) || defined(L_unpack_sf)
303 void
304 unpack_d (FLO_union_type * src, fp_number_type * dst)
305 {
306   /* We previously used bitfields to store the number, but this doesn't
307      handle little/big endian systems conveniently, so use shifts and
308      masks */
309   fractype fraction;
310   int exp;
311   int sign;
312
313 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
314   FLO_union_type swapped;
315
316   swapped.words[0] = src->words[1];
317   swapped.words[1] = src->words[0];
318   src = &swapped;
319 #endif
320   
321 #ifdef FLOAT_BIT_ORDER_MISMATCH
322   fraction = src->bits.fraction;
323   exp = src->bits.exp;
324   sign = src->bits.sign;
325 #else
326   fraction = src->value_raw & ((((fractype)1) << FRACBITS) - (fractype)1);
327   exp = ((int)(src->value_raw >> FRACBITS)) & ((1 << EXPBITS) - 1);
328   sign = ((int)(src->value_raw >> (FRACBITS + EXPBITS))) & 1;
329 #endif
330
331   dst->sign = sign;
332   if (exp == 0)
333     {
334       /* Hmm.  Looks like 0 */
335       if (fraction == 0
336 #ifdef NO_DENORMALS
337           || 1
338 #endif
339           )
340         {
341           /* tastes like zero */
342           dst->class = CLASS_ZERO;
343         }
344       else
345         {
346           /* Zero exponent with non zero fraction - it's denormalized,
347              so there isn't a leading implicit one - we'll shift it so
348              it gets one.  */
349           dst->normal_exp = exp - EXPBIAS + 1;
350           fraction <<= NGARDS;
351
352           dst->class = CLASS_NUMBER;
353 #if 1
354           while (fraction < IMPLICIT_1)
355             {
356               fraction <<= 1;
357               dst->normal_exp--;
358             }
359 #endif
360           dst->fraction.ll = fraction;
361         }
362     }
363   else if (exp == EXPMAX)
364     {
365       /* Huge exponent*/
366       if (fraction == 0)
367         {
368           /* Attached to a zero fraction - means infinity */
369           dst->class = CLASS_INFINITY;
370         }
371       else
372         {
373           /* Non zero fraction, means nan */
374           if (fraction & QUIET_NAN)
375             {
376               dst->class = CLASS_QNAN;
377             }
378           else
379             {
380               dst->class = CLASS_SNAN;
381             }
382           /* Keep the fraction part as the nan number */
383           dst->fraction.ll = fraction;
384         }
385     }
386   else
387     {
388       /* Nothing strange about this number */
389       dst->normal_exp = exp - EXPBIAS;
390       dst->class = CLASS_NUMBER;
391       dst->fraction.ll = (fraction << NGARDS) | IMPLICIT_1;
392     }
393 }
394 #endif /* L_unpack_df || L_unpack_sf */
395
396 #if defined(L_addsub_sf) || defined(L_addsub_df)
397 static fp_number_type *
398 _fpadd_parts (fp_number_type * a,
399               fp_number_type * b,
400               fp_number_type * tmp)
401 {
402   intfrac tfraction;
403
404   /* Put commonly used fields in local variables.  */
405   int a_normal_exp;
406   int b_normal_exp;
407   fractype a_fraction;
408   fractype b_fraction;
409
410   if (isnan (a))
411     {
412       return a;
413     }
414   if (isnan (b))
415     {
416       return b;
417     }
418   if (isinf (a))
419     {
420       /* Adding infinities with opposite signs yields a NaN.  */
421       if (isinf (b) && a->sign != b->sign)
422         return nan ();
423       return a;
424     }
425   if (isinf (b))
426     {
427       return b;
428     }
429   if (iszero (b))
430     {
431       if (iszero (a))
432         {
433           *tmp = *a;
434           tmp->sign = a->sign & b->sign;
435           return tmp;
436         }
437       return a;
438     }
439   if (iszero (a))
440     {
441       return b;
442     }
443
444   /* Got two numbers. shift the smaller and increment the exponent till
445      they're the same */
446   {
447     int diff;
448
449     a_normal_exp = a->normal_exp;
450     b_normal_exp = b->normal_exp;
451     a_fraction = a->fraction.ll;
452     b_fraction = b->fraction.ll;
453
454     diff = a_normal_exp - b_normal_exp;
455
456     if (diff < 0)
457       diff = -diff;
458     if (diff < FRAC_NBITS)
459       {
460         /* ??? This does shifts one bit at a time.  Optimize.  */
461         while (a_normal_exp > b_normal_exp)
462           {
463             b_normal_exp++;
464             LSHIFT (b_fraction);
465           }
466         while (b_normal_exp > a_normal_exp)
467           {
468             a_normal_exp++;
469             LSHIFT (a_fraction);
470           }
471       }
472     else
473       {
474         /* Somethings's up.. choose the biggest */
475         if (a_normal_exp > b_normal_exp)
476           {
477             b_normal_exp = a_normal_exp;
478             b_fraction = 0;
479           }
480         else
481           {
482             a_normal_exp = b_normal_exp;
483             a_fraction = 0;
484           }
485       }
486   }
487
488   if (a->sign != b->sign)
489     {
490       if (a->sign)
491         {
492           tfraction = -a_fraction + b_fraction;
493         }
494       else
495         {
496           tfraction = a_fraction - b_fraction;
497         }
498       if (tfraction >= 0)
499         {
500           tmp->sign = 0;
501           tmp->normal_exp = a_normal_exp;
502           tmp->fraction.ll = tfraction;
503         }
504       else
505         {
506           tmp->sign = 1;
507           tmp->normal_exp = a_normal_exp;
508           tmp->fraction.ll = -tfraction;
509         }
510       /* and renormalize it */
511
512       while (tmp->fraction.ll < IMPLICIT_1 && tmp->fraction.ll)
513         {
514           tmp->fraction.ll <<= 1;
515           tmp->normal_exp--;
516         }
517     }
518   else
519     {
520       tmp->sign = a->sign;
521       tmp->normal_exp = a_normal_exp;
522       tmp->fraction.ll = a_fraction + b_fraction;
523     }
524   tmp->class = CLASS_NUMBER;
525   /* Now the fraction is added, we have to shift down to renormalize the
526      number */
527
528   if (tmp->fraction.ll >= IMPLICIT_2)
529     {
530       LSHIFT (tmp->fraction.ll);
531       tmp->normal_exp++;
532     }
533   return tmp;
534
535 }
536
537 FLO_type
538 add (FLO_type arg_a, FLO_type arg_b)
539 {
540   fp_number_type a;
541   fp_number_type b;
542   fp_number_type tmp;
543   fp_number_type *res;
544   FLO_union_type au, bu;
545
546   au.value = arg_a;
547   bu.value = arg_b;
548
549   unpack_d (&au, &a);
550   unpack_d (&bu, &b);
551
552   res = _fpadd_parts (&a, &b, &tmp);
553
554   return pack_d (res);
555 }
556
557 FLO_type
558 sub (FLO_type arg_a, FLO_type arg_b)
559 {
560   fp_number_type a;
561   fp_number_type b;
562   fp_number_type tmp;
563   fp_number_type *res;
564   FLO_union_type au, bu;
565
566   au.value = arg_a;
567   bu.value = arg_b;
568
569   unpack_d (&au, &a);
570   unpack_d (&bu, &b);
571
572   b.sign ^= 1;
573
574   res = _fpadd_parts (&a, &b, &tmp);
575
576   return pack_d (res);
577 }
578 #endif /* L_addsub_sf || L_addsub_df */
579
580 #if defined(L_mul_sf) || defined(L_mul_df)
581 static INLINE fp_number_type *
582 _fpmul_parts ( fp_number_type *  a,
583                fp_number_type *  b,
584                fp_number_type * tmp)
585 {
586   fractype low = 0;
587   fractype high = 0;
588
589   if (isnan (a))
590     {
591       a->sign = a->sign != b->sign;
592       return a;
593     }
594   if (isnan (b))
595     {
596       b->sign = a->sign != b->sign;
597       return b;
598     }
599   if (isinf (a))
600     {
601       if (iszero (b))
602         return nan ();
603       a->sign = a->sign != b->sign;
604       return a;
605     }
606   if (isinf (b))
607     {
608       if (iszero (a))
609         {
610           return nan ();
611         }
612       b->sign = a->sign != b->sign;
613       return b;
614     }
615   if (iszero (a))
616     {
617       a->sign = a->sign != b->sign;
618       return a;
619     }
620   if (iszero (b))
621     {
622       b->sign = a->sign != b->sign;
623       return b;
624     }
625
626   /* Calculate the mantissa by multiplying both 64bit numbers to get a
627      128 bit number */
628   {
629 #if defined(NO_DI_MODE)
630     {
631       fractype x = a->fraction.ll;
632       fractype ylow = b->fraction.ll;
633       fractype yhigh = 0;
634       int bit;
635
636       /* ??? This does multiplies one bit at a time.  Optimize.  */
637       for (bit = 0; bit < FRAC_NBITS; bit++)
638         {
639           int carry;
640
641           if (x & 1)
642             {
643               carry = (low += ylow) < ylow;
644               high += yhigh + carry;
645             }
646           yhigh <<= 1;
647           if (ylow & FRACHIGH)
648             {
649               yhigh |= 1;
650             }
651           ylow <<= 1;
652           x >>= 1;
653         }
654     }
655 #elif defined(FLOAT) 
656     {
657       /* Multiplying two 32 bit numbers to get a 64 bit number  on 
658         a machine with DI, so we're safe */
659
660       DItype answer = (DItype)(a->fraction.ll) * (DItype)(b->fraction.ll);
661       
662       high = answer >> 32;
663       low = answer;
664     }
665 #else
666     /* Doing a 64*64 to 128 */
667     {
668       UDItype nl = a->fraction.ll & 0xffffffff;
669       UDItype nh = a->fraction.ll >> 32;
670       UDItype ml = b->fraction.ll & 0xffffffff;
671       UDItype mh = b->fraction.ll >>32;
672       UDItype pp_ll = ml * nl;
673       UDItype pp_hl = mh * nl;
674       UDItype pp_lh = ml * nh;
675       UDItype pp_hh = mh * nh;
676       UDItype res2 = 0;
677       UDItype res0 = 0;
678       UDItype ps_hh__ = pp_hl + pp_lh;
679       if (ps_hh__ < pp_hl)
680         res2 += 0x100000000LL;
681       pp_hl = (ps_hh__ << 32) & 0xffffffff00000000LL;
682       res0 = pp_ll + pp_hl;
683       if (res0 < pp_ll)
684         res2++;
685       res2 += ((ps_hh__ >> 32) & 0xffffffffL) + pp_hh;
686       high = res2;
687       low = res0;
688     }
689 #endif
690   }
691
692   tmp->normal_exp = a->normal_exp + b->normal_exp;
693   tmp->sign = a->sign != b->sign;
694 #ifdef FLOAT
695   tmp->normal_exp += 2;         /* ??????????????? */
696 #else
697   tmp->normal_exp += 4;         /* ??????????????? */
698 #endif
699   while (high >= IMPLICIT_2)
700     {
701       tmp->normal_exp++;
702       if (high & 1)
703         {
704           low >>= 1;
705           low |= FRACHIGH;
706         }
707       high >>= 1;
708     }
709   while (high < IMPLICIT_1)
710     {
711       tmp->normal_exp--;
712
713       high <<= 1;
714       if (low & FRACHIGH)
715         high |= 1;
716       low <<= 1;
717     }
718   /* rounding is tricky. if we only round if it won't make us round later. */
719 #if 0
720   if (low & FRACHIGH2)
721     {
722       if (((high & GARDMASK) != GARDMSB)
723           && (((high + 1) & GARDMASK) == GARDMSB))
724         {
725           /* don't round, it gets done again later. */
726         }
727       else
728         {
729           high++;
730         }
731     }
732 #endif
733   if ((high & GARDMASK) == GARDMSB)
734     {
735       if (high & (1 << NGARDS))
736         {
737           /* half way, so round to even */
738           high += GARDROUND + 1;
739         }
740       else if (low)
741         {
742           /* but we really weren't half way */
743           high += GARDROUND + 1;
744         }
745     }
746   tmp->fraction.ll = high;
747   tmp->class = CLASS_NUMBER;
748   return tmp;
749 }
750
751 FLO_type
752 multiply (FLO_type arg_a, FLO_type arg_b)
753 {
754   fp_number_type a;
755   fp_number_type b;
756   fp_number_type tmp;
757   fp_number_type *res;
758   FLO_union_type au, bu;
759
760   au.value = arg_a;
761   bu.value = arg_b;
762
763   unpack_d (&au, &a);
764   unpack_d (&bu, &b);
765
766   res = _fpmul_parts (&a, &b, &tmp);
767
768   return pack_d (res);
769 }
770 #endif /* L_mul_sf || L_mul_df */
771
772 #if defined(L_div_sf) || defined(L_div_df)
773 static INLINE fp_number_type *
774 _fpdiv_parts (fp_number_type * a,
775               fp_number_type * b)
776 {
777   fractype bit;
778   fractype numerator;
779   fractype denominator;
780   fractype quotient;
781
782   if (isnan (a))
783     {
784       return a;
785     }
786   if (isnan (b))
787     {
788       return b;
789     }
790
791   a->sign = a->sign ^ b->sign;
792
793   if (isinf (a) || iszero (a))
794     {
795       if (a->class == b->class)
796         return nan ();
797       return a;
798     }
799
800   if (isinf (b))
801     {
802       a->fraction.ll = 0;
803       a->normal_exp = 0;
804       return a;
805     }
806   if (iszero (b))
807     {
808       a->class = CLASS_INFINITY;
809       return a;
810     }
811
812   /* Calculate the mantissa by multiplying both 64bit numbers to get a
813      128 bit number */
814   {
815     /* quotient =
816        ( numerator / denominator) * 2^(numerator exponent -  denominator exponent)
817      */
818
819     a->normal_exp = a->normal_exp - b->normal_exp;
820     numerator = a->fraction.ll;
821     denominator = b->fraction.ll;
822
823     if (numerator < denominator)
824       {
825         /* Fraction will be less than 1.0 */
826         numerator *= 2;
827         a->normal_exp--;
828       }
829     bit = IMPLICIT_1;
830     quotient = 0;
831     /* ??? Does divide one bit at a time.  Optimize.  */
832     while (bit)
833       {
834         if (numerator >= denominator)
835           {
836             quotient |= bit;
837             numerator -= denominator;
838           }
839         bit >>= 1;
840         numerator *= 2;
841       }
842
843     if ((quotient & GARDMASK) == GARDMSB)
844       {
845         if (quotient & (1 << NGARDS))
846           {
847             /* half way, so round to even */
848             quotient += GARDROUND + 1;
849           }
850         else if (numerator)
851           {
852             /* but we really weren't half way, more bits exist */
853             quotient += GARDROUND + 1;
854           }
855       }
856
857     a->fraction.ll = quotient;
858     return (a);
859   }
860 }
861
862 FLO_type
863 divide (FLO_type arg_a, FLO_type arg_b)
864 {
865   fp_number_type a;
866   fp_number_type b;
867   fp_number_type *res;
868   FLO_union_type au, bu;
869
870   au.value = arg_a;
871   bu.value = arg_b;
872
873   unpack_d (&au, &a);
874   unpack_d (&bu, &b);
875
876   res = _fpdiv_parts (&a, &b);
877
878   return pack_d (res);
879 }
880 #endif /* L_div_sf || L_div_df */
881
882 #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df)
883 /* according to the demo, fpcmp returns a comparison with 0... thus
884    a<b -> -1
885    a==b -> 0
886    a>b -> +1
887  */
888
889 int
890 __fpcmp_parts (fp_number_type * a, fp_number_type * b)
891 {
892 #if 0
893   /* either nan -> unordered. Must be checked outside of this routine. */
894   if (isnan (a) && isnan (b))
895     {
896       return 1;                 /* still unordered! */
897     }
898 #endif
899
900   if (isnan (a) || isnan (b))
901     {
902       return 1;                 /* how to indicate unordered compare? */
903     }
904   if (isinf (a) && isinf (b))
905     {
906       /* +inf > -inf, but +inf != +inf */
907       /* b    \a| +inf(0)| -inf(1)
908        ______\+--------+--------
909        +inf(0)| a==b(0)| a<b(-1)
910        -------+--------+--------
911        -inf(1)| a>b(1) | a==b(0)
912        -------+--------+--------
913        So since unordered must be non zero, just line up the columns...
914        */
915       return b->sign - a->sign;
916     }
917   /* but not both... */
918   if (isinf (a))
919     {
920       return a->sign ? -1 : 1;
921     }
922   if (isinf (b))
923     {
924       return b->sign ? 1 : -1;
925     }
926   if (iszero (a) && iszero (b))
927     {
928       return 0;
929     }
930   if (iszero (a))
931     {
932       return b->sign ? 1 : -1;
933     }
934   if (iszero (b))
935     {
936       return a->sign ? -1 : 1;
937     }
938   /* now both are "normal". */
939   if (a->sign != b->sign)
940     {
941       /* opposite signs */
942       return a->sign ? -1 : 1;
943     }
944   /* same sign; exponents? */
945   if (a->normal_exp > b->normal_exp)
946     {
947       return a->sign ? -1 : 1;
948     }
949   if (a->normal_exp < b->normal_exp)
950     {
951       return a->sign ? 1 : -1;
952     }
953   /* same exponents; check size. */
954   if (a->fraction.ll > b->fraction.ll)
955     {
956       return a->sign ? -1 : 1;
957     }
958   if (a->fraction.ll < b->fraction.ll)
959     {
960       return a->sign ? 1 : -1;
961     }
962   /* after all that, they're equal. */
963   return 0;
964 }
965 #endif
966
967 #if defined(L_compare_sf) || defined(L_compare_df)
968 CMPtype
969 compare (FLO_type arg_a, FLO_type arg_b)
970 {
971   fp_number_type a;
972   fp_number_type b;
973   FLO_union_type au, bu;
974
975   au.value = arg_a;
976   bu.value = arg_b;
977
978   unpack_d (&au, &a);
979   unpack_d (&bu, &b);
980
981   return __fpcmp_parts (&a, &b);
982 }
983 #endif /* L_compare_sf || L_compare_df */
984
985 #ifndef US_SOFTWARE_GOFAST
986
987 /* These should be optimized for their specific tasks someday.  */
988
989 #if defined(L_eq_sf) || defined(L_eq_df)
990 CMPtype
991 _eq_f2 (FLO_type arg_a, FLO_type arg_b)
992 {
993   fp_number_type a;
994   fp_number_type b;
995   FLO_union_type au, bu;
996
997   au.value = arg_a;
998   bu.value = arg_b;
999
1000   unpack_d (&au, &a);
1001   unpack_d (&bu, &b);
1002
1003   if (isnan (&a) || isnan (&b))
1004     return 1;                   /* false, truth == 0 */
1005
1006   return __fpcmp_parts (&a, &b) ;
1007 }
1008 #endif /* L_eq_sf || L_eq_df */
1009
1010 #if defined(L_ne_sf) || defined(L_ne_df)
1011 CMPtype
1012 _ne_f2 (FLO_type arg_a, FLO_type arg_b)
1013 {
1014   fp_number_type a;
1015   fp_number_type b;
1016   FLO_union_type au, bu;
1017
1018   au.value = arg_a;
1019   bu.value = arg_b;
1020
1021   unpack_d (&au, &a);
1022   unpack_d (&bu, &b);
1023
1024   if (isnan (&a) || isnan (&b))
1025     return 1;                   /* true, truth != 0 */
1026
1027   return  __fpcmp_parts (&a, &b) ;
1028 }
1029 #endif /* L_ne_sf || L_ne_df */
1030
1031 #if defined(L_gt_sf) || defined(L_gt_df)
1032 CMPtype
1033 _gt_f2 (FLO_type arg_a, FLO_type arg_b)
1034 {
1035   fp_number_type a;
1036   fp_number_type b;
1037   FLO_union_type au, bu;
1038
1039   au.value = arg_a;
1040   bu.value = arg_b;
1041
1042   unpack_d (&au, &a);
1043   unpack_d (&bu, &b);
1044
1045   if (isnan (&a) || isnan (&b))
1046     return -1;                  /* false, truth > 0 */
1047
1048   return __fpcmp_parts (&a, &b);
1049 }
1050 #endif /* L_gt_sf || L_gt_df */
1051
1052 #if defined(L_ge_sf) || defined(L_ge_df)
1053 CMPtype
1054 _ge_f2 (FLO_type arg_a, FLO_type arg_b)
1055 {
1056   fp_number_type a;
1057   fp_number_type b;
1058   FLO_union_type au, bu;
1059
1060   au.value = arg_a;
1061   bu.value = arg_b;
1062
1063   unpack_d (&au, &a);
1064   unpack_d (&bu, &b);
1065
1066   if (isnan (&a) || isnan (&b))
1067     return -1;                  /* false, truth >= 0 */
1068   return __fpcmp_parts (&a, &b) ;
1069 }
1070 #endif /* L_ge_sf || L_ge_df */
1071
1072 #if defined(L_lt_sf) || defined(L_lt_df)
1073 CMPtype
1074 _lt_f2 (FLO_type arg_a, FLO_type arg_b)
1075 {
1076   fp_number_type a;
1077   fp_number_type b;
1078   FLO_union_type au, bu;
1079
1080   au.value = arg_a;
1081   bu.value = arg_b;
1082
1083   unpack_d (&au, &a);
1084   unpack_d (&bu, &b);
1085
1086   if (isnan (&a) || isnan (&b))
1087     return 1;                   /* false, truth < 0 */
1088
1089   return __fpcmp_parts (&a, &b);
1090 }
1091 #endif /* L_lt_sf || L_lt_df */
1092
1093 #if defined(L_le_sf) || defined(L_le_df)
1094 CMPtype
1095 _le_f2 (FLO_type arg_a, FLO_type arg_b)
1096 {
1097   fp_number_type a;
1098   fp_number_type b;
1099   FLO_union_type au, bu;
1100
1101   au.value = arg_a;
1102   bu.value = arg_b;
1103
1104   unpack_d (&au, &a);
1105   unpack_d (&bu, &b);
1106
1107   if (isnan (&a) || isnan (&b))
1108     return 1;                   /* false, truth <= 0 */
1109
1110   return __fpcmp_parts (&a, &b) ;
1111 }
1112 #endif /* L_le_sf || L_le_df */
1113
1114 #if defined(L_unord_sf) || defined(L_unord_df)
1115 CMPtype
1116 _unord_f2 (FLO_type arg_a, FLO_type arg_b)
1117 {
1118   fp_number_type a;
1119   fp_number_type b;
1120   FLO_union_type au, bu;
1121
1122   au.value = arg_a;
1123   bu.value = arg_b;
1124
1125   unpack_d (&au, &a);
1126   unpack_d (&bu, &b);
1127
1128   return (isnan (&a) || isnan (&b));
1129 }
1130 #endif /* L_unord_sf || L_unord_df */
1131
1132 #endif /* ! US_SOFTWARE_GOFAST */
1133
1134 #if defined(L_si_to_sf) || defined(L_si_to_df)
1135 FLO_type
1136 si_to_float (SItype arg_a)
1137 {
1138   fp_number_type in;
1139
1140   in.class = CLASS_NUMBER;
1141   in.sign = arg_a < 0;
1142   if (!arg_a)
1143     {
1144       in.class = CLASS_ZERO;
1145     }
1146   else
1147     {
1148       in.normal_exp = FRACBITS + NGARDS;
1149       if (in.sign) 
1150         {
1151           /* Special case for minint, since there is no +ve integer
1152              representation for it */
1153           if (arg_a == (SItype) 0x80000000)
1154             {
1155               return -2147483648.0;
1156             }
1157           in.fraction.ll = (-arg_a);
1158         }
1159       else
1160         in.fraction.ll = arg_a;
1161
1162       while (in.fraction.ll < (1LL << (FRACBITS + NGARDS)))
1163         {
1164           in.fraction.ll <<= 1;
1165           in.normal_exp -= 1;
1166         }
1167     }
1168   return pack_d (&in);
1169 }
1170 #endif /* L_si_to_sf || L_si_to_df */
1171
1172 #if defined(L_sf_to_si) || defined(L_df_to_si)
1173 SItype
1174 float_to_si (FLO_type arg_a)
1175 {
1176   fp_number_type a;
1177   SItype tmp;
1178   FLO_union_type au;
1179
1180   au.value = arg_a;
1181   unpack_d (&au, &a);
1182
1183   if (iszero (&a))
1184     return 0;
1185   if (isnan (&a))
1186     return 0;
1187   /* get reasonable MAX_SI_INT... */
1188   if (isinf (&a))
1189     return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1190   /* it is a number, but a small one */
1191   if (a.normal_exp < 0)
1192     return 0;
1193   if (a.normal_exp > 30)
1194     return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1195   tmp = a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1196   return a.sign ? (-tmp) : (tmp);
1197 }
1198 #endif /* L_sf_to_si || L_df_to_si */
1199
1200 #if defined(L_sf_to_usi) || defined(L_df_to_usi)
1201 #ifdef US_SOFTWARE_GOFAST
1202 /* While libgcc2.c defines its own __fixunssfsi and __fixunsdfsi routines,
1203    we also define them for GOFAST because the ones in libgcc2.c have the
1204    wrong names and I'd rather define these here and keep GOFAST CYG-LOC's
1205    out of libgcc2.c.  We can't define these here if not GOFAST because then
1206    there'd be duplicate copies.  */
1207
1208 USItype
1209 float_to_usi (FLO_type arg_a)
1210 {
1211   fp_number_type a;
1212   FLO_union_type au;
1213
1214   au.value = arg_a;
1215   unpack_d (&au, &a);
1216
1217   if (iszero (&a))
1218     return 0;
1219   if (isnan (&a))
1220     return 0;
1221   /* it is a negative number */
1222   if (a.sign)
1223     return 0;
1224   /* get reasonable MAX_USI_INT... */
1225   if (isinf (&a))
1226     return MAX_USI_INT;
1227   /* it is a number, but a small one */
1228   if (a.normal_exp < 0)
1229     return 0;
1230   if (a.normal_exp > 31)
1231     return MAX_USI_INT;
1232   else if (a.normal_exp > (FRACBITS + NGARDS))
1233     return a.fraction.ll << (a.normal_exp - (FRACBITS + NGARDS));
1234   else
1235     return a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1236 }
1237 #endif /* US_SOFTWARE_GOFAST */
1238 #endif /* L_sf_to_usi || L_df_to_usi */
1239
1240 #if defined(L_negate_sf) || defined(L_negate_df)
1241 FLO_type
1242 negate (FLO_type arg_a)
1243 {
1244   fp_number_type a;
1245   FLO_union_type au;
1246
1247   au.value = arg_a;
1248   unpack_d (&au, &a);
1249
1250   flip_sign (&a);
1251   return pack_d (&a);
1252 }
1253 #endif /* L_negate_sf || L_negate_df */
1254
1255 #ifdef FLOAT
1256
1257 #if defined(L_make_sf)
1258 SFtype
1259 __make_fp(fp_class_type class,
1260              unsigned int sign,
1261              int exp, 
1262              USItype frac)
1263 {
1264   fp_number_type in;
1265
1266   in.class = class;
1267   in.sign = sign;
1268   in.normal_exp = exp;
1269   in.fraction.ll = frac;
1270   return pack_d (&in);
1271 }
1272 #endif /* L_make_sf */
1273
1274 #ifndef FLOAT_ONLY
1275
1276 /* This enables one to build an fp library that supports float but not double.
1277    Otherwise, we would get an undefined reference to __make_dp.
1278    This is needed for some 8-bit ports that can't handle well values that
1279    are 8-bytes in size, so we just don't support double for them at all.  */
1280
1281 #if defined(L_sf_to_df)
1282 DFtype
1283 sf_to_df (SFtype arg_a)
1284 {
1285   fp_number_type in;
1286   FLO_union_type au;
1287
1288   au.value = arg_a;
1289   unpack_d (&au, &in);
1290
1291   return __make_dp (in.class, in.sign, in.normal_exp,
1292                     ((UDItype) in.fraction.ll) << F_D_BITOFF);
1293 }
1294 #endif /* L_sf_to_df */
1295
1296 #endif /* ! FLOAT_ONLY */
1297 #endif /* FLOAT */
1298
1299 #ifndef FLOAT
1300
1301 extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
1302
1303 #if defined(L_make_df)
1304 DFtype
1305 __make_dp (fp_class_type class, unsigned int sign, int exp, UDItype frac)
1306 {
1307   fp_number_type in;
1308
1309   in.class = class;
1310   in.sign = sign;
1311   in.normal_exp = exp;
1312   in.fraction.ll = frac;
1313   return pack_d (&in);
1314 }
1315 #endif /* L_make_df */
1316
1317 #if defined(L_df_to_sf)
1318 SFtype
1319 df_to_sf (DFtype arg_a)
1320 {
1321   fp_number_type in;
1322   USItype sffrac;
1323   FLO_union_type au;
1324
1325   au.value = arg_a;
1326   unpack_d (&au, &in);
1327
1328   sffrac = in.fraction.ll >> F_D_BITOFF;
1329
1330   /* We set the lowest guard bit in SFFRAC if we discarded any non
1331      zero bits.  */
1332   if ((in.fraction.ll & (((USItype) 1 << F_D_BITOFF) - 1)) != 0)
1333     sffrac |= 1;
1334
1335   return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
1336 }
1337 #endif /* L_df_to_sf */
1338
1339 #endif /* ! FLOAT */
1340 #endif /* !EXTENDED_FLOAT_STUBS */