OSDN Git Service

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