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