Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / gcc / libgcc2.c
blob68623c371223ac22f2ece352c4c50445274e2e6d
1 /* More subroutines needed by GCC output code on some machines. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 2000, 2001, 2002, 2003, 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, 59 Temple Place - Suite 330, Boston, MA
30 02111-1307, USA. */
33 /* We include auto-host.h here to get HAVE_GAS_HIDDEN. This is
34 supposedly valid even though this is a "target" file. */
35 #include "auto-host.h"
37 /* It is incorrect to include config.h here, because this file is being
38 compiled for the target, and hence definitions concerning only the host
39 do not apply. */
40 #include "tconfig.h"
41 #include "tsystem.h"
42 #include "coretypes.h"
43 #include "tm.h"
45 /* Don't use `fancy_abort' here even if config.h says to use it. */
46 #ifdef abort
47 #undef abort
48 #endif
50 #ifdef HAVE_GAS_HIDDEN
51 #define ATTRIBUTE_HIDDEN __attribute__ ((__visibility__ ("hidden")))
52 #else
53 #define ATTRIBUTE_HIDDEN
54 #endif
56 #include "libgcc2.h"
58 #ifdef DECLARE_LIBRARY_RENAMES
59 DECLARE_LIBRARY_RENAMES
60 #endif
62 #if defined (L_negdi2)
63 DWtype
64 __negdi2 (DWtype u)
66 const DWunion uu = {.ll = u};
67 const DWunion w = { {.low = -uu.s.low,
68 .high = -uu.s.high - ((UWtype) -uu.s.low > 0) } };
70 return w.ll;
72 #endif
74 #ifdef L_addvsi3
75 Wtype
76 __addvSI3 (Wtype a, Wtype b)
78 const Wtype w = a + b;
80 if (b >= 0 ? w < a : w > a)
81 abort ();
83 return w;
85 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
86 SItype
87 __addvsi3 (SItype a, SItype b)
89 const SItype w = a + b;
91 if (b >= 0 ? w < a : w > a)
92 abort ();
94 return w;
96 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
97 #endif
99 #ifdef L_addvdi3
100 DWtype
101 __addvDI3 (DWtype a, DWtype b)
103 const DWtype w = a + b;
105 if (b >= 0 ? w < a : w > a)
106 abort ();
108 return w;
110 #endif
112 #ifdef L_subvsi3
113 Wtype
114 __subvSI3 (Wtype a, Wtype b)
116 const Wtype w = a - b;
118 if (b >= 0 ? w > a : w < a)
119 abort ();
121 return w;
123 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
124 SItype
125 __subvsi3 (SItype a, SItype b)
127 const SItype w = a - b;
129 if (b >= 0 ? w > a : w < a)
130 abort ();
132 return w;
134 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
135 #endif
137 #ifdef L_subvdi3
138 DWtype
139 __subvDI3 (DWtype a, DWtype b)
141 const DWtype w = a - b;
143 if (b >= 0 ? w > a : w < a)
144 abort ();
146 return w;
148 #endif
150 #ifdef L_mulvsi3
151 Wtype
152 __mulvSI3 (Wtype a, Wtype b)
154 const DWtype w = (DWtype) a * (DWtype) b;
156 if ((Wtype) (w >> W_TYPE_SIZE) != (Wtype) w >> (W_TYPE_SIZE - 1))
157 abort ();
159 return w;
161 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
162 #undef WORD_SIZE
163 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
164 SItype
165 __mulvsi3 (SItype a, SItype b)
167 const DItype w = (DItype) a * (DItype) b;
169 if ((SItype) (w >> WORD_SIZE) != (SItype) w >> (WORD_SIZE-1))
170 abort ();
172 return w;
174 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
175 #endif
177 #ifdef L_negvsi2
178 Wtype
179 __negvSI2 (Wtype a)
181 const Wtype w = -a;
183 if (a >= 0 ? w > 0 : w < 0)
184 abort ();
186 return w;
188 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
189 SItype
190 __negvsi2 (SItype a)
192 const SItype w = -a;
194 if (a >= 0 ? w > 0 : w < 0)
195 abort ();
197 return w;
199 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
200 #endif
202 #ifdef L_negvdi2
203 DWtype
204 __negvDI2 (DWtype a)
206 const DWtype w = -a;
208 if (a >= 0 ? w > 0 : w < 0)
209 abort ();
211 return w;
213 #endif
215 #ifdef L_absvsi2
216 Wtype
217 __absvSI2 (Wtype a)
219 Wtype w = a;
221 if (a < 0)
222 #ifdef L_negvsi2
223 w = __negvSI2 (a);
224 #else
225 w = -a;
227 if (w < 0)
228 abort ();
229 #endif
231 return w;
233 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
234 SItype
235 __absvsi2 (SItype a)
237 SItype w = a;
239 if (a < 0)
240 #ifdef L_negvsi2
241 w = __negvsi2 (a);
242 #else
243 w = -a;
245 if (w < 0)
246 abort ();
247 #endif
249 return w;
251 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
252 #endif
254 #ifdef L_absvdi2
255 DWtype
256 __absvDI2 (DWtype a)
258 DWtype w = a;
260 if (a < 0)
261 #ifdef L_negvdi2
262 w = __negvDI2 (a);
263 #else
264 w = -a;
266 if (w < 0)
267 abort ();
268 #endif
270 return w;
272 #endif
274 #ifdef L_mulvdi3
275 DWtype
276 __mulvDI3 (DWtype u, DWtype v)
278 /* The unchecked multiplication needs 3 Wtype x Wtype multiplications,
279 but the checked multiplication needs only two. */
280 const DWunion uu = {.ll = u};
281 const DWunion vv = {.ll = v};
283 if (__builtin_expect (uu.s.high == uu.s.low >> (W_TYPE_SIZE - 1), 1))
285 /* u fits in a single Wtype. */
286 if (__builtin_expect (vv.s.high == vv.s.low >> (W_TYPE_SIZE - 1), 1))
288 /* v fits in a single Wtype as well. */
289 /* A single multiplication. No overflow risk. */
290 return (DWtype) uu.s.low * (DWtype) vv.s.low;
292 else
294 /* Two multiplications. */
295 DWunion w0 = {.ll = (UDWtype) (UWtype) uu.s.low
296 * (UDWtype) (UWtype) vv.s.low};
297 DWunion w1 = {.ll = (UDWtype) (UWtype) uu.s.low
298 * (UDWtype) (UWtype) vv.s.high};
300 if (vv.s.high < 0)
301 w1.s.high -= uu.s.low;
302 if (uu.s.low < 0)
303 w1.ll -= vv.ll;
304 w1.ll += (UWtype) w0.s.high;
305 if (__builtin_expect (w1.s.high == w1.s.low >> (W_TYPE_SIZE - 1), 1))
307 w0.s.high = w1.s.low;
308 return w0.ll;
312 else
314 if (__builtin_expect (vv.s.high == vv.s.low >> (W_TYPE_SIZE - 1), 1))
316 /* v fits into a single Wtype. */
317 /* Two multiplications. */
318 DWunion w0 = {.ll = (UDWtype) (UWtype) uu.s.low
319 * (UDWtype) (UWtype) vv.s.low};
320 DWunion w1 = {.ll = (UDWtype) (UWtype) uu.s.high
321 * (UDWtype) (UWtype) vv.s.low};
323 if (uu.s.high < 0)
324 w1.s.high -= vv.s.low;
325 if (vv.s.low < 0)
326 w1.ll -= uu.ll;
327 w1.ll += (UWtype) w0.s.high;
328 if (__builtin_expect (w1.s.high == w1.s.low >> (W_TYPE_SIZE - 1), 1))
330 w0.s.high = w1.s.low;
331 return w0.ll;
334 else
336 /* A few sign checks and a single multiplication. */
337 if (uu.s.high >= 0)
339 if (vv.s.high >= 0)
341 if (uu.s.high == 0 && vv.s.high == 0)
343 const DWtype w = (UDWtype) (UWtype) uu.s.low
344 * (UDWtype) (UWtype) vv.s.low;
345 if (__builtin_expect (w >= 0, 1))
346 return w;
349 else
351 if (uu.s.high == 0 && vv.s.high == (Wtype) -1)
353 DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
354 * (UDWtype) (UWtype) vv.s.low};
356 ww.s.high -= uu.s.low;
357 if (__builtin_expect (ww.s.high < 0, 1))
358 return ww.ll;
362 else
364 if (vv.s.high >= 0)
366 if (uu.s.high == (Wtype) -1 && vv.s.high == 0)
368 DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
369 * (UDWtype) (UWtype) vv.s.low};
371 ww.s.high -= vv.s.low;
372 if (__builtin_expect (ww.s.high < 0, 1))
373 return ww.ll;
376 else
378 if (uu.s.high == (Wtype) -1 && vv.s.high == (Wtype) - 1)
380 DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
381 * (UDWtype) (UWtype) vv.s.low};
383 ww.s.high -= uu.s.low;
384 ww.s.high -= vv.s.low;
385 if (__builtin_expect (ww.s.high >= 0, 1))
386 return ww.ll;
393 /* Overflow. */
394 abort ();
396 #endif
399 /* Unless shift functions are defined with full ANSI prototypes,
400 parameter b will be promoted to int if word_type is smaller than an int. */
401 #ifdef L_lshrdi3
402 DWtype
403 __lshrdi3 (DWtype u, word_type b)
405 if (b == 0)
406 return u;
408 const DWunion uu = {.ll = u};
409 const word_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
410 DWunion w;
412 if (bm <= 0)
414 w.s.high = 0;
415 w.s.low = (UWtype) uu.s.high >> -bm;
417 else
419 const UWtype carries = (UWtype) uu.s.high << bm;
421 w.s.high = (UWtype) uu.s.high >> b;
422 w.s.low = ((UWtype) uu.s.low >> b) | carries;
425 return w.ll;
427 #endif
429 #ifdef L_ashldi3
430 DWtype
431 __ashldi3 (DWtype u, word_type b)
433 if (b == 0)
434 return u;
436 const DWunion uu = {.ll = u};
437 const word_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
438 DWunion w;
440 if (bm <= 0)
442 w.s.low = 0;
443 w.s.high = (UWtype) uu.s.low << -bm;
445 else
447 const UWtype carries = (UWtype) uu.s.low >> bm;
449 w.s.low = (UWtype) uu.s.low << b;
450 w.s.high = ((UWtype) uu.s.high << b) | carries;
453 return w.ll;
455 #endif
457 #ifdef L_ashrdi3
458 DWtype
459 __ashrdi3 (DWtype u, word_type b)
461 if (b == 0)
462 return u;
464 const DWunion uu = {.ll = u};
465 const word_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
466 DWunion w;
468 if (bm <= 0)
470 /* w.s.high = 1..1 or 0..0 */
471 w.s.high = uu.s.high >> (sizeof (Wtype) * BITS_PER_UNIT - 1);
472 w.s.low = uu.s.high >> -bm;
474 else
476 const UWtype carries = (UWtype) uu.s.high << bm;
478 w.s.high = uu.s.high >> b;
479 w.s.low = ((UWtype) uu.s.low >> b) | carries;
482 return w.ll;
484 #endif
486 #ifdef L_ffssi2
487 #undef int
489 __ffsSI2 (UWtype u)
491 UWtype count;
493 if (u == 0)
494 return 0;
496 count_trailing_zeros (count, u);
497 return count + 1;
499 #endif
501 #ifdef L_ffsdi2
502 #undef int
504 __ffsDI2 (DWtype u)
506 const DWunion uu = {.ll = u};
507 UWtype word, count, add;
509 if (uu.s.low != 0)
510 word = uu.s.low, add = 0;
511 else if (uu.s.high != 0)
512 word = uu.s.high, add = BITS_PER_UNIT * sizeof (Wtype);
513 else
514 return 0;
516 count_trailing_zeros (count, word);
517 return count + add + 1;
519 #endif
521 #ifdef L_muldi3
522 DWtype
523 __muldi3 (DWtype u, DWtype v)
525 const DWunion uu = {.ll = u};
526 const DWunion vv = {.ll = v};
527 DWunion w = {.ll = __umulsidi3 (uu.s.low, vv.s.low)};
529 w.s.high += ((UWtype) uu.s.low * (UWtype) vv.s.high
530 + (UWtype) uu.s.high * (UWtype) vv.s.low);
532 return w.ll;
534 #endif
536 #if (defined (L_udivdi3) || defined (L_divdi3) || \
537 defined (L_umoddi3) || defined (L_moddi3))
538 #if defined (sdiv_qrnnd)
539 #define L_udiv_w_sdiv
540 #endif
541 #endif
543 #ifdef L_udiv_w_sdiv
544 #if defined (sdiv_qrnnd)
545 #if (defined (L_udivdi3) || defined (L_divdi3) || \
546 defined (L_umoddi3) || defined (L_moddi3))
547 static inline __attribute__ ((__always_inline__))
548 #endif
549 UWtype
550 __udiv_w_sdiv (UWtype *rp, UWtype a1, UWtype a0, UWtype d)
552 UWtype q, r;
553 UWtype c0, c1, b1;
555 if ((Wtype) d >= 0)
557 if (a1 < d - a1 - (a0 >> (W_TYPE_SIZE - 1)))
559 /* Dividend, divisor, and quotient are nonnegative. */
560 sdiv_qrnnd (q, r, a1, a0, d);
562 else
564 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d. */
565 sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (W_TYPE_SIZE - 1));
566 /* Divide (c1*2^32 + c0) by d. */
567 sdiv_qrnnd (q, r, c1, c0, d);
568 /* Add 2^31 to quotient. */
569 q += (UWtype) 1 << (W_TYPE_SIZE - 1);
572 else
574 b1 = d >> 1; /* d/2, between 2^30 and 2^31 - 1 */
575 c1 = a1 >> 1; /* A/2 */
576 c0 = (a1 << (W_TYPE_SIZE - 1)) + (a0 >> 1);
578 if (a1 < b1) /* A < 2^32*b1, so A/2 < 2^31*b1 */
580 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
582 r = 2*r + (a0 & 1); /* Remainder from A/(2*b1) */
583 if ((d & 1) != 0)
585 if (r >= q)
586 r = r - q;
587 else if (q - r <= d)
589 r = r - q + d;
590 q--;
592 else
594 r = r - q + 2*d;
595 q -= 2;
599 else if (c1 < b1) /* So 2^31 <= (A/2)/b1 < 2^32 */
601 c1 = (b1 - 1) - c1;
602 c0 = ~c0; /* logical NOT */
604 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
606 q = ~q; /* (A/2)/b1 */
607 r = (b1 - 1) - r;
609 r = 2*r + (a0 & 1); /* A/(2*b1) */
611 if ((d & 1) != 0)
613 if (r >= q)
614 r = r - q;
615 else if (q - r <= d)
617 r = r - q + d;
618 q--;
620 else
622 r = r - q + 2*d;
623 q -= 2;
627 else /* Implies c1 = b1 */
628 { /* Hence a1 = d - 1 = 2*b1 - 1 */
629 if (a0 >= -d)
631 q = -1;
632 r = a0 + d;
634 else
636 q = -2;
637 r = a0 + 2*d;
642 *rp = r;
643 return q;
645 #else
646 /* If sdiv_qrnnd doesn't exist, define dummy __udiv_w_sdiv. */
647 UWtype
648 __udiv_w_sdiv (UWtype *rp __attribute__ ((__unused__)),
649 UWtype a1 __attribute__ ((__unused__)),
650 UWtype a0 __attribute__ ((__unused__)),
651 UWtype d __attribute__ ((__unused__)))
653 return 0;
655 #endif
656 #endif
658 #if (defined (L_udivdi3) || defined (L_divdi3) || \
659 defined (L_umoddi3) || defined (L_moddi3))
660 #define L_udivmoddi4
661 #endif
663 #ifdef L_clz
664 const UQItype __clz_tab[] =
666 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
667 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
668 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
669 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
670 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
671 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
672 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
673 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
675 #endif
677 #ifdef L_clzsi2
678 #undef int
680 __clzSI2 (UWtype x)
682 Wtype ret;
684 count_leading_zeros (ret, x);
686 return ret;
688 #endif
690 #ifdef L_clzdi2
691 #undef int
693 __clzDI2 (UDWtype x)
695 const DWunion uu = {.ll = x};
696 UWtype word;
697 Wtype ret, add;
699 if (uu.s.high)
700 word = uu.s.high, add = 0;
701 else
702 word = uu.s.low, add = W_TYPE_SIZE;
704 count_leading_zeros (ret, word);
705 return ret + add;
707 #endif
709 #ifdef L_ctzsi2
710 #undef int
712 __ctzSI2 (UWtype x)
714 Wtype ret;
716 count_trailing_zeros (ret, x);
718 return ret;
720 #endif
722 #ifdef L_ctzdi2
723 #undef int
725 __ctzDI2 (UDWtype x)
727 const DWunion uu = {.ll = x};
728 UWtype word;
729 Wtype ret, add;
731 if (uu.s.low)
732 word = uu.s.low, add = 0;
733 else
734 word = uu.s.high, add = W_TYPE_SIZE;
736 count_trailing_zeros (ret, word);
737 return ret + add;
739 #endif
741 #if (defined (L_popcountsi2) || defined (L_popcountdi2) \
742 || defined (L_popcount_tab))
743 extern const UQItype __popcount_tab[] ATTRIBUTE_HIDDEN;
744 #endif
746 #ifdef L_popcount_tab
747 const UQItype __popcount_tab[] =
749 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
750 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
751 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
752 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
753 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
754 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
755 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
756 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
758 #endif
760 #ifdef L_popcountsi2
761 #undef int
763 __popcountSI2 (UWtype x)
765 UWtype i, ret = 0;
767 for (i = 0; i < W_TYPE_SIZE; i += 8)
768 ret += __popcount_tab[(x >> i) & 0xff];
770 return ret;
772 #endif
774 #ifdef L_popcountdi2
775 #undef int
777 __popcountDI2 (UDWtype x)
779 UWtype i, ret = 0;
781 for (i = 0; i < 2*W_TYPE_SIZE; i += 8)
782 ret += __popcount_tab[(x >> i) & 0xff];
784 return ret;
786 #endif
788 #ifdef L_paritysi2
789 #undef int
791 __paritySI2 (UWtype x)
793 #if W_TYPE_SIZE > 64
794 # error "fill out the table"
795 #endif
796 #if W_TYPE_SIZE > 32
797 x ^= x >> 32;
798 #endif
799 #if W_TYPE_SIZE > 16
800 x ^= x >> 16;
801 #endif
802 x ^= x >> 8;
803 x ^= x >> 4;
804 x &= 0xf;
805 return (0x6996 >> x) & 1;
807 #endif
809 #ifdef L_paritydi2
810 #undef int
812 __parityDI2 (UDWtype x)
814 const DWunion uu = {.ll = x};
815 UWtype nx = uu.s.low ^ uu.s.high;
817 #if W_TYPE_SIZE > 64
818 # error "fill out the table"
819 #endif
820 #if W_TYPE_SIZE > 32
821 nx ^= nx >> 32;
822 #endif
823 #if W_TYPE_SIZE > 16
824 nx ^= nx >> 16;
825 #endif
826 nx ^= nx >> 8;
827 nx ^= nx >> 4;
828 nx &= 0xf;
829 return (0x6996 >> nx) & 1;
831 #endif
833 #ifdef L_udivmoddi4
835 #if (defined (L_udivdi3) || defined (L_divdi3) || \
836 defined (L_umoddi3) || defined (L_moddi3))
837 static inline __attribute__ ((__always_inline__))
838 #endif
839 UDWtype
840 __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
842 const DWunion nn = {.ll = n};
843 const DWunion dd = {.ll = d};
844 DWunion rr;
845 UWtype d0, d1, n0, n1, n2;
846 UWtype q0, q1;
847 UWtype b, bm;
849 d0 = dd.s.low;
850 d1 = dd.s.high;
851 n0 = nn.s.low;
852 n1 = nn.s.high;
854 #if !UDIV_NEEDS_NORMALIZATION
855 if (d1 == 0)
857 if (d0 > n1)
859 /* 0q = nn / 0D */
861 udiv_qrnnd (q0, n0, n1, n0, d0);
862 q1 = 0;
864 /* Remainder in n0. */
866 else
868 /* qq = NN / 0d */
870 if (d0 == 0)
871 d0 = 1 / d0; /* Divide intentionally by zero. */
873 udiv_qrnnd (q1, n1, 0, n1, d0);
874 udiv_qrnnd (q0, n0, n1, n0, d0);
876 /* Remainder in n0. */
879 if (rp != 0)
881 rr.s.low = n0;
882 rr.s.high = 0;
883 *rp = rr.ll;
887 #else /* UDIV_NEEDS_NORMALIZATION */
889 if (d1 == 0)
891 if (d0 > n1)
893 /* 0q = nn / 0D */
895 count_leading_zeros (bm, d0);
897 if (bm != 0)
899 /* Normalize, i.e. make the most significant bit of the
900 denominator set. */
902 d0 = d0 << bm;
903 n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm));
904 n0 = n0 << bm;
907 udiv_qrnnd (q0, n0, n1, n0, d0);
908 q1 = 0;
910 /* Remainder in n0 >> bm. */
912 else
914 /* qq = NN / 0d */
916 if (d0 == 0)
917 d0 = 1 / d0; /* Divide intentionally by zero. */
919 count_leading_zeros (bm, d0);
921 if (bm == 0)
923 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
924 conclude (the most significant bit of n1 is set) /\ (the
925 leading quotient digit q1 = 1).
927 This special case is necessary, not an optimization.
928 (Shifts counts of W_TYPE_SIZE are undefined.) */
930 n1 -= d0;
931 q1 = 1;
933 else
935 /* Normalize. */
937 b = W_TYPE_SIZE - bm;
939 d0 = d0 << bm;
940 n2 = n1 >> b;
941 n1 = (n1 << bm) | (n0 >> b);
942 n0 = n0 << bm;
944 udiv_qrnnd (q1, n1, n2, n1, d0);
947 /* n1 != d0... */
949 udiv_qrnnd (q0, n0, n1, n0, d0);
951 /* Remainder in n0 >> bm. */
954 if (rp != 0)
956 rr.s.low = n0 >> bm;
957 rr.s.high = 0;
958 *rp = rr.ll;
961 #endif /* UDIV_NEEDS_NORMALIZATION */
963 else
965 if (d1 > n1)
967 /* 00 = nn / DD */
969 q0 = 0;
970 q1 = 0;
972 /* Remainder in n1n0. */
973 if (rp != 0)
975 rr.s.low = n0;
976 rr.s.high = n1;
977 *rp = rr.ll;
980 else
982 /* 0q = NN / dd */
984 count_leading_zeros (bm, d1);
985 if (bm == 0)
987 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
988 conclude (the most significant bit of n1 is set) /\ (the
989 quotient digit q0 = 0 or 1).
991 This special case is necessary, not an optimization. */
993 /* The condition on the next line takes advantage of that
994 n1 >= d1 (true due to program flow). */
995 if (n1 > d1 || n0 >= d0)
997 q0 = 1;
998 sub_ddmmss (n1, n0, n1, n0, d1, d0);
1000 else
1001 q0 = 0;
1003 q1 = 0;
1005 if (rp != 0)
1007 rr.s.low = n0;
1008 rr.s.high = n1;
1009 *rp = rr.ll;
1012 else
1014 UWtype m1, m0;
1015 /* Normalize. */
1017 b = W_TYPE_SIZE - bm;
1019 d1 = (d1 << bm) | (d0 >> b);
1020 d0 = d0 << bm;
1021 n2 = n1 >> b;
1022 n1 = (n1 << bm) | (n0 >> b);
1023 n0 = n0 << bm;
1025 udiv_qrnnd (q0, n1, n2, n1, d1);
1026 umul_ppmm (m1, m0, q0, d0);
1028 if (m1 > n1 || (m1 == n1 && m0 > n0))
1030 q0--;
1031 sub_ddmmss (m1, m0, m1, m0, d1, d0);
1034 q1 = 0;
1036 /* Remainder in (n1n0 - m1m0) >> bm. */
1037 if (rp != 0)
1039 sub_ddmmss (n1, n0, n1, n0, m1, m0);
1040 rr.s.low = (n1 << b) | (n0 >> bm);
1041 rr.s.high = n1 >> bm;
1042 *rp = rr.ll;
1048 const DWunion ww = {{.low = q0, .high = q1}};
1049 return ww.ll;
1051 #endif
1053 #ifdef L_divdi3
1054 DWtype
1055 __divdi3 (DWtype u, DWtype v)
1057 word_type c = 0;
1058 DWunion uu = {.ll = u};
1059 DWunion vv = {.ll = v};
1060 DWtype w;
1062 if (uu.s.high < 0)
1063 c = ~c,
1064 uu.ll = -uu.ll;
1065 if (vv.s.high < 0)
1066 c = ~c,
1067 vv.ll = -vv.ll;
1069 w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0);
1070 if (c)
1071 w = -w;
1073 return w;
1075 #endif
1077 #ifdef L_moddi3
1078 DWtype
1079 __moddi3 (DWtype u, DWtype v)
1081 word_type c = 0;
1082 DWunion uu = {.ll = u};
1083 DWunion vv = {.ll = v};
1084 DWtype w;
1086 if (uu.s.high < 0)
1087 c = ~c,
1088 uu.ll = -uu.ll;
1089 if (vv.s.high < 0)
1090 vv.ll = -vv.ll;
1092 (void) __udivmoddi4 (uu.ll, vv.ll, (UDWtype*)&w);
1093 if (c)
1094 w = -w;
1096 return w;
1098 #endif
1100 #ifdef L_umoddi3
1101 UDWtype
1102 __umoddi3 (UDWtype u, UDWtype v)
1104 UDWtype w;
1106 (void) __udivmoddi4 (u, v, &w);
1108 return w;
1110 #endif
1112 #ifdef L_udivdi3
1113 UDWtype
1114 __udivdi3 (UDWtype n, UDWtype d)
1116 return __udivmoddi4 (n, d, (UDWtype *) 0);
1118 #endif
1120 #ifdef L_cmpdi2
1121 word_type
1122 __cmpdi2 (DWtype a, DWtype b)
1124 const DWunion au = {.ll = a};
1125 const DWunion bu = {.ll = b};
1127 if (au.s.high < bu.s.high)
1128 return 0;
1129 else if (au.s.high > bu.s.high)
1130 return 2;
1131 if ((UWtype) au.s.low < (UWtype) bu.s.low)
1132 return 0;
1133 else if ((UWtype) au.s.low > (UWtype) bu.s.low)
1134 return 2;
1135 return 1;
1137 #endif
1139 #ifdef L_ucmpdi2
1140 word_type
1141 __ucmpdi2 (DWtype a, DWtype b)
1143 const DWunion au = {.ll = a};
1144 const DWunion bu = {.ll = b};
1146 if ((UWtype) au.s.high < (UWtype) bu.s.high)
1147 return 0;
1148 else if ((UWtype) au.s.high > (UWtype) bu.s.high)
1149 return 2;
1150 if ((UWtype) au.s.low < (UWtype) bu.s.low)
1151 return 0;
1152 else if ((UWtype) au.s.low > (UWtype) bu.s.low)
1153 return 2;
1154 return 1;
1156 #endif
1158 #if defined(L_fixunstfdi) && LIBGCC2_HAS_TF_MODE
1159 DWtype
1160 __fixunstfDI (TFtype a)
1162 if (a < 0)
1163 return 0;
1165 /* Compute high word of result, as a flonum. */
1166 const TFtype b = (a / Wtype_MAXp1_F);
1167 /* Convert that to fixed (but not to DWtype!),
1168 and shift it into the high word. */
1169 UDWtype v = (UWtype) b;
1170 v <<= W_TYPE_SIZE;
1171 /* Remove high part from the TFtype, leaving the low part as flonum. */
1172 a -= (TFtype)v;
1173 /* Convert that to fixed (but not to DWtype!) and add it in.
1174 Sometimes A comes out negative. This is significant, since
1175 A has more bits than a long int does. */
1176 if (a < 0)
1177 v -= (UWtype) (- a);
1178 else
1179 v += (UWtype) a;
1180 return v;
1182 #endif
1184 #if defined(L_fixtfdi) && LIBGCC2_HAS_TF_MODE
1185 DWtype
1186 __fixtfdi (TFtype a)
1188 if (a < 0)
1189 return - __fixunstfDI (-a);
1190 return __fixunstfDI (a);
1192 #endif
1194 #if defined(L_fixunsxfdi) && LIBGCC2_HAS_XF_MODE
1195 DWtype
1196 __fixunsxfDI (XFtype a)
1198 if (a < 0)
1199 return 0;
1201 /* Compute high word of result, as a flonum. */
1202 const XFtype b = (a / Wtype_MAXp1_F);
1203 /* Convert that to fixed (but not to DWtype!),
1204 and shift it into the high word. */
1205 UDWtype v = (UWtype) b;
1206 v <<= W_TYPE_SIZE;
1207 /* Remove high part from the XFtype, leaving the low part as flonum. */
1208 a -= (XFtype)v;
1209 /* Convert that to fixed (but not to DWtype!) and add it in.
1210 Sometimes A comes out negative. This is significant, since
1211 A has more bits than a long int does. */
1212 if (a < 0)
1213 v -= (UWtype) (- a);
1214 else
1215 v += (UWtype) a;
1216 return v;
1218 #endif
1220 #if defined(L_fixxfdi) && LIBGCC2_HAS_XF_MODE
1221 DWtype
1222 __fixxfdi (XFtype a)
1224 if (a < 0)
1225 return - __fixunsxfDI (-a);
1226 return __fixunsxfDI (a);
1228 #endif
1230 #if defined(L_fixunsdfdi) && LIBGCC2_HAS_DF_MODE
1231 DWtype
1232 __fixunsdfDI (DFtype a)
1234 /* Get high part of result. The division here will just moves the radix
1235 point and will not cause any rounding. Then the conversion to integral
1236 type chops result as desired. */
1237 const UWtype hi = a / Wtype_MAXp1_F;
1239 /* Get low part of result. Convert `hi' to floating type and scale it back,
1240 then subtract this from the number being converted. This leaves the low
1241 part. Convert that to integral type. */
1242 const UWtype lo = a - (DFtype) hi * Wtype_MAXp1_F;
1244 /* Assemble result from the two parts. */
1245 return ((UDWtype) hi << W_TYPE_SIZE) | lo;
1247 #endif
1249 #if defined(L_fixdfdi) && LIBGCC2_HAS_DF_MODE
1250 DWtype
1251 __fixdfdi (DFtype a)
1253 if (a < 0)
1254 return - __fixunsdfDI (-a);
1255 return __fixunsdfDI (a);
1257 #endif
1259 #ifdef L_fixunssfdi
1260 DWtype
1261 __fixunssfDI (SFtype a)
1263 #if LIBGCC2_HAS_DF_MODE
1264 /* Convert the SFtype to a DFtype, because that is surely not going
1265 to lose any bits. Some day someone else can write a faster version
1266 that avoids converting to DFtype, and verify it really works right. */
1267 const DFtype dfa = a;
1269 /* Get high part of result. The division here will just moves the radix
1270 point and will not cause any rounding. Then the conversion to integral
1271 type chops result as desired. */
1272 const UWtype hi = dfa / Wtype_MAXp1_F;
1274 /* Get low part of result. Convert `hi' to floating type and scale it back,
1275 then subtract this from the number being converted. This leaves the low
1276 part. Convert that to integral type. */
1277 const UWtype lo = dfa - (DFtype) hi * Wtype_MAXp1_F;
1279 /* Assemble result from the two parts. */
1280 return ((UDWtype) hi << W_TYPE_SIZE) | lo;
1281 #elif FLT_MANT_DIG < W_TYPE_SIZE
1282 if (a < 1)
1283 return 0;
1284 if (a < Wtype_MAXp1_F)
1285 return (UWtype)a;
1286 if (a < Wtype_MAXp1_F * Wtype_MAXp1_F)
1288 /* Since we know that there are fewer significant bits in the SFmode
1289 quantity than in a word, we know that we can convert out all the
1290 significant bits in one step, and thus avoid losing bits. */
1292 /* ??? This following loop essentially performs frexpf. If we could
1293 use the real libm function, or poke at the actual bits of the fp
1294 format, it would be significantly faster. */
1296 UWtype shift = 0, counter;
1297 SFtype msb;
1299 a /= Wtype_MAXp1_F;
1300 for (counter = W_TYPE_SIZE / 2; counter != 0; counter >>= 1)
1302 SFtype counterf = (UWtype)1 << counter;
1303 if (a >= counterf)
1305 shift |= counter;
1306 a /= counterf;
1310 /* Rescale into the range of one word, extract the bits of that
1311 one word, and shift the result into position. */
1312 a *= Wtype_MAXp1_F;
1313 counter = a;
1314 return (DWtype)counter << shift;
1316 return -1;
1317 #else
1318 # error
1319 #endif
1321 #endif
1323 #ifdef L_fixsfdi
1324 DWtype
1325 __fixsfdi (SFtype a)
1327 if (a < 0)
1328 return - __fixunssfDI (-a);
1329 return __fixunssfDI (a);
1331 #endif
1333 #if defined(L_floatdixf) && LIBGCC2_HAS_XF_MODE
1334 XFtype
1335 __floatdixf (DWtype u)
1337 XFtype d = (Wtype) (u >> W_TYPE_SIZE);
1338 d *= Wtype_MAXp1_F;
1339 d += (UWtype)u;
1340 return d;
1342 #endif
1344 #if defined(L_floatditf) && LIBGCC2_HAS_TF_MODE
1345 TFtype
1346 __floatditf (DWtype u)
1348 TFtype d = (Wtype) (u >> W_TYPE_SIZE);
1349 d *= Wtype_MAXp1_F;
1350 d += (UWtype)u;
1351 return d;
1353 #endif
1355 #if defined(L_floatdidf) && LIBGCC2_HAS_DF_MODE
1356 DFtype
1357 __floatdidf (DWtype u)
1359 DFtype d = (Wtype) (u >> W_TYPE_SIZE);
1360 d *= Wtype_MAXp1_F;
1361 d += (UWtype)u;
1362 return d;
1364 #endif
1366 #ifdef L_floatdisf
1367 #define DI_SIZE (W_TYPE_SIZE * 2)
1368 #define SF_SIZE FLT_MANT_DIG
1370 SFtype
1371 __floatdisf (DWtype u)
1373 #if SF_SIZE >= W_TYPE_SIZE
1374 /* When the word size is small, we never get any rounding error. */
1375 SFtype f = (Wtype) (u >> W_TYPE_SIZE);
1376 f *= Wtype_MAXp1_F;
1377 f += (UWtype)u;
1378 return f;
1379 #elif LIBGCC2_HAS_DF_MODE
1381 #if LIBGCC2_DOUBLE_TYPE_SIZE == 64
1382 #define DF_SIZE DBL_MANT_DIG
1383 #elif LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 64
1384 #define DF_SIZE LDBL_MANT_DIG
1385 #else
1386 # error
1387 #endif
1389 #define REP_BIT ((UDWtype) 1 << (DI_SIZE - DF_SIZE))
1391 /* Protect against double-rounding error.
1392 Represent any low-order bits, that might be truncated by a bit that
1393 won't be lost. The bit can go in anywhere below the rounding position
1394 of the SFmode. A fixed mask and bit position handles all usual
1395 configurations. It doesn't handle the case of 128-bit DImode, however. */
1396 if (DF_SIZE < DI_SIZE
1397 && DF_SIZE > (DI_SIZE - DF_SIZE + SF_SIZE))
1399 if (! (- ((DWtype) 1 << DF_SIZE) < u
1400 && u < ((DWtype) 1 << DF_SIZE)))
1402 if ((UDWtype) u & (REP_BIT - 1))
1404 u &= ~ (REP_BIT - 1);
1405 u |= REP_BIT;
1410 /* Do the calculation in DFmode so that we don't lose any of the
1411 precision of the high word while multiplying it. */
1412 DFtype f = (Wtype) (u >> W_TYPE_SIZE);
1413 f *= Wtype_MAXp1_F;
1414 f += (UWtype)u;
1415 return (SFtype) f;
1416 #else
1417 /* Finally, the word size is larger than the number of bits in SFmode,
1418 and we've got no DFmode. The only way to avoid double rounding is
1419 to special case the extraction. */
1421 /* If there are no high bits set, fall back to one conversion. */
1422 if ((Wtype)u == u)
1423 return (SFtype)(Wtype)u;
1425 /* Otherwise, find the power of two. */
1426 Wtype hi = u >> W_TYPE_SIZE;
1427 if (hi < 0)
1428 hi = -hi;
1430 UWtype count, shift;
1431 count_leading_zeros (count, hi);
1433 /* No leading bits means u == minimum. */
1434 if (count == 0)
1435 return -(Wtype_MAXp1_F * Wtype_MAXp1_F / 2);
1437 shift = W_TYPE_SIZE - count;
1439 /* Shift down the most significant bits. */
1440 hi = u >> shift;
1442 /* If we lost any nonzero bits, set the lsb to ensure correct rounding. */
1443 if (u & ((1 << shift) - 1))
1444 hi |= 1;
1446 /* Convert the one word of data, and rescale. */
1447 SFtype f = hi;
1448 f *= (UWtype)1 << shift;
1449 return f;
1450 #endif
1452 #endif
1454 #if defined(L_fixunsxfsi) && LIBGCC2_HAS_XF_MODE
1455 /* Reenable the normal types, in case limits.h needs them. */
1456 #undef char
1457 #undef short
1458 #undef int
1459 #undef long
1460 #undef unsigned
1461 #undef float
1462 #undef double
1463 #undef MIN
1464 #undef MAX
1465 #include <limits.h>
1467 UWtype
1468 __fixunsxfSI (XFtype a)
1470 if (a >= - (DFtype) Wtype_MIN)
1471 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1472 return (Wtype) a;
1474 #endif
1476 #if defined(L_fixunsdfsi) && LIBGCC2_HAS_DF_MODE
1477 /* Reenable the normal types, in case limits.h needs them. */
1478 #undef char
1479 #undef short
1480 #undef int
1481 #undef long
1482 #undef unsigned
1483 #undef float
1484 #undef double
1485 #undef MIN
1486 #undef MAX
1487 #include <limits.h>
1489 UWtype
1490 __fixunsdfSI (DFtype a)
1492 if (a >= - (DFtype) Wtype_MIN)
1493 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1494 return (Wtype) a;
1496 #endif
1498 #ifdef L_fixunssfsi
1499 /* Reenable the normal types, in case limits.h needs them. */
1500 #undef char
1501 #undef short
1502 #undef int
1503 #undef long
1504 #undef unsigned
1505 #undef float
1506 #undef double
1507 #undef MIN
1508 #undef MAX
1509 #include <limits.h>
1511 UWtype
1512 __fixunssfSI (SFtype a)
1514 if (a >= - (SFtype) Wtype_MIN)
1515 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1516 return (Wtype) a;
1518 #endif
1520 /* Integer power helper used from __builtin_powi for non-constant
1521 exponents. */
1523 #if defined(L_powisf2) \
1524 || (defined(L_powidf2) && LIBGCC2_HAS_DF_MODE) \
1525 || (defined(L_powixf2) && LIBGCC2_HAS_XF_MODE) \
1526 || (defined(L_powitf2) && LIBGCC2_HAS_TF_MODE)
1527 # if defined(L_powisf2)
1528 # define TYPE SFtype
1529 # define NAME __powisf2
1530 # elif defined(L_powidf2)
1531 # define TYPE DFtype
1532 # define NAME __powidf2
1533 # elif defined(L_powixf2)
1534 # define TYPE XFtype
1535 # define NAME __powixf2
1536 # elif defined(L_powitf2)
1537 # define TYPE TFtype
1538 # define NAME __powitf2
1539 # endif
1541 TYPE
1542 NAME (TYPE x, Wtype m)
1544 UWtype n = m < 0 ? -m : m;
1545 TYPE y = n % 2 ? x : 1;
1546 while (n >>= 1)
1548 x = x * x;
1549 if (n % 2)
1550 y = y * x;
1552 return m < 0 ? 1/y : y;
1555 #endif
1557 #if defined(L_mulsc3) || defined(L_divsc3) \
1558 || ((defined(L_muldc3) || defined(L_divdc3)) && LIBGCC2_HAS_DF_MODE) \
1559 || ((defined(L_mulxc3) || defined(L_divxc3)) && LIBGCC2_HAS_XF_MODE) \
1560 || ((defined(L_multc3) || defined(L_divtc3)) && LIBGCC2_HAS_TF_MODE)
1562 #undef float
1563 #undef double
1564 #undef long
1566 #if defined(L_mulsc3) || defined(L_divsc3)
1567 # define MTYPE SFtype
1568 # define CTYPE SCtype
1569 # define MODE sc
1570 # define CEXT f
1571 # define NOTRUNC __FLT_EVAL_METHOD__ == 0
1572 #elif defined(L_muldc3) || defined(L_divdc3)
1573 # define MTYPE DFtype
1574 # define CTYPE DCtype
1575 # define MODE dc
1576 # if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 64
1577 # define CEXT l
1578 # define NOTRUNC 1
1579 # else
1580 # define CEXT
1581 # define NOTRUNC __FLT_EVAL_METHOD__ == 0 || __FLT_EVAL_METHOD__ == 1
1582 # endif
1583 #elif defined(L_mulxc3) || defined(L_divxc3)
1584 # define MTYPE XFtype
1585 # define CTYPE XCtype
1586 # define MODE xc
1587 # define CEXT l
1588 # define NOTRUNC 1
1589 #elif defined(L_multc3) || defined(L_divtc3)
1590 # define MTYPE TFtype
1591 # define CTYPE TCtype
1592 # define MODE tc
1593 # define CEXT l
1594 # define NOTRUNC 1
1595 #else
1596 # error
1597 #endif
1599 #define CONCAT3(A,B,C) _CONCAT3(A,B,C)
1600 #define _CONCAT3(A,B,C) A##B##C
1602 #define CONCAT2(A,B) _CONCAT2(A,B)
1603 #define _CONCAT2(A,B) A##B
1605 /* All of these would be present in a full C99 implementation of <math.h>
1606 and <complex.h>. Our problem is that only a few systems have such full
1607 implementations. Further, libgcc_s.so isn't currently linked against
1608 libm.so, and even for systems that do provide full C99, the extra overhead
1609 of all programs using libgcc having to link against libm. So avoid it. */
1611 #define isnan(x) __builtin_expect ((x) != (x), 0)
1612 #define isfinite(x) __builtin_expect (!isnan((x) - (x)), 1)
1613 #define isinf(x) __builtin_expect (!isnan(x) & !isfinite(x), 0)
1615 #define INFINITY CONCAT2(__builtin_inf, CEXT) ()
1616 #define I 1i
1618 /* Helpers to make the following code slightly less gross. */
1619 #define COPYSIGN CONCAT2(__builtin_copysign, CEXT)
1620 #define FABS CONCAT2(__builtin_fabs, CEXT)
1622 /* Verify that MTYPE matches up with CEXT. */
1623 extern void *compile_type_assert[sizeof(INFINITY) == sizeof(MTYPE) ? 1 : -1];
1625 /* Ensure that we've lost any extra precision. */
1626 #if NOTRUNC
1627 # define TRUNC(x)
1628 #else
1629 # define TRUNC(x) __asm__ ("" : "=m"(x) : "m"(x))
1630 #endif
1632 #if defined(L_mulsc3) || defined(L_muldc3) \
1633 || defined(L_mulxc3) || defined(L_multc3)
1635 CTYPE
1636 CONCAT3(__mul,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
1638 MTYPE ac, bd, ad, bc, x, y;
1640 ac = a * c;
1641 bd = b * d;
1642 ad = a * d;
1643 bc = b * c;
1645 TRUNC (ac);
1646 TRUNC (bd);
1647 TRUNC (ad);
1648 TRUNC (bc);
1650 x = ac - bd;
1651 y = ad + bc;
1653 if (isnan (x) && isnan (y))
1655 /* Recover infinities that computed as NaN + iNaN. */
1656 _Bool recalc = 0;
1657 if (isinf (a) || isinf (b))
1659 /* z is infinite. "Box" the infinity and change NaNs in
1660 the other factor to 0. */
1661 a = COPYSIGN (isinf (a) ? 1 : 0, a);
1662 b = COPYSIGN (isinf (b) ? 1 : 0, b);
1663 if (isnan (c)) c = COPYSIGN (0, c);
1664 if (isnan (d)) d = COPYSIGN (0, d);
1665 recalc = 1;
1667 if (isinf (c) || isinf (d))
1669 /* w is infinite. "Box" the infinity and change NaNs in
1670 the other factor to 0. */
1671 c = COPYSIGN (isinf (c) ? 1 : 0, c);
1672 d = COPYSIGN (isinf (d) ? 1 : 0, d);
1673 if (isnan (a)) a = COPYSIGN (0, a);
1674 if (isnan (b)) b = COPYSIGN (0, b);
1675 recalc = 1;
1677 if (!recalc
1678 && (isinf (ac) || isinf (bd)
1679 || isinf (ad) || isinf (bc)))
1681 /* Recover infinities from overflow by changing NaNs to 0. */
1682 if (isnan (a)) a = COPYSIGN (0, a);
1683 if (isnan (b)) b = COPYSIGN (0, b);
1684 if (isnan (c)) c = COPYSIGN (0, c);
1685 if (isnan (d)) d = COPYSIGN (0, d);
1686 recalc = 1;
1688 if (recalc)
1690 x = INFINITY * (a * c - b * d);
1691 y = INFINITY * (a * d + b * c);
1695 return x + I * y;
1697 #endif /* complex multiply */
1699 #if defined(L_divsc3) || defined(L_divdc3) \
1700 || defined(L_divxc3) || defined(L_divtc3)
1702 CTYPE
1703 CONCAT3(__div,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
1705 MTYPE denom, ratio, x, y;
1707 /* ??? We can get better behavior from logarithmic scaling instead of
1708 the division. But that would mean starting to link libgcc against
1709 libm. We could implement something akin to ldexp/frexp as gcc builtins
1710 fairly easily... */
1711 if (FABS (c) < FABS (d))
1713 ratio = c / d;
1714 denom = (c * ratio) + d;
1715 x = ((a * ratio) + b) / denom;
1716 y = ((b * ratio) - a) / denom;
1718 else
1720 ratio = d / c;
1721 denom = (d * ratio) + c;
1722 x = ((b * ratio) + a) / denom;
1723 y = (b - (a * ratio)) / denom;
1726 /* Recover infinities and zeros that computed as NaN+iNaN; the only cases
1727 are nonzero/zero, infinite/finite, and finite/infinite. */
1728 if (isnan (x) && isnan (y))
1730 if (denom == 0.0 && (!isnan (a) || !isnan (b)))
1732 x = COPYSIGN (INFINITY, c) * a;
1733 y = COPYSIGN (INFINITY, c) * b;
1735 else if ((isinf (a) || isinf (b)) && isfinite (c) && isfinite (d))
1737 a = COPYSIGN (isinf (a) ? 1 : 0, a);
1738 b = COPYSIGN (isinf (b) ? 1 : 0, b);
1739 x = INFINITY * (a * c + b * d);
1740 y = INFINITY * (b * c - a * d);
1742 else if ((isinf (c) || isinf (d)) && isfinite (a) && isfinite (b))
1744 c = COPYSIGN (isinf (c) ? 1 : 0, c);
1745 d = COPYSIGN (isinf (d) ? 1 : 0, d);
1746 x = 0.0 * (a * c + b * d);
1747 y = 0.0 * (b * c - a * d);
1751 return x + I * y;
1753 #endif /* complex divide */
1755 #endif /* all complex float routines */
1757 /* From here on down, the routines use normal data types. */
1759 #define SItype bogus_type
1760 #define USItype bogus_type
1761 #define DItype bogus_type
1762 #define UDItype bogus_type
1763 #define SFtype bogus_type
1764 #define DFtype bogus_type
1765 #undef Wtype
1766 #undef UWtype
1767 #undef HWtype
1768 #undef UHWtype
1769 #undef DWtype
1770 #undef UDWtype
1772 #undef char
1773 #undef short
1774 #undef int
1775 #undef long
1776 #undef unsigned
1777 #undef float
1778 #undef double
1780 #ifdef L__gcc_bcmp
1782 /* Like bcmp except the sign is meaningful.
1783 Result is negative if S1 is less than S2,
1784 positive if S1 is greater, 0 if S1 and S2 are equal. */
1787 __gcc_bcmp (const unsigned char *s1, const unsigned char *s2, size_t size)
1789 while (size > 0)
1791 const unsigned char c1 = *s1++, c2 = *s2++;
1792 if (c1 != c2)
1793 return c1 - c2;
1794 size--;
1796 return 0;
1799 #endif
1801 /* __eprintf used to be used by GCC's private version of <assert.h>.
1802 We no longer provide that header, but this routine remains in libgcc.a
1803 for binary backward compatibility. Note that it is not included in
1804 the shared version of libgcc. */
1805 #ifdef L_eprintf
1806 #ifndef inhibit_libc
1808 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1809 #include <stdio.h>
1811 void
1812 __eprintf (const char *string, const char *expression,
1813 unsigned int line, const char *filename)
1815 fprintf (stderr, string, expression, line, filename);
1816 fflush (stderr);
1817 abort ();
1820 #endif
1821 #endif
1824 #ifdef L_clear_cache
1825 /* Clear part of an instruction cache. */
1827 void
1828 __clear_cache (char *beg __attribute__((__unused__)),
1829 char *end __attribute__((__unused__)))
1831 #ifdef CLEAR_INSN_CACHE
1832 CLEAR_INSN_CACHE (beg, end);
1833 #endif /* CLEAR_INSN_CACHE */
1836 #endif /* L_clear_cache */
1838 #ifdef L_enable_execute_stack
1839 /* Attempt to turn on execute permission for the stack. */
1841 #ifdef ENABLE_EXECUTE_STACK
1842 ENABLE_EXECUTE_STACK
1843 #else
1844 void
1845 __enable_execute_stack (void *addr __attribute__((__unused__)))
1847 #endif /* ENABLE_EXECUTE_STACK */
1849 #endif /* L_enable_execute_stack */
1851 #ifdef L_trampoline
1853 /* Jump to a trampoline, loading the static chain address. */
1855 #if defined(WINNT) && ! defined(__CYGWIN__) && ! defined (_UWIN)
1858 getpagesize (void)
1860 #ifdef _ALPHA_
1861 return 8192;
1862 #else
1863 return 4096;
1864 #endif
1867 #ifdef __i386__
1868 extern int VirtualProtect (char *, int, int, int *) __attribute__((stdcall));
1869 #endif
1872 mprotect (char *addr, int len, int prot)
1874 int np, op;
1876 if (prot == 7)
1877 np = 0x40;
1878 else if (prot == 5)
1879 np = 0x20;
1880 else if (prot == 4)
1881 np = 0x10;
1882 else if (prot == 3)
1883 np = 0x04;
1884 else if (prot == 1)
1885 np = 0x02;
1886 else if (prot == 0)
1887 np = 0x01;
1889 if (VirtualProtect (addr, len, np, &op))
1890 return 0;
1891 else
1892 return -1;
1895 #endif /* WINNT && ! __CYGWIN__ && ! _UWIN */
1897 #ifdef TRANSFER_FROM_TRAMPOLINE
1898 TRANSFER_FROM_TRAMPOLINE
1899 #endif
1900 #endif /* L_trampoline */
1902 #ifndef __CYGWIN__
1903 #ifdef L__main
1905 #include "gbl-ctors.h"
1906 /* Some systems use __main in a way incompatible with its use in gcc, in these
1907 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
1908 give the same symbol without quotes for an alternative entry point. You
1909 must define both, or neither. */
1910 #ifndef NAME__MAIN
1911 #define NAME__MAIN "__main"
1912 #define SYMBOL__MAIN __main
1913 #endif
1915 #ifdef INIT_SECTION_ASM_OP
1916 #undef HAS_INIT_SECTION
1917 #define HAS_INIT_SECTION
1918 #endif
1920 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
1922 /* Some ELF crosses use crtstuff.c to provide __CTOR_LIST__, but use this
1923 code to run constructors. In that case, we need to handle EH here, too. */
1925 #ifdef EH_FRAME_SECTION_NAME
1926 #include "unwind-dw2-fde.h"
1927 extern unsigned char __EH_FRAME_BEGIN__[];
1928 #endif
1930 /* Run all the global destructors on exit from the program. */
1932 void
1933 __do_global_dtors (void)
1935 #ifdef DO_GLOBAL_DTORS_BODY
1936 DO_GLOBAL_DTORS_BODY;
1937 #else
1938 static func_ptr *p = __DTOR_LIST__ + 1;
1939 while (*p)
1941 p++;
1942 (*(p-1)) ();
1944 #endif
1945 #if defined (EH_FRAME_SECTION_NAME) && !defined (HAS_INIT_SECTION)
1947 static int completed = 0;
1948 if (! completed)
1950 completed = 1;
1951 __deregister_frame_info (__EH_FRAME_BEGIN__);
1954 #endif
1956 #endif
1958 #ifndef HAS_INIT_SECTION
1959 /* Run all the global constructors on entry to the program. */
1961 void
1962 __do_global_ctors (void)
1964 #ifdef EH_FRAME_SECTION_NAME
1966 static struct object object;
1967 __register_frame_info (__EH_FRAME_BEGIN__, &object);
1969 #endif
1970 DO_GLOBAL_CTORS_BODY;
1971 atexit (__do_global_dtors);
1973 #endif /* no HAS_INIT_SECTION */
1975 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
1976 /* Subroutine called automatically by `main'.
1977 Compiling a global function named `main'
1978 produces an automatic call to this function at the beginning.
1980 For many systems, this routine calls __do_global_ctors.
1981 For systems which support a .init section we use the .init section
1982 to run __do_global_ctors, so we need not do anything here. */
1984 extern void SYMBOL__MAIN (void);
1985 void
1986 SYMBOL__MAIN (void)
1988 /* Support recursive calls to `main': run initializers just once. */
1989 static int initialized;
1990 if (! initialized)
1992 initialized = 1;
1993 __do_global_ctors ();
1996 #endif /* no HAS_INIT_SECTION or INVOKE__main */
1998 #endif /* L__main */
1999 #endif /* __CYGWIN__ */
2001 #ifdef L_ctors
2003 #include "gbl-ctors.h"
2005 /* Provide default definitions for the lists of constructors and
2006 destructors, so that we don't get linker errors. These symbols are
2007 intentionally bss symbols, so that gld and/or collect will provide
2008 the right values. */
2010 /* We declare the lists here with two elements each,
2011 so that they are valid empty lists if no other definition is loaded.
2013 If we are using the old "set" extensions to have the gnu linker
2014 collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
2015 must be in the bss/common section.
2017 Long term no port should use those extensions. But many still do. */
2018 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
2019 #if defined (TARGET_ASM_CONSTRUCTOR) || defined (USE_COLLECT2)
2020 func_ptr __CTOR_LIST__[2] = {0, 0};
2021 func_ptr __DTOR_LIST__[2] = {0, 0};
2022 #else
2023 func_ptr __CTOR_LIST__[2];
2024 func_ptr __DTOR_LIST__[2];
2025 #endif
2026 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
2027 #endif /* L_ctors */