PR rtl-optimization/26254
[official-gcc.git] / gcc / config / fp-bit.c
blobbdf04ffd312b282ed84e0483652807947e121f4b
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 2004, 2005 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combine
20 executable.)
22 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
23 WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 for more details.
27 You should have received a copy of the GNU General Public License
28 along with GCC; see the file COPYING. If not, write to the Free
29 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
30 02110-1301, USA. */
32 /* This implements IEEE 754 format arithmetic, but does not provide a
33 mechanism for setting the rounding mode, or for generating or handling
34 exceptions.
36 The original code by Steve Chamberlain, hacked by Mark Eichin and Jim
37 Wilson, all of Cygnus Support. */
39 /* The intended way to use this file is to make two copies, add `#define FLOAT'
40 to one copy, then compile both copies and add them to libgcc.a. */
42 #include "tconfig.h"
43 #include "coretypes.h"
44 #include "tm.h"
45 #include "config/fp-bit.h"
47 /* The following macros can be defined to change the behavior of this file:
48 FLOAT: Implement a `float', aka SFmode, fp library. If this is not
49 defined, then this file implements a `double', aka DFmode, fp library.
50 FLOAT_ONLY: Used with FLOAT, to implement a `float' only library, i.e.
51 don't include float->double conversion which requires the double library.
52 This is useful only for machines which can't support doubles, e.g. some
53 8-bit processors.
54 CMPtype: Specify the type that floating point compares should return.
55 This defaults to SItype, aka int.
56 US_SOFTWARE_GOFAST: This makes all entry points use the same names as the
57 US Software goFast library.
58 _DEBUG_BITFLOAT: This makes debugging the code a little easier, by adding
59 two integers to the FLO_union_type.
60 NO_DENORMALS: Disable handling of denormals.
61 NO_NANS: Disable nan and infinity handling
62 SMALL_MACHINE: Useful when operations on QIs and HIs are faster
63 than on an SI */
65 /* We don't currently support extended floats (long doubles) on machines
66 without hardware to deal with them.
68 These stubs are just to keep the linker from complaining about unresolved
69 references which can be pulled in from libio & libstdc++, even if the
70 user isn't using long doubles. However, they may generate an unresolved
71 external to abort if abort is not used by the function, and the stubs
72 are referenced from within libc, since libgcc goes before and after the
73 system library. */
75 #ifdef DECLARE_LIBRARY_RENAMES
76 DECLARE_LIBRARY_RENAMES
77 #endif
79 #ifdef EXTENDED_FLOAT_STUBS
80 extern void abort (void);
81 void __extendsfxf2 (void) { abort(); }
82 void __extenddfxf2 (void) { abort(); }
83 void __truncxfdf2 (void) { abort(); }
84 void __truncxfsf2 (void) { abort(); }
85 void __fixxfsi (void) { abort(); }
86 void __floatsixf (void) { abort(); }
87 void __addxf3 (void) { abort(); }
88 void __subxf3 (void) { abort(); }
89 void __mulxf3 (void) { abort(); }
90 void __divxf3 (void) { abort(); }
91 void __negxf2 (void) { abort(); }
92 void __eqxf2 (void) { abort(); }
93 void __nexf2 (void) { abort(); }
94 void __gtxf2 (void) { abort(); }
95 void __gexf2 (void) { abort(); }
96 void __lexf2 (void) { abort(); }
97 void __ltxf2 (void) { abort(); }
99 void __extendsftf2 (void) { abort(); }
100 void __extenddftf2 (void) { abort(); }
101 void __trunctfdf2 (void) { abort(); }
102 void __trunctfsf2 (void) { abort(); }
103 void __fixtfsi (void) { abort(); }
104 void __floatsitf (void) { abort(); }
105 void __addtf3 (void) { abort(); }
106 void __subtf3 (void) { abort(); }
107 void __multf3 (void) { abort(); }
108 void __divtf3 (void) { abort(); }
109 void __negtf2 (void) { abort(); }
110 void __eqtf2 (void) { abort(); }
111 void __netf2 (void) { abort(); }
112 void __gttf2 (void) { abort(); }
113 void __getf2 (void) { abort(); }
114 void __letf2 (void) { abort(); }
115 void __lttf2 (void) { abort(); }
116 #else /* !EXTENDED_FLOAT_STUBS, rest of file */
118 /* IEEE "special" number predicates */
120 #ifdef NO_NANS
122 #define nan() 0
123 #define isnan(x) 0
124 #define isinf(x) 0
125 #else
127 #if defined L_thenan_sf
128 const fp_number_type __thenan_sf = { CLASS_SNAN, 0, 0, {(fractype) 0} };
129 #elif defined L_thenan_df
130 const fp_number_type __thenan_df = { CLASS_SNAN, 0, 0, {(fractype) 0} };
131 #elif defined L_thenan_tf
132 const fp_number_type __thenan_tf = { CLASS_SNAN, 0, 0, {(fractype) 0} };
133 #elif defined TFLOAT
134 extern const fp_number_type __thenan_tf;
135 #elif defined FLOAT
136 extern const fp_number_type __thenan_sf;
137 #else
138 extern const fp_number_type __thenan_df;
139 #endif
141 INLINE
142 static fp_number_type *
143 nan (void)
145 /* Discard the const qualifier... */
146 #ifdef TFLOAT
147 return (fp_number_type *) (& __thenan_tf);
148 #elif defined FLOAT
149 return (fp_number_type *) (& __thenan_sf);
150 #else
151 return (fp_number_type *) (& __thenan_df);
152 #endif
155 INLINE
156 static int
157 isnan ( fp_number_type * x)
159 return __builtin_expect (x->class == CLASS_SNAN || x->class == CLASS_QNAN,
163 INLINE
164 static int
165 isinf ( fp_number_type * x)
167 return __builtin_expect (x->class == CLASS_INFINITY, 0);
170 #endif /* NO_NANS */
172 INLINE
173 static int
174 iszero ( fp_number_type * x)
176 return x->class == CLASS_ZERO;
179 INLINE
180 static void
181 flip_sign ( fp_number_type * x)
183 x->sign = !x->sign;
186 /* Count leading zeroes in N. */
187 INLINE
188 static int
189 clzusi (USItype n)
191 extern int __clzsi2 (USItype);
192 if (sizeof (USItype) == sizeof (unsigned int))
193 return __builtin_clz (n);
194 else if (sizeof (USItype) == sizeof (unsigned long))
195 return __builtin_clzl (n);
196 else if (sizeof (USItype) == sizeof (unsigned long long))
197 return __builtin_clzll (n);
198 else
199 return __clzsi2 (n);
202 extern FLO_type pack_d ( fp_number_type * );
204 #if defined(L_pack_df) || defined(L_pack_sf) || defined(L_pack_tf)
205 FLO_type
206 pack_d ( fp_number_type * src)
208 FLO_union_type dst;
209 fractype fraction = src->fraction.ll; /* wasn't unsigned before? */
210 int sign = src->sign;
211 int exp = 0;
213 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && (isnan (src) || isinf (src)))
215 /* We can't represent these values accurately. By using the
216 largest possible magnitude, we guarantee that the conversion
217 of infinity is at least as big as any finite number. */
218 exp = EXPMAX;
219 fraction = ((fractype) 1 << FRACBITS) - 1;
221 else if (isnan (src))
223 exp = EXPMAX;
224 if (src->class == CLASS_QNAN || 1)
226 #ifdef QUIET_NAN_NEGATED
227 fraction |= QUIET_NAN - 1;
228 #else
229 fraction |= QUIET_NAN;
230 #endif
233 else if (isinf (src))
235 exp = EXPMAX;
236 fraction = 0;
238 else if (iszero (src))
240 exp = 0;
241 fraction = 0;
243 else if (fraction == 0)
245 exp = 0;
247 else
249 if (__builtin_expect (src->normal_exp < NORMAL_EXPMIN, 0))
251 #ifdef NO_DENORMALS
252 /* Go straight to a zero representation if denormals are not
253 supported. The denormal handling would be harmless but
254 isn't unnecessary. */
255 exp = 0;
256 fraction = 0;
257 #else /* NO_DENORMALS */
258 /* This number's exponent is too low to fit into the bits
259 available in the number, so we'll store 0 in the exponent and
260 shift the fraction to the right to make up for it. */
262 int shift = NORMAL_EXPMIN - src->normal_exp;
264 exp = 0;
266 if (shift > FRAC_NBITS - NGARDS)
268 /* No point shifting, since it's more that 64 out. */
269 fraction = 0;
271 else
273 int lowbit = (fraction & (((fractype)1 << shift) - 1)) ? 1 : 0;
274 fraction = (fraction >> shift) | lowbit;
276 if ((fraction & GARDMASK) == GARDMSB)
278 if ((fraction & (1 << NGARDS)))
279 fraction += GARDROUND + 1;
281 else
283 /* Add to the guards to round up. */
284 fraction += GARDROUND;
286 /* Perhaps the rounding means we now need to change the
287 exponent, because the fraction is no longer denormal. */
288 if (fraction >= IMPLICIT_1)
290 exp += 1;
292 fraction >>= NGARDS;
293 #endif /* NO_DENORMALS */
295 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS)
296 && __builtin_expect (src->normal_exp > EXPBIAS, 0))
298 exp = EXPMAX;
299 fraction = 0;
301 else
303 exp = src->normal_exp + EXPBIAS;
304 if (!ROUND_TOWARDS_ZERO)
306 /* IF the gard bits are the all zero, but the first, then we're
307 half way between two numbers, choose the one which makes the
308 lsb of the answer 0. */
309 if ((fraction & GARDMASK) == GARDMSB)
311 if (fraction & (1 << NGARDS))
312 fraction += GARDROUND + 1;
314 else
316 /* Add a one to the guards to round up */
317 fraction += GARDROUND;
319 if (fraction >= IMPLICIT_2)
321 fraction >>= 1;
322 exp += 1;
325 fraction >>= NGARDS;
327 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && exp > EXPMAX)
329 /* Saturate on overflow. */
330 exp = EXPMAX;
331 fraction = ((fractype) 1 << FRACBITS) - 1;
336 /* We previously used bitfields to store the number, but this doesn't
337 handle little/big endian systems conveniently, so use shifts and
338 masks */
339 #ifdef FLOAT_BIT_ORDER_MISMATCH
340 dst.bits.fraction = fraction;
341 dst.bits.exp = exp;
342 dst.bits.sign = sign;
343 #else
344 # if defined TFLOAT && defined HALFFRACBITS
346 halffractype high, low, unity;
347 int lowsign, lowexp;
349 unity = (halffractype) 1 << HALFFRACBITS;
351 /* Set HIGH to the high double's significand, masking out the implicit 1.
352 Set LOW to the low double's full significand. */
353 high = (fraction >> (FRACBITS - HALFFRACBITS)) & (unity - 1);
354 low = fraction & (unity * 2 - 1);
356 /* Get the initial sign and exponent of the low double. */
357 lowexp = exp - HALFFRACBITS - 1;
358 lowsign = sign;
360 /* HIGH should be rounded like a normal double, making |LOW| <=
361 0.5 ULP of HIGH. Assume round-to-nearest. */
362 if (exp < EXPMAX)
363 if (low > unity || (low == unity && (high & 1) == 1))
365 /* Round HIGH up and adjust LOW to match. */
366 high++;
367 if (high == unity)
369 /* May make it infinite, but that's OK. */
370 high = 0;
371 exp++;
373 low = unity * 2 - low;
374 lowsign ^= 1;
377 high |= (halffractype) exp << HALFFRACBITS;
378 high |= (halffractype) sign << (HALFFRACBITS + EXPBITS);
380 if (exp == EXPMAX || exp == 0 || low == 0)
381 low = 0;
382 else
384 while (lowexp > 0 && low < unity)
386 low <<= 1;
387 lowexp--;
390 if (lowexp <= 0)
392 halffractype roundmsb, round;
393 int shift;
395 shift = 1 - lowexp;
396 roundmsb = (1 << (shift - 1));
397 round = low & ((roundmsb << 1) - 1);
399 low >>= shift;
400 lowexp = 0;
402 if (round > roundmsb || (round == roundmsb && (low & 1) == 1))
404 low++;
405 if (low == unity)
406 /* LOW rounds up to the smallest normal number. */
407 lowexp++;
411 low &= unity - 1;
412 low |= (halffractype) lowexp << HALFFRACBITS;
413 low |= (halffractype) lowsign << (HALFFRACBITS + EXPBITS);
415 dst.value_raw = ((fractype) high << HALFSHIFT) | low;
417 # else
418 dst.value_raw = fraction & ((((fractype)1) << FRACBITS) - (fractype)1);
419 dst.value_raw |= ((fractype) (exp & ((1 << EXPBITS) - 1))) << FRACBITS;
420 dst.value_raw |= ((fractype) (sign & 1)) << (FRACBITS | EXPBITS);
421 # endif
422 #endif
424 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
425 #ifdef TFLOAT
427 qrtrfractype tmp1 = dst.words[0];
428 qrtrfractype tmp2 = dst.words[1];
429 dst.words[0] = dst.words[3];
430 dst.words[1] = dst.words[2];
431 dst.words[2] = tmp2;
432 dst.words[3] = tmp1;
434 #else
436 halffractype tmp = dst.words[0];
437 dst.words[0] = dst.words[1];
438 dst.words[1] = tmp;
440 #endif
441 #endif
443 return dst.value;
445 #endif
447 #if defined(L_unpack_df) || defined(L_unpack_sf) || defined(L_unpack_tf)
448 void
449 unpack_d (FLO_union_type * src, fp_number_type * dst)
451 /* We previously used bitfields to store the number, but this doesn't
452 handle little/big endian systems conveniently, so use shifts and
453 masks */
454 fractype fraction;
455 int exp;
456 int sign;
458 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
459 FLO_union_type swapped;
461 #ifdef TFLOAT
462 swapped.words[0] = src->words[3];
463 swapped.words[1] = src->words[2];
464 swapped.words[2] = src->words[1];
465 swapped.words[3] = src->words[0];
466 #else
467 swapped.words[0] = src->words[1];
468 swapped.words[1] = src->words[0];
469 #endif
470 src = &swapped;
471 #endif
473 #ifdef FLOAT_BIT_ORDER_MISMATCH
474 fraction = src->bits.fraction;
475 exp = src->bits.exp;
476 sign = src->bits.sign;
477 #else
478 # if defined TFLOAT && defined HALFFRACBITS
480 halffractype high, low;
482 high = src->value_raw >> HALFSHIFT;
483 low = src->value_raw & (((fractype)1 << HALFSHIFT) - 1);
485 fraction = high & ((((fractype)1) << HALFFRACBITS) - 1);
486 fraction <<= FRACBITS - HALFFRACBITS;
487 exp = ((int)(high >> HALFFRACBITS)) & ((1 << EXPBITS) - 1);
488 sign = ((int)(high >> (((HALFFRACBITS + EXPBITS))))) & 1;
490 if (exp != EXPMAX && exp != 0 && low != 0)
492 int lowexp = ((int)(low >> HALFFRACBITS)) & ((1 << EXPBITS) - 1);
493 int lowsign = ((int)(low >> (((HALFFRACBITS + EXPBITS))))) & 1;
494 int shift;
495 fractype xlow;
497 xlow = low & ((((fractype)1) << HALFFRACBITS) - 1);
498 if (lowexp)
499 xlow |= (((halffractype)1) << HALFFRACBITS);
500 else
501 lowexp = 1;
502 shift = (FRACBITS - HALFFRACBITS) - (exp - lowexp);
503 if (shift > 0)
504 xlow <<= shift;
505 else if (shift < 0)
506 xlow >>= -shift;
507 if (sign == lowsign)
508 fraction += xlow;
509 else if (fraction >= xlow)
510 fraction -= xlow;
511 else
513 /* The high part is a power of two but the full number is lower.
514 This code will leave the implicit 1 in FRACTION, but we'd
515 have added that below anyway. */
516 fraction = (((fractype) 1 << FRACBITS) - xlow) << 1;
517 exp--;
521 # else
522 fraction = src->value_raw & ((((fractype)1) << FRACBITS) - 1);
523 exp = ((int)(src->value_raw >> FRACBITS)) & ((1 << EXPBITS) - 1);
524 sign = ((int)(src->value_raw >> (FRACBITS + EXPBITS))) & 1;
525 # endif
526 #endif
528 dst->sign = sign;
529 if (exp == 0)
531 /* Hmm. Looks like 0 */
532 if (fraction == 0
533 #ifdef NO_DENORMALS
534 || 1
535 #endif
538 /* tastes like zero */
539 dst->class = CLASS_ZERO;
541 else
543 /* Zero exponent with nonzero fraction - it's denormalized,
544 so there isn't a leading implicit one - we'll shift it so
545 it gets one. */
546 dst->normal_exp = exp - EXPBIAS + 1;
547 fraction <<= NGARDS;
549 dst->class = CLASS_NUMBER;
550 #if 1
551 while (fraction < IMPLICIT_1)
553 fraction <<= 1;
554 dst->normal_exp--;
556 #endif
557 dst->fraction.ll = fraction;
560 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS)
561 && __builtin_expect (exp == EXPMAX, 0))
563 /* Huge exponent*/
564 if (fraction == 0)
566 /* Attached to a zero fraction - means infinity */
567 dst->class = CLASS_INFINITY;
569 else
571 /* Nonzero fraction, means nan */
572 #ifdef QUIET_NAN_NEGATED
573 if ((fraction & QUIET_NAN) == 0)
574 #else
575 if (fraction & QUIET_NAN)
576 #endif
578 dst->class = CLASS_QNAN;
580 else
582 dst->class = CLASS_SNAN;
584 /* Keep the fraction part as the nan number */
585 dst->fraction.ll = fraction;
588 else
590 /* Nothing strange about this number */
591 dst->normal_exp = exp - EXPBIAS;
592 dst->class = CLASS_NUMBER;
593 dst->fraction.ll = (fraction << NGARDS) | IMPLICIT_1;
596 #endif /* L_unpack_df || L_unpack_sf */
598 #if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
599 static fp_number_type *
600 _fpadd_parts (fp_number_type * a,
601 fp_number_type * b,
602 fp_number_type * tmp)
604 intfrac tfraction;
606 /* Put commonly used fields in local variables. */
607 int a_normal_exp;
608 int b_normal_exp;
609 fractype a_fraction;
610 fractype b_fraction;
612 if (isnan (a))
614 return a;
616 if (isnan (b))
618 return b;
620 if (isinf (a))
622 /* Adding infinities with opposite signs yields a NaN. */
623 if (isinf (b) && a->sign != b->sign)
624 return nan ();
625 return a;
627 if (isinf (b))
629 return b;
631 if (iszero (b))
633 if (iszero (a))
635 *tmp = *a;
636 tmp->sign = a->sign & b->sign;
637 return tmp;
639 return a;
641 if (iszero (a))
643 return b;
646 /* Got two numbers. shift the smaller and increment the exponent till
647 they're the same */
649 int diff;
650 int sdiff;
652 a_normal_exp = a->normal_exp;
653 b_normal_exp = b->normal_exp;
654 a_fraction = a->fraction.ll;
655 b_fraction = b->fraction.ll;
657 diff = a_normal_exp - b_normal_exp;
658 sdiff = diff;
660 if (diff < 0)
661 diff = -diff;
662 if (diff < FRAC_NBITS)
664 if (sdiff > 0)
666 b_normal_exp += diff;
667 LSHIFT (b_fraction, diff);
669 else if (sdiff < 0)
671 a_normal_exp += diff;
672 LSHIFT (a_fraction, diff);
675 else
677 /* Somethings's up.. choose the biggest */
678 if (a_normal_exp > b_normal_exp)
680 b_normal_exp = a_normal_exp;
681 b_fraction = 0;
683 else
685 a_normal_exp = b_normal_exp;
686 a_fraction = 0;
691 if (a->sign != b->sign)
693 if (a->sign)
695 tfraction = -a_fraction + b_fraction;
697 else
699 tfraction = a_fraction - b_fraction;
701 if (tfraction >= 0)
703 tmp->sign = 0;
704 tmp->normal_exp = a_normal_exp;
705 tmp->fraction.ll = tfraction;
707 else
709 tmp->sign = 1;
710 tmp->normal_exp = a_normal_exp;
711 tmp->fraction.ll = -tfraction;
713 /* and renormalize it */
715 while (tmp->fraction.ll < IMPLICIT_1 && tmp->fraction.ll)
717 tmp->fraction.ll <<= 1;
718 tmp->normal_exp--;
721 else
723 tmp->sign = a->sign;
724 tmp->normal_exp = a_normal_exp;
725 tmp->fraction.ll = a_fraction + b_fraction;
727 tmp->class = CLASS_NUMBER;
728 /* Now the fraction is added, we have to shift down to renormalize the
729 number */
731 if (tmp->fraction.ll >= IMPLICIT_2)
733 LSHIFT (tmp->fraction.ll, 1);
734 tmp->normal_exp++;
736 return tmp;
740 FLO_type
741 add (FLO_type arg_a, FLO_type arg_b)
743 fp_number_type a;
744 fp_number_type b;
745 fp_number_type tmp;
746 fp_number_type *res;
747 FLO_union_type au, bu;
749 au.value = arg_a;
750 bu.value = arg_b;
752 unpack_d (&au, &a);
753 unpack_d (&bu, &b);
755 res = _fpadd_parts (&a, &b, &tmp);
757 return pack_d (res);
760 FLO_type
761 sub (FLO_type arg_a, FLO_type arg_b)
763 fp_number_type a;
764 fp_number_type b;
765 fp_number_type tmp;
766 fp_number_type *res;
767 FLO_union_type au, bu;
769 au.value = arg_a;
770 bu.value = arg_b;
772 unpack_d (&au, &a);
773 unpack_d (&bu, &b);
775 b.sign ^= 1;
777 res = _fpadd_parts (&a, &b, &tmp);
779 return pack_d (res);
781 #endif /* L_addsub_sf || L_addsub_df */
783 #if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
784 static inline __attribute__ ((__always_inline__)) fp_number_type *
785 _fpmul_parts ( fp_number_type * a,
786 fp_number_type * b,
787 fp_number_type * tmp)
789 fractype low = 0;
790 fractype high = 0;
792 if (isnan (a))
794 a->sign = a->sign != b->sign;
795 return a;
797 if (isnan (b))
799 b->sign = a->sign != b->sign;
800 return b;
802 if (isinf (a))
804 if (iszero (b))
805 return nan ();
806 a->sign = a->sign != b->sign;
807 return a;
809 if (isinf (b))
811 if (iszero (a))
813 return nan ();
815 b->sign = a->sign != b->sign;
816 return b;
818 if (iszero (a))
820 a->sign = a->sign != b->sign;
821 return a;
823 if (iszero (b))
825 b->sign = a->sign != b->sign;
826 return b;
829 /* Calculate the mantissa by multiplying both numbers to get a
830 twice-as-wide number. */
832 #if defined(NO_DI_MODE) || defined(TFLOAT)
834 fractype x = a->fraction.ll;
835 fractype ylow = b->fraction.ll;
836 fractype yhigh = 0;
837 int bit;
839 /* ??? This does multiplies one bit at a time. Optimize. */
840 for (bit = 0; bit < FRAC_NBITS; bit++)
842 int carry;
844 if (x & 1)
846 carry = (low += ylow) < ylow;
847 high += yhigh + carry;
849 yhigh <<= 1;
850 if (ylow & FRACHIGH)
852 yhigh |= 1;
854 ylow <<= 1;
855 x >>= 1;
858 #elif defined(FLOAT)
859 /* Multiplying two USIs to get a UDI, we're safe. */
861 UDItype answer = (UDItype)a->fraction.ll * (UDItype)b->fraction.ll;
863 high = answer >> BITS_PER_SI;
864 low = answer;
866 #else
867 /* fractype is DImode, but we need the result to be twice as wide.
868 Assuming a widening multiply from DImode to TImode is not
869 available, build one by hand. */
871 USItype nl = a->fraction.ll;
872 USItype nh = a->fraction.ll >> BITS_PER_SI;
873 USItype ml = b->fraction.ll;
874 USItype mh = b->fraction.ll >> BITS_PER_SI;
875 UDItype pp_ll = (UDItype) ml * nl;
876 UDItype pp_hl = (UDItype) mh * nl;
877 UDItype pp_lh = (UDItype) ml * nh;
878 UDItype pp_hh = (UDItype) mh * nh;
879 UDItype res2 = 0;
880 UDItype res0 = 0;
881 UDItype ps_hh__ = pp_hl + pp_lh;
882 if (ps_hh__ < pp_hl)
883 res2 += (UDItype)1 << BITS_PER_SI;
884 pp_hl = (UDItype)(USItype)ps_hh__ << BITS_PER_SI;
885 res0 = pp_ll + pp_hl;
886 if (res0 < pp_ll)
887 res2++;
888 res2 += (ps_hh__ >> BITS_PER_SI) + pp_hh;
889 high = res2;
890 low = res0;
892 #endif
895 tmp->normal_exp = a->normal_exp + b->normal_exp
896 + FRAC_NBITS - (FRACBITS + NGARDS);
897 tmp->sign = a->sign != b->sign;
898 while (high >= IMPLICIT_2)
900 tmp->normal_exp++;
901 if (high & 1)
903 low >>= 1;
904 low |= FRACHIGH;
906 high >>= 1;
908 while (high < IMPLICIT_1)
910 tmp->normal_exp--;
912 high <<= 1;
913 if (low & FRACHIGH)
914 high |= 1;
915 low <<= 1;
918 if (!ROUND_TOWARDS_ZERO && (high & GARDMASK) == GARDMSB)
920 if (high & (1 << NGARDS))
922 /* Because we're half way, we would round to even by adding
923 GARDROUND + 1, except that's also done in the packing
924 function, and rounding twice will lose precision and cause
925 the result to be too far off. Example: 32-bit floats with
926 bit patterns 0xfff * 0x3f800400 ~= 0xfff (less than 0.5ulp
927 off), not 0x1000 (more than 0.5ulp off). */
929 else if (low)
931 /* We're a further than half way by a small amount corresponding
932 to the bits set in "low". Knowing that, we round here and
933 not in pack_d, because there we don't have "low" available
934 anymore. */
935 high += GARDROUND + 1;
937 /* Avoid further rounding in pack_d. */
938 high &= ~(fractype) GARDMASK;
941 tmp->fraction.ll = high;
942 tmp->class = CLASS_NUMBER;
943 return tmp;
946 FLO_type
947 multiply (FLO_type arg_a, FLO_type arg_b)
949 fp_number_type a;
950 fp_number_type b;
951 fp_number_type tmp;
952 fp_number_type *res;
953 FLO_union_type au, bu;
955 au.value = arg_a;
956 bu.value = arg_b;
958 unpack_d (&au, &a);
959 unpack_d (&bu, &b);
961 res = _fpmul_parts (&a, &b, &tmp);
963 return pack_d (res);
965 #endif /* L_mul_sf || L_mul_df || L_mul_tf */
967 #if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
968 static inline __attribute__ ((__always_inline__)) fp_number_type *
969 _fpdiv_parts (fp_number_type * a,
970 fp_number_type * b)
972 fractype bit;
973 fractype numerator;
974 fractype denominator;
975 fractype quotient;
977 if (isnan (a))
979 return a;
981 if (isnan (b))
983 return b;
986 a->sign = a->sign ^ b->sign;
988 if (isinf (a) || iszero (a))
990 if (a->class == b->class)
991 return nan ();
992 return a;
995 if (isinf (b))
997 a->fraction.ll = 0;
998 a->normal_exp = 0;
999 return a;
1001 if (iszero (b))
1003 a->class = CLASS_INFINITY;
1004 return a;
1007 /* Calculate the mantissa by multiplying both 64bit numbers to get a
1008 128 bit number */
1010 /* quotient =
1011 ( numerator / denominator) * 2^(numerator exponent - denominator exponent)
1014 a->normal_exp = a->normal_exp - b->normal_exp;
1015 numerator = a->fraction.ll;
1016 denominator = b->fraction.ll;
1018 if (numerator < denominator)
1020 /* Fraction will be less than 1.0 */
1021 numerator *= 2;
1022 a->normal_exp--;
1024 bit = IMPLICIT_1;
1025 quotient = 0;
1026 /* ??? Does divide one bit at a time. Optimize. */
1027 while (bit)
1029 if (numerator >= denominator)
1031 quotient |= bit;
1032 numerator -= denominator;
1034 bit >>= 1;
1035 numerator *= 2;
1038 if (!ROUND_TOWARDS_ZERO && (quotient & GARDMASK) == GARDMSB)
1040 if (quotient & (1 << NGARDS))
1042 /* Because we're half way, we would round to even by adding
1043 GARDROUND + 1, except that's also done in the packing
1044 function, and rounding twice will lose precision and cause
1045 the result to be too far off. */
1047 else if (numerator)
1049 /* We're a further than half way by the small amount
1050 corresponding to the bits set in "numerator". Knowing
1051 that, we round here and not in pack_d, because there we
1052 don't have "numerator" available anymore. */
1053 quotient += GARDROUND + 1;
1055 /* Avoid further rounding in pack_d. */
1056 quotient &= ~(fractype) GARDMASK;
1060 a->fraction.ll = quotient;
1061 return (a);
1065 FLO_type
1066 divide (FLO_type arg_a, FLO_type arg_b)
1068 fp_number_type a;
1069 fp_number_type b;
1070 fp_number_type *res;
1071 FLO_union_type au, bu;
1073 au.value = arg_a;
1074 bu.value = arg_b;
1076 unpack_d (&au, &a);
1077 unpack_d (&bu, &b);
1079 res = _fpdiv_parts (&a, &b);
1081 return pack_d (res);
1083 #endif /* L_div_sf || L_div_df */
1085 #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df) \
1086 || defined(L_fpcmp_parts_tf)
1087 /* according to the demo, fpcmp returns a comparison with 0... thus
1088 a<b -> -1
1089 a==b -> 0
1090 a>b -> +1
1094 __fpcmp_parts (fp_number_type * a, fp_number_type * b)
1096 #if 0
1097 /* either nan -> unordered. Must be checked outside of this routine. */
1098 if (isnan (a) && isnan (b))
1100 return 1; /* still unordered! */
1102 #endif
1104 if (isnan (a) || isnan (b))
1106 return 1; /* how to indicate unordered compare? */
1108 if (isinf (a) && isinf (b))
1110 /* +inf > -inf, but +inf != +inf */
1111 /* b \a| +inf(0)| -inf(1)
1112 ______\+--------+--------
1113 +inf(0)| a==b(0)| a<b(-1)
1114 -------+--------+--------
1115 -inf(1)| a>b(1) | a==b(0)
1116 -------+--------+--------
1117 So since unordered must be nonzero, just line up the columns...
1119 return b->sign - a->sign;
1121 /* but not both... */
1122 if (isinf (a))
1124 return a->sign ? -1 : 1;
1126 if (isinf (b))
1128 return b->sign ? 1 : -1;
1130 if (iszero (a) && iszero (b))
1132 return 0;
1134 if (iszero (a))
1136 return b->sign ? 1 : -1;
1138 if (iszero (b))
1140 return a->sign ? -1 : 1;
1142 /* now both are "normal". */
1143 if (a->sign != b->sign)
1145 /* opposite signs */
1146 return a->sign ? -1 : 1;
1148 /* same sign; exponents? */
1149 if (a->normal_exp > b->normal_exp)
1151 return a->sign ? -1 : 1;
1153 if (a->normal_exp < b->normal_exp)
1155 return a->sign ? 1 : -1;
1157 /* same exponents; check size. */
1158 if (a->fraction.ll > b->fraction.ll)
1160 return a->sign ? -1 : 1;
1162 if (a->fraction.ll < b->fraction.ll)
1164 return a->sign ? 1 : -1;
1166 /* after all that, they're equal. */
1167 return 0;
1169 #endif
1171 #if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compoare_tf)
1172 CMPtype
1173 compare (FLO_type arg_a, FLO_type arg_b)
1175 fp_number_type a;
1176 fp_number_type b;
1177 FLO_union_type au, bu;
1179 au.value = arg_a;
1180 bu.value = arg_b;
1182 unpack_d (&au, &a);
1183 unpack_d (&bu, &b);
1185 return __fpcmp_parts (&a, &b);
1187 #endif /* L_compare_sf || L_compare_df */
1189 #ifndef US_SOFTWARE_GOFAST
1191 /* These should be optimized for their specific tasks someday. */
1193 #if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
1194 CMPtype
1195 _eq_f2 (FLO_type arg_a, FLO_type arg_b)
1197 fp_number_type a;
1198 fp_number_type b;
1199 FLO_union_type au, bu;
1201 au.value = arg_a;
1202 bu.value = arg_b;
1204 unpack_d (&au, &a);
1205 unpack_d (&bu, &b);
1207 if (isnan (&a) || isnan (&b))
1208 return 1; /* false, truth == 0 */
1210 return __fpcmp_parts (&a, &b) ;
1212 #endif /* L_eq_sf || L_eq_df */
1214 #if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
1215 CMPtype
1216 _ne_f2 (FLO_type arg_a, FLO_type arg_b)
1218 fp_number_type a;
1219 fp_number_type b;
1220 FLO_union_type au, bu;
1222 au.value = arg_a;
1223 bu.value = arg_b;
1225 unpack_d (&au, &a);
1226 unpack_d (&bu, &b);
1228 if (isnan (&a) || isnan (&b))
1229 return 1; /* true, truth != 0 */
1231 return __fpcmp_parts (&a, &b) ;
1233 #endif /* L_ne_sf || L_ne_df */
1235 #if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
1236 CMPtype
1237 _gt_f2 (FLO_type arg_a, FLO_type arg_b)
1239 fp_number_type a;
1240 fp_number_type b;
1241 FLO_union_type au, bu;
1243 au.value = arg_a;
1244 bu.value = arg_b;
1246 unpack_d (&au, &a);
1247 unpack_d (&bu, &b);
1249 if (isnan (&a) || isnan (&b))
1250 return -1; /* false, truth > 0 */
1252 return __fpcmp_parts (&a, &b);
1254 #endif /* L_gt_sf || L_gt_df */
1256 #if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
1257 CMPtype
1258 _ge_f2 (FLO_type arg_a, FLO_type arg_b)
1260 fp_number_type a;
1261 fp_number_type b;
1262 FLO_union_type au, bu;
1264 au.value = arg_a;
1265 bu.value = arg_b;
1267 unpack_d (&au, &a);
1268 unpack_d (&bu, &b);
1270 if (isnan (&a) || isnan (&b))
1271 return -1; /* false, truth >= 0 */
1272 return __fpcmp_parts (&a, &b) ;
1274 #endif /* L_ge_sf || L_ge_df */
1276 #if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
1277 CMPtype
1278 _lt_f2 (FLO_type arg_a, FLO_type arg_b)
1280 fp_number_type a;
1281 fp_number_type b;
1282 FLO_union_type au, bu;
1284 au.value = arg_a;
1285 bu.value = arg_b;
1287 unpack_d (&au, &a);
1288 unpack_d (&bu, &b);
1290 if (isnan (&a) || isnan (&b))
1291 return 1; /* false, truth < 0 */
1293 return __fpcmp_parts (&a, &b);
1295 #endif /* L_lt_sf || L_lt_df */
1297 #if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
1298 CMPtype
1299 _le_f2 (FLO_type arg_a, FLO_type arg_b)
1301 fp_number_type a;
1302 fp_number_type b;
1303 FLO_union_type au, bu;
1305 au.value = arg_a;
1306 bu.value = arg_b;
1308 unpack_d (&au, &a);
1309 unpack_d (&bu, &b);
1311 if (isnan (&a) || isnan (&b))
1312 return 1; /* false, truth <= 0 */
1314 return __fpcmp_parts (&a, &b) ;
1316 #endif /* L_le_sf || L_le_df */
1318 #endif /* ! US_SOFTWARE_GOFAST */
1320 #if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
1321 CMPtype
1322 _unord_f2 (FLO_type arg_a, FLO_type arg_b)
1324 fp_number_type a;
1325 fp_number_type b;
1326 FLO_union_type au, bu;
1328 au.value = arg_a;
1329 bu.value = arg_b;
1331 unpack_d (&au, &a);
1332 unpack_d (&bu, &b);
1334 return (isnan (&a) || isnan (&b));
1336 #endif /* L_unord_sf || L_unord_df */
1338 #if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
1339 FLO_type
1340 si_to_float (SItype arg_a)
1342 fp_number_type in;
1344 in.class = CLASS_NUMBER;
1345 in.sign = arg_a < 0;
1346 if (!arg_a)
1348 in.class = CLASS_ZERO;
1350 else
1352 USItype uarg;
1353 int shift;
1354 in.normal_exp = FRACBITS + NGARDS;
1355 if (in.sign)
1357 /* Special case for minint, since there is no +ve integer
1358 representation for it */
1359 if (arg_a == (- MAX_SI_INT - 1))
1361 return (FLO_type)(- MAX_SI_INT - 1);
1363 uarg = (-arg_a);
1365 else
1366 uarg = arg_a;
1368 in.fraction.ll = uarg;
1369 shift = clzusi (uarg) - (BITS_PER_SI - 1 - FRACBITS - NGARDS);
1370 if (shift > 0)
1372 in.fraction.ll <<= shift;
1373 in.normal_exp -= shift;
1376 return pack_d (&in);
1378 #endif /* L_si_to_sf || L_si_to_df */
1380 #if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
1381 FLO_type
1382 usi_to_float (USItype arg_a)
1384 fp_number_type in;
1386 in.sign = 0;
1387 if (!arg_a)
1389 in.class = CLASS_ZERO;
1391 else
1393 int shift;
1394 in.class = CLASS_NUMBER;
1395 in.normal_exp = FRACBITS + NGARDS;
1396 in.fraction.ll = arg_a;
1398 shift = clzusi (arg_a) - (BITS_PER_SI - 1 - FRACBITS - NGARDS);
1399 if (shift < 0)
1401 fractype guard = in.fraction.ll & (((fractype)1 << -shift) - 1);
1402 in.fraction.ll >>= -shift;
1403 in.fraction.ll |= (guard != 0);
1404 in.normal_exp -= shift;
1406 else if (shift > 0)
1408 in.fraction.ll <<= shift;
1409 in.normal_exp -= shift;
1412 return pack_d (&in);
1414 #endif
1416 #if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
1417 SItype
1418 float_to_si (FLO_type arg_a)
1420 fp_number_type a;
1421 SItype tmp;
1422 FLO_union_type au;
1424 au.value = arg_a;
1425 unpack_d (&au, &a);
1427 if (iszero (&a))
1428 return 0;
1429 if (isnan (&a))
1430 return 0;
1431 /* get reasonable MAX_SI_INT... */
1432 if (isinf (&a))
1433 return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1434 /* it is a number, but a small one */
1435 if (a.normal_exp < 0)
1436 return 0;
1437 if (a.normal_exp > BITS_PER_SI - 2)
1438 return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1439 tmp = a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1440 return a.sign ? (-tmp) : (tmp);
1442 #endif /* L_sf_to_si || L_df_to_si */
1444 #if defined(L_sf_to_usi) || defined(L_df_to_usi) || defined(L_tf_to_usi)
1445 #if defined US_SOFTWARE_GOFAST || defined(L_tf_to_usi)
1446 /* While libgcc2.c defines its own __fixunssfsi and __fixunsdfsi routines,
1447 we also define them for GOFAST because the ones in libgcc2.c have the
1448 wrong names and I'd rather define these here and keep GOFAST CYG-LOC's
1449 out of libgcc2.c. We can't define these here if not GOFAST because then
1450 there'd be duplicate copies. */
1452 USItype
1453 float_to_usi (FLO_type arg_a)
1455 fp_number_type a;
1456 FLO_union_type au;
1458 au.value = arg_a;
1459 unpack_d (&au, &a);
1461 if (iszero (&a))
1462 return 0;
1463 if (isnan (&a))
1464 return 0;
1465 /* it is a negative number */
1466 if (a.sign)
1467 return 0;
1468 /* get reasonable MAX_USI_INT... */
1469 if (isinf (&a))
1470 return MAX_USI_INT;
1471 /* it is a number, but a small one */
1472 if (a.normal_exp < 0)
1473 return 0;
1474 if (a.normal_exp > BITS_PER_SI - 1)
1475 return MAX_USI_INT;
1476 else if (a.normal_exp > (FRACBITS + NGARDS))
1477 return a.fraction.ll << (a.normal_exp - (FRACBITS + NGARDS));
1478 else
1479 return a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1481 #endif /* US_SOFTWARE_GOFAST */
1482 #endif /* L_sf_to_usi || L_df_to_usi */
1484 #if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
1485 FLO_type
1486 negate (FLO_type arg_a)
1488 fp_number_type a;
1489 FLO_union_type au;
1491 au.value = arg_a;
1492 unpack_d (&au, &a);
1494 flip_sign (&a);
1495 return pack_d (&a);
1497 #endif /* L_negate_sf || L_negate_df */
1499 #ifdef FLOAT
1501 #if defined(L_make_sf)
1502 SFtype
1503 __make_fp(fp_class_type class,
1504 unsigned int sign,
1505 int exp,
1506 USItype frac)
1508 fp_number_type in;
1510 in.class = class;
1511 in.sign = sign;
1512 in.normal_exp = exp;
1513 in.fraction.ll = frac;
1514 return pack_d (&in);
1516 #endif /* L_make_sf */
1518 #ifndef FLOAT_ONLY
1520 /* This enables one to build an fp library that supports float but not double.
1521 Otherwise, we would get an undefined reference to __make_dp.
1522 This is needed for some 8-bit ports that can't handle well values that
1523 are 8-bytes in size, so we just don't support double for them at all. */
1525 #if defined(L_sf_to_df)
1526 DFtype
1527 sf_to_df (SFtype arg_a)
1529 fp_number_type in;
1530 FLO_union_type au;
1532 au.value = arg_a;
1533 unpack_d (&au, &in);
1535 return __make_dp (in.class, in.sign, in.normal_exp,
1536 ((UDItype) in.fraction.ll) << F_D_BITOFF);
1538 #endif /* L_sf_to_df */
1540 #if defined(L_sf_to_tf) && defined(TMODES)
1541 TFtype
1542 sf_to_tf (SFtype arg_a)
1544 fp_number_type in;
1545 FLO_union_type au;
1547 au.value = arg_a;
1548 unpack_d (&au, &in);
1550 return __make_tp (in.class, in.sign, in.normal_exp,
1551 ((UTItype) in.fraction.ll) << F_T_BITOFF);
1553 #endif /* L_sf_to_df */
1555 #endif /* ! FLOAT_ONLY */
1556 #endif /* FLOAT */
1558 #ifndef FLOAT
1560 extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
1562 #if defined(L_make_df)
1563 DFtype
1564 __make_dp (fp_class_type class, unsigned int sign, int exp, UDItype frac)
1566 fp_number_type in;
1568 in.class = class;
1569 in.sign = sign;
1570 in.normal_exp = exp;
1571 in.fraction.ll = frac;
1572 return pack_d (&in);
1574 #endif /* L_make_df */
1576 #if defined(L_df_to_sf)
1577 SFtype
1578 df_to_sf (DFtype arg_a)
1580 fp_number_type in;
1581 USItype sffrac;
1582 FLO_union_type au;
1584 au.value = arg_a;
1585 unpack_d (&au, &in);
1587 sffrac = in.fraction.ll >> F_D_BITOFF;
1589 /* We set the lowest guard bit in SFFRAC if we discarded any non
1590 zero bits. */
1591 if ((in.fraction.ll & (((USItype) 1 << F_D_BITOFF) - 1)) != 0)
1592 sffrac |= 1;
1594 return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
1596 #endif /* L_df_to_sf */
1598 #if defined(L_df_to_tf) && defined(TMODES) \
1599 && !defined(FLOAT) && !defined(TFLOAT)
1600 TFtype
1601 df_to_tf (DFtype arg_a)
1603 fp_number_type in;
1604 FLO_union_type au;
1606 au.value = arg_a;
1607 unpack_d (&au, &in);
1609 return __make_tp (in.class, in.sign, in.normal_exp,
1610 ((UTItype) in.fraction.ll) << D_T_BITOFF);
1612 #endif /* L_sf_to_df */
1614 #ifdef TFLOAT
1615 #if defined(L_make_tf)
1616 TFtype
1617 __make_tp(fp_class_type class,
1618 unsigned int sign,
1619 int exp,
1620 UTItype frac)
1622 fp_number_type in;
1624 in.class = class;
1625 in.sign = sign;
1626 in.normal_exp = exp;
1627 in.fraction.ll = frac;
1628 return pack_d (&in);
1630 #endif /* L_make_tf */
1632 #if defined(L_tf_to_df)
1633 DFtype
1634 tf_to_df (TFtype arg_a)
1636 fp_number_type in;
1637 UDItype sffrac;
1638 FLO_union_type au;
1640 au.value = arg_a;
1641 unpack_d (&au, &in);
1643 sffrac = in.fraction.ll >> D_T_BITOFF;
1645 /* We set the lowest guard bit in SFFRAC if we discarded any non
1646 zero bits. */
1647 if ((in.fraction.ll & (((UTItype) 1 << D_T_BITOFF) - 1)) != 0)
1648 sffrac |= 1;
1650 return __make_dp (in.class, in.sign, in.normal_exp, sffrac);
1652 #endif /* L_tf_to_df */
1654 #if defined(L_tf_to_sf)
1655 SFtype
1656 tf_to_sf (TFtype arg_a)
1658 fp_number_type in;
1659 USItype sffrac;
1660 FLO_union_type au;
1662 au.value = arg_a;
1663 unpack_d (&au, &in);
1665 sffrac = in.fraction.ll >> F_T_BITOFF;
1667 /* We set the lowest guard bit in SFFRAC if we discarded any non
1668 zero bits. */
1669 if ((in.fraction.ll & (((UTItype) 1 << F_T_BITOFF) - 1)) != 0)
1670 sffrac |= 1;
1672 return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
1674 #endif /* L_tf_to_sf */
1675 #endif /* TFLOAT */
1677 #endif /* ! FLOAT */
1678 #endif /* !EXTENDED_FLOAT_STUBS */