]> git.ipfire.org Git - thirdparty/gcc.git/blame_incremental - libgcc/libgcc2.c
cobol: Repair printf format of size_t.
[thirdparty/gcc.git] / libgcc / libgcc2.c
... / ...
CommitLineData
1/* More subroutines needed by GCC output code on some machines. */
2/* Compile this one with gcc. */
3/* Copyright (C) 1989-2025 Free Software Foundation, Inc.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17Under Section 7 of GPL version 3, you are granted additional
18permissions described in the GCC Runtime Library Exception, version
193.1, as published by the Free Software Foundation.
20
21You should have received a copy of the GNU General Public License and
22a copy of the GCC Runtime Library Exception along with this program;
23see 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)
63DWtype
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
75Wtype
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
86SItype
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
100DWtype
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
113Wtype
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
124SItype
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
138DWtype
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
151Wtype
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
162SItype
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
176Wtype
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
187SItype
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
201DWtype
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
214Wtype
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
226SItype
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
241DWtype
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
255DWtype
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
383DWtype
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
411DWtype
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
439DWtype
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
468SItype
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
478DItype
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
493int
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
508int
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
527DWtype
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))
552static inline __attribute__ ((__always_inline__))
553#endif
554UWtype
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. */
652UWtype
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
670const 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
685int
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
698int
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
717int
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
730int
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
749int
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
765int
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
791const 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
821int
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
845int
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
875int
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
896int
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))
924static inline __attribute__ ((__always_inline__))
925#endif
926UDWtype
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))
992static inline __attribute__ ((__always_inline__))
993#endif
994UDWtype
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
1210DWtype
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
1234DWtype
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
1257DWtype
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
1285UDWtype
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
1297UDWtype
1298__udivdi3 (UDWtype n, UDWtype d)
1299{
1300 return __udivmoddi4 (n, d, (UDWtype *) 0);
1301}
1302#endif
1303\f
1304#if (defined(__BITINT_MAXWIDTH__) \
1305 && (defined(L_mulbitint3) || defined(L_divmodbitint4)))
1306/* _BitInt support. */
1307
1308/* If *P is zero or sign extended (the latter only for PREC < 0) from
1309 some narrower _BitInt value, reduce precision. */
1310
1311static inline __attribute__((__always_inline__)) SItype
1312bitint_reduce_prec (const UBILtype **p, SItype prec)
1313{
1314 UWtype mslimb;
1315 SItype i;
1316 if (prec < 0)
1317 {
1318#if __LIBGCC_BITINT_ORDER__ == __ORDER_BIG_ENDIAN__
1319 i = 0;
1320#else
1321 i = ((USItype) -1 - prec) / W_TYPE_SIZE;
1322#endif
1323 mslimb = (*p)[i];
1324 if (mslimb & ((UWtype) 1 << (((USItype) -1 - prec) % W_TYPE_SIZE)))
1325 {
1326 SItype n = ((USItype) -prec) % W_TYPE_SIZE;
1327 if (n)
1328 {
1329 mslimb |= ((UWtype) -1 << (((USItype) -1 - prec) % W_TYPE_SIZE));
1330 if (mslimb == (UWtype) -1)
1331 {
1332 prec += n;
1333 if (prec >= -1)
1334 return -2;
1335#if __LIBGCC_BITINT_ORDER__ == __ORDER_BIG_ENDIAN__
1336 ++*p;
1337#else
1338 --i;
1339#endif
1340 mslimb = (*p)[i];
1341 n = 0;
1342 }
1343 }
1344 while (mslimb == (UWtype) -1)
1345 {
1346 prec += W_TYPE_SIZE;
1347 if (prec >= -1)
1348 return -2;
1349#if __LIBGCC_BITINT_ORDER__ == __ORDER_BIG_ENDIAN__
1350 ++*p;
1351#else
1352 --i;
1353#endif
1354 mslimb = (*p)[i];
1355 }
1356 if (n == 0)
1357 {
1358 if ((Wtype) mslimb >= 0)
1359 {
1360#if __LIBGCC_BITINT_ORDER__ == __ORDER_BIG_ENDIAN__
1361 --*p;
1362#endif
1363 return prec - 1;
1364 }
1365 }
1366 return prec;
1367 }
1368 else
1369 prec = -prec;
1370 }
1371 else
1372 {
1373#if __LIBGCC_BITINT_ORDER__ == __ORDER_BIG_ENDIAN__
1374 i = 0;
1375#else
1376 i = ((USItype) prec - 1) / W_TYPE_SIZE;
1377#endif
1378 mslimb = (*p)[i];
1379 }
1380 SItype n = ((USItype) prec) % W_TYPE_SIZE;
1381 if (n)
1382 {
1383 mslimb &= ((UWtype) 1 << (((USItype) prec) % W_TYPE_SIZE)) - 1;
1384 if (mslimb == 0)
1385 {
1386 prec -= n;
1387 if (prec == 0)
1388 return 1;
1389#if __LIBGCC_BITINT_ORDER__ == __ORDER_BIG_ENDIAN__
1390 ++*p;
1391#else
1392 --i;
1393#endif
1394 mslimb = (*p)[i];
1395 }
1396 }
1397 while (mslimb == 0)
1398 {
1399 prec -= W_TYPE_SIZE;
1400 if (prec == 0)
1401 return 1;
1402#if __LIBGCC_BITINT_ORDER__ == __ORDER_BIG_ENDIAN__
1403 ++*p;
1404#else
1405 --i;
1406#endif
1407 mslimb = (*p)[i];
1408 }
1409 return prec;
1410}
1411
1412#if __LIBGCC_BITINT_ORDER__ == __ORDER_BIG_ENDIAN__
1413# define BITINT_INC -1
1414# define BITINT_END(be, le) (be)
1415#else
1416# define BITINT_INC 1
1417# define BITINT_END(be, le) (le)
1418#endif
1419
1420#ifdef L_mulbitint3
1421/* D = S * L. */
1422
1423static UWtype
1424bitint_mul_1 (UBILtype *d, const UBILtype *s, UWtype l, SItype n)
1425{
1426 UWtype sv, hi, lo, c = 0;
1427 do
1428 {
1429 sv = *s;
1430 s += BITINT_INC;
1431 umul_ppmm (hi, lo, sv, l);
1432 c = __builtin_add_overflow (lo, c, &lo) + hi;
1433 *d = lo;
1434 d += BITINT_INC;
1435 }
1436 while (--n);
1437 return c;
1438}
1439
1440/* D += S * L. */
1441
1442static UWtype
1443bitint_addmul_1 (UBILtype *d, const UBILtype *s, UWtype l, SItype n)
1444{
1445 UWtype sv, hi, lo, c = 0;
1446 do
1447 {
1448 sv = *s;
1449 s += BITINT_INC;
1450 umul_ppmm (hi, lo, sv, l);
1451 hi += __builtin_add_overflow (lo, *d, &lo);
1452 c = __builtin_add_overflow (lo, c, &lo) + hi;
1453 *d = lo;
1454 d += BITINT_INC;
1455 }
1456 while (--n);
1457 return c;
1458}
1459
1460/* If XPREC is positive, it is precision in bits
1461 of an unsigned _BitInt operand (which has XPREC/W_TYPE_SIZE
1462 full limbs and if Xprec%W_TYPE_SIZE one partial limb.
1463 If Xprec is negative, -XPREC is precision in bits
1464 of a signed _BitInt operand. RETPREC should be always
1465 positive. */
1466
1467void
1468__mulbitint3 (UBILtype *ret, SItype retprec,
1469 const UBILtype *u, SItype uprec,
1470 const UBILtype *v, SItype vprec)
1471{
1472 uprec = bitint_reduce_prec (&u, uprec);
1473 vprec = bitint_reduce_prec (&v, vprec);
1474 USItype auprec = uprec < 0 ? -uprec : uprec;
1475 USItype avprec = vprec < 0 ? -vprec : vprec;
1476
1477 /* Prefer non-negative U.
1478 Otherwise make sure V doesn't have higher precision than U. */
1479 if ((uprec < 0 && vprec >= 0)
1480 || (avprec > auprec && !(uprec >= 0 && vprec < 0)))
1481 {
1482 SItype p;
1483 const UBILtype *t;
1484 p = uprec; uprec = vprec; vprec = p;
1485 p = auprec; auprec = avprec; avprec = p;
1486 t = u; u = v; v = t;
1487 }
1488
1489 USItype un = auprec / W_TYPE_SIZE;
1490 USItype un2 = (auprec + W_TYPE_SIZE - 1) / W_TYPE_SIZE;
1491 USItype vn = avprec / W_TYPE_SIZE;
1492 USItype vn2 = (avprec + W_TYPE_SIZE - 1) / W_TYPE_SIZE;
1493 USItype retn = ((USItype) retprec + W_TYPE_SIZE - 1) / W_TYPE_SIZE;
1494 USItype retidx, uidx, vidx;
1495 UWtype vv;
1496 /* Indexes of least significant limb. */
1497#if __LIBGCC_BITINT_ORDER__ == __ORDER_BIG_ENDIAN__
1498 retidx = retn - 1;
1499 uidx = un2 - 1;
1500 vidx = vn2 - 1;
1501#else
1502 retidx = 0;
1503 uidx = 0;
1504 vidx = 0;
1505#endif
1506 if (__builtin_expect (auprec <= W_TYPE_SIZE, 0) && vprec < 0)
1507 {
1508 UWtype uu = u[uidx];
1509 if (__builtin_expect (auprec < W_TYPE_SIZE, 0))
1510 uu &= ((UWtype) 1 << (auprec % W_TYPE_SIZE)) - 1;
1511 if (uu == 0)
1512 {
1513 /* 0 * negative would be otherwise mishandled below, so
1514 handle it specially. */
1515 __builtin_memset (ret, 0, retn * sizeof (UWtype));
1516 return;
1517 }
1518 }
1519 vv = v[vidx];
1520 if (__builtin_expect (avprec < W_TYPE_SIZE, 0))
1521 {
1522 if (vprec > 0)
1523 vv &= ((UWtype) 1 << (avprec % W_TYPE_SIZE)) - 1;
1524 else
1525 vv |= (UWtype) -1 << (avprec % W_TYPE_SIZE);
1526 }
1527
1528 USItype n = un > retn ? retn : un;
1529 USItype n2 = n;
1530 USItype retidx2 = retidx + n * BITINT_INC;
1531 UWtype c = 0, uv = 0;
1532 if (n)
1533 c = bitint_mul_1 (ret + retidx, u + uidx, vv, n);
1534 if (retn > un && un2 != un)
1535 {
1536 UWtype hi, lo;
1537 uv = u[uidx + n * BITINT_INC];
1538 if (uprec > 0)
1539 uv &= ((UWtype) 1 << (auprec % W_TYPE_SIZE)) - 1;
1540 else
1541 uv |= (UWtype) -1 << (auprec % W_TYPE_SIZE);
1542 umul_ppmm (hi, lo, uv, vv);
1543 c = __builtin_add_overflow (lo, c, &lo) + hi;
1544 ret[retidx2] = lo;
1545 retidx2 += BITINT_INC;
1546 ++n2;
1547 }
1548 if (retn > un2)
1549 {
1550 if (uprec < 0)
1551 {
1552 while (n2 < retn)
1553 {
1554 if (n2 >= un2 + vn2)
1555 break;
1556 UWtype hi, lo;
1557 umul_ppmm (hi, lo, (UWtype) -1, vv);
1558 c = __builtin_add_overflow (lo, c, &lo) + hi;
1559 ret[retidx2] = lo;
1560 retidx2 += BITINT_INC;
1561 ++n2;
1562 }
1563 }
1564 else
1565 {
1566 ret[retidx2] = c;
1567 retidx2 += BITINT_INC;
1568 ++n2;
1569 }
1570 /* If RET has more limbs than U after precision reduction,
1571 fill in the remaining limbs. */
1572 while (n2 < retn)
1573 {
1574 if (n2 < un2 + vn2 || (uprec ^ vprec) >= 0)
1575 c = 0;
1576 else
1577 c = (UWtype) -1;
1578 ret[retidx2] = c;
1579 retidx2 += BITINT_INC;
1580 ++n2;
1581 }
1582 }
1583 /* N is now number of possibly non-zero limbs in RET (ignoring
1584 limbs above UN2 + VN2 which if any have been finalized already). */
1585 USItype end = vprec < 0 ? un2 + vn2 : vn2;
1586 if (retn > un2 + vn2) retn = un2 + vn2;
1587 if (end > retn) end = retn;
1588 for (USItype m = 1; m < end; ++m)
1589 {
1590 retidx += BITINT_INC;
1591 vidx += BITINT_INC;
1592 if (m < vn2)
1593 {
1594 vv = v[vidx];
1595 if (__builtin_expect (m == vn, 0))
1596 {
1597 if (vprec > 0)
1598 vv &= ((UWtype) 1 << (avprec % W_TYPE_SIZE)) - 1;
1599 else
1600 vv |= (UWtype) -1 << (avprec % W_TYPE_SIZE);
1601 }
1602 }
1603 else
1604 vv = (UWtype) -1;
1605 if (m + n > retn)
1606 n = retn - m;
1607 c = 0;
1608 if (n)
1609 c = bitint_addmul_1 (ret + retidx, u + uidx, vv, n);
1610 n2 = m + n;
1611 retidx2 = retidx + n * BITINT_INC;
1612 if (n2 < retn && un2 != un)
1613 {
1614 UWtype hi, lo;
1615 umul_ppmm (hi, lo, uv, vv);
1616 hi += __builtin_add_overflow (lo, ret[retidx2], &lo);
1617 c = __builtin_add_overflow (lo, c, &lo) + hi;
1618 ret[retidx2] = lo;
1619 retidx2 += BITINT_INC;
1620 ++n2;
1621 }
1622 if (uprec < 0)
1623 while (n2 < retn)
1624 {
1625 UWtype hi, lo;
1626 umul_ppmm (hi, lo, (UWtype) -1, vv);
1627 hi += __builtin_add_overflow (lo, ret[retidx2], &lo);
1628 c = __builtin_add_overflow (lo, c, &lo) + hi;
1629 ret[retidx2] = lo;
1630 retidx2 += BITINT_INC;
1631 ++n2;
1632 }
1633 else if (n2 < retn)
1634 {
1635 ret[retidx2] = c;
1636 retidx2 += BITINT_INC;
1637 }
1638 }
1639}
1640#endif
1641
1642#ifdef L_divmodbitint4
1643/* D = -S. */
1644
1645static UWtype
1646bitint_negate (UBILtype *d, const UBILtype *s, SItype n)
1647{
1648 UWtype c = 1;
1649 UWtype r = 0;
1650 do
1651 {
1652 UWtype sv = *s, lo;
1653 r |= sv;
1654 s += BITINT_INC;
1655 c = __builtin_add_overflow (~sv, c, &lo);
1656 *d = lo;
1657 d += BITINT_INC;
1658 }
1659 while (--n);
1660 return r;
1661}
1662
1663/* D -= S * L. */
1664
1665static UWtype
1666bitint_submul_1 (UBILtype *d, const UBILtype *s, UWtype l, SItype n)
1667{
1668 UWtype sv, hi, lo, c = 0;
1669 do
1670 {
1671 sv = *s;
1672 s += BITINT_INC;
1673 umul_ppmm (hi, lo, sv, l);
1674 hi += __builtin_sub_overflow (*d, lo, &lo);
1675 c = __builtin_sub_overflow (lo, c, &lo) + hi;
1676 *d = lo;
1677 d += BITINT_INC;
1678 }
1679 while (--n);
1680 return c;
1681}
1682
1683/* If XPREC is positive, it is precision in bits
1684 of an unsigned _BitInt operand (which has XPREC/W_TYPE_SIZE
1685 full limbs and if Xprec%W_TYPE_SIZE one partial limb.
1686 If Xprec is negative, -XPREC is precision in bits
1687 of a signed _BitInt operand. QPREC and RPREC should be
1688 always non-negative. If either Q or R is NULL (at least
1689 one should be non-NULL), then corresponding QPREC or RPREC
1690 should be 0. */
1691
1692void
1693__divmodbitint4 (UBILtype *q, SItype qprec,
1694 UBILtype *r, SItype rprec,
1695 const UBILtype *u, SItype uprec,
1696 const UBILtype *v, SItype vprec)
1697{
1698 uprec = bitint_reduce_prec (&u, uprec);
1699 vprec = bitint_reduce_prec (&v, vprec);
1700 USItype auprec = uprec < 0 ? -uprec : uprec;
1701 USItype avprec = vprec < 0 ? -vprec : vprec;
1702 USItype un = (auprec + W_TYPE_SIZE - 1) / W_TYPE_SIZE;
1703 USItype vn = (avprec + W_TYPE_SIZE - 1) / W_TYPE_SIZE;
1704 USItype qn = ((USItype) qprec + W_TYPE_SIZE - 1) / W_TYPE_SIZE;
1705 USItype rn = ((USItype) rprec + W_TYPE_SIZE - 1) / W_TYPE_SIZE;
1706 USItype up = auprec % W_TYPE_SIZE;
1707 USItype vp = avprec % W_TYPE_SIZE;
1708 /* If vprec < 0 and the top limb of v is all ones and the second most
1709 significant limb has most significant bit clear, then just decrease
1710 vn/avprec/vp, because after negation otherwise v2 would have most
1711 significant limb clear. */
1712 if (vprec < 0
1713 && ((v[BITINT_END (0, vn - 1)] | (vp ? ((UWtype) -1 << vp) : 0))
1714 == (UWtype) -1)
1715 && vn > 1
1716 && (Wtype) v[BITINT_END (1, vn - 2)] >= 0)
1717 {
1718 /* Unless all bits below the most significant limb are zero. */
1719 SItype vn2;
1720 for (vn2 = vn - 2; vn2 >= 0; --vn2)
1721 if (v[BITINT_END (vn - 1 - vn2, vn2)])
1722 {
1723 vp = 0;
1724 --vn;
1725#if __LIBGCC_BITINT_ORDER__ == __ORDER_BIG_ENDIAN__
1726 ++v;
1727#endif
1728 break;
1729 }
1730 }
1731 if (__builtin_expect (un < vn, 0))
1732 {
1733 /* q is 0 and r is u. */
1734 if (q)
1735 __builtin_memset (q, 0, qn * sizeof (UWtype));
1736 if (r == NULL)
1737 return;
1738#if __LIBGCC_BITINT_ORDER__ == __ORDER_BIG_ENDIAN__
1739 r += rn - 1;
1740 u += un - 1;
1741#endif
1742 if (up)
1743 --un;
1744 if (rn < un)
1745 un = rn;
1746 for (rn -= un; un; --un)
1747 {
1748 *r = *u;
1749 r += BITINT_INC;
1750 u += BITINT_INC;
1751 }
1752 if (!rn)
1753 return;
1754 if (up)
1755 {
1756 if (uprec > 0)
1757 *r = *u & (((UWtype) 1 << up) - 1);
1758 else
1759 *r = *u | ((UWtype) -1 << up);
1760 r += BITINT_INC;
1761 if (!--rn)
1762 return;
1763 }
1764 UWtype c = uprec < 0 ? (UWtype) -1 : (UWtype) 0;
1765 for (; rn; --rn)
1766 {
1767 *r = c;
1768 r += BITINT_INC;
1769 }
1770 return;
1771 }
1772 USItype qn2 = un - vn + 1;
1773 if (qn >= qn2)
1774 qn2 = 0;
1775 USItype sz = un + 1 + vn + qn2;
1776 UBILtype *buf = __builtin_alloca (sz * sizeof (UWtype));
1777 USItype uidx, vidx;
1778#if __LIBGCC_BITINT_ORDER__ == __ORDER_BIG_ENDIAN__
1779 uidx = un - 1;
1780 vidx = vn - 1;
1781#else
1782 uidx = 0;
1783 vidx = 0;
1784#endif
1785 if (uprec < 0)
1786 bitint_negate (buf + BITINT_END (uidx + 1, 0), u + uidx, un);
1787 else
1788 __builtin_memcpy (buf + BITINT_END (1, 0), u, un * sizeof (UWtype));
1789 if (up)
1790 buf[BITINT_END (1, un - 1)] &= (((UWtype) 1 << up) - 1);
1791 if (vprec < 0)
1792 bitint_negate (buf + un + 1 + vidx, v + vidx, vn);
1793 else
1794 __builtin_memcpy (buf + un + 1, v, vn * sizeof (UWtype));
1795 if (vp)
1796 buf[un + 1 + BITINT_END (0, vn - 1)] &= (((UWtype) 1 << vp) - 1);
1797 UBILtype *u2 = buf;
1798 UBILtype *v2 = u2 + un + 1;
1799 UBILtype *q2 = v2 + vn;
1800 if (!qn2)
1801 q2 = q + BITINT_END (qn - (un - vn + 1), 0);
1802
1803 /* Knuth's algorithm. See also ../gcc/wide-int.cc (divmod_internal_2). */
1804
1805#ifndef UDIV_NEEDS_NORMALIZATION
1806 /* Handle single limb divisor first. */
1807 if (vn == 1)
1808 {
1809 UWtype vv = v2[0];
1810 if (vv == 0)
1811 vv = 1 / vv; /* Divide intentionally by zero. */
1812 UWtype k = 0;
1813#if __LIBGCC_BITINT_ORDER__ == __ORDER_BIG_ENDIAN__
1814 for (SItype i = 0; i <= un - 1; ++i)
1815#else
1816 for (SItype i = un - 1; i >= 0; --i)
1817#endif
1818 udiv_qrnnd (q2[i], k, k, u2[BITINT_END (i + 1, i)], vv);
1819 if (r != NULL)
1820 r[BITINT_END (rn - 1, 0)] = k;
1821 }
1822 else
1823#endif
1824 {
1825 SItype s;
1826#ifdef UDIV_NEEDS_NORMALIZATION
1827 if (vn == 1 && v2[0] == 0)
1828 s = 0;
1829 else
1830#endif
1831 if (sizeof (0U) == sizeof (UWtype))
1832 s = __builtin_clz (v2[BITINT_END (0, vn - 1)]);
1833 else if (sizeof (0UL) == sizeof (UWtype))
1834 s = __builtin_clzl (v2[BITINT_END (0, vn - 1)]);
1835 else
1836 s = __builtin_clzll (v2[BITINT_END (0, vn - 1)]);
1837 if (s)
1838 {
1839 /* Normalize by shifting v2 left so that it has msb set. */
1840 const SItype n = sizeof (UWtype) * __CHAR_BIT__;
1841#if __LIBGCC_BITINT_ORDER__ == __ORDER_BIG_ENDIAN__
1842 for (SItype i = 0; i < vn - 1; ++i)
1843#else
1844 for (SItype i = vn - 1; i > 0; --i)
1845#endif
1846 v2[i] = (v2[i] << s) | (v2[i - BITINT_INC] >> (n - s));
1847 v2[vidx] = v2[vidx] << s;
1848 /* And shift u2 left by the same amount. */
1849 u2[BITINT_END (0, un)] = u2[BITINT_END (1, un - 1)] >> (n - s);
1850#if __LIBGCC_BITINT_ORDER__ == __ORDER_BIG_ENDIAN__
1851 for (SItype i = 1; i < un; ++i)
1852#else
1853 for (SItype i = un - 1; i > 0; --i)
1854#endif
1855 u2[i] = (u2[i] << s) | (u2[i - BITINT_INC] >> (n - s));
1856 u2[BITINT_END (un, 0)] = u2[BITINT_END (un, 0)] << s;
1857 }
1858 else
1859 u2[BITINT_END (0, un)] = 0;
1860#ifdef UDIV_NEEDS_NORMALIZATION
1861 /* Handle single limb divisor first. */
1862 if (vn == 1)
1863 {
1864 UWtype vv = v2[0];
1865 if (vv == 0)
1866 vv = 1 / vv; /* Divide intentionally by zero. */
1867 UWtype k = u2[BITINT_END (0, un)];
1868#if __LIBGCC_BITINT_ORDER__ == __ORDER_BIG_ENDIAN__
1869 for (SItype i = 0; i <= un - 1; ++i)
1870#else
1871 for (SItype i = un - 1; i >= 0; --i)
1872#endif
1873 udiv_qrnnd (q2[i], k, k, u2[BITINT_END (i + 1, i)], vv);
1874 if (r != NULL)
1875 r[BITINT_END (rn - 1, 0)] = k >> s;
1876 }
1877 else
1878#endif
1879 {
1880 UWtype vv1 = v2[BITINT_END (0, vn - 1)];
1881 UWtype vv0 = v2[BITINT_END (1, vn - 2)];
1882 /* Main loop. */
1883 for (SItype j = un - vn; j >= 0; --j)
1884 {
1885 /* Compute estimate in qhat. */
1886 UWtype uv1 = u2[BITINT_END (un - j - vn, j + vn)];
1887 UWtype uv0 = u2[BITINT_END (un - j - vn + 1, j + vn - 1)];
1888 UWtype qhat, rhat, hi, lo, c;
1889 if (uv1 >= vv1)
1890 {
1891 /* udiv_qrnnd doesn't support quotients which don't
1892 fit into UWtype, while Knuth's algorithm originally
1893 uses a double-word by word to double-word division.
1894 Fortunately, the algorithm guarantees that uv1 <= vv1,
1895 because if uv1 > vv1, then even if v would have all
1896 bits in all words below vv1 set, the previous iteration
1897 would be supposed to use qhat larger by 1 and subtract
1898 v. With uv1 == vv1 and uv0 >= vv1 the double-word
1899 qhat in Knuth's algorithm would be 1 in the upper word
1900 and 1 in the lower word, say for
1901 uv1 0x8000000000000000ULL
1902 uv0 0xffffffffffffffffULL
1903 vv1 0x8000000000000000ULL
1904 0x8000000000000000ffffffffffffffffuwb
1905 / 0x8000000000000000uwb == 0x10000000000000001uwb, and
1906 exactly like that also for any other value
1907 > 0x8000000000000000ULL in uv1 and vv1 and uv0 >= uv1.
1908 So we need to subtract one or at most two vv1s from
1909 uv1:uv0 (qhat because of that decreases by 1 or 2 and
1910 is then representable in UWtype) and need to increase
1911 rhat by vv1 once or twice because of that. Now, if
1912 we need to subtract 2 vv1s, i.e. if
1913 uv1 == vv1 && uv0 >= vv1, then rhat (which is uv0 - vv1)
1914 + vv1 computation can't overflow, because it is equal
1915 to uv0 and therefore the original algorithm in that case
1916 performs goto again, but the second vv1 addition must
1917 overflow already because vv1 has msb set from the
1918 canonicalization. */
1919 uv1 -= __builtin_sub_overflow (uv0, vv1, &uv0);
1920 if (uv1 >= vv1)
1921 {
1922 uv1 -= __builtin_sub_overflow (uv0, vv1, &uv0);
1923 udiv_qrnnd (qhat, rhat, uv1, uv0, vv1);
1924 rhat += 2 * vv1;
1925 }
1926 else
1927 {
1928 udiv_qrnnd (qhat, rhat, uv1, uv0, vv1);
1929 if (!__builtin_add_overflow (rhat, vv1, &rhat))
1930 goto again;
1931 }
1932 }
1933 else
1934 {
1935 udiv_qrnnd (qhat, rhat, uv1, uv0, vv1);
1936 again:
1937 umul_ppmm (hi, lo, qhat, vv0);
1938 if (hi > rhat
1939 || (hi == rhat
1940 && lo > u2[BITINT_END (un - j - vn + 2,
1941 j + vn - 2)]))
1942 {
1943 --qhat;
1944 if (!__builtin_add_overflow (rhat, vv1, &rhat))
1945 goto again;
1946 }
1947 }
1948
1949 c = bitint_submul_1 (u2 + BITINT_END (un - j, j),
1950 v2 + BITINT_END (vn - 1, 0), qhat, vn);
1951 u2[BITINT_END (un - j - vn, j + vn)] -= c;
1952 /* If we've subtracted too much, decrease qhat and
1953 and add back. */
1954 if ((Wtype) u2[BITINT_END (un - j - vn, j + vn)] < 0)
1955 {
1956 --qhat;
1957 c = 0;
1958 for (USItype i = 0; i < vn; ++i)
1959 {
1960 UWtype s = v2[BITINT_END (vn - 1 - i, i)];
1961 UWtype d = u2[BITINT_END (un - i - j, i + j)];
1962 UWtype c1 = __builtin_add_overflow (d, s, &d);
1963 UWtype c2 = __builtin_add_overflow (d, c, &d);
1964 c = c1 + c2;
1965 u2[BITINT_END (un - i - j, i + j)] = d;
1966 }
1967 u2[BITINT_END (un - j - vn, j + vn)] += c;
1968 }
1969 q2[BITINT_END (un - vn - j, j)] = qhat;
1970 }
1971 if (r != NULL)
1972 {
1973 if (s)
1974 {
1975 const SItype n = sizeof (UWtype) * __CHAR_BIT__;
1976 /* Unnormalize remainder. */
1977 USItype i;
1978 for (i = 0; i < vn && i < rn; ++i)
1979 r[BITINT_END (rn - 1 - i, i)]
1980 = ((u2[BITINT_END (un - i, i)] >> s)
1981 | (u2[BITINT_END (un - i - 1, i + 1)] << (n - s)));
1982 if (i < rn)
1983 r[BITINT_END (rn - vn, vn - 1)]
1984 = u2[BITINT_END (un - vn + 1, vn - 1)] >> s;
1985 }
1986 else if (rn > vn)
1987 __builtin_memcpy (&r[BITINT_END (rn - vn, 0)],
1988 &u2[BITINT_END (un + 1 - vn, 0)],
1989 vn * sizeof (UWtype));
1990 else
1991 __builtin_memcpy (&r[0], &u2[BITINT_END (un + 1 - rn, 0)],
1992 rn * sizeof (UWtype));
1993 }
1994 }
1995 }
1996 if (q != NULL)
1997 {
1998 if ((uprec < 0) ^ (vprec < 0))
1999 {
2000 /* Negative quotient. */
2001 USItype n;
2002 if (un - vn + 1 > qn)
2003 n = qn;
2004 else
2005 n = un - vn + 1;
2006 SItype c = bitint_negate (q + BITINT_END (qn - 1, 0),
2007 q2 + BITINT_END (un - vn, 0), n) ? -1 : 0;
2008 if (qn > n)
2009 __builtin_memset (q + BITINT_END (0, n), c,
2010 (qn - n) * sizeof (UWtype));
2011 }
2012 else
2013 {
2014 /* Positive quotient. */
2015 if (qn2)
2016 __builtin_memcpy (q, q2 + BITINT_END (un - vn + 1 - qn, 0),
2017 qn * sizeof (UWtype));
2018 else if (qn > un - vn + 1)
2019 __builtin_memset (q + BITINT_END (0, un - vn + 1), 0,
2020 (qn - (un - vn + 1)) * sizeof (UWtype));
2021 }
2022 }
2023 if (r != NULL)
2024 {
2025 if (uprec < 0)
2026 {
2027 /* Negative remainder. */
2028 SItype c = bitint_negate (r + BITINT_END (rn - 1, 0),
2029 r + BITINT_END (rn - 1, 0),
2030 rn > vn ? vn : rn) ? -1 : 0;
2031 if (rn > vn)
2032 __builtin_memset (r + BITINT_END (0, vn), c,
2033 (rn - vn) * sizeof (UWtype));
2034 }
2035 else
2036 {
2037 /* Positive remainder. */
2038 if (rn > vn)
2039 __builtin_memset (r + BITINT_END (0, vn), 0,
2040 (rn - vn) * sizeof (UWtype));
2041 }
2042 }
2043}
2044#endif
2045#endif
2046\f
2047#ifdef L_cmpdi2
2048cmp_return_type
2049__cmpdi2 (DWtype a, DWtype b)
2050{
2051 return (a > b) - (a < b) + 1;
2052}
2053#endif
2054
2055#ifdef L_ucmpdi2
2056cmp_return_type
2057__ucmpdi2 (UDWtype a, UDWtype b)
2058{
2059 return (a > b) - (a < b) + 1;
2060}
2061#endif
2062\f
2063#if defined(L_fixunstfdi) && LIBGCC2_HAS_TF_MODE
2064UDWtype
2065__fixunstfDI (TFtype a)
2066{
2067 if (a < 0)
2068 return 0;
2069
2070 /* Compute high word of result, as a flonum. */
2071 const TFtype b = (a / Wtype_MAXp1_F);
2072 /* Convert that to fixed (but not to DWtype!),
2073 and shift it into the high word. */
2074 UDWtype v = (UWtype) b;
2075 v <<= W_TYPE_SIZE;
2076 /* Remove high part from the TFtype, leaving the low part as flonum. */
2077 a -= (TFtype)v;
2078 /* Convert that to fixed (but not to DWtype!) and add it in.
2079 Sometimes A comes out negative. This is significant, since
2080 A has more bits than a long int does. */
2081 if (a < 0)
2082 v -= (UWtype) (- a);
2083 else
2084 v += (UWtype) a;
2085 return v;
2086}
2087#endif
2088
2089#if defined(L_fixtfdi) && LIBGCC2_HAS_TF_MODE
2090DWtype
2091__fixtfdi (TFtype a)
2092{
2093 if (a < 0)
2094 return - __fixunstfDI (-a);
2095 return __fixunstfDI (a);
2096}
2097#endif
2098
2099#if defined(L_fixunsxfdi) && LIBGCC2_HAS_XF_MODE
2100UDWtype
2101__fixunsxfDI (XFtype a)
2102{
2103 if (a < 0)
2104 return 0;
2105
2106 /* Compute high word of result, as a flonum. */
2107 const XFtype b = (a / Wtype_MAXp1_F);
2108 /* Convert that to fixed (but not to DWtype!),
2109 and shift it into the high word. */
2110 UDWtype v = (UWtype) b;
2111 v <<= W_TYPE_SIZE;
2112 /* Remove high part from the XFtype, leaving the low part as flonum. */
2113 a -= (XFtype)v;
2114 /* Convert that to fixed (but not to DWtype!) and add it in.
2115 Sometimes A comes out negative. This is significant, since
2116 A has more bits than a long int does. */
2117 if (a < 0)
2118 v -= (UWtype) (- a);
2119 else
2120 v += (UWtype) a;
2121 return v;
2122}
2123#endif
2124
2125#if defined(L_fixxfdi) && LIBGCC2_HAS_XF_MODE
2126DWtype
2127__fixxfdi (XFtype a)
2128{
2129 if (a < 0)
2130 return - __fixunsxfDI (-a);
2131 return __fixunsxfDI (a);
2132}
2133#endif
2134
2135#if defined(L_fixunsdfdi) && LIBGCC2_HAS_DF_MODE
2136UDWtype
2137__fixunsdfDI (DFtype a)
2138{
2139 /* Get high part of result. The division here will just moves the radix
2140 point and will not cause any rounding. Then the conversion to integral
2141 type chops result as desired. */
2142 const UWtype hi = a / Wtype_MAXp1_F;
2143
2144 /* Get low part of result. Convert `hi' to floating type and scale it back,
2145 then subtract this from the number being converted. This leaves the low
2146 part. Convert that to integral type. */
2147 const UWtype lo = a - (DFtype) hi * Wtype_MAXp1_F;
2148
2149 /* Assemble result from the two parts. */
2150 return ((UDWtype) hi << W_TYPE_SIZE) | lo;
2151}
2152#endif
2153
2154#if defined(L_fixdfdi) && LIBGCC2_HAS_DF_MODE
2155DWtype
2156__fixdfdi (DFtype a)
2157{
2158 if (a < 0)
2159 return - __fixunsdfDI (-a);
2160 return __fixunsdfDI (a);
2161}
2162#endif
2163
2164#if defined(L_fixunssfdi) && LIBGCC2_HAS_SF_MODE
2165UDWtype
2166__fixunssfDI (SFtype a)
2167{
2168#if LIBGCC2_HAS_DF_MODE
2169 /* Convert the SFtype to a DFtype, because that is surely not going
2170 to lose any bits. Some day someone else can write a faster version
2171 that avoids converting to DFtype, and verify it really works right. */
2172 const DFtype dfa = a;
2173
2174 /* Get high part of result. The division here will just moves the radix
2175 point and will not cause any rounding. Then the conversion to integral
2176 type chops result as desired. */
2177 const UWtype hi = dfa / Wtype_MAXp1_F;
2178
2179 /* Get low part of result. Convert `hi' to floating type and scale it back,
2180 then subtract this from the number being converted. This leaves the low
2181 part. Convert that to integral type. */
2182 const UWtype lo = dfa - (DFtype) hi * Wtype_MAXp1_F;
2183
2184 /* Assemble result from the two parts. */
2185 return ((UDWtype) hi << W_TYPE_SIZE) | lo;
2186#elif FLT_MANT_DIG < W_TYPE_SIZE
2187 if (a < 1)
2188 return 0;
2189 if (a < Wtype_MAXp1_F)
2190 return (UWtype)a;
2191 if (a < Wtype_MAXp1_F * Wtype_MAXp1_F)
2192 {
2193 /* Since we know that there are fewer significant bits in the SFmode
2194 quantity than in a word, we know that we can convert out all the
2195 significant bits in one step, and thus avoid losing bits. */
2196
2197 /* ??? This following loop essentially performs frexpf. If we could
2198 use the real libm function, or poke at the actual bits of the fp
2199 format, it would be significantly faster. */
2200
2201 UWtype shift = 0, counter;
2202 SFtype msb;
2203
2204 a /= Wtype_MAXp1_F;
2205 for (counter = W_TYPE_SIZE / 2; counter != 0; counter >>= 1)
2206 {
2207 SFtype counterf = (UWtype)1 << counter;
2208 if (a >= counterf)
2209 {
2210 shift |= counter;
2211 a /= counterf;
2212 }
2213 }
2214
2215 /* Rescale into the range of one word, extract the bits of that
2216 one word, and shift the result into position. */
2217 a *= Wtype_MAXp1_F;
2218 counter = a;
2219 return (DWtype)counter << shift;
2220 }
2221 return -1;
2222#else
2223# error
2224#endif
2225}
2226#endif
2227
2228#if defined(L_fixsfdi) && LIBGCC2_HAS_SF_MODE
2229DWtype
2230__fixsfdi (SFtype a)
2231{
2232 if (a < 0)
2233 return - __fixunssfDI (-a);
2234 return __fixunssfDI (a);
2235}
2236#endif
2237
2238#if defined(L_floatdixf) && LIBGCC2_HAS_XF_MODE
2239XFtype
2240__floatdixf (DWtype u)
2241{
2242#if W_TYPE_SIZE > __LIBGCC_XF_MANT_DIG__
2243# error
2244#endif
2245 XFtype d = (Wtype) (u >> W_TYPE_SIZE);
2246 d *= Wtype_MAXp1_F;
2247 d += (UWtype)u;
2248 return d;
2249}
2250#endif
2251
2252#if defined(L_floatundixf) && LIBGCC2_HAS_XF_MODE
2253XFtype
2254__floatundixf (UDWtype u)
2255{
2256#if W_TYPE_SIZE > __LIBGCC_XF_MANT_DIG__
2257# error
2258#endif
2259 XFtype d = (UWtype) (u >> W_TYPE_SIZE);
2260 d *= Wtype_MAXp1_F;
2261 d += (UWtype)u;
2262 return d;
2263}
2264#endif
2265
2266#if defined(L_floatditf) && LIBGCC2_HAS_TF_MODE
2267TFtype
2268__floatditf (DWtype u)
2269{
2270#if W_TYPE_SIZE > __LIBGCC_TF_MANT_DIG__
2271# error
2272#endif
2273 TFtype d = (Wtype) (u >> W_TYPE_SIZE);
2274 d *= Wtype_MAXp1_F;
2275 d += (UWtype)u;
2276 return d;
2277}
2278#endif
2279
2280#if defined(L_floatunditf) && LIBGCC2_HAS_TF_MODE
2281TFtype
2282__floatunditf (UDWtype u)
2283{
2284#if W_TYPE_SIZE > __LIBGCC_TF_MANT_DIG__
2285# error
2286#endif
2287 TFtype d = (UWtype) (u >> W_TYPE_SIZE);
2288 d *= Wtype_MAXp1_F;
2289 d += (UWtype)u;
2290 return d;
2291}
2292#endif
2293
2294#if (defined(L_floatdisf) && LIBGCC2_HAS_SF_MODE) \
2295 || (defined(L_floatdidf) && LIBGCC2_HAS_DF_MODE)
2296#define DI_SIZE (W_TYPE_SIZE * 2)
2297#define F_MODE_OK(SIZE) \
2298 (SIZE < DI_SIZE \
2299 && SIZE > (DI_SIZE - SIZE + FSSIZE) \
2300 && !AVOID_FP_TYPE_CONVERSION(SIZE))
2301#if defined(L_floatdisf)
2302#define FUNC __floatdisf
2303#define FSTYPE SFtype
2304#define FSSIZE __LIBGCC_SF_MANT_DIG__
2305#else
2306#define FUNC __floatdidf
2307#define FSTYPE DFtype
2308#define FSSIZE __LIBGCC_DF_MANT_DIG__
2309#endif
2310
2311FSTYPE
2312FUNC (DWtype u)
2313{
2314#if FSSIZE >= W_TYPE_SIZE
2315 /* When the word size is small, we never get any rounding error. */
2316 FSTYPE f = (Wtype) (u >> W_TYPE_SIZE);
2317 f *= Wtype_MAXp1_F;
2318 f += (UWtype)u;
2319 return f;
2320#elif (LIBGCC2_HAS_DF_MODE && F_MODE_OK (__LIBGCC_DF_MANT_DIG__)) \
2321 || (LIBGCC2_HAS_XF_MODE && F_MODE_OK (__LIBGCC_XF_MANT_DIG__)) \
2322 || (LIBGCC2_HAS_TF_MODE && F_MODE_OK (__LIBGCC_TF_MANT_DIG__))
2323
2324#if (LIBGCC2_HAS_DF_MODE && F_MODE_OK (__LIBGCC_DF_MANT_DIG__))
2325# define FSIZE __LIBGCC_DF_MANT_DIG__
2326# define FTYPE DFtype
2327#elif (LIBGCC2_HAS_XF_MODE && F_MODE_OK (__LIBGCC_XF_MANT_DIG__))
2328# define FSIZE __LIBGCC_XF_MANT_DIG__
2329# define FTYPE XFtype
2330#elif (LIBGCC2_HAS_TF_MODE && F_MODE_OK (__LIBGCC_TF_MANT_DIG__))
2331# define FSIZE __LIBGCC_TF_MANT_DIG__
2332# define FTYPE TFtype
2333#else
2334# error
2335#endif
2336
2337#define REP_BIT ((UDWtype) 1 << (DI_SIZE - FSIZE))
2338
2339 /* Protect against double-rounding error.
2340 Represent any low-order bits, that might be truncated by a bit that
2341 won't be lost. The bit can go in anywhere below the rounding position
2342 of the FSTYPE. A fixed mask and bit position handles all usual
2343 configurations. */
2344 if (! (- ((DWtype) 1 << FSIZE) < u
2345 && u < ((DWtype) 1 << FSIZE)))
2346 {
2347 if ((UDWtype) u & (REP_BIT - 1))
2348 {
2349 u &= ~ (REP_BIT - 1);
2350 u |= REP_BIT;
2351 }
2352 }
2353
2354 /* Do the calculation in a wider type so that we don't lose any of
2355 the precision of the high word while multiplying it. */
2356 FTYPE f = (Wtype) (u >> W_TYPE_SIZE);
2357 f *= Wtype_MAXp1_F;
2358 f += (UWtype)u;
2359 return (FSTYPE) f;
2360#else
2361#if FSSIZE >= W_TYPE_SIZE - 2
2362# error
2363#endif
2364 /* Finally, the word size is larger than the number of bits in the
2365 required FSTYPE, and we've got no suitable wider type. The only
2366 way to avoid double rounding is to special case the
2367 extraction. */
2368
2369 /* If there are no high bits set, fall back to one conversion. */
2370 if ((Wtype)u == u)
2371 return (FSTYPE)(Wtype)u;
2372
2373 /* Otherwise, find the power of two. */
2374 Wtype hi = u >> W_TYPE_SIZE;
2375 if (hi < 0)
2376 hi = -(UWtype) hi;
2377
2378 UWtype count, shift;
2379#if !defined (COUNT_LEADING_ZEROS_0) || COUNT_LEADING_ZEROS_0 != W_TYPE_SIZE
2380 if (hi == 0)
2381 count = W_TYPE_SIZE;
2382 else
2383#endif
2384 count_leading_zeros (count, hi);
2385
2386 /* No leading bits means u == minimum. */
2387 if (count == 0)
2388 return Wtype_MAXp1_F * (FSTYPE) (hi | ((UWtype) u != 0));
2389
2390 shift = 1 + W_TYPE_SIZE - count;
2391
2392 /* Shift down the most significant bits. */
2393 hi = u >> shift;
2394
2395 /* If we lost any nonzero bits, set the lsb to ensure correct rounding. */
2396 if ((UWtype)u << (W_TYPE_SIZE - shift))
2397 hi |= 1;
2398
2399 /* Convert the one word of data, and rescale. */
2400 FSTYPE f = hi, e;
2401 if (shift == W_TYPE_SIZE)
2402 e = Wtype_MAXp1_F;
2403 /* The following two cases could be merged if we knew that the target
2404 supported a native unsigned->float conversion. More often, we only
2405 have a signed conversion, and have to add extra fixup code. */
2406 else if (shift == W_TYPE_SIZE - 1)
2407 e = Wtype_MAXp1_F / 2;
2408 else
2409 e = (Wtype)1 << shift;
2410 return f * e;
2411#endif
2412}
2413#endif
2414
2415#if (defined(L_floatundisf) && LIBGCC2_HAS_SF_MODE) \
2416 || (defined(L_floatundidf) && LIBGCC2_HAS_DF_MODE)
2417#define DI_SIZE (W_TYPE_SIZE * 2)
2418#define F_MODE_OK(SIZE) \
2419 (SIZE < DI_SIZE \
2420 && SIZE > (DI_SIZE - SIZE + FSSIZE) \
2421 && !AVOID_FP_TYPE_CONVERSION(SIZE))
2422#if defined(L_floatundisf)
2423#define FUNC __floatundisf
2424#define FSTYPE SFtype
2425#define FSSIZE __LIBGCC_SF_MANT_DIG__
2426#else
2427#define FUNC __floatundidf
2428#define FSTYPE DFtype
2429#define FSSIZE __LIBGCC_DF_MANT_DIG__
2430#endif
2431
2432FSTYPE
2433FUNC (UDWtype u)
2434{
2435#if FSSIZE >= W_TYPE_SIZE
2436 /* When the word size is small, we never get any rounding error. */
2437 FSTYPE f = (UWtype) (u >> W_TYPE_SIZE);
2438 f *= Wtype_MAXp1_F;
2439 f += (UWtype)u;
2440 return f;
2441#elif (LIBGCC2_HAS_DF_MODE && F_MODE_OK (__LIBGCC_DF_MANT_DIG__)) \
2442 || (LIBGCC2_HAS_XF_MODE && F_MODE_OK (__LIBGCC_XF_MANT_DIG__)) \
2443 || (LIBGCC2_HAS_TF_MODE && F_MODE_OK (__LIBGCC_TF_MANT_DIG__))
2444
2445#if (LIBGCC2_HAS_DF_MODE && F_MODE_OK (__LIBGCC_DF_MANT_DIG__))
2446# define FSIZE __LIBGCC_DF_MANT_DIG__
2447# define FTYPE DFtype
2448#elif (LIBGCC2_HAS_XF_MODE && F_MODE_OK (__LIBGCC_XF_MANT_DIG__))
2449# define FSIZE __LIBGCC_XF_MANT_DIG__
2450# define FTYPE XFtype
2451#elif (LIBGCC2_HAS_TF_MODE && F_MODE_OK (__LIBGCC_TF_MANT_DIG__))
2452# define FSIZE __LIBGCC_TF_MANT_DIG__
2453# define FTYPE TFtype
2454#else
2455# error
2456#endif
2457
2458#define REP_BIT ((UDWtype) 1 << (DI_SIZE - FSIZE))
2459
2460 /* Protect against double-rounding error.
2461 Represent any low-order bits, that might be truncated by a bit that
2462 won't be lost. The bit can go in anywhere below the rounding position
2463 of the FSTYPE. A fixed mask and bit position handles all usual
2464 configurations. */
2465 if (u >= ((UDWtype) 1 << FSIZE))
2466 {
2467 if ((UDWtype) u & (REP_BIT - 1))
2468 {
2469 u &= ~ (REP_BIT - 1);
2470 u |= REP_BIT;
2471 }
2472 }
2473
2474 /* Do the calculation in a wider type so that we don't lose any of
2475 the precision of the high word while multiplying it. */
2476 FTYPE f = (UWtype) (u >> W_TYPE_SIZE);
2477 f *= Wtype_MAXp1_F;
2478 f += (UWtype)u;
2479 return (FSTYPE) f;
2480#else
2481#if FSSIZE == W_TYPE_SIZE - 1
2482# error
2483#endif
2484 /* Finally, the word size is larger than the number of bits in the
2485 required FSTYPE, and we've got no suitable wider type. The only
2486 way to avoid double rounding is to special case the
2487 extraction. */
2488
2489 /* If there are no high bits set, fall back to one conversion. */
2490 if ((UWtype)u == u)
2491 return (FSTYPE)(UWtype)u;
2492
2493 /* Otherwise, find the power of two. */
2494 UWtype hi = u >> W_TYPE_SIZE;
2495
2496 UWtype count, shift;
2497 count_leading_zeros (count, hi);
2498
2499 shift = W_TYPE_SIZE - count;
2500
2501 /* Shift down the most significant bits. */
2502 hi = u >> shift;
2503
2504 /* If we lost any nonzero bits, set the lsb to ensure correct rounding. */
2505 if ((UWtype)u << (W_TYPE_SIZE - shift))
2506 hi |= 1;
2507
2508 /* Convert the one word of data, and rescale. */
2509 FSTYPE f = hi, e;
2510 if (shift == W_TYPE_SIZE)
2511 e = Wtype_MAXp1_F;
2512 /* The following two cases could be merged if we knew that the target
2513 supported a native unsigned->float conversion. More often, we only
2514 have a signed conversion, and have to add extra fixup code. */
2515 else if (shift == W_TYPE_SIZE - 1)
2516 e = Wtype_MAXp1_F / 2;
2517 else
2518 e = (Wtype)1 << shift;
2519 return f * e;
2520#endif
2521}
2522#endif
2523
2524#if defined(L_fixunsxfsi) && LIBGCC2_HAS_XF_MODE
2525UWtype
2526__fixunsxfSI (XFtype a)
2527{
2528 if (a >= - (DFtype) Wtype_MIN)
2529 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
2530 return (Wtype) a;
2531}
2532#endif
2533
2534#if defined(L_fixunsdfsi) && LIBGCC2_HAS_DF_MODE
2535UWtype
2536__fixunsdfSI (DFtype a)
2537{
2538 if (a >= - (DFtype) Wtype_MIN)
2539 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
2540 return (Wtype) a;
2541}
2542#endif
2543
2544#if defined(L_fixunssfsi) && LIBGCC2_HAS_SF_MODE
2545UWtype
2546__fixunssfSI (SFtype a)
2547{
2548 if (a >= - (SFtype) Wtype_MIN)
2549 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
2550 return (Wtype) a;
2551}
2552#endif
2553\f
2554/* Integer power helper used from __builtin_powi for non-constant
2555 exponents. */
2556
2557#if (defined(L_powisf2) && LIBGCC2_HAS_SF_MODE) \
2558 || (defined(L_powidf2) && LIBGCC2_HAS_DF_MODE) \
2559 || (defined(L_powixf2) && LIBGCC2_HAS_XF_MODE) \
2560 || (defined(L_powitf2) && LIBGCC2_HAS_TF_MODE)
2561# if defined(L_powisf2)
2562# define TYPE SFtype
2563# define NAME __powisf2
2564# elif defined(L_powidf2)
2565# define TYPE DFtype
2566# define NAME __powidf2
2567# elif defined(L_powixf2)
2568# define TYPE XFtype
2569# define NAME __powixf2
2570# elif defined(L_powitf2)
2571# define TYPE TFtype
2572# define NAME __powitf2
2573# endif
2574
2575#undef int
2576#undef unsigned
2577TYPE
2578NAME (TYPE x, int m)
2579{
2580 unsigned int n = m < 0 ? -(unsigned int) m : (unsigned int) m;
2581 TYPE y = n % 2 ? x : 1;
2582 while (n >>= 1)
2583 {
2584 x = x * x;
2585 if (n % 2)
2586 y = y * x;
2587 }
2588 return m < 0 ? 1/y : y;
2589}
2590
2591#endif
2592\f
2593#if((defined(L_mulhc3) || defined(L_divhc3)) && LIBGCC2_HAS_HF_MODE) \
2594 || ((defined(L_mulsc3) || defined(L_divsc3)) && LIBGCC2_HAS_SF_MODE) \
2595 || ((defined(L_muldc3) || defined(L_divdc3)) && LIBGCC2_HAS_DF_MODE) \
2596 || ((defined(L_mulxc3) || defined(L_divxc3)) && LIBGCC2_HAS_XF_MODE) \
2597 || ((defined(L_multc3) || defined(L_divtc3)) && LIBGCC2_HAS_TF_MODE)
2598
2599#undef float
2600#undef double
2601#undef long
2602
2603#if defined(L_mulhc3) || defined(L_divhc3)
2604# define MTYPE HFtype
2605# define CTYPE HCtype
2606# define AMTYPE SFtype
2607# define MODE hc
2608# define CEXT __LIBGCC_HF_FUNC_EXT__
2609# define NOTRUNC (!__LIBGCC_HF_EXCESS_PRECISION__)
2610#elif defined(L_mulsc3) || defined(L_divsc3)
2611# define MTYPE SFtype
2612# define CTYPE SCtype
2613# define AMTYPE DFtype
2614# define MODE sc
2615# define CEXT __LIBGCC_SF_FUNC_EXT__
2616# define NOTRUNC (!__LIBGCC_SF_EXCESS_PRECISION__)
2617# define RBIG (__LIBGCC_SF_MAX__ / 2)
2618# define RMIN (__LIBGCC_SF_MIN__)
2619# define RMIN2 (__LIBGCC_SF_EPSILON__)
2620# define RMINSCAL (1 / __LIBGCC_SF_EPSILON__)
2621# define RMAX2 (RBIG * RMIN2)
2622#elif defined(L_muldc3) || defined(L_divdc3)
2623# define MTYPE DFtype
2624# define CTYPE DCtype
2625# define MODE dc
2626# define CEXT __LIBGCC_DF_FUNC_EXT__
2627# define NOTRUNC (!__LIBGCC_DF_EXCESS_PRECISION__)
2628# define RBIG (__LIBGCC_DF_MAX__ / 2)
2629# define RMIN (__LIBGCC_DF_MIN__)
2630# define RMIN2 (__LIBGCC_DF_EPSILON__)
2631# define RMINSCAL (1 / __LIBGCC_DF_EPSILON__)
2632# define RMAX2 (RBIG * RMIN2)
2633#elif defined(L_mulxc3) || defined(L_divxc3)
2634# define MTYPE XFtype
2635# define CTYPE XCtype
2636# define MODE xc
2637# define CEXT __LIBGCC_XF_FUNC_EXT__
2638# define NOTRUNC (!__LIBGCC_XF_EXCESS_PRECISION__)
2639# define RBIG (__LIBGCC_XF_MAX__ / 2)
2640# define RMIN (__LIBGCC_XF_MIN__)
2641# define RMIN2 (__LIBGCC_XF_EPSILON__)
2642# define RMINSCAL (1 / __LIBGCC_XF_EPSILON__)
2643# define RMAX2 (RBIG * RMIN2)
2644#elif defined(L_multc3) || defined(L_divtc3)
2645# define MTYPE TFtype
2646# define CTYPE TCtype
2647# define MODE tc
2648# define CEXT __LIBGCC_TF_FUNC_EXT__
2649# define NOTRUNC (!__LIBGCC_TF_EXCESS_PRECISION__)
2650# if __LIBGCC_TF_MANT_DIG__ == 106
2651# define RBIG (__LIBGCC_DF_MAX__ / 2)
2652# define RMIN (__LIBGCC_DF_MIN__)
2653# define RMIN2 (__LIBGCC_DF_EPSILON__)
2654# define RMINSCAL (1 / __LIBGCC_DF_EPSILON__)
2655# else
2656# define RBIG (__LIBGCC_TF_MAX__ / 2)
2657# define RMIN (__LIBGCC_TF_MIN__)
2658# define RMIN2 (__LIBGCC_TF_EPSILON__)
2659# define RMINSCAL (1 / __LIBGCC_TF_EPSILON__)
2660# endif
2661# define RMAX2 (RBIG * RMIN2)
2662#else
2663# error
2664#endif
2665
2666#define CONCAT3(A,B,C) _CONCAT3(A,B,C)
2667#define _CONCAT3(A,B,C) A##B##C
2668
2669#define CONCAT2(A,B) _CONCAT2(A,B)
2670#define _CONCAT2(A,B) A##B
2671
2672#define isnan(x) __builtin_isnan (x)
2673#define isfinite(x) __builtin_isfinite (x)
2674#define isinf(x) __builtin_isinf (x)
2675
2676#undef INFINITY
2677#define INFINITY CONCAT2(__builtin_huge_val, CEXT) ()
2678#define I 1i
2679
2680/* Helpers to make the following code slightly less gross. */
2681#define COPYSIGN CONCAT2(__builtin_copysign, CEXT)
2682#define FABS CONCAT2(__builtin_fabs, CEXT)
2683
2684/* Verify that MTYPE matches up with CEXT. */
2685extern void *compile_type_assert[sizeof(INFINITY) == sizeof(MTYPE) ? 1 : -1];
2686
2687/* Ensure that we've lost any extra precision. */
2688#if NOTRUNC
2689# define TRUNC(x)
2690#else
2691# define TRUNC(x) __asm__ ("" : "=m"(x) : "m"(x))
2692#endif
2693
2694#if defined(L_mulhc3) || defined(L_mulsc3) || defined(L_muldc3) \
2695 || defined(L_mulxc3) || defined(L_multc3)
2696
2697CTYPE
2698CONCAT3(__mul,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
2699{
2700 MTYPE ac, bd, ad, bc, x, y;
2701 CTYPE res;
2702
2703 ac = a * c;
2704 bd = b * d;
2705 ad = a * d;
2706 bc = b * c;
2707
2708 TRUNC (ac);
2709 TRUNC (bd);
2710 TRUNC (ad);
2711 TRUNC (bc);
2712
2713 x = ac - bd;
2714 y = ad + bc;
2715
2716 if (isnan (x) && isnan (y))
2717 {
2718 /* Recover infinities that computed as NaN + iNaN. */
2719 _Bool recalc = 0;
2720 if (isinf (a) || isinf (b))
2721 {
2722 /* z is infinite. "Box" the infinity and change NaNs in
2723 the other factor to 0. */
2724 a = COPYSIGN (isinf (a) ? 1 : 0, a);
2725 b = COPYSIGN (isinf (b) ? 1 : 0, b);
2726 if (isnan (c)) c = COPYSIGN (0, c);
2727 if (isnan (d)) d = COPYSIGN (0, d);
2728 recalc = 1;
2729 }
2730 if (isinf (c) || isinf (d))
2731 {
2732 /* w is infinite. "Box" the infinity and change NaNs in
2733 the other factor to 0. */
2734 c = COPYSIGN (isinf (c) ? 1 : 0, c);
2735 d = COPYSIGN (isinf (d) ? 1 : 0, d);
2736 if (isnan (a)) a = COPYSIGN (0, a);
2737 if (isnan (b)) b = COPYSIGN (0, b);
2738 recalc = 1;
2739 }
2740 if (!recalc
2741 && (isinf (ac) || isinf (bd)
2742 || isinf (ad) || isinf (bc)))
2743 {
2744 /* Recover infinities from overflow by changing NaNs to 0. */
2745 if (isnan (a)) a = COPYSIGN (0, a);
2746 if (isnan (b)) b = COPYSIGN (0, b);
2747 if (isnan (c)) c = COPYSIGN (0, c);
2748 if (isnan (d)) d = COPYSIGN (0, d);
2749 recalc = 1;
2750 }
2751 if (recalc)
2752 {
2753 x = INFINITY * (a * c - b * d);
2754 y = INFINITY * (a * d + b * c);
2755 }
2756 }
2757
2758 __real__ res = x;
2759 __imag__ res = y;
2760 return res;
2761}
2762#endif /* complex multiply */
2763
2764#if defined(L_divhc3) || defined(L_divsc3) || defined(L_divdc3) \
2765 || defined(L_divxc3) || defined(L_divtc3)
2766
2767CTYPE
2768CONCAT3(__div,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
2769{
2770#if defined(L_divhc3) \
2771 || (defined(L_divsc3) && defined(__LIBGCC_HAVE_HWDBL__) )
2772
2773 /* Half precision is handled with float precision.
2774 float is handled with double precision when double precision
2775 hardware is available.
2776 Due to the additional precision, the simple complex divide
2777 method (without Smith's method) is sufficient to get accurate
2778 answers and runs slightly faster than Smith's method. */
2779
2780 AMTYPE aa, bb, cc, dd;
2781 AMTYPE denom;
2782 MTYPE x, y;
2783 CTYPE res;
2784 aa = a;
2785 bb = b;
2786 cc = c;
2787 dd = d;
2788
2789 denom = (cc * cc) + (dd * dd);
2790 x = ((aa * cc) + (bb * dd)) / denom;
2791 y = ((bb * cc) - (aa * dd)) / denom;
2792
2793#else
2794 MTYPE denom, ratio, x, y;
2795 CTYPE res;
2796
2797 /* double, extended, long double have significant potential
2798 underflow/overflow errors that can be greatly reduced with
2799 a limited number of tests and adjustments. float is handled
2800 the same way when no HW double is available.
2801 */
2802
2803 /* Scale by max(c,d) to reduce chances of denominator overflowing. */
2804 if (FABS (c) < FABS (d))
2805 {
2806 /* Prevent underflow when denominator is near max representable. */
2807 if (FABS (d) >= RBIG)
2808 {
2809 a = a / 2;
2810 b = b / 2;
2811 c = c / 2;
2812 d = d / 2;
2813 }
2814 /* Avoid overflow/underflow issues when c and d are small.
2815 Scaling up helps avoid some underflows.
2816 No new overflow possible since c&d < RMIN2. */
2817 if (FABS (d) < RMIN2)
2818 {
2819 a = a * RMINSCAL;
2820 b = b * RMINSCAL;
2821 c = c * RMINSCAL;
2822 d = d * RMINSCAL;
2823 }
2824 else
2825 {
2826 if (((FABS (a) < RMIN) && (FABS (b) < RMAX2) && (FABS (d) < RMAX2))
2827 || ((FABS (b) < RMIN) && (FABS (a) < RMAX2)
2828 && (FABS (d) < RMAX2)))
2829 {
2830 a = a * RMINSCAL;
2831 b = b * RMINSCAL;
2832 c = c * RMINSCAL;
2833 d = d * RMINSCAL;
2834 }
2835 }
2836 ratio = c / d;
2837 denom = (c * ratio) + d;
2838 /* Choose alternate order of computation if ratio is subnormal. */
2839 if (FABS (ratio) > RMIN)
2840 {
2841 x = ((a * ratio) + b) / denom;
2842 y = ((b * ratio) - a) / denom;
2843 }
2844 else
2845 {
2846 x = ((c * (a / d)) + b) / denom;
2847 y = ((c * (b / d)) - a) / denom;
2848 }
2849 }
2850 else
2851 {
2852 /* Prevent underflow when denominator is near max representable. */
2853 if (FABS (c) >= RBIG)
2854 {
2855 a = a / 2;
2856 b = b / 2;
2857 c = c / 2;
2858 d = d / 2;
2859 }
2860 /* Avoid overflow/underflow issues when both c and d are small.
2861 Scaling up helps avoid some underflows.
2862 No new overflow possible since both c&d are less than RMIN2. */
2863 if (FABS (c) < RMIN2)
2864 {
2865 a = a * RMINSCAL;
2866 b = b * RMINSCAL;
2867 c = c * RMINSCAL;
2868 d = d * RMINSCAL;
2869 }
2870 else
2871 {
2872 if (((FABS (a) < RMIN) && (FABS (b) < RMAX2) && (FABS (c) < RMAX2))
2873 || ((FABS (b) < RMIN) && (FABS (a) < RMAX2)
2874 && (FABS (c) < RMAX2)))
2875 {
2876 a = a * RMINSCAL;
2877 b = b * RMINSCAL;
2878 c = c * RMINSCAL;
2879 d = d * RMINSCAL;
2880 }
2881 }
2882 ratio = d / c;
2883 denom = (d * ratio) + c;
2884 /* Choose alternate order of computation if ratio is subnormal. */
2885 if (FABS (ratio) > RMIN)
2886 {
2887 x = ((b * ratio) + a) / denom;
2888 y = (b - (a * ratio)) / denom;
2889 }
2890 else
2891 {
2892 x = (a + (d * (b / c))) / denom;
2893 y = (b - (d * (a / c))) / denom;
2894 }
2895 }
2896#endif
2897
2898 /* Recover infinities and zeros that computed as NaN+iNaN; the only
2899 cases are nonzero/zero, infinite/finite, and finite/infinite. */
2900 if (isnan (x) && isnan (y))
2901 {
2902 if (c == 0.0 && d == 0.0 && (!isnan (a) || !isnan (b)))
2903 {
2904 x = COPYSIGN (INFINITY, c) * a;
2905 y = COPYSIGN (INFINITY, c) * b;
2906 }
2907 else if ((isinf (a) || isinf (b)) && isfinite (c) && isfinite (d))
2908 {
2909 a = COPYSIGN (isinf (a) ? 1 : 0, a);
2910 b = COPYSIGN (isinf (b) ? 1 : 0, b);
2911 x = INFINITY * (a * c + b * d);
2912 y = INFINITY * (b * c - a * d);
2913 }
2914 else if ((isinf (c) || isinf (d)) && isfinite (a) && isfinite (b))
2915 {
2916 c = COPYSIGN (isinf (c) ? 1 : 0, c);
2917 d = COPYSIGN (isinf (d) ? 1 : 0, d);
2918 x = 0.0 * (a * c + b * d);
2919 y = 0.0 * (b * c - a * d);
2920 }
2921 }
2922
2923 __real__ res = x;
2924 __imag__ res = y;
2925 return res;
2926}
2927#endif /* complex divide */
2928
2929#endif /* all complex float routines */
2930\f
2931/* From here on down, the routines use normal data types. */
2932
2933#define SItype bogus_type
2934#define USItype bogus_type
2935#define DItype bogus_type
2936#define UDItype bogus_type
2937#define SFtype bogus_type
2938#define DFtype bogus_type
2939#undef Wtype
2940#undef UWtype
2941#undef HWtype
2942#undef UHWtype
2943#undef DWtype
2944#undef UDWtype
2945
2946#undef char
2947#undef short
2948#undef int
2949#undef long
2950#undef unsigned
2951#undef float
2952#undef double
2953\f
2954#ifdef L__gcc_bcmp
2955
2956/* Like bcmp except the sign is meaningful.
2957 Result is negative if S1 is less than S2,
2958 positive if S1 is greater, 0 if S1 and S2 are equal. */
2959
2960int
2961__gcc_bcmp (const unsigned char *s1, const unsigned char *s2, size_t size)
2962{
2963 while (size > 0)
2964 {
2965 const unsigned char c1 = *s1++, c2 = *s2++;
2966 if (c1 != c2)
2967 return c1 - c2;
2968 size--;
2969 }
2970 return 0;
2971}
2972
2973#endif
2974\f
2975/* __eprintf used to be used by GCC's private version of <assert.h>.
2976 We no longer provide that header, but this routine remains in libgcc.a
2977 for binary backward compatibility. Note that it is not included in
2978 the shared version of libgcc. */
2979#ifdef L_eprintf
2980#ifndef inhibit_libc
2981
2982#undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
2983#include <stdio.h>
2984
2985void
2986__eprintf (const char *string, const char *expression,
2987 unsigned int line, const char *filename)
2988{
2989 fprintf (stderr, string, expression, line, filename);
2990 fflush (stderr);
2991 abort ();
2992}
2993
2994#endif
2995#endif
2996
2997\f
2998#ifdef L_clear_cache
2999/* Clear part of an instruction cache. */
3000
3001void
3002__clear_cache (void *beg __attribute__((__unused__)),
3003 void *end __attribute__((__unused__)))
3004{
3005#ifdef CLEAR_INSN_CACHE
3006 /* Cast the void* pointers to char* as some implementations
3007 of the macro assume the pointers can be subtracted from
3008 one another. */
3009 CLEAR_INSN_CACHE ((char *) beg, (char *) end);
3010#endif /* CLEAR_INSN_CACHE */
3011}
3012
3013#endif /* L_clear_cache */
3014\f
3015#ifdef L_trampoline
3016
3017/* Jump to a trampoline, loading the static chain address. */
3018
3019#if defined(WINNT) && ! defined(__CYGWIN__)
3020#define WIN32_LEAN_AND_MEAN
3021#include <windows.h>
3022int getpagesize (void);
3023int mprotect (char *,int, int);
3024
3025int
3026getpagesize (void)
3027{
3028#ifdef _ALPHA_
3029 return 8192;
3030#else
3031 return 4096;
3032#endif
3033}
3034
3035int
3036mprotect (char *addr, int len, int prot)
3037{
3038 DWORD np, op;
3039
3040 if (prot == 7)
3041 np = 0x40;
3042 else if (prot == 5)
3043 np = 0x20;
3044 else if (prot == 4)
3045 np = 0x10;
3046 else if (prot == 3)
3047 np = 0x04;
3048 else if (prot == 1)
3049 np = 0x02;
3050 else if (prot == 0)
3051 np = 0x01;
3052 else
3053 return -1;
3054
3055 if (VirtualProtect (addr, len, np, &op))
3056 return 0;
3057 else
3058 return -1;
3059}
3060
3061#endif /* WINNT && ! __CYGWIN__ */
3062
3063#ifdef TRANSFER_FROM_TRAMPOLINE
3064TRANSFER_FROM_TRAMPOLINE
3065#endif
3066#endif /* L_trampoline */
3067\f
3068#ifndef __CYGWIN__
3069#ifdef L__main
3070
3071#include "gbl-ctors.h"
3072
3073/* Some systems use __main in a way incompatible with its use in gcc, in these
3074 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
3075 give the same symbol without quotes for an alternative entry point. You
3076 must define both, or neither. */
3077#ifndef NAME__MAIN
3078#define NAME__MAIN "__main"
3079#define SYMBOL__MAIN __main
3080#endif
3081
3082#if defined (__LIBGCC_INIT_SECTION_ASM_OP__) \
3083 || defined (__LIBGCC_INIT_ARRAY_SECTION_ASM_OP__)
3084#undef HAS_INIT_SECTION
3085#define HAS_INIT_SECTION
3086#endif
3087
3088#if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
3089
3090/* Some ELF crosses use crtstuff.c to provide __CTOR_LIST__, but use this
3091 code to run constructors. In that case, we need to handle EH here, too.
3092 But MINGW32 is special because it handles CRTSTUFF and EH on its own. */
3093
3094#ifdef __MINGW32__
3095#undef __LIBGCC_EH_FRAME_SECTION_NAME__
3096#endif
3097
3098#ifdef __LIBGCC_EH_FRAME_SECTION_NAME__
3099#include "unwind-dw2-fde.h"
3100extern unsigned char __EH_FRAME_BEGIN__[];
3101#endif
3102
3103/* Run all the global destructors on exit from the program. */
3104
3105void
3106__do_global_dtors (void)
3107{
3108#ifdef DO_GLOBAL_DTORS_BODY
3109 DO_GLOBAL_DTORS_BODY;
3110#else
3111 static func_ptr *p = __DTOR_LIST__ + 1;
3112 while (*p)
3113 {
3114 p++;
3115 (*(p-1)) ();
3116 }
3117#endif
3118#if defined (__LIBGCC_EH_FRAME_SECTION_NAME__) && !defined (HAS_INIT_SECTION)
3119 {
3120 static int completed = 0;
3121 if (! completed)
3122 {
3123 completed = 1;
3124 __deregister_frame_info (__EH_FRAME_BEGIN__);
3125 }
3126 }
3127#endif
3128}
3129#endif
3130
3131#ifndef HAS_INIT_SECTION
3132/* Run all the global constructors on entry to the program. */
3133
3134void
3135__do_global_ctors (void)
3136{
3137#ifdef __LIBGCC_EH_FRAME_SECTION_NAME__
3138 {
3139 static struct object object;
3140 __register_frame_info (__EH_FRAME_BEGIN__, &object);
3141 }
3142#endif
3143 DO_GLOBAL_CTORS_BODY;
3144 atexit (__do_global_dtors);
3145}
3146#endif /* no HAS_INIT_SECTION */
3147
3148#if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
3149/* Subroutine called automatically by `main'.
3150 Compiling a global function named `main'
3151 produces an automatic call to this function at the beginning.
3152
3153 For many systems, this routine calls __do_global_ctors.
3154 For systems which support a .init section we use the .init section
3155 to run __do_global_ctors, so we need not do anything here. */
3156
3157extern void SYMBOL__MAIN (void);
3158void
3159SYMBOL__MAIN (void)
3160{
3161 /* Support recursive calls to `main': run initializers just once. */
3162 static int initialized;
3163 if (! initialized)
3164 {
3165 initialized = 1;
3166 __do_global_ctors ();
3167 }
3168}
3169#endif /* no HAS_INIT_SECTION or INVOKE__main */
3170
3171#endif /* L__main */
3172#endif /* __CYGWIN__ */
3173\f
3174#ifdef L_ctors
3175
3176#include "gbl-ctors.h"
3177
3178/* Provide default definitions for the lists of constructors and
3179 destructors, so that we don't get linker errors. These symbols are
3180 intentionally bss symbols, so that gld and/or collect will provide
3181 the right values. */
3182
3183/* We declare the lists here with two elements each,
3184 so that they are valid empty lists if no other definition is loaded.
3185
3186 If we are using the old "set" extensions to have the gnu linker
3187 collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
3188 must be in the bss/common section.
3189
3190 Long term no port should use those extensions. But many still do. */
3191#if !defined(__LIBGCC_INIT_SECTION_ASM_OP__)
3192#if defined (TARGET_ASM_CONSTRUCTOR) || defined (USE_COLLECT2)
3193func_ptr __CTOR_LIST__[2] = {0, 0};
3194func_ptr __DTOR_LIST__[2] = {0, 0};
3195#else
3196func_ptr __CTOR_LIST__[2];
3197func_ptr __DTOR_LIST__[2];
3198#endif
3199#endif /* no __LIBGCC_INIT_SECTION_ASM_OP__ */
3200#endif /* L_ctors */
3201#endif /* LIBGCC2_UNITS_PER_WORD <= MIN_UNITS_PER_WORD */