OSDN Git Service

2005-12-16 Paolo Bonzini <bonzini@gnu.org>
[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, 51 Franklin Street, Fifth Floor, Boston, MA
30 02110-1301, 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[256] =
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 #ifdef L_popcount_tab
729 const UQItype __popcount_tab[256] =
730 {
731     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,
732     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,
733     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,
734     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,
735     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,
736     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,
737     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,
738     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
739 };
740 #endif
741 \f
742 #ifdef L_popcountsi2
743 #undef int
744 int
745 __popcountSI2 (UWtype x)
746 {
747   int i, ret = 0;
748
749   for (i = 0; i < W_TYPE_SIZE; i += 8)
750     ret += __popcount_tab[(x >> i) & 0xff];
751
752   return ret;
753 }
754 #endif
755 \f
756 #ifdef L_popcountdi2
757 #undef int
758 int
759 __popcountDI2 (UDWtype x)
760 {
761   int i, ret = 0;
762
763   for (i = 0; i < 2*W_TYPE_SIZE; i += 8)
764     ret += __popcount_tab[(x >> i) & 0xff];
765
766   return ret;
767 }
768 #endif
769 \f
770 #ifdef L_paritysi2
771 #undef int
772 int
773 __paritySI2 (UWtype x)
774 {
775 #if W_TYPE_SIZE > 64
776 # error "fill out the table"
777 #endif
778 #if W_TYPE_SIZE > 32
779   x ^= x >> 32;
780 #endif
781 #if W_TYPE_SIZE > 16
782   x ^= x >> 16;
783 #endif
784   x ^= x >> 8;
785   x ^= x >> 4;
786   x &= 0xf;
787   return (0x6996 >> x) & 1;
788 }
789 #endif
790 \f
791 #ifdef L_paritydi2
792 #undef int
793 int
794 __parityDI2 (UDWtype x)
795 {
796   const DWunion uu = {.ll = x};
797   UWtype nx = uu.s.low ^ uu.s.high;
798
799 #if W_TYPE_SIZE > 64
800 # error "fill out the table"
801 #endif
802 #if W_TYPE_SIZE > 32
803   nx ^= nx >> 32;
804 #endif
805 #if W_TYPE_SIZE > 16
806   nx ^= nx >> 16;
807 #endif
808   nx ^= nx >> 8;
809   nx ^= nx >> 4;
810   nx &= 0xf;
811   return (0x6996 >> nx) & 1;
812 }
813 #endif
814
815 #ifdef L_udivmoddi4
816
817 #if (defined (L_udivdi3) || defined (L_divdi3) || \
818      defined (L_umoddi3) || defined (L_moddi3))
819 static inline __attribute__ ((__always_inline__))
820 #endif
821 UDWtype
822 __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
823 {
824   const DWunion nn = {.ll = n};
825   const DWunion dd = {.ll = d};
826   DWunion rr;
827   UWtype d0, d1, n0, n1, n2;
828   UWtype q0, q1;
829   UWtype b, bm;
830
831   d0 = dd.s.low;
832   d1 = dd.s.high;
833   n0 = nn.s.low;
834   n1 = nn.s.high;
835
836 #if !UDIV_NEEDS_NORMALIZATION
837   if (d1 == 0)
838     {
839       if (d0 > n1)
840         {
841           /* 0q = nn / 0D */
842
843           udiv_qrnnd (q0, n0, n1, n0, d0);
844           q1 = 0;
845
846           /* Remainder in n0.  */
847         }
848       else
849         {
850           /* qq = NN / 0d */
851
852           if (d0 == 0)
853             d0 = 1 / d0;        /* Divide intentionally by zero.  */
854
855           udiv_qrnnd (q1, n1, 0, n1, d0);
856           udiv_qrnnd (q0, n0, n1, n0, d0);
857
858           /* Remainder in n0.  */
859         }
860
861       if (rp != 0)
862         {
863           rr.s.low = n0;
864           rr.s.high = 0;
865           *rp = rr.ll;
866         }
867     }
868
869 #else /* UDIV_NEEDS_NORMALIZATION */
870
871   if (d1 == 0)
872     {
873       if (d0 > n1)
874         {
875           /* 0q = nn / 0D */
876
877           count_leading_zeros (bm, d0);
878
879           if (bm != 0)
880             {
881               /* Normalize, i.e. make the most significant bit of the
882                  denominator set.  */
883
884               d0 = d0 << bm;
885               n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm));
886               n0 = n0 << bm;
887             }
888
889           udiv_qrnnd (q0, n0, n1, n0, d0);
890           q1 = 0;
891
892           /* Remainder in n0 >> bm.  */
893         }
894       else
895         {
896           /* qq = NN / 0d */
897
898           if (d0 == 0)
899             d0 = 1 / d0;        /* Divide intentionally by zero.  */
900
901           count_leading_zeros (bm, d0);
902
903           if (bm == 0)
904             {
905               /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
906                  conclude (the most significant bit of n1 is set) /\ (the
907                  leading quotient digit q1 = 1).
908
909                  This special case is necessary, not an optimization.
910                  (Shifts counts of W_TYPE_SIZE are undefined.)  */
911
912               n1 -= d0;
913               q1 = 1;
914             }
915           else
916             {
917               /* Normalize.  */
918
919               b = W_TYPE_SIZE - bm;
920
921               d0 = d0 << bm;
922               n2 = n1 >> b;
923               n1 = (n1 << bm) | (n0 >> b);
924               n0 = n0 << bm;
925
926               udiv_qrnnd (q1, n1, n2, n1, d0);
927             }
928
929           /* n1 != d0...  */
930
931           udiv_qrnnd (q0, n0, n1, n0, d0);
932
933           /* Remainder in n0 >> bm.  */
934         }
935
936       if (rp != 0)
937         {
938           rr.s.low = n0 >> bm;
939           rr.s.high = 0;
940           *rp = rr.ll;
941         }
942     }
943 #endif /* UDIV_NEEDS_NORMALIZATION */
944
945   else
946     {
947       if (d1 > n1)
948         {
949           /* 00 = nn / DD */
950
951           q0 = 0;
952           q1 = 0;
953
954           /* Remainder in n1n0.  */
955           if (rp != 0)
956             {
957               rr.s.low = n0;
958               rr.s.high = n1;
959               *rp = rr.ll;
960             }
961         }
962       else
963         {
964           /* 0q = NN / dd */
965
966           count_leading_zeros (bm, d1);
967           if (bm == 0)
968             {
969               /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
970                  conclude (the most significant bit of n1 is set) /\ (the
971                  quotient digit q0 = 0 or 1).
972
973                  This special case is necessary, not an optimization.  */
974
975               /* The condition on the next line takes advantage of that
976                  n1 >= d1 (true due to program flow).  */
977               if (n1 > d1 || n0 >= d0)
978                 {
979                   q0 = 1;
980                   sub_ddmmss (n1, n0, n1, n0, d1, d0);
981                 }
982               else
983                 q0 = 0;
984
985               q1 = 0;
986
987               if (rp != 0)
988                 {
989                   rr.s.low = n0;
990                   rr.s.high = n1;
991                   *rp = rr.ll;
992                 }
993             }
994           else
995             {
996               UWtype m1, m0;
997               /* Normalize.  */
998
999               b = W_TYPE_SIZE - bm;
1000
1001               d1 = (d1 << bm) | (d0 >> b);
1002               d0 = d0 << bm;
1003               n2 = n1 >> b;
1004               n1 = (n1 << bm) | (n0 >> b);
1005               n0 = n0 << bm;
1006
1007               udiv_qrnnd (q0, n1, n2, n1, d1);
1008               umul_ppmm (m1, m0, q0, d0);
1009
1010               if (m1 > n1 || (m1 == n1 && m0 > n0))
1011                 {
1012                   q0--;
1013                   sub_ddmmss (m1, m0, m1, m0, d1, d0);
1014                 }
1015
1016               q1 = 0;
1017
1018               /* Remainder in (n1n0 - m1m0) >> bm.  */
1019               if (rp != 0)
1020                 {
1021                   sub_ddmmss (n1, n0, n1, n0, m1, m0);
1022                   rr.s.low = (n1 << b) | (n0 >> bm);
1023                   rr.s.high = n1 >> bm;
1024                   *rp = rr.ll;
1025                 }
1026             }
1027         }
1028     }
1029
1030   const DWunion ww = {{.low = q0, .high = q1}};
1031   return ww.ll;
1032 }
1033 #endif
1034
1035 #ifdef L_divdi3
1036 DWtype
1037 __divdi3 (DWtype u, DWtype v)
1038 {
1039   word_type c = 0;
1040   DWunion uu = {.ll = u};
1041   DWunion vv = {.ll = v};
1042   DWtype w;
1043
1044   if (uu.s.high < 0)
1045     c = ~c,
1046     uu.ll = -uu.ll;
1047   if (vv.s.high < 0)
1048     c = ~c,
1049     vv.ll = -vv.ll;
1050
1051   w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0);
1052   if (c)
1053     w = -w;
1054
1055   return w;
1056 }
1057 #endif
1058
1059 #ifdef L_moddi3
1060 DWtype
1061 __moddi3 (DWtype u, DWtype v)
1062 {
1063   word_type c = 0;
1064   DWunion uu = {.ll = u};
1065   DWunion vv = {.ll = v};
1066   DWtype w;
1067
1068   if (uu.s.high < 0)
1069     c = ~c,
1070     uu.ll = -uu.ll;
1071   if (vv.s.high < 0)
1072     vv.ll = -vv.ll;
1073
1074   (void) __udivmoddi4 (uu.ll, vv.ll, (UDWtype*)&w);
1075   if (c)
1076     w = -w;
1077
1078   return w;
1079 }
1080 #endif
1081
1082 #ifdef L_umoddi3
1083 UDWtype
1084 __umoddi3 (UDWtype u, UDWtype v)
1085 {
1086   UDWtype w;
1087
1088   (void) __udivmoddi4 (u, v, &w);
1089
1090   return w;
1091 }
1092 #endif
1093
1094 #ifdef L_udivdi3
1095 UDWtype
1096 __udivdi3 (UDWtype n, UDWtype d)
1097 {
1098   return __udivmoddi4 (n, d, (UDWtype *) 0);
1099 }
1100 #endif
1101 \f
1102 #ifdef L_cmpdi2
1103 word_type
1104 __cmpdi2 (DWtype a, DWtype b)
1105 {
1106   const DWunion au = {.ll = a};
1107   const DWunion bu = {.ll = b};
1108
1109   if (au.s.high < bu.s.high)
1110     return 0;
1111   else if (au.s.high > bu.s.high)
1112     return 2;
1113   if ((UWtype) au.s.low < (UWtype) bu.s.low)
1114     return 0;
1115   else if ((UWtype) au.s.low > (UWtype) bu.s.low)
1116     return 2;
1117   return 1;
1118 }
1119 #endif
1120
1121 #ifdef L_ucmpdi2
1122 word_type
1123 __ucmpdi2 (DWtype a, DWtype b)
1124 {
1125   const DWunion au = {.ll = a};
1126   const DWunion bu = {.ll = b};
1127
1128   if ((UWtype) au.s.high < (UWtype) bu.s.high)
1129     return 0;
1130   else if ((UWtype) au.s.high > (UWtype) bu.s.high)
1131     return 2;
1132   if ((UWtype) au.s.low < (UWtype) bu.s.low)
1133     return 0;
1134   else if ((UWtype) au.s.low > (UWtype) bu.s.low)
1135     return 2;
1136   return 1;
1137 }
1138 #endif
1139 \f
1140 #if defined(L_fixunstfdi) && LIBGCC2_HAS_TF_MODE
1141 DWtype
1142 __fixunstfDI (TFtype a)
1143 {
1144   if (a < 0)
1145     return 0;
1146
1147   /* Compute high word of result, as a flonum.  */
1148   const TFtype b = (a / Wtype_MAXp1_F);
1149   /* Convert that to fixed (but not to DWtype!),
1150      and shift it into the high word.  */
1151   UDWtype v = (UWtype) b;
1152   v <<= W_TYPE_SIZE;
1153   /* Remove high part from the TFtype, leaving the low part as flonum.  */
1154   a -= (TFtype)v;
1155   /* Convert that to fixed (but not to DWtype!) and add it in.
1156      Sometimes A comes out negative.  This is significant, since
1157      A has more bits than a long int does.  */
1158   if (a < 0)
1159     v -= (UWtype) (- a);
1160   else
1161     v += (UWtype) a;
1162   return v;
1163 }
1164 #endif
1165
1166 #if defined(L_fixtfdi) && LIBGCC2_HAS_TF_MODE
1167 DWtype
1168 __fixtfdi (TFtype a)
1169 {
1170   if (a < 0)
1171     return - __fixunstfDI (-a);
1172   return __fixunstfDI (a);
1173 }
1174 #endif
1175
1176 #if defined(L_fixunsxfdi) && LIBGCC2_HAS_XF_MODE
1177 DWtype
1178 __fixunsxfDI (XFtype a)
1179 {
1180   if (a < 0)
1181     return 0;
1182
1183   /* Compute high word of result, as a flonum.  */
1184   const XFtype b = (a / Wtype_MAXp1_F);
1185   /* Convert that to fixed (but not to DWtype!),
1186      and shift it into the high word.  */
1187   UDWtype v = (UWtype) b;
1188   v <<= W_TYPE_SIZE;
1189   /* Remove high part from the XFtype, leaving the low part as flonum.  */
1190   a -= (XFtype)v;
1191   /* Convert that to fixed (but not to DWtype!) and add it in.
1192      Sometimes A comes out negative.  This is significant, since
1193      A has more bits than a long int does.  */
1194   if (a < 0)
1195     v -= (UWtype) (- a);
1196   else
1197     v += (UWtype) a;
1198   return v;
1199 }
1200 #endif
1201
1202 #if defined(L_fixxfdi) && LIBGCC2_HAS_XF_MODE
1203 DWtype
1204 __fixxfdi (XFtype a)
1205 {
1206   if (a < 0)
1207     return - __fixunsxfDI (-a);
1208   return __fixunsxfDI (a);
1209 }
1210 #endif
1211
1212 #if defined(L_fixunsdfdi) && LIBGCC2_HAS_DF_MODE
1213 DWtype
1214 __fixunsdfDI (DFtype a)
1215 {
1216   /* Get high part of result.  The division here will just moves the radix
1217      point and will not cause any rounding.  Then the conversion to integral
1218      type chops result as desired.  */
1219   const UWtype hi = a / Wtype_MAXp1_F;
1220
1221   /* Get low part of result.  Convert `hi' to floating type and scale it back,
1222      then subtract this from the number being converted.  This leaves the low
1223      part.  Convert that to integral type.  */
1224   const UWtype lo = a - (DFtype) hi * Wtype_MAXp1_F;
1225
1226   /* Assemble result from the two parts.  */
1227   return ((UDWtype) hi << W_TYPE_SIZE) | lo;
1228 }
1229 #endif
1230
1231 #if defined(L_fixdfdi) && LIBGCC2_HAS_DF_MODE
1232 DWtype
1233 __fixdfdi (DFtype a)
1234 {
1235   if (a < 0)
1236     return - __fixunsdfDI (-a);
1237   return __fixunsdfDI (a);
1238 }
1239 #endif
1240
1241 #if defined(L_fixunssfdi) && LIBGCC2_HAS_SF_MODE
1242 DWtype
1243 __fixunssfDI (SFtype a)
1244 {
1245 #if LIBGCC2_HAS_DF_MODE
1246   /* Convert the SFtype to a DFtype, because that is surely not going
1247      to lose any bits.  Some day someone else can write a faster version
1248      that avoids converting to DFtype, and verify it really works right.  */
1249   const DFtype dfa = a;
1250
1251   /* Get high part of result.  The division here will just moves the radix
1252      point and will not cause any rounding.  Then the conversion to integral
1253      type chops result as desired.  */
1254   const UWtype hi = dfa / Wtype_MAXp1_F;
1255
1256   /* Get low part of result.  Convert `hi' to floating type and scale it back,
1257      then subtract this from the number being converted.  This leaves the low
1258      part.  Convert that to integral type.  */
1259   const UWtype lo = dfa - (DFtype) hi * Wtype_MAXp1_F;
1260
1261   /* Assemble result from the two parts.  */
1262   return ((UDWtype) hi << W_TYPE_SIZE) | lo;
1263 #elif FLT_MANT_DIG < W_TYPE_SIZE
1264   if (a < 1)
1265     return 0;
1266   if (a < Wtype_MAXp1_F)
1267     return (UWtype)a;
1268   if (a < Wtype_MAXp1_F * Wtype_MAXp1_F)
1269     {
1270       /* Since we know that there are fewer significant bits in the SFmode
1271          quantity than in a word, we know that we can convert out all the
1272          significant bits in one step, and thus avoid losing bits.  */
1273
1274       /* ??? This following loop essentially performs frexpf.  If we could
1275          use the real libm function, or poke at the actual bits of the fp
1276          format, it would be significantly faster.  */
1277
1278       UWtype shift = 0, counter;
1279       SFtype msb;
1280
1281       a /= Wtype_MAXp1_F;
1282       for (counter = W_TYPE_SIZE / 2; counter != 0; counter >>= 1)
1283         {
1284           SFtype counterf = (UWtype)1 << counter;
1285           if (a >= counterf)
1286             {
1287               shift |= counter;
1288               a /= counterf;
1289             }
1290         }
1291
1292       /* Rescale into the range of one word, extract the bits of that
1293          one word, and shift the result into position.  */
1294       a *= Wtype_MAXp1_F;
1295       counter = a;
1296       return (DWtype)counter << shift;
1297     }
1298   return -1;
1299 #else
1300 # error
1301 #endif
1302 }
1303 #endif
1304
1305 #if defined(L_fixsfdi) && LIBGCC2_HAS_SF_MODE
1306 DWtype
1307 __fixsfdi (SFtype a)
1308 {
1309   if (a < 0)
1310     return - __fixunssfDI (-a);
1311   return __fixunssfDI (a);
1312 }
1313 #endif
1314
1315 #if defined(L_floatdixf) && LIBGCC2_HAS_XF_MODE
1316 XFtype
1317 __floatdixf (DWtype u)
1318 {
1319 #if W_TYPE_SIZE > XF_SIZE
1320 # error
1321 #endif
1322   XFtype d = (Wtype) (u >> W_TYPE_SIZE);
1323   d *= Wtype_MAXp1_F;
1324   d += (UWtype)u;
1325   return d;
1326 }
1327 #endif
1328
1329 #if defined(L_floatundixf) && LIBGCC2_HAS_XF_MODE
1330 XFtype
1331 __floatundixf (UDWtype u)
1332 {
1333 #if W_TYPE_SIZE > XF_SIZE
1334 # error
1335 #endif
1336   XFtype d = (UWtype) (u >> W_TYPE_SIZE);
1337   d *= Wtype_MAXp1_F;
1338   d += (UWtype)u;
1339   return d;
1340 }
1341 #endif
1342
1343 #if defined(L_floatditf) && LIBGCC2_HAS_TF_MODE
1344 TFtype
1345 __floatditf (DWtype u)
1346 {
1347 #if W_TYPE_SIZE > TF_SIZE
1348 # error
1349 #endif
1350   TFtype d = (Wtype) (u >> W_TYPE_SIZE);
1351   d *= Wtype_MAXp1_F;
1352   d += (UWtype)u;
1353   return d;
1354 }
1355 #endif
1356
1357 #if defined(L_floatunditf) && LIBGCC2_HAS_TF_MODE
1358 TFtype
1359 __floatunditf (UDWtype u)
1360 {
1361 #if W_TYPE_SIZE > TF_SIZE
1362 # error
1363 #endif
1364   TFtype d = (UWtype) (u >> W_TYPE_SIZE);
1365   d *= Wtype_MAXp1_F;
1366   d += (UWtype)u;
1367   return d;
1368 }
1369 #endif
1370
1371 #if (defined(L_floatdisf) && LIBGCC2_HAS_SF_MODE)       \
1372      || (defined(L_floatdidf) && LIBGCC2_HAS_DF_MODE)
1373 #define DI_SIZE (W_TYPE_SIZE * 2)
1374 #define F_MODE_OK(SIZE) (SIZE < DI_SIZE && SIZE > (DI_SIZE - SIZE + FSSIZE))
1375 #if defined(L_floatdisf)
1376 #define FUNC __floatdisf
1377 #define FSTYPE SFtype
1378 #define FSSIZE SF_SIZE
1379 #else
1380 #define FUNC __floatdidf
1381 #define FSTYPE DFtype
1382 #define FSSIZE DF_SIZE
1383 #endif
1384
1385 FSTYPE
1386 FUNC (DWtype u)
1387 {
1388 #if FSSIZE >= W_TYPE_SIZE
1389   /* When the word size is small, we never get any rounding error.  */
1390   FSTYPE f = (Wtype) (u >> W_TYPE_SIZE);
1391   f *= Wtype_MAXp1_F;
1392   f += (UWtype)u;
1393   return f;
1394 #elif (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE))      \
1395      || (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE))    \
1396      || (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1397
1398 #if (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE))
1399 # define FSIZE DF_SIZE
1400 # define FTYPE DFtype
1401 #elif (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE))
1402 # define FSIZE XF_SIZE
1403 # define FTYPE XFtype
1404 #elif (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1405 # define FSIZE TF_SIZE
1406 # define FTYPE TFtype
1407 #else
1408 # error
1409 #endif
1410
1411 #define REP_BIT ((UDWtype) 1 << (DI_SIZE - FSIZE))
1412
1413   /* Protect against double-rounding error.
1414      Represent any low-order bits, that might be truncated by a bit that
1415      won't be lost.  The bit can go in anywhere below the rounding position
1416      of the FSTYPE.  A fixed mask and bit position handles all usual
1417      configurations.  */
1418   if (! (- ((DWtype) 1 << FSIZE) < u
1419          && u < ((DWtype) 1 << FSIZE)))
1420     {
1421       if ((UDWtype) u & (REP_BIT - 1))
1422         {
1423           u &= ~ (REP_BIT - 1);
1424           u |= REP_BIT;
1425         }
1426     }
1427
1428   /* Do the calculation in a wider type so that we don't lose any of
1429      the precision of the high word while multiplying it.  */
1430   FTYPE f = (Wtype) (u >> W_TYPE_SIZE);
1431   f *= Wtype_MAXp1_F;
1432   f += (UWtype)u;
1433   return (FSTYPE) f;
1434 #else
1435 #if FSSIZE >= W_TYPE_SIZE - 2
1436 # error
1437 #endif
1438   /* Finally, the word size is larger than the number of bits in the
1439      required FSTYPE, and we've got no suitable wider type.  The only
1440      way to avoid double rounding is to special case the
1441      extraction.  */
1442
1443   /* If there are no high bits set, fall back to one conversion.  */
1444   if ((Wtype)u == u)
1445     return (FSTYPE)(Wtype)u;
1446
1447   /* Otherwise, find the power of two.  */
1448   Wtype hi = u >> W_TYPE_SIZE;
1449   if (hi < 0)
1450     hi = -hi;
1451
1452   UWtype count, shift;
1453   count_leading_zeros (count, hi);
1454
1455   /* No leading bits means u == minimum.  */
1456   if (count == 0)
1457     return -(Wtype_MAXp1_F * (Wtype_MAXp1_F / 2));
1458
1459   shift = 1 + W_TYPE_SIZE - count;
1460
1461   /* Shift down the most significant bits.  */
1462   hi = u >> shift;
1463
1464   /* If we lost any nonzero bits, set the lsb to ensure correct rounding.  */
1465   if (u & (((DWtype)1 << shift) - 1))
1466     hi |= 1;
1467
1468   /* Convert the one word of data, and rescale.  */
1469   FSTYPE f = hi;
1470   f *= (UDWtype)1 << shift;
1471   return f;
1472 #endif
1473 }
1474 #endif
1475
1476 #if (defined(L_floatundisf) && LIBGCC2_HAS_SF_MODE)     \
1477      || (defined(L_floatundidf) && LIBGCC2_HAS_DF_MODE)
1478 #define DI_SIZE (W_TYPE_SIZE * 2)
1479 #define F_MODE_OK(SIZE) (SIZE < DI_SIZE && SIZE > (DI_SIZE - SIZE + FSSIZE))
1480 #if defined(L_floatundisf)
1481 #define FUNC __floatundisf
1482 #define FSTYPE SFtype
1483 #define FSSIZE SF_SIZE
1484 #else
1485 #define FUNC __floatundidf
1486 #define FSTYPE DFtype
1487 #define FSSIZE DF_SIZE
1488 #endif
1489
1490 FSTYPE
1491 FUNC (UDWtype u)
1492 {
1493 #if FSSIZE >= W_TYPE_SIZE
1494   /* When the word size is small, we never get any rounding error.  */
1495   FSTYPE f = (UWtype) (u >> W_TYPE_SIZE);
1496   f *= Wtype_MAXp1_F;
1497   f += (UWtype)u;
1498   return f;
1499 #elif (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE))      \
1500      || (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE))    \
1501      || (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1502
1503 #if (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE))
1504 # define FSIZE DF_SIZE
1505 # define FTYPE DFtype
1506 #elif (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE))
1507 # define FSIZE XF_SIZE
1508 # define FTYPE XFtype
1509 #elif (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1510 # define FSIZE TF_SIZE
1511 # define FTYPE TFtype
1512 #else
1513 # error
1514 #endif
1515
1516 #define REP_BIT ((UDWtype) 1 << (DI_SIZE - FSIZE))
1517
1518   /* Protect against double-rounding error.
1519      Represent any low-order bits, that might be truncated by a bit that
1520      won't be lost.  The bit can go in anywhere below the rounding position
1521      of the FSTYPE.  A fixed mask and bit position handles all usual
1522      configurations.  */
1523   if (u >= ((UDWtype) 1 << FSIZE))
1524     {
1525       if ((UDWtype) u & (REP_BIT - 1))
1526         {
1527           u &= ~ (REP_BIT - 1);
1528           u |= REP_BIT;
1529         }
1530     }
1531
1532   /* Do the calculation in a wider type so that we don't lose any of
1533      the precision of the high word while multiplying it.  */
1534   FTYPE f = (UWtype) (u >> W_TYPE_SIZE);
1535   f *= Wtype_MAXp1_F;
1536   f += (UWtype)u;
1537   return (FSTYPE) f;
1538 #else
1539 #if FSSIZE == W_TYPE_SIZE - 1
1540 # error
1541 #endif
1542   /* Finally, the word size is larger than the number of bits in the
1543      required FSTYPE, and we've got no suitable wider type.  The only
1544      way to avoid double rounding is to special case the
1545      extraction.  */
1546
1547   /* If there are no high bits set, fall back to one conversion.  */
1548   if ((UWtype)u == u)
1549     return (FSTYPE)(UWtype)u;
1550
1551   /* Otherwise, find the power of two.  */
1552   UWtype hi = u >> W_TYPE_SIZE;
1553
1554   UWtype count, shift;
1555   count_leading_zeros (count, hi);
1556
1557   shift = W_TYPE_SIZE - count;
1558
1559   /* Shift down the most significant bits.  */
1560   hi = u >> shift;
1561
1562   /* If we lost any nonzero bits, set the lsb to ensure correct rounding.  */
1563   if (u & (((UDWtype)1 << shift) - 1))
1564     hi |= 1;
1565
1566   /* Convert the one word of data, and rescale.  */
1567   FSTYPE f = hi;
1568   f *= (UDWtype)1 << shift;
1569   return f;
1570 #endif
1571 }
1572 #endif
1573
1574 #if defined(L_fixunsxfsi) && LIBGCC2_HAS_XF_MODE
1575 /* Reenable the normal types, in case limits.h needs them.  */
1576 #undef char
1577 #undef short
1578 #undef int
1579 #undef long
1580 #undef unsigned
1581 #undef float
1582 #undef double
1583 #undef MIN
1584 #undef MAX
1585 #include <limits.h>
1586
1587 UWtype
1588 __fixunsxfSI (XFtype a)
1589 {
1590   if (a >= - (DFtype) Wtype_MIN)
1591     return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1592   return (Wtype) a;
1593 }
1594 #endif
1595
1596 #if defined(L_fixunsdfsi) && LIBGCC2_HAS_DF_MODE
1597 /* Reenable the normal types, in case limits.h needs them.  */
1598 #undef char
1599 #undef short
1600 #undef int
1601 #undef long
1602 #undef unsigned
1603 #undef float
1604 #undef double
1605 #undef MIN
1606 #undef MAX
1607 #include <limits.h>
1608
1609 UWtype
1610 __fixunsdfSI (DFtype a)
1611 {
1612   if (a >= - (DFtype) Wtype_MIN)
1613     return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1614   return (Wtype) a;
1615 }
1616 #endif
1617
1618 #if defined(L_fixunssfsi) && LIBGCC2_HAS_SF_MODE
1619 /* Reenable the normal types, in case limits.h needs them.  */
1620 #undef char
1621 #undef short
1622 #undef int
1623 #undef long
1624 #undef unsigned
1625 #undef float
1626 #undef double
1627 #undef MIN
1628 #undef MAX
1629 #include <limits.h>
1630
1631 UWtype
1632 __fixunssfSI (SFtype a)
1633 {
1634   if (a >= - (SFtype) Wtype_MIN)
1635     return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
1636   return (Wtype) a;
1637 }
1638 #endif
1639 \f
1640 /* Integer power helper used from __builtin_powi for non-constant
1641    exponents.  */
1642
1643 #if (defined(L_powisf2) && LIBGCC2_HAS_SF_MODE) \
1644     || (defined(L_powidf2) && LIBGCC2_HAS_DF_MODE) \
1645     || (defined(L_powixf2) && LIBGCC2_HAS_XF_MODE) \
1646     || (defined(L_powitf2) && LIBGCC2_HAS_TF_MODE)
1647 # if defined(L_powisf2)
1648 #  define TYPE SFtype
1649 #  define NAME __powisf2
1650 # elif defined(L_powidf2)
1651 #  define TYPE DFtype
1652 #  define NAME __powidf2
1653 # elif defined(L_powixf2)
1654 #  define TYPE XFtype
1655 #  define NAME __powixf2
1656 # elif defined(L_powitf2)
1657 #  define TYPE TFtype
1658 #  define NAME __powitf2
1659 # endif
1660
1661 #undef int
1662 #undef unsigned
1663 TYPE
1664 NAME (TYPE x, int m)
1665 {
1666   unsigned int n = m < 0 ? -m : m;
1667   TYPE y = n % 2 ? x : 1;
1668   while (n >>= 1)
1669     {
1670       x = x * x;
1671       if (n % 2)
1672         y = y * x;
1673     }
1674   return m < 0 ? 1/y : y;
1675 }
1676
1677 #endif
1678 \f
1679 #if ((defined(L_mulsc3) || defined(L_divsc3)) && LIBGCC2_HAS_SF_MODE) \
1680     || ((defined(L_muldc3) || defined(L_divdc3)) && LIBGCC2_HAS_DF_MODE) \
1681     || ((defined(L_mulxc3) || defined(L_divxc3)) && LIBGCC2_HAS_XF_MODE) \
1682     || ((defined(L_multc3) || defined(L_divtc3)) && LIBGCC2_HAS_TF_MODE)
1683
1684 #undef float
1685 #undef double
1686 #undef long
1687
1688 #if defined(L_mulsc3) || defined(L_divsc3)
1689 # define MTYPE  SFtype
1690 # define CTYPE  SCtype
1691 # define MODE   sc
1692 # define CEXT   f
1693 # define NOTRUNC __FLT_EVAL_METHOD__ == 0
1694 #elif defined(L_muldc3) || defined(L_divdc3)
1695 # define MTYPE  DFtype
1696 # define CTYPE  DCtype
1697 # define MODE   dc
1698 # if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 64
1699 #  define CEXT  l
1700 #  define NOTRUNC 1
1701 # else
1702 #  define CEXT
1703 #  define NOTRUNC __FLT_EVAL_METHOD__ == 0 || __FLT_EVAL_METHOD__ == 1
1704 # endif
1705 #elif defined(L_mulxc3) || defined(L_divxc3)
1706 # define MTYPE  XFtype
1707 # define CTYPE  XCtype
1708 # define MODE   xc
1709 # define CEXT   l
1710 # define NOTRUNC 1
1711 #elif defined(L_multc3) || defined(L_divtc3)
1712 # define MTYPE  TFtype
1713 # define CTYPE  TCtype
1714 # define MODE   tc
1715 # define CEXT   l
1716 # define NOTRUNC 1
1717 #else
1718 # error
1719 #endif
1720
1721 #define CONCAT3(A,B,C)  _CONCAT3(A,B,C)
1722 #define _CONCAT3(A,B,C) A##B##C
1723
1724 #define CONCAT2(A,B)    _CONCAT2(A,B)
1725 #define _CONCAT2(A,B)   A##B
1726
1727 /* All of these would be present in a full C99 implementation of <math.h>
1728    and <complex.h>.  Our problem is that only a few systems have such full
1729    implementations.  Further, libgcc_s.so isn't currently linked against
1730    libm.so, and even for systems that do provide full C99, the extra overhead
1731    of all programs using libgcc having to link against libm.  So avoid it.  */
1732
1733 #define isnan(x)        __builtin_expect ((x) != (x), 0)
1734 #define isfinite(x)     __builtin_expect (!isnan((x) - (x)), 1)
1735 #define isinf(x)        __builtin_expect (!isnan(x) & !isfinite(x), 0)
1736
1737 #define INFINITY        CONCAT2(__builtin_inf, CEXT) ()
1738 #define I               1i
1739
1740 /* Helpers to make the following code slightly less gross.  */
1741 #define COPYSIGN        CONCAT2(__builtin_copysign, CEXT)
1742 #define FABS            CONCAT2(__builtin_fabs, CEXT)
1743
1744 /* Verify that MTYPE matches up with CEXT.  */
1745 extern void *compile_type_assert[sizeof(INFINITY) == sizeof(MTYPE) ? 1 : -1];
1746
1747 /* Ensure that we've lost any extra precision.  */
1748 #if NOTRUNC
1749 # define TRUNC(x)
1750 #else
1751 # define TRUNC(x)       __asm__ ("" : "=m"(x) : "m"(x))
1752 #endif
1753
1754 #if defined(L_mulsc3) || defined(L_muldc3) \
1755     || defined(L_mulxc3) || defined(L_multc3)
1756
1757 CTYPE
1758 CONCAT3(__mul,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
1759 {
1760   MTYPE ac, bd, ad, bc, x, y;
1761
1762   ac = a * c;
1763   bd = b * d;
1764   ad = a * d;
1765   bc = b * c;
1766
1767   TRUNC (ac);
1768   TRUNC (bd);
1769   TRUNC (ad);
1770   TRUNC (bc);
1771
1772   x = ac - bd;
1773   y = ad + bc;
1774
1775   if (isnan (x) && isnan (y))
1776     {
1777       /* Recover infinities that computed as NaN + iNaN.  */
1778       _Bool recalc = 0;
1779       if (isinf (a) || isinf (b))
1780         {
1781           /* z is infinite.  "Box" the infinity and change NaNs in
1782              the other factor to 0.  */
1783           a = COPYSIGN (isinf (a) ? 1 : 0, a);
1784           b = COPYSIGN (isinf (b) ? 1 : 0, b);
1785           if (isnan (c)) c = COPYSIGN (0, c);
1786           if (isnan (d)) d = COPYSIGN (0, d);
1787           recalc = 1;
1788         }
1789      if (isinf (c) || isinf (d))
1790         {
1791           /* w is infinite.  "Box" the infinity and change NaNs in
1792              the other factor to 0.  */
1793           c = COPYSIGN (isinf (c) ? 1 : 0, c);
1794           d = COPYSIGN (isinf (d) ? 1 : 0, d);
1795           if (isnan (a)) a = COPYSIGN (0, a);
1796           if (isnan (b)) b = COPYSIGN (0, b);
1797           recalc = 1;
1798         }
1799      if (!recalc
1800           && (isinf (ac) || isinf (bd)
1801               || isinf (ad) || isinf (bc)))
1802         {
1803           /* Recover infinities from overflow by changing NaNs to 0.  */
1804           if (isnan (a)) a = COPYSIGN (0, a);
1805           if (isnan (b)) b = COPYSIGN (0, b);
1806           if (isnan (c)) c = COPYSIGN (0, c);
1807           if (isnan (d)) d = COPYSIGN (0, d);
1808           recalc = 1;
1809         }
1810       if (recalc)
1811         {
1812           x = INFINITY * (a * c - b * d);
1813           y = INFINITY * (a * d + b * c);
1814         }
1815     }
1816
1817   return x + I * y;
1818 }
1819 #endif /* complex multiply */
1820
1821 #if defined(L_divsc3) || defined(L_divdc3) \
1822     || defined(L_divxc3) || defined(L_divtc3)
1823
1824 CTYPE
1825 CONCAT3(__div,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
1826 {
1827   MTYPE denom, ratio, x, y;
1828
1829   /* ??? We can get better behavior from logarithmic scaling instead of 
1830      the division.  But that would mean starting to link libgcc against
1831      libm.  We could implement something akin to ldexp/frexp as gcc builtins
1832      fairly easily...  */
1833   if (FABS (c) < FABS (d))
1834     {
1835       ratio = c / d;
1836       denom = (c * ratio) + d;
1837       x = ((a * ratio) + b) / denom;
1838       y = ((b * ratio) - a) / denom;
1839     }
1840   else
1841     {
1842       ratio = d / c;
1843       denom = (d * ratio) + c;
1844       x = ((b * ratio) + a) / denom;
1845       y = (b - (a * ratio)) / denom;
1846     }
1847
1848   /* Recover infinities and zeros that computed as NaN+iNaN; the only cases
1849      are nonzero/zero, infinite/finite, and finite/infinite.  */
1850   if (isnan (x) && isnan (y))
1851     {
1852       if (denom == 0.0 && (!isnan (a) || !isnan (b)))
1853         {
1854           x = COPYSIGN (INFINITY, c) * a;
1855           y = COPYSIGN (INFINITY, c) * b;
1856         }
1857       else if ((isinf (a) || isinf (b)) && isfinite (c) && isfinite (d))
1858         {
1859           a = COPYSIGN (isinf (a) ? 1 : 0, a);
1860           b = COPYSIGN (isinf (b) ? 1 : 0, b);
1861           x = INFINITY * (a * c + b * d);
1862           y = INFINITY * (b * c - a * d);
1863         }
1864       else if ((isinf (c) || isinf (d)) && isfinite (a) && isfinite (b))
1865         {
1866           c = COPYSIGN (isinf (c) ? 1 : 0, c);
1867           d = COPYSIGN (isinf (d) ? 1 : 0, d);
1868           x = 0.0 * (a * c + b * d);
1869           y = 0.0 * (b * c - a * d);
1870         }
1871     }
1872
1873   return x + I * y;
1874 }
1875 #endif /* complex divide */
1876
1877 #endif /* all complex float routines */
1878 \f
1879 /* From here on down, the routines use normal data types.  */
1880
1881 #define SItype bogus_type
1882 #define USItype bogus_type
1883 #define DItype bogus_type
1884 #define UDItype bogus_type
1885 #define SFtype bogus_type
1886 #define DFtype bogus_type
1887 #undef Wtype
1888 #undef UWtype
1889 #undef HWtype
1890 #undef UHWtype
1891 #undef DWtype
1892 #undef UDWtype
1893
1894 #undef char
1895 #undef short
1896 #undef int
1897 #undef long
1898 #undef unsigned
1899 #undef float
1900 #undef double
1901 \f
1902 #ifdef L__gcc_bcmp
1903
1904 /* Like bcmp except the sign is meaningful.
1905    Result is negative if S1 is less than S2,
1906    positive if S1 is greater, 0 if S1 and S2 are equal.  */
1907
1908 int
1909 __gcc_bcmp (const unsigned char *s1, const unsigned char *s2, size_t size)
1910 {
1911   while (size > 0)
1912     {
1913       const unsigned char c1 = *s1++, c2 = *s2++;
1914       if (c1 != c2)
1915         return c1 - c2;
1916       size--;
1917     }
1918   return 0;
1919 }
1920
1921 #endif
1922 \f
1923 /* __eprintf used to be used by GCC's private version of <assert.h>.
1924    We no longer provide that header, but this routine remains in libgcc.a
1925    for binary backward compatibility.  Note that it is not included in
1926    the shared version of libgcc.  */
1927 #ifdef L_eprintf
1928 #ifndef inhibit_libc
1929
1930 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch.  */
1931 #include <stdio.h>
1932
1933 void
1934 __eprintf (const char *string, const char *expression,
1935            unsigned int line, const char *filename)
1936 {
1937   fprintf (stderr, string, expression, line, filename);
1938   fflush (stderr);
1939   abort ();
1940 }
1941
1942 #endif
1943 #endif
1944
1945 \f
1946 #ifdef L_clear_cache
1947 /* Clear part of an instruction cache.  */
1948
1949 void
1950 __clear_cache (char *beg __attribute__((__unused__)),
1951                char *end __attribute__((__unused__)))
1952 {
1953 #ifdef CLEAR_INSN_CACHE
1954   CLEAR_INSN_CACHE (beg, end);
1955 #endif /* CLEAR_INSN_CACHE */
1956 }
1957
1958 #endif /* L_clear_cache */
1959 \f
1960 #ifdef L_enable_execute_stack
1961 /* Attempt to turn on execute permission for the stack.  */
1962
1963 #ifdef ENABLE_EXECUTE_STACK
1964   ENABLE_EXECUTE_STACK
1965 #else
1966 void
1967 __enable_execute_stack (void *addr __attribute__((__unused__)))
1968 {}
1969 #endif /* ENABLE_EXECUTE_STACK */
1970
1971 #endif /* L_enable_execute_stack */
1972 \f
1973 #ifdef L_trampoline
1974
1975 /* Jump to a trampoline, loading the static chain address.  */
1976
1977 #if defined(WINNT) && ! defined(__CYGWIN__) && ! defined (_UWIN)
1978
1979 int
1980 getpagesize (void)
1981 {
1982 #ifdef _ALPHA_
1983   return 8192;
1984 #else
1985   return 4096;
1986 #endif
1987 }
1988
1989 #ifdef __i386__
1990 extern int VirtualProtect (char *, int, int, int *) __attribute__((stdcall));
1991 #endif
1992
1993 int
1994 mprotect (char *addr, int len, int prot)
1995 {
1996   int np, op;
1997
1998   if (prot == 7)
1999     np = 0x40;
2000   else if (prot == 5)
2001     np = 0x20;
2002   else if (prot == 4)
2003     np = 0x10;
2004   else if (prot == 3)
2005     np = 0x04;
2006   else if (prot == 1)
2007     np = 0x02;
2008   else if (prot == 0)
2009     np = 0x01;
2010
2011   if (VirtualProtect (addr, len, np, &op))
2012     return 0;
2013   else
2014     return -1;
2015 }
2016
2017 #endif /* WINNT && ! __CYGWIN__ && ! _UWIN */
2018
2019 #ifdef TRANSFER_FROM_TRAMPOLINE
2020 TRANSFER_FROM_TRAMPOLINE
2021 #endif
2022 #endif /* L_trampoline */
2023 \f
2024 #ifndef __CYGWIN__
2025 #ifdef L__main
2026
2027 #include "gbl-ctors.h"
2028
2029 /* Some systems use __main in a way incompatible with its use in gcc, in these
2030    cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
2031    give the same symbol without quotes for an alternative entry point.  You
2032    must define both, or neither.  */
2033 #ifndef NAME__MAIN
2034 #define NAME__MAIN "__main"
2035 #define SYMBOL__MAIN __main
2036 #endif
2037
2038 #if defined (INIT_SECTION_ASM_OP) || defined (INIT_ARRAY_SECTION_ASM_OP)
2039 #undef HAS_INIT_SECTION
2040 #define HAS_INIT_SECTION
2041 #endif
2042
2043 #if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
2044
2045 /* Some ELF crosses use crtstuff.c to provide __CTOR_LIST__, but use this
2046    code to run constructors.  In that case, we need to handle EH here, too.  */
2047
2048 #ifdef EH_FRAME_SECTION_NAME
2049 #include "unwind-dw2-fde.h"
2050 extern unsigned char __EH_FRAME_BEGIN__[];
2051 #endif
2052
2053 /* Run all the global destructors on exit from the program.  */
2054
2055 void
2056 __do_global_dtors (void)
2057 {
2058 #ifdef DO_GLOBAL_DTORS_BODY
2059   DO_GLOBAL_DTORS_BODY;
2060 #else
2061   static func_ptr *p = __DTOR_LIST__ + 1;
2062   while (*p)
2063     {
2064       p++;
2065       (*(p-1)) ();
2066     }
2067 #endif
2068 #if defined (EH_FRAME_SECTION_NAME) && !defined (HAS_INIT_SECTION)
2069   {
2070     static int completed = 0;
2071     if (! completed)
2072       {
2073         completed = 1;
2074         __deregister_frame_info (__EH_FRAME_BEGIN__);
2075       }
2076   }
2077 #endif
2078 }
2079 #endif
2080
2081 #ifndef HAS_INIT_SECTION
2082 /* Run all the global constructors on entry to the program.  */
2083
2084 void
2085 __do_global_ctors (void)
2086 {
2087 #ifdef EH_FRAME_SECTION_NAME
2088   {
2089     static struct object object;
2090     __register_frame_info (__EH_FRAME_BEGIN__, &object);
2091   }
2092 #endif
2093   DO_GLOBAL_CTORS_BODY;
2094   atexit (__do_global_dtors);
2095 }
2096 #endif /* no HAS_INIT_SECTION */
2097
2098 #if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
2099 /* Subroutine called automatically by `main'.
2100    Compiling a global function named `main'
2101    produces an automatic call to this function at the beginning.
2102
2103    For many systems, this routine calls __do_global_ctors.
2104    For systems which support a .init section we use the .init section
2105    to run __do_global_ctors, so we need not do anything here.  */
2106
2107 extern void SYMBOL__MAIN (void);
2108 void
2109 SYMBOL__MAIN (void)
2110 {
2111   /* Support recursive calls to `main': run initializers just once.  */
2112   static int initialized;
2113   if (! initialized)
2114     {
2115       initialized = 1;
2116       __do_global_ctors ();
2117     }
2118 }
2119 #endif /* no HAS_INIT_SECTION or INVOKE__main */
2120
2121 #endif /* L__main */
2122 #endif /* __CYGWIN__ */
2123 \f
2124 #ifdef L_ctors
2125
2126 #include "gbl-ctors.h"
2127
2128 /* Provide default definitions for the lists of constructors and
2129    destructors, so that we don't get linker errors.  These symbols are
2130    intentionally bss symbols, so that gld and/or collect will provide
2131    the right values.  */
2132
2133 /* We declare the lists here with two elements each,
2134    so that they are valid empty lists if no other definition is loaded.
2135
2136    If we are using the old "set" extensions to have the gnu linker
2137    collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
2138    must be in the bss/common section.
2139
2140    Long term no port should use those extensions.  But many still do.  */
2141 #if !defined(INIT_SECTION_ASM_OP) && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
2142 #if defined (TARGET_ASM_CONSTRUCTOR) || defined (USE_COLLECT2)
2143 func_ptr __CTOR_LIST__[2] = {0, 0};
2144 func_ptr __DTOR_LIST__[2] = {0, 0};
2145 #else
2146 func_ptr __CTOR_LIST__[2];
2147 func_ptr __DTOR_LIST__[2];
2148 #endif
2149 #endif /* no INIT_SECTION_ASM_OP and not CTOR_LISTS_DEFINED_EXTERNALLY */
2150 #endif /* L_ctors */