OSDN Git Service

* objc.dg/isa-field-1.m: Only run on darwin.
[pf3gnuchains/gcc-fork.git] / gcc / libgcc2.c
1 /* More subroutines needed by GCC output code on some machines.  */
2 /* Compile this one with gcc.  */
3 /* Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4    2000, 2001, 2002, 2003, 2004, 2005  Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file.  (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combine
20 executable.)
21
22 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
23 WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25 for more details.
26
27 You should have received a copy of the GNU General Public License
28 along with GCC; see the file COPYING.  If not, write to the Free
29 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
30 02111-1307, USA.  */
31
32 #include "tconfig.h"
33 #include "tsystem.h"
34 #include "coretypes.h"
35 #include "tm.h"
36
37 #ifdef HAVE_GAS_HIDDEN
38 #define ATTRIBUTE_HIDDEN  __attribute__ ((__visibility__ ("hidden")))
39 #else
40 #define ATTRIBUTE_HIDDEN
41 #endif
42
43 #include "libgcc2.h"
44 \f
45 #ifdef DECLARE_LIBRARY_RENAMES
46   DECLARE_LIBRARY_RENAMES
47 #endif
48
49 #if defined (L_negdi2)
50 DWtype
51 __negdi2 (DWtype u)
52 {
53   const DWunion uu = {.ll = u};
54   const DWunion w = { {.low = -uu.s.low,
55                        .high = -uu.s.high - ((UWtype) -uu.s.low > 0) } };
56
57   return w.ll;
58 }
59 #endif
60
61 #ifdef L_addvsi3
62 Wtype
63 __addvSI3 (Wtype a, Wtype b)
64 {
65   const Wtype w = a + b;
66
67   if (b >= 0 ? w < a : w > a)
68     abort ();
69
70   return w;
71 }
72 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
73 SItype
74 __addvsi3 (SItype a, SItype b)
75 {
76   const SItype w = a + b;
77
78   if (b >= 0 ? w < a : w > a)
79     abort ();
80
81   return w;
82 }
83 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
84 #endif
85 \f
86 #ifdef L_addvdi3
87 DWtype
88 __addvDI3 (DWtype a, DWtype b)
89 {
90   const DWtype w = a + b;
91
92   if (b >= 0 ? w < a : w > a)
93     abort ();
94
95   return w;
96 }
97 #endif
98 \f
99 #ifdef L_subvsi3
100 Wtype
101 __subvSI3 (Wtype a, Wtype b)
102 {
103   const Wtype w = a - b;
104
105   if (b >= 0 ? w > a : w < a)
106     abort ();
107
108   return w;
109 }
110 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
111 SItype
112 __subvsi3 (SItype a, SItype b)
113 {
114   const SItype w = a - b;
115
116   if (b >= 0 ? w > a : w < a)
117     abort ();
118
119   return w;
120 }
121 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
122 #endif
123 \f
124 #ifdef L_subvdi3
125 DWtype
126 __subvDI3 (DWtype a, DWtype b)
127 {
128   const DWtype w = a - b;
129
130   if (b >= 0 ? w > a : w < a)
131     abort ();
132
133   return w;
134 }
135 #endif
136 \f
137 #ifdef L_mulvsi3
138 Wtype
139 __mulvSI3 (Wtype a, Wtype b)
140 {
141   const DWtype w = (DWtype) a * (DWtype) b;
142
143   if ((Wtype) (w >> W_TYPE_SIZE) != (Wtype) w >> (W_TYPE_SIZE - 1))
144     abort ();
145
146   return w;
147 }
148 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
149 #undef WORD_SIZE
150 #define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
151 SItype
152 __mulvsi3 (SItype a, SItype b)
153 {
154   const DItype w = (DItype) a * (DItype) b;
155
156   if ((SItype) (w >> WORD_SIZE) != (SItype) w >> (WORD_SIZE-1))
157     abort ();
158
159   return w;
160 }
161 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
162 #endif
163 \f
164 #ifdef L_negvsi2
165 Wtype
166 __negvSI2 (Wtype a)
167 {
168   const Wtype w = -a;
169
170   if (a >= 0 ? w > 0 : w < 0)
171     abort ();
172
173    return w;
174 }
175 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
176 SItype
177 __negvsi2 (SItype a)
178 {
179   const SItype w = -a;
180
181   if (a >= 0 ? w > 0 : w < 0)
182     abort ();
183
184    return w;
185 }
186 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
187 #endif
188 \f
189 #ifdef L_negvdi2
190 DWtype
191 __negvDI2 (DWtype a)
192 {
193   const DWtype w = -a;
194
195   if (a >= 0 ? w > 0 : w < 0)
196     abort ();
197
198   return w;
199 }
200 #endif
201 \f
202 #ifdef L_absvsi2
203 Wtype
204 __absvSI2 (Wtype a)
205 {
206   Wtype w = a;
207
208   if (a < 0)
209 #ifdef L_negvsi2
210     w = __negvSI2 (a);
211 #else
212     w = -a;
213
214   if (w < 0)
215     abort ();
216 #endif
217
218    return w;
219 }
220 #ifdef COMPAT_SIMODE_TRAPPING_ARITHMETIC
221 SItype
222 __absvsi2 (SItype a)
223 {
224   SItype w = a;
225
226   if (a < 0)
227 #ifdef L_negvsi2
228     w = __negvsi2 (a);
229 #else
230     w = -a;
231
232   if (w < 0)
233     abort ();
234 #endif
235
236    return w;
237 }
238 #endif /* COMPAT_SIMODE_TRAPPING_ARITHMETIC */
239 #endif
240 \f
241 #ifdef L_absvdi2
242 DWtype
243 __absvDI2 (DWtype a)
244 {
245   DWtype w = a;
246
247   if (a < 0)
248 #ifdef L_negvdi2
249     w = __negvDI2 (a);
250 #else
251     w = -a;
252
253   if (w < 0)
254     abort ();
255 #endif
256
257   return w;
258 }
259 #endif
260 \f
261 #ifdef L_mulvdi3
262 DWtype
263 __mulvDI3 (DWtype u, DWtype v)
264 {
265   /* The unchecked multiplication needs 3 Wtype x Wtype multiplications,
266      but the checked multiplication needs only two.  */
267   const DWunion uu = {.ll = u};
268   const DWunion vv = {.ll = v};
269
270   if (__builtin_expect (uu.s.high == uu.s.low >> (W_TYPE_SIZE - 1), 1))
271     {
272       /* u fits in a single Wtype.  */
273       if (__builtin_expect (vv.s.high == vv.s.low >> (W_TYPE_SIZE - 1), 1))
274         {
275           /* v fits in a single Wtype as well.  */
276           /* A single multiplication.  No overflow risk.  */
277           return (DWtype) uu.s.low * (DWtype) vv.s.low;
278         }
279       else
280         {
281           /* Two multiplications.  */
282           DWunion w0 = {.ll = (UDWtype) (UWtype) uu.s.low
283                         * (UDWtype) (UWtype) vv.s.low};
284           DWunion w1 = {.ll = (UDWtype) (UWtype) uu.s.low
285                         * (UDWtype) (UWtype) vv.s.high};
286
287           if (vv.s.high < 0)
288             w1.s.high -= uu.s.low;
289           if (uu.s.low < 0)
290             w1.ll -= vv.ll;
291           w1.ll += (UWtype) w0.s.high;
292           if (__builtin_expect (w1.s.high == w1.s.low >> (W_TYPE_SIZE - 1), 1))
293             {
294               w0.s.high = w1.s.low;
295               return w0.ll;
296             }
297         }
298     }
299   else
300     {
301       if (__builtin_expect (vv.s.high == vv.s.low >> (W_TYPE_SIZE - 1), 1))
302         {
303           /* v fits into a single Wtype.  */
304           /* Two multiplications.  */
305           DWunion w0 = {.ll = (UDWtype) (UWtype) uu.s.low
306                         * (UDWtype) (UWtype) vv.s.low};
307           DWunion w1 = {.ll = (UDWtype) (UWtype) uu.s.high
308                         * (UDWtype) (UWtype) vv.s.low};
309
310           if (uu.s.high < 0)
311             w1.s.high -= vv.s.low;
312           if (vv.s.low < 0)
313             w1.ll -= uu.ll;
314           w1.ll += (UWtype) w0.s.high;
315           if (__builtin_expect (w1.s.high == w1.s.low >> (W_TYPE_SIZE - 1), 1))
316             {
317               w0.s.high = w1.s.low;
318               return w0.ll;
319             }
320         }
321       else
322         {
323           /* A few sign checks and a single multiplication.  */
324           if (uu.s.high >= 0)
325             {
326               if (vv.s.high >= 0)
327                 {
328                   if (uu.s.high == 0 && vv.s.high == 0)
329                     {
330                       const DWtype w = (UDWtype) (UWtype) uu.s.low
331                         * (UDWtype) (UWtype) vv.s.low;
332                       if (__builtin_expect (w >= 0, 1))
333                         return w;
334                     }
335                 }
336               else
337                 {
338                   if (uu.s.high == 0 && vv.s.high == (Wtype) -1)
339                     {
340                       DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
341                                     * (UDWtype) (UWtype) vv.s.low};
342
343                       ww.s.high -= uu.s.low;
344                       if (__builtin_expect (ww.s.high < 0, 1))
345                         return ww.ll;
346                     }
347                 }
348             }
349           else
350             {
351               if (vv.s.high >= 0)
352                 {
353                   if (uu.s.high == (Wtype) -1 && vv.s.high == 0)
354                     {
355                       DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
356                                     * (UDWtype) (UWtype) vv.s.low};
357
358                       ww.s.high -= vv.s.low;
359                       if (__builtin_expect (ww.s.high < 0, 1))
360                         return ww.ll;
361                     }
362                 }
363               else
364                 {
365                   if (uu.s.high == (Wtype) -1 && vv.s.high == (Wtype) - 1)
366                     {
367                       DWunion ww = {.ll = (UDWtype) (UWtype) uu.s.low
368                                     * (UDWtype) (UWtype) vv.s.low};
369
370                       ww.s.high -= uu.s.low;
371                       ww.s.high -= vv.s.low;
372                       if (__builtin_expect (ww.s.high >= 0, 1))
373                         return ww.ll;
374                     }
375                 }
376             }
377         }
378     }
379
380   /* Overflow.  */
381   abort ();
382 }
383 #endif
384 \f
385
386 /* Unless shift functions are defined with full ANSI prototypes,
387    parameter b will be promoted to int if word_type is smaller than an int.  */
388 #ifdef L_lshrdi3
389 DWtype
390 __lshrdi3 (DWtype u, word_type b)
391 {
392   if (b == 0)
393     return u;
394
395   const DWunion uu = {.ll = u};
396   const word_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
397   DWunion w;
398
399   if (bm <= 0)
400     {
401       w.s.high = 0;
402       w.s.low = (UWtype) uu.s.high >> -bm;
403     }
404   else
405     {
406       const UWtype carries = (UWtype) uu.s.high << bm;
407
408       w.s.high = (UWtype) uu.s.high >> b;
409       w.s.low = ((UWtype) uu.s.low >> b) | carries;
410     }
411
412   return w.ll;
413 }
414 #endif
415
416 #ifdef L_ashldi3
417 DWtype
418 __ashldi3 (DWtype u, word_type b)
419 {
420   if (b == 0)
421     return u;
422
423   const DWunion uu = {.ll = u};
424   const word_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
425   DWunion w;
426
427   if (bm <= 0)
428     {
429       w.s.low = 0;
430       w.s.high = (UWtype) uu.s.low << -bm;
431     }
432   else
433     {
434       const UWtype carries = (UWtype) uu.s.low >> bm;
435
436       w.s.low = (UWtype) uu.s.low << b;
437       w.s.high = ((UWtype) uu.s.high << b) | carries;
438     }
439
440   return w.ll;
441 }
442 #endif
443
444 #ifdef L_ashrdi3
445 DWtype
446 __ashrdi3 (DWtype u, word_type b)
447 {
448   if (b == 0)
449     return u;
450
451   const DWunion uu = {.ll = u};
452   const word_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
453   DWunion w;
454
455   if (bm <= 0)
456     {
457       /* w.s.high = 1..1 or 0..0 */
458       w.s.high = uu.s.high >> (sizeof (Wtype) * BITS_PER_UNIT - 1);
459       w.s.low = uu.s.high >> -bm;
460     }
461   else
462     {
463       const UWtype carries = (UWtype) uu.s.high << bm;
464
465       w.s.high = uu.s.high >> b;
466       w.s.low = ((UWtype) uu.s.low >> b) | carries;
467     }
468
469   return w.ll;
470 }
471 #endif
472 \f
473 #ifdef L_ffssi2
474 #undef int
475 int
476 __ffsSI2 (UWtype u)
477 {
478   UWtype count;
479
480   if (u == 0)
481     return 0;
482
483   count_trailing_zeros (count, u);
484   return count + 1;
485 }
486 #endif
487 \f
488 #ifdef L_ffsdi2
489 #undef int
490 int
491 __ffsDI2 (DWtype u)
492 {
493   const DWunion uu = {.ll = u};
494   UWtype word, count, add;
495
496   if (uu.s.low != 0)
497     word = uu.s.low, add = 0;
498   else if (uu.s.high != 0)
499     word = uu.s.high, add = BITS_PER_UNIT * sizeof (Wtype);
500   else
501     return 0;
502
503   count_trailing_zeros (count, word);
504   return count + add + 1;
505 }
506 #endif
507 \f
508 #ifdef L_muldi3
509 DWtype
510 __muldi3 (DWtype u, DWtype v)
511 {
512   const DWunion uu = {.ll = u};
513   const DWunion vv = {.ll = v};
514   DWunion w = {.ll = __umulsidi3 (uu.s.low, vv.s.low)};
515
516   w.s.high += ((UWtype) uu.s.low * (UWtype) vv.s.high
517                + (UWtype) uu.s.high * (UWtype) vv.s.low);
518
519   return w.ll;
520 }
521 #endif
522 \f
523 #if (defined (L_udivdi3) || defined (L_divdi3) || \
524      defined (L_umoddi3) || defined (L_moddi3))
525 #if defined (sdiv_qrnnd)
526 #define L_udiv_w_sdiv
527 #endif
528 #endif
529
530 #ifdef L_udiv_w_sdiv
531 #if defined (sdiv_qrnnd)
532 #if (defined (L_udivdi3) || defined (L_divdi3) || \
533      defined (L_umoddi3) || defined (L_moddi3))
534 static inline __attribute__ ((__always_inline__))
535 #endif
536 UWtype
537 __udiv_w_sdiv (UWtype *rp, UWtype a1, UWtype a0, UWtype d)
538 {
539   UWtype q, r;
540   UWtype c0, c1, b1;
541
542   if ((Wtype) d >= 0)
543     {
544       if (a1 < d - a1 - (a0 >> (W_TYPE_SIZE - 1)))
545         {
546           /* Dividend, divisor, and quotient are nonnegative.  */
547           sdiv_qrnnd (q, r, a1, a0, d);
548         }
549       else
550         {
551           /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d.  */
552           sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (W_TYPE_SIZE - 1));
553           /* Divide (c1*2^32 + c0) by d.  */
554           sdiv_qrnnd (q, r, c1, c0, d);
555           /* Add 2^31 to quotient.  */
556           q += (UWtype) 1 << (W_TYPE_SIZE - 1);
557         }
558     }
559   else
560     {
561       b1 = d >> 1;                      /* d/2, between 2^30 and 2^31 - 1 */
562       c1 = a1 >> 1;                     /* A/2 */
563       c0 = (a1 << (W_TYPE_SIZE - 1)) + (a0 >> 1);
564
565       if (a1 < b1)                      /* A < 2^32*b1, so A/2 < 2^31*b1 */
566         {
567           sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
568
569           r = 2*r + (a0 & 1);           /* Remainder from A/(2*b1) */
570           if ((d & 1) != 0)
571             {
572               if (r >= q)
573                 r = r - q;
574               else if (q - r <= d)
575                 {
576                   r = r - q + d;
577                   q--;
578                 }
579               else
580                 {
581                   r = r - q + 2*d;
582                   q -= 2;
583                 }
584             }
585         }
586       else if (c1 < b1)                 /* So 2^31 <= (A/2)/b1 < 2^32 */
587         {
588           c1 = (b1 - 1) - c1;
589           c0 = ~c0;                     /* logical NOT */
590
591           sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
592
593           q = ~q;                       /* (A/2)/b1 */
594           r = (b1 - 1) - r;
595
596           r = 2*r + (a0 & 1);           /* A/(2*b1) */
597
598           if ((d & 1) != 0)
599             {
600               if (r >= q)
601                 r = r - q;
602               else if (q - r <= d)
603                 {
604                   r = r - q + d;
605                   q--;
606                 }
607               else
608                 {
609                   r = r - q + 2*d;
610                   q -= 2;
611                 }
612             }
613         }
614       else                              /* Implies c1 = b1 */
615         {                               /* Hence a1 = d - 1 = 2*b1 - 1 */
616           if (a0 >= -d)
617             {
618               q = -1;
619               r = a0 + d;
620             }
621           else
622             {
623               q = -2;
624               r = a0 + 2*d;
625             }
626         }
627     }
628
629   *rp = r;
630   return q;
631 }
632 #else
633 /* If sdiv_qrnnd doesn't exist, define dummy __udiv_w_sdiv.  */
634 UWtype
635 __udiv_w_sdiv (UWtype *rp __attribute__ ((__unused__)),
636                UWtype a1 __attribute__ ((__unused__)),
637                UWtype a0 __attribute__ ((__unused__)),
638                UWtype d __attribute__ ((__unused__)))
639 {
640   return 0;
641 }
642 #endif
643 #endif
644 \f
645 #if (defined (L_udivdi3) || defined (L_divdi3) || \
646      defined (L_umoddi3) || defined (L_moddi3))
647 #define L_udivmoddi4
648 #endif
649
650 #ifdef L_clz
651 const UQItype __clz_tab[] =
652 {
653   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,
654   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,
655   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,
656   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,
657   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,
658   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,
659   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,
660   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,
661 };
662 #endif
663 \f
664 #ifdef L_clzsi2
665 #undef int
666 int
667 __clzSI2 (UWtype x)
668 {
669   Wtype ret;
670
671   count_leading_zeros (ret, x);
672
673   return ret;
674 }
675 #endif
676 \f
677 #ifdef L_clzdi2
678 #undef int
679 int
680 __clzDI2 (UDWtype x)
681 {
682   const DWunion uu = {.ll = x};
683   UWtype word;
684   Wtype ret, add;
685
686   if (uu.s.high)
687     word = uu.s.high, add = 0;
688   else
689     word = uu.s.low, add = W_TYPE_SIZE;
690
691   count_leading_zeros (ret, word);
692   return ret + add;
693 }
694 #endif
695 \f
696 #ifdef L_ctzsi2
697 #undef int
698 int
699 __ctzSI2 (UWtype x)
700 {
701   Wtype ret;
702
703   count_trailing_zeros (ret, x);
704
705   return ret;
706 }
707 #endif
708 \f
709 #ifdef L_ctzdi2
710 #undef int
711 int
712 __ctzDI2 (UDWtype x)
713 {
714   const DWunion uu = {.ll = x};
715   UWtype word;
716   Wtype ret, add;
717
718   if (uu.s.low)
719     word = uu.s.low, add = 0;
720   else
721     word = uu.s.high, add = W_TYPE_SIZE;
722
723   count_trailing_zeros (ret, word);
724   return ret + add;
725 }
726 #endif
727
728 #if (defined (L_popcountsi2) || defined (L_popcountdi2) \
729      || defined (L_popcount_tab))
730 extern const UQItype __popcount_tab[] ATTRIBUTE_HIDDEN;
731 #endif
732
733 #ifdef L_popcount_tab
734 const UQItype __popcount_tab[] =
735 {
736     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,
737     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,
738     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,
739     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,
740     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,
741     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,
742     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,
743     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,
744 };
745 #endif
746 \f
747 #ifdef L_popcountsi2
748 #undef int
749 int
750 __popcountSI2 (UWtype x)
751 {
752   UWtype i, ret = 0;
753
754   for (i = 0; i < W_TYPE_SIZE; i += 8)
755     ret += __popcount_tab[(x >> i) & 0xff];
756
757   return ret;
758 }
759 #endif
760 \f
761 #ifdef L_popcountdi2
762 #undef int
763 int
764 __popcountDI2 (UDWtype x)
765 {
766   UWtype i, ret = 0;
767
768   for (i = 0; i < 2*W_TYPE_SIZE; i += 8)
769     ret += __popcount_tab[(x >> i) & 0xff];
770
771   return ret;
772 }
773 #endif
774 \f
775 #ifdef L_paritysi2
776 #undef int
777 int
778 __paritySI2 (UWtype x)
779 {
780 #if W_TYPE_SIZE > 64
781 # error "fill out the table"
782 #endif
783 #if W_TYPE_SIZE > 32
784   x ^= x >> 32;
785 #endif
786 #if W_TYPE_SIZE > 16
787   x ^= x >> 16;
788 #endif
789   x ^= x >> 8;
790   x ^= x >> 4;
791   x &= 0xf;
792   return (0x6996 >> x) & 1;
793 }
794 #endif
795 \f
796 #ifdef L_paritydi2
797 #undef int
798 int
799 __parityDI2 (UDWtype x)
800 {
801   const DWunion uu = {.ll = x};
802   UWtype nx = uu.s.low ^ uu.s.high;
803
804 #if W_TYPE_SIZE > 64
805 # error "fill out the table"
806 #endif
807 #if W_TYPE_SIZE > 32
808   nx ^= nx >> 32;
809 #endif
810 #if W_TYPE_SIZE > 16
811   nx ^= nx >> 16;
812 #endif
813   nx ^= nx >> 8;
814   nx ^= nx >> 4;
815   nx &= 0xf;
816   return (0x6996 >> nx) & 1;
817 }
818 #endif
819
820 #ifdef L_udivmoddi4
821
822 #if (defined (L_udivdi3) || defined (L_divdi3) || \
823      defined (L_umoddi3) || defined (L_moddi3))
824 static inline __attribute__ ((__always_inline__))
825 #endif
826 UDWtype
827 __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
828 {
829   const DWunion nn = {.ll = n};
830   const DWunion dd = {.ll = d};
831   DWunion rr;
832   UWtype d0, d1, n0, n1, n2;
833   UWtype q0, q1;
834   UWtype b, bm;
835
836   d0 = dd.s.low;
837   d1 = dd.s.high;
838   n0 = nn.s.low;
839   n1 = nn.s.high;
840
841 #if !UDIV_NEEDS_NORMALIZATION
842   if (d1 == 0)
843     {
844       if (d0 > n1)
845         {
846           /* 0q = nn / 0D */
847
848           udiv_qrnnd (q0, n0, n1, n0, d0);
849           q1 = 0;
850
851           /* Remainder in n0.  */
852         }
853       else
854         {
855           /* qq = NN / 0d */
856
857           if (d0 == 0)
858             d0 = 1 / d0;        /* Divide intentionally by zero.  */
859
860           udiv_qrnnd (q1, n1, 0, n1, d0);
861           udiv_qrnnd (q0, n0, n1, n0, d0);
862
863           /* Remainder in n0.  */
864         }
865
866       if (rp != 0)
867         {
868           rr.s.low = n0;
869           rr.s.high = 0;
870           *rp = rr.ll;
871         }
872     }
873
874 #else /* UDIV_NEEDS_NORMALIZATION */
875
876   if (d1 == 0)
877     {
878       if (d0 > n1)
879         {
880           /* 0q = nn / 0D */
881
882           count_leading_zeros (bm, d0);
883
884           if (bm != 0)
885             {
886               /* Normalize, i.e. make the most significant bit of the
887                  denominator set.  */
888
889               d0 = d0 << bm;
890               n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm));
891               n0 = n0 << bm;
892             }
893
894           udiv_qrnnd (q0, n0, n1, n0, d0);
895           q1 = 0;
896
897           /* Remainder in n0 >> bm.  */
898         }
899       else
900         {
901           /* qq = NN / 0d */
902
903           if (d0 == 0)
904             d0 = 1 / d0;        /* Divide intentionally by zero.  */
905
906           count_leading_zeros (bm, d0);
907
908           if (bm == 0)
909             {
910               /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
911                  conclude (the most significant bit of n1 is set) /\ (the
912                  leading quotient digit q1 = 1).
913
914                  This special case is necessary, not an optimization.
915                  (Shifts counts of W_TYPE_SIZE are undefined.)  */
916
917               n1 -= d0;
918               q1 = 1;
919             }
920           else
921             {
922               /* Normalize.  */
923
924               b = W_TYPE_SIZE - bm;
925
926               d0 = d0 << bm;
927               n2 = n1 >> b;
928               n1 = (n1 << bm) | (n0 >> b);
929               n0 = n0 << bm;
930
931               udiv_qrnnd (q1, n1, n2, n1, d0);
932             }
933
934           /* n1 != d0...  */
935
936           udiv_qrnnd (q0, n0, n1, n0, d0);
937
938           /* Remainder in n0 >> bm.  */
939         }
940
941       if (rp != 0)
942         {
943           rr.s.low = n0 >> bm;
944           rr.s.high = 0;
945           *rp = rr.ll;
946         }
947     }
948 #endif /* UDIV_NEEDS_NORMALIZATION */
949
950   else
951     {
952       if (d1 > n1)
953         {
954           /* 00 = nn / DD */
955
956           q0 = 0;
957           q1 = 0;
958
959           /* Remainder in n1n0.  */
960           if (rp != 0)
961             {
962               rr.s.low = n0;
963               rr.s.high = n1;
964               *rp = rr.ll;
965             }
966         }
967       else
968         {
969           /* 0q = NN / dd */
970
971           count_leading_zeros (bm, d1);
972           if (bm == 0)
973             {
974               /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
975                  conclude (the most significant bit of n1 is set) /\ (the
976                  quotient digit q0 = 0 or 1).
977
978                  This special case is necessary, not an optimization.  */
979
980               /* The condition on the next line takes advantage of that
981                  n1 >= d1 (true due to program flow).  */
982               if (n1 > d1 || n0 >= d0)
983                 {
984                   q0 = 1;
985                   sub_ddmmss (n1, n0, n1, n0, d1, d0);
986                 }
987               else
988                 q0 = 0;
989
990               q1 = 0;
991
992               if (rp != 0)
993                 {
994                   rr.s.low = n0;
995                   rr.s.high = n1;
996                   *rp = rr.ll;
997                 }
998             }
999           else
1000             {
1001               UWtype m1, m0;
1002               /* Normalize.  */
1003
1004               b = W_TYPE_SIZE - bm;
1005
1006               d1 = (d1 << bm) | (d0 >> b);
1007               d0 = d0 << bm;
1008               n2 = n1 >> b;
1009               n1 = (n1 << bm) | (n0 >> b);
1010               n0 = n0 << bm;
1011
1012               udiv_qrnnd (q0, n1, n2, n1, d1);
1013               umul_ppmm (m1, m0, q0, d0);
1014
1015               if (m1 > n1 || (m1 == n1 && m0 > n0))
1016                 {
1017                   q0--;
1018                   sub_ddmmss (m1, m0, m1, m0, d1, d0);
1019                 }
1020
1021               q1 = 0;
1022
1023               /* Remainder in (n1n0 - m1m0) >> bm.  */
1024               if (rp != 0)
1025                 {
1026                   sub_ddmmss (n1, n0, n1, n0, m1, m0);
1027                   rr.s.low = (n1 << b) | (n0 >> bm);
1028                   rr.s.high = n1 >> bm;
1029                   *rp = rr.ll;
1030                 }
1031             }
1032         }
1033     }
1034
1035   const DWunion ww = {{.low = q0, .high = q1}};
1036   return ww.ll;
1037 }
1038 #endif
1039
1040 #ifdef L_divdi3
1041 DWtype
1042 __divdi3 (DWtype u, DWtype v)
1043 {
1044   word_type c = 0;
1045   DWunion uu = {.ll = u};
1046   DWunion vv = {.ll = v};
1047   DWtype w;
1048
1049   if (uu.s.high < 0)
1050     c = ~c,
1051     uu.ll = -uu.ll;
1052   if (vv.s.high < 0)
1053     c = ~c,
1054     vv.ll = -vv.ll;
1055
1056   w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0);
1057   if (c)
1058     w = -w;
1059
1060   return w;
1061 }
1062 #endif
1063
1064 #ifdef L_moddi3
1065 DWtype
1066 __moddi3 (DWtype u, DWtype v)
1067 {
1068   word_type c = 0;
1069   DWunion uu = {.ll = u};
1070   DWunion vv = {.ll = v};
1071   DWtype w;
1072
1073   if (uu.s.high < 0)
1074     c = ~c,
1075     uu.ll = -uu.ll;
1076   if (vv.s.high < 0)
1077     vv.ll = -vv.ll;
1078
1079   (void) __udivmoddi4 (uu.ll, vv.ll, (UDWtype*)&w);
1080   if (c)
1081     w = -w;
1082
1083   return w;
1084 }
1085 #endif
1086
1087 #ifdef L_umoddi3
1088 UDWtype
1089 __umoddi3 (UDWtype u, UDWtype v)
1090 {
1091   UDWtype w;
1092
1093   (void) __udivmoddi4 (u, v, &w);
1094
1095   return w;
1096 }
1097 #endif
1098
1099 #ifdef L_udivdi3
1100 UDWtype
1101 __udivdi3 (UDWtype n, UDWtype d)
1102 {
1103   return __udivmoddi4 (n, d, (UDWtype *) 0);
1104 }
1105 #endif
1106 \f
1107 #ifdef L_cmpdi2
1108 word_type
1109 __cmpdi2 (DWtype a, DWtype b)
1110 {
1111   const DWunion au = {.ll = a};
1112   const DWunion bu = {.ll = b};
1113
1114   if (au.s.high < bu.s.high)
1115     return 0;
1116   else if (au.s.high > bu.s.high)
1117     return 2;
1118   if ((UWtype) au.s.low < (UWtype) bu.s.low)
1119     return 0;
1120   else if ((UWtype) au.s.low > (UWtype) bu.s.low)
1121     return 2;
1122   return 1;
1123 }
1124 #endif
1125
1126 #ifdef L_ucmpdi2
1127 word_type
1128 __ucmpdi2 (DWtype a, DWtype b)
1129 {
1130   const DWunion au = {.ll = a};
1131   const DWunion bu = {.ll = b};
1132
1133   if ((UWtype) au.s.high < (UWtype) bu.s.high)
1134     return 0;
1135   else if ((UWtype) au.s.high > (UWtype) bu.s.high)
1136     return 2;
1137   if ((UWtype) au.s.low < (UWtype) bu.s.low)
1138     return 0;
1139   else if ((UWtype) au.s.low > (UWtype) bu.s.low)
1140     return 2;
1141   return 1;
1142 }
1143 #endif
1144 \f
1145 #if defined(L_fixunstfdi) && LIBGCC2_HAS_TF_MODE
1146 DWtype
1147 __fixunstfDI (TFtype a)
1148 {
1149   if (a < 0)
1150     return 0;
1151
1152   /* Compute high word of result, as a flonum.  */
1153   const TFtype b = (a / Wtype_MAXp1_F);
1154   /* Convert that to fixed (but not to DWtype!),
1155      and shift it into the high word.  */
1156   UDWtype v = (UWtype) b;
1157   v <<= W_TYPE_SIZE;
1158   /* Remove high part from the TFtype, leaving the low part as flonum.  */
1159   a -= (TFtype)v;
1160   /* Convert that to fixed (but not to DWtype!) and add it in.
1161      Sometimes A comes out negative.  This is significant, since
1162      A has more bits than a long int does.  */
1163   if (a < 0)
1164     v -= (UWtype) (- a);
1165   else
1166     v += (UWtype) a;
1167   return v;
1168 }
1169 #endif
1170
1171 #if defined(L_fixtfdi) && LIBGCC2_HAS_TF_MODE
1172 DWtype
1173 __fixtfdi (TFtype a)
1174 {
1175   if (a < 0)
1176     return - __fixunstfDI (-a);
1177   return __fixunstfDI (a);
1178 }
1179 #endif
1180
1181 #if defined(L_fixunsxfdi) && LIBGCC2_HAS_XF_MODE
1182 DWtype
1183 __fixunsxfDI (XFtype a)
1184 {
1185   if (a < 0)
1186     return 0;
1187
1188   /* Compute high word of result, as a flonum.  */
1189   const XFtype b = (a / Wtype_MAXp1_F);
1190   /* Convert that to fixed (but not to DWtype!),
1191      and shift it into the high word.  */
1192   UDWtype v = (UWtype) b;
1193   v <<= W_TYPE_SIZE;
1194   /* Remove high part from the XFtype, leaving the low part as flonum.  */
1195   a -= (XFtype)v;
1196   /* Convert that to fixed (but not to DWtype!) and add it in.
1197      Sometimes A comes out negative.  This is significant, since
1198      A has more bits than a long int does.  */
1199   if (a < 0)
1200     v -= (UWtype) (- a);
1201   else
1202     v += (UWtype) a;
1203   return v;
1204 }
1205 #endif
1206
1207 #if defined(L_fixxfdi) && LIBGCC2_HAS_XF_MODE
1208 DWtype
1209 __fixxfdi (XFtype a)
1210 {
1211   if (a < 0)
1212     return - __fixunsxfDI (-a);
1213   return __fixunsxfDI (a);
1214 }
1215 #endif
1216
1217 #if defined(L_fixunsdfdi) && LIBGCC2_HAS_DF_MODE
1218 DWtype
1219 __fixunsdfDI (DFtype a)
1220 {
1221   /* Get high part of result.  The division here will just moves the radix
1222      point and will not cause any rounding.  Then the conversion to integral
1223      type chops result as desired.  */
1224   const UWtype hi = a / Wtype_MAXp1_F;
1225
1226   /* Get low part of result.  Convert `hi' to floating type and scale it back,
1227      then subtract this from the number being converted.  This leaves the low
1228      part.  Convert that to integral type.  */
1229   const UWtype lo = a - (DFtype) hi * Wtype_MAXp1_F;
1230
1231   /* Assemble result from the two parts.  */
1232   return ((UDWtype) hi << W_TYPE_SIZE) | lo;
1233 }
1234 #endif
1235
1236 #if defined(L_fixdfdi) && LIBGCC2_HAS_DF_MODE
1237 DWtype
1238 __fixdfdi (DFtype a)
1239 {
1240   if (a < 0)
1241     return - __fixunsdfDI (-a);
1242   return __fixunsdfDI (a);
1243 }
1244 #endif
1245
1246 #if defined(L_fixunssfdi) && LIBGCC2_HAS_SF_MODE
1247 DWtype
1248 __fixunssfDI (SFtype a)
1249 {
1250 #if LIBGCC2_HAS_DF_MODE
1251   /* Convert the SFtype to a DFtype, because that is surely not going
1252      to lose any bits.  Some day someone else can write a faster version
1253      that avoids converting to DFtype, and verify it really works right.  */
1254   const DFtype dfa = a;
1255
1256   /* Get high part of result.  The division here will just moves the radix
1257      point and will not cause any rounding.  Then the conversion to integral
1258      type chops result as desired.  */
1259   const UWtype hi = dfa / Wtype_MAXp1_F;
1260
1261   /* Get low part of result.  Convert `hi' to floating type and scale it back,
1262      then subtract this from the number being converted.  This leaves the low
1263      part.  Convert that to integral type.  */
1264   const UWtype lo = dfa - (DFtype) hi * Wtype_MAXp1_F;
1265
1266   /* Assemble result from the two parts.  */
1267   return ((UDWtype) hi << W_TYPE_SIZE) | lo;
1268 #elif FLT_MANT_DIG < W_TYPE_SIZE
1269   if (a < 1)
1270     return 0;
1271   if (a < Wtype_MAXp1_F)
1272     return (UWtype)a;
1273   if (a < Wtype_MAXp1_F * Wtype_MAXp1_F)
1274     {
1275       /* Since we know that there are fewer significant bits in the SFmode
1276          quantity than in a word, we know that we can convert out all the
1277          significant bits in one step, and thus avoid losing bits.  */
1278
1279       /* ??? This following loop essentially performs frexpf.  If we could
1280          use the real libm function, or poke at the actual bits of the fp
1281          format, it would be significantly faster.  */
1282
1283       UWtype shift = 0, counter;
1284       SFtype msb;
1285
1286       a /= Wtype_MAXp1_F;
1287       for (counter = W_TYPE_SIZE / 2; counter != 0; counter >>= 1)
1288         {
1289           SFtype counterf = (UWtype)1 << counter;
1290           if (a >= counterf)
1291             {
1292               shift |= counter;
1293               a /= counterf;
1294             }
1295         }
1296
1297       /* Rescale into the range of one word, extract the bits of that
1298          one word, and shift the result into position.  */
1299       a *= Wtype_MAXp1_F;
1300       counter = a;
1301       return (DWtype)counter << shift;
1302     }
1303   return -1;
1304 #else
1305 # error
1306 #endif
1307 }
1308 #endif
1309
1310 #if defined(L_fixsfdi) && LIBGCC2_HAS_SF_MODE
1311 DWtype
1312 __fixsfdi (SFtype a)
1313 {
1314   if (a < 0)
1315     return - __fixunssfDI (-a);
1316   return __fixunssfDI (a);
1317 }
1318 #endif
1319
1320 #if defined(L_floatdixf) && LIBGCC2_HAS_XF_MODE
1321 XFtype
1322 __floatdixf (DWtype u)
1323 {
1324   XFtype d = (Wtype) (u >> W_TYPE_SIZE);
1325   d *= Wtype_MAXp1_F;
1326   d += (UWtype)u;
1327   return d;
1328 }
1329 #endif
1330
1331 #if defined(L_floatditf) && LIBGCC2_HAS_TF_MODE
1332 TFtype
1333 __floatditf (DWtype u)
1334 {
1335   TFtype d = (Wtype) (u >> W_TYPE_SIZE);
1336   d *= Wtype_MAXp1_F;
1337   d += (UWtype)u;
1338   return d;
1339 }
1340 #endif
1341
1342 #if defined(L_floatdidf) && LIBGCC2_HAS_DF_MODE
1343 DFtype
1344 __floatdidf (DWtype u)
1345 {
1346   DFtype d = (Wtype) (u >> W_TYPE_SIZE);
1347   d *= Wtype_MAXp1_F;
1348   d += (UWtype)u;
1349   return d;
1350 }
1351 #endif
1352
1353 #if defined(L_floatdisf) && LIBGCC2_HAS_SF_MODE
1354 #define DI_SIZE (W_TYPE_SIZE * 2)
1355 #define SF_SIZE FLT_MANT_DIG
1356
1357 SFtype
1358 __floatdisf (DWtype u)
1359 {
1360 #if SF_SIZE >= W_TYPE_SIZE
1361   /* When the word size is small, we never get any rounding error.  */
1362   SFtype f = (Wtype) (u >> W_TYPE_SIZE);
1363   f *= Wtype_MAXp1_F;
1364   f += (UWtype)u;
1365   return f;
1366 #elif LIBGCC2_HAS_DF_MODE
1367
1368 #if LIBGCC2_DOUBLE_TYPE_SIZE == 64
1369 #define DF_SIZE DBL_MANT_DIG
1370 #elif LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 64
1371 #define DF_SIZE LDBL_MANT_DIG
1372 #else
1373 # error
1374 #endif
1375
1376 #define REP_BIT ((UDWtype) 1 << (DI_SIZE - DF_SIZE))
1377
1378   /* Protect against double-rounding error.
1379      Represent any low-order bits, that might be truncated by a bit that
1380      won't be lost.  The bit can go in anywhere below the rounding position
1381      of the SFmode.  A fixed mask and bit position handles all usual
1382      configurations.  It doesn't handle the case of 128-bit DImode, however.  */
1383   if (DF_SIZE < DI_SIZE
1384       && DF_SIZE > (DI_SIZE - DF_SIZE + SF_SIZE))
1385     {
1386       if (! (- ((DWtype) 1 << DF_SIZE) < u
1387              && u < ((DWtype) 1 << DF_SIZE)))
1388         {
1389           if ((UDWtype) u & (REP_BIT - 1))
1390             {
1391               u &= ~ (REP_BIT - 1);
1392               u |= REP_BIT;
1393             }
1394         }
1395     }
1396
1397   /* Do the calculation in DFmode so that we don't lose any of the
1398      precision of the high word while multiplying it.  */
1399   DFtype f = (Wtype) (u >> W_TYPE_SIZE);
1400   f *= Wtype_MAXp1_F;
1401   f += (UWtype)u;
1402   return (SFtype) f;
1403 #else
1404   /* Finally, the word size is larger than the number of bits in SFmode,
1405      and we've got no DFmode.  The only way to avoid double rounding is
1406      to special case the extraction.  */
1407
1408   /* If there are no high bits set, fall back to one conversion.  */
1409   if ((Wtype)u == u)
1410     return (SFtype)(Wtype)u;
1411
1412   /* Otherwise, find the power of two.  */
1413   Wtype hi = u >> W_TYPE_SIZE;
1414   if (hi < 0)
1415     hi = -hi;
1416
1417   UWtype count, shift;
1418   count_leading_zeros (count, hi);
1419
1420   /* No leading bits means u == minimum.  */
1421   if (count == 0)
1422     return -(Wtype_MAXp1_F * Wtype_MAXp1_F / 2);
1423
1424   shift = W_TYPE_SIZE - count;
1425
1426   /* Shift down the most significant bits.  */
1427   hi = u >> shift;
1428
1429   /* If we lost any nonzero bits, set the lsb to ensure correct rounding.  */
1430   if (u & ((1 << shift) - 1))
1431     hi |= 1;
1432
1433   /* Convert the one word of data, and rescale.  */
1434   SFtype f = hi;
1435   f *= (UWtype)1 << shift;
1436   return f;
1437 #endif
1438 }
1439 #endif
1440
1441 #if defined(L_fixunsxfsi) && LIBGCC2_HAS_XF_MODE
1442 /* Reenable the normal types, in case limits.h needs them.  */
1443 #undef char
1444 #undef short
1445 #undef int
1446 #undef long
1447 #undef unsigned
1448 #undef float
1449 #undef double
1450 #undef MIN
1451 #undef MAX
1452 #include <limits.h>
1453
1454 UWtype
1455 __fixunsxfSI (XFtype a)
1456 {
1457   if (a >= - (DFtype) Wtype_MIN)
1458     return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1459   return (Wtype) a;
1460 }
1461 #endif
1462
1463 #if defined(L_fixunsdfsi) && LIBGCC2_HAS_DF_MODE
1464 /* Reenable the normal types, in case limits.h needs them.  */
1465 #undef char
1466 #undef short
1467 #undef int
1468 #undef long
1469 #undef unsigned
1470 #undef float
1471 #undef double
1472 #undef MIN
1473 #undef MAX
1474 #include <limits.h>
1475
1476 UWtype
1477 __fixunsdfSI (DFtype a)
1478 {
1479   if (a >= - (DFtype) Wtype_MIN)
1480     return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1481   return (Wtype) a;
1482 }
1483 #endif
1484
1485 #if defined(L_fixunssfsi) && LIBGCC2_HAS_SF_MODE
1486 /* Reenable the normal types, in case limits.h needs them.  */
1487 #undef char
1488 #undef short
1489 #undef int
1490 #undef long
1491 #undef unsigned
1492 #undef float
1493 #undef double
1494 #undef MIN
1495 #undef MAX
1496 #include <limits.h>
1497
1498 UWtype
1499 __fixunssfSI (SFtype a)
1500 {
1501   if (a >= - (SFtype) Wtype_MIN)
1502     return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1503   return (Wtype) a;
1504 }
1505 #endif
1506 \f
1507 /* Integer power helper used from __builtin_powi for non-constant
1508    exponents.  */
1509
1510 #if (defined(L_powisf2) && LIBGCC2_HAS_SF_MODE) \
1511     || (defined(L_powidf2) && LIBGCC2_HAS_DF_MODE) \
1512     || (defined(L_powixf2) && LIBGCC2_HAS_XF_MODE) \
1513     || (defined(L_powitf2) && LIBGCC2_HAS_TF_MODE)
1514 # if defined(L_powisf2)
1515 #  define TYPE SFtype
1516 #  define NAME __powisf2
1517 # elif defined(L_powidf2)
1518 #  define TYPE DFtype
1519 #  define NAME __powidf2
1520 # elif defined(L_powixf2)
1521 #  define TYPE XFtype
1522 #  define NAME __powixf2
1523 # elif defined(L_powitf2)
1524 #  define TYPE TFtype
1525 #  define NAME __powitf2
1526 # endif
1527
1528 #undef int
1529 #undef unsigned
1530 TYPE
1531 NAME (TYPE x, int m)
1532 {
1533   unsigned int n = m < 0 ? -m : m;
1534   TYPE y = n % 2 ? x : 1;
1535   while (n >>= 1)
1536     {
1537       x = x * x;
1538       if (n % 2)
1539         y = y * x;
1540     }
1541   return m < 0 ? 1/y : y;
1542 }
1543
1544 #endif
1545 \f
1546 #if ((defined(L_mulsc3) || defined(L_divsc3)) && LIBGCC2_HAS_SF_MODE) \
1547     || ((defined(L_muldc3) || defined(L_divdc3)) && LIBGCC2_HAS_DF_MODE) \
1548     || ((defined(L_mulxc3) || defined(L_divxc3)) && LIBGCC2_HAS_XF_MODE) \
1549     || ((defined(L_multc3) || defined(L_divtc3)) && LIBGCC2_HAS_TF_MODE)
1550
1551 #undef float
1552 #undef double
1553 #undef long
1554
1555 #if defined(L_mulsc3) || defined(L_divsc3)
1556 # define MTYPE  SFtype
1557 # define CTYPE  SCtype
1558 # define MODE   sc
1559 # define CEXT   f
1560 # define NOTRUNC __FLT_EVAL_METHOD__ == 0
1561 #elif defined(L_muldc3) || defined(L_divdc3)
1562 # define MTYPE  DFtype
1563 # define CTYPE  DCtype
1564 # define MODE   dc
1565 # if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 64
1566 #  define CEXT  l
1567 #  define NOTRUNC 1
1568 # else
1569 #  define CEXT
1570 #  define NOTRUNC __FLT_EVAL_METHOD__ == 0 || __FLT_EVAL_METHOD__ == 1
1571 # endif
1572 #elif defined(L_mulxc3) || defined(L_divxc3)
1573 # define MTYPE  XFtype
1574 # define CTYPE  XCtype
1575 # define MODE   xc
1576 # define CEXT   l
1577 # define NOTRUNC 1
1578 #elif defined(L_multc3) || defined(L_divtc3)
1579 # define MTYPE  TFtype
1580 # define CTYPE  TCtype
1581 # define MODE   tc
1582 # define CEXT   l
1583 # define NOTRUNC 1
1584 #else
1585 # error
1586 #endif
1587
1588 #define CONCAT3(A,B,C)  _CONCAT3(A,B,C)
1589 #define _CONCAT3(A,B,C) A##B##C
1590
1591 #define CONCAT2(A,B)    _CONCAT2(A,B)
1592 #define _CONCAT2(A,B)   A##B
1593
1594 /* All of these would be present in a full C99 implementation of <math.h>
1595    and <complex.h>.  Our problem is that only a few systems have such full
1596    implementations.  Further, libgcc_s.so isn't currently linked against
1597    libm.so, and even for systems that do provide full C99, the extra overhead
1598    of all programs using libgcc having to link against libm.  So avoid it.  */
1599
1600 #define isnan(x)        __builtin_expect ((x) != (x), 0)
1601 #define isfinite(x)     __builtin_expect (!isnan((x) - (x)), 1)
1602 #define isinf(x)        __builtin_expect (!isnan(x) & !isfinite(x), 0)
1603
1604 #define INFINITY        CONCAT2(__builtin_inf, CEXT) ()
1605 #define I               1i
1606
1607 /* Helpers to make the following code slightly less gross.  */
1608 #define COPYSIGN        CONCAT2(__builtin_copysign, CEXT)
1609 #define FABS            CONCAT2(__builtin_fabs, CEXT)
1610
1611 /* Verify that MTYPE matches up with CEXT.  */
1612 extern void *compile_type_assert[sizeof(INFINITY) == sizeof(MTYPE) ? 1 : -1];
1613
1614 /* Ensure that we've lost any extra precision.  */
1615 #if NOTRUNC
1616 # define TRUNC(x)
1617 #else
1618 # define TRUNC(x)       __asm__ ("" : "=m"(x) : "m"(x))
1619 #endif
1620
1621 #if defined(L_mulsc3) || defined(L_muldc3) \
1622     || defined(L_mulxc3) || defined(L_multc3)
1623
1624 CTYPE
1625 CONCAT3(__mul,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
1626 {
1627   MTYPE ac, bd, ad, bc, x, y;
1628
1629   ac = a * c;
1630   bd = b * d;
1631   ad = a * d;
1632   bc = b * c;
1633
1634   TRUNC (ac);
1635   TRUNC (bd);
1636   TRUNC (ad);
1637   TRUNC (bc);
1638
1639   x = ac - bd;
1640   y = ad + bc;
1641
1642   if (isnan (x) && isnan (y))
1643     {
1644       /* Recover infinities that computed as NaN + iNaN.  */
1645       _Bool recalc = 0;
1646       if (isinf (a) || isinf (b))
1647         {
1648           /* z is infinite.  "Box" the infinity and change NaNs in
1649              the other factor to 0.  */
1650           a = COPYSIGN (isinf (a) ? 1 : 0, a);
1651           b = COPYSIGN (isinf (b) ? 1 : 0, b);
1652           if (isnan (c)) c = COPYSIGN (0, c);
1653           if (isnan (d)) d = COPYSIGN (0, d);
1654           recalc = 1;
1655         }
1656      if (isinf (c) || isinf (d))
1657         {
1658           /* w is infinite.  "Box" the infinity and change NaNs in
1659              the other factor to 0.  */
1660           c = COPYSIGN (isinf (c) ? 1 : 0, c);
1661           d = COPYSIGN (isinf (d) ? 1 : 0, d);
1662           if (isnan (a)) a = COPYSIGN (0, a);
1663           if (isnan (b)) b = COPYSIGN (0, b);
1664           recalc = 1;
1665         }
1666      if (!recalc
1667           && (isinf (ac) || isinf (bd)
1668               || isinf (ad) || isinf (bc)))
1669         {
1670           /* Recover infinities from overflow by changing NaNs to 0.  */
1671           if (isnan (a)) a = COPYSIGN (0, a);
1672           if (isnan (b)) b = COPYSIGN (0, b);
1673           if (isnan (c)) c = COPYSIGN (0, c);
1674           if (isnan (d)) d = COPYSIGN (0, d);
1675           recalc = 1;
1676         }
1677       if (recalc)
1678         {
1679           x = INFINITY * (a * c - b * d);
1680           y = INFINITY * (a * d + b * c);
1681         }
1682     }
1683
1684   return x + I * y;
1685 }
1686 #endif /* complex multiply */
1687
1688 #if defined(L_divsc3) || defined(L_divdc3) \
1689     || defined(L_divxc3) || defined(L_divtc3)
1690
1691 CTYPE
1692 CONCAT3(__div,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
1693 {
1694   MTYPE denom, ratio, x, y;
1695
1696   /* ??? We can get better behavior from logarithmic scaling instead of 
1697      the division.  But that would mean starting to link libgcc against
1698      libm.  We could implement something akin to ldexp/frexp as gcc builtins
1699      fairly easily...  */
1700   if (FABS (c) < FABS (d))
1701     {
1702       ratio = c / d;
1703       denom = (c * ratio) + d;
1704       x = ((a * ratio) + b) / denom;
1705       y = ((b * ratio) - a) / denom;
1706     }
1707   else
1708     {
1709       ratio = d / c;
1710       denom = (d * ratio) + c;
1711       x = ((b * ratio) + a) / denom;
1712       y = (b - (a * ratio)) / denom;
1713     }
1714
1715   /* Recover infinities and zeros that computed as NaN+iNaN; the only cases
1716      are nonzero/zero, infinite/finite, and finite/infinite.  */
1717   if (isnan (x) && isnan (y))
1718     {
1719       if (denom == 0.0 && (!isnan (a) || !isnan (b)))
1720         {
1721           x = COPYSIGN (INFINITY, c) * a;
1722           y = COPYSIGN (INFINITY, c) * b;
1723         }
1724       else if ((isinf (a) || isinf (b)) && isfinite (c) && isfinite (d))
1725         {
1726           a = COPYSIGN (isinf (a) ? 1 : 0, a);
1727           b = COPYSIGN (isinf (b) ? 1 : 0, b);
1728           x = INFINITY * (a * c + b * d);
1729           y = INFINITY * (b * c - a * d);
1730         }
1731       else if ((isinf (c) || isinf (d)) && isfinite (a) && isfinite (b))
1732         {
1733           c = COPYSIGN (isinf (c) ? 1 : 0, c);
1734           d = COPYSIGN (isinf (d) ? 1 : 0, d);
1735           x = 0.0 * (a * c + b * d);
1736           y = 0.0 * (b * c - a * d);
1737         }
1738     }
1739
1740   return x + I * y;
1741 }
1742 #endif /* complex divide */
1743
1744 #endif /* all complex float routines */
1745 \f
1746 /* From here on down, the routines use normal data types.  */
1747
1748 #define SItype bogus_type
1749 #define USItype bogus_type
1750 #define DItype bogus_type
1751 #define UDItype bogus_type
1752 #define SFtype bogus_type
1753 #define DFtype bogus_type
1754 #undef Wtype
1755 #undef UWtype
1756 #undef HWtype
1757 #undef UHWtype
1758 #undef DWtype
1759 #undef UDWtype
1760
1761 #undef char
1762 #undef short
1763 #undef int
1764 #undef long
1765 #undef unsigned
1766 #undef float
1767 #undef double
1768 \f
1769 #ifdef L__gcc_bcmp
1770
1771 /* Like bcmp except the sign is meaningful.
1772    Result is negative if S1 is less than S2,
1773    positive if S1 is greater, 0 if S1 and S2 are equal.  */
1774
1775 int
1776 __gcc_bcmp (const unsigned char *s1, const unsigned char *s2, size_t size)
1777 {
1778   while (size > 0)
1779     {
1780       const unsigned char c1 = *s1++, c2 = *s2++;
1781       if (c1 != c2)
1782         return c1 - c2;
1783       size--;
1784     }
1785   return 0;
1786 }
1787
1788 #endif
1789 \f
1790 /* __eprintf used to be used by GCC's private version of <assert.h>.
1791    We no longer provide that header, but this routine remains in libgcc.a
1792    for binary backward compatibility.  Note that it is not included in
1793    the shared version of libgcc.  */
1794 #ifdef L_eprintf
1795 #ifndef inhibit_libc
1796
1797 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch.  */
1798 #include <stdio.h>
1799
1800 void
1801 __eprintf (const char *string, const char *expression,
1802            unsigned int line, const char *filename)
1803 {
1804   fprintf (stderr, string, expression, line, filename);
1805   fflush (stderr);
1806   abort ();
1807 }
1808
1809 #endif
1810 #endif
1811
1812 \f
1813 #ifdef L_clear_cache
1814 /* Clear part of an instruction cache.  */
1815
1816 void
1817 __clear_cache (char *beg __attribute__((__unused__)),
1818                char *end __attribute__((__unused__)))
1819 {
1820 #ifdef CLEAR_INSN_CACHE
1821   CLEAR_INSN_CACHE (beg, end);
1822 #endif /* CLEAR_INSN_CACHE */
1823 }
1824
1825 #endif /* L_clear_cache */
1826 \f
1827 #ifdef L_enable_execute_stack
1828 /* Attempt to turn on execute permission for the stack.  */
1829
1830 #ifdef ENABLE_EXECUTE_STACK
1831   ENABLE_EXECUTE_STACK
1832 #else
1833 void
1834 __enable_execute_stack (void *addr __attribute__((__unused__)))
1835 {}
1836 #endif /* ENABLE_EXECUTE_STACK */
1837
1838 #endif /* L_enable_execute_stack */
1839 \f
1840 #ifdef L_trampoline
1841
1842 /* Jump to a trampoline, loading the static chain address.  */
1843
1844 #if defined(WINNT) && ! defined(__CYGWIN__) && ! defined (_UWIN)
1845
1846 int
1847 getpagesize (void)
1848 {
1849 #ifdef _ALPHA_
1850   return 8192;
1851 #else
1852   return 4096;
1853 #endif
1854 }
1855
1856 #ifdef __i386__
1857 extern int VirtualProtect (char *, int, int, int *) __attribute__((stdcall));
1858 #endif
1859
1860 int
1861 mprotect (char *addr, int len, int prot)
1862 {
1863   int np, op;
1864
1865   if (prot == 7)
1866     np = 0x40;
1867   else if (prot == 5)
1868     np = 0x20;
1869   else if (prot == 4)
1870     np = 0x10;
1871   else if (prot == 3)
1872     np = 0x04;
1873   else if (prot == 1)
1874     np = 0x02;
1875   else if (prot == 0)
1876     np = 0x01;
1877
1878   if (VirtualProtect (addr, len, np, &op))
1879     return 0;
1880   else
1881     return -1;
1882 }
1883
1884 #endif /* WINNT && ! __CYGWIN__ && ! _UWIN */
1885
1886 #ifdef TRANSFER_FROM_TRAMPOLINE
1887 TRANSFER_FROM_TRAMPOLINE
1888 #endif
1889 #endif /* L_trampoline */
1890 \f
1891 #ifndef __CYGWIN__
1892 #ifdef L__main
1893
1894 #include "gbl-ctors.h"
1895
1896 /* Some systems use __main in a way incompatible with its use in gcc, in these
1897    cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
1898    give the same symbol without quotes for an alternative entry point.  You
1899    must define both, or neither.  */
1900 #ifndef NAME__MAIN
1901 #define NAME__MAIN "__main"
1902 #define SYMBOL__MAIN __main
1903 #endif
1904
1905 #if defined (INIT_SECTION_ASM_OP) || defined (INIT_ARRAY_SECTION_ASM_OP)
1906 #undef HAS_INIT_SECTION
1907 #define HAS_INIT_SECTION
1908 #endif
1909
1910 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
1911
1912 /* Some ELF crosses use crtstuff.c to provide __CTOR_LIST__, but use this
1913    code to run constructors.  In that case, we need to handle EH here, too.  */
1914
1915 #ifdef EH_FRAME_SECTION_NAME
1916 #include "unwind-dw2-fde.h"
1917 extern unsigned char __EH_FRAME_BEGIN__[];
1918 #endif
1919
1920 /* Run all the global destructors on exit from the program.  */
1921
1922 void
1923 __do_global_dtors (void)
1924 {
1925 #ifdef DO_GLOBAL_DTORS_BODY
1926   DO_GLOBAL_DTORS_BODY;
1927 #else
1928   static func_ptr *p = __DTOR_LIST__ + 1;
1929   while (*p)
1930     {
1931       p++;
1932       (*(p-1)) ();
1933     }
1934 #endif
1935 #if defined (EH_FRAME_SECTION_NAME) && !defined (HAS_INIT_SECTION)
1936   {
1937     static int completed = 0;
1938     if (! completed)
1939       {
1940         completed = 1;
1941         __deregister_frame_info (__EH_FRAME_BEGIN__);
1942       }
1943   }
1944 #endif
1945 }
1946 #endif
1947
1948 #ifndef HAS_INIT_SECTION
1949 /* Run all the global constructors on entry to the program.  */
1950
1951 void
1952 __do_global_ctors (void)
1953 {
1954 #ifdef EH_FRAME_SECTION_NAME
1955   {
1956     static struct object object;
1957     __register_frame_info (__EH_FRAME_BEGIN__, &object);
1958   }
1959 #endif
1960   DO_GLOBAL_CTORS_BODY;
1961   atexit (__do_global_dtors);
1962 }
1963 #endif /* no HAS_INIT_SECTION */
1964
1965 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
1966 /* Subroutine called automatically by `main'.
1967    Compiling a global function named `main'
1968    produces an automatic call to this function at the beginning.
1969
1970    For many systems, this routine calls __do_global_ctors.
1971    For systems which support a .init section we use the .init section
1972    to run __do_global_ctors, so we need not do anything here.  */
1973
1974 extern void SYMBOL__MAIN (void);
1975 void
1976 SYMBOL__MAIN (void)
1977 {
1978   /* Support recursive calls to `main': run initializers just once.  */
1979   static int initialized;
1980   if (! initialized)
1981     {
1982       initialized = 1;
1983       __do_global_ctors ();
1984     }
1985 }
1986 #endif /* no HAS_INIT_SECTION or INVOKE__main */
1987
1988 #endif /* L__main */
1989 #endif /* __CYGWIN__ */
1990 \f
1991 #ifdef L_ctors
1992
1993 #include "gbl-ctors.h"
1994
1995 /* Provide default definitions for the lists of constructors and
1996    destructors, so that we don't get linker errors.  These symbols are
1997    intentionally bss symbols, so that gld and/or collect will provide
1998    the right values.  */
1999
2000 /* We declare the lists here with two elements each,
2001    so that they are valid empty lists if no other definition is loaded.
2002
2003    If we are using the old "set" extensions to have the gnu linker
2004    collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
2005    must be in the bss/common section.
2006
2007    Long term no port should use those extensions.  But many still do.  */
2008 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
2009 #if defined (TARGET_ASM_CONSTRUCTOR) || defined (USE_COLLECT2)
2010 func_ptr __CTOR_LIST__[2] = {0, 0};
2011 func_ptr __DTOR_LIST__[2] = {0, 0};
2012 #else
2013 func_ptr __CTOR_LIST__[2];
2014 func_ptr __DTOR_LIST__[2];
2015 #endif
2016 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
2017 #endif /* L_ctors */