* arm.c (emit_set_insn): New function.
[official-gcc.git] / gcc / config / fp-bit.c
blobccf927e8c3bc667980f7f208d50f5dd7417d6081
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 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, 51 Franklin Street, Fifth Floor,
27 Boston, MA 02110-1301, 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 "config/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 /* Count leading zeroes in N. */
190 INLINE
191 static int
192 clzusi (USItype n)
194 extern int __clzsi2 (USItype);
195 if (sizeof (USItype) == sizeof (unsigned int))
196 return __builtin_clz (n);
197 else if (sizeof (USItype) == sizeof (unsigned long))
198 return __builtin_clzl (n);
199 else if (sizeof (USItype) == sizeof (unsigned long long))
200 return __builtin_clzll (n);
201 else
202 return __clzsi2 (n);
205 extern FLO_type pack_d ( fp_number_type * );
207 #if defined(L_pack_df) || defined(L_pack_sf) || defined(L_pack_tf)
208 FLO_type
209 pack_d ( fp_number_type * src)
211 FLO_union_type dst;
212 fractype fraction = src->fraction.ll; /* wasn't unsigned before? */
213 int sign = src->sign;
214 int exp = 0;
216 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && (isnan (src) || isinf (src)))
218 /* We can't represent these values accurately. By using the
219 largest possible magnitude, we guarantee that the conversion
220 of infinity is at least as big as any finite number. */
221 exp = EXPMAX;
222 fraction = ((fractype) 1 << FRACBITS) - 1;
224 else if (isnan (src))
226 exp = EXPMAX;
227 if (src->class == CLASS_QNAN || 1)
229 #ifdef QUIET_NAN_NEGATED
230 fraction |= QUIET_NAN - 1;
231 #else
232 fraction |= QUIET_NAN;
233 #endif
236 else if (isinf (src))
238 exp = EXPMAX;
239 fraction = 0;
241 else if (iszero (src))
243 exp = 0;
244 fraction = 0;
246 else if (fraction == 0)
248 exp = 0;
250 else
252 if (src->normal_exp < NORMAL_EXPMIN)
254 #ifdef NO_DENORMALS
255 /* Go straight to a zero representation if denormals are not
256 supported. The denormal handling would be harmless but
257 isn't unnecessary. */
258 exp = 0;
259 fraction = 0;
260 #else /* NO_DENORMALS */
261 /* This number's exponent is too low to fit into the bits
262 available in the number, so we'll store 0 in the exponent and
263 shift the fraction to the right to make up for it. */
265 int shift = NORMAL_EXPMIN - src->normal_exp;
267 exp = 0;
269 if (shift > FRAC_NBITS - NGARDS)
271 /* No point shifting, since it's more that 64 out. */
272 fraction = 0;
274 else
276 int lowbit = (fraction & (((fractype)1 << shift) - 1)) ? 1 : 0;
277 fraction = (fraction >> shift) | lowbit;
279 if ((fraction & GARDMASK) == GARDMSB)
281 if ((fraction & (1 << NGARDS)))
282 fraction += GARDROUND + 1;
284 else
286 /* Add to the guards to round up. */
287 fraction += GARDROUND;
289 /* Perhaps the rounding means we now need to change the
290 exponent, because the fraction is no longer denormal. */
291 if (fraction >= IMPLICIT_1)
293 exp += 1;
295 fraction >>= NGARDS;
296 #endif /* NO_DENORMALS */
298 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS)
299 && src->normal_exp > EXPBIAS)
301 exp = EXPMAX;
302 fraction = 0;
304 else
306 exp = src->normal_exp + EXPBIAS;
307 if (!ROUND_TOWARDS_ZERO)
309 /* IF the gard bits are the all zero, but the first, then we're
310 half way between two numbers, choose the one which makes the
311 lsb of the answer 0. */
312 if ((fraction & GARDMASK) == GARDMSB)
314 if (fraction & (1 << NGARDS))
315 fraction += GARDROUND + 1;
317 else
319 /* Add a one to the guards to round up */
320 fraction += GARDROUND;
322 if (fraction >= IMPLICIT_2)
324 fraction >>= 1;
325 exp += 1;
328 fraction >>= NGARDS;
330 if (LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && exp > EXPMAX)
332 /* Saturate on overflow. */
333 exp = EXPMAX;
334 fraction = ((fractype) 1 << FRACBITS) - 1;
339 /* We previously used bitfields to store the number, but this doesn't
340 handle little/big endian systems conveniently, so use shifts and
341 masks */
342 #ifdef FLOAT_BIT_ORDER_MISMATCH
343 dst.bits.fraction = fraction;
344 dst.bits.exp = exp;
345 dst.bits.sign = sign;
346 #else
347 # if defined TFLOAT && defined HALFFRACBITS
349 halffractype high, low, unity;
350 int lowsign, lowexp;
352 unity = (halffractype) 1 << HALFFRACBITS;
354 /* Set HIGH to the high double's significand, masking out the implicit 1.
355 Set LOW to the low double's full significand. */
356 high = (fraction >> (FRACBITS - HALFFRACBITS)) & (unity - 1);
357 low = fraction & (unity * 2 - 1);
359 /* Get the initial sign and exponent of the low double. */
360 lowexp = exp - HALFFRACBITS - 1;
361 lowsign = sign;
363 /* HIGH should be rounded like a normal double, making |LOW| <=
364 0.5 ULP of HIGH. Assume round-to-nearest. */
365 if (exp < EXPMAX)
366 if (low > unity || (low == unity && (high & 1) == 1))
368 /* Round HIGH up and adjust LOW to match. */
369 high++;
370 if (high == unity)
372 /* May make it infinite, but that's OK. */
373 high = 0;
374 exp++;
376 low = unity * 2 - low;
377 lowsign ^= 1;
380 high |= (halffractype) exp << HALFFRACBITS;
381 high |= (halffractype) sign << (HALFFRACBITS + EXPBITS);
383 if (exp == EXPMAX || exp == 0 || low == 0)
384 low = 0;
385 else
387 while (lowexp > 0 && low < unity)
389 low <<= 1;
390 lowexp--;
393 if (lowexp <= 0)
395 halffractype roundmsb, round;
396 int shift;
398 shift = 1 - lowexp;
399 roundmsb = (1 << (shift - 1));
400 round = low & ((roundmsb << 1) - 1);
402 low >>= shift;
403 lowexp = 0;
405 if (round > roundmsb || (round == roundmsb && (low & 1) == 1))
407 low++;
408 if (low == unity)
409 /* LOW rounds up to the smallest normal number. */
410 lowexp++;
414 low &= unity - 1;
415 low |= (halffractype) lowexp << HALFFRACBITS;
416 low |= (halffractype) lowsign << (HALFFRACBITS + EXPBITS);
418 dst.value_raw = ((fractype) high << HALFSHIFT) | low;
420 # else
421 dst.value_raw = fraction & ((((fractype)1) << FRACBITS) - (fractype)1);
422 dst.value_raw |= ((fractype) (exp & ((1 << EXPBITS) - 1))) << FRACBITS;
423 dst.value_raw |= ((fractype) (sign & 1)) << (FRACBITS | EXPBITS);
424 # endif
425 #endif
427 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
428 #ifdef TFLOAT
430 qrtrfractype tmp1 = dst.words[0];
431 qrtrfractype tmp2 = dst.words[1];
432 dst.words[0] = dst.words[3];
433 dst.words[1] = dst.words[2];
434 dst.words[2] = tmp2;
435 dst.words[3] = tmp1;
437 #else
439 halffractype tmp = dst.words[0];
440 dst.words[0] = dst.words[1];
441 dst.words[1] = tmp;
443 #endif
444 #endif
446 return dst.value;
448 #endif
450 #if defined(L_unpack_df) || defined(L_unpack_sf) || defined(L_unpack_tf)
451 void
452 unpack_d (FLO_union_type * src, fp_number_type * dst)
454 /* We previously used bitfields to store the number, but this doesn't
455 handle little/big endian systems conveniently, so use shifts and
456 masks */
457 fractype fraction;
458 int exp;
459 int sign;
461 #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
462 FLO_union_type swapped;
464 #ifdef TFLOAT
465 swapped.words[0] = src->words[3];
466 swapped.words[1] = src->words[2];
467 swapped.words[2] = src->words[1];
468 swapped.words[3] = src->words[0];
469 #else
470 swapped.words[0] = src->words[1];
471 swapped.words[1] = src->words[0];
472 #endif
473 src = &swapped;
474 #endif
476 #ifdef FLOAT_BIT_ORDER_MISMATCH
477 fraction = src->bits.fraction;
478 exp = src->bits.exp;
479 sign = src->bits.sign;
480 #else
481 # if defined TFLOAT && defined HALFFRACBITS
483 halffractype high, low;
485 high = src->value_raw >> HALFSHIFT;
486 low = src->value_raw & (((fractype)1 << HALFSHIFT) - 1);
488 fraction = high & ((((fractype)1) << HALFFRACBITS) - 1);
489 fraction <<= FRACBITS - HALFFRACBITS;
490 exp = ((int)(high >> HALFFRACBITS)) & ((1 << EXPBITS) - 1);
491 sign = ((int)(high >> (((HALFFRACBITS + EXPBITS))))) & 1;
493 if (exp != EXPMAX && exp != 0 && low != 0)
495 int lowexp = ((int)(low >> HALFFRACBITS)) & ((1 << EXPBITS) - 1);
496 int lowsign = ((int)(low >> (((HALFFRACBITS + EXPBITS))))) & 1;
497 int shift;
498 fractype xlow;
500 xlow = low & ((((fractype)1) << HALFFRACBITS) - 1);
501 if (lowexp)
502 xlow |= (((halffractype)1) << HALFFRACBITS);
503 else
504 lowexp = 1;
505 shift = (FRACBITS - HALFFRACBITS) - (exp - lowexp);
506 if (shift > 0)
507 xlow <<= shift;
508 else if (shift < 0)
509 xlow >>= -shift;
510 if (sign == lowsign)
511 fraction += xlow;
512 else if (fraction >= xlow)
513 fraction -= xlow;
514 else
516 /* The high part is a power of two but the full number is lower.
517 This code will leave the implicit 1 in FRACTION, but we'd
518 have added that below anyway. */
519 fraction = (((fractype) 1 << FRACBITS) - xlow) << 1;
520 exp--;
524 # else
525 fraction = src->value_raw & ((((fractype)1) << FRACBITS) - 1);
526 exp = ((int)(src->value_raw >> FRACBITS)) & ((1 << EXPBITS) - 1);
527 sign = ((int)(src->value_raw >> (FRACBITS + EXPBITS))) & 1;
528 # endif
529 #endif
531 dst->sign = sign;
532 if (exp == 0)
534 /* Hmm. Looks like 0 */
535 if (fraction == 0
536 #ifdef NO_DENORMALS
537 || 1
538 #endif
541 /* tastes like zero */
542 dst->class = CLASS_ZERO;
544 else
546 /* Zero exponent with nonzero fraction - it's denormalized,
547 so there isn't a leading implicit one - we'll shift it so
548 it gets one. */
549 dst->normal_exp = exp - EXPBIAS + 1;
550 fraction <<= NGARDS;
552 dst->class = CLASS_NUMBER;
553 #if 1
554 while (fraction < IMPLICIT_1)
556 fraction <<= 1;
557 dst->normal_exp--;
559 #endif
560 dst->fraction.ll = fraction;
563 else if (!LARGEST_EXPONENT_IS_NORMAL (FRAC_NBITS) && exp == EXPMAX)
565 /* Huge exponent*/
566 if (fraction == 0)
568 /* Attached to a zero fraction - means infinity */
569 dst->class = CLASS_INFINITY;
571 else
573 /* Nonzero fraction, means nan */
574 #ifdef QUIET_NAN_NEGATED
575 if ((fraction & QUIET_NAN) == 0)
576 #else
577 if (fraction & QUIET_NAN)
578 #endif
580 dst->class = CLASS_QNAN;
582 else
584 dst->class = CLASS_SNAN;
586 /* Keep the fraction part as the nan number */
587 dst->fraction.ll = fraction;
590 else
592 /* Nothing strange about this number */
593 dst->normal_exp = exp - EXPBIAS;
594 dst->class = CLASS_NUMBER;
595 dst->fraction.ll = (fraction << NGARDS) | IMPLICIT_1;
598 #endif /* L_unpack_df || L_unpack_sf */
600 #if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
601 static fp_number_type *
602 _fpadd_parts (fp_number_type * a,
603 fp_number_type * b,
604 fp_number_type * tmp)
606 intfrac tfraction;
608 /* Put commonly used fields in local variables. */
609 int a_normal_exp;
610 int b_normal_exp;
611 fractype a_fraction;
612 fractype b_fraction;
614 if (isnan (a))
616 return a;
618 if (isnan (b))
620 return b;
622 if (isinf (a))
624 /* Adding infinities with opposite signs yields a NaN. */
625 if (isinf (b) && a->sign != b->sign)
626 return nan ();
627 return a;
629 if (isinf (b))
631 return b;
633 if (iszero (b))
635 if (iszero (a))
637 *tmp = *a;
638 tmp->sign = a->sign & b->sign;
639 return tmp;
641 return a;
643 if (iszero (a))
645 return b;
648 /* Got two numbers. shift the smaller and increment the exponent till
649 they're the same */
651 int diff;
653 a_normal_exp = a->normal_exp;
654 b_normal_exp = b->normal_exp;
655 a_fraction = a->fraction.ll;
656 b_fraction = b->fraction.ll;
658 diff = a_normal_exp - b_normal_exp;
660 if (diff < 0)
661 diff = -diff;
662 if (diff < FRAC_NBITS)
664 /* ??? This does shifts one bit at a time. Optimize. */
665 while (a_normal_exp > b_normal_exp)
667 b_normal_exp++;
668 LSHIFT (b_fraction);
670 while (b_normal_exp > a_normal_exp)
672 a_normal_exp++;
673 LSHIFT (a_fraction);
676 else
678 /* Somethings's up.. choose the biggest */
679 if (a_normal_exp > b_normal_exp)
681 b_normal_exp = a_normal_exp;
682 b_fraction = 0;
684 else
686 a_normal_exp = b_normal_exp;
687 a_fraction = 0;
692 if (a->sign != b->sign)
694 if (a->sign)
696 tfraction = -a_fraction + b_fraction;
698 else
700 tfraction = a_fraction - b_fraction;
702 if (tfraction >= 0)
704 tmp->sign = 0;
705 tmp->normal_exp = a_normal_exp;
706 tmp->fraction.ll = tfraction;
708 else
710 tmp->sign = 1;
711 tmp->normal_exp = a_normal_exp;
712 tmp->fraction.ll = -tfraction;
714 /* and renormalize it */
716 while (tmp->fraction.ll < IMPLICIT_1 && tmp->fraction.ll)
718 tmp->fraction.ll <<= 1;
719 tmp->normal_exp--;
722 else
724 tmp->sign = a->sign;
725 tmp->normal_exp = a_normal_exp;
726 tmp->fraction.ll = a_fraction + b_fraction;
728 tmp->class = CLASS_NUMBER;
729 /* Now the fraction is added, we have to shift down to renormalize the
730 number */
732 if (tmp->fraction.ll >= IMPLICIT_2)
734 LSHIFT (tmp->fraction.ll);
735 tmp->normal_exp++;
737 return tmp;
741 FLO_type
742 add (FLO_type arg_a, FLO_type arg_b)
744 fp_number_type a;
745 fp_number_type b;
746 fp_number_type tmp;
747 fp_number_type *res;
748 FLO_union_type au, bu;
750 au.value = arg_a;
751 bu.value = arg_b;
753 unpack_d (&au, &a);
754 unpack_d (&bu, &b);
756 res = _fpadd_parts (&a, &b, &tmp);
758 return pack_d (res);
761 FLO_type
762 sub (FLO_type arg_a, FLO_type arg_b)
764 fp_number_type a;
765 fp_number_type b;
766 fp_number_type tmp;
767 fp_number_type *res;
768 FLO_union_type au, bu;
770 au.value = arg_a;
771 bu.value = arg_b;
773 unpack_d (&au, &a);
774 unpack_d (&bu, &b);
776 b.sign ^= 1;
778 res = _fpadd_parts (&a, &b, &tmp);
780 return pack_d (res);
782 #endif /* L_addsub_sf || L_addsub_df */
784 #if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
785 static inline __attribute__ ((__always_inline__)) fp_number_type *
786 _fpmul_parts ( fp_number_type * a,
787 fp_number_type * b,
788 fp_number_type * tmp)
790 fractype low = 0;
791 fractype high = 0;
793 if (isnan (a))
795 a->sign = a->sign != b->sign;
796 return a;
798 if (isnan (b))
800 b->sign = a->sign != b->sign;
801 return b;
803 if (isinf (a))
805 if (iszero (b))
806 return nan ();
807 a->sign = a->sign != b->sign;
808 return a;
810 if (isinf (b))
812 if (iszero (a))
814 return nan ();
816 b->sign = a->sign != b->sign;
817 return b;
819 if (iszero (a))
821 a->sign = a->sign != b->sign;
822 return a;
824 if (iszero (b))
826 b->sign = a->sign != b->sign;
827 return b;
830 /* Calculate the mantissa by multiplying both numbers to get a
831 twice-as-wide number. */
833 #if defined(NO_DI_MODE) || defined(TFLOAT)
835 fractype x = a->fraction.ll;
836 fractype ylow = b->fraction.ll;
837 fractype yhigh = 0;
838 int bit;
840 /* ??? This does multiplies one bit at a time. Optimize. */
841 for (bit = 0; bit < FRAC_NBITS; bit++)
843 int carry;
845 if (x & 1)
847 carry = (low += ylow) < ylow;
848 high += yhigh + carry;
850 yhigh <<= 1;
851 if (ylow & FRACHIGH)
853 yhigh |= 1;
855 ylow <<= 1;
856 x >>= 1;
859 #elif defined(FLOAT)
860 /* Multiplying two USIs to get a UDI, we're safe. */
862 UDItype answer = (UDItype)a->fraction.ll * (UDItype)b->fraction.ll;
864 high = answer >> BITS_PER_SI;
865 low = answer;
867 #else
868 /* fractype is DImode, but we need the result to be twice as wide.
869 Assuming a widening multiply from DImode to TImode is not
870 available, build one by hand. */
872 USItype nl = a->fraction.ll;
873 USItype nh = a->fraction.ll >> BITS_PER_SI;
874 USItype ml = b->fraction.ll;
875 USItype mh = b->fraction.ll >> BITS_PER_SI;
876 UDItype pp_ll = (UDItype) ml * nl;
877 UDItype pp_hl = (UDItype) mh * nl;
878 UDItype pp_lh = (UDItype) ml * nh;
879 UDItype pp_hh = (UDItype) mh * nh;
880 UDItype res2 = 0;
881 UDItype res0 = 0;
882 UDItype ps_hh__ = pp_hl + pp_lh;
883 if (ps_hh__ < pp_hl)
884 res2 += (UDItype)1 << BITS_PER_SI;
885 pp_hl = (UDItype)(USItype)ps_hh__ << BITS_PER_SI;
886 res0 = pp_ll + pp_hl;
887 if (res0 < pp_ll)
888 res2++;
889 res2 += (ps_hh__ >> BITS_PER_SI) + pp_hh;
890 high = res2;
891 low = res0;
893 #endif
896 tmp->normal_exp = a->normal_exp + b->normal_exp
897 + FRAC_NBITS - (FRACBITS + NGARDS);
898 tmp->sign = a->sign != b->sign;
899 while (high >= IMPLICIT_2)
901 tmp->normal_exp++;
902 if (high & 1)
904 low >>= 1;
905 low |= FRACHIGH;
907 high >>= 1;
909 while (high < IMPLICIT_1)
911 tmp->normal_exp--;
913 high <<= 1;
914 if (low & FRACHIGH)
915 high |= 1;
916 low <<= 1;
919 if (!ROUND_TOWARDS_ZERO && (high & GARDMASK) == GARDMSB)
921 if (high & (1 << NGARDS))
923 /* Because we're half way, we would round to even by adding
924 GARDROUND + 1, except that's also done in the packing
925 function, and rounding twice will lose precision and cause
926 the result to be too far off. Example: 32-bit floats with
927 bit patterns 0xfff * 0x3f800400 ~= 0xfff (less than 0.5ulp
928 off), not 0x1000 (more than 0.5ulp off). */
930 else if (low)
932 /* We're a further than half way by a small amount corresponding
933 to the bits set in "low". Knowing that, we round here and
934 not in pack_d, because there we don't have "low" available
935 anymore. */
936 high += GARDROUND + 1;
938 /* Avoid further rounding in pack_d. */
939 high &= ~(fractype) GARDMASK;
942 tmp->fraction.ll = high;
943 tmp->class = CLASS_NUMBER;
944 return tmp;
947 FLO_type
948 multiply (FLO_type arg_a, FLO_type arg_b)
950 fp_number_type a;
951 fp_number_type b;
952 fp_number_type tmp;
953 fp_number_type *res;
954 FLO_union_type au, bu;
956 au.value = arg_a;
957 bu.value = arg_b;
959 unpack_d (&au, &a);
960 unpack_d (&bu, &b);
962 res = _fpmul_parts (&a, &b, &tmp);
964 return pack_d (res);
966 #endif /* L_mul_sf || L_mul_df || L_mul_tf */
968 #if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
969 static inline __attribute__ ((__always_inline__)) fp_number_type *
970 _fpdiv_parts (fp_number_type * a,
971 fp_number_type * b)
973 fractype bit;
974 fractype numerator;
975 fractype denominator;
976 fractype quotient;
978 if (isnan (a))
980 return a;
982 if (isnan (b))
984 return b;
987 a->sign = a->sign ^ b->sign;
989 if (isinf (a) || iszero (a))
991 if (a->class == b->class)
992 return nan ();
993 return a;
996 if (isinf (b))
998 a->fraction.ll = 0;
999 a->normal_exp = 0;
1000 return a;
1002 if (iszero (b))
1004 a->class = CLASS_INFINITY;
1005 return a;
1008 /* Calculate the mantissa by multiplying both 64bit numbers to get a
1009 128 bit number */
1011 /* quotient =
1012 ( numerator / denominator) * 2^(numerator exponent - denominator exponent)
1015 a->normal_exp = a->normal_exp - b->normal_exp;
1016 numerator = a->fraction.ll;
1017 denominator = b->fraction.ll;
1019 if (numerator < denominator)
1021 /* Fraction will be less than 1.0 */
1022 numerator *= 2;
1023 a->normal_exp--;
1025 bit = IMPLICIT_1;
1026 quotient = 0;
1027 /* ??? Does divide one bit at a time. Optimize. */
1028 while (bit)
1030 if (numerator >= denominator)
1032 quotient |= bit;
1033 numerator -= denominator;
1035 bit >>= 1;
1036 numerator *= 2;
1039 if (!ROUND_TOWARDS_ZERO && (quotient & GARDMASK) == GARDMSB)
1041 if (quotient & (1 << NGARDS))
1043 /* Because we're half way, we would round to even by adding
1044 GARDROUND + 1, except that's also done in the packing
1045 function, and rounding twice will lose precision and cause
1046 the result to be too far off. */
1048 else if (numerator)
1050 /* We're a further than half way by the small amount
1051 corresponding to the bits set in "numerator". Knowing
1052 that, we round here and not in pack_d, because there we
1053 don't have "numerator" available anymore. */
1054 quotient += GARDROUND + 1;
1056 /* Avoid further rounding in pack_d. */
1057 quotient &= ~(fractype) GARDMASK;
1061 a->fraction.ll = quotient;
1062 return (a);
1066 FLO_type
1067 divide (FLO_type arg_a, FLO_type arg_b)
1069 fp_number_type a;
1070 fp_number_type b;
1071 fp_number_type *res;
1072 FLO_union_type au, bu;
1074 au.value = arg_a;
1075 bu.value = arg_b;
1077 unpack_d (&au, &a);
1078 unpack_d (&bu, &b);
1080 res = _fpdiv_parts (&a, &b);
1082 return pack_d (res);
1084 #endif /* L_div_sf || L_div_df */
1086 #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df) \
1087 || defined(L_fpcmp_parts_tf)
1088 /* according to the demo, fpcmp returns a comparison with 0... thus
1089 a<b -> -1
1090 a==b -> 0
1091 a>b -> +1
1095 __fpcmp_parts (fp_number_type * a, fp_number_type * b)
1097 #if 0
1098 /* either nan -> unordered. Must be checked outside of this routine. */
1099 if (isnan (a) && isnan (b))
1101 return 1; /* still unordered! */
1103 #endif
1105 if (isnan (a) || isnan (b))
1107 return 1; /* how to indicate unordered compare? */
1109 if (isinf (a) && isinf (b))
1111 /* +inf > -inf, but +inf != +inf */
1112 /* b \a| +inf(0)| -inf(1)
1113 ______\+--------+--------
1114 +inf(0)| a==b(0)| a<b(-1)
1115 -------+--------+--------
1116 -inf(1)| a>b(1) | a==b(0)
1117 -------+--------+--------
1118 So since unordered must be nonzero, just line up the columns...
1120 return b->sign - a->sign;
1122 /* but not both... */
1123 if (isinf (a))
1125 return a->sign ? -1 : 1;
1127 if (isinf (b))
1129 return b->sign ? 1 : -1;
1131 if (iszero (a) && iszero (b))
1133 return 0;
1135 if (iszero (a))
1137 return b->sign ? 1 : -1;
1139 if (iszero (b))
1141 return a->sign ? -1 : 1;
1143 /* now both are "normal". */
1144 if (a->sign != b->sign)
1146 /* opposite signs */
1147 return a->sign ? -1 : 1;
1149 /* same sign; exponents? */
1150 if (a->normal_exp > b->normal_exp)
1152 return a->sign ? -1 : 1;
1154 if (a->normal_exp < b->normal_exp)
1156 return a->sign ? 1 : -1;
1158 /* same exponents; check size. */
1159 if (a->fraction.ll > b->fraction.ll)
1161 return a->sign ? -1 : 1;
1163 if (a->fraction.ll < b->fraction.ll)
1165 return a->sign ? 1 : -1;
1167 /* after all that, they're equal. */
1168 return 0;
1170 #endif
1172 #if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compoare_tf)
1173 CMPtype
1174 compare (FLO_type arg_a, FLO_type arg_b)
1176 fp_number_type a;
1177 fp_number_type b;
1178 FLO_union_type au, bu;
1180 au.value = arg_a;
1181 bu.value = arg_b;
1183 unpack_d (&au, &a);
1184 unpack_d (&bu, &b);
1186 return __fpcmp_parts (&a, &b);
1188 #endif /* L_compare_sf || L_compare_df */
1190 #ifndef US_SOFTWARE_GOFAST
1192 /* These should be optimized for their specific tasks someday. */
1194 #if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
1195 CMPtype
1196 _eq_f2 (FLO_type arg_a, FLO_type arg_b)
1198 fp_number_type a;
1199 fp_number_type b;
1200 FLO_union_type au, bu;
1202 au.value = arg_a;
1203 bu.value = arg_b;
1205 unpack_d (&au, &a);
1206 unpack_d (&bu, &b);
1208 if (isnan (&a) || isnan (&b))
1209 return 1; /* false, truth == 0 */
1211 return __fpcmp_parts (&a, &b) ;
1213 #endif /* L_eq_sf || L_eq_df */
1215 #if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
1216 CMPtype
1217 _ne_f2 (FLO_type arg_a, FLO_type arg_b)
1219 fp_number_type a;
1220 fp_number_type b;
1221 FLO_union_type au, bu;
1223 au.value = arg_a;
1224 bu.value = arg_b;
1226 unpack_d (&au, &a);
1227 unpack_d (&bu, &b);
1229 if (isnan (&a) || isnan (&b))
1230 return 1; /* true, truth != 0 */
1232 return __fpcmp_parts (&a, &b) ;
1234 #endif /* L_ne_sf || L_ne_df */
1236 #if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
1237 CMPtype
1238 _gt_f2 (FLO_type arg_a, FLO_type arg_b)
1240 fp_number_type a;
1241 fp_number_type b;
1242 FLO_union_type au, bu;
1244 au.value = arg_a;
1245 bu.value = arg_b;
1247 unpack_d (&au, &a);
1248 unpack_d (&bu, &b);
1250 if (isnan (&a) || isnan (&b))
1251 return -1; /* false, truth > 0 */
1253 return __fpcmp_parts (&a, &b);
1255 #endif /* L_gt_sf || L_gt_df */
1257 #if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
1258 CMPtype
1259 _ge_f2 (FLO_type arg_a, FLO_type arg_b)
1261 fp_number_type a;
1262 fp_number_type b;
1263 FLO_union_type au, bu;
1265 au.value = arg_a;
1266 bu.value = arg_b;
1268 unpack_d (&au, &a);
1269 unpack_d (&bu, &b);
1271 if (isnan (&a) || isnan (&b))
1272 return -1; /* false, truth >= 0 */
1273 return __fpcmp_parts (&a, &b) ;
1275 #endif /* L_ge_sf || L_ge_df */
1277 #if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
1278 CMPtype
1279 _lt_f2 (FLO_type arg_a, FLO_type arg_b)
1281 fp_number_type a;
1282 fp_number_type b;
1283 FLO_union_type au, bu;
1285 au.value = arg_a;
1286 bu.value = arg_b;
1288 unpack_d (&au, &a);
1289 unpack_d (&bu, &b);
1291 if (isnan (&a) || isnan (&b))
1292 return 1; /* false, truth < 0 */
1294 return __fpcmp_parts (&a, &b);
1296 #endif /* L_lt_sf || L_lt_df */
1298 #if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
1299 CMPtype
1300 _le_f2 (FLO_type arg_a, FLO_type arg_b)
1302 fp_number_type a;
1303 fp_number_type b;
1304 FLO_union_type au, bu;
1306 au.value = arg_a;
1307 bu.value = arg_b;
1309 unpack_d (&au, &a);
1310 unpack_d (&bu, &b);
1312 if (isnan (&a) || isnan (&b))
1313 return 1; /* false, truth <= 0 */
1315 return __fpcmp_parts (&a, &b) ;
1317 #endif /* L_le_sf || L_le_df */
1319 #endif /* ! US_SOFTWARE_GOFAST */
1321 #if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
1322 CMPtype
1323 _unord_f2 (FLO_type arg_a, FLO_type arg_b)
1325 fp_number_type a;
1326 fp_number_type b;
1327 FLO_union_type au, bu;
1329 au.value = arg_a;
1330 bu.value = arg_b;
1332 unpack_d (&au, &a);
1333 unpack_d (&bu, &b);
1335 return (isnan (&a) || isnan (&b));
1337 #endif /* L_unord_sf || L_unord_df */
1339 #if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
1340 FLO_type
1341 si_to_float (SItype arg_a)
1343 fp_number_type in;
1345 in.class = CLASS_NUMBER;
1346 in.sign = arg_a < 0;
1347 if (!arg_a)
1349 in.class = CLASS_ZERO;
1351 else
1353 USItype uarg;
1354 int shift;
1355 in.normal_exp = FRACBITS + NGARDS;
1356 if (in.sign)
1358 /* Special case for minint, since there is no +ve integer
1359 representation for it */
1360 if (arg_a == (- MAX_SI_INT - 1))
1362 return (FLO_type)(- MAX_SI_INT - 1);
1364 uarg = (-arg_a);
1366 else
1367 uarg = arg_a;
1369 in.fraction.ll = uarg;
1370 shift = clzusi (uarg) - (BITS_PER_SI - 1 - FRACBITS - NGARDS);
1371 if (shift > 0)
1373 in.fraction.ll <<= shift;
1374 in.normal_exp -= shift;
1377 return pack_d (&in);
1379 #endif /* L_si_to_sf || L_si_to_df */
1381 #if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
1382 FLO_type
1383 usi_to_float (USItype arg_a)
1385 fp_number_type in;
1387 in.sign = 0;
1388 if (!arg_a)
1390 in.class = CLASS_ZERO;
1392 else
1394 int shift;
1395 in.class = CLASS_NUMBER;
1396 in.normal_exp = FRACBITS + NGARDS;
1397 in.fraction.ll = arg_a;
1399 shift = clzusi (arg_a) - (BITS_PER_SI - 1 - FRACBITS - NGARDS);
1400 if (shift < 0)
1402 fractype guard = in.fraction.ll & (((fractype)1 << -shift) - 1);
1403 in.fraction.ll >>= -shift;
1404 in.fraction.ll |= (guard != 0);
1405 in.normal_exp -= shift;
1407 else if (shift > 0)
1409 in.fraction.ll <<= shift;
1410 in.normal_exp -= shift;
1413 return pack_d (&in);
1415 #endif
1417 #if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
1418 SItype
1419 float_to_si (FLO_type arg_a)
1421 fp_number_type a;
1422 SItype tmp;
1423 FLO_union_type au;
1425 au.value = arg_a;
1426 unpack_d (&au, &a);
1428 if (iszero (&a))
1429 return 0;
1430 if (isnan (&a))
1431 return 0;
1432 /* get reasonable MAX_SI_INT... */
1433 if (isinf (&a))
1434 return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1435 /* it is a number, but a small one */
1436 if (a.normal_exp < 0)
1437 return 0;
1438 if (a.normal_exp > BITS_PER_SI - 2)
1439 return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
1440 tmp = a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1441 return a.sign ? (-tmp) : (tmp);
1443 #endif /* L_sf_to_si || L_df_to_si */
1445 #if defined(L_sf_to_usi) || defined(L_df_to_usi) || defined(L_tf_to_usi)
1446 #if defined US_SOFTWARE_GOFAST || defined(L_tf_to_usi)
1447 /* While libgcc2.c defines its own __fixunssfsi and __fixunsdfsi routines,
1448 we also define them for GOFAST because the ones in libgcc2.c have the
1449 wrong names and I'd rather define these here and keep GOFAST CYG-LOC's
1450 out of libgcc2.c. We can't define these here if not GOFAST because then
1451 there'd be duplicate copies. */
1453 USItype
1454 float_to_usi (FLO_type arg_a)
1456 fp_number_type a;
1457 FLO_union_type au;
1459 au.value = arg_a;
1460 unpack_d (&au, &a);
1462 if (iszero (&a))
1463 return 0;
1464 if (isnan (&a))
1465 return 0;
1466 /* it is a negative number */
1467 if (a.sign)
1468 return 0;
1469 /* get reasonable MAX_USI_INT... */
1470 if (isinf (&a))
1471 return MAX_USI_INT;
1472 /* it is a number, but a small one */
1473 if (a.normal_exp < 0)
1474 return 0;
1475 if (a.normal_exp > BITS_PER_SI - 1)
1476 return MAX_USI_INT;
1477 else if (a.normal_exp > (FRACBITS + NGARDS))
1478 return a.fraction.ll << (a.normal_exp - (FRACBITS + NGARDS));
1479 else
1480 return a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
1482 #endif /* US_SOFTWARE_GOFAST */
1483 #endif /* L_sf_to_usi || L_df_to_usi */
1485 #if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
1486 FLO_type
1487 negate (FLO_type arg_a)
1489 fp_number_type a;
1490 FLO_union_type au;
1492 au.value = arg_a;
1493 unpack_d (&au, &a);
1495 flip_sign (&a);
1496 return pack_d (&a);
1498 #endif /* L_negate_sf || L_negate_df */
1500 #ifdef FLOAT
1502 #if defined(L_make_sf)
1503 SFtype
1504 __make_fp(fp_class_type class,
1505 unsigned int sign,
1506 int exp,
1507 USItype frac)
1509 fp_number_type in;
1511 in.class = class;
1512 in.sign = sign;
1513 in.normal_exp = exp;
1514 in.fraction.ll = frac;
1515 return pack_d (&in);
1517 #endif /* L_make_sf */
1519 #ifndef FLOAT_ONLY
1521 /* This enables one to build an fp library that supports float but not double.
1522 Otherwise, we would get an undefined reference to __make_dp.
1523 This is needed for some 8-bit ports that can't handle well values that
1524 are 8-bytes in size, so we just don't support double for them at all. */
1526 #if defined(L_sf_to_df)
1527 DFtype
1528 sf_to_df (SFtype arg_a)
1530 fp_number_type in;
1531 FLO_union_type au;
1533 au.value = arg_a;
1534 unpack_d (&au, &in);
1536 return __make_dp (in.class, in.sign, in.normal_exp,
1537 ((UDItype) in.fraction.ll) << F_D_BITOFF);
1539 #endif /* L_sf_to_df */
1541 #if defined(L_sf_to_tf) && defined(TMODES)
1542 TFtype
1543 sf_to_tf (SFtype arg_a)
1545 fp_number_type in;
1546 FLO_union_type au;
1548 au.value = arg_a;
1549 unpack_d (&au, &in);
1551 return __make_tp (in.class, in.sign, in.normal_exp,
1552 ((UTItype) in.fraction.ll) << F_T_BITOFF);
1554 #endif /* L_sf_to_df */
1556 #endif /* ! FLOAT_ONLY */
1557 #endif /* FLOAT */
1559 #ifndef FLOAT
1561 extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
1563 #if defined(L_make_df)
1564 DFtype
1565 __make_dp (fp_class_type class, unsigned int sign, int exp, UDItype frac)
1567 fp_number_type in;
1569 in.class = class;
1570 in.sign = sign;
1571 in.normal_exp = exp;
1572 in.fraction.ll = frac;
1573 return pack_d (&in);
1575 #endif /* L_make_df */
1577 #if defined(L_df_to_sf)
1578 SFtype
1579 df_to_sf (DFtype arg_a)
1581 fp_number_type in;
1582 USItype sffrac;
1583 FLO_union_type au;
1585 au.value = arg_a;
1586 unpack_d (&au, &in);
1588 sffrac = in.fraction.ll >> F_D_BITOFF;
1590 /* We set the lowest guard bit in SFFRAC if we discarded any non
1591 zero bits. */
1592 if ((in.fraction.ll & (((USItype) 1 << F_D_BITOFF) - 1)) != 0)
1593 sffrac |= 1;
1595 return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
1597 #endif /* L_df_to_sf */
1599 #if defined(L_df_to_tf) && defined(TMODES) \
1600 && !defined(FLOAT) && !defined(TFLOAT)
1601 TFtype
1602 df_to_tf (DFtype arg_a)
1604 fp_number_type in;
1605 FLO_union_type au;
1607 au.value = arg_a;
1608 unpack_d (&au, &in);
1610 return __make_tp (in.class, in.sign, in.normal_exp,
1611 ((UTItype) in.fraction.ll) << D_T_BITOFF);
1613 #endif /* L_sf_to_df */
1615 #ifdef TFLOAT
1616 #if defined(L_make_tf)
1617 TFtype
1618 __make_tp(fp_class_type class,
1619 unsigned int sign,
1620 int exp,
1621 UTItype frac)
1623 fp_number_type in;
1625 in.class = class;
1626 in.sign = sign;
1627 in.normal_exp = exp;
1628 in.fraction.ll = frac;
1629 return pack_d (&in);
1631 #endif /* L_make_tf */
1633 #if defined(L_tf_to_df)
1634 DFtype
1635 tf_to_df (TFtype arg_a)
1637 fp_number_type in;
1638 UDItype sffrac;
1639 FLO_union_type au;
1641 au.value = arg_a;
1642 unpack_d (&au, &in);
1644 sffrac = in.fraction.ll >> D_T_BITOFF;
1646 /* We set the lowest guard bit in SFFRAC if we discarded any non
1647 zero bits. */
1648 if ((in.fraction.ll & (((UTItype) 1 << D_T_BITOFF) - 1)) != 0)
1649 sffrac |= 1;
1651 return __make_dp (in.class, in.sign, in.normal_exp, sffrac);
1653 #endif /* L_tf_to_df */
1655 #if defined(L_tf_to_sf)
1656 SFtype
1657 tf_to_sf (TFtype arg_a)
1659 fp_number_type in;
1660 USItype sffrac;
1661 FLO_union_type au;
1663 au.value = arg_a;
1664 unpack_d (&au, &in);
1666 sffrac = in.fraction.ll >> F_T_BITOFF;
1668 /* We set the lowest guard bit in SFFRAC if we discarded any non
1669 zero bits. */
1670 if ((in.fraction.ll & (((UTItype) 1 << F_T_BITOFF) - 1)) != 0)
1671 sffrac |= 1;
1673 return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
1675 #endif /* L_tf_to_sf */
1676 #endif /* TFLOAT */
1678 #endif /* ! FLOAT */
1679 #endif /* !EXTENDED_FLOAT_STUBS */