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