Improve support for arm-wince-pe target:
[official-gcc.git] / gcc / config / fp-bit.c
blobb8ac2982cdd734573e2b99f29a0f90e064d18d51
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.
6 This file is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file with other programs, and to distribute
14 those programs without any restriction coming from the use of this
15 file. (The General Public License restrictions do apply in other
16 respects; for example, they cover modification of the file, and
17 distribution when not linked into another program.)
19 This file is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; see the file COPYING. If not, write to
26 the Free Software Foundation, 59 Temple Place - Suite 330,
27 Boston, MA 02111-1307, USA. */
29 /* As a special exception, if you link this library with other files,
30 some of which are compiled with GCC, to produce an executable,
31 this library does not by itself cause the resulting executable
32 to be covered by the GNU General Public License.
33 This exception does not however invalidate any other reasons why
34 the executable file might be covered by the GNU General Public License. */
36 /* This implements IEEE 754 format arithmetic, but does not provide a
37 mechanism for setting the rounding mode, or for generating or handling
38 exceptions.
40 The original code by Steve Chamberlain, hacked by Mark Eichin and Jim
41 Wilson, all of Cygnus Support. */
43 /* The intended way to use this file is to make two copies, add `#define FLOAT'
44 to one copy, then compile both copies and add them to libgcc.a. */
46 #include "tconfig.h"
47 #include "coretypes.h"
48 #include "tm.h"
49 #include "fp-bit.h"
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 */
69 /* We don't currently support extended floats (long doubles) on machines
70 without hardware to deal with them.
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. */
79 #ifdef DECLARE_LIBRARY_RENAMES
80 DECLARE_LIBRARY_RENAMES
81 #endif
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(); }
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 */
122 /* IEEE "special" number predicates */
124 #ifdef NO_NANS
126 #define nan() 0
127 #define isnan(x) 0
128 #define isinf(x) 0
129 #else
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
145 INLINE
146 static fp_number_type *
147 nan (void)
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
159 INLINE
160 static int
161 isnan ( fp_number_type * x)
163 return x->class == CLASS_SNAN || x->class == CLASS_QNAN;
166 INLINE
167 static int
168 isinf ( fp_number_type * x)
170 return x->class == CLASS_INFINITY;
173 #endif /* NO_NANS */
175 INLINE
176 static int
177 iszero ( fp_number_type * x)
179 return x->class == CLASS_ZERO;
182 INLINE
183 static void
184 flip_sign ( fp_number_type * x)
186 x->sign = !x->sign;
189 extern FLO_type pack_d ( fp_number_type * );
191 #if defined(L_pack_df) || defined(L_pack_sf) || defined(L_pack_tf)
192 FLO_type
193 pack_d ( fp_number_type * src)
195 FLO_union_type dst;
196 fractype fraction = src->fraction.ll; /* wasn't unsigned before? */
197 int sign = src->sign;
198 int exp = 0;
200 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && (isnan (src) || isinf (src)))
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;
208 else if (isnan (src))
210 exp = EXPMAX;
211 if (src->class == CLASS_QNAN || 1)
213 #ifdef QUIET_NAN_NEGATED
214 fraction |= QUIET_NAN - 1;
215 #else
216 fraction |= QUIET_NAN;
217 #endif
220 else if (isinf (src))
222 exp = EXPMAX;
223 fraction = 0;
225 else if (iszero (src))
227 exp = 0;
228 fraction = 0;
230 else if (fraction == 0)
232 exp = 0;
234 else
236 if (src->normal_exp < NORMAL_EXPMIN)
238 #ifdef NO_DENORMALS
239 /* Go straight to a zero representation if denormals are not
240 supported. The denormal handling would be harmless but
241 isn't unnecessary. */
242 exp = 0;
243 fraction = 0;
244 #else /* NO_DENORMALS */
245 /* This number's exponent is too low to fit into the bits
246 available in the number, so we'll store 0 in the exponent and
247 shift the fraction to the right to make up for it. */
249 int shift = NORMAL_EXPMIN - src->normal_exp;
251 exp = 0;
253 if (shift > FRAC_NBITS - NGARDS)
255 /* No point shifting, since it's more that 64 out. */
256 fraction = 0;
258 else
260 int lowbit = (fraction & (((fractype)1 << shift) - 1)) ? 1 : 0;
261 fraction = (fraction >> shift) | lowbit;
263 if ((fraction & GARDMASK) == GARDMSB)
265 if ((fraction & (1 << NGARDS)))
266 fraction += GARDROUND + 1;
268 else
270 /* Add to the guards to round up. */
271 fraction += GARDROUND;
273 /* Perhaps the rounding means we now need to change the
274 exponent, because the fraction is no longer denormal. */
275 if (fraction >= IMPLICIT_1)
277 exp += 1;
279 fraction >>= NGARDS;
280 #endif /* NO_DENORMALS */
282 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS)
283 && src->normal_exp > EXPBIAS)
285 exp = EXPMAX;
286 fraction = 0;
288 else
290 exp = src->normal_exp + EXPBIAS;
291 if (!ROUND_TOWARDS_ZERO)
293 /* IF the gard bits are the all zero, but the first, then we're
294 half way between two numbers, choose the one which makes the
295 lsb of the answer 0. */
296 if ((fraction & GARDMASK) == GARDMSB)
298 if (fraction & (1 << NGARDS))
299 fraction += GARDROUND + 1;
301 else
303 /* Add a one to the guards to round up */
304 fraction += GARDROUND;
306 if (fraction >= IMPLICIT_2)
308 fraction >>= 1;
309 exp += 1;
312 fraction >>= NGARDS;
314 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && exp > EXPMAX)
316 /* Saturate on overflow. */
317 exp = EXPMAX;
318 fraction = ((fractype) 1 << FRACBITS) - 1;
323 /* We previously used bitfields to store the number, but this doesn't
324 handle little/big endian systems conveniently, so use shifts and
325 masks */
326 #ifdef FLOAT_BIT_ORDER_MISMATCH
327 dst.bits.fraction = fraction;
328 dst.bits.exp = exp;
329 dst.bits.sign = sign;
330 #else
331 # if defined TFLOAT && defined HALFFRACBITS
333 halffractype high, low;
335 high = (fraction >> (FRACBITS - HALFFRACBITS));
336 high &= (((fractype)1) << HALFFRACBITS) - 1;
337 high |= ((fractype) (exp & ((1 << EXPBITS) - 1))) << HALFFRACBITS;
338 high |= ((fractype) (sign & 1)) << (HALFFRACBITS | EXPBITS);
340 low = (halffractype)fraction &
341 ((((halffractype)1) << (FRACBITS - HALFFRACBITS)) - 1);
343 if (exp == EXPMAX || exp == 0 || low == 0)
344 low = 0;
345 else
347 exp -= HALFFRACBITS + 1;
349 while (exp > 0
350 && low < ((halffractype)1 << HALFFRACBITS))
352 low <<= 1;
353 exp--;
356 if (exp <= 0)
358 halffractype roundmsb, round;
360 exp = -exp + 1;
362 roundmsb = (1 << (exp - 1));
363 round = low & ((roundmsb << 1) - 1);
365 low >>= exp;
366 exp = 0;
368 if (round > roundmsb || (round == roundmsb && (low & 1)))
370 low++;
371 if (low >= ((halffractype)1 << HALFFRACBITS))
372 /* We don't shift left, since it has just become the
373 smallest normal number, whose implicit 1 bit is
374 now indicated by the non-zero exponent. */
375 exp++;
379 low &= ((halffractype)1 << HALFFRACBITS) - 1;
380 low |= ((fractype) (exp & ((1 << EXPBITS) - 1))) << HALFFRACBITS;
381 low |= ((fractype) (sign & 1)) << (HALFFRACBITS | EXPBITS);
384 dst.value_raw = (((fractype) high) << HALFSHIFT) | low;
386 # else
387 dst.value_raw = fraction & ((((fractype)1) << FRACBITS) - (fractype)1);
388 dst.value_raw |= ((fractype) (exp & ((1 << EXPBITS) - 1))) << FRACBITS;
389 dst.value_raw |= ((fractype) (sign & 1)) << (FRACBITS | EXPBITS);
390 # endif
391 #endif
393 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
394 #ifdef TFLOAT
396 qrtrfractype tmp1 = dst.words[0];
397 qrtrfractype tmp2 = dst.words[1];
398 dst.words[0] = dst.words[3];
399 dst.words[1] = dst.words[2];
400 dst.words[2] = tmp2;
401 dst.words[3] = tmp1;
403 #else
405 halffractype tmp = dst.words[0];
406 dst.words[0] = dst.words[1];
407 dst.words[1] = tmp;
409 #endif
410 #endif
412 return dst.value;
414 #endif
416 #if defined(L_unpack_df) || defined(L_unpack_sf) || defined(L_unpack_tf)
417 void
418 unpack_d (FLO_union_type * src, fp_number_type * dst)
420 /* We previously used bitfields to store the number, but this doesn't
421 handle little/big endian systems conveniently, so use shifts and
422 masks */
423 fractype fraction;
424 int exp;
425 int sign;
427 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
428 FLO_union_type swapped;
430 #ifdef TFLOAT
431 swapped.words[0] = src->words[3];
432 swapped.words[1] = src->words[2];
433 swapped.words[2] = src->words[1];
434 swapped.words[3] = src->words[0];
435 #else
436 swapped.words[0] = src->words[1];
437 swapped.words[1] = src->words[0];
438 #endif
439 src = &swapped;
440 #endif
442 #ifdef FLOAT_BIT_ORDER_MISMATCH
443 fraction = src->bits.fraction;
444 exp = src->bits.exp;
445 sign = src->bits.sign;
446 #else
447 # if defined TFLOAT && defined HALFFRACBITS
449 halffractype high, low;
451 high = src->value_raw >> HALFSHIFT;
452 low = src->value_raw & (((fractype)1 << HALFSHIFT) - 1);
454 fraction = high & ((((fractype)1) << HALFFRACBITS) - 1);
455 fraction <<= FRACBITS - HALFFRACBITS;
456 exp = ((int)(high >> HALFFRACBITS)) & ((1 << EXPBITS) - 1);
457 sign = ((int)(high >> (((HALFFRACBITS + EXPBITS))))) & 1;
459 if (exp != EXPMAX && exp != 0 && low != 0)
461 int lowexp = ((int)(low >> HALFFRACBITS)) & ((1 << EXPBITS) - 1);
462 int lowsign = ((int)(low >> (((HALFFRACBITS + EXPBITS))))) & 1;
463 int shift;
464 fractype xlow;
466 xlow = low & ((((fractype)1) << HALFFRACBITS) - 1);
467 if (lowexp)
468 xlow |= (((halffractype)1) << HALFFRACBITS);
469 else
470 lowexp = 1;
471 shift = (FRACBITS - HALFFRACBITS) - (exp - lowexp);
472 if (shift > 0)
473 xlow <<= shift;
474 else if (shift < 0)
475 xlow >>= -shift;
476 if (sign == lowsign)
477 fraction += xlow;
478 else
479 fraction -= xlow;
482 # else
483 fraction = src->value_raw & ((((fractype)1) << FRACBITS) - 1);
484 exp = ((int)(src->value_raw >> FRACBITS)) & ((1 << EXPBITS) - 1);
485 sign = ((int)(src->value_raw >> (FRACBITS + EXPBITS))) & 1;
486 # endif
487 #endif
489 dst->sign = sign;
490 if (exp == 0)
492 /* Hmm. Looks like 0 */
493 if (fraction == 0
494 #ifdef NO_DENORMALS
495 || 1
496 #endif
499 /* tastes like zero */
500 dst->class = CLASS_ZERO;
502 else
504 /* Zero exponent with nonzero fraction - it's denormalized,
505 so there isn't a leading implicit one - we'll shift it so
506 it gets one. */
507 dst->normal_exp = exp - EXPBIAS + 1;
508 fraction <<= NGARDS;
510 dst->class = CLASS_NUMBER;
511 #if 1
512 while (fraction < IMPLICIT_1)
514 fraction <<= 1;
515 dst->normal_exp--;
517 #endif
518 dst->fraction.ll = fraction;
521 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && exp == EXPMAX)
523 /* Huge exponent*/
524 if (fraction == 0)
526 /* Attached to a zero fraction - means infinity */
527 dst->class = CLASS_INFINITY;
529 else
531 /* Nonzero fraction, means nan */
532 #ifdef QUIET_NAN_NEGATED
533 if ((fraction & QUIET_NAN) == 0)
534 #else
535 if (fraction & QUIET_NAN)
536 #endif
538 dst->class = CLASS_QNAN;
540 else
542 dst->class = CLASS_SNAN;
544 /* Keep the fraction part as the nan number */
545 dst->fraction.ll = fraction;
548 else
550 /* Nothing strange about this number */
551 dst->normal_exp = exp - EXPBIAS;
552 dst->class = CLASS_NUMBER;
553 dst->fraction.ll = (fraction << NGARDS) | IMPLICIT_1;
556 #endif /* L_unpack_df || L_unpack_sf */
558 #if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
559 static fp_number_type *
560 _fpadd_parts (fp_number_type * a,
561 fp_number_type * b,
562 fp_number_type * tmp)
564 intfrac tfraction;
566 /* Put commonly used fields in local variables. */
567 int a_normal_exp;
568 int b_normal_exp;
569 fractype a_fraction;
570 fractype b_fraction;
572 if (isnan (a))
574 return a;
576 if (isnan (b))
578 return b;
580 if (isinf (a))
582 /* Adding infinities with opposite signs yields a NaN. */
583 if (isinf (b) && a->sign != b->sign)
584 return nan ();
585 return a;
587 if (isinf (b))
589 return b;
591 if (iszero (b))
593 if (iszero (a))
595 *tmp = *a;
596 tmp->sign = a->sign & b->sign;
597 return tmp;
599 return a;
601 if (iszero (a))
603 return b;
606 /* Got two numbers. shift the smaller and increment the exponent till
607 they're the same */
609 int diff;
611 a_normal_exp = a->normal_exp;
612 b_normal_exp = b->normal_exp;
613 a_fraction = a->fraction.ll;
614 b_fraction = b->fraction.ll;
616 diff = a_normal_exp - b_normal_exp;
618 if (diff < 0)
619 diff = -diff;
620 if (diff < FRAC_NBITS)
622 /* ??? This does shifts one bit at a time. Optimize. */
623 while (a_normal_exp > b_normal_exp)
625 b_normal_exp++;
626 LSHIFT (b_fraction);
628 while (b_normal_exp > a_normal_exp)
630 a_normal_exp++;
631 LSHIFT (a_fraction);
634 else
636 /* Somethings's up.. choose the biggest */
637 if (a_normal_exp > b_normal_exp)
639 b_normal_exp = a_normal_exp;
640 b_fraction = 0;
642 else
644 a_normal_exp = b_normal_exp;
645 a_fraction = 0;
650 if (a->sign != b->sign)
652 if (a->sign)
654 tfraction = -a_fraction + b_fraction;
656 else
658 tfraction = a_fraction - b_fraction;
660 if (tfraction >= 0)
662 tmp->sign = 0;
663 tmp->normal_exp = a_normal_exp;
664 tmp->fraction.ll = tfraction;
666 else
668 tmp->sign = 1;
669 tmp->normal_exp = a_normal_exp;
670 tmp->fraction.ll = -tfraction;
672 /* and renormalize it */
674 while (tmp->fraction.ll < IMPLICIT_1 && tmp->fraction.ll)
676 tmp->fraction.ll <<= 1;
677 tmp->normal_exp--;
680 else
682 tmp->sign = a->sign;
683 tmp->normal_exp = a_normal_exp;
684 tmp->fraction.ll = a_fraction + b_fraction;
686 tmp->class = CLASS_NUMBER;
687 /* Now the fraction is added, we have to shift down to renormalize the
688 number */
690 if (tmp->fraction.ll >= IMPLICIT_2)
692 LSHIFT (tmp->fraction.ll);
693 tmp->normal_exp++;
695 return tmp;
699 FLO_type
700 add (FLO_type arg_a, FLO_type arg_b)
702 fp_number_type a;
703 fp_number_type b;
704 fp_number_type tmp;
705 fp_number_type *res;
706 FLO_union_type au, bu;
708 au.value = arg_a;
709 bu.value = arg_b;
711 unpack_d (&au, &a);
712 unpack_d (&bu, &b);
714 res = _fpadd_parts (&a, &b, &tmp);
716 return pack_d (res);
719 FLO_type
720 sub (FLO_type arg_a, FLO_type arg_b)
722 fp_number_type a;
723 fp_number_type b;
724 fp_number_type tmp;
725 fp_number_type *res;
726 FLO_union_type au, bu;
728 au.value = arg_a;
729 bu.value = arg_b;
731 unpack_d (&au, &a);
732 unpack_d (&bu, &b);
734 b.sign ^= 1;
736 res = _fpadd_parts (&a, &b, &tmp);
738 return pack_d (res);
740 #endif /* L_addsub_sf || L_addsub_df */
742 #if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
743 static inline __attribute__ ((__always_inline__)) fp_number_type *
744 _fpmul_parts ( fp_number_type * a,
745 fp_number_type * b,
746 fp_number_type * tmp)
748 fractype low = 0;
749 fractype high = 0;
751 if (isnan (a))
753 a->sign = a->sign != b->sign;
754 return a;
756 if (isnan (b))
758 b->sign = a->sign != b->sign;
759 return b;
761 if (isinf (a))
763 if (iszero (b))
764 return nan ();
765 a->sign = a->sign != b->sign;
766 return a;
768 if (isinf (b))
770 if (iszero (a))
772 return nan ();
774 b->sign = a->sign != b->sign;
775 return b;
777 if (iszero (a))
779 a->sign = a->sign != b->sign;
780 return a;
782 if (iszero (b))
784 b->sign = a->sign != b->sign;
785 return b;
788 /* Calculate the mantissa by multiplying both numbers to get a
789 twice-as-wide number. */
791 #if defined(NO_DI_MODE) || defined(TFLOAT)
793 fractype x = a->fraction.ll;
794 fractype ylow = b->fraction.ll;
795 fractype yhigh = 0;
796 int bit;
798 /* ??? This does multiplies one bit at a time. Optimize. */
799 for (bit = 0; bit < FRAC_NBITS; bit++)
801 int carry;
803 if (x & 1)
805 carry = (low += ylow) < ylow;
806 high += yhigh + carry;
808 yhigh <<= 1;
809 if (ylow & FRACHIGH)
811 yhigh |= 1;
813 ylow <<= 1;
814 x >>= 1;
817 #elif defined(FLOAT)
818 /* Multiplying two USIs to get a UDI, we're safe. */
820 UDItype answer = (UDItype)a->fraction.ll * (UDItype)b->fraction.ll;
822 high = answer >> BITS_PER_SI;
823 low = answer;
825 #else
826 /* fractype is DImode, but we need the result to be twice as wide.
827 Assuming a widening multiply from DImode to TImode is not
828 available, build one by hand. */
830 USItype nl = a->fraction.ll;
831 USItype nh = a->fraction.ll >> BITS_PER_SI;
832 USItype ml = b->fraction.ll;
833 USItype mh = b->fraction.ll >> BITS_PER_SI;
834 UDItype pp_ll = (UDItype) ml * nl;
835 UDItype pp_hl = (UDItype) mh * nl;
836 UDItype pp_lh = (UDItype) ml * nh;
837 UDItype pp_hh = (UDItype) mh * nh;
838 UDItype res2 = 0;
839 UDItype res0 = 0;
840 UDItype ps_hh__ = pp_hl + pp_lh;
841 if (ps_hh__ < pp_hl)
842 res2 += (UDItype)1 << BITS_PER_SI;
843 pp_hl = (UDItype)(USItype)ps_hh__ << BITS_PER_SI;
844 res0 = pp_ll + pp_hl;
845 if (res0 < pp_ll)
846 res2++;
847 res2 += (ps_hh__ >> BITS_PER_SI) + pp_hh;
848 high = res2;
849 low = res0;
851 #endif
854 tmp->normal_exp = a->normal_exp + b->normal_exp
855 + FRAC_NBITS - (FRACBITS + NGARDS);
856 tmp->sign = a->sign != b->sign;
857 while (high >= IMPLICIT_2)
859 tmp->normal_exp++;
860 if (high & 1)
862 low >>= 1;
863 low |= FRACHIGH;
865 high >>= 1;
867 while (high < IMPLICIT_1)
869 tmp->normal_exp--;
871 high <<= 1;
872 if (low & FRACHIGH)
873 high |= 1;
874 low <<= 1;
876 /* rounding is tricky. if we only round if it won't make us round later. */
877 #if 0
878 if (low & FRACHIGH2)
880 if (((high & GARDMASK) != GARDMSB)
881 && (((high + 1) & GARDMASK) == GARDMSB))
883 /* don't round, it gets done again later. */
885 else
887 high++;
890 #endif
891 if (!ROUND_TOWARDS_ZERO && (high & GARDMASK) == GARDMSB)
893 if (high & (1 << NGARDS))
895 /* half way, so round to even */
896 high += GARDROUND + 1;
898 else if (low)
900 /* but we really weren't half way */
901 high += GARDROUND + 1;
904 tmp->fraction.ll = high;
905 tmp->class = CLASS_NUMBER;
906 return tmp;
909 FLO_type
910 multiply (FLO_type arg_a, FLO_type arg_b)
912 fp_number_type a;
913 fp_number_type b;
914 fp_number_type tmp;
915 fp_number_type *res;
916 FLO_union_type au, bu;
918 au.value = arg_a;
919 bu.value = arg_b;
921 unpack_d (&au, &a);
922 unpack_d (&bu, &b);
924 res = _fpmul_parts (&a, &b, &tmp);
926 return pack_d (res);
928 #endif /* L_mul_sf || L_mul_df */
930 #if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
931 static inline __attribute__ ((__always_inline__)) fp_number_type *
932 _fpdiv_parts (fp_number_type * a,
933 fp_number_type * b)
935 fractype bit;
936 fractype numerator;
937 fractype denominator;
938 fractype quotient;
940 if (isnan (a))
942 return a;
944 if (isnan (b))
946 return b;
949 a->sign = a->sign ^ b->sign;
951 if (isinf (a) || iszero (a))
953 if (a->class == b->class)
954 return nan ();
955 return a;
958 if (isinf (b))
960 a->fraction.ll = 0;
961 a->normal_exp = 0;
962 return a;
964 if (iszero (b))
966 a->class = CLASS_INFINITY;
967 return a;
970 /* Calculate the mantissa by multiplying both 64bit numbers to get a
971 128 bit number */
973 /* quotient =
974 ( numerator / denominator) * 2^(numerator exponent - denominator exponent)
977 a->normal_exp = a->normal_exp - b->normal_exp;
978 numerator = a->fraction.ll;
979 denominator = b->fraction.ll;
981 if (numerator < denominator)
983 /* Fraction will be less than 1.0 */
984 numerator *= 2;
985 a->normal_exp--;
987 bit = IMPLICIT_1;
988 quotient = 0;
989 /* ??? Does divide one bit at a time. Optimize. */
990 while (bit)
992 if (numerator >= denominator)
994 quotient |= bit;
995 numerator -= denominator;
997 bit >>= 1;
998 numerator *= 2;
1001 if (!ROUND_TOWARDS_ZERO && (quotient & GARDMASK) == GARDMSB)
1003 if (quotient & (1 << NGARDS))
1005 /* half way, so round to even */
1006 quotient += GARDROUND + 1;
1008 else if (numerator)
1010 /* but we really weren't half way, more bits exist */
1011 quotient += GARDROUND + 1;
1015 a->fraction.ll = quotient;
1016 return (a);
1020 FLO_type
1021 divide (FLO_type arg_a, FLO_type arg_b)
1023 fp_number_type a;
1024 fp_number_type b;
1025 fp_number_type *res;
1026 FLO_union_type au, bu;
1028 au.value = arg_a;
1029 bu.value = arg_b;
1031 unpack_d (&au, &a);
1032 unpack_d (&bu, &b);
1034 res = _fpdiv_parts (&a, &b);
1036 return pack_d (res);
1038 #endif /* L_div_sf || L_div_df */
1040 #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df) \
1041 || defined(L_fpcmp_parts_tf)
1042 /* according to the demo, fpcmp returns a comparison with 0... thus
1043 a<b -> -1
1044 a==b -> 0
1045 a>b -> +1
1049 __fpcmp_parts (fp_number_type * a, fp_number_type * b)
1051 #if 0
1052 /* either nan -> unordered. Must be checked outside of this routine. */
1053 if (isnan (a) && isnan (b))
1055 return 1; /* still unordered! */
1057 #endif
1059 if (isnan (a) || isnan (b))
1061 return 1; /* how to indicate unordered compare? */
1063 if (isinf (a) && isinf (b))
1065 /* +inf > -inf, but +inf != +inf */
1066 /* b \a| +inf(0)| -inf(1)
1067 ______\+--------+--------
1068 +inf(0)| a==b(0)| a<b(-1)
1069 -------+--------+--------
1070 -inf(1)| a>b(1) | a==b(0)
1071 -------+--------+--------
1072 So since unordered must be nonzero, just line up the columns...
1074 return b->sign - a->sign;
1076 /* but not both... */
1077 if (isinf (a))
1079 return a->sign ? -1 : 1;
1081 if (isinf (b))
1083 return b->sign ? 1 : -1;
1085 if (iszero (a) && iszero (b))
1087 return 0;
1089 if (iszero (a))
1091 return b->sign ? 1 : -1;
1093 if (iszero (b))
1095 return a->sign ? -1 : 1;
1097 /* now both are "normal". */
1098 if (a->sign != b->sign)
1100 /* opposite signs */
1101 return a->sign ? -1 : 1;
1103 /* same sign; exponents? */
1104 if (a->normal_exp > b->normal_exp)
1106 return a->sign ? -1 : 1;
1108 if (a->normal_exp < b->normal_exp)
1110 return a->sign ? 1 : -1;
1112 /* same exponents; check size. */
1113 if (a->fraction.ll > b->fraction.ll)
1115 return a->sign ? -1 : 1;
1117 if (a->fraction.ll < b->fraction.ll)
1119 return a->sign ? 1 : -1;
1121 /* after all that, they're equal. */
1122 return 0;
1124 #endif
1126 #if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compoare_tf)
1127 CMPtype
1128 compare (FLO_type arg_a, FLO_type arg_b)
1130 fp_number_type a;
1131 fp_number_type b;
1132 FLO_union_type au, bu;
1134 au.value = arg_a;
1135 bu.value = arg_b;
1137 unpack_d (&au, &a);
1138 unpack_d (&bu, &b);
1140 return __fpcmp_parts (&a, &b);
1142 #endif /* L_compare_sf || L_compare_df */
1144 #ifndef US_SOFTWARE_GOFAST
1146 /* These should be optimized for their specific tasks someday. */
1148 #if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
1149 CMPtype
1150 _eq_f2 (FLO_type arg_a, FLO_type arg_b)
1152 fp_number_type a;
1153 fp_number_type b;
1154 FLO_union_type au, bu;
1156 au.value = arg_a;
1157 bu.value = arg_b;
1159 unpack_d (&au, &a);
1160 unpack_d (&bu, &b);
1162 if (isnan (&a) || isnan (&b))
1163 return 1; /* false, truth == 0 */
1165 return __fpcmp_parts (&a, &b) ;
1167 #endif /* L_eq_sf || L_eq_df */
1169 #if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
1170 CMPtype
1171 _ne_f2 (FLO_type arg_a, FLO_type arg_b)
1173 fp_number_type a;
1174 fp_number_type b;
1175 FLO_union_type au, bu;
1177 au.value = arg_a;
1178 bu.value = arg_b;
1180 unpack_d (&au, &a);
1181 unpack_d (&bu, &b);
1183 if (isnan (&a) || isnan (&b))
1184 return 1; /* true, truth != 0 */
1186 return __fpcmp_parts (&a, &b) ;
1188 #endif /* L_ne_sf || L_ne_df */
1190 #if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
1191 CMPtype
1192 _gt_f2 (FLO_type arg_a, FLO_type arg_b)
1194 fp_number_type a;
1195 fp_number_type b;
1196 FLO_union_type au, bu;
1198 au.value = arg_a;
1199 bu.value = arg_b;
1201 unpack_d (&au, &a);
1202 unpack_d (&bu, &b);
1204 if (isnan (&a) || isnan (&b))
1205 return -1; /* false, truth > 0 */
1207 return __fpcmp_parts (&a, &b);
1209 #endif /* L_gt_sf || L_gt_df */
1211 #if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
1212 CMPtype
1213 _ge_f2 (FLO_type arg_a, FLO_type arg_b)
1215 fp_number_type a;
1216 fp_number_type b;
1217 FLO_union_type au, bu;
1219 au.value = arg_a;
1220 bu.value = arg_b;
1222 unpack_d (&au, &a);
1223 unpack_d (&bu, &b);
1225 if (isnan (&a) || isnan (&b))
1226 return -1; /* false, truth >= 0 */
1227 return __fpcmp_parts (&a, &b) ;
1229 #endif /* L_ge_sf || L_ge_df */
1231 #if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
1232 CMPtype
1233 _lt_f2 (FLO_type arg_a, FLO_type arg_b)
1235 fp_number_type a;
1236 fp_number_type b;
1237 FLO_union_type au, bu;
1239 au.value = arg_a;
1240 bu.value = arg_b;
1242 unpack_d (&au, &a);
1243 unpack_d (&bu, &b);
1245 if (isnan (&a) || isnan (&b))
1246 return 1; /* false, truth < 0 */
1248 return __fpcmp_parts (&a, &b);
1250 #endif /* L_lt_sf || L_lt_df */
1252 #if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
1253 CMPtype
1254 _le_f2 (FLO_type arg_a, FLO_type arg_b)
1256 fp_number_type a;
1257 fp_number_type b;
1258 FLO_union_type au, bu;
1260 au.value = arg_a;
1261 bu.value = arg_b;
1263 unpack_d (&au, &a);
1264 unpack_d (&bu, &b);
1266 if (isnan (&a) || isnan (&b))
1267 return 1; /* false, truth <= 0 */
1269 return __fpcmp_parts (&a, &b) ;
1271 #endif /* L_le_sf || L_le_df */
1273 #endif /* ! US_SOFTWARE_GOFAST */
1275 #if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
1276 CMPtype
1277 _unord_f2 (FLO_type arg_a, FLO_type arg_b)
1279 fp_number_type a;
1280 fp_number_type b;
1281 FLO_union_type au, bu;
1283 au.value = arg_a;
1284 bu.value = arg_b;
1286 unpack_d (&au, &a);
1287 unpack_d (&bu, &b);
1289 return (isnan (&a) || isnan (&b));
1291 #endif /* L_unord_sf || L_unord_df */
1293 #if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
1294 FLO_type
1295 si_to_float (SItype arg_a)
1297 fp_number_type in;
1299 in.class = CLASS_NUMBER;
1300 in.sign = arg_a < 0;
1301 if (!arg_a)
1303 in.class = CLASS_ZERO;
1305 else
1307 in.normal_exp = FRACBITS + NGARDS;
1308 if (in.sign)
1310 /* Special case for minint, since there is no +ve integer
1311 representation for it */
1312 if (arg_a == (- MAX_SI_INT - 1))
1314 return (FLO_type)(- MAX_SI_INT - 1);
1316 in.fraction.ll = (-arg_a);
1318 else
1319 in.fraction.ll = arg_a;
1321 while (in.fraction.ll < ((fractype)1 << (FRACBITS + NGARDS)))
1323 in.fraction.ll <<= 1;
1324 in.normal_exp -= 1;
1327 return pack_d (&in);
1329 #endif /* L_si_to_sf || L_si_to_df */
1331 #if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
1332 FLO_type
1333 usi_to_float (USItype arg_a)
1335 fp_number_type in;
1337 in.sign = 0;
1338 if (!arg_a)
1340 in.class = CLASS_ZERO;
1342 else
1344 in.class = CLASS_NUMBER;
1345 in.normal_exp = FRACBITS + NGARDS;
1346 in.fraction.ll = arg_a;
1348 while (in.fraction.ll > ((fractype)1 << (FRACBITS + NGARDS)))
1350 in.fraction.ll >>= 1;
1351 in.normal_exp += 1;
1353 while (in.fraction.ll < ((fractype)1 << (FRACBITS + NGARDS)))
1355 in.fraction.ll <<= 1;
1356 in.normal_exp -= 1;
1359 return pack_d (&in);
1361 #endif
1363 #if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
1364 SItype
1365 float_to_si (FLO_type arg_a)
1367 fp_number_type a;
1368 SItype tmp;
1369 FLO_union_type au;
1371 au.value = arg_a;
1372 unpack_d (&au, &a);
1374 if (iszero (&a))
1375 return 0;
1376 if (isnan (&a))
1377 return 0;
1378 /* get reasonable MAX_SI_INT... */
1379 if (isinf (&a))
1380 return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1381 /* it is a number, but a small one */
1382 if (a.normal_exp < 0)
1383 return 0;
1384 if (a.normal_exp > BITS_PER_SI - 2)
1385 return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1386 tmp = a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1387 return a.sign ? (-tmp) : (tmp);
1389 #endif /* L_sf_to_si || L_df_to_si */
1391 #if defined(L_sf_to_usi) || defined(L_df_to_usi) || defined(L_tf_to_usi)
1392 #if defined US_SOFTWARE_GOFAST || defined(L_tf_to_usi)
1393 /* While libgcc2.c defines its own __fixunssfsi and __fixunsdfsi routines,
1394 we also define them for GOFAST because the ones in libgcc2.c have the
1395 wrong names and I'd rather define these here and keep GOFAST CYG-LOC's
1396 out of libgcc2.c. We can't define these here if not GOFAST because then
1397 there'd be duplicate copies. */
1399 USItype
1400 float_to_usi (FLO_type arg_a)
1402 fp_number_type a;
1403 FLO_union_type au;
1405 au.value = arg_a;
1406 unpack_d (&au, &a);
1408 if (iszero (&a))
1409 return 0;
1410 if (isnan (&a))
1411 return 0;
1412 /* it is a negative number */
1413 if (a.sign)
1414 return 0;
1415 /* get reasonable MAX_USI_INT... */
1416 if (isinf (&a))
1417 return MAX_USI_INT;
1418 /* it is a number, but a small one */
1419 if (a.normal_exp < 0)
1420 return 0;
1421 if (a.normal_exp > BITS_PER_SI - 1)
1422 return MAX_USI_INT;
1423 else if (a.normal_exp > (FRACBITS + NGARDS))
1424 return a.fraction.ll << (a.normal_exp - (FRACBITS + NGARDS));
1425 else
1426 return a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1428 #endif /* US_SOFTWARE_GOFAST */
1429 #endif /* L_sf_to_usi || L_df_to_usi */
1431 #if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
1432 FLO_type
1433 negate (FLO_type arg_a)
1435 fp_number_type a;
1436 FLO_union_type au;
1438 au.value = arg_a;
1439 unpack_d (&au, &a);
1441 flip_sign (&a);
1442 return pack_d (&a);
1444 #endif /* L_negate_sf || L_negate_df */
1446 #ifdef FLOAT
1448 #if defined(L_make_sf)
1449 SFtype
1450 __make_fp(fp_class_type class,
1451 unsigned int sign,
1452 int exp,
1453 USItype frac)
1455 fp_number_type in;
1457 in.class = class;
1458 in.sign = sign;
1459 in.normal_exp = exp;
1460 in.fraction.ll = frac;
1461 return pack_d (&in);
1463 #endif /* L_make_sf */
1465 #ifndef FLOAT_ONLY
1467 /* This enables one to build an fp library that supports float but not double.
1468 Otherwise, we would get an undefined reference to __make_dp.
1469 This is needed for some 8-bit ports that can't handle well values that
1470 are 8-bytes in size, so we just don't support double for them at all. */
1472 #if defined(L_sf_to_df)
1473 DFtype
1474 sf_to_df (SFtype arg_a)
1476 fp_number_type in;
1477 FLO_union_type au;
1479 au.value = arg_a;
1480 unpack_d (&au, &in);
1482 return __make_dp (in.class, in.sign, in.normal_exp,
1483 ((UDItype) in.fraction.ll) << F_D_BITOFF);
1485 #endif /* L_sf_to_df */
1487 #if defined(L_sf_to_tf) && defined(TMODES)
1488 TFtype
1489 sf_to_tf (SFtype arg_a)
1491 fp_number_type in;
1492 FLO_union_type au;
1494 au.value = arg_a;
1495 unpack_d (&au, &in);
1497 return __make_tp (in.class, in.sign, in.normal_exp,
1498 ((UTItype) in.fraction.ll) << F_T_BITOFF);
1500 #endif /* L_sf_to_df */
1502 #endif /* ! FLOAT_ONLY */
1503 #endif /* FLOAT */
1505 #ifndef FLOAT
1507 extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
1509 #if defined(L_make_df)
1510 DFtype
1511 __make_dp (fp_class_type class, unsigned int sign, int exp, UDItype frac)
1513 fp_number_type in;
1515 in.class = class;
1516 in.sign = sign;
1517 in.normal_exp = exp;
1518 in.fraction.ll = frac;
1519 return pack_d (&in);
1521 #endif /* L_make_df */
1523 #if defined(L_df_to_sf)
1524 SFtype
1525 df_to_sf (DFtype arg_a)
1527 fp_number_type in;
1528 USItype sffrac;
1529 FLO_union_type au;
1531 au.value = arg_a;
1532 unpack_d (&au, &in);
1534 sffrac = in.fraction.ll >> F_D_BITOFF;
1536 /* We set the lowest guard bit in SFFRAC if we discarded any non
1537 zero bits. */
1538 if ((in.fraction.ll & (((USItype) 1 << F_D_BITOFF) - 1)) != 0)
1539 sffrac |= 1;
1541 return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
1543 #endif /* L_df_to_sf */
1545 #if defined(L_df_to_tf) && defined(TMODES) \
1546 && !defined(FLOAT) && !defined(TFLOAT)
1547 TFtype
1548 df_to_tf (DFtype arg_a)
1550 fp_number_type in;
1551 FLO_union_type au;
1553 au.value = arg_a;
1554 unpack_d (&au, &in);
1556 return __make_tp (in.class, in.sign, in.normal_exp,
1557 ((UTItype) in.fraction.ll) << D_T_BITOFF);
1559 #endif /* L_sf_to_df */
1561 #ifdef TFLOAT
1562 #if defined(L_make_tf)
1563 TFtype
1564 __make_tp(fp_class_type class,
1565 unsigned int sign,
1566 int exp,
1567 UTItype frac)
1569 fp_number_type in;
1571 in.class = class;
1572 in.sign = sign;
1573 in.normal_exp = exp;
1574 in.fraction.ll = frac;
1575 return pack_d (&in);
1577 #endif /* L_make_tf */
1579 #if defined(L_tf_to_df)
1580 DFtype
1581 tf_to_df (TFtype arg_a)
1583 fp_number_type in;
1584 UDItype sffrac;
1585 FLO_union_type au;
1587 au.value = arg_a;
1588 unpack_d (&au, &in);
1590 sffrac = in.fraction.ll >> D_T_BITOFF;
1592 /* We set the lowest guard bit in SFFRAC if we discarded any non
1593 zero bits. */
1594 if ((in.fraction.ll & (((UTItype) 1 << D_T_BITOFF) - 1)) != 0)
1595 sffrac |= 1;
1597 return __make_dp (in.class, in.sign, in.normal_exp, sffrac);
1599 #endif /* L_tf_to_df */
1601 #if defined(L_tf_to_sf)
1602 SFtype
1603 tf_to_sf (TFtype arg_a)
1605 fp_number_type in;
1606 USItype sffrac;
1607 FLO_union_type au;
1609 au.value = arg_a;
1610 unpack_d (&au, &in);
1612 sffrac = in.fraction.ll >> F_T_BITOFF;
1614 /* We set the lowest guard bit in SFFRAC if we discarded any non
1615 zero bits. */
1616 if ((in.fraction.ll & (((UTItype) 1 << F_T_BITOFF) - 1)) != 0)
1617 sffrac |= 1;
1619 return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
1621 #endif /* L_tf_to_sf */
1622 #endif /* TFLOAT */
1624 #endif /* ! FLOAT */
1625 #endif /* !EXTENDED_FLOAT_STUBS */