OSDN Git Service

fc87dd311b7b12b5d8386b88f299345d9a082845
[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, 2003
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 L_thenan_tf
136 const fp_number_type __thenan_tf = { CLASS_SNAN, 0, 0, {(fractype) 0} };
137 #elif defined TFLOAT
138 extern const fp_number_type __thenan_tf;
139 #elif defined FLOAT
140 extern const fp_number_type __thenan_sf;
141 #else
142 extern const fp_number_type __thenan_df;
143 #endif
144
145 INLINE
146 static fp_number_type *
147 nan (void)
148 {
149   /* Discard the const qualifier...  */
150 #ifdef TFLOAT
151   return (fp_number_type *) (& __thenan_tf);
152 #elif defined FLOAT  
153   return (fp_number_type *) (& __thenan_sf);
154 #else
155   return (fp_number_type *) (& __thenan_df);
156 #endif
157 }
158
159 INLINE
160 static int
161 isnan ( fp_number_type *  x)
162 {
163   return x->class == CLASS_SNAN || x->class == CLASS_QNAN;
164 }
165
166 INLINE
167 static int
168 isinf ( fp_number_type *  x)
169 {
170   return x->class == CLASS_INFINITY;
171 }
172
173 #endif /* NO_NANS */
174
175 INLINE
176 static int
177 iszero ( fp_number_type *  x)
178 {
179   return x->class == CLASS_ZERO;
180 }
181
182 INLINE 
183 static void
184 flip_sign ( fp_number_type *  x)
185 {
186   x->sign = !x->sign;
187 }
188
189 extern FLO_type pack_d ( fp_number_type * );
190
191 #if defined(L_pack_df) || defined(L_pack_sf) || defined(L_pack_tf)
192 FLO_type
193 pack_d ( fp_number_type *  src)
194 {
195   FLO_union_type dst;
196   fractype fraction = src->fraction.ll; /* wasn't unsigned before? */
197   int sign = src->sign;
198   int exp = 0;
199
200   if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && (isnan (src) || isinf (src)))
201     {
202       /* We can't represent these values accurately.  By using the
203          largest possible magnitude, we guarantee that the conversion
204          of infinity is at least as big as any finite number.  */
205       exp = EXPMAX;
206       fraction = ((fractype) 1 << FRACBITS) - 1;
207     }
208   else if (isnan (src))
209     {
210       exp = EXPMAX;
211       if (src->class == CLASS_QNAN || 1)
212         {
213           fraction |= QUIET_NAN;
214         }
215     }
216   else if (isinf (src))
217     {
218       exp = EXPMAX;
219       fraction = 0;
220     }
221   else if (iszero (src))
222     {
223       exp = 0;
224       fraction = 0;
225     }
226   else if (fraction == 0)
227     {
228       exp = 0;
229     }
230   else
231     {
232       if (src->normal_exp < NORMAL_EXPMIN)
233         {
234 #ifdef NO_DENORMALS
235           /* Go straight to a zero representation if denormals are not
236              supported.  The denormal handling would be harmless but
237              isn't unnecessary.  */
238           exp = 0;
239           fraction = 0;
240 #else /* NO_DENORMALS */
241           /* This number's exponent is too low to fit into the bits
242              available in the number, so we'll store 0 in the exponent and
243              shift the fraction to the right to make up for it.  */
244
245           int shift = NORMAL_EXPMIN - src->normal_exp;
246
247           exp = 0;
248
249           if (shift > FRAC_NBITS - NGARDS)
250             {
251               /* No point shifting, since it's more that 64 out.  */
252               fraction = 0;
253             }
254           else
255             {
256               int lowbit = (fraction & (((fractype)1 << shift) - 1)) ? 1 : 0;
257               fraction = (fraction >> shift) | lowbit;
258             }
259           if ((fraction & GARDMASK) == GARDMSB)
260             {
261               if ((fraction & (1 << NGARDS)))
262                 fraction += GARDROUND + 1;
263             }
264           else
265             {
266               /* Add to the guards to round up.  */
267               fraction += GARDROUND;
268             }
269           /* Perhaps the rounding means we now need to change the
270              exponent, because the fraction is no longer denormal.  */
271           if (fraction >= IMPLICIT_1)
272             {
273               exp += 1;
274             }
275           fraction >>= NGARDS;
276 #endif /* NO_DENORMALS */
277         }
278       else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS)
279                && src->normal_exp > EXPBIAS)
280         {
281           exp = EXPMAX;
282           fraction = 0;
283         }
284       else
285         {
286           exp = src->normal_exp + EXPBIAS;
287           if (!ROUND_TOWARDS_ZERO)
288             {
289               /* IF the gard bits are the all zero, but the first, then we're
290                  half way between two numbers, choose the one which makes the
291                  lsb of the answer 0.  */
292               if ((fraction & GARDMASK) == GARDMSB)
293                 {
294                   if (fraction & (1 << NGARDS))
295                     fraction += GARDROUND + 1;
296                 }
297               else
298                 {
299                   /* Add a one to the guards to round up */
300                   fraction += GARDROUND;
301                 }
302               if (fraction >= IMPLICIT_2)
303                 {
304                   fraction >>= 1;
305                   exp += 1;
306                 }
307             }
308           fraction >>= NGARDS;
309
310           if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && exp > EXPMAX)
311             {
312               /* Saturate on overflow.  */
313               exp = EXPMAX;
314               fraction = ((fractype) 1 << FRACBITS) - 1;
315             }
316         }
317     }
318
319   /* We previously used bitfields to store the number, but this doesn't
320      handle little/big endian systems conveniently, so use shifts and
321      masks */
322 #ifdef FLOAT_BIT_ORDER_MISMATCH
323   dst.bits.fraction = fraction;
324   dst.bits.exp = exp;
325   dst.bits.sign = sign;
326 #else
327   dst.value_raw = fraction & ((((fractype)1) << FRACBITS) - (fractype)1);
328   dst.value_raw |= ((fractype) (exp & ((1 << EXPBITS) - 1))) << FRACBITS;
329   dst.value_raw |= ((fractype) (sign & 1)) << (FRACBITS | EXPBITS);
330 #endif
331
332 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
333 #ifdef TFLOAT
334   {
335     qrtrfractype tmp1 = dst.words[0];
336     qrtrfractype tmp2 = dst.words[1];
337     dst.words[0] = dst.words[3];
338     dst.words[1] = dst.words[2];
339     dst.words[2] = tmp2;
340     dst.words[3] = tmp1;
341   }
342 #else
343   {
344     halffractype tmp = dst.words[0];
345     dst.words[0] = dst.words[1];
346     dst.words[1] = tmp;
347   }
348 #endif
349 #endif
350
351   return dst.value;
352 }
353 #endif
354
355 #if defined(L_unpack_df) || defined(L_unpack_sf) || defined(L_unpack_tf)
356 void
357 unpack_d (FLO_union_type * src, fp_number_type * dst)
358 {
359   /* We previously used bitfields to store the number, but this doesn't
360      handle little/big endian systems conveniently, so use shifts and
361      masks */
362   fractype fraction;
363   int exp;
364   int sign;
365
366 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
367   FLO_union_type swapped;
368
369 #ifdef TFLOAT
370   swapped.words[0] = src->words[3];
371   swapped.words[1] = src->words[2];
372   swapped.words[2] = src->words[1];
373   swapped.words[3] = src->words[0];
374 #else
375   swapped.words[0] = src->words[1];
376   swapped.words[1] = src->words[0];
377 #endif
378   src = &swapped;
379 #endif
380   
381 #ifdef FLOAT_BIT_ORDER_MISMATCH
382   fraction = src->bits.fraction;
383   exp = src->bits.exp;
384   sign = src->bits.sign;
385 #else
386   fraction = src->value_raw & ((((fractype)1) << FRACBITS) - 1);
387   exp = ((int)(src->value_raw >> FRACBITS)) & ((1 << EXPBITS) - 1);
388   sign = ((int)(src->value_raw >> (FRACBITS + EXPBITS))) & 1;
389 #endif
390
391   dst->sign = sign;
392   if (exp == 0)
393     {
394       /* Hmm.  Looks like 0 */
395       if (fraction == 0
396 #ifdef NO_DENORMALS
397           || 1
398 #endif
399           )
400         {
401           /* tastes like zero */
402           dst->class = CLASS_ZERO;
403         }
404       else
405         {
406           /* Zero exponent with nonzero fraction - it's denormalized,
407              so there isn't a leading implicit one - we'll shift it so
408              it gets one.  */
409           dst->normal_exp = exp - EXPBIAS + 1;
410           fraction <<= NGARDS;
411
412           dst->class = CLASS_NUMBER;
413 #if 1
414           while (fraction < IMPLICIT_1)
415             {
416               fraction <<= 1;
417               dst->normal_exp--;
418             }
419 #endif
420           dst->fraction.ll = fraction;
421         }
422     }
423   else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && exp == EXPMAX)
424     {
425       /* Huge exponent*/
426       if (fraction == 0)
427         {
428           /* Attached to a zero fraction - means infinity */
429           dst->class = CLASS_INFINITY;
430         }
431       else
432         {
433           /* Nonzero fraction, means nan */
434           if (fraction & QUIET_NAN)
435             {
436               dst->class = CLASS_QNAN;
437             }
438           else
439             {
440               dst->class = CLASS_SNAN;
441             }
442           /* Keep the fraction part as the nan number */
443           dst->fraction.ll = fraction;
444         }
445     }
446   else
447     {
448       /* Nothing strange about this number */
449       dst->normal_exp = exp - EXPBIAS;
450       dst->class = CLASS_NUMBER;
451       dst->fraction.ll = (fraction << NGARDS) | IMPLICIT_1;
452     }
453 }
454 #endif /* L_unpack_df || L_unpack_sf */
455
456 #if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
457 static fp_number_type *
458 _fpadd_parts (fp_number_type * a,
459               fp_number_type * b,
460               fp_number_type * tmp)
461 {
462   intfrac tfraction;
463
464   /* Put commonly used fields in local variables.  */
465   int a_normal_exp;
466   int b_normal_exp;
467   fractype a_fraction;
468   fractype b_fraction;
469
470   if (isnan (a))
471     {
472       return a;
473     }
474   if (isnan (b))
475     {
476       return b;
477     }
478   if (isinf (a))
479     {
480       /* Adding infinities with opposite signs yields a NaN.  */
481       if (isinf (b) && a->sign != b->sign)
482         return nan ();
483       return a;
484     }
485   if (isinf (b))
486     {
487       return b;
488     }
489   if (iszero (b))
490     {
491       if (iszero (a))
492         {
493           *tmp = *a;
494           tmp->sign = a->sign & b->sign;
495           return tmp;
496         }
497       return a;
498     }
499   if (iszero (a))
500     {
501       return b;
502     }
503
504   /* Got two numbers. shift the smaller and increment the exponent till
505      they're the same */
506   {
507     int diff;
508
509     a_normal_exp = a->normal_exp;
510     b_normal_exp = b->normal_exp;
511     a_fraction = a->fraction.ll;
512     b_fraction = b->fraction.ll;
513
514     diff = a_normal_exp - b_normal_exp;
515
516     if (diff < 0)
517       diff = -diff;
518     if (diff < FRAC_NBITS)
519       {
520         /* ??? This does shifts one bit at a time.  Optimize.  */
521         while (a_normal_exp > b_normal_exp)
522           {
523             b_normal_exp++;
524             LSHIFT (b_fraction);
525           }
526         while (b_normal_exp > a_normal_exp)
527           {
528             a_normal_exp++;
529             LSHIFT (a_fraction);
530           }
531       }
532     else
533       {
534         /* Somethings's up.. choose the biggest */
535         if (a_normal_exp > b_normal_exp)
536           {
537             b_normal_exp = a_normal_exp;
538             b_fraction = 0;
539           }
540         else
541           {
542             a_normal_exp = b_normal_exp;
543             a_fraction = 0;
544           }
545       }
546   }
547
548   if (a->sign != b->sign)
549     {
550       if (a->sign)
551         {
552           tfraction = -a_fraction + b_fraction;
553         }
554       else
555         {
556           tfraction = a_fraction - b_fraction;
557         }
558       if (tfraction >= 0)
559         {
560           tmp->sign = 0;
561           tmp->normal_exp = a_normal_exp;
562           tmp->fraction.ll = tfraction;
563         }
564       else
565         {
566           tmp->sign = 1;
567           tmp->normal_exp = a_normal_exp;
568           tmp->fraction.ll = -tfraction;
569         }
570       /* and renormalize it */
571
572       while (tmp->fraction.ll < IMPLICIT_1 && tmp->fraction.ll)
573         {
574           tmp->fraction.ll <<= 1;
575           tmp->normal_exp--;
576         }
577     }
578   else
579     {
580       tmp->sign = a->sign;
581       tmp->normal_exp = a_normal_exp;
582       tmp->fraction.ll = a_fraction + b_fraction;
583     }
584   tmp->class = CLASS_NUMBER;
585   /* Now the fraction is added, we have to shift down to renormalize the
586      number */
587
588   if (tmp->fraction.ll >= IMPLICIT_2)
589     {
590       LSHIFT (tmp->fraction.ll);
591       tmp->normal_exp++;
592     }
593   return tmp;
594
595 }
596
597 FLO_type
598 add (FLO_type arg_a, FLO_type arg_b)
599 {
600   fp_number_type a;
601   fp_number_type b;
602   fp_number_type tmp;
603   fp_number_type *res;
604   FLO_union_type au, bu;
605
606   au.value = arg_a;
607   bu.value = arg_b;
608
609   unpack_d (&au, &a);
610   unpack_d (&bu, &b);
611
612   res = _fpadd_parts (&a, &b, &tmp);
613
614   return pack_d (res);
615 }
616
617 FLO_type
618 sub (FLO_type arg_a, FLO_type arg_b)
619 {
620   fp_number_type a;
621   fp_number_type b;
622   fp_number_type tmp;
623   fp_number_type *res;
624   FLO_union_type au, bu;
625
626   au.value = arg_a;
627   bu.value = arg_b;
628
629   unpack_d (&au, &a);
630   unpack_d (&bu, &b);
631
632   b.sign ^= 1;
633
634   res = _fpadd_parts (&a, &b, &tmp);
635
636   return pack_d (res);
637 }
638 #endif /* L_addsub_sf || L_addsub_df */
639
640 #if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
641 static inline __attribute__ ((__always_inline__)) fp_number_type *
642 _fpmul_parts ( fp_number_type *  a,
643                fp_number_type *  b,
644                fp_number_type * tmp)
645 {
646   fractype low = 0;
647   fractype high = 0;
648
649   if (isnan (a))
650     {
651       a->sign = a->sign != b->sign;
652       return a;
653     }
654   if (isnan (b))
655     {
656       b->sign = a->sign != b->sign;
657       return b;
658     }
659   if (isinf (a))
660     {
661       if (iszero (b))
662         return nan ();
663       a->sign = a->sign != b->sign;
664       return a;
665     }
666   if (isinf (b))
667     {
668       if (iszero (a))
669         {
670           return nan ();
671         }
672       b->sign = a->sign != b->sign;
673       return b;
674     }
675   if (iszero (a))
676     {
677       a->sign = a->sign != b->sign;
678       return a;
679     }
680   if (iszero (b))
681     {
682       b->sign = a->sign != b->sign;
683       return b;
684     }
685
686   /* Calculate the mantissa by multiplying both numbers to get a
687      twice-as-wide number.  */
688   {
689 #if defined(NO_DI_MODE) || defined(TFLOAT)
690     {
691       fractype x = a->fraction.ll;
692       fractype ylow = b->fraction.ll;
693       fractype yhigh = 0;
694       int bit;
695
696       /* ??? This does multiplies one bit at a time.  Optimize.  */
697       for (bit = 0; bit < FRAC_NBITS; bit++)
698         {
699           int carry;
700
701           if (x & 1)
702             {
703               carry = (low += ylow) < ylow;
704               high += yhigh + carry;
705             }
706           yhigh <<= 1;
707           if (ylow & FRACHIGH)
708             {
709               yhigh |= 1;
710             }
711           ylow <<= 1;
712           x >>= 1;
713         }
714     }
715 #elif defined(FLOAT) 
716     /* Multiplying two USIs to get a UDI, we're safe.  */
717     {
718       UDItype answer = (UDItype)a->fraction.ll * (UDItype)b->fraction.ll;
719       
720       high = answer >> BITS_PER_SI;
721       low = answer;
722     }
723 #else
724     /* fractype is DImode, but we need the result to be twice as wide.
725        Assuming a widening multiply from DImode to TImode is not
726        available, build one by hand.  */
727     {
728       USItype nl = a->fraction.ll;
729       USItype nh = a->fraction.ll >> BITS_PER_SI;
730       USItype ml = b->fraction.ll;
731       USItype mh = b->fraction.ll >> BITS_PER_SI;
732       UDItype pp_ll = (UDItype) ml * nl;
733       UDItype pp_hl = (UDItype) mh * nl;
734       UDItype pp_lh = (UDItype) ml * nh;
735       UDItype pp_hh = (UDItype) mh * nh;
736       UDItype res2 = 0;
737       UDItype res0 = 0;
738       UDItype ps_hh__ = pp_hl + pp_lh;
739       if (ps_hh__ < pp_hl)
740         res2 += (UDItype)1 << BITS_PER_SI;
741       pp_hl = (UDItype)(USItype)ps_hh__ << BITS_PER_SI;
742       res0 = pp_ll + pp_hl;
743       if (res0 < pp_ll)
744         res2++;
745       res2 += (ps_hh__ >> BITS_PER_SI) + pp_hh;
746       high = res2;
747       low = res0;
748     }
749 #endif
750   }
751
752   tmp->normal_exp = a->normal_exp + b->normal_exp
753     + FRAC_NBITS - (FRACBITS + NGARDS);
754   tmp->sign = a->sign != b->sign;
755   while (high >= IMPLICIT_2)
756     {
757       tmp->normal_exp++;
758       if (high & 1)
759         {
760           low >>= 1;
761           low |= FRACHIGH;
762         }
763       high >>= 1;
764     }
765   while (high < IMPLICIT_1)
766     {
767       tmp->normal_exp--;
768
769       high <<= 1;
770       if (low & FRACHIGH)
771         high |= 1;
772       low <<= 1;
773     }
774   /* rounding is tricky. if we only round if it won't make us round later.  */
775 #if 0
776   if (low & FRACHIGH2)
777     {
778       if (((high & GARDMASK) != GARDMSB)
779           && (((high + 1) & GARDMASK) == GARDMSB))
780         {
781           /* don't round, it gets done again later.  */
782         }
783       else
784         {
785           high++;
786         }
787     }
788 #endif
789   if (!ROUND_TOWARDS_ZERO && (high & GARDMASK) == GARDMSB)
790     {
791       if (high & (1 << NGARDS))
792         {
793           /* half way, so round to even */
794           high += GARDROUND + 1;
795         }
796       else if (low)
797         {
798           /* but we really weren't half way */
799           high += GARDROUND + 1;
800         }
801     }
802   tmp->fraction.ll = high;
803   tmp->class = CLASS_NUMBER;
804   return tmp;
805 }
806
807 FLO_type
808 multiply (FLO_type arg_a, FLO_type arg_b)
809 {
810   fp_number_type a;
811   fp_number_type b;
812   fp_number_type tmp;
813   fp_number_type *res;
814   FLO_union_type au, bu;
815
816   au.value = arg_a;
817   bu.value = arg_b;
818
819   unpack_d (&au, &a);
820   unpack_d (&bu, &b);
821
822   res = _fpmul_parts (&a, &b, &tmp);
823
824   return pack_d (res);
825 }
826 #endif /* L_mul_sf || L_mul_df */
827
828 #if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
829 static inline __attribute__ ((__always_inline__)) fp_number_type *
830 _fpdiv_parts (fp_number_type * a,
831               fp_number_type * b)
832 {
833   fractype bit;
834   fractype numerator;
835   fractype denominator;
836   fractype quotient;
837
838   if (isnan (a))
839     {
840       return a;
841     }
842   if (isnan (b))
843     {
844       return b;
845     }
846
847   a->sign = a->sign ^ b->sign;
848
849   if (isinf (a) || iszero (a))
850     {
851       if (a->class == b->class)
852         return nan ();
853       return a;
854     }
855
856   if (isinf (b))
857     {
858       a->fraction.ll = 0;
859       a->normal_exp = 0;
860       return a;
861     }
862   if (iszero (b))
863     {
864       a->class = CLASS_INFINITY;
865       return a;
866     }
867
868   /* Calculate the mantissa by multiplying both 64bit numbers to get a
869      128 bit number */
870   {
871     /* quotient =
872        ( numerator / denominator) * 2^(numerator exponent -  denominator exponent)
873      */
874
875     a->normal_exp = a->normal_exp - b->normal_exp;
876     numerator = a->fraction.ll;
877     denominator = b->fraction.ll;
878
879     if (numerator < denominator)
880       {
881         /* Fraction will be less than 1.0 */
882         numerator *= 2;
883         a->normal_exp--;
884       }
885     bit = IMPLICIT_1;
886     quotient = 0;
887     /* ??? Does divide one bit at a time.  Optimize.  */
888     while (bit)
889       {
890         if (numerator >= denominator)
891           {
892             quotient |= bit;
893             numerator -= denominator;
894           }
895         bit >>= 1;
896         numerator *= 2;
897       }
898
899     if (!ROUND_TOWARDS_ZERO && (quotient & GARDMASK) == GARDMSB)
900       {
901         if (quotient & (1 << NGARDS))
902           {
903             /* half way, so round to even */
904             quotient += GARDROUND + 1;
905           }
906         else if (numerator)
907           {
908             /* but we really weren't half way, more bits exist */
909             quotient += GARDROUND + 1;
910           }
911       }
912
913     a->fraction.ll = quotient;
914     return (a);
915   }
916 }
917
918 FLO_type
919 divide (FLO_type arg_a, FLO_type arg_b)
920 {
921   fp_number_type a;
922   fp_number_type b;
923   fp_number_type *res;
924   FLO_union_type au, bu;
925
926   au.value = arg_a;
927   bu.value = arg_b;
928
929   unpack_d (&au, &a);
930   unpack_d (&bu, &b);
931
932   res = _fpdiv_parts (&a, &b);
933
934   return pack_d (res);
935 }
936 #endif /* L_div_sf || L_div_df */
937
938 #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df) \
939     || defined(L_fpcmp_parts_tf)
940 /* according to the demo, fpcmp returns a comparison with 0... thus
941    a<b -> -1
942    a==b -> 0
943    a>b -> +1
944  */
945
946 int
947 __fpcmp_parts (fp_number_type * a, fp_number_type * b)
948 {
949 #if 0
950   /* either nan -> unordered. Must be checked outside of this routine.  */
951   if (isnan (a) && isnan (b))
952     {
953       return 1;                 /* still unordered! */
954     }
955 #endif
956
957   if (isnan (a) || isnan (b))
958     {
959       return 1;                 /* how to indicate unordered compare? */
960     }
961   if (isinf (a) && isinf (b))
962     {
963       /* +inf > -inf, but +inf != +inf */
964       /* b    \a| +inf(0)| -inf(1)
965        ______\+--------+--------
966        +inf(0)| a==b(0)| a<b(-1)
967        -------+--------+--------
968        -inf(1)| a>b(1) | a==b(0)
969        -------+--------+--------
970        So since unordered must be nonzero, just line up the columns...
971        */
972       return b->sign - a->sign;
973     }
974   /* but not both...  */
975   if (isinf (a))
976     {
977       return a->sign ? -1 : 1;
978     }
979   if (isinf (b))
980     {
981       return b->sign ? 1 : -1;
982     }
983   if (iszero (a) && iszero (b))
984     {
985       return 0;
986     }
987   if (iszero (a))
988     {
989       return b->sign ? 1 : -1;
990     }
991   if (iszero (b))
992     {
993       return a->sign ? -1 : 1;
994     }
995   /* now both are "normal".  */
996   if (a->sign != b->sign)
997     {
998       /* opposite signs */
999       return a->sign ? -1 : 1;
1000     }
1001   /* same sign; exponents? */
1002   if (a->normal_exp > b->normal_exp)
1003     {
1004       return a->sign ? -1 : 1;
1005     }
1006   if (a->normal_exp < b->normal_exp)
1007     {
1008       return a->sign ? 1 : -1;
1009     }
1010   /* same exponents; check size.  */
1011   if (a->fraction.ll > b->fraction.ll)
1012     {
1013       return a->sign ? -1 : 1;
1014     }
1015   if (a->fraction.ll < b->fraction.ll)
1016     {
1017       return a->sign ? 1 : -1;
1018     }
1019   /* after all that, they're equal.  */
1020   return 0;
1021 }
1022 #endif
1023
1024 #if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compoare_tf)
1025 CMPtype
1026 compare (FLO_type arg_a, FLO_type arg_b)
1027 {
1028   fp_number_type a;
1029   fp_number_type b;
1030   FLO_union_type au, bu;
1031
1032   au.value = arg_a;
1033   bu.value = arg_b;
1034
1035   unpack_d (&au, &a);
1036   unpack_d (&bu, &b);
1037
1038   return __fpcmp_parts (&a, &b);
1039 }
1040 #endif /* L_compare_sf || L_compare_df */
1041
1042 #ifndef US_SOFTWARE_GOFAST
1043
1044 /* These should be optimized for their specific tasks someday.  */
1045
1046 #if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
1047 CMPtype
1048 _eq_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;                   /* false, truth == 0 */
1062
1063   return __fpcmp_parts (&a, &b) ;
1064 }
1065 #endif /* L_eq_sf || L_eq_df */
1066
1067 #if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
1068 CMPtype
1069 _ne_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;                   /* true, truth != 0 */
1083
1084   return  __fpcmp_parts (&a, &b) ;
1085 }
1086 #endif /* L_ne_sf || L_ne_df */
1087
1088 #if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
1089 CMPtype
1090 _gt_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
1105   return __fpcmp_parts (&a, &b);
1106 }
1107 #endif /* L_gt_sf || L_gt_df */
1108
1109 #if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
1110 CMPtype
1111 _ge_f2 (FLO_type arg_a, FLO_type arg_b)
1112 {
1113   fp_number_type a;
1114   fp_number_type b;
1115   FLO_union_type au, bu;
1116
1117   au.value = arg_a;
1118   bu.value = arg_b;
1119
1120   unpack_d (&au, &a);
1121   unpack_d (&bu, &b);
1122
1123   if (isnan (&a) || isnan (&b))
1124     return -1;                  /* false, truth >= 0 */
1125   return __fpcmp_parts (&a, &b) ;
1126 }
1127 #endif /* L_ge_sf || L_ge_df */
1128
1129 #if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
1130 CMPtype
1131 _lt_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_lt_sf || L_lt_df */
1149
1150 #if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
1151 CMPtype
1152 _le_f2 (FLO_type arg_a, FLO_type arg_b)
1153 {
1154   fp_number_type a;
1155   fp_number_type b;
1156   FLO_union_type au, bu;
1157
1158   au.value = arg_a;
1159   bu.value = arg_b;
1160
1161   unpack_d (&au, &a);
1162   unpack_d (&bu, &b);
1163
1164   if (isnan (&a) || isnan (&b))
1165     return 1;                   /* false, truth <= 0 */
1166
1167   return __fpcmp_parts (&a, &b) ;
1168 }
1169 #endif /* L_le_sf || L_le_df */
1170
1171 #endif /* ! US_SOFTWARE_GOFAST */
1172
1173 #if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
1174 CMPtype
1175 _unord_f2 (FLO_type arg_a, FLO_type arg_b)
1176 {
1177   fp_number_type a;
1178   fp_number_type b;
1179   FLO_union_type au, bu;
1180
1181   au.value = arg_a;
1182   bu.value = arg_b;
1183
1184   unpack_d (&au, &a);
1185   unpack_d (&bu, &b);
1186
1187   return (isnan (&a) || isnan (&b));
1188 }
1189 #endif /* L_unord_sf || L_unord_df */
1190
1191 #if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
1192 FLO_type
1193 si_to_float (SItype arg_a)
1194 {
1195   fp_number_type in;
1196
1197   in.class = CLASS_NUMBER;
1198   in.sign = arg_a < 0;
1199   if (!arg_a)
1200     {
1201       in.class = CLASS_ZERO;
1202     }
1203   else
1204     {
1205       in.normal_exp = FRACBITS + NGARDS;
1206       if (in.sign) 
1207         {
1208           /* Special case for minint, since there is no +ve integer
1209              representation for it */
1210           if (arg_a == (- MAX_SI_INT - 1))
1211             {
1212               return (FLO_type)(- MAX_SI_INT - 1);
1213             }
1214           in.fraction.ll = (-arg_a);
1215         }
1216       else
1217         in.fraction.ll = arg_a;
1218
1219       while (in.fraction.ll < ((fractype)1 << (FRACBITS + NGARDS)))
1220         {
1221           in.fraction.ll <<= 1;
1222           in.normal_exp -= 1;
1223         }
1224     }
1225   return pack_d (&in);
1226 }
1227 #endif /* L_si_to_sf || L_si_to_df */
1228
1229 #if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
1230 FLO_type
1231 usi_to_float (USItype arg_a)
1232 {
1233   fp_number_type in;
1234
1235   in.sign = 0;
1236   if (!arg_a)
1237     {
1238       in.class = CLASS_ZERO;
1239     }
1240   else
1241     {
1242       in.class = CLASS_NUMBER;
1243       in.normal_exp = FRACBITS + NGARDS;
1244       in.fraction.ll = arg_a;
1245
1246       while (in.fraction.ll > ((fractype)1 << (FRACBITS + NGARDS)))
1247         {
1248           in.fraction.ll >>= 1;
1249           in.normal_exp += 1;
1250         }
1251       while (in.fraction.ll < ((fractype)1 << (FRACBITS + NGARDS)))
1252         {
1253           in.fraction.ll <<= 1;
1254           in.normal_exp -= 1;
1255         }
1256     }
1257   return pack_d (&in);
1258 }
1259 #endif
1260
1261 #if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
1262 SItype
1263 float_to_si (FLO_type arg_a)
1264 {
1265   fp_number_type a;
1266   SItype tmp;
1267   FLO_union_type au;
1268
1269   au.value = arg_a;
1270   unpack_d (&au, &a);
1271
1272   if (iszero (&a))
1273     return 0;
1274   if (isnan (&a))
1275     return 0;
1276   /* get reasonable MAX_SI_INT...  */
1277   if (isinf (&a))
1278     return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1279   /* it is a number, but a small one */
1280   if (a.normal_exp < 0)
1281     return 0;
1282   if (a.normal_exp > BITS_PER_SI - 2)
1283     return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1284   tmp = a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1285   return a.sign ? (-tmp) : (tmp);
1286 }
1287 #endif /* L_sf_to_si || L_df_to_si */
1288
1289 #if defined(L_sf_to_usi) || defined(L_df_to_usi) || defined(L_tf_to_usi)
1290 #if defined US_SOFTWARE_GOFAST || defined(L_tf_to_usi)
1291 /* While libgcc2.c defines its own __fixunssfsi and __fixunsdfsi routines,
1292    we also define them for GOFAST because the ones in libgcc2.c have the
1293    wrong names and I'd rather define these here and keep GOFAST CYG-LOC's
1294    out of libgcc2.c.  We can't define these here if not GOFAST because then
1295    there'd be duplicate copies.  */
1296
1297 USItype
1298 float_to_usi (FLO_type arg_a)
1299 {
1300   fp_number_type a;
1301   FLO_union_type au;
1302
1303   au.value = arg_a;
1304   unpack_d (&au, &a);
1305
1306   if (iszero (&a))
1307     return 0;
1308   if (isnan (&a))
1309     return 0;
1310   /* it is a negative number */
1311   if (a.sign)
1312     return 0;
1313   /* get reasonable MAX_USI_INT...  */
1314   if (isinf (&a))
1315     return MAX_USI_INT;
1316   /* it is a number, but a small one */
1317   if (a.normal_exp < 0)
1318     return 0;
1319   if (a.normal_exp > BITS_PER_SI - 1)
1320     return MAX_USI_INT;
1321   else if (a.normal_exp > (FRACBITS + NGARDS))
1322     return a.fraction.ll << (a.normal_exp - (FRACBITS + NGARDS));
1323   else
1324     return a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1325 }
1326 #endif /* US_SOFTWARE_GOFAST */
1327 #endif /* L_sf_to_usi || L_df_to_usi */
1328
1329 #if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
1330 FLO_type
1331 negate (FLO_type arg_a)
1332 {
1333   fp_number_type a;
1334   FLO_union_type au;
1335
1336   au.value = arg_a;
1337   unpack_d (&au, &a);
1338
1339   flip_sign (&a);
1340   return pack_d (&a);
1341 }
1342 #endif /* L_negate_sf || L_negate_df */
1343
1344 #ifdef FLOAT
1345
1346 #if defined(L_make_sf)
1347 SFtype
1348 __make_fp(fp_class_type class,
1349              unsigned int sign,
1350              int exp, 
1351              USItype frac)
1352 {
1353   fp_number_type in;
1354
1355   in.class = class;
1356   in.sign = sign;
1357   in.normal_exp = exp;
1358   in.fraction.ll = frac;
1359   return pack_d (&in);
1360 }
1361 #endif /* L_make_sf */
1362
1363 #ifndef FLOAT_ONLY
1364
1365 /* This enables one to build an fp library that supports float but not double.
1366    Otherwise, we would get an undefined reference to __make_dp.
1367    This is needed for some 8-bit ports that can't handle well values that
1368    are 8-bytes in size, so we just don't support double for them at all.  */
1369
1370 #if defined(L_sf_to_df)
1371 DFtype
1372 sf_to_df (SFtype arg_a)
1373 {
1374   fp_number_type in;
1375   FLO_union_type au;
1376
1377   au.value = arg_a;
1378   unpack_d (&au, &in);
1379
1380   return __make_dp (in.class, in.sign, in.normal_exp,
1381                     ((UDItype) in.fraction.ll) << F_D_BITOFF);
1382 }
1383 #endif /* L_sf_to_df */
1384
1385 #if defined(L_sf_to_tf) && defined(TMODES)
1386 TFtype
1387 sf_to_tf (SFtype arg_a)
1388 {
1389   fp_number_type in;
1390   FLO_union_type au;
1391
1392   au.value = arg_a;
1393   unpack_d (&au, &in);
1394
1395   return __make_tp (in.class, in.sign, in.normal_exp,
1396                     ((UTItype) in.fraction.ll) << F_T_BITOFF);
1397 }
1398 #endif /* L_sf_to_df */
1399
1400 #endif /* ! FLOAT_ONLY */
1401 #endif /* FLOAT */
1402
1403 #ifndef FLOAT
1404
1405 extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
1406
1407 #if defined(L_make_df)
1408 DFtype
1409 __make_dp (fp_class_type class, unsigned int sign, int exp, UDItype frac)
1410 {
1411   fp_number_type in;
1412
1413   in.class = class;
1414   in.sign = sign;
1415   in.normal_exp = exp;
1416   in.fraction.ll = frac;
1417   return pack_d (&in);
1418 }
1419 #endif /* L_make_df */
1420
1421 #if defined(L_df_to_sf)
1422 SFtype
1423 df_to_sf (DFtype arg_a)
1424 {
1425   fp_number_type in;
1426   USItype sffrac;
1427   FLO_union_type au;
1428
1429   au.value = arg_a;
1430   unpack_d (&au, &in);
1431
1432   sffrac = in.fraction.ll >> F_D_BITOFF;
1433
1434   /* We set the lowest guard bit in SFFRAC if we discarded any non
1435      zero bits.  */
1436   if ((in.fraction.ll & (((USItype) 1 << F_D_BITOFF) - 1)) != 0)
1437     sffrac |= 1;
1438
1439   return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
1440 }
1441 #endif /* L_df_to_sf */
1442
1443 #if defined(L_df_to_tf) && defined(TMODES) \
1444     && !defined(FLOAT) && !defined(TFLOAT)
1445 TFtype
1446 df_to_tf (DFtype arg_a)
1447 {
1448   fp_number_type in;
1449   FLO_union_type au;
1450
1451   au.value = arg_a;
1452   unpack_d (&au, &in);
1453
1454   return __make_tp (in.class, in.sign, in.normal_exp,
1455                     ((UTItype) in.fraction.ll) << D_T_BITOFF);
1456 }
1457 #endif /* L_sf_to_df */
1458
1459 #ifdef TFLOAT
1460 #if defined(L_make_tf)
1461 TFtype
1462 __make_tp(fp_class_type class,
1463              unsigned int sign,
1464              int exp, 
1465              UTItype frac)
1466 {
1467   fp_number_type in;
1468
1469   in.class = class;
1470   in.sign = sign;
1471   in.normal_exp = exp;
1472   in.fraction.ll = frac;
1473   return pack_d (&in);
1474 }
1475 #endif /* L_make_tf */
1476
1477 #if defined(L_tf_to_df)
1478 DFtype
1479 tf_to_df (TFtype arg_a)
1480 {
1481   fp_number_type in;
1482   UDItype sffrac;
1483   FLO_union_type au;
1484
1485   au.value = arg_a;
1486   unpack_d (&au, &in);
1487
1488   sffrac = in.fraction.ll >> D_T_BITOFF;
1489
1490   /* We set the lowest guard bit in SFFRAC if we discarded any non
1491      zero bits.  */
1492   if ((in.fraction.ll & (((UTItype) 1 << D_T_BITOFF) - 1)) != 0)
1493     sffrac |= 1;
1494
1495   return __make_dp (in.class, in.sign, in.normal_exp, sffrac);
1496 }
1497 #endif /* L_tf_to_df */
1498
1499 #if defined(L_tf_to_sf)
1500 SFtype
1501 tf_to_sf (TFtype arg_a)
1502 {
1503   fp_number_type in;
1504   USItype sffrac;
1505   FLO_union_type au;
1506
1507   au.value = arg_a;
1508   unpack_d (&au, &in);
1509
1510   sffrac = in.fraction.ll >> F_T_BITOFF;
1511
1512   /* We set the lowest guard bit in SFFRAC if we discarded any non
1513      zero bits.  */
1514   if ((in.fraction.ll & (((UTItype) 1 << F_T_BITOFF) - 1)) != 0)
1515     sffrac |= 1;
1516
1517   return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
1518 }
1519 #endif /* L_tf_to_sf */
1520 #endif /* TFLOAT */
1521
1522 #endif /* ! FLOAT */
1523 #endif /* !EXTENDED_FLOAT_STUBS */