Enable cltd for modern CPUs
[official-gcc.git] / libgcc / libgcc2.c
blobb672bcbdd415c1808a800ca4d71d74bc67523175
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, 2009, 2010, 2011
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 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
28 #include "tconfig.h"
29 #include "tsystem.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "libgcc_tm.h"
34 #ifdef HAVE_GAS_HIDDEN
35 #define ATTRIBUTE_HIDDEN __attribute__ ((__visibility__ ("hidden")))
36 #else
37 #define ATTRIBUTE_HIDDEN
38 #endif
40 /* Work out the largest "word" size that we can deal with on this target. */
41 #if MIN_UNITS_PER_WORD > 4
42 # define LIBGCC2_MAX_UNITS_PER_WORD 8
43 #elif (MIN_UNITS_PER_WORD > 2 \
44 || (MIN_UNITS_PER_WORD > 1 && __SIZEOF_LONG_LONG__ > 4))
45 # define LIBGCC2_MAX_UNITS_PER_WORD 4
46 #else
47 # define LIBGCC2_MAX_UNITS_PER_WORD MIN_UNITS_PER_WORD
48 #endif
50 /* Work out what word size we are using for this compilation.
51 The value can be set on the command line. */
52 #ifndef LIBGCC2_UNITS_PER_WORD
53 #define LIBGCC2_UNITS_PER_WORD LIBGCC2_MAX_UNITS_PER_WORD
54 #endif
56 #if LIBGCC2_UNITS_PER_WORD <= LIBGCC2_MAX_UNITS_PER_WORD
58 #include "libgcc2.h"
60 #ifdef DECLARE_LIBRARY_RENAMES
61 DECLARE_LIBRARY_RENAMES
62 #endif
64 #if defined (L_negdi2)
65 DWtype
66 __negdi2 (DWtype u)
68 const DWunion uu = {.ll = u};
69 const DWunion w = { {.low = -uu.s.low,
70 .high = -uu.s.high - ((UWtype) -uu.s.low > 0) } };
72 return w.ll;
74 #endif
76 #ifdef L_addvsi3
77 Wtype
78 __addvSI3 (Wtype a, Wtype b)
80 const Wtype w = (UWtype) a + (UWtype) b;
82 if (b >= 0 ? w < a : w > a)
83 abort ();
85 return w;
87 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
88 SItype
89 __addvsi3 (SItype a, SItype b)
91 const SItype w = (USItype) a + (USItype) b;
93 if (b >= 0 ? w < a : w > a)
94 abort ();
96 return w;
98 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
99 #endif
101 #ifdef L_addvdi3
102 DWtype
103 __addvDI3 (DWtype a, DWtype b)
105 const DWtype w = (UDWtype) a + (UDWtype) b;
107 if (b >= 0 ? w < a : w > a)
108 abort ();
110 return w;
112 #endif
114 #ifdef L_subvsi3
115 Wtype
116 __subvSI3 (Wtype a, Wtype b)
118 const Wtype w = (UWtype) a - (UWtype) b;
120 if (b >= 0 ? w > a : w < a)
121 abort ();
123 return w;
125 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
126 SItype
127 __subvsi3 (SItype a, SItype b)
129 const SItype w = (USItype) a - (USItype) b;
131 if (b >= 0 ? w > a : w < a)
132 abort ();
134 return w;
136 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
137 #endif
139 #ifdef L_subvdi3
140 DWtype
141 __subvDI3 (DWtype a, DWtype b)
143 const DWtype w = (UDWtype) a - (UDWtype) b;
145 if (b >= 0 ? w > a : w < a)
146 abort ();
148 return w;
150 #endif
152 #ifdef L_mulvsi3
153 Wtype
154 __mulvSI3 (Wtype a, Wtype b)
156 const DWtype w = (DWtype) a * (DWtype) b;
158 if ((Wtype) (w >> W_TYPE_SIZE) != (Wtype) w >> (W_TYPE_SIZE - 1))
159 abort ();
161 return w;
163 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
164 #undef WORD_SIZE
165 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
166 SItype
167 __mulvsi3 (SItype a, SItype b)
169 const DItype w = (DItype) a * (DItype) b;
171 if ((SItype) (w >> WORD_SIZE) != (SItype) w >> (WORD_SIZE-1))
172 abort ();
174 return w;
176 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
177 #endif
179 #ifdef L_negvsi2
180 Wtype
181 __negvSI2 (Wtype a)
183 const Wtype w = -(UWtype) a;
185 if (a >= 0 ? w > 0 : w < 0)
186 abort ();
188 return w;
190 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
191 SItype
192 __negvsi2 (SItype a)
194 const SItype w = -(USItype) a;
196 if (a >= 0 ? w > 0 : w < 0)
197 abort ();
199 return w;
201 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
202 #endif
204 #ifdef L_negvdi2
205 DWtype
206 __negvDI2 (DWtype a)
208 const DWtype w = -(UDWtype) a;
210 if (a >= 0 ? w > 0 : w < 0)
211 abort ();
213 return w;
215 #endif
217 #ifdef L_absvsi2
218 Wtype
219 __absvSI2 (Wtype a)
221 Wtype w = a;
223 if (a < 0)
224 #ifdef L_negvsi2
225 w = __negvSI2 (a);
226 #else
227 w = -(UWtype) a;
229 if (w < 0)
230 abort ();
231 #endif
233 return w;
235 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
236 SItype
237 __absvsi2 (SItype a)
239 SItype w = a;
241 if (a < 0)
242 #ifdef L_negvsi2
243 w = __negvsi2 (a);
244 #else
245 w = -(USItype) a;
247 if (w < 0)
248 abort ();
249 #endif
251 return w;
253 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
254 #endif
256 #ifdef L_absvdi2
257 DWtype
258 __absvDI2 (DWtype a)
260 DWtype w = a;
262 if (a < 0)
263 #ifdef L_negvdi2
264 w = __negvDI2 (a);
265 #else
266 w = -(UDWtype) a;
268 if (w < 0)
269 abort ();
270 #endif
272 return w;
274 #endif
276 #ifdef L_mulvdi3
277 DWtype
278 __mulvDI3 (DWtype u, DWtype v)
280 /* The unchecked multiplication needs 3 Wtype x Wtype multiplications,
281 but the checked multiplication needs only two. */
282 const DWunion uu = {.ll = u};
283 const DWunion vv = {.ll = v};
285 if (__builtin_expect (uu.s.high == uu.s.low >> (W_TYPE_SIZE - 1), 1))
287 /* u fits in a single Wtype. */
288 if (__builtin_expect (vv.s.high == vv.s.low >> (W_TYPE_SIZE - 1), 1))
290 /* v fits in a single Wtype as well. */
291 /* A single multiplication. No overflow risk. */
292 return (DWtype) uu.s.low * (DWtype) vv.s.low;
294 else
296 /* Two multiplications. */
297 DWunion w0 = {.ll = (UDWtype) (UWtype) uu.s.low
298 * (UDWtype) (UWtype) vv.s.low};
299 DWunion w1 = {.ll = (UDWtype) (UWtype) uu.s.low
300 * (UDWtype) (UWtype) vv.s.high};
302 if (vv.s.high < 0)
303 w1.s.high -= uu.s.low;
304 if (uu.s.low < 0)
305 w1.ll -= vv.ll;
306 w1.ll += (UWtype) w0.s.high;
307 if (__builtin_expect (w1.s.high == w1.s.low >> (W_TYPE_SIZE - 1), 1))
309 w0.s.high = w1.s.low;
310 return w0.ll;
314 else
316 if (__builtin_expect (vv.s.high == vv.s.low >> (W_TYPE_SIZE - 1), 1))
318 /* v fits into a single Wtype. */
319 /* Two multiplications. */
320 DWunion w0 = {.ll = (UDWtype) (UWtype) uu.s.low
321 * (UDWtype) (UWtype) vv.s.low};
322 DWunion w1 = {.ll = (UDWtype) (UWtype) uu.s.high
323 * (UDWtype) (UWtype) vv.s.low};
325 if (uu.s.high < 0)
326 w1.s.high -= vv.s.low;
327 if (vv.s.low < 0)
328 w1.ll -= uu.ll;
329 w1.ll += (UWtype) w0.s.high;
330 if (__builtin_expect (w1.s.high == w1.s.low >> (W_TYPE_SIZE - 1), 1))
332 w0.s.high = w1.s.low;
333 return w0.ll;
336 else
338 /* A few sign checks and a single multiplication. */
339 if (uu.s.high >= 0)
341 if (vv.s.high >= 0)
343 if (uu.s.high == 0 && vv.s.high == 0)
345 const DWtype w = (UDWtype) (UWtype) uu.s.low
346 * (UDWtype) (UWtype) vv.s.low;
347 if (__builtin_expect (w >= 0, 1))
348 return w;
351 else
353 if (uu.s.high == 0 && vv.s.high == (Wtype) -1)
355 DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
356 * (UDWtype) (UWtype) vv.s.low};
358 ww.s.high -= uu.s.low;
359 if (__builtin_expect (ww.s.high < 0, 1))
360 return ww.ll;
364 else
366 if (vv.s.high >= 0)
368 if (uu.s.high == (Wtype) -1 && vv.s.high == 0)
370 DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
371 * (UDWtype) (UWtype) vv.s.low};
373 ww.s.high -= vv.s.low;
374 if (__builtin_expect (ww.s.high < 0, 1))
375 return ww.ll;
378 else
380 if (uu.s.high == (Wtype) -1 && vv.s.high == (Wtype) - 1)
382 DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
383 * (UDWtype) (UWtype) vv.s.low};
385 ww.s.high -= uu.s.low;
386 ww.s.high -= vv.s.low;
387 if (__builtin_expect (ww.s.high >= 0, 1))
388 return ww.ll;
395 /* Overflow. */
396 abort ();
398 #endif
401 /* Unless shift functions are defined with full ANSI prototypes,
402 parameter b will be promoted to int if shift_count_type is smaller than an int. */
403 #ifdef L_lshrdi3
404 DWtype
405 __lshrdi3 (DWtype u, shift_count_type b)
407 if (b == 0)
408 return u;
410 const DWunion uu = {.ll = u};
411 const shift_count_type bm = W_TYPE_SIZE - b;
412 DWunion w;
414 if (bm <= 0)
416 w.s.high = 0;
417 w.s.low = (UWtype) uu.s.high >> -bm;
419 else
421 const UWtype carries = (UWtype) uu.s.high << bm;
423 w.s.high = (UWtype) uu.s.high >> b;
424 w.s.low = ((UWtype) uu.s.low >> b) | carries;
427 return w.ll;
429 #endif
431 #ifdef L_ashldi3
432 DWtype
433 __ashldi3 (DWtype u, shift_count_type b)
435 if (b == 0)
436 return u;
438 const DWunion uu = {.ll = u};
439 const shift_count_type bm = W_TYPE_SIZE - b;
440 DWunion w;
442 if (bm <= 0)
444 w.s.low = 0;
445 w.s.high = (UWtype) uu.s.low << -bm;
447 else
449 const UWtype carries = (UWtype) uu.s.low >> bm;
451 w.s.low = (UWtype) uu.s.low << b;
452 w.s.high = ((UWtype) uu.s.high << b) | carries;
455 return w.ll;
457 #endif
459 #ifdef L_ashrdi3
460 DWtype
461 __ashrdi3 (DWtype u, shift_count_type b)
463 if (b == 0)
464 return u;
466 const DWunion uu = {.ll = u};
467 const shift_count_type bm = W_TYPE_SIZE - b;
468 DWunion w;
470 if (bm <= 0)
472 /* w.s.high = 1..1 or 0..0 */
473 w.s.high = uu.s.high >> (W_TYPE_SIZE - 1);
474 w.s.low = uu.s.high >> -bm;
476 else
478 const UWtype carries = (UWtype) uu.s.high << bm;
480 w.s.high = uu.s.high >> b;
481 w.s.low = ((UWtype) uu.s.low >> b) | carries;
484 return w.ll;
486 #endif
488 #ifdef L_bswapsi2
489 SItype
490 __bswapsi2 (SItype u)
492 return ((((u) & 0xff000000) >> 24)
493 | (((u) & 0x00ff0000) >> 8)
494 | (((u) & 0x0000ff00) << 8)
495 | (((u) & 0x000000ff) << 24));
497 #endif
498 #ifdef L_bswapdi2
499 DItype
500 __bswapdi2 (DItype u)
502 return ((((u) & 0xff00000000000000ull) >> 56)
503 | (((u) & 0x00ff000000000000ull) >> 40)
504 | (((u) & 0x0000ff0000000000ull) >> 24)
505 | (((u) & 0x000000ff00000000ull) >> 8)
506 | (((u) & 0x00000000ff000000ull) << 8)
507 | (((u) & 0x0000000000ff0000ull) << 24)
508 | (((u) & 0x000000000000ff00ull) << 40)
509 | (((u) & 0x00000000000000ffull) << 56));
511 #endif
512 #ifdef L_ffssi2
513 #undef int
515 __ffsSI2 (UWtype u)
517 UWtype count;
519 if (u == 0)
520 return 0;
522 count_trailing_zeros (count, u);
523 return count + 1;
525 #endif
527 #ifdef L_ffsdi2
528 #undef int
530 __ffsDI2 (DWtype u)
532 const DWunion uu = {.ll = u};
533 UWtype word, count, add;
535 if (uu.s.low != 0)
536 word = uu.s.low, add = 0;
537 else if (uu.s.high != 0)
538 word = uu.s.high, add = W_TYPE_SIZE;
539 else
540 return 0;
542 count_trailing_zeros (count, word);
543 return count + add + 1;
545 #endif
547 #ifdef L_muldi3
548 DWtype
549 __muldi3 (DWtype u, DWtype v)
551 const DWunion uu = {.ll = u};
552 const DWunion vv = {.ll = v};
553 DWunion w = {.ll = __umulsidi3 (uu.s.low, vv.s.low)};
555 w.s.high += ((UWtype) uu.s.low * (UWtype) vv.s.high
556 + (UWtype) uu.s.high * (UWtype) vv.s.low);
558 return w.ll;
560 #endif
562 #if (defined (L_udivdi3) || defined (L_divdi3) || \
563 defined (L_umoddi3) || defined (L_moddi3))
564 #if defined (sdiv_qrnnd)
565 #define L_udiv_w_sdiv
566 #endif
567 #endif
569 #ifdef L_udiv_w_sdiv
570 #if defined (sdiv_qrnnd)
571 #if (defined (L_udivdi3) || defined (L_divdi3) || \
572 defined (L_umoddi3) || defined (L_moddi3))
573 static inline __attribute__ ((__always_inline__))
574 #endif
575 UWtype
576 __udiv_w_sdiv (UWtype *rp, UWtype a1, UWtype a0, UWtype d)
578 UWtype q, r;
579 UWtype c0, c1, b1;
581 if ((Wtype) d >= 0)
583 if (a1 < d - a1 - (a0 >> (W_TYPE_SIZE - 1)))
585 /* Dividend, divisor, and quotient are nonnegative. */
586 sdiv_qrnnd (q, r, a1, a0, d);
588 else
590 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d. */
591 sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (W_TYPE_SIZE - 1));
592 /* Divide (c1*2^32 + c0) by d. */
593 sdiv_qrnnd (q, r, c1, c0, d);
594 /* Add 2^31 to quotient. */
595 q += (UWtype) 1 << (W_TYPE_SIZE - 1);
598 else
600 b1 = d >> 1; /* d/2, between 2^30 and 2^31 - 1 */
601 c1 = a1 >> 1; /* A/2 */
602 c0 = (a1 << (W_TYPE_SIZE - 1)) + (a0 >> 1);
604 if (a1 < b1) /* A < 2^32*b1, so A/2 < 2^31*b1 */
606 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
608 r = 2*r + (a0 & 1); /* Remainder from A/(2*b1) */
609 if ((d & 1) != 0)
611 if (r >= q)
612 r = r - q;
613 else if (q - r <= d)
615 r = r - q + d;
616 q--;
618 else
620 r = r - q + 2*d;
621 q -= 2;
625 else if (c1 < b1) /* So 2^31 <= (A/2)/b1 < 2^32 */
627 c1 = (b1 - 1) - c1;
628 c0 = ~c0; /* logical NOT */
630 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
632 q = ~q; /* (A/2)/b1 */
633 r = (b1 - 1) - r;
635 r = 2*r + (a0 & 1); /* A/(2*b1) */
637 if ((d & 1) != 0)
639 if (r >= q)
640 r = r - q;
641 else if (q - r <= d)
643 r = r - q + d;
644 q--;
646 else
648 r = r - q + 2*d;
649 q -= 2;
653 else /* Implies c1 = b1 */
654 { /* Hence a1 = d - 1 = 2*b1 - 1 */
655 if (a0 >= -d)
657 q = -1;
658 r = a0 + d;
660 else
662 q = -2;
663 r = a0 + 2*d;
668 *rp = r;
669 return q;
671 #else
672 /* If sdiv_qrnnd doesn't exist, define dummy __udiv_w_sdiv. */
673 UWtype
674 __udiv_w_sdiv (UWtype *rp __attribute__ ((__unused__)),
675 UWtype a1 __attribute__ ((__unused__)),
676 UWtype a0 __attribute__ ((__unused__)),
677 UWtype d __attribute__ ((__unused__)))
679 return 0;
681 #endif
682 #endif
684 #if (defined (L_udivdi3) || defined (L_divdi3) || \
685 defined (L_umoddi3) || defined (L_moddi3))
686 #define L_udivmoddi4
687 #endif
689 #ifdef L_clz
690 const UQItype __clz_tab[256] =
692 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,
693 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,
694 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,
695 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,
696 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,
697 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,
698 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,
699 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
701 #endif
703 #ifdef L_clzsi2
704 #undef int
706 __clzSI2 (UWtype x)
708 Wtype ret;
710 count_leading_zeros (ret, x);
712 return ret;
714 #endif
716 #ifdef L_clzdi2
717 #undef int
719 __clzDI2 (UDWtype x)
721 const DWunion uu = {.ll = x};
722 UWtype word;
723 Wtype ret, add;
725 if (uu.s.high)
726 word = uu.s.high, add = 0;
727 else
728 word = uu.s.low, add = W_TYPE_SIZE;
730 count_leading_zeros (ret, word);
731 return ret + add;
733 #endif
735 #ifdef L_ctzsi2
736 #undef int
738 __ctzSI2 (UWtype x)
740 Wtype ret;
742 count_trailing_zeros (ret, x);
744 return ret;
746 #endif
748 #ifdef L_ctzdi2
749 #undef int
751 __ctzDI2 (UDWtype x)
753 const DWunion uu = {.ll = x};
754 UWtype word;
755 Wtype ret, add;
757 if (uu.s.low)
758 word = uu.s.low, add = 0;
759 else
760 word = uu.s.high, add = W_TYPE_SIZE;
762 count_trailing_zeros (ret, word);
763 return ret + add;
765 #endif
767 #ifdef L_clrsbsi2
768 #undef int
770 __clrsbSI2 (Wtype x)
772 Wtype ret;
774 if (x < 0)
775 x = ~x;
776 if (x == 0)
777 return W_TYPE_SIZE - 1;
778 count_leading_zeros (ret, x);
779 return ret - 1;
781 #endif
783 #ifdef L_clrsbdi2
784 #undef int
786 __clrsbDI2 (DWtype x)
788 const DWunion uu = {.ll = x};
789 UWtype word;
790 Wtype ret, add;
792 if (uu.s.high == 0)
793 word = uu.s.low, add = W_TYPE_SIZE;
794 else if (uu.s.high == -1)
795 word = ~uu.s.low, add = W_TYPE_SIZE;
796 else if (uu.s.high >= 0)
797 word = uu.s.high, add = 0;
798 else
799 word = ~uu.s.high, add = 0;
801 if (word == 0)
802 ret = W_TYPE_SIZE;
803 else
804 count_leading_zeros (ret, word);
806 return ret + add - 1;
808 #endif
810 #ifdef L_popcount_tab
811 const UQItype __popcount_tab[256] =
813 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,
814 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,
815 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,
816 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,
817 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,
818 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,
819 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,
820 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
822 #endif
824 #ifdef L_popcountsi2
825 #undef int
827 __popcountSI2 (UWtype x)
829 int i, ret = 0;
831 for (i = 0; i < W_TYPE_SIZE; i += 8)
832 ret += __popcount_tab[(x >> i) & 0xff];
834 return ret;
836 #endif
838 #ifdef L_popcountdi2
839 #undef int
841 __popcountDI2 (UDWtype x)
843 int i, ret = 0;
845 for (i = 0; i < 2*W_TYPE_SIZE; i += 8)
846 ret += __popcount_tab[(x >> i) & 0xff];
848 return ret;
850 #endif
852 #ifdef L_paritysi2
853 #undef int
855 __paritySI2 (UWtype x)
857 #if W_TYPE_SIZE > 64
858 # error "fill out the table"
859 #endif
860 #if W_TYPE_SIZE > 32
861 x ^= x >> 32;
862 #endif
863 #if W_TYPE_SIZE > 16
864 x ^= x >> 16;
865 #endif
866 x ^= x >> 8;
867 x ^= x >> 4;
868 x &= 0xf;
869 return (0x6996 >> x) & 1;
871 #endif
873 #ifdef L_paritydi2
874 #undef int
876 __parityDI2 (UDWtype x)
878 const DWunion uu = {.ll = x};
879 UWtype nx = uu.s.low ^ uu.s.high;
881 #if W_TYPE_SIZE > 64
882 # error "fill out the table"
883 #endif
884 #if W_TYPE_SIZE > 32
885 nx ^= nx >> 32;
886 #endif
887 #if W_TYPE_SIZE > 16
888 nx ^= nx >> 16;
889 #endif
890 nx ^= nx >> 8;
891 nx ^= nx >> 4;
892 nx &= 0xf;
893 return (0x6996 >> nx) & 1;
895 #endif
897 #ifdef L_udivmoddi4
899 #if (defined (L_udivdi3) || defined (L_divdi3) || \
900 defined (L_umoddi3) || defined (L_moddi3))
901 static inline __attribute__ ((__always_inline__))
902 #endif
903 UDWtype
904 __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
906 const DWunion nn = {.ll = n};
907 const DWunion dd = {.ll = d};
908 DWunion rr;
909 UWtype d0, d1, n0, n1, n2;
910 UWtype q0, q1;
911 UWtype b, bm;
913 d0 = dd.s.low;
914 d1 = dd.s.high;
915 n0 = nn.s.low;
916 n1 = nn.s.high;
918 #if !UDIV_NEEDS_NORMALIZATION
919 if (d1 == 0)
921 if (d0 > n1)
923 /* 0q = nn / 0D */
925 udiv_qrnnd (q0, n0, n1, n0, d0);
926 q1 = 0;
928 /* Remainder in n0. */
930 else
932 /* qq = NN / 0d */
934 if (d0 == 0)
935 d0 = 1 / d0; /* Divide intentionally by zero. */
937 udiv_qrnnd (q1, n1, 0, n1, d0);
938 udiv_qrnnd (q0, n0, n1, n0, d0);
940 /* Remainder in n0. */
943 if (rp != 0)
945 rr.s.low = n0;
946 rr.s.high = 0;
947 *rp = rr.ll;
951 #else /* UDIV_NEEDS_NORMALIZATION */
953 if (d1 == 0)
955 if (d0 > n1)
957 /* 0q = nn / 0D */
959 count_leading_zeros (bm, d0);
961 if (bm != 0)
963 /* Normalize, i.e. make the most significant bit of the
964 denominator set. */
966 d0 = d0 << bm;
967 n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm));
968 n0 = n0 << bm;
971 udiv_qrnnd (q0, n0, n1, n0, d0);
972 q1 = 0;
974 /* Remainder in n0 >> bm. */
976 else
978 /* qq = NN / 0d */
980 if (d0 == 0)
981 d0 = 1 / d0; /* Divide intentionally by zero. */
983 count_leading_zeros (bm, d0);
985 if (bm == 0)
987 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
988 conclude (the most significant bit of n1 is set) /\ (the
989 leading quotient digit q1 = 1).
991 This special case is necessary, not an optimization.
992 (Shifts counts of W_TYPE_SIZE are undefined.) */
994 n1 -= d0;
995 q1 = 1;
997 else
999 /* Normalize. */
1001 b = W_TYPE_SIZE - bm;
1003 d0 = d0 << bm;
1004 n2 = n1 >> b;
1005 n1 = (n1 << bm) | (n0 >> b);
1006 n0 = n0 << bm;
1008 udiv_qrnnd (q1, n1, n2, n1, d0);
1011 /* n1 != d0... */
1013 udiv_qrnnd (q0, n0, n1, n0, d0);
1015 /* Remainder in n0 >> bm. */
1018 if (rp != 0)
1020 rr.s.low = n0 >> bm;
1021 rr.s.high = 0;
1022 *rp = rr.ll;
1025 #endif /* UDIV_NEEDS_NORMALIZATION */
1027 else
1029 if (d1 > n1)
1031 /* 00 = nn / DD */
1033 q0 = 0;
1034 q1 = 0;
1036 /* Remainder in n1n0. */
1037 if (rp != 0)
1039 rr.s.low = n0;
1040 rr.s.high = n1;
1041 *rp = rr.ll;
1044 else
1046 /* 0q = NN / dd */
1048 count_leading_zeros (bm, d1);
1049 if (bm == 0)
1051 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
1052 conclude (the most significant bit of n1 is set) /\ (the
1053 quotient digit q0 = 0 or 1).
1055 This special case is necessary, not an optimization. */
1057 /* The condition on the next line takes advantage of that
1058 n1 >= d1 (true due to program flow). */
1059 if (n1 > d1 || n0 >= d0)
1061 q0 = 1;
1062 sub_ddmmss (n1, n0, n1, n0, d1, d0);
1064 else
1065 q0 = 0;
1067 q1 = 0;
1069 if (rp != 0)
1071 rr.s.low = n0;
1072 rr.s.high = n1;
1073 *rp = rr.ll;
1076 else
1078 UWtype m1, m0;
1079 /* Normalize. */
1081 b = W_TYPE_SIZE - bm;
1083 d1 = (d1 << bm) | (d0 >> b);
1084 d0 = d0 << bm;
1085 n2 = n1 >> b;
1086 n1 = (n1 << bm) | (n0 >> b);
1087 n0 = n0 << bm;
1089 udiv_qrnnd (q0, n1, n2, n1, d1);
1090 umul_ppmm (m1, m0, q0, d0);
1092 if (m1 > n1 || (m1 == n1 && m0 > n0))
1094 q0--;
1095 sub_ddmmss (m1, m0, m1, m0, d1, d0);
1098 q1 = 0;
1100 /* Remainder in (n1n0 - m1m0) >> bm. */
1101 if (rp != 0)
1103 sub_ddmmss (n1, n0, n1, n0, m1, m0);
1104 rr.s.low = (n1 << b) | (n0 >> bm);
1105 rr.s.high = n1 >> bm;
1106 *rp = rr.ll;
1112 const DWunion ww = {{.low = q0, .high = q1}};
1113 return ww.ll;
1115 #endif
1117 #ifdef L_divdi3
1118 DWtype
1119 __divdi3 (DWtype u, DWtype v)
1121 Wtype c = 0;
1122 DWunion uu = {.ll = u};
1123 DWunion vv = {.ll = v};
1124 DWtype w;
1126 if (uu.s.high < 0)
1127 c = ~c,
1128 uu.ll = -uu.ll;
1129 if (vv.s.high < 0)
1130 c = ~c,
1131 vv.ll = -vv.ll;
1133 w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0);
1134 if (c)
1135 w = -w;
1137 return w;
1139 #endif
1141 #ifdef L_moddi3
1142 DWtype
1143 __moddi3 (DWtype u, DWtype v)
1145 Wtype c = 0;
1146 DWunion uu = {.ll = u};
1147 DWunion vv = {.ll = v};
1148 DWtype w;
1150 if (uu.s.high < 0)
1151 c = ~c,
1152 uu.ll = -uu.ll;
1153 if (vv.s.high < 0)
1154 vv.ll = -vv.ll;
1156 (void) __udivmoddi4 (uu.ll, vv.ll, (UDWtype*)&w);
1157 if (c)
1158 w = -w;
1160 return w;
1162 #endif
1164 #ifdef L_umoddi3
1165 UDWtype
1166 __umoddi3 (UDWtype u, UDWtype v)
1168 UDWtype w;
1170 (void) __udivmoddi4 (u, v, &w);
1172 return w;
1174 #endif
1176 #ifdef L_udivdi3
1177 UDWtype
1178 __udivdi3 (UDWtype n, UDWtype d)
1180 return __udivmoddi4 (n, d, (UDWtype *) 0);
1182 #endif
1184 #ifdef L_cmpdi2
1185 cmp_return_type
1186 __cmpdi2 (DWtype a, DWtype b)
1188 const DWunion au = {.ll = a};
1189 const DWunion bu = {.ll = b};
1191 if (au.s.high < bu.s.high)
1192 return 0;
1193 else if (au.s.high > bu.s.high)
1194 return 2;
1195 if ((UWtype) au.s.low < (UWtype) bu.s.low)
1196 return 0;
1197 else if ((UWtype) au.s.low > (UWtype) bu.s.low)
1198 return 2;
1199 return 1;
1201 #endif
1203 #ifdef L_ucmpdi2
1204 cmp_return_type
1205 __ucmpdi2 (DWtype a, DWtype b)
1207 const DWunion au = {.ll = a};
1208 const DWunion bu = {.ll = b};
1210 if ((UWtype) au.s.high < (UWtype) bu.s.high)
1211 return 0;
1212 else if ((UWtype) au.s.high > (UWtype) bu.s.high)
1213 return 2;
1214 if ((UWtype) au.s.low < (UWtype) bu.s.low)
1215 return 0;
1216 else if ((UWtype) au.s.low > (UWtype) bu.s.low)
1217 return 2;
1218 return 1;
1220 #endif
1222 #if defined(L_fixunstfdi) && LIBGCC2_HAS_TF_MODE
1223 UDWtype
1224 __fixunstfDI (TFtype a)
1226 if (a < 0)
1227 return 0;
1229 /* Compute high word of result, as a flonum. */
1230 const TFtype b = (a / Wtype_MAXp1_F);
1231 /* Convert that to fixed (but not to DWtype!),
1232 and shift it into the high word. */
1233 UDWtype v = (UWtype) b;
1234 v <<= W_TYPE_SIZE;
1235 /* Remove high part from the TFtype, leaving the low part as flonum. */
1236 a -= (TFtype)v;
1237 /* Convert that to fixed (but not to DWtype!) and add it in.
1238 Sometimes A comes out negative. This is significant, since
1239 A has more bits than a long int does. */
1240 if (a < 0)
1241 v -= (UWtype) (- a);
1242 else
1243 v += (UWtype) a;
1244 return v;
1246 #endif
1248 #if defined(L_fixtfdi) && LIBGCC2_HAS_TF_MODE
1249 DWtype
1250 __fixtfdi (TFtype a)
1252 if (a < 0)
1253 return - __fixunstfDI (-a);
1254 return __fixunstfDI (a);
1256 #endif
1258 #if defined(L_fixunsxfdi) && LIBGCC2_HAS_XF_MODE
1259 UDWtype
1260 __fixunsxfDI (XFtype a)
1262 if (a < 0)
1263 return 0;
1265 /* Compute high word of result, as a flonum. */
1266 const XFtype b = (a / Wtype_MAXp1_F);
1267 /* Convert that to fixed (but not to DWtype!),
1268 and shift it into the high word. */
1269 UDWtype v = (UWtype) b;
1270 v <<= W_TYPE_SIZE;
1271 /* Remove high part from the XFtype, leaving the low part as flonum. */
1272 a -= (XFtype)v;
1273 /* Convert that to fixed (but not to DWtype!) and add it in.
1274 Sometimes A comes out negative. This is significant, since
1275 A has more bits than a long int does. */
1276 if (a < 0)
1277 v -= (UWtype) (- a);
1278 else
1279 v += (UWtype) a;
1280 return v;
1282 #endif
1284 #if defined(L_fixxfdi) && LIBGCC2_HAS_XF_MODE
1285 DWtype
1286 __fixxfdi (XFtype a)
1288 if (a < 0)
1289 return - __fixunsxfDI (-a);
1290 return __fixunsxfDI (a);
1292 #endif
1294 #if defined(L_fixunsdfdi) && LIBGCC2_HAS_DF_MODE
1295 UDWtype
1296 __fixunsdfDI (DFtype 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 = a / 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 = a - (DFtype) hi * Wtype_MAXp1_F;
1308 /* Assemble result from the two parts. */
1309 return ((UDWtype) hi << W_TYPE_SIZE) | lo;
1311 #endif
1313 #if defined(L_fixdfdi) && LIBGCC2_HAS_DF_MODE
1314 DWtype
1315 __fixdfdi (DFtype a)
1317 if (a < 0)
1318 return - __fixunsdfDI (-a);
1319 return __fixunsdfDI (a);
1321 #endif
1323 #if defined(L_fixunssfdi) && LIBGCC2_HAS_SF_MODE
1324 UDWtype
1325 __fixunssfDI (SFtype a)
1327 #if LIBGCC2_HAS_DF_MODE
1328 /* Convert the SFtype to a DFtype, because that is surely not going
1329 to lose any bits. Some day someone else can write a faster version
1330 that avoids converting to DFtype, and verify it really works right. */
1331 const DFtype dfa = a;
1333 /* Get high part of result. The division here will just moves the radix
1334 point and will not cause any rounding. Then the conversion to integral
1335 type chops result as desired. */
1336 const UWtype hi = dfa / Wtype_MAXp1_F;
1338 /* Get low part of result. Convert `hi' to floating type and scale it back,
1339 then subtract this from the number being converted. This leaves the low
1340 part. Convert that to integral type. */
1341 const UWtype lo = dfa - (DFtype) hi * Wtype_MAXp1_F;
1343 /* Assemble result from the two parts. */
1344 return ((UDWtype) hi << W_TYPE_SIZE) | lo;
1345 #elif FLT_MANT_DIG < W_TYPE_SIZE
1346 if (a < 1)
1347 return 0;
1348 if (a < Wtype_MAXp1_F)
1349 return (UWtype)a;
1350 if (a < Wtype_MAXp1_F * Wtype_MAXp1_F)
1352 /* Since we know that there are fewer significant bits in the SFmode
1353 quantity than in a word, we know that we can convert out all the
1354 significant bits in one step, and thus avoid losing bits. */
1356 /* ??? This following loop essentially performs frexpf. If we could
1357 use the real libm function, or poke at the actual bits of the fp
1358 format, it would be significantly faster. */
1360 UWtype shift = 0, counter;
1361 SFtype msb;
1363 a /= Wtype_MAXp1_F;
1364 for (counter = W_TYPE_SIZE / 2; counter != 0; counter >>= 1)
1366 SFtype counterf = (UWtype)1 << counter;
1367 if (a >= counterf)
1369 shift |= counter;
1370 a /= counterf;
1374 /* Rescale into the range of one word, extract the bits of that
1375 one word, and shift the result into position. */
1376 a *= Wtype_MAXp1_F;
1377 counter = a;
1378 return (DWtype)counter << shift;
1380 return -1;
1381 #else
1382 # error
1383 #endif
1385 #endif
1387 #if defined(L_fixsfdi) && LIBGCC2_HAS_SF_MODE
1388 DWtype
1389 __fixsfdi (SFtype a)
1391 if (a < 0)
1392 return - __fixunssfDI (-a);
1393 return __fixunssfDI (a);
1395 #endif
1397 #if defined(L_floatdixf) && LIBGCC2_HAS_XF_MODE
1398 XFtype
1399 __floatdixf (DWtype u)
1401 #if W_TYPE_SIZE > XF_SIZE
1402 # error
1403 #endif
1404 XFtype d = (Wtype) (u >> W_TYPE_SIZE);
1405 d *= Wtype_MAXp1_F;
1406 d += (UWtype)u;
1407 return d;
1409 #endif
1411 #if defined(L_floatundixf) && LIBGCC2_HAS_XF_MODE
1412 XFtype
1413 __floatundixf (UDWtype u)
1415 #if W_TYPE_SIZE > XF_SIZE
1416 # error
1417 #endif
1418 XFtype d = (UWtype) (u >> W_TYPE_SIZE);
1419 d *= Wtype_MAXp1_F;
1420 d += (UWtype)u;
1421 return d;
1423 #endif
1425 #if defined(L_floatditf) && LIBGCC2_HAS_TF_MODE
1426 TFtype
1427 __floatditf (DWtype u)
1429 #if W_TYPE_SIZE > TF_SIZE
1430 # error
1431 #endif
1432 TFtype d = (Wtype) (u >> W_TYPE_SIZE);
1433 d *= Wtype_MAXp1_F;
1434 d += (UWtype)u;
1435 return d;
1437 #endif
1439 #if defined(L_floatunditf) && LIBGCC2_HAS_TF_MODE
1440 TFtype
1441 __floatunditf (UDWtype u)
1443 #if W_TYPE_SIZE > TF_SIZE
1444 # error
1445 #endif
1446 TFtype d = (UWtype) (u >> W_TYPE_SIZE);
1447 d *= Wtype_MAXp1_F;
1448 d += (UWtype)u;
1449 return d;
1451 #endif
1453 #if (defined(L_floatdisf) && LIBGCC2_HAS_SF_MODE) \
1454 || (defined(L_floatdidf) && LIBGCC2_HAS_DF_MODE)
1455 #define DI_SIZE (W_TYPE_SIZE * 2)
1456 #define F_MODE_OK(SIZE) \
1457 (SIZE < DI_SIZE \
1458 && SIZE > (DI_SIZE - SIZE + FSSIZE) \
1459 && !AVOID_FP_TYPE_CONVERSION(SIZE))
1460 #if defined(L_floatdisf)
1461 #define FUNC __floatdisf
1462 #define FSTYPE SFtype
1463 #define FSSIZE SF_SIZE
1464 #else
1465 #define FUNC __floatdidf
1466 #define FSTYPE DFtype
1467 #define FSSIZE DF_SIZE
1468 #endif
1470 FSTYPE
1471 FUNC (DWtype u)
1473 #if FSSIZE >= W_TYPE_SIZE
1474 /* When the word size is small, we never get any rounding error. */
1475 FSTYPE f = (Wtype) (u >> W_TYPE_SIZE);
1476 f *= Wtype_MAXp1_F;
1477 f += (UWtype)u;
1478 return f;
1479 #elif (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE)) \
1480 || (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE)) \
1481 || (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1483 #if (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE))
1484 # define FSIZE DF_SIZE
1485 # define FTYPE DFtype
1486 #elif (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE))
1487 # define FSIZE XF_SIZE
1488 # define FTYPE XFtype
1489 #elif (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1490 # define FSIZE TF_SIZE
1491 # define FTYPE TFtype
1492 #else
1493 # error
1494 #endif
1496 #define REP_BIT ((UDWtype) 1 << (DI_SIZE - FSIZE))
1498 /* Protect against double-rounding error.
1499 Represent any low-order bits, that might be truncated by a bit that
1500 won't be lost. The bit can go in anywhere below the rounding position
1501 of the FSTYPE. A fixed mask and bit position handles all usual
1502 configurations. */
1503 if (! (- ((DWtype) 1 << FSIZE) < u
1504 && u < ((DWtype) 1 << FSIZE)))
1506 if ((UDWtype) u & (REP_BIT - 1))
1508 u &= ~ (REP_BIT - 1);
1509 u |= REP_BIT;
1513 /* Do the calculation in a wider type so that we don't lose any of
1514 the precision of the high word while multiplying it. */
1515 FTYPE f = (Wtype) (u >> W_TYPE_SIZE);
1516 f *= Wtype_MAXp1_F;
1517 f += (UWtype)u;
1518 return (FSTYPE) f;
1519 #else
1520 #if FSSIZE >= W_TYPE_SIZE - 2
1521 # error
1522 #endif
1523 /* Finally, the word size is larger than the number of bits in the
1524 required FSTYPE, and we've got no suitable wider type. The only
1525 way to avoid double rounding is to special case the
1526 extraction. */
1528 /* If there are no high bits set, fall back to one conversion. */
1529 if ((Wtype)u == u)
1530 return (FSTYPE)(Wtype)u;
1532 /* Otherwise, find the power of two. */
1533 Wtype hi = u >> W_TYPE_SIZE;
1534 if (hi < 0)
1535 hi = -hi;
1537 UWtype count, shift;
1538 count_leading_zeros (count, hi);
1540 /* No leading bits means u == minimum. */
1541 if (count == 0)
1542 return -(Wtype_MAXp1_F * (Wtype_MAXp1_F / 2));
1544 shift = 1 + W_TYPE_SIZE - count;
1546 /* Shift down the most significant bits. */
1547 hi = u >> shift;
1549 /* If we lost any nonzero bits, set the lsb to ensure correct rounding. */
1550 if ((UWtype)u << (W_TYPE_SIZE - shift))
1551 hi |= 1;
1553 /* Convert the one word of data, and rescale. */
1554 FSTYPE f = hi, e;
1555 if (shift == W_TYPE_SIZE)
1556 e = Wtype_MAXp1_F;
1557 /* The following two cases could be merged if we knew that the target
1558 supported a native unsigned->float conversion. More often, we only
1559 have a signed conversion, and have to add extra fixup code. */
1560 else if (shift == W_TYPE_SIZE - 1)
1561 e = Wtype_MAXp1_F / 2;
1562 else
1563 e = (Wtype)1 << shift;
1564 return f * e;
1565 #endif
1567 #endif
1569 #if (defined(L_floatundisf) && LIBGCC2_HAS_SF_MODE) \
1570 || (defined(L_floatundidf) && LIBGCC2_HAS_DF_MODE)
1571 #define DI_SIZE (W_TYPE_SIZE * 2)
1572 #define F_MODE_OK(SIZE) \
1573 (SIZE < DI_SIZE \
1574 && SIZE > (DI_SIZE - SIZE + FSSIZE) \
1575 && !AVOID_FP_TYPE_CONVERSION(SIZE))
1576 #if defined(L_floatundisf)
1577 #define FUNC __floatundisf
1578 #define FSTYPE SFtype
1579 #define FSSIZE SF_SIZE
1580 #else
1581 #define FUNC __floatundidf
1582 #define FSTYPE DFtype
1583 #define FSSIZE DF_SIZE
1584 #endif
1586 FSTYPE
1587 FUNC (UDWtype u)
1589 #if FSSIZE >= W_TYPE_SIZE
1590 /* When the word size is small, we never get any rounding error. */
1591 FSTYPE f = (UWtype) (u >> W_TYPE_SIZE);
1592 f *= Wtype_MAXp1_F;
1593 f += (UWtype)u;
1594 return f;
1595 #elif (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE)) \
1596 || (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE)) \
1597 || (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1599 #if (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE))
1600 # define FSIZE DF_SIZE
1601 # define FTYPE DFtype
1602 #elif (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE))
1603 # define FSIZE XF_SIZE
1604 # define FTYPE XFtype
1605 #elif (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1606 # define FSIZE TF_SIZE
1607 # define FTYPE TFtype
1608 #else
1609 # error
1610 #endif
1612 #define REP_BIT ((UDWtype) 1 << (DI_SIZE - FSIZE))
1614 /* Protect against double-rounding error.
1615 Represent any low-order bits, that might be truncated by a bit that
1616 won't be lost. The bit can go in anywhere below the rounding position
1617 of the FSTYPE. A fixed mask and bit position handles all usual
1618 configurations. */
1619 if (u >= ((UDWtype) 1 << FSIZE))
1621 if ((UDWtype) u & (REP_BIT - 1))
1623 u &= ~ (REP_BIT - 1);
1624 u |= REP_BIT;
1628 /* Do the calculation in a wider type so that we don't lose any of
1629 the precision of the high word while multiplying it. */
1630 FTYPE f = (UWtype) (u >> W_TYPE_SIZE);
1631 f *= Wtype_MAXp1_F;
1632 f += (UWtype)u;
1633 return (FSTYPE) f;
1634 #else
1635 #if FSSIZE == W_TYPE_SIZE - 1
1636 # error
1637 #endif
1638 /* Finally, the word size is larger than the number of bits in the
1639 required FSTYPE, and we've got no suitable wider type. The only
1640 way to avoid double rounding is to special case the
1641 extraction. */
1643 /* If there are no high bits set, fall back to one conversion. */
1644 if ((UWtype)u == u)
1645 return (FSTYPE)(UWtype)u;
1647 /* Otherwise, find the power of two. */
1648 UWtype hi = u >> W_TYPE_SIZE;
1650 UWtype count, shift;
1651 count_leading_zeros (count, hi);
1653 shift = W_TYPE_SIZE - count;
1655 /* Shift down the most significant bits. */
1656 hi = u >> shift;
1658 /* If we lost any nonzero bits, set the lsb to ensure correct rounding. */
1659 if ((UWtype)u << (W_TYPE_SIZE - shift))
1660 hi |= 1;
1662 /* Convert the one word of data, and rescale. */
1663 FSTYPE f = hi, e;
1664 if (shift == W_TYPE_SIZE)
1665 e = Wtype_MAXp1_F;
1666 /* The following two cases could be merged if we knew that the target
1667 supported a native unsigned->float conversion. More often, we only
1668 have a signed conversion, and have to add extra fixup code. */
1669 else if (shift == W_TYPE_SIZE - 1)
1670 e = Wtype_MAXp1_F / 2;
1671 else
1672 e = (Wtype)1 << shift;
1673 return f * e;
1674 #endif
1676 #endif
1678 #if defined(L_fixunsxfsi) && LIBGCC2_HAS_XF_MODE
1679 /* Reenable the normal types, in case limits.h needs them. */
1680 #undef char
1681 #undef short
1682 #undef int
1683 #undef long
1684 #undef unsigned
1685 #undef float
1686 #undef double
1687 #undef MIN
1688 #undef MAX
1689 #include <limits.h>
1691 UWtype
1692 __fixunsxfSI (XFtype a)
1694 if (a >= - (DFtype) Wtype_MIN)
1695 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1696 return (Wtype) a;
1698 #endif
1700 #if defined(L_fixunsdfsi) && LIBGCC2_HAS_DF_MODE
1701 /* Reenable the normal types, in case limits.h needs them. */
1702 #undef char
1703 #undef short
1704 #undef int
1705 #undef long
1706 #undef unsigned
1707 #undef float
1708 #undef double
1709 #undef MIN
1710 #undef MAX
1711 #include <limits.h>
1713 UWtype
1714 __fixunsdfSI (DFtype a)
1716 if (a >= - (DFtype) Wtype_MIN)
1717 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1718 return (Wtype) a;
1720 #endif
1722 #if defined(L_fixunssfsi) && LIBGCC2_HAS_SF_MODE
1723 /* Reenable the normal types, in case limits.h needs them. */
1724 #undef char
1725 #undef short
1726 #undef int
1727 #undef long
1728 #undef unsigned
1729 #undef float
1730 #undef double
1731 #undef MIN
1732 #undef MAX
1733 #include <limits.h>
1735 UWtype
1736 __fixunssfSI (SFtype a)
1738 if (a >= - (SFtype) Wtype_MIN)
1739 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1740 return (Wtype) a;
1742 #endif
1744 /* Integer power helper used from __builtin_powi for non-constant
1745 exponents. */
1747 #if (defined(L_powisf2) && LIBGCC2_HAS_SF_MODE) \
1748 || (defined(L_powidf2) && LIBGCC2_HAS_DF_MODE) \
1749 || (defined(L_powixf2) && LIBGCC2_HAS_XF_MODE) \
1750 || (defined(L_powitf2) && LIBGCC2_HAS_TF_MODE)
1751 # if defined(L_powisf2)
1752 # define TYPE SFtype
1753 # define NAME __powisf2
1754 # elif defined(L_powidf2)
1755 # define TYPE DFtype
1756 # define NAME __powidf2
1757 # elif defined(L_powixf2)
1758 # define TYPE XFtype
1759 # define NAME __powixf2
1760 # elif defined(L_powitf2)
1761 # define TYPE TFtype
1762 # define NAME __powitf2
1763 # endif
1765 #undef int
1766 #undef unsigned
1767 TYPE
1768 NAME (TYPE x, int m)
1770 unsigned int n = m < 0 ? -m : m;
1771 TYPE y = n % 2 ? x : 1;
1772 while (n >>= 1)
1774 x = x * x;
1775 if (n % 2)
1776 y = y * x;
1778 return m < 0 ? 1/y : y;
1781 #endif
1783 #if ((defined(L_mulsc3) || defined(L_divsc3)) && LIBGCC2_HAS_SF_MODE) \
1784 || ((defined(L_muldc3) || defined(L_divdc3)) && LIBGCC2_HAS_DF_MODE) \
1785 || ((defined(L_mulxc3) || defined(L_divxc3)) && LIBGCC2_HAS_XF_MODE) \
1786 || ((defined(L_multc3) || defined(L_divtc3)) && LIBGCC2_HAS_TF_MODE)
1788 #undef float
1789 #undef double
1790 #undef long
1792 #if defined(L_mulsc3) || defined(L_divsc3)
1793 # define MTYPE SFtype
1794 # define CTYPE SCtype
1795 # define MODE sc
1796 # define CEXT f
1797 # define NOTRUNC __FLT_EVAL_METHOD__ == 0
1798 #elif defined(L_muldc3) || defined(L_divdc3)
1799 # define MTYPE DFtype
1800 # define CTYPE DCtype
1801 # define MODE dc
1802 # if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 64
1803 # define CEXT l
1804 # define NOTRUNC 1
1805 # else
1806 # define CEXT
1807 # define NOTRUNC __FLT_EVAL_METHOD__ == 0 || __FLT_EVAL_METHOD__ == 1
1808 # endif
1809 #elif defined(L_mulxc3) || defined(L_divxc3)
1810 # define MTYPE XFtype
1811 # define CTYPE XCtype
1812 # define MODE xc
1813 # define CEXT l
1814 # define NOTRUNC 1
1815 #elif defined(L_multc3) || defined(L_divtc3)
1816 # define MTYPE TFtype
1817 # define CTYPE TCtype
1818 # define MODE tc
1819 # if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128
1820 # define CEXT l
1821 # else
1822 # define CEXT LIBGCC2_TF_CEXT
1823 # endif
1824 # define NOTRUNC 1
1825 #else
1826 # error
1827 #endif
1829 #define CONCAT3(A,B,C) _CONCAT3(A,B,C)
1830 #define _CONCAT3(A,B,C) A##B##C
1832 #define CONCAT2(A,B) _CONCAT2(A,B)
1833 #define _CONCAT2(A,B) A##B
1835 /* All of these would be present in a full C99 implementation of <math.h>
1836 and <complex.h>. Our problem is that only a few systems have such full
1837 implementations. Further, libgcc_s.so isn't currently linked against
1838 libm.so, and even for systems that do provide full C99, the extra overhead
1839 of all programs using libgcc having to link against libm. So avoid it. */
1841 #define isnan(x) __builtin_expect ((x) != (x), 0)
1842 #define isfinite(x) __builtin_expect (!isnan((x) - (x)), 1)
1843 #define isinf(x) __builtin_expect (!isnan(x) & !isfinite(x), 0)
1845 #define INFINITY CONCAT2(__builtin_huge_val, CEXT) ()
1846 #define I 1i
1848 /* Helpers to make the following code slightly less gross. */
1849 #define COPYSIGN CONCAT2(__builtin_copysign, CEXT)
1850 #define FABS CONCAT2(__builtin_fabs, CEXT)
1852 /* Verify that MTYPE matches up with CEXT. */
1853 extern void *compile_type_assert[sizeof(INFINITY) == sizeof(MTYPE) ? 1 : -1];
1855 /* Ensure that we've lost any extra precision. */
1856 #if NOTRUNC
1857 # define TRUNC(x)
1858 #else
1859 # define TRUNC(x) __asm__ ("" : "=m"(x) : "m"(x))
1860 #endif
1862 #if defined(L_mulsc3) || defined(L_muldc3) \
1863 || defined(L_mulxc3) || defined(L_multc3)
1865 CTYPE
1866 CONCAT3(__mul,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
1868 MTYPE ac, bd, ad, bc, x, y;
1869 CTYPE res;
1871 ac = a * c;
1872 bd = b * d;
1873 ad = a * d;
1874 bc = b * c;
1876 TRUNC (ac);
1877 TRUNC (bd);
1878 TRUNC (ad);
1879 TRUNC (bc);
1881 x = ac - bd;
1882 y = ad + bc;
1884 if (isnan (x) && isnan (y))
1886 /* Recover infinities that computed as NaN + iNaN. */
1887 _Bool recalc = 0;
1888 if (isinf (a) || isinf (b))
1890 /* z is infinite. "Box" the infinity and change NaNs in
1891 the other factor to 0. */
1892 a = COPYSIGN (isinf (a) ? 1 : 0, a);
1893 b = COPYSIGN (isinf (b) ? 1 : 0, b);
1894 if (isnan (c)) c = COPYSIGN (0, c);
1895 if (isnan (d)) d = COPYSIGN (0, d);
1896 recalc = 1;
1898 if (isinf (c) || isinf (d))
1900 /* w is infinite. "Box" the infinity and change NaNs in
1901 the other factor to 0. */
1902 c = COPYSIGN (isinf (c) ? 1 : 0, c);
1903 d = COPYSIGN (isinf (d) ? 1 : 0, d);
1904 if (isnan (a)) a = COPYSIGN (0, a);
1905 if (isnan (b)) b = COPYSIGN (0, b);
1906 recalc = 1;
1908 if (!recalc
1909 && (isinf (ac) || isinf (bd)
1910 || isinf (ad) || isinf (bc)))
1912 /* Recover infinities from overflow by changing NaNs to 0. */
1913 if (isnan (a)) a = COPYSIGN (0, a);
1914 if (isnan (b)) b = COPYSIGN (0, b);
1915 if (isnan (c)) c = COPYSIGN (0, c);
1916 if (isnan (d)) d = COPYSIGN (0, d);
1917 recalc = 1;
1919 if (recalc)
1921 x = INFINITY * (a * c - b * d);
1922 y = INFINITY * (a * d + b * c);
1926 __real__ res = x;
1927 __imag__ res = y;
1928 return res;
1930 #endif /* complex multiply */
1932 #if defined(L_divsc3) || defined(L_divdc3) \
1933 || defined(L_divxc3) || defined(L_divtc3)
1935 CTYPE
1936 CONCAT3(__div,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
1938 MTYPE denom, ratio, x, y;
1939 CTYPE res;
1941 /* ??? We can get better behavior from logarithmic scaling instead of
1942 the division. But that would mean starting to link libgcc against
1943 libm. We could implement something akin to ldexp/frexp as gcc builtins
1944 fairly easily... */
1945 if (FABS (c) < FABS (d))
1947 ratio = c / d;
1948 denom = (c * ratio) + d;
1949 x = ((a * ratio) + b) / denom;
1950 y = ((b * ratio) - a) / denom;
1952 else
1954 ratio = d / c;
1955 denom = (d * ratio) + c;
1956 x = ((b * ratio) + a) / denom;
1957 y = (b - (a * ratio)) / denom;
1960 /* Recover infinities and zeros that computed as NaN+iNaN; the only cases
1961 are nonzero/zero, infinite/finite, and finite/infinite. */
1962 if (isnan (x) && isnan (y))
1964 if (c == 0.0 && d == 0.0 && (!isnan (a) || !isnan (b)))
1966 x = COPYSIGN (INFINITY, c) * a;
1967 y = COPYSIGN (INFINITY, c) * b;
1969 else if ((isinf (a) || isinf (b)) && isfinite (c) && isfinite (d))
1971 a = COPYSIGN (isinf (a) ? 1 : 0, a);
1972 b = COPYSIGN (isinf (b) ? 1 : 0, b);
1973 x = INFINITY * (a * c + b * d);
1974 y = INFINITY * (b * c - a * d);
1976 else if ((isinf (c) || isinf (d)) && isfinite (a) && isfinite (b))
1978 c = COPYSIGN (isinf (c) ? 1 : 0, c);
1979 d = COPYSIGN (isinf (d) ? 1 : 0, d);
1980 x = 0.0 * (a * c + b * d);
1981 y = 0.0 * (b * c - a * d);
1985 __real__ res = x;
1986 __imag__ res = y;
1987 return res;
1989 #endif /* complex divide */
1991 #endif /* all complex float routines */
1993 /* From here on down, the routines use normal data types. */
1995 #define SItype bogus_type
1996 #define USItype bogus_type
1997 #define DItype bogus_type
1998 #define UDItype bogus_type
1999 #define SFtype bogus_type
2000 #define DFtype bogus_type
2001 #undef Wtype
2002 #undef UWtype
2003 #undef HWtype
2004 #undef UHWtype
2005 #undef DWtype
2006 #undef UDWtype
2008 #undef char
2009 #undef short
2010 #undef int
2011 #undef long
2012 #undef unsigned
2013 #undef float
2014 #undef double
2016 #ifdef L__gcc_bcmp
2018 /* Like bcmp except the sign is meaningful.
2019 Result is negative if S1 is less than S2,
2020 positive if S1 is greater, 0 if S1 and S2 are equal. */
2023 __gcc_bcmp (const unsigned char *s1, const unsigned char *s2, size_t size)
2025 while (size > 0)
2027 const unsigned char c1 = *s1++, c2 = *s2++;
2028 if (c1 != c2)
2029 return c1 - c2;
2030 size--;
2032 return 0;
2035 #endif
2037 /* __eprintf used to be used by GCC's private version of <assert.h>.
2038 We no longer provide that header, but this routine remains in libgcc.a
2039 for binary backward compatibility. Note that it is not included in
2040 the shared version of libgcc. */
2041 #ifdef L_eprintf
2042 #ifndef inhibit_libc
2044 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
2045 #include <stdio.h>
2047 void
2048 __eprintf (const char *string, const char *expression,
2049 unsigned int line, const char *filename)
2051 fprintf (stderr, string, expression, line, filename);
2052 fflush (stderr);
2053 abort ();
2056 #endif
2057 #endif
2060 #ifdef L_clear_cache
2061 /* Clear part of an instruction cache. */
2063 void
2064 __clear_cache (char *beg __attribute__((__unused__)),
2065 char *end __attribute__((__unused__)))
2067 #ifdef CLEAR_INSN_CACHE
2068 CLEAR_INSN_CACHE (beg, end);
2069 #endif /* CLEAR_INSN_CACHE */
2072 #endif /* L_clear_cache */
2074 #ifdef L_trampoline
2076 /* Jump to a trampoline, loading the static chain address. */
2078 #if defined(WINNT) && ! defined(__CYGWIN__)
2079 #include <windows.h>
2080 int getpagesize (void);
2081 int mprotect (char *,int, int);
2084 getpagesize (void)
2086 #ifdef _ALPHA_
2087 return 8192;
2088 #else
2089 return 4096;
2090 #endif
2094 mprotect (char *addr, int len, int prot)
2096 DWORD np, op;
2098 if (prot == 7)
2099 np = 0x40;
2100 else if (prot == 5)
2101 np = 0x20;
2102 else if (prot == 4)
2103 np = 0x10;
2104 else if (prot == 3)
2105 np = 0x04;
2106 else if (prot == 1)
2107 np = 0x02;
2108 else if (prot == 0)
2109 np = 0x01;
2110 else
2111 return -1;
2113 if (VirtualProtect (addr, len, np, &op))
2114 return 0;
2115 else
2116 return -1;
2119 #endif /* WINNT && ! __CYGWIN__ */
2121 #ifdef TRANSFER_FROM_TRAMPOLINE
2122 TRANSFER_FROM_TRAMPOLINE
2123 #endif
2124 #endif /* L_trampoline */
2126 #ifndef __CYGWIN__
2127 #ifdef L__main
2129 #include "gbl-ctors.h"
2131 /* Some systems use __main in a way incompatible with its use in gcc, in these
2132 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
2133 give the same symbol without quotes for an alternative entry point. You
2134 must define both, or neither. */
2135 #ifndef NAME__MAIN
2136 #define NAME__MAIN "__main"
2137 #define SYMBOL__MAIN __main
2138 #endif
2140 #if defined (INIT_SECTION_ASM_OP) || defined (INIT_ARRAY_SECTION_ASM_OP)
2141 #undef HAS_INIT_SECTION
2142 #define HAS_INIT_SECTION
2143 #endif
2145 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
2147 /* Some ELF crosses use crtstuff.c to provide __CTOR_LIST__, but use this
2148 code to run constructors. In that case, we need to handle EH here, too. */
2150 #ifdef EH_FRAME_SECTION_NAME
2151 #include "unwind-dw2-fde.h"
2152 extern unsigned char __EH_FRAME_BEGIN__[];
2153 #endif
2155 /* Run all the global destructors on exit from the program. */
2157 void
2158 __do_global_dtors (void)
2160 #ifdef DO_GLOBAL_DTORS_BODY
2161 DO_GLOBAL_DTORS_BODY;
2162 #else
2163 static func_ptr *p = __DTOR_LIST__ + 1;
2164 while (*p)
2166 p++;
2167 (*(p-1)) ();
2169 #endif
2170 #if defined (EH_FRAME_SECTION_NAME) && !defined (HAS_INIT_SECTION)
2172 static int completed = 0;
2173 if (! completed)
2175 completed = 1;
2176 __deregister_frame_info (__EH_FRAME_BEGIN__);
2179 #endif
2181 #endif
2183 #ifndef HAS_INIT_SECTION
2184 /* Run all the global constructors on entry to the program. */
2186 void
2187 __do_global_ctors (void)
2189 #ifdef EH_FRAME_SECTION_NAME
2191 static struct object object;
2192 __register_frame_info (__EH_FRAME_BEGIN__, &object);
2194 #endif
2195 DO_GLOBAL_CTORS_BODY;
2196 atexit (__do_global_dtors);
2198 #endif /* no HAS_INIT_SECTION */
2200 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
2201 /* Subroutine called automatically by `main'.
2202 Compiling a global function named `main'
2203 produces an automatic call to this function at the beginning.
2205 For many systems, this routine calls __do_global_ctors.
2206 For systems which support a .init section we use the .init section
2207 to run __do_global_ctors, so we need not do anything here. */
2209 extern void SYMBOL__MAIN (void);
2210 void
2211 SYMBOL__MAIN (void)
2213 /* Support recursive calls to `main': run initializers just once. */
2214 static int initialized;
2215 if (! initialized)
2217 initialized = 1;
2218 __do_global_ctors ();
2221 #endif /* no HAS_INIT_SECTION or INVOKE__main */
2223 #endif /* L__main */
2224 #endif /* __CYGWIN__ */
2226 #ifdef L_ctors
2228 #include "gbl-ctors.h"
2230 /* Provide default definitions for the lists of constructors and
2231 destructors, so that we don't get linker errors. These symbols are
2232 intentionally bss symbols, so that gld and/or collect will provide
2233 the right values. */
2235 /* We declare the lists here with two elements each,
2236 so that they are valid empty lists if no other definition is loaded.
2238 If we are using the old "set" extensions to have the gnu linker
2239 collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
2240 must be in the bss/common section.
2242 Long term no port should use those extensions. But many still do. */
2243 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
2244 #if defined (TARGET_ASM_CONSTRUCTOR) || defined (USE_COLLECT2)
2245 func_ptr __CTOR_LIST__[2] = {0, 0};
2246 func_ptr __DTOR_LIST__[2] = {0, 0};
2247 #else
2248 func_ptr __CTOR_LIST__[2];
2249 func_ptr __DTOR_LIST__[2];
2250 #endif
2251 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
2252 #endif /* L_ctors */
2253 #endif /* LIBGCC2_UNITS_PER_WORD <= MIN_UNITS_PER_WORD */