Relax constraints on Machine_Attribute argument types:
[official-gcc.git] / gcc / libgcc2.c
blob4097d3eefb07f436e68ef181435b03b9b2828594
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
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"
33 #ifdef HAVE_GAS_HIDDEN
34 #define ATTRIBUTE_HIDDEN __attribute__ ((__visibility__ ("hidden")))
35 #else
36 #define ATTRIBUTE_HIDDEN
37 #endif
39 #ifndef MIN_UNITS_PER_WORD
40 #define MIN_UNITS_PER_WORD UNITS_PER_WORD
41 #endif
43 /* Work out the largest "word" size that we can deal with on this target. */
44 #if MIN_UNITS_PER_WORD > 4
45 # define LIBGCC2_MAX_UNITS_PER_WORD 8
46 #elif (MIN_UNITS_PER_WORD > 2 \
47 || (MIN_UNITS_PER_WORD > 1 && LONG_LONG_TYPE_SIZE > 32))
48 # define LIBGCC2_MAX_UNITS_PER_WORD 4
49 #else
50 # define LIBGCC2_MAX_UNITS_PER_WORD MIN_UNITS_PER_WORD
51 #endif
53 /* Work out what word size we are using for this compilation.
54 The value can be set on the command line. */
55 #ifndef LIBGCC2_UNITS_PER_WORD
56 #define LIBGCC2_UNITS_PER_WORD LIBGCC2_MAX_UNITS_PER_WORD
57 #endif
59 #if LIBGCC2_UNITS_PER_WORD <= LIBGCC2_MAX_UNITS_PER_WORD
61 #include "libgcc2.h"
63 #ifdef DECLARE_LIBRARY_RENAMES
64 DECLARE_LIBRARY_RENAMES
65 #endif
67 #if defined (L_negdi2)
68 DWtype
69 __negdi2 (DWtype u)
71 const DWunion uu = {.ll = u};
72 const DWunion w = { {.low = -uu.s.low,
73 .high = -uu.s.high - ((UWtype) -uu.s.low > 0) } };
75 return w.ll;
77 #endif
79 #ifdef L_addvsi3
80 Wtype
81 __addvSI3 (Wtype a, Wtype b)
83 const Wtype w = (UWtype) a + (UWtype) b;
85 if (b >= 0 ? w < a : w > a)
86 abort ();
88 return w;
90 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
91 SItype
92 __addvsi3 (SItype a, SItype b)
94 const SItype w = (USItype) a + (USItype) b;
96 if (b >= 0 ? w < a : w > a)
97 abort ();
99 return w;
101 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
102 #endif
104 #ifdef L_addvdi3
105 DWtype
106 __addvDI3 (DWtype a, DWtype b)
108 const DWtype w = (UDWtype) a + (UDWtype) b;
110 if (b >= 0 ? w < a : w > a)
111 abort ();
113 return w;
115 #endif
117 #ifdef L_subvsi3
118 Wtype
119 __subvSI3 (Wtype a, Wtype b)
121 const Wtype w = (UWtype) a - (UWtype) b;
123 if (b >= 0 ? w > a : w < a)
124 abort ();
126 return w;
128 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
129 SItype
130 __subvsi3 (SItype a, SItype b)
132 const SItype w = (USItype) a - (USItype) b;
134 if (b >= 0 ? w > a : w < a)
135 abort ();
137 return w;
139 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
140 #endif
142 #ifdef L_subvdi3
143 DWtype
144 __subvDI3 (DWtype a, DWtype b)
146 const DWtype w = (UDWtype) a - (UDWtype) b;
148 if (b >= 0 ? w > a : w < a)
149 abort ();
151 return w;
153 #endif
155 #ifdef L_mulvsi3
156 Wtype
157 __mulvSI3 (Wtype a, Wtype b)
159 const DWtype w = (DWtype) a * (DWtype) b;
161 if ((Wtype) (w >> W_TYPE_SIZE) != (Wtype) w >> (W_TYPE_SIZE - 1))
162 abort ();
164 return w;
166 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
167 #undef WORD_SIZE
168 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
169 SItype
170 __mulvsi3 (SItype a, SItype b)
172 const DItype w = (DItype) a * (DItype) b;
174 if ((SItype) (w >> WORD_SIZE) != (SItype) w >> (WORD_SIZE-1))
175 abort ();
177 return w;
179 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
180 #endif
182 #ifdef L_negvsi2
183 Wtype
184 __negvSI2 (Wtype a)
186 const Wtype w = -(UWtype) a;
188 if (a >= 0 ? w > 0 : w < 0)
189 abort ();
191 return w;
193 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
194 SItype
195 __negvsi2 (SItype a)
197 const SItype w = -(USItype) a;
199 if (a >= 0 ? w > 0 : w < 0)
200 abort ();
202 return w;
204 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
205 #endif
207 #ifdef L_negvdi2
208 DWtype
209 __negvDI2 (DWtype a)
211 const DWtype w = -(UDWtype) a;
213 if (a >= 0 ? w > 0 : w < 0)
214 abort ();
216 return w;
218 #endif
220 #ifdef L_absvsi2
221 Wtype
222 __absvSI2 (Wtype a)
224 Wtype w = a;
226 if (a < 0)
227 #ifdef L_negvsi2
228 w = __negvSI2 (a);
229 #else
230 w = -(UWtype) a;
232 if (w < 0)
233 abort ();
234 #endif
236 return w;
238 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
239 SItype
240 __absvsi2 (SItype a)
242 SItype w = a;
244 if (a < 0)
245 #ifdef L_negvsi2
246 w = __negvsi2 (a);
247 #else
248 w = -(USItype) a;
250 if (w < 0)
251 abort ();
252 #endif
254 return w;
256 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
257 #endif
259 #ifdef L_absvdi2
260 DWtype
261 __absvDI2 (DWtype a)
263 DWtype w = a;
265 if (a < 0)
266 #ifdef L_negvdi2
267 w = __negvDI2 (a);
268 #else
269 w = -(UDWtype) a;
271 if (w < 0)
272 abort ();
273 #endif
275 return w;
277 #endif
279 #ifdef L_mulvdi3
280 DWtype
281 __mulvDI3 (DWtype u, DWtype v)
283 /* The unchecked multiplication needs 3 Wtype x Wtype multiplications,
284 but the checked multiplication needs only two. */
285 const DWunion uu = {.ll = u};
286 const DWunion vv = {.ll = v};
288 if (__builtin_expect (uu.s.high == uu.s.low >> (W_TYPE_SIZE - 1), 1))
290 /* u fits in a single Wtype. */
291 if (__builtin_expect (vv.s.high == vv.s.low >> (W_TYPE_SIZE - 1), 1))
293 /* v fits in a single Wtype as well. */
294 /* A single multiplication. No overflow risk. */
295 return (DWtype) uu.s.low * (DWtype) vv.s.low;
297 else
299 /* Two multiplications. */
300 DWunion w0 = {.ll = (UDWtype) (UWtype) uu.s.low
301 * (UDWtype) (UWtype) vv.s.low};
302 DWunion w1 = {.ll = (UDWtype) (UWtype) uu.s.low
303 * (UDWtype) (UWtype) vv.s.high};
305 if (vv.s.high < 0)
306 w1.s.high -= uu.s.low;
307 if (uu.s.low < 0)
308 w1.ll -= vv.ll;
309 w1.ll += (UWtype) w0.s.high;
310 if (__builtin_expect (w1.s.high == w1.s.low >> (W_TYPE_SIZE - 1), 1))
312 w0.s.high = w1.s.low;
313 return w0.ll;
317 else
319 if (__builtin_expect (vv.s.high == vv.s.low >> (W_TYPE_SIZE - 1), 1))
321 /* v fits into a single Wtype. */
322 /* Two multiplications. */
323 DWunion w0 = {.ll = (UDWtype) (UWtype) uu.s.low
324 * (UDWtype) (UWtype) vv.s.low};
325 DWunion w1 = {.ll = (UDWtype) (UWtype) uu.s.high
326 * (UDWtype) (UWtype) vv.s.low};
328 if (uu.s.high < 0)
329 w1.s.high -= vv.s.low;
330 if (vv.s.low < 0)
331 w1.ll -= uu.ll;
332 w1.ll += (UWtype) w0.s.high;
333 if (__builtin_expect (w1.s.high == w1.s.low >> (W_TYPE_SIZE - 1), 1))
335 w0.s.high = w1.s.low;
336 return w0.ll;
339 else
341 /* A few sign checks and a single multiplication. */
342 if (uu.s.high >= 0)
344 if (vv.s.high >= 0)
346 if (uu.s.high == 0 && vv.s.high == 0)
348 const DWtype w = (UDWtype) (UWtype) uu.s.low
349 * (UDWtype) (UWtype) vv.s.low;
350 if (__builtin_expect (w >= 0, 1))
351 return w;
354 else
356 if (uu.s.high == 0 && vv.s.high == (Wtype) -1)
358 DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
359 * (UDWtype) (UWtype) vv.s.low};
361 ww.s.high -= uu.s.low;
362 if (__builtin_expect (ww.s.high < 0, 1))
363 return ww.ll;
367 else
369 if (vv.s.high >= 0)
371 if (uu.s.high == (Wtype) -1 && vv.s.high == 0)
373 DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
374 * (UDWtype) (UWtype) vv.s.low};
376 ww.s.high -= vv.s.low;
377 if (__builtin_expect (ww.s.high < 0, 1))
378 return ww.ll;
381 else
383 if (uu.s.high == (Wtype) -1 && vv.s.high == (Wtype) - 1)
385 DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
386 * (UDWtype) (UWtype) vv.s.low};
388 ww.s.high -= uu.s.low;
389 ww.s.high -= vv.s.low;
390 if (__builtin_expect (ww.s.high >= 0, 1))
391 return ww.ll;
398 /* Overflow. */
399 abort ();
401 #endif
404 /* Unless shift functions are defined with full ANSI prototypes,
405 parameter b will be promoted to int if shift_count_type is smaller than an int. */
406 #ifdef L_lshrdi3
407 DWtype
408 __lshrdi3 (DWtype u, shift_count_type b)
410 if (b == 0)
411 return u;
413 const DWunion uu = {.ll = u};
414 const shift_count_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
415 DWunion w;
417 if (bm <= 0)
419 w.s.high = 0;
420 w.s.low = (UWtype) uu.s.high >> -bm;
422 else
424 const UWtype carries = (UWtype) uu.s.high << bm;
426 w.s.high = (UWtype) uu.s.high >> b;
427 w.s.low = ((UWtype) uu.s.low >> b) | carries;
430 return w.ll;
432 #endif
434 #ifdef L_ashldi3
435 DWtype
436 __ashldi3 (DWtype u, shift_count_type b)
438 if (b == 0)
439 return u;
441 const DWunion uu = {.ll = u};
442 const shift_count_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
443 DWunion w;
445 if (bm <= 0)
447 w.s.low = 0;
448 w.s.high = (UWtype) uu.s.low << -bm;
450 else
452 const UWtype carries = (UWtype) uu.s.low >> bm;
454 w.s.low = (UWtype) uu.s.low << b;
455 w.s.high = ((UWtype) uu.s.high << b) | carries;
458 return w.ll;
460 #endif
462 #ifdef L_ashrdi3
463 DWtype
464 __ashrdi3 (DWtype u, shift_count_type b)
466 if (b == 0)
467 return u;
469 const DWunion uu = {.ll = u};
470 const shift_count_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
471 DWunion w;
473 if (bm <= 0)
475 /* w.s.high = 1..1 or 0..0 */
476 w.s.high = uu.s.high >> (sizeof (Wtype) * BITS_PER_UNIT - 1);
477 w.s.low = uu.s.high >> -bm;
479 else
481 const UWtype carries = (UWtype) uu.s.high << bm;
483 w.s.high = uu.s.high >> b;
484 w.s.low = ((UWtype) uu.s.low >> b) | carries;
487 return w.ll;
489 #endif
491 #ifdef L_bswapsi2
492 SItype
493 __bswapsi2 (SItype u)
495 return ((((u) & 0xff000000) >> 24)
496 | (((u) & 0x00ff0000) >> 8)
497 | (((u) & 0x0000ff00) << 8)
498 | (((u) & 0x000000ff) << 24));
500 #endif
501 #ifdef L_bswapdi2
502 DItype
503 __bswapdi2 (DItype u)
505 return ((((u) & 0xff00000000000000ull) >> 56)
506 | (((u) & 0x00ff000000000000ull) >> 40)
507 | (((u) & 0x0000ff0000000000ull) >> 24)
508 | (((u) & 0x000000ff00000000ull) >> 8)
509 | (((u) & 0x00000000ff000000ull) << 8)
510 | (((u) & 0x0000000000ff0000ull) << 24)
511 | (((u) & 0x000000000000ff00ull) << 40)
512 | (((u) & 0x00000000000000ffull) << 56));
514 #endif
515 #ifdef L_ffssi2
516 #undef int
518 __ffsSI2 (UWtype u)
520 UWtype count;
522 if (u == 0)
523 return 0;
525 count_trailing_zeros (count, u);
526 return count + 1;
528 #endif
530 #ifdef L_ffsdi2
531 #undef int
533 __ffsDI2 (DWtype u)
535 const DWunion uu = {.ll = u};
536 UWtype word, count, add;
538 if (uu.s.low != 0)
539 word = uu.s.low, add = 0;
540 else if (uu.s.high != 0)
541 word = uu.s.high, add = BITS_PER_UNIT * sizeof (Wtype);
542 else
543 return 0;
545 count_trailing_zeros (count, word);
546 return count + add + 1;
548 #endif
550 #ifdef L_muldi3
551 DWtype
552 __muldi3 (DWtype u, DWtype v)
554 const DWunion uu = {.ll = u};
555 const DWunion vv = {.ll = v};
556 DWunion w = {.ll = __umulsidi3 (uu.s.low, vv.s.low)};
558 w.s.high += ((UWtype) uu.s.low * (UWtype) vv.s.high
559 + (UWtype) uu.s.high * (UWtype) vv.s.low);
561 return w.ll;
563 #endif
565 #if (defined (L_udivdi3) || defined (L_divdi3) || \
566 defined (L_umoddi3) || defined (L_moddi3))
567 #if defined (sdiv_qrnnd)
568 #define L_udiv_w_sdiv
569 #endif
570 #endif
572 #ifdef L_udiv_w_sdiv
573 #if defined (sdiv_qrnnd)
574 #if (defined (L_udivdi3) || defined (L_divdi3) || \
575 defined (L_umoddi3) || defined (L_moddi3))
576 static inline __attribute__ ((__always_inline__))
577 #endif
578 UWtype
579 __udiv_w_sdiv (UWtype *rp, UWtype a1, UWtype a0, UWtype d)
581 UWtype q, r;
582 UWtype c0, c1, b1;
584 if ((Wtype) d >= 0)
586 if (a1 < d - a1 - (a0 >> (W_TYPE_SIZE - 1)))
588 /* Dividend, divisor, and quotient are nonnegative. */
589 sdiv_qrnnd (q, r, a1, a0, d);
591 else
593 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d. */
594 sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (W_TYPE_SIZE - 1));
595 /* Divide (c1*2^32 + c0) by d. */
596 sdiv_qrnnd (q, r, c1, c0, d);
597 /* Add 2^31 to quotient. */
598 q += (UWtype) 1 << (W_TYPE_SIZE - 1);
601 else
603 b1 = d >> 1; /* d/2, between 2^30 and 2^31 - 1 */
604 c1 = a1 >> 1; /* A/2 */
605 c0 = (a1 << (W_TYPE_SIZE - 1)) + (a0 >> 1);
607 if (a1 < b1) /* A < 2^32*b1, so A/2 < 2^31*b1 */
609 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
611 r = 2*r + (a0 & 1); /* Remainder from A/(2*b1) */
612 if ((d & 1) != 0)
614 if (r >= q)
615 r = r - q;
616 else if (q - r <= d)
618 r = r - q + d;
619 q--;
621 else
623 r = r - q + 2*d;
624 q -= 2;
628 else if (c1 < b1) /* So 2^31 <= (A/2)/b1 < 2^32 */
630 c1 = (b1 - 1) - c1;
631 c0 = ~c0; /* logical NOT */
633 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
635 q = ~q; /* (A/2)/b1 */
636 r = (b1 - 1) - r;
638 r = 2*r + (a0 & 1); /* A/(2*b1) */
640 if ((d & 1) != 0)
642 if (r >= q)
643 r = r - q;
644 else if (q - r <= d)
646 r = r - q + d;
647 q--;
649 else
651 r = r - q + 2*d;
652 q -= 2;
656 else /* Implies c1 = b1 */
657 { /* Hence a1 = d - 1 = 2*b1 - 1 */
658 if (a0 >= -d)
660 q = -1;
661 r = a0 + d;
663 else
665 q = -2;
666 r = a0 + 2*d;
671 *rp = r;
672 return q;
674 #else
675 /* If sdiv_qrnnd doesn't exist, define dummy __udiv_w_sdiv. */
676 UWtype
677 __udiv_w_sdiv (UWtype *rp __attribute__ ((__unused__)),
678 UWtype a1 __attribute__ ((__unused__)),
679 UWtype a0 __attribute__ ((__unused__)),
680 UWtype d __attribute__ ((__unused__)))
682 return 0;
684 #endif
685 #endif
687 #if (defined (L_udivdi3) || defined (L_divdi3) || \
688 defined (L_umoddi3) || defined (L_moddi3))
689 #define L_udivmoddi4
690 #endif
692 #ifdef L_clz
693 const UQItype __clz_tab[256] =
695 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,
696 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,
697 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,
698 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,
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,
700 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 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,
702 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
704 #endif
706 #ifdef L_clzsi2
707 #undef int
709 __clzSI2 (UWtype x)
711 Wtype ret;
713 count_leading_zeros (ret, x);
715 return ret;
717 #endif
719 #ifdef L_clzdi2
720 #undef int
722 __clzDI2 (UDWtype x)
724 const DWunion uu = {.ll = x};
725 UWtype word;
726 Wtype ret, add;
728 if (uu.s.high)
729 word = uu.s.high, add = 0;
730 else
731 word = uu.s.low, add = W_TYPE_SIZE;
733 count_leading_zeros (ret, word);
734 return ret + add;
736 #endif
738 #ifdef L_ctzsi2
739 #undef int
741 __ctzSI2 (UWtype x)
743 Wtype ret;
745 count_trailing_zeros (ret, x);
747 return ret;
749 #endif
751 #ifdef L_ctzdi2
752 #undef int
754 __ctzDI2 (UDWtype x)
756 const DWunion uu = {.ll = x};
757 UWtype word;
758 Wtype ret, add;
760 if (uu.s.low)
761 word = uu.s.low, add = 0;
762 else
763 word = uu.s.high, add = W_TYPE_SIZE;
765 count_trailing_zeros (ret, word);
766 return ret + add;
768 #endif
770 #ifdef L_popcount_tab
771 const UQItype __popcount_tab[256] =
773 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,
774 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,
775 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,
776 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,
777 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,
778 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,
779 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,
780 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
782 #endif
784 #ifdef L_popcountsi2
785 #undef int
787 __popcountSI2 (UWtype x)
789 int i, ret = 0;
791 for (i = 0; i < W_TYPE_SIZE; i += 8)
792 ret += __popcount_tab[(x >> i) & 0xff];
794 return ret;
796 #endif
798 #ifdef L_popcountdi2
799 #undef int
801 __popcountDI2 (UDWtype x)
803 int i, ret = 0;
805 for (i = 0; i < 2*W_TYPE_SIZE; i += 8)
806 ret += __popcount_tab[(x >> i) & 0xff];
808 return ret;
810 #endif
812 #ifdef L_paritysi2
813 #undef int
815 __paritySI2 (UWtype x)
817 #if W_TYPE_SIZE > 64
818 # error "fill out the table"
819 #endif
820 #if W_TYPE_SIZE > 32
821 x ^= x >> 32;
822 #endif
823 #if W_TYPE_SIZE > 16
824 x ^= x >> 16;
825 #endif
826 x ^= x >> 8;
827 x ^= x >> 4;
828 x &= 0xf;
829 return (0x6996 >> x) & 1;
831 #endif
833 #ifdef L_paritydi2
834 #undef int
836 __parityDI2 (UDWtype x)
838 const DWunion uu = {.ll = x};
839 UWtype nx = uu.s.low ^ uu.s.high;
841 #if W_TYPE_SIZE > 64
842 # error "fill out the table"
843 #endif
844 #if W_TYPE_SIZE > 32
845 nx ^= nx >> 32;
846 #endif
847 #if W_TYPE_SIZE > 16
848 nx ^= nx >> 16;
849 #endif
850 nx ^= nx >> 8;
851 nx ^= nx >> 4;
852 nx &= 0xf;
853 return (0x6996 >> nx) & 1;
855 #endif
857 #ifdef L_udivmoddi4
859 #if (defined (L_udivdi3) || defined (L_divdi3) || \
860 defined (L_umoddi3) || defined (L_moddi3))
861 static inline __attribute__ ((__always_inline__))
862 #endif
863 UDWtype
864 __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
866 const DWunion nn = {.ll = n};
867 const DWunion dd = {.ll = d};
868 DWunion rr;
869 UWtype d0, d1, n0, n1, n2;
870 UWtype q0, q1;
871 UWtype b, bm;
873 d0 = dd.s.low;
874 d1 = dd.s.high;
875 n0 = nn.s.low;
876 n1 = nn.s.high;
878 #if !UDIV_NEEDS_NORMALIZATION
879 if (d1 == 0)
881 if (d0 > n1)
883 /* 0q = nn / 0D */
885 udiv_qrnnd (q0, n0, n1, n0, d0);
886 q1 = 0;
888 /* Remainder in n0. */
890 else
892 /* qq = NN / 0d */
894 if (d0 == 0)
895 d0 = 1 / d0; /* Divide intentionally by zero. */
897 udiv_qrnnd (q1, n1, 0, n1, d0);
898 udiv_qrnnd (q0, n0, n1, n0, d0);
900 /* Remainder in n0. */
903 if (rp != 0)
905 rr.s.low = n0;
906 rr.s.high = 0;
907 *rp = rr.ll;
911 #else /* UDIV_NEEDS_NORMALIZATION */
913 if (d1 == 0)
915 if (d0 > n1)
917 /* 0q = nn / 0D */
919 count_leading_zeros (bm, d0);
921 if (bm != 0)
923 /* Normalize, i.e. make the most significant bit of the
924 denominator set. */
926 d0 = d0 << bm;
927 n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm));
928 n0 = n0 << bm;
931 udiv_qrnnd (q0, n0, n1, n0, d0);
932 q1 = 0;
934 /* Remainder in n0 >> bm. */
936 else
938 /* qq = NN / 0d */
940 if (d0 == 0)
941 d0 = 1 / d0; /* Divide intentionally by zero. */
943 count_leading_zeros (bm, d0);
945 if (bm == 0)
947 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
948 conclude (the most significant bit of n1 is set) /\ (the
949 leading quotient digit q1 = 1).
951 This special case is necessary, not an optimization.
952 (Shifts counts of W_TYPE_SIZE are undefined.) */
954 n1 -= d0;
955 q1 = 1;
957 else
959 /* Normalize. */
961 b = W_TYPE_SIZE - bm;
963 d0 = d0 << bm;
964 n2 = n1 >> b;
965 n1 = (n1 << bm) | (n0 >> b);
966 n0 = n0 << bm;
968 udiv_qrnnd (q1, n1, n2, n1, d0);
971 /* n1 != d0... */
973 udiv_qrnnd (q0, n0, n1, n0, d0);
975 /* Remainder in n0 >> bm. */
978 if (rp != 0)
980 rr.s.low = n0 >> bm;
981 rr.s.high = 0;
982 *rp = rr.ll;
985 #endif /* UDIV_NEEDS_NORMALIZATION */
987 else
989 if (d1 > n1)
991 /* 00 = nn / DD */
993 q0 = 0;
994 q1 = 0;
996 /* Remainder in n1n0. */
997 if (rp != 0)
999 rr.s.low = n0;
1000 rr.s.high = n1;
1001 *rp = rr.ll;
1004 else
1006 /* 0q = NN / dd */
1008 count_leading_zeros (bm, d1);
1009 if (bm == 0)
1011 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
1012 conclude (the most significant bit of n1 is set) /\ (the
1013 quotient digit q0 = 0 or 1).
1015 This special case is necessary, not an optimization. */
1017 /* The condition on the next line takes advantage of that
1018 n1 >= d1 (true due to program flow). */
1019 if (n1 > d1 || n0 >= d0)
1021 q0 = 1;
1022 sub_ddmmss (n1, n0, n1, n0, d1, d0);
1024 else
1025 q0 = 0;
1027 q1 = 0;
1029 if (rp != 0)
1031 rr.s.low = n0;
1032 rr.s.high = n1;
1033 *rp = rr.ll;
1036 else
1038 UWtype m1, m0;
1039 /* Normalize. */
1041 b = W_TYPE_SIZE - bm;
1043 d1 = (d1 << bm) | (d0 >> b);
1044 d0 = d0 << bm;
1045 n2 = n1 >> b;
1046 n1 = (n1 << bm) | (n0 >> b);
1047 n0 = n0 << bm;
1049 udiv_qrnnd (q0, n1, n2, n1, d1);
1050 umul_ppmm (m1, m0, q0, d0);
1052 if (m1 > n1 || (m1 == n1 && m0 > n0))
1054 q0--;
1055 sub_ddmmss (m1, m0, m1, m0, d1, d0);
1058 q1 = 0;
1060 /* Remainder in (n1n0 - m1m0) >> bm. */
1061 if (rp != 0)
1063 sub_ddmmss (n1, n0, n1, n0, m1, m0);
1064 rr.s.low = (n1 << b) | (n0 >> bm);
1065 rr.s.high = n1 >> bm;
1066 *rp = rr.ll;
1072 const DWunion ww = {{.low = q0, .high = q1}};
1073 return ww.ll;
1075 #endif
1077 #ifdef L_divdi3
1078 DWtype
1079 __divdi3 (DWtype u, DWtype v)
1081 Wtype c = 0;
1082 DWunion uu = {.ll = u};
1083 DWunion vv = {.ll = v};
1084 DWtype w;
1086 if (uu.s.high < 0)
1087 c = ~c,
1088 uu.ll = -uu.ll;
1089 if (vv.s.high < 0)
1090 c = ~c,
1091 vv.ll = -vv.ll;
1093 w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0);
1094 if (c)
1095 w = -w;
1097 return w;
1099 #endif
1101 #ifdef L_moddi3
1102 DWtype
1103 __moddi3 (DWtype u, DWtype v)
1105 Wtype c = 0;
1106 DWunion uu = {.ll = u};
1107 DWunion vv = {.ll = v};
1108 DWtype w;
1110 if (uu.s.high < 0)
1111 c = ~c,
1112 uu.ll = -uu.ll;
1113 if (vv.s.high < 0)
1114 vv.ll = -vv.ll;
1116 (void) __udivmoddi4 (uu.ll, vv.ll, (UDWtype*)&w);
1117 if (c)
1118 w = -w;
1120 return w;
1122 #endif
1124 #ifdef L_umoddi3
1125 UDWtype
1126 __umoddi3 (UDWtype u, UDWtype v)
1128 UDWtype w;
1130 (void) __udivmoddi4 (u, v, &w);
1132 return w;
1134 #endif
1136 #ifdef L_udivdi3
1137 UDWtype
1138 __udivdi3 (UDWtype n, UDWtype d)
1140 return __udivmoddi4 (n, d, (UDWtype *) 0);
1142 #endif
1144 #ifdef L_cmpdi2
1145 cmp_return_type
1146 __cmpdi2 (DWtype a, DWtype b)
1148 const DWunion au = {.ll = a};
1149 const DWunion bu = {.ll = b};
1151 if (au.s.high < bu.s.high)
1152 return 0;
1153 else if (au.s.high > bu.s.high)
1154 return 2;
1155 if ((UWtype) au.s.low < (UWtype) bu.s.low)
1156 return 0;
1157 else if ((UWtype) au.s.low > (UWtype) bu.s.low)
1158 return 2;
1159 return 1;
1161 #endif
1163 #ifdef L_ucmpdi2
1164 cmp_return_type
1165 __ucmpdi2 (DWtype a, DWtype b)
1167 const DWunion au = {.ll = a};
1168 const DWunion bu = {.ll = b};
1170 if ((UWtype) au.s.high < (UWtype) bu.s.high)
1171 return 0;
1172 else if ((UWtype) au.s.high > (UWtype) bu.s.high)
1173 return 2;
1174 if ((UWtype) au.s.low < (UWtype) bu.s.low)
1175 return 0;
1176 else if ((UWtype) au.s.low > (UWtype) bu.s.low)
1177 return 2;
1178 return 1;
1180 #endif
1182 #if defined(L_fixunstfdi) && LIBGCC2_HAS_TF_MODE
1183 UDWtype
1184 __fixunstfDI (TFtype a)
1186 if (a < 0)
1187 return 0;
1189 /* Compute high word of result, as a flonum. */
1190 const TFtype b = (a / Wtype_MAXp1_F);
1191 /* Convert that to fixed (but not to DWtype!),
1192 and shift it into the high word. */
1193 UDWtype v = (UWtype) b;
1194 v <<= W_TYPE_SIZE;
1195 /* Remove high part from the TFtype, leaving the low part as flonum. */
1196 a -= (TFtype)v;
1197 /* Convert that to fixed (but not to DWtype!) and add it in.
1198 Sometimes A comes out negative. This is significant, since
1199 A has more bits than a long int does. */
1200 if (a < 0)
1201 v -= (UWtype) (- a);
1202 else
1203 v += (UWtype) a;
1204 return v;
1206 #endif
1208 #if defined(L_fixtfdi) && LIBGCC2_HAS_TF_MODE
1209 DWtype
1210 __fixtfdi (TFtype a)
1212 if (a < 0)
1213 return - __fixunstfDI (-a);
1214 return __fixunstfDI (a);
1216 #endif
1218 #if defined(L_fixunsxfdi) && LIBGCC2_HAS_XF_MODE
1219 UDWtype
1220 __fixunsxfDI (XFtype a)
1222 if (a < 0)
1223 return 0;
1225 /* Compute high word of result, as a flonum. */
1226 const XFtype b = (a / Wtype_MAXp1_F);
1227 /* Convert that to fixed (but not to DWtype!),
1228 and shift it into the high word. */
1229 UDWtype v = (UWtype) b;
1230 v <<= W_TYPE_SIZE;
1231 /* Remove high part from the XFtype, leaving the low part as flonum. */
1232 a -= (XFtype)v;
1233 /* Convert that to fixed (but not to DWtype!) and add it in.
1234 Sometimes A comes out negative. This is significant, since
1235 A has more bits than a long int does. */
1236 if (a < 0)
1237 v -= (UWtype) (- a);
1238 else
1239 v += (UWtype) a;
1240 return v;
1242 #endif
1244 #if defined(L_fixxfdi) && LIBGCC2_HAS_XF_MODE
1245 DWtype
1246 __fixxfdi (XFtype a)
1248 if (a < 0)
1249 return - __fixunsxfDI (-a);
1250 return __fixunsxfDI (a);
1252 #endif
1254 #if defined(L_fixunsdfdi) && LIBGCC2_HAS_DF_MODE
1255 UDWtype
1256 __fixunsdfDI (DFtype a)
1258 /* Get high part of result. The division here will just moves the radix
1259 point and will not cause any rounding. Then the conversion to integral
1260 type chops result as desired. */
1261 const UWtype hi = a / Wtype_MAXp1_F;
1263 /* Get low part of result. Convert `hi' to floating type and scale it back,
1264 then subtract this from the number being converted. This leaves the low
1265 part. Convert that to integral type. */
1266 const UWtype lo = a - (DFtype) hi * Wtype_MAXp1_F;
1268 /* Assemble result from the two parts. */
1269 return ((UDWtype) hi << W_TYPE_SIZE) | lo;
1271 #endif
1273 #if defined(L_fixdfdi) && LIBGCC2_HAS_DF_MODE
1274 DWtype
1275 __fixdfdi (DFtype a)
1277 if (a < 0)
1278 return - __fixunsdfDI (-a);
1279 return __fixunsdfDI (a);
1281 #endif
1283 #if defined(L_fixunssfdi) && LIBGCC2_HAS_SF_MODE
1284 UDWtype
1285 __fixunssfDI (SFtype a)
1287 #if LIBGCC2_HAS_DF_MODE
1288 /* Convert the SFtype to a DFtype, because that is surely not going
1289 to lose any bits. Some day someone else can write a faster version
1290 that avoids converting to DFtype, and verify it really works right. */
1291 const DFtype dfa = a;
1293 /* Get high part of result. The division here will just moves the radix
1294 point and will not cause any rounding. Then the conversion to integral
1295 type chops result as desired. */
1296 const UWtype hi = dfa / Wtype_MAXp1_F;
1298 /* Get low part of result. Convert `hi' to floating type and scale it back,
1299 then subtract this from the number being converted. This leaves the low
1300 part. Convert that to integral type. */
1301 const UWtype lo = dfa - (DFtype) hi * Wtype_MAXp1_F;
1303 /* Assemble result from the two parts. */
1304 return ((UDWtype) hi << W_TYPE_SIZE) | lo;
1305 #elif FLT_MANT_DIG < W_TYPE_SIZE
1306 if (a < 1)
1307 return 0;
1308 if (a < Wtype_MAXp1_F)
1309 return (UWtype)a;
1310 if (a < Wtype_MAXp1_F * Wtype_MAXp1_F)
1312 /* Since we know that there are fewer significant bits in the SFmode
1313 quantity than in a word, we know that we can convert out all the
1314 significant bits in one step, and thus avoid losing bits. */
1316 /* ??? This following loop essentially performs frexpf. If we could
1317 use the real libm function, or poke at the actual bits of the fp
1318 format, it would be significantly faster. */
1320 UWtype shift = 0, counter;
1321 SFtype msb;
1323 a /= Wtype_MAXp1_F;
1324 for (counter = W_TYPE_SIZE / 2; counter != 0; counter >>= 1)
1326 SFtype counterf = (UWtype)1 << counter;
1327 if (a >= counterf)
1329 shift |= counter;
1330 a /= counterf;
1334 /* Rescale into the range of one word, extract the bits of that
1335 one word, and shift the result into position. */
1336 a *= Wtype_MAXp1_F;
1337 counter = a;
1338 return (DWtype)counter << shift;
1340 return -1;
1341 #else
1342 # error
1343 #endif
1345 #endif
1347 #if defined(L_fixsfdi) && LIBGCC2_HAS_SF_MODE
1348 DWtype
1349 __fixsfdi (SFtype a)
1351 if (a < 0)
1352 return - __fixunssfDI (-a);
1353 return __fixunssfDI (a);
1355 #endif
1357 #if defined(L_floatdixf) && LIBGCC2_HAS_XF_MODE
1358 XFtype
1359 __floatdixf (DWtype u)
1361 #if W_TYPE_SIZE > XF_SIZE
1362 # error
1363 #endif
1364 XFtype d = (Wtype) (u >> W_TYPE_SIZE);
1365 d *= Wtype_MAXp1_F;
1366 d += (UWtype)u;
1367 return d;
1369 #endif
1371 #if defined(L_floatundixf) && LIBGCC2_HAS_XF_MODE
1372 XFtype
1373 __floatundixf (UDWtype u)
1375 #if W_TYPE_SIZE > XF_SIZE
1376 # error
1377 #endif
1378 XFtype d = (UWtype) (u >> W_TYPE_SIZE);
1379 d *= Wtype_MAXp1_F;
1380 d += (UWtype)u;
1381 return d;
1383 #endif
1385 #if defined(L_floatditf) && LIBGCC2_HAS_TF_MODE
1386 TFtype
1387 __floatditf (DWtype u)
1389 #if W_TYPE_SIZE > TF_SIZE
1390 # error
1391 #endif
1392 TFtype d = (Wtype) (u >> W_TYPE_SIZE);
1393 d *= Wtype_MAXp1_F;
1394 d += (UWtype)u;
1395 return d;
1397 #endif
1399 #if defined(L_floatunditf) && LIBGCC2_HAS_TF_MODE
1400 TFtype
1401 __floatunditf (UDWtype u)
1403 #if W_TYPE_SIZE > TF_SIZE
1404 # error
1405 #endif
1406 TFtype d = (UWtype) (u >> W_TYPE_SIZE);
1407 d *= Wtype_MAXp1_F;
1408 d += (UWtype)u;
1409 return d;
1411 #endif
1413 #if (defined(L_floatdisf) && LIBGCC2_HAS_SF_MODE) \
1414 || (defined(L_floatdidf) && LIBGCC2_HAS_DF_MODE)
1415 #define DI_SIZE (W_TYPE_SIZE * 2)
1416 #define F_MODE_OK(SIZE) \
1417 (SIZE < DI_SIZE \
1418 && SIZE > (DI_SIZE - SIZE + FSSIZE) \
1419 && !AVOID_FP_TYPE_CONVERSION(SIZE))
1420 #if defined(L_floatdisf)
1421 #define FUNC __floatdisf
1422 #define FSTYPE SFtype
1423 #define FSSIZE SF_SIZE
1424 #else
1425 #define FUNC __floatdidf
1426 #define FSTYPE DFtype
1427 #define FSSIZE DF_SIZE
1428 #endif
1430 FSTYPE
1431 FUNC (DWtype u)
1433 #if FSSIZE >= W_TYPE_SIZE
1434 /* When the word size is small, we never get any rounding error. */
1435 FSTYPE f = (Wtype) (u >> W_TYPE_SIZE);
1436 f *= Wtype_MAXp1_F;
1437 f += (UWtype)u;
1438 return f;
1439 #elif (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE)) \
1440 || (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE)) \
1441 || (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1443 #if (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE))
1444 # define FSIZE DF_SIZE
1445 # define FTYPE DFtype
1446 #elif (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE))
1447 # define FSIZE XF_SIZE
1448 # define FTYPE XFtype
1449 #elif (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1450 # define FSIZE TF_SIZE
1451 # define FTYPE TFtype
1452 #else
1453 # error
1454 #endif
1456 #define REP_BIT ((UDWtype) 1 << (DI_SIZE - FSIZE))
1458 /* Protect against double-rounding error.
1459 Represent any low-order bits, that might be truncated by a bit that
1460 won't be lost. The bit can go in anywhere below the rounding position
1461 of the FSTYPE. A fixed mask and bit position handles all usual
1462 configurations. */
1463 if (! (- ((DWtype) 1 << FSIZE) < u
1464 && u < ((DWtype) 1 << FSIZE)))
1466 if ((UDWtype) u & (REP_BIT - 1))
1468 u &= ~ (REP_BIT - 1);
1469 u |= REP_BIT;
1473 /* Do the calculation in a wider type so that we don't lose any of
1474 the precision of the high word while multiplying it. */
1475 FTYPE f = (Wtype) (u >> W_TYPE_SIZE);
1476 f *= Wtype_MAXp1_F;
1477 f += (UWtype)u;
1478 return (FSTYPE) f;
1479 #else
1480 #if FSSIZE >= W_TYPE_SIZE - 2
1481 # error
1482 #endif
1483 /* Finally, the word size is larger than the number of bits in the
1484 required FSTYPE, and we've got no suitable wider type. The only
1485 way to avoid double rounding is to special case the
1486 extraction. */
1488 /* If there are no high bits set, fall back to one conversion. */
1489 if ((Wtype)u == u)
1490 return (FSTYPE)(Wtype)u;
1492 /* Otherwise, find the power of two. */
1493 Wtype hi = u >> W_TYPE_SIZE;
1494 if (hi < 0)
1495 hi = -hi;
1497 UWtype count, shift;
1498 count_leading_zeros (count, hi);
1500 /* No leading bits means u == minimum. */
1501 if (count == 0)
1502 return -(Wtype_MAXp1_F * (Wtype_MAXp1_F / 2));
1504 shift = 1 + W_TYPE_SIZE - count;
1506 /* Shift down the most significant bits. */
1507 hi = u >> shift;
1509 /* If we lost any nonzero bits, set the lsb to ensure correct rounding. */
1510 if ((UWtype)u << (W_TYPE_SIZE - shift))
1511 hi |= 1;
1513 /* Convert the one word of data, and rescale. */
1514 FSTYPE f = hi, e;
1515 if (shift == W_TYPE_SIZE)
1516 e = Wtype_MAXp1_F;
1517 /* The following two cases could be merged if we knew that the target
1518 supported a native unsigned->float conversion. More often, we only
1519 have a signed conversion, and have to add extra fixup code. */
1520 else if (shift == W_TYPE_SIZE - 1)
1521 e = Wtype_MAXp1_F / 2;
1522 else
1523 e = (Wtype)1 << shift;
1524 return f * e;
1525 #endif
1527 #endif
1529 #if (defined(L_floatundisf) && LIBGCC2_HAS_SF_MODE) \
1530 || (defined(L_floatundidf) && LIBGCC2_HAS_DF_MODE)
1531 #define DI_SIZE (W_TYPE_SIZE * 2)
1532 #define F_MODE_OK(SIZE) \
1533 (SIZE < DI_SIZE \
1534 && SIZE > (DI_SIZE - SIZE + FSSIZE) \
1535 && !AVOID_FP_TYPE_CONVERSION(SIZE))
1536 #if defined(L_floatundisf)
1537 #define FUNC __floatundisf
1538 #define FSTYPE SFtype
1539 #define FSSIZE SF_SIZE
1540 #else
1541 #define FUNC __floatundidf
1542 #define FSTYPE DFtype
1543 #define FSSIZE DF_SIZE
1544 #endif
1546 FSTYPE
1547 FUNC (UDWtype u)
1549 #if FSSIZE >= W_TYPE_SIZE
1550 /* When the word size is small, we never get any rounding error. */
1551 FSTYPE f = (UWtype) (u >> W_TYPE_SIZE);
1552 f *= Wtype_MAXp1_F;
1553 f += (UWtype)u;
1554 return f;
1555 #elif (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE)) \
1556 || (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE)) \
1557 || (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1559 #if (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE))
1560 # define FSIZE DF_SIZE
1561 # define FTYPE DFtype
1562 #elif (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE))
1563 # define FSIZE XF_SIZE
1564 # define FTYPE XFtype
1565 #elif (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1566 # define FSIZE TF_SIZE
1567 # define FTYPE TFtype
1568 #else
1569 # error
1570 #endif
1572 #define REP_BIT ((UDWtype) 1 << (DI_SIZE - FSIZE))
1574 /* Protect against double-rounding error.
1575 Represent any low-order bits, that might be truncated by a bit that
1576 won't be lost. The bit can go in anywhere below the rounding position
1577 of the FSTYPE. A fixed mask and bit position handles all usual
1578 configurations. */
1579 if (u >= ((UDWtype) 1 << FSIZE))
1581 if ((UDWtype) u & (REP_BIT - 1))
1583 u &= ~ (REP_BIT - 1);
1584 u |= REP_BIT;
1588 /* Do the calculation in a wider type so that we don't lose any of
1589 the precision of the high word while multiplying it. */
1590 FTYPE f = (UWtype) (u >> W_TYPE_SIZE);
1591 f *= Wtype_MAXp1_F;
1592 f += (UWtype)u;
1593 return (FSTYPE) f;
1594 #else
1595 #if FSSIZE == W_TYPE_SIZE - 1
1596 # error
1597 #endif
1598 /* Finally, the word size is larger than the number of bits in the
1599 required FSTYPE, and we've got no suitable wider type. The only
1600 way to avoid double rounding is to special case the
1601 extraction. */
1603 /* If there are no high bits set, fall back to one conversion. */
1604 if ((UWtype)u == u)
1605 return (FSTYPE)(UWtype)u;
1607 /* Otherwise, find the power of two. */
1608 UWtype hi = u >> W_TYPE_SIZE;
1610 UWtype count, shift;
1611 count_leading_zeros (count, hi);
1613 shift = W_TYPE_SIZE - count;
1615 /* Shift down the most significant bits. */
1616 hi = u >> shift;
1618 /* If we lost any nonzero bits, set the lsb to ensure correct rounding. */
1619 if ((UWtype)u << (W_TYPE_SIZE - shift))
1620 hi |= 1;
1622 /* Convert the one word of data, and rescale. */
1623 FSTYPE f = hi, e;
1624 if (shift == W_TYPE_SIZE)
1625 e = Wtype_MAXp1_F;
1626 /* The following two cases could be merged if we knew that the target
1627 supported a native unsigned->float conversion. More often, we only
1628 have a signed conversion, and have to add extra fixup code. */
1629 else if (shift == W_TYPE_SIZE - 1)
1630 e = Wtype_MAXp1_F / 2;
1631 else
1632 e = (Wtype)1 << shift;
1633 return f * e;
1634 #endif
1636 #endif
1638 #if defined(L_fixunsxfsi) && LIBGCC2_HAS_XF_MODE
1639 /* Reenable the normal types, in case limits.h needs them. */
1640 #undef char
1641 #undef short
1642 #undef int
1643 #undef long
1644 #undef unsigned
1645 #undef float
1646 #undef double
1647 #undef MIN
1648 #undef MAX
1649 #include <limits.h>
1651 UWtype
1652 __fixunsxfSI (XFtype a)
1654 if (a >= - (DFtype) Wtype_MIN)
1655 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1656 return (Wtype) a;
1658 #endif
1660 #if defined(L_fixunsdfsi) && LIBGCC2_HAS_DF_MODE
1661 /* Reenable the normal types, in case limits.h needs them. */
1662 #undef char
1663 #undef short
1664 #undef int
1665 #undef long
1666 #undef unsigned
1667 #undef float
1668 #undef double
1669 #undef MIN
1670 #undef MAX
1671 #include <limits.h>
1673 UWtype
1674 __fixunsdfSI (DFtype a)
1676 if (a >= - (DFtype) Wtype_MIN)
1677 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1678 return (Wtype) a;
1680 #endif
1682 #if defined(L_fixunssfsi) && LIBGCC2_HAS_SF_MODE
1683 /* Reenable the normal types, in case limits.h needs them. */
1684 #undef char
1685 #undef short
1686 #undef int
1687 #undef long
1688 #undef unsigned
1689 #undef float
1690 #undef double
1691 #undef MIN
1692 #undef MAX
1693 #include <limits.h>
1695 UWtype
1696 __fixunssfSI (SFtype a)
1698 if (a >= - (SFtype) Wtype_MIN)
1699 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1700 return (Wtype) a;
1702 #endif
1704 /* Integer power helper used from __builtin_powi for non-constant
1705 exponents. */
1707 #if (defined(L_powisf2) && LIBGCC2_HAS_SF_MODE) \
1708 || (defined(L_powidf2) && LIBGCC2_HAS_DF_MODE) \
1709 || (defined(L_powixf2) && LIBGCC2_HAS_XF_MODE) \
1710 || (defined(L_powitf2) && LIBGCC2_HAS_TF_MODE)
1711 # if defined(L_powisf2)
1712 # define TYPE SFtype
1713 # define NAME __powisf2
1714 # elif defined(L_powidf2)
1715 # define TYPE DFtype
1716 # define NAME __powidf2
1717 # elif defined(L_powixf2)
1718 # define TYPE XFtype
1719 # define NAME __powixf2
1720 # elif defined(L_powitf2)
1721 # define TYPE TFtype
1722 # define NAME __powitf2
1723 # endif
1725 #undef int
1726 #undef unsigned
1727 TYPE
1728 NAME (TYPE x, int m)
1730 unsigned int n = m < 0 ? -m : m;
1731 TYPE y = n % 2 ? x : 1;
1732 while (n >>= 1)
1734 x = x * x;
1735 if (n % 2)
1736 y = y * x;
1738 return m < 0 ? 1/y : y;
1741 #endif
1743 #if ((defined(L_mulsc3) || defined(L_divsc3)) && LIBGCC2_HAS_SF_MODE) \
1744 || ((defined(L_muldc3) || defined(L_divdc3)) && LIBGCC2_HAS_DF_MODE) \
1745 || ((defined(L_mulxc3) || defined(L_divxc3)) && LIBGCC2_HAS_XF_MODE) \
1746 || ((defined(L_multc3) || defined(L_divtc3)) && LIBGCC2_HAS_TF_MODE)
1748 #undef float
1749 #undef double
1750 #undef long
1752 #if defined(L_mulsc3) || defined(L_divsc3)
1753 # define MTYPE SFtype
1754 # define CTYPE SCtype
1755 # define MODE sc
1756 # define CEXT f
1757 # define NOTRUNC __FLT_EVAL_METHOD__ == 0
1758 #elif defined(L_muldc3) || defined(L_divdc3)
1759 # define MTYPE DFtype
1760 # define CTYPE DCtype
1761 # define MODE dc
1762 # if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 64
1763 # define CEXT l
1764 # define NOTRUNC 1
1765 # else
1766 # define CEXT
1767 # define NOTRUNC __FLT_EVAL_METHOD__ == 0 || __FLT_EVAL_METHOD__ == 1
1768 # endif
1769 #elif defined(L_mulxc3) || defined(L_divxc3)
1770 # define MTYPE XFtype
1771 # define CTYPE XCtype
1772 # define MODE xc
1773 # define CEXT l
1774 # define NOTRUNC 1
1775 #elif defined(L_multc3) || defined(L_divtc3)
1776 # define MTYPE TFtype
1777 # define CTYPE TCtype
1778 # define MODE tc
1779 # if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128
1780 # define CEXT l
1781 # else
1782 # define CEXT LIBGCC2_TF_CEXT
1783 # endif
1784 # define NOTRUNC 1
1785 #else
1786 # error
1787 #endif
1789 #define CONCAT3(A,B,C) _CONCAT3(A,B,C)
1790 #define _CONCAT3(A,B,C) A##B##C
1792 #define CONCAT2(A,B) _CONCAT2(A,B)
1793 #define _CONCAT2(A,B) A##B
1795 /* All of these would be present in a full C99 implementation of <math.h>
1796 and <complex.h>. Our problem is that only a few systems have such full
1797 implementations. Further, libgcc_s.so isn't currently linked against
1798 libm.so, and even for systems that do provide full C99, the extra overhead
1799 of all programs using libgcc having to link against libm. So avoid it. */
1801 #define isnan(x) __builtin_expect ((x) != (x), 0)
1802 #define isfinite(x) __builtin_expect (!isnan((x) - (x)), 1)
1803 #define isinf(x) __builtin_expect (!isnan(x) & !isfinite(x), 0)
1805 #define INFINITY CONCAT2(__builtin_huge_val, CEXT) ()
1806 #define I 1i
1808 /* Helpers to make the following code slightly less gross. */
1809 #define COPYSIGN CONCAT2(__builtin_copysign, CEXT)
1810 #define FABS CONCAT2(__builtin_fabs, CEXT)
1812 /* Verify that MTYPE matches up with CEXT. */
1813 extern void *compile_type_assert[sizeof(INFINITY) == sizeof(MTYPE) ? 1 : -1];
1815 /* Ensure that we've lost any extra precision. */
1816 #if NOTRUNC
1817 # define TRUNC(x)
1818 #else
1819 # define TRUNC(x) __asm__ ("" : "=m"(x) : "m"(x))
1820 #endif
1822 #if defined(L_mulsc3) || defined(L_muldc3) \
1823 || defined(L_mulxc3) || defined(L_multc3)
1825 CTYPE
1826 CONCAT3(__mul,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
1828 MTYPE ac, bd, ad, bc, x, y;
1829 CTYPE res;
1831 ac = a * c;
1832 bd = b * d;
1833 ad = a * d;
1834 bc = b * c;
1836 TRUNC (ac);
1837 TRUNC (bd);
1838 TRUNC (ad);
1839 TRUNC (bc);
1841 x = ac - bd;
1842 y = ad + bc;
1844 if (isnan (x) && isnan (y))
1846 /* Recover infinities that computed as NaN + iNaN. */
1847 _Bool recalc = 0;
1848 if (isinf (a) || isinf (b))
1850 /* z is infinite. "Box" the infinity and change NaNs in
1851 the other factor to 0. */
1852 a = COPYSIGN (isinf (a) ? 1 : 0, a);
1853 b = COPYSIGN (isinf (b) ? 1 : 0, b);
1854 if (isnan (c)) c = COPYSIGN (0, c);
1855 if (isnan (d)) d = COPYSIGN (0, d);
1856 recalc = 1;
1858 if (isinf (c) || isinf (d))
1860 /* w is infinite. "Box" the infinity and change NaNs in
1861 the other factor to 0. */
1862 c = COPYSIGN (isinf (c) ? 1 : 0, c);
1863 d = COPYSIGN (isinf (d) ? 1 : 0, d);
1864 if (isnan (a)) a = COPYSIGN (0, a);
1865 if (isnan (b)) b = COPYSIGN (0, b);
1866 recalc = 1;
1868 if (!recalc
1869 && (isinf (ac) || isinf (bd)
1870 || isinf (ad) || isinf (bc)))
1872 /* Recover infinities from overflow by changing NaNs to 0. */
1873 if (isnan (a)) a = COPYSIGN (0, a);
1874 if (isnan (b)) b = COPYSIGN (0, b);
1875 if (isnan (c)) c = COPYSIGN (0, c);
1876 if (isnan (d)) d = COPYSIGN (0, d);
1877 recalc = 1;
1879 if (recalc)
1881 x = INFINITY * (a * c - b * d);
1882 y = INFINITY * (a * d + b * c);
1886 __real__ res = x;
1887 __imag__ res = y;
1888 return res;
1890 #endif /* complex multiply */
1892 #if defined(L_divsc3) || defined(L_divdc3) \
1893 || defined(L_divxc3) || defined(L_divtc3)
1895 CTYPE
1896 CONCAT3(__div,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
1898 MTYPE denom, ratio, x, y;
1899 CTYPE res;
1901 /* ??? We can get better behavior from logarithmic scaling instead of
1902 the division. But that would mean starting to link libgcc against
1903 libm. We could implement something akin to ldexp/frexp as gcc builtins
1904 fairly easily... */
1905 if (FABS (c) < FABS (d))
1907 ratio = c / d;
1908 denom = (c * ratio) + d;
1909 x = ((a * ratio) + b) / denom;
1910 y = ((b * ratio) - a) / denom;
1912 else
1914 ratio = d / c;
1915 denom = (d * ratio) + c;
1916 x = ((b * ratio) + a) / denom;
1917 y = (b - (a * ratio)) / denom;
1920 /* Recover infinities and zeros that computed as NaN+iNaN; the only cases
1921 are nonzero/zero, infinite/finite, and finite/infinite. */
1922 if (isnan (x) && isnan (y))
1924 if (c == 0.0 && d == 0.0 && (!isnan (a) || !isnan (b)))
1926 x = COPYSIGN (INFINITY, c) * a;
1927 y = COPYSIGN (INFINITY, c) * b;
1929 else if ((isinf (a) || isinf (b)) && isfinite (c) && isfinite (d))
1931 a = COPYSIGN (isinf (a) ? 1 : 0, a);
1932 b = COPYSIGN (isinf (b) ? 1 : 0, b);
1933 x = INFINITY * (a * c + b * d);
1934 y = INFINITY * (b * c - a * d);
1936 else if ((isinf (c) || isinf (d)) && isfinite (a) && isfinite (b))
1938 c = COPYSIGN (isinf (c) ? 1 : 0, c);
1939 d = COPYSIGN (isinf (d) ? 1 : 0, d);
1940 x = 0.0 * (a * c + b * d);
1941 y = 0.0 * (b * c - a * d);
1945 __real__ res = x;
1946 __imag__ res = y;
1947 return res;
1949 #endif /* complex divide */
1951 #endif /* all complex float routines */
1953 /* From here on down, the routines use normal data types. */
1955 #define SItype bogus_type
1956 #define USItype bogus_type
1957 #define DItype bogus_type
1958 #define UDItype bogus_type
1959 #define SFtype bogus_type
1960 #define DFtype bogus_type
1961 #undef Wtype
1962 #undef UWtype
1963 #undef HWtype
1964 #undef UHWtype
1965 #undef DWtype
1966 #undef UDWtype
1968 #undef char
1969 #undef short
1970 #undef int
1971 #undef long
1972 #undef unsigned
1973 #undef float
1974 #undef double
1976 #ifdef L__gcc_bcmp
1978 /* Like bcmp except the sign is meaningful.
1979 Result is negative if S1 is less than S2,
1980 positive if S1 is greater, 0 if S1 and S2 are equal. */
1983 __gcc_bcmp (const unsigned char *s1, const unsigned char *s2, size_t size)
1985 while (size > 0)
1987 const unsigned char c1 = *s1++, c2 = *s2++;
1988 if (c1 != c2)
1989 return c1 - c2;
1990 size--;
1992 return 0;
1995 #endif
1997 /* __eprintf used to be used by GCC's private version of <assert.h>.
1998 We no longer provide that header, but this routine remains in libgcc.a
1999 for binary backward compatibility. Note that it is not included in
2000 the shared version of libgcc. */
2001 #ifdef L_eprintf
2002 #ifndef inhibit_libc
2004 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
2005 #include <stdio.h>
2007 void
2008 __eprintf (const char *string, const char *expression,
2009 unsigned int line, const char *filename)
2011 fprintf (stderr, string, expression, line, filename);
2012 fflush (stderr);
2013 abort ();
2016 #endif
2017 #endif
2020 #ifdef L_clear_cache
2021 /* Clear part of an instruction cache. */
2023 void
2024 __clear_cache (char *beg __attribute__((__unused__)),
2025 char *end __attribute__((__unused__)))
2027 #ifdef CLEAR_INSN_CACHE
2028 CLEAR_INSN_CACHE (beg, end);
2029 #endif /* CLEAR_INSN_CACHE */
2032 #endif /* L_clear_cache */
2034 #ifdef L_enable_execute_stack
2035 /* Attempt to turn on execute permission for the stack. */
2037 #ifdef ENABLE_EXECUTE_STACK
2038 ENABLE_EXECUTE_STACK
2039 #else
2040 void
2041 __enable_execute_stack (void *addr __attribute__((__unused__)))
2043 #endif /* ENABLE_EXECUTE_STACK */
2045 #endif /* L_enable_execute_stack */
2047 #ifdef L_trampoline
2049 /* Jump to a trampoline, loading the static chain address. */
2051 #if defined(WINNT) && ! defined(__CYGWIN__)
2054 getpagesize (void)
2056 #ifdef _ALPHA_
2057 return 8192;
2058 #else
2059 return 4096;
2060 #endif
2064 mprotect (char *addr, int len, int prot)
2066 DWORD np, op;
2068 if (prot == 7)
2069 np = 0x40;
2070 else if (prot == 5)
2071 np = 0x20;
2072 else if (prot == 4)
2073 np = 0x10;
2074 else if (prot == 3)
2075 np = 0x04;
2076 else if (prot == 1)
2077 np = 0x02;
2078 else if (prot == 0)
2079 np = 0x01;
2080 else
2081 return -1;
2083 if (VirtualProtect (addr, len, np, &op))
2084 return 0;
2085 else
2086 return -1;
2089 #endif /* WINNT && ! __CYGWIN__ */
2091 #ifdef TRANSFER_FROM_TRAMPOLINE
2092 TRANSFER_FROM_TRAMPOLINE
2093 #endif
2094 #endif /* L_trampoline */
2096 #ifndef __CYGWIN__
2097 #ifdef L__main
2099 #include "gbl-ctors.h"
2101 /* Some systems use __main in a way incompatible with its use in gcc, in these
2102 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
2103 give the same symbol without quotes for an alternative entry point. You
2104 must define both, or neither. */
2105 #ifndef NAME__MAIN
2106 #define NAME__MAIN "__main"
2107 #define SYMBOL__MAIN __main
2108 #endif
2110 #if defined (INIT_SECTION_ASM_OP) || defined (INIT_ARRAY_SECTION_ASM_OP)
2111 #undef HAS_INIT_SECTION
2112 #define HAS_INIT_SECTION
2113 #endif
2115 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
2117 /* Some ELF crosses use crtstuff.c to provide __CTOR_LIST__, but use this
2118 code to run constructors. In that case, we need to handle EH here, too. */
2120 #ifdef EH_FRAME_SECTION_NAME
2121 #include "unwind-dw2-fde.h"
2122 extern unsigned char __EH_FRAME_BEGIN__[];
2123 #endif
2125 /* Run all the global destructors on exit from the program. */
2127 void
2128 __do_global_dtors (void)
2130 #ifdef DO_GLOBAL_DTORS_BODY
2131 DO_GLOBAL_DTORS_BODY;
2132 #else
2133 static func_ptr *p = __DTOR_LIST__ + 1;
2134 while (*p)
2136 p++;
2137 (*(p-1)) ();
2139 #endif
2140 #if defined (EH_FRAME_SECTION_NAME) && !defined (HAS_INIT_SECTION)
2142 static int completed = 0;
2143 if (! completed)
2145 completed = 1;
2146 __deregister_frame_info (__EH_FRAME_BEGIN__);
2149 #endif
2151 #endif
2153 #ifndef HAS_INIT_SECTION
2154 /* Run all the global constructors on entry to the program. */
2156 void
2157 __do_global_ctors (void)
2159 #ifdef EH_FRAME_SECTION_NAME
2161 static struct object object;
2162 __register_frame_info (__EH_FRAME_BEGIN__, &object);
2164 #endif
2165 DO_GLOBAL_CTORS_BODY;
2166 atexit (__do_global_dtors);
2168 #endif /* no HAS_INIT_SECTION */
2170 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
2171 /* Subroutine called automatically by `main'.
2172 Compiling a global function named `main'
2173 produces an automatic call to this function at the beginning.
2175 For many systems, this routine calls __do_global_ctors.
2176 For systems which support a .init section we use the .init section
2177 to run __do_global_ctors, so we need not do anything here. */
2179 extern void SYMBOL__MAIN (void);
2180 void
2181 SYMBOL__MAIN (void)
2183 /* Support recursive calls to `main': run initializers just once. */
2184 static int initialized;
2185 if (! initialized)
2187 initialized = 1;
2188 __do_global_ctors ();
2191 #endif /* no HAS_INIT_SECTION or INVOKE__main */
2193 #endif /* L__main */
2194 #endif /* __CYGWIN__ */
2196 #ifdef L_ctors
2198 #include "gbl-ctors.h"
2200 /* Provide default definitions for the lists of constructors and
2201 destructors, so that we don't get linker errors. These symbols are
2202 intentionally bss symbols, so that gld and/or collect will provide
2203 the right values. */
2205 /* We declare the lists here with two elements each,
2206 so that they are valid empty lists if no other definition is loaded.
2208 If we are using the old "set" extensions to have the gnu linker
2209 collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
2210 must be in the bss/common section.
2212 Long term no port should use those extensions. But many still do. */
2213 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
2214 #if defined (TARGET_ASM_CONSTRUCTOR) || defined (USE_COLLECT2)
2215 func_ptr __CTOR_LIST__[2] = {0, 0};
2216 func_ptr __DTOR_LIST__[2] = {0, 0};
2217 #else
2218 func_ptr __CTOR_LIST__[2];
2219 func_ptr __DTOR_LIST__[2];
2220 #endif
2221 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
2222 #endif /* L_ctors */
2223 #endif /* LIBGCC2_UNITS_PER_WORD <= MIN_UNITS_PER_WORD */