* config.gcc (cygwin tm_file): Add cygwin-stdint.h.
[official-gcc.git] / gcc / libgcc2.c
blob369c29f6abff1a201c390d2a6effbe272b7c6db9
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, 2007, 2008
5 Free Software Foundation, Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
14 In addition to the permissions in the GNU General Public License, the
15 Free Software Foundation gives you unlimited permission to link the
16 compiled version of this file into combinations with other programs,
17 and to distribute those combinations without any restriction coming
18 from the use of this file. (The General Public License restrictions
19 do apply in other respects; for example, they cover modification of
20 the file, and distribution when not linked into a combine
21 executable.)
23 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
24 WARRANTY; without even the implied warranty of MERCHANTABILITY or
25 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 for more details.
28 You should have received a copy of the GNU General Public License
29 along with GCC; see the file COPYING. If not, write to the Free
30 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
31 02110-1301, USA. */
33 #include "tconfig.h"
34 #include "tsystem.h"
35 #include "coretypes.h"
36 #include "tm.h"
38 #ifdef HAVE_GAS_HIDDEN
39 #define ATTRIBUTE_HIDDEN __attribute__ ((__visibility__ ("hidden")))
40 #else
41 #define ATTRIBUTE_HIDDEN
42 #endif
44 #ifndef MIN_UNITS_PER_WORD
45 #define MIN_UNITS_PER_WORD UNITS_PER_WORD
46 #endif
48 /* Work out the largest "word" size that we can deal with on this target. */
49 #if MIN_UNITS_PER_WORD > 4
50 # define LIBGCC2_MAX_UNITS_PER_WORD 8
51 #elif (MIN_UNITS_PER_WORD > 2 \
52 || (MIN_UNITS_PER_WORD > 1 && LONG_LONG_TYPE_SIZE > 32))
53 # define LIBGCC2_MAX_UNITS_PER_WORD 4
54 #else
55 # define LIBGCC2_MAX_UNITS_PER_WORD MIN_UNITS_PER_WORD
56 #endif
58 /* Work out what word size we are using for this compilation.
59 The value can be set on the command line. */
60 #ifndef LIBGCC2_UNITS_PER_WORD
61 #define LIBGCC2_UNITS_PER_WORD LIBGCC2_MAX_UNITS_PER_WORD
62 #endif
64 #if LIBGCC2_UNITS_PER_WORD <= LIBGCC2_MAX_UNITS_PER_WORD
66 #include "libgcc2.h"
68 #ifdef DECLARE_LIBRARY_RENAMES
69 DECLARE_LIBRARY_RENAMES
70 #endif
72 #if defined (L_negdi2)
73 DWtype
74 __negdi2 (DWtype u)
76 const DWunion uu = {.ll = u};
77 const DWunion w = { {.low = -uu.s.low,
78 .high = -uu.s.high - ((UWtype) -uu.s.low > 0) } };
80 return w.ll;
82 #endif
84 #ifdef L_addvsi3
85 Wtype
86 __addvSI3 (Wtype a, Wtype b)
88 const Wtype w = (UWtype) a + (UWtype) b;
90 if (b >= 0 ? w < a : w > a)
91 abort ();
93 return w;
95 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
96 SItype
97 __addvsi3 (SItype a, SItype b)
99 const SItype w = (USItype) a + (USItype) b;
101 if (b >= 0 ? w < a : w > a)
102 abort ();
104 return w;
106 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
107 #endif
109 #ifdef L_addvdi3
110 DWtype
111 __addvDI3 (DWtype a, DWtype b)
113 const DWtype w = (UDWtype) a + (UDWtype) b;
115 if (b >= 0 ? w < a : w > a)
116 abort ();
118 return w;
120 #endif
122 #ifdef L_subvsi3
123 Wtype
124 __subvSI3 (Wtype a, Wtype b)
126 const Wtype w = (UWtype) a - (UWtype) b;
128 if (b >= 0 ? w > a : w < a)
129 abort ();
131 return w;
133 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
134 SItype
135 __subvsi3 (SItype a, SItype b)
137 const SItype w = (USItype) a - (USItype) b;
139 if (b >= 0 ? w > a : w < a)
140 abort ();
142 return w;
144 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
145 #endif
147 #ifdef L_subvdi3
148 DWtype
149 __subvDI3 (DWtype a, DWtype b)
151 const DWtype w = (UDWtype) a - (UDWtype) b;
153 if (b >= 0 ? w > a : w < a)
154 abort ();
156 return w;
158 #endif
160 #ifdef L_mulvsi3
161 Wtype
162 __mulvSI3 (Wtype a, Wtype b)
164 const DWtype w = (DWtype) a * (DWtype) b;
166 if ((Wtype) (w >> W_TYPE_SIZE) != (Wtype) w >> (W_TYPE_SIZE - 1))
167 abort ();
169 return w;
171 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
172 #undef WORD_SIZE
173 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
174 SItype
175 __mulvsi3 (SItype a, SItype b)
177 const DItype w = (DItype) a * (DItype) b;
179 if ((SItype) (w >> WORD_SIZE) != (SItype) w >> (WORD_SIZE-1))
180 abort ();
182 return w;
184 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
185 #endif
187 #ifdef L_negvsi2
188 Wtype
189 __negvSI2 (Wtype a)
191 const Wtype w = -(UWtype) a;
193 if (a >= 0 ? w > 0 : w < 0)
194 abort ();
196 return w;
198 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
199 SItype
200 __negvsi2 (SItype a)
202 const SItype w = -(USItype) a;
204 if (a >= 0 ? w > 0 : w < 0)
205 abort ();
207 return w;
209 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
210 #endif
212 #ifdef L_negvdi2
213 DWtype
214 __negvDI2 (DWtype a)
216 const DWtype w = -(UDWtype) a;
218 if (a >= 0 ? w > 0 : w < 0)
219 abort ();
221 return w;
223 #endif
225 #ifdef L_absvsi2
226 Wtype
227 __absvSI2 (Wtype a)
229 Wtype w = a;
231 if (a < 0)
232 #ifdef L_negvsi2
233 w = __negvSI2 (a);
234 #else
235 w = -(UWtype) a;
237 if (w < 0)
238 abort ();
239 #endif
241 return w;
243 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
244 SItype
245 __absvsi2 (SItype a)
247 SItype w = a;
249 if (a < 0)
250 #ifdef L_negvsi2
251 w = __negvsi2 (a);
252 #else
253 w = -(USItype) a;
255 if (w < 0)
256 abort ();
257 #endif
259 return w;
261 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
262 #endif
264 #ifdef L_absvdi2
265 DWtype
266 __absvDI2 (DWtype a)
268 DWtype w = a;
270 if (a < 0)
271 #ifdef L_negvdi2
272 w = __negvDI2 (a);
273 #else
274 w = -(UDWtype) a;
276 if (w < 0)
277 abort ();
278 #endif
280 return w;
282 #endif
284 #ifdef L_mulvdi3
285 DWtype
286 __mulvDI3 (DWtype u, DWtype v)
288 /* The unchecked multiplication needs 3 Wtype x Wtype multiplications,
289 but the checked multiplication needs only two. */
290 const DWunion uu = {.ll = u};
291 const DWunion vv = {.ll = v};
293 if (__builtin_expect (uu.s.high == uu.s.low >> (W_TYPE_SIZE - 1), 1))
295 /* u fits in a single Wtype. */
296 if (__builtin_expect (vv.s.high == vv.s.low >> (W_TYPE_SIZE - 1), 1))
298 /* v fits in a single Wtype as well. */
299 /* A single multiplication. No overflow risk. */
300 return (DWtype) uu.s.low * (DWtype) vv.s.low;
302 else
304 /* Two multiplications. */
305 DWunion w0 = {.ll = (UDWtype) (UWtype) uu.s.low
306 * (UDWtype) (UWtype) vv.s.low};
307 DWunion w1 = {.ll = (UDWtype) (UWtype) uu.s.low
308 * (UDWtype) (UWtype) vv.s.high};
310 if (vv.s.high < 0)
311 w1.s.high -= uu.s.low;
312 if (uu.s.low < 0)
313 w1.ll -= vv.ll;
314 w1.ll += (UWtype) w0.s.high;
315 if (__builtin_expect (w1.s.high == w1.s.low >> (W_TYPE_SIZE - 1), 1))
317 w0.s.high = w1.s.low;
318 return w0.ll;
322 else
324 if (__builtin_expect (vv.s.high == vv.s.low >> (W_TYPE_SIZE - 1), 1))
326 /* v fits into a single Wtype. */
327 /* Two multiplications. */
328 DWunion w0 = {.ll = (UDWtype) (UWtype) uu.s.low
329 * (UDWtype) (UWtype) vv.s.low};
330 DWunion w1 = {.ll = (UDWtype) (UWtype) uu.s.high
331 * (UDWtype) (UWtype) vv.s.low};
333 if (uu.s.high < 0)
334 w1.s.high -= vv.s.low;
335 if (vv.s.low < 0)
336 w1.ll -= uu.ll;
337 w1.ll += (UWtype) w0.s.high;
338 if (__builtin_expect (w1.s.high == w1.s.low >> (W_TYPE_SIZE - 1), 1))
340 w0.s.high = w1.s.low;
341 return w0.ll;
344 else
346 /* A few sign checks and a single multiplication. */
347 if (uu.s.high >= 0)
349 if (vv.s.high >= 0)
351 if (uu.s.high == 0 && vv.s.high == 0)
353 const DWtype w = (UDWtype) (UWtype) uu.s.low
354 * (UDWtype) (UWtype) vv.s.low;
355 if (__builtin_expect (w >= 0, 1))
356 return w;
359 else
361 if (uu.s.high == 0 && vv.s.high == (Wtype) -1)
363 DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
364 * (UDWtype) (UWtype) vv.s.low};
366 ww.s.high -= uu.s.low;
367 if (__builtin_expect (ww.s.high < 0, 1))
368 return ww.ll;
372 else
374 if (vv.s.high >= 0)
376 if (uu.s.high == (Wtype) -1 && vv.s.high == 0)
378 DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
379 * (UDWtype) (UWtype) vv.s.low};
381 ww.s.high -= vv.s.low;
382 if (__builtin_expect (ww.s.high < 0, 1))
383 return ww.ll;
386 else
388 if (uu.s.high == (Wtype) -1 && vv.s.high == (Wtype) - 1)
390 DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
391 * (UDWtype) (UWtype) vv.s.low};
393 ww.s.high -= uu.s.low;
394 ww.s.high -= vv.s.low;
395 if (__builtin_expect (ww.s.high >= 0, 1))
396 return ww.ll;
403 /* Overflow. */
404 abort ();
406 #endif
409 /* Unless shift functions are defined with full ANSI prototypes,
410 parameter b will be promoted to int if shift_count_type is smaller than an int. */
411 #ifdef L_lshrdi3
412 DWtype
413 __lshrdi3 (DWtype u, shift_count_type b)
415 if (b == 0)
416 return u;
418 const DWunion uu = {.ll = u};
419 const shift_count_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
420 DWunion w;
422 if (bm <= 0)
424 w.s.high = 0;
425 w.s.low = (UWtype) uu.s.high >> -bm;
427 else
429 const UWtype carries = (UWtype) uu.s.high << bm;
431 w.s.high = (UWtype) uu.s.high >> b;
432 w.s.low = ((UWtype) uu.s.low >> b) | carries;
435 return w.ll;
437 #endif
439 #ifdef L_ashldi3
440 DWtype
441 __ashldi3 (DWtype u, shift_count_type b)
443 if (b == 0)
444 return u;
446 const DWunion uu = {.ll = u};
447 const shift_count_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
448 DWunion w;
450 if (bm <= 0)
452 w.s.low = 0;
453 w.s.high = (UWtype) uu.s.low << -bm;
455 else
457 const UWtype carries = (UWtype) uu.s.low >> bm;
459 w.s.low = (UWtype) uu.s.low << b;
460 w.s.high = ((UWtype) uu.s.high << b) | carries;
463 return w.ll;
465 #endif
467 #ifdef L_ashrdi3
468 DWtype
469 __ashrdi3 (DWtype u, shift_count_type b)
471 if (b == 0)
472 return u;
474 const DWunion uu = {.ll = u};
475 const shift_count_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
476 DWunion w;
478 if (bm <= 0)
480 /* w.s.high = 1..1 or 0..0 */
481 w.s.high = uu.s.high >> (sizeof (Wtype) * BITS_PER_UNIT - 1);
482 w.s.low = uu.s.high >> -bm;
484 else
486 const UWtype carries = (UWtype) uu.s.high << bm;
488 w.s.high = uu.s.high >> b;
489 w.s.low = ((UWtype) uu.s.low >> b) | carries;
492 return w.ll;
494 #endif
496 #ifdef L_bswapsi2
497 SItype
498 __bswapsi2 (SItype u)
500 return ((((u) & 0xff000000) >> 24)
501 | (((u) & 0x00ff0000) >> 8)
502 | (((u) & 0x0000ff00) << 8)
503 | (((u) & 0x000000ff) << 24));
505 #endif
506 #ifdef L_bswapdi2
507 DItype
508 __bswapdi2 (DItype u)
510 return ((((u) & 0xff00000000000000ull) >> 56)
511 | (((u) & 0x00ff000000000000ull) >> 40)
512 | (((u) & 0x0000ff0000000000ull) >> 24)
513 | (((u) & 0x000000ff00000000ull) >> 8)
514 | (((u) & 0x00000000ff000000ull) << 8)
515 | (((u) & 0x0000000000ff0000ull) << 24)
516 | (((u) & 0x000000000000ff00ull) << 40)
517 | (((u) & 0x00000000000000ffull) << 56));
519 #endif
520 #ifdef L_ffssi2
521 #undef int
523 __ffsSI2 (UWtype u)
525 UWtype count;
527 if (u == 0)
528 return 0;
530 count_trailing_zeros (count, u);
531 return count + 1;
533 #endif
535 #ifdef L_ffsdi2
536 #undef int
538 __ffsDI2 (DWtype u)
540 const DWunion uu = {.ll = u};
541 UWtype word, count, add;
543 if (uu.s.low != 0)
544 word = uu.s.low, add = 0;
545 else if (uu.s.high != 0)
546 word = uu.s.high, add = BITS_PER_UNIT * sizeof (Wtype);
547 else
548 return 0;
550 count_trailing_zeros (count, word);
551 return count + add + 1;
553 #endif
555 #ifdef L_muldi3
556 DWtype
557 __muldi3 (DWtype u, DWtype v)
559 const DWunion uu = {.ll = u};
560 const DWunion vv = {.ll = v};
561 DWunion w = {.ll = __umulsidi3 (uu.s.low, vv.s.low)};
563 w.s.high += ((UWtype) uu.s.low * (UWtype) vv.s.high
564 + (UWtype) uu.s.high * (UWtype) vv.s.low);
566 return w.ll;
568 #endif
570 #if (defined (L_udivdi3) || defined (L_divdi3) || \
571 defined (L_umoddi3) || defined (L_moddi3))
572 #if defined (sdiv_qrnnd)
573 #define L_udiv_w_sdiv
574 #endif
575 #endif
577 #ifdef L_udiv_w_sdiv
578 #if defined (sdiv_qrnnd)
579 #if (defined (L_udivdi3) || defined (L_divdi3) || \
580 defined (L_umoddi3) || defined (L_moddi3))
581 static inline __attribute__ ((__always_inline__))
582 #endif
583 UWtype
584 __udiv_w_sdiv (UWtype *rp, UWtype a1, UWtype a0, UWtype d)
586 UWtype q, r;
587 UWtype c0, c1, b1;
589 if ((Wtype) d >= 0)
591 if (a1 < d - a1 - (a0 >> (W_TYPE_SIZE - 1)))
593 /* Dividend, divisor, and quotient are nonnegative. */
594 sdiv_qrnnd (q, r, a1, a0, d);
596 else
598 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d. */
599 sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (W_TYPE_SIZE - 1));
600 /* Divide (c1*2^32 + c0) by d. */
601 sdiv_qrnnd (q, r, c1, c0, d);
602 /* Add 2^31 to quotient. */
603 q += (UWtype) 1 << (W_TYPE_SIZE - 1);
606 else
608 b1 = d >> 1; /* d/2, between 2^30 and 2^31 - 1 */
609 c1 = a1 >> 1; /* A/2 */
610 c0 = (a1 << (W_TYPE_SIZE - 1)) + (a0 >> 1);
612 if (a1 < b1) /* A < 2^32*b1, so A/2 < 2^31*b1 */
614 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
616 r = 2*r + (a0 & 1); /* Remainder from A/(2*b1) */
617 if ((d & 1) != 0)
619 if (r >= q)
620 r = r - q;
621 else if (q - r <= d)
623 r = r - q + d;
624 q--;
626 else
628 r = r - q + 2*d;
629 q -= 2;
633 else if (c1 < b1) /* So 2^31 <= (A/2)/b1 < 2^32 */
635 c1 = (b1 - 1) - c1;
636 c0 = ~c0; /* logical NOT */
638 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
640 q = ~q; /* (A/2)/b1 */
641 r = (b1 - 1) - r;
643 r = 2*r + (a0 & 1); /* A/(2*b1) */
645 if ((d & 1) != 0)
647 if (r >= q)
648 r = r - q;
649 else if (q - r <= d)
651 r = r - q + d;
652 q--;
654 else
656 r = r - q + 2*d;
657 q -= 2;
661 else /* Implies c1 = b1 */
662 { /* Hence a1 = d - 1 = 2*b1 - 1 */
663 if (a0 >= -d)
665 q = -1;
666 r = a0 + d;
668 else
670 q = -2;
671 r = a0 + 2*d;
676 *rp = r;
677 return q;
679 #else
680 /* If sdiv_qrnnd doesn't exist, define dummy __udiv_w_sdiv. */
681 UWtype
682 __udiv_w_sdiv (UWtype *rp __attribute__ ((__unused__)),
683 UWtype a1 __attribute__ ((__unused__)),
684 UWtype a0 __attribute__ ((__unused__)),
685 UWtype d __attribute__ ((__unused__)))
687 return 0;
689 #endif
690 #endif
692 #if (defined (L_udivdi3) || defined (L_divdi3) || \
693 defined (L_umoddi3) || defined (L_moddi3))
694 #define L_udivmoddi4
695 #endif
697 #ifdef L_clz
698 const UQItype __clz_tab[256] =
700 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,
701 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,
702 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,
703 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,
704 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,
705 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,
706 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,
707 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
709 #endif
711 #ifdef L_clzsi2
712 #undef int
714 __clzSI2 (UWtype x)
716 Wtype ret;
718 count_leading_zeros (ret, x);
720 return ret;
722 #endif
724 #ifdef L_clzdi2
725 #undef int
727 __clzDI2 (UDWtype x)
729 const DWunion uu = {.ll = x};
730 UWtype word;
731 Wtype ret, add;
733 if (uu.s.high)
734 word = uu.s.high, add = 0;
735 else
736 word = uu.s.low, add = W_TYPE_SIZE;
738 count_leading_zeros (ret, word);
739 return ret + add;
741 #endif
743 #ifdef L_ctzsi2
744 #undef int
746 __ctzSI2 (UWtype x)
748 Wtype ret;
750 count_trailing_zeros (ret, x);
752 return ret;
754 #endif
756 #ifdef L_ctzdi2
757 #undef int
759 __ctzDI2 (UDWtype x)
761 const DWunion uu = {.ll = x};
762 UWtype word;
763 Wtype ret, add;
765 if (uu.s.low)
766 word = uu.s.low, add = 0;
767 else
768 word = uu.s.high, add = W_TYPE_SIZE;
770 count_trailing_zeros (ret, word);
771 return ret + add;
773 #endif
775 #ifdef L_popcount_tab
776 const UQItype __popcount_tab[256] =
778 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,
779 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,
780 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,
781 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,
782 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,
783 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,
784 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,
785 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
787 #endif
789 #ifdef L_popcountsi2
790 #undef int
792 __popcountSI2 (UWtype x)
794 int i, ret = 0;
796 for (i = 0; i < W_TYPE_SIZE; i += 8)
797 ret += __popcount_tab[(x >> i) & 0xff];
799 return ret;
801 #endif
803 #ifdef L_popcountdi2
804 #undef int
806 __popcountDI2 (UDWtype x)
808 int i, ret = 0;
810 for (i = 0; i < 2*W_TYPE_SIZE; i += 8)
811 ret += __popcount_tab[(x >> i) & 0xff];
813 return ret;
815 #endif
817 #ifdef L_paritysi2
818 #undef int
820 __paritySI2 (UWtype x)
822 #if W_TYPE_SIZE > 64
823 # error "fill out the table"
824 #endif
825 #if W_TYPE_SIZE > 32
826 x ^= x >> 32;
827 #endif
828 #if W_TYPE_SIZE > 16
829 x ^= x >> 16;
830 #endif
831 x ^= x >> 8;
832 x ^= x >> 4;
833 x &= 0xf;
834 return (0x6996 >> x) & 1;
836 #endif
838 #ifdef L_paritydi2
839 #undef int
841 __parityDI2 (UDWtype x)
843 const DWunion uu = {.ll = x};
844 UWtype nx = uu.s.low ^ uu.s.high;
846 #if W_TYPE_SIZE > 64
847 # error "fill out the table"
848 #endif
849 #if W_TYPE_SIZE > 32
850 nx ^= nx >> 32;
851 #endif
852 #if W_TYPE_SIZE > 16
853 nx ^= nx >> 16;
854 #endif
855 nx ^= nx >> 8;
856 nx ^= nx >> 4;
857 nx &= 0xf;
858 return (0x6996 >> nx) & 1;
860 #endif
862 #ifdef L_udivmoddi4
864 #if (defined (L_udivdi3) || defined (L_divdi3) || \
865 defined (L_umoddi3) || defined (L_moddi3))
866 static inline __attribute__ ((__always_inline__))
867 #endif
868 UDWtype
869 __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
871 const DWunion nn = {.ll = n};
872 const DWunion dd = {.ll = d};
873 DWunion rr;
874 UWtype d0, d1, n0, n1, n2;
875 UWtype q0, q1;
876 UWtype b, bm;
878 d0 = dd.s.low;
879 d1 = dd.s.high;
880 n0 = nn.s.low;
881 n1 = nn.s.high;
883 #if !UDIV_NEEDS_NORMALIZATION
884 if (d1 == 0)
886 if (d0 > n1)
888 /* 0q = nn / 0D */
890 udiv_qrnnd (q0, n0, n1, n0, d0);
891 q1 = 0;
893 /* Remainder in n0. */
895 else
897 /* qq = NN / 0d */
899 if (d0 == 0)
900 d0 = 1 / d0; /* Divide intentionally by zero. */
902 udiv_qrnnd (q1, n1, 0, n1, d0);
903 udiv_qrnnd (q0, n0, n1, n0, d0);
905 /* Remainder in n0. */
908 if (rp != 0)
910 rr.s.low = n0;
911 rr.s.high = 0;
912 *rp = rr.ll;
916 #else /* UDIV_NEEDS_NORMALIZATION */
918 if (d1 == 0)
920 if (d0 > n1)
922 /* 0q = nn / 0D */
924 count_leading_zeros (bm, d0);
926 if (bm != 0)
928 /* Normalize, i.e. make the most significant bit of the
929 denominator set. */
931 d0 = d0 << bm;
932 n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm));
933 n0 = n0 << bm;
936 udiv_qrnnd (q0, n0, n1, n0, d0);
937 q1 = 0;
939 /* Remainder in n0 >> bm. */
941 else
943 /* qq = NN / 0d */
945 if (d0 == 0)
946 d0 = 1 / d0; /* Divide intentionally by zero. */
948 count_leading_zeros (bm, d0);
950 if (bm == 0)
952 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
953 conclude (the most significant bit of n1 is set) /\ (the
954 leading quotient digit q1 = 1).
956 This special case is necessary, not an optimization.
957 (Shifts counts of W_TYPE_SIZE are undefined.) */
959 n1 -= d0;
960 q1 = 1;
962 else
964 /* Normalize. */
966 b = W_TYPE_SIZE - bm;
968 d0 = d0 << bm;
969 n2 = n1 >> b;
970 n1 = (n1 << bm) | (n0 >> b);
971 n0 = n0 << bm;
973 udiv_qrnnd (q1, n1, n2, n1, d0);
976 /* n1 != d0... */
978 udiv_qrnnd (q0, n0, n1, n0, d0);
980 /* Remainder in n0 >> bm. */
983 if (rp != 0)
985 rr.s.low = n0 >> bm;
986 rr.s.high = 0;
987 *rp = rr.ll;
990 #endif /* UDIV_NEEDS_NORMALIZATION */
992 else
994 if (d1 > n1)
996 /* 00 = nn / DD */
998 q0 = 0;
999 q1 = 0;
1001 /* Remainder in n1n0. */
1002 if (rp != 0)
1004 rr.s.low = n0;
1005 rr.s.high = n1;
1006 *rp = rr.ll;
1009 else
1011 /* 0q = NN / dd */
1013 count_leading_zeros (bm, d1);
1014 if (bm == 0)
1016 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
1017 conclude (the most significant bit of n1 is set) /\ (the
1018 quotient digit q0 = 0 or 1).
1020 This special case is necessary, not an optimization. */
1022 /* The condition on the next line takes advantage of that
1023 n1 >= d1 (true due to program flow). */
1024 if (n1 > d1 || n0 >= d0)
1026 q0 = 1;
1027 sub_ddmmss (n1, n0, n1, n0, d1, d0);
1029 else
1030 q0 = 0;
1032 q1 = 0;
1034 if (rp != 0)
1036 rr.s.low = n0;
1037 rr.s.high = n1;
1038 *rp = rr.ll;
1041 else
1043 UWtype m1, m0;
1044 /* Normalize. */
1046 b = W_TYPE_SIZE - bm;
1048 d1 = (d1 << bm) | (d0 >> b);
1049 d0 = d0 << bm;
1050 n2 = n1 >> b;
1051 n1 = (n1 << bm) | (n0 >> b);
1052 n0 = n0 << bm;
1054 udiv_qrnnd (q0, n1, n2, n1, d1);
1055 umul_ppmm (m1, m0, q0, d0);
1057 if (m1 > n1 || (m1 == n1 && m0 > n0))
1059 q0--;
1060 sub_ddmmss (m1, m0, m1, m0, d1, d0);
1063 q1 = 0;
1065 /* Remainder in (n1n0 - m1m0) >> bm. */
1066 if (rp != 0)
1068 sub_ddmmss (n1, n0, n1, n0, m1, m0);
1069 rr.s.low = (n1 << b) | (n0 >> bm);
1070 rr.s.high = n1 >> bm;
1071 *rp = rr.ll;
1077 const DWunion ww = {{.low = q0, .high = q1}};
1078 return ww.ll;
1080 #endif
1082 #ifdef L_divdi3
1083 DWtype
1084 __divdi3 (DWtype u, DWtype v)
1086 Wtype c = 0;
1087 DWunion uu = {.ll = u};
1088 DWunion vv = {.ll = v};
1089 DWtype w;
1091 if (uu.s.high < 0)
1092 c = ~c,
1093 uu.ll = -uu.ll;
1094 if (vv.s.high < 0)
1095 c = ~c,
1096 vv.ll = -vv.ll;
1098 w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0);
1099 if (c)
1100 w = -w;
1102 return w;
1104 #endif
1106 #ifdef L_moddi3
1107 DWtype
1108 __moddi3 (DWtype u, DWtype v)
1110 Wtype c = 0;
1111 DWunion uu = {.ll = u};
1112 DWunion vv = {.ll = v};
1113 DWtype w;
1115 if (uu.s.high < 0)
1116 c = ~c,
1117 uu.ll = -uu.ll;
1118 if (vv.s.high < 0)
1119 vv.ll = -vv.ll;
1121 (void) __udivmoddi4 (uu.ll, vv.ll, (UDWtype*)&w);
1122 if (c)
1123 w = -w;
1125 return w;
1127 #endif
1129 #ifdef L_umoddi3
1130 UDWtype
1131 __umoddi3 (UDWtype u, UDWtype v)
1133 UDWtype w;
1135 (void) __udivmoddi4 (u, v, &w);
1137 return w;
1139 #endif
1141 #ifdef L_udivdi3
1142 UDWtype
1143 __udivdi3 (UDWtype n, UDWtype d)
1145 return __udivmoddi4 (n, d, (UDWtype *) 0);
1147 #endif
1149 #ifdef L_cmpdi2
1150 cmp_return_type
1151 __cmpdi2 (DWtype a, DWtype b)
1153 const DWunion au = {.ll = a};
1154 const DWunion bu = {.ll = b};
1156 if (au.s.high < bu.s.high)
1157 return 0;
1158 else if (au.s.high > bu.s.high)
1159 return 2;
1160 if ((UWtype) au.s.low < (UWtype) bu.s.low)
1161 return 0;
1162 else if ((UWtype) au.s.low > (UWtype) bu.s.low)
1163 return 2;
1164 return 1;
1166 #endif
1168 #ifdef L_ucmpdi2
1169 cmp_return_type
1170 __ucmpdi2 (DWtype a, DWtype b)
1172 const DWunion au = {.ll = a};
1173 const DWunion bu = {.ll = b};
1175 if ((UWtype) au.s.high < (UWtype) bu.s.high)
1176 return 0;
1177 else if ((UWtype) au.s.high > (UWtype) bu.s.high)
1178 return 2;
1179 if ((UWtype) au.s.low < (UWtype) bu.s.low)
1180 return 0;
1181 else if ((UWtype) au.s.low > (UWtype) bu.s.low)
1182 return 2;
1183 return 1;
1185 #endif
1187 #if defined(L_fixunstfdi) && LIBGCC2_HAS_TF_MODE
1188 UDWtype
1189 __fixunstfDI (TFtype a)
1191 if (a < 0)
1192 return 0;
1194 /* Compute high word of result, as a flonum. */
1195 const TFtype b = (a / Wtype_MAXp1_F);
1196 /* Convert that to fixed (but not to DWtype!),
1197 and shift it into the high word. */
1198 UDWtype v = (UWtype) b;
1199 v <<= W_TYPE_SIZE;
1200 /* Remove high part from the TFtype, leaving the low part as flonum. */
1201 a -= (TFtype)v;
1202 /* Convert that to fixed (but not to DWtype!) and add it in.
1203 Sometimes A comes out negative. This is significant, since
1204 A has more bits than a long int does. */
1205 if (a < 0)
1206 v -= (UWtype) (- a);
1207 else
1208 v += (UWtype) a;
1209 return v;
1211 #endif
1213 #if defined(L_fixtfdi) && LIBGCC2_HAS_TF_MODE
1214 DWtype
1215 __fixtfdi (TFtype a)
1217 if (a < 0)
1218 return - __fixunstfDI (-a);
1219 return __fixunstfDI (a);
1221 #endif
1223 #if defined(L_fixunsxfdi) && LIBGCC2_HAS_XF_MODE
1224 UDWtype
1225 __fixunsxfDI (XFtype a)
1227 if (a < 0)
1228 return 0;
1230 /* Compute high word of result, as a flonum. */
1231 const XFtype b = (a / Wtype_MAXp1_F);
1232 /* Convert that to fixed (but not to DWtype!),
1233 and shift it into the high word. */
1234 UDWtype v = (UWtype) b;
1235 v <<= W_TYPE_SIZE;
1236 /* Remove high part from the XFtype, leaving the low part as flonum. */
1237 a -= (XFtype)v;
1238 /* Convert that to fixed (but not to DWtype!) and add it in.
1239 Sometimes A comes out negative. This is significant, since
1240 A has more bits than a long int does. */
1241 if (a < 0)
1242 v -= (UWtype) (- a);
1243 else
1244 v += (UWtype) a;
1245 return v;
1247 #endif
1249 #if defined(L_fixxfdi) && LIBGCC2_HAS_XF_MODE
1250 DWtype
1251 __fixxfdi (XFtype a)
1253 if (a < 0)
1254 return - __fixunsxfDI (-a);
1255 return __fixunsxfDI (a);
1257 #endif
1259 #if defined(L_fixunsdfdi) && LIBGCC2_HAS_DF_MODE
1260 UDWtype
1261 __fixunsdfDI (DFtype a)
1263 /* Get high part of result. The division here will just moves the radix
1264 point and will not cause any rounding. Then the conversion to integral
1265 type chops result as desired. */
1266 const UWtype hi = a / Wtype_MAXp1_F;
1268 /* Get low part of result. Convert `hi' to floating type and scale it back,
1269 then subtract this from the number being converted. This leaves the low
1270 part. Convert that to integral type. */
1271 const UWtype lo = a - (DFtype) hi * Wtype_MAXp1_F;
1273 /* Assemble result from the two parts. */
1274 return ((UDWtype) hi << W_TYPE_SIZE) | lo;
1276 #endif
1278 #if defined(L_fixdfdi) && LIBGCC2_HAS_DF_MODE
1279 DWtype
1280 __fixdfdi (DFtype a)
1282 if (a < 0)
1283 return - __fixunsdfDI (-a);
1284 return __fixunsdfDI (a);
1286 #endif
1288 #if defined(L_fixunssfdi) && LIBGCC2_HAS_SF_MODE
1289 UDWtype
1290 __fixunssfDI (SFtype a)
1292 #if LIBGCC2_HAS_DF_MODE
1293 /* Convert the SFtype to a DFtype, because that is surely not going
1294 to lose any bits. Some day someone else can write a faster version
1295 that avoids converting to DFtype, and verify it really works right. */
1296 const DFtype dfa = a;
1298 /* Get high part of result. The division here will just moves the radix
1299 point and will not cause any rounding. Then the conversion to integral
1300 type chops result as desired. */
1301 const UWtype hi = dfa / Wtype_MAXp1_F;
1303 /* Get low part of result. Convert `hi' to floating type and scale it back,
1304 then subtract this from the number being converted. This leaves the low
1305 part. Convert that to integral type. */
1306 const UWtype lo = dfa - (DFtype) hi * Wtype_MAXp1_F;
1308 /* Assemble result from the two parts. */
1309 return ((UDWtype) hi << W_TYPE_SIZE) | lo;
1310 #elif FLT_MANT_DIG < W_TYPE_SIZE
1311 if (a < 1)
1312 return 0;
1313 if (a < Wtype_MAXp1_F)
1314 return (UWtype)a;
1315 if (a < Wtype_MAXp1_F * Wtype_MAXp1_F)
1317 /* Since we know that there are fewer significant bits in the SFmode
1318 quantity than in a word, we know that we can convert out all the
1319 significant bits in one step, and thus avoid losing bits. */
1321 /* ??? This following loop essentially performs frexpf. If we could
1322 use the real libm function, or poke at the actual bits of the fp
1323 format, it would be significantly faster. */
1325 UWtype shift = 0, counter;
1326 SFtype msb;
1328 a /= Wtype_MAXp1_F;
1329 for (counter = W_TYPE_SIZE / 2; counter != 0; counter >>= 1)
1331 SFtype counterf = (UWtype)1 << counter;
1332 if (a >= counterf)
1334 shift |= counter;
1335 a /= counterf;
1339 /* Rescale into the range of one word, extract the bits of that
1340 one word, and shift the result into position. */
1341 a *= Wtype_MAXp1_F;
1342 counter = a;
1343 return (DWtype)counter << shift;
1345 return -1;
1346 #else
1347 # error
1348 #endif
1350 #endif
1352 #if defined(L_fixsfdi) && LIBGCC2_HAS_SF_MODE
1353 DWtype
1354 __fixsfdi (SFtype a)
1356 if (a < 0)
1357 return - __fixunssfDI (-a);
1358 return __fixunssfDI (a);
1360 #endif
1362 #if defined(L_floatdixf) && LIBGCC2_HAS_XF_MODE
1363 XFtype
1364 __floatdixf (DWtype u)
1366 #if W_TYPE_SIZE > XF_SIZE
1367 # error
1368 #endif
1369 XFtype d = (Wtype) (u >> W_TYPE_SIZE);
1370 d *= Wtype_MAXp1_F;
1371 d += (UWtype)u;
1372 return d;
1374 #endif
1376 #if defined(L_floatundixf) && LIBGCC2_HAS_XF_MODE
1377 XFtype
1378 __floatundixf (UDWtype u)
1380 #if W_TYPE_SIZE > XF_SIZE
1381 # error
1382 #endif
1383 XFtype d = (UWtype) (u >> W_TYPE_SIZE);
1384 d *= Wtype_MAXp1_F;
1385 d += (UWtype)u;
1386 return d;
1388 #endif
1390 #if defined(L_floatditf) && LIBGCC2_HAS_TF_MODE
1391 TFtype
1392 __floatditf (DWtype u)
1394 #if W_TYPE_SIZE > TF_SIZE
1395 # error
1396 #endif
1397 TFtype d = (Wtype) (u >> W_TYPE_SIZE);
1398 d *= Wtype_MAXp1_F;
1399 d += (UWtype)u;
1400 return d;
1402 #endif
1404 #if defined(L_floatunditf) && LIBGCC2_HAS_TF_MODE
1405 TFtype
1406 __floatunditf (UDWtype u)
1408 #if W_TYPE_SIZE > TF_SIZE
1409 # error
1410 #endif
1411 TFtype d = (UWtype) (u >> W_TYPE_SIZE);
1412 d *= Wtype_MAXp1_F;
1413 d += (UWtype)u;
1414 return d;
1416 #endif
1418 #if (defined(L_floatdisf) && LIBGCC2_HAS_SF_MODE) \
1419 || (defined(L_floatdidf) && LIBGCC2_HAS_DF_MODE)
1420 #define DI_SIZE (W_TYPE_SIZE * 2)
1421 #define F_MODE_OK(SIZE) \
1422 (SIZE < DI_SIZE \
1423 && SIZE > (DI_SIZE - SIZE + FSSIZE) \
1424 && !AVOID_FP_TYPE_CONVERSION(SIZE))
1425 #if defined(L_floatdisf)
1426 #define FUNC __floatdisf
1427 #define FSTYPE SFtype
1428 #define FSSIZE SF_SIZE
1429 #else
1430 #define FUNC __floatdidf
1431 #define FSTYPE DFtype
1432 #define FSSIZE DF_SIZE
1433 #endif
1435 FSTYPE
1436 FUNC (DWtype u)
1438 #if FSSIZE >= W_TYPE_SIZE
1439 /* When the word size is small, we never get any rounding error. */
1440 FSTYPE f = (Wtype) (u >> W_TYPE_SIZE);
1441 f *= Wtype_MAXp1_F;
1442 f += (UWtype)u;
1443 return f;
1444 #elif (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE)) \
1445 || (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE)) \
1446 || (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1448 #if (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE))
1449 # define FSIZE DF_SIZE
1450 # define FTYPE DFtype
1451 #elif (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE))
1452 # define FSIZE XF_SIZE
1453 # define FTYPE XFtype
1454 #elif (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1455 # define FSIZE TF_SIZE
1456 # define FTYPE TFtype
1457 #else
1458 # error
1459 #endif
1461 #define REP_BIT ((UDWtype) 1 << (DI_SIZE - FSIZE))
1463 /* Protect against double-rounding error.
1464 Represent any low-order bits, that might be truncated by a bit that
1465 won't be lost. The bit can go in anywhere below the rounding position
1466 of the FSTYPE. A fixed mask and bit position handles all usual
1467 configurations. */
1468 if (! (- ((DWtype) 1 << FSIZE) < u
1469 && u < ((DWtype) 1 << FSIZE)))
1471 if ((UDWtype) u & (REP_BIT - 1))
1473 u &= ~ (REP_BIT - 1);
1474 u |= REP_BIT;
1478 /* Do the calculation in a wider type so that we don't lose any of
1479 the precision of the high word while multiplying it. */
1480 FTYPE f = (Wtype) (u >> W_TYPE_SIZE);
1481 f *= Wtype_MAXp1_F;
1482 f += (UWtype)u;
1483 return (FSTYPE) f;
1484 #else
1485 #if FSSIZE >= W_TYPE_SIZE - 2
1486 # error
1487 #endif
1488 /* Finally, the word size is larger than the number of bits in the
1489 required FSTYPE, and we've got no suitable wider type. The only
1490 way to avoid double rounding is to special case the
1491 extraction. */
1493 /* If there are no high bits set, fall back to one conversion. */
1494 if ((Wtype)u == u)
1495 return (FSTYPE)(Wtype)u;
1497 /* Otherwise, find the power of two. */
1498 Wtype hi = u >> W_TYPE_SIZE;
1499 if (hi < 0)
1500 hi = -hi;
1502 UWtype count, shift;
1503 count_leading_zeros (count, hi);
1505 /* No leading bits means u == minimum. */
1506 if (count == 0)
1507 return -(Wtype_MAXp1_F * (Wtype_MAXp1_F / 2));
1509 shift = 1 + W_TYPE_SIZE - count;
1511 /* Shift down the most significant bits. */
1512 hi = u >> shift;
1514 /* If we lost any nonzero bits, set the lsb to ensure correct rounding. */
1515 if ((UWtype)u << (W_TYPE_SIZE - shift))
1516 hi |= 1;
1518 /* Convert the one word of data, and rescale. */
1519 FSTYPE f = hi, e;
1520 if (shift == W_TYPE_SIZE)
1521 e = Wtype_MAXp1_F;
1522 /* The following two cases could be merged if we knew that the target
1523 supported a native unsigned->float conversion. More often, we only
1524 have a signed conversion, and have to add extra fixup code. */
1525 else if (shift == W_TYPE_SIZE - 1)
1526 e = Wtype_MAXp1_F / 2;
1527 else
1528 e = (Wtype)1 << shift;
1529 return f * e;
1530 #endif
1532 #endif
1534 #if (defined(L_floatundisf) && LIBGCC2_HAS_SF_MODE) \
1535 || (defined(L_floatundidf) && LIBGCC2_HAS_DF_MODE)
1536 #define DI_SIZE (W_TYPE_SIZE * 2)
1537 #define F_MODE_OK(SIZE) \
1538 (SIZE < DI_SIZE \
1539 && SIZE > (DI_SIZE - SIZE + FSSIZE) \
1540 && !AVOID_FP_TYPE_CONVERSION(SIZE))
1541 #if defined(L_floatundisf)
1542 #define FUNC __floatundisf
1543 #define FSTYPE SFtype
1544 #define FSSIZE SF_SIZE
1545 #else
1546 #define FUNC __floatundidf
1547 #define FSTYPE DFtype
1548 #define FSSIZE DF_SIZE
1549 #endif
1551 FSTYPE
1552 FUNC (UDWtype u)
1554 #if FSSIZE >= W_TYPE_SIZE
1555 /* When the word size is small, we never get any rounding error. */
1556 FSTYPE f = (UWtype) (u >> W_TYPE_SIZE);
1557 f *= Wtype_MAXp1_F;
1558 f += (UWtype)u;
1559 return f;
1560 #elif (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE)) \
1561 || (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE)) \
1562 || (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1564 #if (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE))
1565 # define FSIZE DF_SIZE
1566 # define FTYPE DFtype
1567 #elif (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE))
1568 # define FSIZE XF_SIZE
1569 # define FTYPE XFtype
1570 #elif (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1571 # define FSIZE TF_SIZE
1572 # define FTYPE TFtype
1573 #else
1574 # error
1575 #endif
1577 #define REP_BIT ((UDWtype) 1 << (DI_SIZE - FSIZE))
1579 /* Protect against double-rounding error.
1580 Represent any low-order bits, that might be truncated by a bit that
1581 won't be lost. The bit can go in anywhere below the rounding position
1582 of the FSTYPE. A fixed mask and bit position handles all usual
1583 configurations. */
1584 if (u >= ((UDWtype) 1 << FSIZE))
1586 if ((UDWtype) u & (REP_BIT - 1))
1588 u &= ~ (REP_BIT - 1);
1589 u |= REP_BIT;
1593 /* Do the calculation in a wider type so that we don't lose any of
1594 the precision of the high word while multiplying it. */
1595 FTYPE f = (UWtype) (u >> W_TYPE_SIZE);
1596 f *= Wtype_MAXp1_F;
1597 f += (UWtype)u;
1598 return (FSTYPE) f;
1599 #else
1600 #if FSSIZE == W_TYPE_SIZE - 1
1601 # error
1602 #endif
1603 /* Finally, the word size is larger than the number of bits in the
1604 required FSTYPE, and we've got no suitable wider type. The only
1605 way to avoid double rounding is to special case the
1606 extraction. */
1608 /* If there are no high bits set, fall back to one conversion. */
1609 if ((UWtype)u == u)
1610 return (FSTYPE)(UWtype)u;
1612 /* Otherwise, find the power of two. */
1613 UWtype hi = u >> W_TYPE_SIZE;
1615 UWtype count, shift;
1616 count_leading_zeros (count, hi);
1618 shift = W_TYPE_SIZE - count;
1620 /* Shift down the most significant bits. */
1621 hi = u >> shift;
1623 /* If we lost any nonzero bits, set the lsb to ensure correct rounding. */
1624 if ((UWtype)u << (W_TYPE_SIZE - shift))
1625 hi |= 1;
1627 /* Convert the one word of data, and rescale. */
1628 FSTYPE f = hi, e;
1629 if (shift == W_TYPE_SIZE)
1630 e = Wtype_MAXp1_F;
1631 /* The following two cases could be merged if we knew that the target
1632 supported a native unsigned->float conversion. More often, we only
1633 have a signed conversion, and have to add extra fixup code. */
1634 else if (shift == W_TYPE_SIZE - 1)
1635 e = Wtype_MAXp1_F / 2;
1636 else
1637 e = (Wtype)1 << shift;
1638 return f * e;
1639 #endif
1641 #endif
1643 #if defined(L_fixunsxfsi) && LIBGCC2_HAS_XF_MODE
1644 /* Reenable the normal types, in case limits.h needs them. */
1645 #undef char
1646 #undef short
1647 #undef int
1648 #undef long
1649 #undef unsigned
1650 #undef float
1651 #undef double
1652 #undef MIN
1653 #undef MAX
1654 #include <limits.h>
1656 UWtype
1657 __fixunsxfSI (XFtype a)
1659 if (a >= - (DFtype) Wtype_MIN)
1660 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1661 return (Wtype) a;
1663 #endif
1665 #if defined(L_fixunsdfsi) && LIBGCC2_HAS_DF_MODE
1666 /* Reenable the normal types, in case limits.h needs them. */
1667 #undef char
1668 #undef short
1669 #undef int
1670 #undef long
1671 #undef unsigned
1672 #undef float
1673 #undef double
1674 #undef MIN
1675 #undef MAX
1676 #include <limits.h>
1678 UWtype
1679 __fixunsdfSI (DFtype a)
1681 if (a >= - (DFtype) Wtype_MIN)
1682 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1683 return (Wtype) a;
1685 #endif
1687 #if defined(L_fixunssfsi) && LIBGCC2_HAS_SF_MODE
1688 /* Reenable the normal types, in case limits.h needs them. */
1689 #undef char
1690 #undef short
1691 #undef int
1692 #undef long
1693 #undef unsigned
1694 #undef float
1695 #undef double
1696 #undef MIN
1697 #undef MAX
1698 #include <limits.h>
1700 UWtype
1701 __fixunssfSI (SFtype a)
1703 if (a >= - (SFtype) Wtype_MIN)
1704 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1705 return (Wtype) a;
1707 #endif
1709 /* Integer power helper used from __builtin_powi for non-constant
1710 exponents. */
1712 #if (defined(L_powisf2) && LIBGCC2_HAS_SF_MODE) \
1713 || (defined(L_powidf2) && LIBGCC2_HAS_DF_MODE) \
1714 || (defined(L_powixf2) && LIBGCC2_HAS_XF_MODE) \
1715 || (defined(L_powitf2) && LIBGCC2_HAS_TF_MODE)
1716 # if defined(L_powisf2)
1717 # define TYPE SFtype
1718 # define NAME __powisf2
1719 # elif defined(L_powidf2)
1720 # define TYPE DFtype
1721 # define NAME __powidf2
1722 # elif defined(L_powixf2)
1723 # define TYPE XFtype
1724 # define NAME __powixf2
1725 # elif defined(L_powitf2)
1726 # define TYPE TFtype
1727 # define NAME __powitf2
1728 # endif
1730 #undef int
1731 #undef unsigned
1732 TYPE
1733 NAME (TYPE x, int m)
1735 unsigned int n = m < 0 ? -m : m;
1736 TYPE y = n % 2 ? x : 1;
1737 while (n >>= 1)
1739 x = x * x;
1740 if (n % 2)
1741 y = y * x;
1743 return m < 0 ? 1/y : y;
1746 #endif
1748 #if ((defined(L_mulsc3) || defined(L_divsc3)) && LIBGCC2_HAS_SF_MODE) \
1749 || ((defined(L_muldc3) || defined(L_divdc3)) && LIBGCC2_HAS_DF_MODE) \
1750 || ((defined(L_mulxc3) || defined(L_divxc3)) && LIBGCC2_HAS_XF_MODE) \
1751 || ((defined(L_multc3) || defined(L_divtc3)) && LIBGCC2_HAS_TF_MODE)
1753 #undef float
1754 #undef double
1755 #undef long
1757 #if defined(L_mulsc3) || defined(L_divsc3)
1758 # define MTYPE SFtype
1759 # define CTYPE SCtype
1760 # define MODE sc
1761 # define CEXT f
1762 # define NOTRUNC __FLT_EVAL_METHOD__ == 0
1763 #elif defined(L_muldc3) || defined(L_divdc3)
1764 # define MTYPE DFtype
1765 # define CTYPE DCtype
1766 # define MODE dc
1767 # if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 64
1768 # define CEXT l
1769 # define NOTRUNC 1
1770 # else
1771 # define CEXT
1772 # define NOTRUNC __FLT_EVAL_METHOD__ == 0 || __FLT_EVAL_METHOD__ == 1
1773 # endif
1774 #elif defined(L_mulxc3) || defined(L_divxc3)
1775 # define MTYPE XFtype
1776 # define CTYPE XCtype
1777 # define MODE xc
1778 # define CEXT l
1779 # define NOTRUNC 1
1780 #elif defined(L_multc3) || defined(L_divtc3)
1781 # define MTYPE TFtype
1782 # define CTYPE TCtype
1783 # define MODE tc
1784 # if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128
1785 # define CEXT l
1786 # else
1787 # define CEXT LIBGCC2_TF_CEXT
1788 # endif
1789 # define NOTRUNC 1
1790 #else
1791 # error
1792 #endif
1794 #define CONCAT3(A,B,C) _CONCAT3(A,B,C)
1795 #define _CONCAT3(A,B,C) A##B##C
1797 #define CONCAT2(A,B) _CONCAT2(A,B)
1798 #define _CONCAT2(A,B) A##B
1800 /* All of these would be present in a full C99 implementation of <math.h>
1801 and <complex.h>. Our problem is that only a few systems have such full
1802 implementations. Further, libgcc_s.so isn't currently linked against
1803 libm.so, and even for systems that do provide full C99, the extra overhead
1804 of all programs using libgcc having to link against libm. So avoid it. */
1806 #define isnan(x) __builtin_expect ((x) != (x), 0)
1807 #define isfinite(x) __builtin_expect (!isnan((x) - (x)), 1)
1808 #define isinf(x) __builtin_expect (!isnan(x) & !isfinite(x), 0)
1810 #define INFINITY CONCAT2(__builtin_inf, CEXT) ()
1811 #define I 1i
1813 /* Helpers to make the following code slightly less gross. */
1814 #define COPYSIGN CONCAT2(__builtin_copysign, CEXT)
1815 #define FABS CONCAT2(__builtin_fabs, CEXT)
1817 /* Verify that MTYPE matches up with CEXT. */
1818 extern void *compile_type_assert[sizeof(INFINITY) == sizeof(MTYPE) ? 1 : -1];
1820 /* Ensure that we've lost any extra precision. */
1821 #if NOTRUNC
1822 # define TRUNC(x)
1823 #else
1824 # define TRUNC(x) __asm__ ("" : "=m"(x) : "m"(x))
1825 #endif
1827 #if defined(L_mulsc3) || defined(L_muldc3) \
1828 || defined(L_mulxc3) || defined(L_multc3)
1830 CTYPE
1831 CONCAT3(__mul,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
1833 MTYPE ac, bd, ad, bc, x, y;
1834 CTYPE res;
1836 ac = a * c;
1837 bd = b * d;
1838 ad = a * d;
1839 bc = b * c;
1841 TRUNC (ac);
1842 TRUNC (bd);
1843 TRUNC (ad);
1844 TRUNC (bc);
1846 x = ac - bd;
1847 y = ad + bc;
1849 if (isnan (x) && isnan (y))
1851 /* Recover infinities that computed as NaN + iNaN. */
1852 _Bool recalc = 0;
1853 if (isinf (a) || isinf (b))
1855 /* z is infinite. "Box" the infinity and change NaNs in
1856 the other factor to 0. */
1857 a = COPYSIGN (isinf (a) ? 1 : 0, a);
1858 b = COPYSIGN (isinf (b) ? 1 : 0, b);
1859 if (isnan (c)) c = COPYSIGN (0, c);
1860 if (isnan (d)) d = COPYSIGN (0, d);
1861 recalc = 1;
1863 if (isinf (c) || isinf (d))
1865 /* w is infinite. "Box" the infinity and change NaNs in
1866 the other factor to 0. */
1867 c = COPYSIGN (isinf (c) ? 1 : 0, c);
1868 d = COPYSIGN (isinf (d) ? 1 : 0, d);
1869 if (isnan (a)) a = COPYSIGN (0, a);
1870 if (isnan (b)) b = COPYSIGN (0, b);
1871 recalc = 1;
1873 if (!recalc
1874 && (isinf (ac) || isinf (bd)
1875 || isinf (ad) || isinf (bc)))
1877 /* Recover infinities from overflow by changing NaNs to 0. */
1878 if (isnan (a)) a = COPYSIGN (0, a);
1879 if (isnan (b)) b = COPYSIGN (0, b);
1880 if (isnan (c)) c = COPYSIGN (0, c);
1881 if (isnan (d)) d = COPYSIGN (0, d);
1882 recalc = 1;
1884 if (recalc)
1886 x = INFINITY * (a * c - b * d);
1887 y = INFINITY * (a * d + b * c);
1891 __real__ res = x;
1892 __imag__ res = y;
1893 return res;
1895 #endif /* complex multiply */
1897 #if defined(L_divsc3) || defined(L_divdc3) \
1898 || defined(L_divxc3) || defined(L_divtc3)
1900 CTYPE
1901 CONCAT3(__div,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
1903 MTYPE denom, ratio, x, y;
1904 CTYPE res;
1906 /* ??? We can get better behavior from logarithmic scaling instead of
1907 the division. But that would mean starting to link libgcc against
1908 libm. We could implement something akin to ldexp/frexp as gcc builtins
1909 fairly easily... */
1910 if (FABS (c) < FABS (d))
1912 ratio = c / d;
1913 denom = (c * ratio) + d;
1914 x = ((a * ratio) + b) / denom;
1915 y = ((b * ratio) - a) / denom;
1917 else
1919 ratio = d / c;
1920 denom = (d * ratio) + c;
1921 x = ((b * ratio) + a) / denom;
1922 y = (b - (a * ratio)) / denom;
1925 /* Recover infinities and zeros that computed as NaN+iNaN; the only cases
1926 are nonzero/zero, infinite/finite, and finite/infinite. */
1927 if (isnan (x) && isnan (y))
1929 if (c == 0.0 && d == 0.0 && (!isnan (a) || !isnan (b)))
1931 x = COPYSIGN (INFINITY, c) * a;
1932 y = COPYSIGN (INFINITY, c) * b;
1934 else if ((isinf (a) || isinf (b)) && isfinite (c) && isfinite (d))
1936 a = COPYSIGN (isinf (a) ? 1 : 0, a);
1937 b = COPYSIGN (isinf (b) ? 1 : 0, b);
1938 x = INFINITY * (a * c + b * d);
1939 y = INFINITY * (b * c - a * d);
1941 else if ((isinf (c) || isinf (d)) && isfinite (a) && isfinite (b))
1943 c = COPYSIGN (isinf (c) ? 1 : 0, c);
1944 d = COPYSIGN (isinf (d) ? 1 : 0, d);
1945 x = 0.0 * (a * c + b * d);
1946 y = 0.0 * (b * c - a * d);
1950 __real__ res = x;
1951 __imag__ res = y;
1952 return res;
1954 #endif /* complex divide */
1956 #endif /* all complex float routines */
1958 /* From here on down, the routines use normal data types. */
1960 #define SItype bogus_type
1961 #define USItype bogus_type
1962 #define DItype bogus_type
1963 #define UDItype bogus_type
1964 #define SFtype bogus_type
1965 #define DFtype bogus_type
1966 #undef Wtype
1967 #undef UWtype
1968 #undef HWtype
1969 #undef UHWtype
1970 #undef DWtype
1971 #undef UDWtype
1973 #undef char
1974 #undef short
1975 #undef int
1976 #undef long
1977 #undef unsigned
1978 #undef float
1979 #undef double
1981 #ifdef L__gcc_bcmp
1983 /* Like bcmp except the sign is meaningful.
1984 Result is negative if S1 is less than S2,
1985 positive if S1 is greater, 0 if S1 and S2 are equal. */
1988 __gcc_bcmp (const unsigned char *s1, const unsigned char *s2, size_t size)
1990 while (size > 0)
1992 const unsigned char c1 = *s1++, c2 = *s2++;
1993 if (c1 != c2)
1994 return c1 - c2;
1995 size--;
1997 return 0;
2000 #endif
2002 /* __eprintf used to be used by GCC's private version of <assert.h>.
2003 We no longer provide that header, but this routine remains in libgcc.a
2004 for binary backward compatibility. Note that it is not included in
2005 the shared version of libgcc. */
2006 #ifdef L_eprintf
2007 #ifndef inhibit_libc
2009 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
2010 #include <stdio.h>
2012 void
2013 __eprintf (const char *string, const char *expression,
2014 unsigned int line, const char *filename)
2016 fprintf (stderr, string, expression, line, filename);
2017 fflush (stderr);
2018 abort ();
2021 #endif
2022 #endif
2025 #ifdef L_clear_cache
2026 /* Clear part of an instruction cache. */
2028 void
2029 __clear_cache (char *beg __attribute__((__unused__)),
2030 char *end __attribute__((__unused__)))
2032 #ifdef CLEAR_INSN_CACHE
2033 CLEAR_INSN_CACHE (beg, end);
2034 #endif /* CLEAR_INSN_CACHE */
2037 #endif /* L_clear_cache */
2039 #ifdef L_enable_execute_stack
2040 /* Attempt to turn on execute permission for the stack. */
2042 #ifdef ENABLE_EXECUTE_STACK
2043 ENABLE_EXECUTE_STACK
2044 #else
2045 void
2046 __enable_execute_stack (void *addr __attribute__((__unused__)))
2048 #endif /* ENABLE_EXECUTE_STACK */
2050 #endif /* L_enable_execute_stack */
2052 #ifdef L_trampoline
2054 /* Jump to a trampoline, loading the static chain address. */
2056 #if defined(WINNT) && ! defined(__CYGWIN__)
2059 getpagesize (void)
2061 #ifdef _ALPHA_
2062 return 8192;
2063 #else
2064 return 4096;
2065 #endif
2069 mprotect (char *addr, int len, int prot)
2071 DWORD np, op;
2073 if (prot == 7)
2074 np = 0x40;
2075 else if (prot == 5)
2076 np = 0x20;
2077 else if (prot == 4)
2078 np = 0x10;
2079 else if (prot == 3)
2080 np = 0x04;
2081 else if (prot == 1)
2082 np = 0x02;
2083 else if (prot == 0)
2084 np = 0x01;
2085 else
2086 return -1;
2088 if (VirtualProtect (addr, len, np, &op))
2089 return 0;
2090 else
2091 return -1;
2094 #endif /* WINNT && ! __CYGWIN__ */
2096 #ifdef TRANSFER_FROM_TRAMPOLINE
2097 TRANSFER_FROM_TRAMPOLINE
2098 #endif
2099 #endif /* L_trampoline */
2101 #ifndef __CYGWIN__
2102 #ifdef L__main
2104 #include "gbl-ctors.h"
2106 /* Some systems use __main in a way incompatible with its use in gcc, in these
2107 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
2108 give the same symbol without quotes for an alternative entry point. You
2109 must define both, or neither. */
2110 #ifndef NAME__MAIN
2111 #define NAME__MAIN "__main"
2112 #define SYMBOL__MAIN __main
2113 #endif
2115 #if defined (INIT_SECTION_ASM_OP) || defined (INIT_ARRAY_SECTION_ASM_OP)
2116 #undef HAS_INIT_SECTION
2117 #define HAS_INIT_SECTION
2118 #endif
2120 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
2122 /* Some ELF crosses use crtstuff.c to provide __CTOR_LIST__, but use this
2123 code to run constructors. In that case, we need to handle EH here, too. */
2125 #ifdef EH_FRAME_SECTION_NAME
2126 #include "unwind-dw2-fde.h"
2127 extern unsigned char __EH_FRAME_BEGIN__[];
2128 #endif
2130 /* Run all the global destructors on exit from the program. */
2132 void
2133 __do_global_dtors (void)
2135 #ifdef DO_GLOBAL_DTORS_BODY
2136 DO_GLOBAL_DTORS_BODY;
2137 #else
2138 static func_ptr *p = __DTOR_LIST__ + 1;
2139 while (*p)
2141 p++;
2142 (*(p-1)) ();
2144 #endif
2145 #if defined (EH_FRAME_SECTION_NAME) && !defined (HAS_INIT_SECTION)
2147 static int completed = 0;
2148 if (! completed)
2150 completed = 1;
2151 __deregister_frame_info (__EH_FRAME_BEGIN__);
2154 #endif
2156 #endif
2158 #ifndef HAS_INIT_SECTION
2159 /* Run all the global constructors on entry to the program. */
2161 void
2162 __do_global_ctors (void)
2164 #ifdef EH_FRAME_SECTION_NAME
2166 static struct object object;
2167 __register_frame_info (__EH_FRAME_BEGIN__, &object);
2169 #endif
2170 DO_GLOBAL_CTORS_BODY;
2171 atexit (__do_global_dtors);
2173 #endif /* no HAS_INIT_SECTION */
2175 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
2176 /* Subroutine called automatically by `main'.
2177 Compiling a global function named `main'
2178 produces an automatic call to this function at the beginning.
2180 For many systems, this routine calls __do_global_ctors.
2181 For systems which support a .init section we use the .init section
2182 to run __do_global_ctors, so we need not do anything here. */
2184 extern void SYMBOL__MAIN (void);
2185 void
2186 SYMBOL__MAIN (void)
2188 /* Support recursive calls to `main': run initializers just once. */
2189 static int initialized;
2190 if (! initialized)
2192 initialized = 1;
2193 __do_global_ctors ();
2196 #endif /* no HAS_INIT_SECTION or INVOKE__main */
2198 #endif /* L__main */
2199 #endif /* __CYGWIN__ */
2201 #ifdef L_ctors
2203 #include "gbl-ctors.h"
2205 /* Provide default definitions for the lists of constructors and
2206 destructors, so that we don't get linker errors. These symbols are
2207 intentionally bss symbols, so that gld and/or collect will provide
2208 the right values. */
2210 /* We declare the lists here with two elements each,
2211 so that they are valid empty lists if no other definition is loaded.
2213 If we are using the old "set" extensions to have the gnu linker
2214 collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
2215 must be in the bss/common section.
2217 Long term no port should use those extensions. But many still do. */
2218 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
2219 #if defined (TARGET_ASM_CONSTRUCTOR) || defined (USE_COLLECT2)
2220 func_ptr __CTOR_LIST__[2] = {0, 0};
2221 func_ptr __DTOR_LIST__[2] = {0, 0};
2222 #else
2223 func_ptr __CTOR_LIST__[2];
2224 func_ptr __DTOR_LIST__[2];
2225 #endif
2226 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
2227 #endif /* L_ctors */
2228 #endif /* LIBGCC2_UNITS_PER_WORD <= MIN_UNITS_PER_WORD */