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