OSDN Git Service

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