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