mdoc: Add NetBSD 6.0 (used in wbsio.4).
[dragonfly.git] / contrib / gcc-4.1 / gcc / libgcc2.c
blobf6c212b77be74f42bdbd7c6d2996fa5c93e98d72
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, 51 Franklin Street, Fifth Floor, Boston, MA
30 02110-1301, USA. */
32 #include "tconfig.h"
33 #include "tsystem.h"
34 #include "coretypes.h"
35 #include "tm.h"
37 #ifdef HAVE_GAS_HIDDEN
38 #define ATTRIBUTE_HIDDEN __attribute__ ((__visibility__ ("hidden")))
39 #else
40 #define ATTRIBUTE_HIDDEN
41 #endif
43 #ifndef MIN_UNITS_PER_WORD
44 #define MIN_UNITS_PER_WORD UNITS_PER_WORD
45 #endif
47 /* Work out the largest "word" size that we can deal with on this target. */
48 #if MIN_UNITS_PER_WORD > 4
49 # define LIBGCC2_MAX_UNITS_PER_WORD 8
50 #elif (MIN_UNITS_PER_WORD > 2 \
51 || (MIN_UNITS_PER_WORD > 1 && LONG_LONG_TYPE_SIZE > 32))
52 # define LIBGCC2_MAX_UNITS_PER_WORD 4
53 #else
54 # define LIBGCC2_MAX_UNITS_PER_WORD MIN_UNITS_PER_WORD
55 #endif
57 /* Work out what word size we are using for this compilation.
58 The value can be set on the command line. */
59 #ifndef LIBGCC2_UNITS_PER_WORD
60 #define LIBGCC2_UNITS_PER_WORD LIBGCC2_MAX_UNITS_PER_WORD
61 #endif
63 #if LIBGCC2_UNITS_PER_WORD <= LIBGCC2_MAX_UNITS_PER_WORD
65 #include "libgcc2.h"
67 #ifdef DECLARE_LIBRARY_RENAMES
68 DECLARE_LIBRARY_RENAMES
69 #endif
71 #if defined (L_negdi2)
72 DWtype
73 __negdi2 (DWtype u)
75 const DWunion uu = {.ll = u};
76 const DWunion w = { {.low = -uu.s.low,
77 .high = -uu.s.high - ((UWtype) -uu.s.low > 0) } };
79 return w.ll;
81 #endif
83 #ifdef L_addvsi3
84 Wtype
85 __addvSI3 (Wtype a, Wtype b)
87 const Wtype w = a + b;
89 if (b >= 0 ? w < a : w > a)
90 abort ();
92 return w;
94 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
95 SItype
96 __addvsi3 (SItype a, SItype b)
98 const SItype w = a + b;
100 if (b >= 0 ? w < a : w > a)
101 abort ();
103 return w;
105 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
106 #endif
108 #ifdef L_addvdi3
109 DWtype
110 __addvDI3 (DWtype a, DWtype b)
112 const DWtype w = a + b;
114 if (b >= 0 ? w < a : w > a)
115 abort ();
117 return w;
119 #endif
121 #ifdef L_subvsi3
122 Wtype
123 __subvSI3 (Wtype a, Wtype b)
125 const Wtype w = a - b;
127 if (b >= 0 ? w > a : w < a)
128 abort ();
130 return w;
132 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
133 SItype
134 __subvsi3 (SItype a, SItype b)
136 const SItype w = a - b;
138 if (b >= 0 ? w > a : w < a)
139 abort ();
141 return w;
143 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
144 #endif
146 #ifdef L_subvdi3
147 DWtype
148 __subvDI3 (DWtype a, DWtype b)
150 const DWtype w = a - b;
152 if (b >= 0 ? w > a : w < a)
153 abort ();
155 return w;
157 #endif
159 #ifdef L_mulvsi3
160 Wtype
161 __mulvSI3 (Wtype a, Wtype b)
163 const DWtype w = (DWtype) a * (DWtype) b;
165 if ((Wtype) (w >> W_TYPE_SIZE) != (Wtype) w >> (W_TYPE_SIZE - 1))
166 abort ();
168 return w;
170 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
171 #undef WORD_SIZE
172 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
173 SItype
174 __mulvsi3 (SItype a, SItype b)
176 const DItype w = (DItype) a * (DItype) b;
178 if ((SItype) (w >> WORD_SIZE) != (SItype) w >> (WORD_SIZE-1))
179 abort ();
181 return w;
183 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
184 #endif
186 #ifdef L_negvsi2
187 Wtype
188 __negvSI2 (Wtype a)
190 const Wtype w = -a;
192 if (a >= 0 ? w > 0 : w < 0)
193 abort ();
195 return w;
197 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
198 SItype
199 __negvsi2 (SItype a)
201 const SItype w = -a;
203 if (a >= 0 ? w > 0 : w < 0)
204 abort ();
206 return w;
208 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
209 #endif
211 #ifdef L_negvdi2
212 DWtype
213 __negvDI2 (DWtype a)
215 const DWtype w = -a;
217 if (a >= 0 ? w > 0 : w < 0)
218 abort ();
220 return w;
222 #endif
224 #ifdef L_absvsi2
225 Wtype
226 __absvSI2 (Wtype a)
228 Wtype w = a;
230 if (a < 0)
231 #ifdef L_negvsi2
232 w = __negvSI2 (a);
233 #else
234 w = -a;
236 if (w < 0)
237 abort ();
238 #endif
240 return w;
242 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
243 SItype
244 __absvsi2 (SItype a)
246 SItype w = a;
248 if (a < 0)
249 #ifdef L_negvsi2
250 w = __negvsi2 (a);
251 #else
252 w = -a;
254 if (w < 0)
255 abort ();
256 #endif
258 return w;
260 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
261 #endif
263 #ifdef L_absvdi2
264 DWtype
265 __absvDI2 (DWtype a)
267 DWtype w = a;
269 if (a < 0)
270 #ifdef L_negvdi2
271 w = __negvDI2 (a);
272 #else
273 w = -a;
275 if (w < 0)
276 abort ();
277 #endif
279 return w;
281 #endif
283 #ifdef L_mulvdi3
284 DWtype
285 __mulvDI3 (DWtype u, DWtype v)
287 /* The unchecked multiplication needs 3 Wtype x Wtype multiplications,
288 but the checked multiplication needs only two. */
289 const DWunion uu = {.ll = u};
290 const DWunion vv = {.ll = v};
292 if (__builtin_expect (uu.s.high == uu.s.low >> (W_TYPE_SIZE - 1), 1))
294 /* u fits in a single Wtype. */
295 if (__builtin_expect (vv.s.high == vv.s.low >> (W_TYPE_SIZE - 1), 1))
297 /* v fits in a single Wtype as well. */
298 /* A single multiplication. No overflow risk. */
299 return (DWtype) uu.s.low * (DWtype) vv.s.low;
301 else
303 /* Two multiplications. */
304 DWunion w0 = {.ll = (UDWtype) (UWtype) uu.s.low
305 * (UDWtype) (UWtype) vv.s.low};
306 DWunion w1 = {.ll = (UDWtype) (UWtype) uu.s.low
307 * (UDWtype) (UWtype) vv.s.high};
309 if (vv.s.high < 0)
310 w1.s.high -= uu.s.low;
311 if (uu.s.low < 0)
312 w1.ll -= vv.ll;
313 w1.ll += (UWtype) w0.s.high;
314 if (__builtin_expect (w1.s.high == w1.s.low >> (W_TYPE_SIZE - 1), 1))
316 w0.s.high = w1.s.low;
317 return w0.ll;
321 else
323 if (__builtin_expect (vv.s.high == vv.s.low >> (W_TYPE_SIZE - 1), 1))
325 /* v fits into a single Wtype. */
326 /* Two multiplications. */
327 DWunion w0 = {.ll = (UDWtype) (UWtype) uu.s.low
328 * (UDWtype) (UWtype) vv.s.low};
329 DWunion w1 = {.ll = (UDWtype) (UWtype) uu.s.high
330 * (UDWtype) (UWtype) vv.s.low};
332 if (uu.s.high < 0)
333 w1.s.high -= vv.s.low;
334 if (vv.s.low < 0)
335 w1.ll -= uu.ll;
336 w1.ll += (UWtype) w0.s.high;
337 if (__builtin_expect (w1.s.high == w1.s.low >> (W_TYPE_SIZE - 1), 1))
339 w0.s.high = w1.s.low;
340 return w0.ll;
343 else
345 /* A few sign checks and a single multiplication. */
346 if (uu.s.high >= 0)
348 if (vv.s.high >= 0)
350 if (uu.s.high == 0 && vv.s.high == 0)
352 const DWtype w = (UDWtype) (UWtype) uu.s.low
353 * (UDWtype) (UWtype) vv.s.low;
354 if (__builtin_expect (w >= 0, 1))
355 return w;
358 else
360 if (uu.s.high == 0 && vv.s.high == (Wtype) -1)
362 DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
363 * (UDWtype) (UWtype) vv.s.low};
365 ww.s.high -= uu.s.low;
366 if (__builtin_expect (ww.s.high < 0, 1))
367 return ww.ll;
371 else
373 if (vv.s.high >= 0)
375 if (uu.s.high == (Wtype) -1 && vv.s.high == 0)
377 DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
378 * (UDWtype) (UWtype) vv.s.low};
380 ww.s.high -= vv.s.low;
381 if (__builtin_expect (ww.s.high < 0, 1))
382 return ww.ll;
385 else
387 if (uu.s.high == (Wtype) -1 && vv.s.high == (Wtype) - 1)
389 DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
390 * (UDWtype) (UWtype) vv.s.low};
392 ww.s.high -= uu.s.low;
393 ww.s.high -= vv.s.low;
394 if (__builtin_expect (ww.s.high >= 0, 1))
395 return ww.ll;
402 /* Overflow. */
403 abort ();
405 #endif
408 /* Unless shift functions are defined with full ANSI prototypes,
409 parameter b will be promoted to int if word_type is smaller than an int. */
410 #ifdef L_lshrdi3
411 DWtype
412 __lshrdi3 (DWtype u, word_type b)
414 if (b == 0)
415 return u;
417 const DWunion uu = {.ll = u};
418 const word_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
419 DWunion w;
421 if (bm <= 0)
423 w.s.high = 0;
424 w.s.low = (UWtype) uu.s.high >> -bm;
426 else
428 const UWtype carries = (UWtype) uu.s.high << bm;
430 w.s.high = (UWtype) uu.s.high >> b;
431 w.s.low = ((UWtype) uu.s.low >> b) | carries;
434 return w.ll;
436 #endif
438 #ifdef L_ashldi3
439 DWtype
440 __ashldi3 (DWtype u, word_type b)
442 if (b == 0)
443 return u;
445 const DWunion uu = {.ll = u};
446 const word_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
447 DWunion w;
449 if (bm <= 0)
451 w.s.low = 0;
452 w.s.high = (UWtype) uu.s.low << -bm;
454 else
456 const UWtype carries = (UWtype) uu.s.low >> bm;
458 w.s.low = (UWtype) uu.s.low << b;
459 w.s.high = ((UWtype) uu.s.high << b) | carries;
462 return w.ll;
464 #endif
466 #ifdef L_ashrdi3
467 DWtype
468 __ashrdi3 (DWtype u, word_type b)
470 if (b == 0)
471 return u;
473 const DWunion uu = {.ll = u};
474 const word_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
475 DWunion w;
477 if (bm <= 0)
479 /* w.s.high = 1..1 or 0..0 */
480 w.s.high = uu.s.high >> (sizeof (Wtype) * BITS_PER_UNIT - 1);
481 w.s.low = uu.s.high >> -bm;
483 else
485 const UWtype carries = (UWtype) uu.s.high << bm;
487 w.s.high = uu.s.high >> b;
488 w.s.low = ((UWtype) uu.s.low >> b) | carries;
491 return w.ll;
493 #endif
495 #ifdef L_ffssi2
496 #undef int
498 __ffsSI2 (UWtype u)
500 UWtype count;
502 if (u == 0)
503 return 0;
505 count_trailing_zeros (count, u);
506 return count + 1;
508 #endif
510 #ifdef L_ffsdi2
511 #undef int
513 __ffsDI2 (DWtype u)
515 const DWunion uu = {.ll = u};
516 UWtype word, count, add;
518 if (uu.s.low != 0)
519 word = uu.s.low, add = 0;
520 else if (uu.s.high != 0)
521 word = uu.s.high, add = BITS_PER_UNIT * sizeof (Wtype);
522 else
523 return 0;
525 count_trailing_zeros (count, word);
526 return count + add + 1;
528 #endif
530 #ifdef L_muldi3
531 DWtype
532 __muldi3 (DWtype u, DWtype v)
534 const DWunion uu = {.ll = u};
535 const DWunion vv = {.ll = v};
536 DWunion w = {.ll = __umulsidi3 (uu.s.low, vv.s.low)};
538 w.s.high += ((UWtype) uu.s.low * (UWtype) vv.s.high
539 + (UWtype) uu.s.high * (UWtype) vv.s.low);
541 return w.ll;
543 #endif
545 #if (defined (L_udivdi3) || defined (L_divdi3) || \
546 defined (L_umoddi3) || defined (L_moddi3))
547 #if defined (sdiv_qrnnd)
548 #define L_udiv_w_sdiv
549 #endif
550 #endif
552 #ifdef L_udiv_w_sdiv
553 #if defined (sdiv_qrnnd)
554 #if (defined (L_udivdi3) || defined (L_divdi3) || \
555 defined (L_umoddi3) || defined (L_moddi3))
556 static inline __attribute__ ((__always_inline__))
557 #endif
558 UWtype
559 __udiv_w_sdiv (UWtype *rp, UWtype a1, UWtype a0, UWtype d)
561 UWtype q, r;
562 UWtype c0, c1, b1;
564 if ((Wtype) d >= 0)
566 if (a1 < d - a1 - (a0 >> (W_TYPE_SIZE - 1)))
568 /* Dividend, divisor, and quotient are nonnegative. */
569 sdiv_qrnnd (q, r, a1, a0, d);
571 else
573 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d. */
574 sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (W_TYPE_SIZE - 1));
575 /* Divide (c1*2^32 + c0) by d. */
576 sdiv_qrnnd (q, r, c1, c0, d);
577 /* Add 2^31 to quotient. */
578 q += (UWtype) 1 << (W_TYPE_SIZE - 1);
581 else
583 b1 = d >> 1; /* d/2, between 2^30 and 2^31 - 1 */
584 c1 = a1 >> 1; /* A/2 */
585 c0 = (a1 << (W_TYPE_SIZE - 1)) + (a0 >> 1);
587 if (a1 < b1) /* A < 2^32*b1, so A/2 < 2^31*b1 */
589 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
591 r = 2*r + (a0 & 1); /* Remainder from A/(2*b1) */
592 if ((d & 1) != 0)
594 if (r >= q)
595 r = r - q;
596 else if (q - r <= d)
598 r = r - q + d;
599 q--;
601 else
603 r = r - q + 2*d;
604 q -= 2;
608 else if (c1 < b1) /* So 2^31 <= (A/2)/b1 < 2^32 */
610 c1 = (b1 - 1) - c1;
611 c0 = ~c0; /* logical NOT */
613 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
615 q = ~q; /* (A/2)/b1 */
616 r = (b1 - 1) - r;
618 r = 2*r + (a0 & 1); /* A/(2*b1) */
620 if ((d & 1) != 0)
622 if (r >= q)
623 r = r - q;
624 else if (q - r <= d)
626 r = r - q + d;
627 q--;
629 else
631 r = r - q + 2*d;
632 q -= 2;
636 else /* Implies c1 = b1 */
637 { /* Hence a1 = d - 1 = 2*b1 - 1 */
638 if (a0 >= -d)
640 q = -1;
641 r = a0 + d;
643 else
645 q = -2;
646 r = a0 + 2*d;
651 *rp = r;
652 return q;
654 #else
655 /* If sdiv_qrnnd doesn't exist, define dummy __udiv_w_sdiv. */
656 UWtype
657 __udiv_w_sdiv (UWtype *rp __attribute__ ((__unused__)),
658 UWtype a1 __attribute__ ((__unused__)),
659 UWtype a0 __attribute__ ((__unused__)),
660 UWtype d __attribute__ ((__unused__)))
662 return 0;
664 #endif
665 #endif
667 #if (defined (L_udivdi3) || defined (L_divdi3) || \
668 defined (L_umoddi3) || defined (L_moddi3))
669 #define L_udivmoddi4
670 #endif
672 #ifdef L_clz
673 const UQItype __clz_tab[256] =
675 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,
676 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,
677 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,
678 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,
679 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,
680 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,
681 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,
682 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
684 #endif
686 #ifdef L_clzsi2
687 #undef int
689 __clzSI2 (UWtype x)
691 Wtype ret;
693 count_leading_zeros (ret, x);
695 return ret;
697 #endif
699 #ifdef L_clzdi2
700 #undef int
702 __clzDI2 (UDWtype x)
704 const DWunion uu = {.ll = x};
705 UWtype word;
706 Wtype ret, add;
708 if (uu.s.high)
709 word = uu.s.high, add = 0;
710 else
711 word = uu.s.low, add = W_TYPE_SIZE;
713 count_leading_zeros (ret, word);
714 return ret + add;
716 #endif
718 #ifdef L_ctzsi2
719 #undef int
721 __ctzSI2 (UWtype x)
723 Wtype ret;
725 count_trailing_zeros (ret, x);
727 return ret;
729 #endif
731 #ifdef L_ctzdi2
732 #undef int
734 __ctzDI2 (UDWtype x)
736 const DWunion uu = {.ll = x};
737 UWtype word;
738 Wtype ret, add;
740 if (uu.s.low)
741 word = uu.s.low, add = 0;
742 else
743 word = uu.s.high, add = W_TYPE_SIZE;
745 count_trailing_zeros (ret, word);
746 return ret + add;
748 #endif
750 #ifdef L_popcount_tab
751 const UQItype __popcount_tab[256] =
753 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,
754 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,
755 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,
756 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,
757 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,
758 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,
759 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,
760 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
762 #endif
764 #ifdef L_popcountsi2
765 #undef int
767 __popcountSI2 (UWtype x)
769 UWtype i, ret = 0;
771 for (i = 0; i < W_TYPE_SIZE; i += 8)
772 ret += __popcount_tab[(x >> i) & 0xff];
774 return ret;
776 #endif
778 #ifdef L_popcountdi2
779 #undef int
781 __popcountDI2 (UDWtype x)
783 UWtype i, ret = 0;
785 for (i = 0; i < 2*W_TYPE_SIZE; i += 8)
786 ret += __popcount_tab[(x >> i) & 0xff];
788 return ret;
790 #endif
792 #ifdef L_paritysi2
793 #undef int
795 __paritySI2 (UWtype x)
797 #if W_TYPE_SIZE > 64
798 # error "fill out the table"
799 #endif
800 #if W_TYPE_SIZE > 32
801 x ^= x >> 32;
802 #endif
803 #if W_TYPE_SIZE > 16
804 x ^= x >> 16;
805 #endif
806 x ^= x >> 8;
807 x ^= x >> 4;
808 x &= 0xf;
809 return (0x6996 >> x) & 1;
811 #endif
813 #ifdef L_paritydi2
814 #undef int
816 __parityDI2 (UDWtype x)
818 const DWunion uu = {.ll = x};
819 UWtype nx = uu.s.low ^ uu.s.high;
821 #if W_TYPE_SIZE > 64
822 # error "fill out the table"
823 #endif
824 #if W_TYPE_SIZE > 32
825 nx ^= nx >> 32;
826 #endif
827 #if W_TYPE_SIZE > 16
828 nx ^= nx >> 16;
829 #endif
830 nx ^= nx >> 8;
831 nx ^= nx >> 4;
832 nx &= 0xf;
833 return (0x6996 >> nx) & 1;
835 #endif
837 #ifdef L_udivmoddi4
839 #if (defined (L_udivdi3) || defined (L_divdi3) || \
840 defined (L_umoddi3) || defined (L_moddi3))
841 static inline __attribute__ ((__always_inline__))
842 #endif
843 UDWtype
844 __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
846 const DWunion nn = {.ll = n};
847 const DWunion dd = {.ll = d};
848 DWunion rr;
849 UWtype d0, d1, n0, n1, n2;
850 UWtype q0, q1;
851 UWtype b, bm;
853 d0 = dd.s.low;
854 d1 = dd.s.high;
855 n0 = nn.s.low;
856 n1 = nn.s.high;
858 #if !UDIV_NEEDS_NORMALIZATION
859 if (d1 == 0)
861 if (d0 > n1)
863 /* 0q = nn / 0D */
865 udiv_qrnnd (q0, n0, n1, n0, d0);
866 q1 = 0;
868 /* Remainder in n0. */
870 else
872 /* qq = NN / 0d */
874 if (d0 == 0)
875 d0 = 1 / d0; /* Divide intentionally by zero. */
877 udiv_qrnnd (q1, n1, 0, n1, d0);
878 udiv_qrnnd (q0, n0, n1, n0, d0);
880 /* Remainder in n0. */
883 if (rp != 0)
885 rr.s.low = n0;
886 rr.s.high = 0;
887 *rp = rr.ll;
891 #else /* UDIV_NEEDS_NORMALIZATION */
893 if (d1 == 0)
895 if (d0 > n1)
897 /* 0q = nn / 0D */
899 count_leading_zeros (bm, d0);
901 if (bm != 0)
903 /* Normalize, i.e. make the most significant bit of the
904 denominator set. */
906 d0 = d0 << bm;
907 n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm));
908 n0 = n0 << bm;
911 udiv_qrnnd (q0, n0, n1, n0, d0);
912 q1 = 0;
914 /* Remainder in n0 >> bm. */
916 else
918 /* qq = NN / 0d */
920 if (d0 == 0)
921 d0 = 1 / d0; /* Divide intentionally by zero. */
923 count_leading_zeros (bm, d0);
925 if (bm == 0)
927 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
928 conclude (the most significant bit of n1 is set) /\ (the
929 leading quotient digit q1 = 1).
931 This special case is necessary, not an optimization.
932 (Shifts counts of W_TYPE_SIZE are undefined.) */
934 n1 -= d0;
935 q1 = 1;
937 else
939 /* Normalize. */
941 b = W_TYPE_SIZE - bm;
943 d0 = d0 << bm;
944 n2 = n1 >> b;
945 n1 = (n1 << bm) | (n0 >> b);
946 n0 = n0 << bm;
948 udiv_qrnnd (q1, n1, n2, n1, d0);
951 /* n1 != d0... */
953 udiv_qrnnd (q0, n0, n1, n0, d0);
955 /* Remainder in n0 >> bm. */
958 if (rp != 0)
960 rr.s.low = n0 >> bm;
961 rr.s.high = 0;
962 *rp = rr.ll;
965 #endif /* UDIV_NEEDS_NORMALIZATION */
967 else
969 if (d1 > n1)
971 /* 00 = nn / DD */
973 q0 = 0;
974 q1 = 0;
976 /* Remainder in n1n0. */
977 if (rp != 0)
979 rr.s.low = n0;
980 rr.s.high = n1;
981 *rp = rr.ll;
984 else
986 /* 0q = NN / dd */
988 count_leading_zeros (bm, d1);
989 if (bm == 0)
991 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
992 conclude (the most significant bit of n1 is set) /\ (the
993 quotient digit q0 = 0 or 1).
995 This special case is necessary, not an optimization. */
997 /* The condition on the next line takes advantage of that
998 n1 >= d1 (true due to program flow). */
999 if (n1 > d1 || n0 >= d0)
1001 q0 = 1;
1002 sub_ddmmss (n1, n0, n1, n0, d1, d0);
1004 else
1005 q0 = 0;
1007 q1 = 0;
1009 if (rp != 0)
1011 rr.s.low = n0;
1012 rr.s.high = n1;
1013 *rp = rr.ll;
1016 else
1018 UWtype m1, m0;
1019 /* Normalize. */
1021 b = W_TYPE_SIZE - bm;
1023 d1 = (d1 << bm) | (d0 >> b);
1024 d0 = d0 << bm;
1025 n2 = n1 >> b;
1026 n1 = (n1 << bm) | (n0 >> b);
1027 n0 = n0 << bm;
1029 udiv_qrnnd (q0, n1, n2, n1, d1);
1030 umul_ppmm (m1, m0, q0, d0);
1032 if (m1 > n1 || (m1 == n1 && m0 > n0))
1034 q0--;
1035 sub_ddmmss (m1, m0, m1, m0, d1, d0);
1038 q1 = 0;
1040 /* Remainder in (n1n0 - m1m0) >> bm. */
1041 if (rp != 0)
1043 sub_ddmmss (n1, n0, n1, n0, m1, m0);
1044 rr.s.low = (n1 << b) | (n0 >> bm);
1045 rr.s.high = n1 >> bm;
1046 *rp = rr.ll;
1052 const DWunion ww = {{.low = q0, .high = q1}};
1053 return ww.ll;
1055 #endif
1057 #ifdef L_divdi3
1058 DWtype
1059 __divdi3 (DWtype u, DWtype v)
1061 word_type c = 0;
1062 DWunion uu = {.ll = u};
1063 DWunion vv = {.ll = v};
1064 DWtype w;
1066 if (uu.s.high < 0)
1067 c = ~c,
1068 uu.ll = -uu.ll;
1069 if (vv.s.high < 0)
1070 c = ~c,
1071 vv.ll = -vv.ll;
1073 w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0);
1074 if (c)
1075 w = -w;
1077 return w;
1079 #endif
1081 #ifdef L_moddi3
1082 DWtype
1083 __moddi3 (DWtype u, DWtype v)
1085 word_type c = 0;
1086 DWunion uu = {.ll = u};
1087 DWunion vv = {.ll = v};
1088 DWtype w;
1090 if (uu.s.high < 0)
1091 c = ~c,
1092 uu.ll = -uu.ll;
1093 if (vv.s.high < 0)
1094 vv.ll = -vv.ll;
1096 (void) __udivmoddi4 (uu.ll, vv.ll, (UDWtype*)&w);
1097 if (c)
1098 w = -w;
1100 return w;
1102 #endif
1104 #ifdef L_umoddi3
1105 UDWtype
1106 __umoddi3 (UDWtype u, UDWtype v)
1108 UDWtype w;
1110 (void) __udivmoddi4 (u, v, &w);
1112 return w;
1114 #endif
1116 #ifdef L_udivdi3
1117 UDWtype
1118 __udivdi3 (UDWtype n, UDWtype d)
1120 return __udivmoddi4 (n, d, (UDWtype *) 0);
1122 #endif
1124 #ifdef L_cmpdi2
1125 word_type
1126 __cmpdi2 (DWtype a, DWtype b)
1128 const DWunion au = {.ll = a};
1129 const DWunion bu = {.ll = b};
1131 if (au.s.high < bu.s.high)
1132 return 0;
1133 else if (au.s.high > bu.s.high)
1134 return 2;
1135 if ((UWtype) au.s.low < (UWtype) bu.s.low)
1136 return 0;
1137 else if ((UWtype) au.s.low > (UWtype) bu.s.low)
1138 return 2;
1139 return 1;
1141 #endif
1143 #ifdef L_ucmpdi2
1144 word_type
1145 __ucmpdi2 (DWtype a, DWtype b)
1147 const DWunion au = {.ll = a};
1148 const DWunion bu = {.ll = b};
1150 if ((UWtype) au.s.high < (UWtype) bu.s.high)
1151 return 0;
1152 else if ((UWtype) au.s.high > (UWtype) bu.s.high)
1153 return 2;
1154 if ((UWtype) au.s.low < (UWtype) bu.s.low)
1155 return 0;
1156 else if ((UWtype) au.s.low > (UWtype) bu.s.low)
1157 return 2;
1158 return 1;
1160 #endif
1162 #if defined(L_fixunstfdi) && LIBGCC2_HAS_TF_MODE
1163 DWtype
1164 __fixunstfDI (TFtype a)
1166 if (a < 0)
1167 return 0;
1169 /* Compute high word of result, as a flonum. */
1170 const TFtype b = (a / Wtype_MAXp1_F);
1171 /* Convert that to fixed (but not to DWtype!),
1172 and shift it into the high word. */
1173 UDWtype v = (UWtype) b;
1174 v <<= W_TYPE_SIZE;
1175 /* Remove high part from the TFtype, leaving the low part as flonum. */
1176 a -= (TFtype)v;
1177 /* Convert that to fixed (but not to DWtype!) and add it in.
1178 Sometimes A comes out negative. This is significant, since
1179 A has more bits than a long int does. */
1180 if (a < 0)
1181 v -= (UWtype) (- a);
1182 else
1183 v += (UWtype) a;
1184 return v;
1186 #endif
1188 #if defined(L_fixtfdi) && LIBGCC2_HAS_TF_MODE
1189 DWtype
1190 __fixtfdi (TFtype a)
1192 if (a < 0)
1193 return - __fixunstfDI (-a);
1194 return __fixunstfDI (a);
1196 #endif
1198 #if defined(L_fixunsxfdi) && LIBGCC2_HAS_XF_MODE
1199 DWtype
1200 __fixunsxfDI (XFtype a)
1202 if (a < 0)
1203 return 0;
1205 /* Compute high word of result, as a flonum. */
1206 const XFtype b = (a / Wtype_MAXp1_F);
1207 /* Convert that to fixed (but not to DWtype!),
1208 and shift it into the high word. */
1209 UDWtype v = (UWtype) b;
1210 v <<= W_TYPE_SIZE;
1211 /* Remove high part from the XFtype, leaving the low part as flonum. */
1212 a -= (XFtype)v;
1213 /* Convert that to fixed (but not to DWtype!) and add it in.
1214 Sometimes A comes out negative. This is significant, since
1215 A has more bits than a long int does. */
1216 if (a < 0)
1217 v -= (UWtype) (- a);
1218 else
1219 v += (UWtype) a;
1220 return v;
1222 #endif
1224 #if defined(L_fixxfdi) && LIBGCC2_HAS_XF_MODE
1225 DWtype
1226 __fixxfdi (XFtype a)
1228 if (a < 0)
1229 return - __fixunsxfDI (-a);
1230 return __fixunsxfDI (a);
1232 #endif
1234 #if defined(L_fixunsdfdi) && LIBGCC2_HAS_DF_MODE
1235 DWtype
1236 __fixunsdfDI (DFtype a)
1238 /* Get high part of result. The division here will just moves the radix
1239 point and will not cause any rounding. Then the conversion to integral
1240 type chops result as desired. */
1241 const UWtype hi = a / Wtype_MAXp1_F;
1243 /* Get low part of result. Convert `hi' to floating type and scale it back,
1244 then subtract this from the number being converted. This leaves the low
1245 part. Convert that to integral type. */
1246 const UWtype lo = a - (DFtype) hi * Wtype_MAXp1_F;
1248 /* Assemble result from the two parts. */
1249 return ((UDWtype) hi << W_TYPE_SIZE) | lo;
1251 #endif
1253 #if defined(L_fixdfdi) && LIBGCC2_HAS_DF_MODE
1254 DWtype
1255 __fixdfdi (DFtype a)
1257 if (a < 0)
1258 return - __fixunsdfDI (-a);
1259 return __fixunsdfDI (a);
1261 #endif
1263 #if defined(L_fixunssfdi) && LIBGCC2_HAS_SF_MODE
1264 DWtype
1265 __fixunssfDI (SFtype a)
1267 #if LIBGCC2_HAS_DF_MODE
1268 /* Convert the SFtype to a DFtype, because that is surely not going
1269 to lose any bits. Some day someone else can write a faster version
1270 that avoids converting to DFtype, and verify it really works right. */
1271 const DFtype dfa = a;
1273 /* Get high part of result. The division here will just moves the radix
1274 point and will not cause any rounding. Then the conversion to integral
1275 type chops result as desired. */
1276 const UWtype hi = dfa / Wtype_MAXp1_F;
1278 /* Get low part of result. Convert `hi' to floating type and scale it back,
1279 then subtract this from the number being converted. This leaves the low
1280 part. Convert that to integral type. */
1281 const UWtype lo = dfa - (DFtype) hi * Wtype_MAXp1_F;
1283 /* Assemble result from the two parts. */
1284 return ((UDWtype) hi << W_TYPE_SIZE) | lo;
1285 #elif FLT_MANT_DIG < W_TYPE_SIZE
1286 if (a < 1)
1287 return 0;
1288 if (a < Wtype_MAXp1_F)
1289 return (UWtype)a;
1290 if (a < Wtype_MAXp1_F * Wtype_MAXp1_F)
1292 /* Since we know that there are fewer significant bits in the SFmode
1293 quantity than in a word, we know that we can convert out all the
1294 significant bits in one step, and thus avoid losing bits. */
1296 /* ??? This following loop essentially performs frexpf. If we could
1297 use the real libm function, or poke at the actual bits of the fp
1298 format, it would be significantly faster. */
1300 UWtype shift = 0, counter;
1301 SFtype msb;
1303 a /= Wtype_MAXp1_F;
1304 for (counter = W_TYPE_SIZE / 2; counter != 0; counter >>= 1)
1306 SFtype counterf = (UWtype)1 << counter;
1307 if (a >= counterf)
1309 shift |= counter;
1310 a /= counterf;
1314 /* Rescale into the range of one word, extract the bits of that
1315 one word, and shift the result into position. */
1316 a *= Wtype_MAXp1_F;
1317 counter = a;
1318 return (DWtype)counter << shift;
1320 return -1;
1321 #else
1322 # error
1323 #endif
1325 #endif
1327 #if defined(L_fixsfdi) && LIBGCC2_HAS_SF_MODE
1328 DWtype
1329 __fixsfdi (SFtype a)
1331 if (a < 0)
1332 return - __fixunssfDI (-a);
1333 return __fixunssfDI (a);
1335 #endif
1337 #if defined(L_floatdixf) && LIBGCC2_HAS_XF_MODE
1338 XFtype
1339 __floatdixf (DWtype u)
1341 XFtype d = (Wtype) (u >> W_TYPE_SIZE);
1342 d *= Wtype_MAXp1_F;
1343 d += (UWtype)u;
1344 return d;
1346 #endif
1348 #if defined(L_floatditf) && LIBGCC2_HAS_TF_MODE
1349 TFtype
1350 __floatditf (DWtype u)
1352 TFtype d = (Wtype) (u >> W_TYPE_SIZE);
1353 d *= Wtype_MAXp1_F;
1354 d += (UWtype)u;
1355 return d;
1357 #endif
1359 #if defined(L_floatdidf) && LIBGCC2_HAS_DF_MODE
1360 DFtype
1361 __floatdidf (DWtype u)
1363 DFtype d = (Wtype) (u >> W_TYPE_SIZE);
1364 d *= Wtype_MAXp1_F;
1365 d += (UWtype)u;
1366 return d;
1368 #endif
1370 #if defined(L_floatdisf) && LIBGCC2_HAS_SF_MODE
1371 #define DI_SIZE (W_TYPE_SIZE * 2)
1372 #define SF_SIZE FLT_MANT_DIG
1374 SFtype
1375 __floatdisf (DWtype u)
1377 #if SF_SIZE >= W_TYPE_SIZE
1378 /* When the word size is small, we never get any rounding error. */
1379 SFtype f = (Wtype) (u >> W_TYPE_SIZE);
1380 f *= Wtype_MAXp1_F;
1381 f += (UWtype)u;
1382 return f;
1383 #elif LIBGCC2_HAS_DF_MODE
1385 #if LIBGCC2_DOUBLE_TYPE_SIZE == 64
1386 #define DF_SIZE DBL_MANT_DIG
1387 #elif LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 64
1388 #define DF_SIZE LDBL_MANT_DIG
1389 #else
1390 # error
1391 #endif
1393 #define REP_BIT ((UDWtype) 1 << (DI_SIZE - DF_SIZE))
1395 /* Protect against double-rounding error.
1396 Represent any low-order bits, that might be truncated by a bit that
1397 won't be lost. The bit can go in anywhere below the rounding position
1398 of the SFmode. A fixed mask and bit position handles all usual
1399 configurations. It doesn't handle the case of 128-bit DImode, however. */
1400 if (DF_SIZE < DI_SIZE
1401 && DF_SIZE > (DI_SIZE - DF_SIZE + SF_SIZE))
1403 if (! (- ((DWtype) 1 << DF_SIZE) < u
1404 && u < ((DWtype) 1 << DF_SIZE)))
1406 if ((UDWtype) u & (REP_BIT - 1))
1408 u &= ~ (REP_BIT - 1);
1409 u |= REP_BIT;
1414 /* Do the calculation in DFmode so that we don't lose any of the
1415 precision of the high word while multiplying it. */
1416 DFtype f = (Wtype) (u >> W_TYPE_SIZE);
1417 f *= Wtype_MAXp1_F;
1418 f += (UWtype)u;
1419 return (SFtype) f;
1420 #else
1421 /* Finally, the word size is larger than the number of bits in SFmode,
1422 and we've got no DFmode. The only way to avoid double rounding is
1423 to special case the extraction. */
1425 /* If there are no high bits set, fall back to one conversion. */
1426 if ((Wtype)u == u)
1427 return (SFtype)(Wtype)u;
1429 /* Otherwise, find the power of two. */
1430 Wtype hi = u >> W_TYPE_SIZE;
1431 if (hi < 0)
1432 hi = -hi;
1434 UWtype count, shift;
1435 count_leading_zeros (count, hi);
1437 /* No leading bits means u == minimum. */
1438 if (count == 0)
1439 return -(Wtype_MAXp1_F * Wtype_MAXp1_F / 2);
1441 shift = W_TYPE_SIZE - count;
1443 /* Shift down the most significant bits. */
1444 hi = u >> shift;
1446 /* If we lost any nonzero bits, set the lsb to ensure correct rounding. */
1447 if (u & ((1 << shift) - 1))
1448 hi |= 1;
1450 /* Convert the one word of data, and rescale. */
1451 SFtype f = hi;
1452 f *= (UWtype)1 << shift;
1453 return f;
1454 #endif
1456 #endif
1458 #if defined(L_fixunsxfsi) && LIBGCC2_HAS_XF_MODE
1459 /* Reenable the normal types, in case limits.h needs them. */
1460 #undef char
1461 #undef short
1462 #undef int
1463 #undef long
1464 #undef unsigned
1465 #undef float
1466 #undef double
1467 #undef MIN
1468 #undef MAX
1469 #include <limits.h>
1471 UWtype
1472 __fixunsxfSI (XFtype a)
1474 if (a >= - (DFtype) Wtype_MIN)
1475 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1476 return (Wtype) a;
1478 #endif
1480 #if defined(L_fixunsdfsi) && LIBGCC2_HAS_DF_MODE
1481 /* Reenable the normal types, in case limits.h needs them. */
1482 #undef char
1483 #undef short
1484 #undef int
1485 #undef long
1486 #undef unsigned
1487 #undef float
1488 #undef double
1489 #undef MIN
1490 #undef MAX
1491 #include <limits.h>
1493 UWtype
1494 __fixunsdfSI (DFtype a)
1496 if (a >= - (DFtype) Wtype_MIN)
1497 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1498 return (Wtype) a;
1500 #endif
1502 #if defined(L_fixunssfsi) && LIBGCC2_HAS_SF_MODE
1503 /* Reenable the normal types, in case limits.h needs them. */
1504 #undef char
1505 #undef short
1506 #undef int
1507 #undef long
1508 #undef unsigned
1509 #undef float
1510 #undef double
1511 #undef MIN
1512 #undef MAX
1513 #include <limits.h>
1515 UWtype
1516 __fixunssfSI (SFtype a)
1518 if (a >= - (SFtype) Wtype_MIN)
1519 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1520 return (Wtype) a;
1522 #endif
1524 /* Integer power helper used from __builtin_powi for non-constant
1525 exponents. */
1527 #if (defined(L_powisf2) && LIBGCC2_HAS_SF_MODE) \
1528 || (defined(L_powidf2) && LIBGCC2_HAS_DF_MODE) \
1529 || (defined(L_powixf2) && LIBGCC2_HAS_XF_MODE) \
1530 || (defined(L_powitf2) && LIBGCC2_HAS_TF_MODE)
1531 # if defined(L_powisf2)
1532 # define TYPE SFtype
1533 # define NAME __powisf2
1534 # elif defined(L_powidf2)
1535 # define TYPE DFtype
1536 # define NAME __powidf2
1537 # elif defined(L_powixf2)
1538 # define TYPE XFtype
1539 # define NAME __powixf2
1540 # elif defined(L_powitf2)
1541 # define TYPE TFtype
1542 # define NAME __powitf2
1543 # endif
1545 #undef int
1546 #undef unsigned
1547 TYPE
1548 NAME (TYPE x, int m)
1550 unsigned int n = m < 0 ? -m : m;
1551 TYPE y = n % 2 ? x : 1;
1552 while (n >>= 1)
1554 x = x * x;
1555 if (n % 2)
1556 y = y * x;
1558 return m < 0 ? 1/y : y;
1561 #endif
1563 #if ((defined(L_mulsc3) || defined(L_divsc3)) && LIBGCC2_HAS_SF_MODE) \
1564 || ((defined(L_muldc3) || defined(L_divdc3)) && LIBGCC2_HAS_DF_MODE) \
1565 || ((defined(L_mulxc3) || defined(L_divxc3)) && LIBGCC2_HAS_XF_MODE) \
1566 || ((defined(L_multc3) || defined(L_divtc3)) && LIBGCC2_HAS_TF_MODE)
1568 #undef float
1569 #undef double
1570 #undef long
1572 #if defined(L_mulsc3) || defined(L_divsc3)
1573 # define MTYPE SFtype
1574 # define CTYPE SCtype
1575 # define MODE sc
1576 # define CEXT f
1577 # define NOTRUNC __FLT_EVAL_METHOD__ == 0
1578 #elif defined(L_muldc3) || defined(L_divdc3)
1579 # define MTYPE DFtype
1580 # define CTYPE DCtype
1581 # define MODE dc
1582 # if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 64
1583 # define CEXT l
1584 # define NOTRUNC 1
1585 # else
1586 # define CEXT
1587 # define NOTRUNC __FLT_EVAL_METHOD__ == 0 || __FLT_EVAL_METHOD__ == 1
1588 # endif
1589 #elif defined(L_mulxc3) || defined(L_divxc3)
1590 # define MTYPE XFtype
1591 # define CTYPE XCtype
1592 # define MODE xc
1593 # define CEXT l
1594 # define NOTRUNC 1
1595 #elif defined(L_multc3) || defined(L_divtc3)
1596 # define MTYPE TFtype
1597 # define CTYPE TCtype
1598 # define MODE tc
1599 # define CEXT l
1600 # define NOTRUNC 1
1601 #else
1602 # error
1603 #endif
1605 #define CONCAT3(A,B,C) _CONCAT3(A,B,C)
1606 #define _CONCAT3(A,B,C) A##B##C
1608 #define CONCAT2(A,B) _CONCAT2(A,B)
1609 #define _CONCAT2(A,B) A##B
1611 /* All of these would be present in a full C99 implementation of <math.h>
1612 and <complex.h>. Our problem is that only a few systems have such full
1613 implementations. Further, libgcc_s.so isn't currently linked against
1614 libm.so, and even for systems that do provide full C99, the extra overhead
1615 of all programs using libgcc having to link against libm. So avoid it. */
1617 #define isnan(x) __builtin_expect ((x) != (x), 0)
1618 #define isfinite(x) __builtin_expect (!isnan((x) - (x)), 1)
1619 #define isinf(x) __builtin_expect (!isnan(x) & !isfinite(x), 0)
1621 #define INFINITY CONCAT2(__builtin_inf, CEXT) ()
1622 #define I 1i
1624 /* Helpers to make the following code slightly less gross. */
1625 #define COPYSIGN CONCAT2(__builtin_copysign, CEXT)
1626 #define FABS CONCAT2(__builtin_fabs, CEXT)
1628 /* Verify that MTYPE matches up with CEXT. */
1629 extern void *compile_type_assert[sizeof(INFINITY) == sizeof(MTYPE) ? 1 : -1];
1631 /* Ensure that we've lost any extra precision. */
1632 #if NOTRUNC
1633 # define TRUNC(x)
1634 #else
1635 # define TRUNC(x) __asm__ ("" : "=m"(x) : "m"(x))
1636 #endif
1638 #if defined(L_mulsc3) || defined(L_muldc3) \
1639 || defined(L_mulxc3) || defined(L_multc3)
1641 CTYPE
1642 CONCAT3(__mul,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
1644 MTYPE ac, bd, ad, bc, x, y;
1646 ac = a * c;
1647 bd = b * d;
1648 ad = a * d;
1649 bc = b * c;
1651 TRUNC (ac);
1652 TRUNC (bd);
1653 TRUNC (ad);
1654 TRUNC (bc);
1656 x = ac - bd;
1657 y = ad + bc;
1659 if (isnan (x) && isnan (y))
1661 /* Recover infinities that computed as NaN + iNaN. */
1662 _Bool recalc = 0;
1663 if (isinf (a) || isinf (b))
1665 /* z is infinite. "Box" the infinity and change NaNs in
1666 the other factor to 0. */
1667 a = COPYSIGN (isinf (a) ? 1 : 0, a);
1668 b = COPYSIGN (isinf (b) ? 1 : 0, b);
1669 if (isnan (c)) c = COPYSIGN (0, c);
1670 if (isnan (d)) d = COPYSIGN (0, d);
1671 recalc = 1;
1673 if (isinf (c) || isinf (d))
1675 /* w is infinite. "Box" the infinity and change NaNs in
1676 the other factor to 0. */
1677 c = COPYSIGN (isinf (c) ? 1 : 0, c);
1678 d = COPYSIGN (isinf (d) ? 1 : 0, d);
1679 if (isnan (a)) a = COPYSIGN (0, a);
1680 if (isnan (b)) b = COPYSIGN (0, b);
1681 recalc = 1;
1683 if (!recalc
1684 && (isinf (ac) || isinf (bd)
1685 || isinf (ad) || isinf (bc)))
1687 /* Recover infinities from overflow by changing NaNs to 0. */
1688 if (isnan (a)) a = COPYSIGN (0, a);
1689 if (isnan (b)) b = COPYSIGN (0, b);
1690 if (isnan (c)) c = COPYSIGN (0, c);
1691 if (isnan (d)) d = COPYSIGN (0, d);
1692 recalc = 1;
1694 if (recalc)
1696 x = INFINITY * (a * c - b * d);
1697 y = INFINITY * (a * d + b * c);
1701 return x + I * y;
1703 #endif /* complex multiply */
1705 #if defined(L_divsc3) || defined(L_divdc3) \
1706 || defined(L_divxc3) || defined(L_divtc3)
1708 CTYPE
1709 CONCAT3(__div,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
1711 MTYPE denom, ratio, x, y;
1713 /* ??? We can get better behavior from logarithmic scaling instead of
1714 the division. But that would mean starting to link libgcc against
1715 libm. We could implement something akin to ldexp/frexp as gcc builtins
1716 fairly easily... */
1717 if (FABS (c) < FABS (d))
1719 ratio = c / d;
1720 denom = (c * ratio) + d;
1721 x = ((a * ratio) + b) / denom;
1722 y = ((b * ratio) - a) / denom;
1724 else
1726 ratio = d / c;
1727 denom = (d * ratio) + c;
1728 x = ((b * ratio) + a) / denom;
1729 y = (b - (a * ratio)) / denom;
1732 /* Recover infinities and zeros that computed as NaN+iNaN; the only cases
1733 are nonzero/zero, infinite/finite, and finite/infinite. */
1734 if (isnan (x) && isnan (y))
1736 if (c == 0.0 && d == 0.0 && (!isnan (a) || !isnan (b)))
1738 x = COPYSIGN (INFINITY, c) * a;
1739 y = COPYSIGN (INFINITY, c) * b;
1741 else if ((isinf (a) || isinf (b)) && isfinite (c) && isfinite (d))
1743 a = COPYSIGN (isinf (a) ? 1 : 0, a);
1744 b = COPYSIGN (isinf (b) ? 1 : 0, b);
1745 x = INFINITY * (a * c + b * d);
1746 y = INFINITY * (b * c - a * d);
1748 else if ((isinf (c) || isinf (d)) && isfinite (a) && isfinite (b))
1750 c = COPYSIGN (isinf (c) ? 1 : 0, c);
1751 d = COPYSIGN (isinf (d) ? 1 : 0, d);
1752 x = 0.0 * (a * c + b * d);
1753 y = 0.0 * (b * c - a * d);
1757 return x + I * y;
1759 #endif /* complex divide */
1761 #endif /* all complex float routines */
1763 /* From here on down, the routines use normal data types. */
1765 #define SItype bogus_type
1766 #define USItype bogus_type
1767 #define DItype bogus_type
1768 #define UDItype bogus_type
1769 #define SFtype bogus_type
1770 #define DFtype bogus_type
1771 #undef Wtype
1772 #undef UWtype
1773 #undef HWtype
1774 #undef UHWtype
1775 #undef DWtype
1776 #undef UDWtype
1778 #undef char
1779 #undef short
1780 #undef int
1781 #undef long
1782 #undef unsigned
1783 #undef float
1784 #undef double
1786 #ifdef L__gcc_bcmp
1788 /* Like bcmp except the sign is meaningful.
1789 Result is negative if S1 is less than S2,
1790 positive if S1 is greater, 0 if S1 and S2 are equal. */
1793 __gcc_bcmp (const unsigned char *s1, const unsigned char *s2, size_t size)
1795 while (size > 0)
1797 const unsigned char c1 = *s1++, c2 = *s2++;
1798 if (c1 != c2)
1799 return c1 - c2;
1800 size--;
1802 return 0;
1805 #endif
1807 /* __eprintf used to be used by GCC's private version of <assert.h>.
1808 We no longer provide that header, but this routine remains in libgcc.a
1809 for binary backward compatibility. Note that it is not included in
1810 the shared version of libgcc. */
1811 #ifdef L_eprintf
1812 #ifndef inhibit_libc
1814 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
1815 #include <stdio.h>
1817 void
1818 __eprintf (const char *string, const char *expression,
1819 unsigned int line, const char *filename)
1821 fprintf (stderr, string, expression, line, filename);
1822 fflush (stderr);
1823 abort ();
1826 #endif
1827 #endif
1830 #ifdef L_clear_cache
1831 /* Clear part of an instruction cache. */
1833 void
1834 __clear_cache (char *beg __attribute__((__unused__)),
1835 char *end __attribute__((__unused__)))
1837 #ifdef CLEAR_INSN_CACHE
1838 CLEAR_INSN_CACHE (beg, end);
1839 #endif /* CLEAR_INSN_CACHE */
1842 #endif /* L_clear_cache */
1844 #ifdef L_enable_execute_stack
1845 /* Attempt to turn on execute permission for the stack. */
1847 #ifdef ENABLE_EXECUTE_STACK
1848 ENABLE_EXECUTE_STACK
1849 #else
1850 void
1851 __enable_execute_stack (void *addr __attribute__((__unused__)))
1853 #endif /* ENABLE_EXECUTE_STACK */
1855 #endif /* L_enable_execute_stack */
1857 #ifdef L_trampoline
1859 /* Jump to a trampoline, loading the static chain address. */
1861 #if defined(WINNT) && ! defined(__CYGWIN__) && ! defined (_UWIN)
1864 getpagesize (void)
1866 #ifdef _ALPHA_
1867 return 8192;
1868 #else
1869 return 4096;
1870 #endif
1873 #ifdef __i386__
1874 extern int VirtualProtect (char *, int, int, int *) __attribute__((stdcall));
1875 #endif
1878 mprotect (char *addr, int len, int prot)
1880 int np, op;
1882 if (prot == 7)
1883 np = 0x40;
1884 else if (prot == 5)
1885 np = 0x20;
1886 else if (prot == 4)
1887 np = 0x10;
1888 else if (prot == 3)
1889 np = 0x04;
1890 else if (prot == 1)
1891 np = 0x02;
1892 else if (prot == 0)
1893 np = 0x01;
1895 if (VirtualProtect (addr, len, np, &op))
1896 return 0;
1897 else
1898 return -1;
1901 #endif /* WINNT && ! __CYGWIN__ && ! _UWIN */
1903 #ifdef TRANSFER_FROM_TRAMPOLINE
1904 TRANSFER_FROM_TRAMPOLINE
1905 #endif
1906 #endif /* L_trampoline */
1908 #ifndef __CYGWIN__
1909 #ifdef L__main
1911 #include "gbl-ctors.h"
1913 /* Some systems use __main in a way incompatible with its use in gcc, in these
1914 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
1915 give the same symbol without quotes for an alternative entry point. You
1916 must define both, or neither. */
1917 #ifndef NAME__MAIN
1918 #define NAME__MAIN "__main"
1919 #define SYMBOL__MAIN __main
1920 #endif
1922 #if defined (INIT_SECTION_ASM_OP) || defined (INIT_ARRAY_SECTION_ASM_OP)
1923 #undef HAS_INIT_SECTION
1924 #define HAS_INIT_SECTION
1925 #endif
1927 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
1929 /* Some ELF crosses use crtstuff.c to provide __CTOR_LIST__, but use this
1930 code to run constructors. In that case, we need to handle EH here, too. */
1932 #ifdef EH_FRAME_SECTION_NAME
1933 #include "unwind-dw2-fde.h"
1934 extern unsigned char __EH_FRAME_BEGIN__[];
1935 #endif
1937 /* Run all the global destructors on exit from the program. */
1939 void
1940 __do_global_dtors (void)
1942 #ifdef DO_GLOBAL_DTORS_BODY
1943 DO_GLOBAL_DTORS_BODY;
1944 #else
1945 static func_ptr *p = __DTOR_LIST__ + 1;
1946 while (*p)
1948 p++;
1949 (*(p-1)) ();
1951 #endif
1952 #if defined (EH_FRAME_SECTION_NAME) && !defined (HAS_INIT_SECTION)
1954 static int completed = 0;
1955 if (! completed)
1957 completed = 1;
1958 __deregister_frame_info (__EH_FRAME_BEGIN__);
1961 #endif
1963 #endif
1965 #ifndef HAS_INIT_SECTION
1966 /* Run all the global constructors on entry to the program. */
1968 void
1969 __do_global_ctors (void)
1971 #ifdef EH_FRAME_SECTION_NAME
1973 static struct object object;
1974 __register_frame_info (__EH_FRAME_BEGIN__, &object);
1976 #endif
1977 DO_GLOBAL_CTORS_BODY;
1978 atexit (__do_global_dtors);
1980 #endif /* no HAS_INIT_SECTION */
1982 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
1983 /* Subroutine called automatically by `main'.
1984 Compiling a global function named `main'
1985 produces an automatic call to this function at the beginning.
1987 For many systems, this routine calls __do_global_ctors.
1988 For systems which support a .init section we use the .init section
1989 to run __do_global_ctors, so we need not do anything here. */
1991 extern void SYMBOL__MAIN (void);
1992 void
1993 SYMBOL__MAIN (void)
1995 /* Support recursive calls to `main': run initializers just once. */
1996 static int initialized;
1997 if (! initialized)
1999 initialized = 1;
2000 __do_global_ctors ();
2003 #endif /* no HAS_INIT_SECTION or INVOKE__main */
2005 #endif /* L__main */
2006 #endif /* __CYGWIN__ */
2008 #ifdef L_ctors
2010 #include "gbl-ctors.h"
2012 /* Provide default definitions for the lists of constructors and
2013 destructors, so that we don't get linker errors. These symbols are
2014 intentionally bss symbols, so that gld and/or collect will provide
2015 the right values. */
2017 /* We declare the lists here with two elements each,
2018 so that they are valid empty lists if no other definition is loaded.
2020 If we are using the old "set" extensions to have the gnu linker
2021 collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
2022 must be in the bss/common section.
2024 Long term no port should use those extensions. But many still do. */
2025 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
2026 #if defined (TARGET_ASM_CONSTRUCTOR) || defined (USE_COLLECT2)
2027 func_ptr __CTOR_LIST__[2] = {0, 0};
2028 func_ptr __DTOR_LIST__[2] = {0, 0};
2029 #else
2030 func_ptr __CTOR_LIST__[2];
2031 func_ptr __DTOR_LIST__[2];
2032 #endif
2033 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
2034 #endif /* L_ctors */
2035 #endif /* LIBGCC2_UNITS_PER_WORD <= MIN_UNITS_PER_WORD */