]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgcc/libgcc2.c
Remove no-longer-needed fp-bit target macros.
[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. */
ac1dca3c 3/* Copyright (C) 1989-2014 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
163#define WORD_SIZE (sizeof (SItype) * BITS_PER_UNIT)
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
RK
682#if (defined (L_udivdi3) || defined (L_divdi3) || \
683 defined (L_umoddi3) || defined (L_moddi3))
684#define L_udivmoddi4
685#endif
686
d6eacd48 687#ifdef L_clz
dcfae47c 688const UQItype __clz_tab[256] =
203b91b9
RS
689{
690 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,
691 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,
692 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,
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 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,
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,
dcfae47c 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
203b91b9 698};
d6eacd48 699#endif
2928cd7a
RH
700\f
701#ifdef L_clzsi2
dabb3f04 702#undef int
dabb3f04 703int
8275b011 704__clzSI2 (UWtype x)
2928cd7a 705{
53585c36 706 Wtype ret;
2928cd7a 707
8275b011 708 count_leading_zeros (ret, x);
53585c36
RH
709
710 return ret;
2928cd7a
RH
711}
712#endif
713\f
714#ifdef L_clzdi2
dabb3f04 715#undef int
dabb3f04 716int
8275b011 717__clzDI2 (UDWtype x)
2928cd7a 718{
b982024e 719 const DWunion uu = {.ll = x};
53585c36
RH
720 UWtype word;
721 Wtype ret, add;
722
8275b011
RH
723 if (uu.s.high)
724 word = uu.s.high, add = 0;
53585c36 725 else
8275b011 726 word = uu.s.low, add = W_TYPE_SIZE;
2928cd7a 727
53585c36
RH
728 count_leading_zeros (ret, word);
729 return ret + add;
2928cd7a
RH
730}
731#endif
732\f
733#ifdef L_ctzsi2
dabb3f04 734#undef int
dabb3f04 735int
8275b011 736__ctzSI2 (UWtype x)
2928cd7a 737{
53585c36 738 Wtype ret;
2928cd7a 739
53585c36 740 count_trailing_zeros (ret, x);
2928cd7a 741
53585c36 742 return ret;
2928cd7a
RH
743}
744#endif
745\f
746#ifdef L_ctzdi2
dabb3f04 747#undef int
dabb3f04 748int
8275b011 749__ctzDI2 (UDWtype x)
2928cd7a 750{
b982024e 751 const DWunion uu = {.ll = x};
53585c36
RH
752 UWtype word;
753 Wtype ret, add;
754
8275b011
RH
755 if (uu.s.low)
756 word = uu.s.low, add = 0;
53585c36 757 else
8275b011 758 word = uu.s.high, add = W_TYPE_SIZE;
2928cd7a 759
53585c36
RH
760 count_trailing_zeros (ret, word);
761 return ret + add;
2928cd7a
RH
762}
763#endif
3801c801
BS
764\f
765#ifdef L_clrsbsi2
766#undef int
767int
768__clrsbSI2 (Wtype x)
769{
770 Wtype ret;
2928cd7a 771
3801c801
BS
772 if (x < 0)
773 x = ~x;
774 if (x == 0)
775 return W_TYPE_SIZE - 1;
776 count_leading_zeros (ret, x);
777 return ret - 1;
778}
779#endif
780\f
781#ifdef L_clrsbdi2
782#undef int
783int
784__clrsbDI2 (DWtype x)
785{
786 const DWunion uu = {.ll = x};
787 UWtype word;
788 Wtype ret, add;
789
790 if (uu.s.high == 0)
791 word = uu.s.low, add = W_TYPE_SIZE;
792 else if (uu.s.high == -1)
793 word = ~uu.s.low, add = W_TYPE_SIZE;
794 else if (uu.s.high >= 0)
795 word = uu.s.high, add = 0;
796 else
797 word = ~uu.s.high, add = 0;
798
799 if (word == 0)
800 ret = W_TYPE_SIZE;
801 else
802 count_leading_zeros (ret, word);
803
804 return ret + add - 1;
805}
806#endif
807\f
2928cd7a 808#ifdef L_popcount_tab
dcfae47c 809const UQItype __popcount_tab[256] =
2928cd7a
RH
810{
811 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,
812 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,
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 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,
815 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,
816 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,
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,
dcfae47c 818 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
819};
820#endif
821\f
4ea3d774
JJ
822#if defined(L_popcountsi2) || defined(L_popcountdi2)
823#define POPCOUNTCST2(x) (((UWtype) x << BITS_PER_UNIT) | x)
824#define POPCOUNTCST4(x) (((UWtype) x << (2 * BITS_PER_UNIT)) | x)
825#define POPCOUNTCST8(x) (((UWtype) x << (4 * BITS_PER_UNIT)) | x)
826#if W_TYPE_SIZE == BITS_PER_UNIT
827#define POPCOUNTCST(x) x
828#elif W_TYPE_SIZE == 2 * BITS_PER_UNIT
829#define POPCOUNTCST(x) POPCOUNTCST2 (x)
830#elif W_TYPE_SIZE == 4 * BITS_PER_UNIT
831#define POPCOUNTCST(x) POPCOUNTCST4 (POPCOUNTCST2 (x))
832#elif W_TYPE_SIZE == 8 * BITS_PER_UNIT
833#define POPCOUNTCST(x) POPCOUNTCST8 (POPCOUNTCST4 (POPCOUNTCST2 (x)))
834#endif
835#endif
836\f
2928cd7a 837#ifdef L_popcountsi2
dabb3f04 838#undef int
dabb3f04 839int
8275b011 840__popcountSI2 (UWtype x)
2928cd7a 841{
4ea3d774
JJ
842 /* Force table lookup on targets like AVR and RL78 which only
843 pretend they have LIBGCC2_UNITS_PER_WORD 4, but actually
844 have 1, and other small word targets. */
845#if __SIZEOF_INT__ > 2 && defined (POPCOUNTCST) && BITS_PER_UNIT == 8
846 x = x - ((x >> 1) & POPCOUNTCST (0x55));
847 x = (x & POPCOUNTCST (0x33)) + ((x >> 2) & POPCOUNTCST (0x33));
848 x = (x + (x >> 4)) & POPCOUNTCST (0x0F);
849 return (x * POPCOUNTCST (0x01)) >> (W_TYPE_SIZE - BITS_PER_UNIT);
850#else
4000debb 851 int i, ret = 0;
8275b011
RH
852
853 for (i = 0; i < W_TYPE_SIZE; i += 8)
854 ret += __popcount_tab[(x >> i) & 0xff];
855
856 return ret;
4ea3d774 857#endif
2928cd7a
RH
858}
859#endif
860\f
861#ifdef L_popcountdi2
dabb3f04 862#undef int
dabb3f04 863int
8275b011 864__popcountDI2 (UDWtype x)
2928cd7a 865{
4ea3d774
JJ
866 /* Force table lookup on targets like AVR and RL78 which only
867 pretend they have LIBGCC2_UNITS_PER_WORD 4, but actually
868 have 1, and other small word targets. */
869#if __SIZEOF_INT__ > 2 && defined (POPCOUNTCST) && BITS_PER_UNIT == 8
870 const DWunion uu = {.ll = x};
871 UWtype x1 = uu.s.low, x2 = uu.s.high;
872 x1 = x1 - ((x1 >> 1) & POPCOUNTCST (0x55));
873 x2 = x2 - ((x2 >> 1) & POPCOUNTCST (0x55));
874 x1 = (x1 & POPCOUNTCST (0x33)) + ((x1 >> 2) & POPCOUNTCST (0x33));
875 x2 = (x2 & POPCOUNTCST (0x33)) + ((x2 >> 2) & POPCOUNTCST (0x33));
876 x1 = (x1 + (x1 >> 4)) & POPCOUNTCST (0x0F);
877 x2 = (x2 + (x2 >> 4)) & POPCOUNTCST (0x0F);
878 x1 += x2;
879 return (x1 * POPCOUNTCST (0x01)) >> (W_TYPE_SIZE - BITS_PER_UNIT);
880#else
4000debb 881 int i, ret = 0;
8275b011
RH
882
883 for (i = 0; i < 2*W_TYPE_SIZE; i += 8)
884 ret += __popcount_tab[(x >> i) & 0xff];
885
886 return ret;
4ea3d774 887#endif
2928cd7a
RH
888}
889#endif
890\f
891#ifdef L_paritysi2
dabb3f04 892#undef int
dabb3f04 893int
8275b011 894__paritySI2 (UWtype x)
2928cd7a 895{
8275b011
RH
896#if W_TYPE_SIZE > 64
897# error "fill out the table"
898#endif
899#if W_TYPE_SIZE > 32
900 x ^= x >> 32;
901#endif
902#if W_TYPE_SIZE > 16
903 x ^= x >> 16;
904#endif
905 x ^= x >> 8;
906 x ^= x >> 4;
907 x &= 0xf;
908 return (0x6996 >> x) & 1;
2928cd7a
RH
909}
910#endif
911\f
912#ifdef L_paritydi2
dabb3f04 913#undef int
dabb3f04 914int
8275b011 915__parityDI2 (UDWtype x)
2928cd7a 916{
b982024e
KG
917 const DWunion uu = {.ll = x};
918 UWtype nx = uu.s.low ^ uu.s.high;
8275b011
RH
919
920#if W_TYPE_SIZE > 64
921# error "fill out the table"
922#endif
923#if W_TYPE_SIZE > 32
924 nx ^= nx >> 32;
925#endif
926#if W_TYPE_SIZE > 16
2928cd7a 927 nx ^= nx >> 16;
8275b011 928#endif
2928cd7a 929 nx ^= nx >> 8;
53585c36 930 nx ^= nx >> 4;
0c9ed856
RH
931 nx &= 0xf;
932 return (0x6996 >> nx) & 1;
2928cd7a
RH
933}
934#endif
d6eacd48
RH
935
936#ifdef L_udivmoddi4
30b8f78b
KV
937#ifdef TARGET_HAS_NO_HW_DIVIDE
938
939#if (defined (L_udivdi3) || defined (L_divdi3) || \
940 defined (L_umoddi3) || defined (L_moddi3))
941static inline __attribute__ ((__always_inline__))
942#endif
943UDWtype
944__udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
945{
946 UDWtype q = 0, r = n, y = d;
947 UWtype lz1, lz2, i, k;
948
949 /* Implements align divisor shift dividend method. This algorithm
950 aligns the divisor under the dividend and then perform number of
951 test-subtract iterations which shift the dividend left. Number of
952 iterations is k + 1 where k is the number of bit positions the
953 divisor must be shifted left to align it under the dividend.
954 quotient bits can be saved in the rightmost positions of the dividend
955 as it shifts left on each test-subtract iteration. */
956
957 if (y <= r)
958 {
959 lz1 = __builtin_clzll (d);
960 lz2 = __builtin_clzll (n);
961
962 k = lz1 - lz2;
963 y = (y << k);
964
965 /* Dividend can exceed 2 ^ (width − 1) − 1 but still be less than the
966 aligned divisor. Normal iteration can drops the high order bit
967 of the dividend. Therefore, first test-subtract iteration is a
968 special case, saving its quotient bit in a separate location and
969 not shifting the dividend. */
970 if (r >= y)
971 {
972 r = r - y;
973 q = (1ULL << k);
974 }
975
976 if (k > 0)
977 {
978 y = y >> 1;
979
980 /* k additional iterations where k regular test subtract shift
981 dividend iterations are done. */
982 i = k;
983 do
984 {
985 if (r >= y)
986 r = ((r - y) << 1) + 1;
987 else
988 r = (r << 1);
989 i = i - 1;
990 } while (i != 0);
991
992 /* First quotient bit is combined with the quotient bits resulting
993 from the k regular iterations. */
994 q = q + r;
995 r = r >> k;
996 q = q - (r << k);
997 }
998 }
999
1000 if (rp)
1001 *rp = r;
1002 return q;
1003}
1004#else
203b91b9 1005
536bfcd0
RK
1006#if (defined (L_udivdi3) || defined (L_divdi3) || \
1007 defined (L_umoddi3) || defined (L_moddi3))
1ab9ba62 1008static inline __attribute__ ((__always_inline__))
536bfcd0 1009#endif
996ed075
JJ
1010UDWtype
1011__udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
203b91b9 1012{
b982024e
KG
1013 const DWunion nn = {.ll = n};
1014 const DWunion dd = {.ll = d};
996ed075
JJ
1015 DWunion rr;
1016 UWtype d0, d1, n0, n1, n2;
1017 UWtype q0, q1;
1018 UWtype b, bm;
203b91b9 1019
203b91b9
RS
1020 d0 = dd.s.low;
1021 d1 = dd.s.high;
1022 n0 = nn.s.low;
1023 n1 = nn.s.high;
1024
1025#if !UDIV_NEEDS_NORMALIZATION
1026 if (d1 == 0)
1027 {
1028 if (d0 > n1)
1029 {
1030 /* 0q = nn / 0D */
1031
1032 udiv_qrnnd (q0, n0, n1, n0, d0);
1033 q1 = 0;
1034
1035 /* Remainder in n0. */
1036 }
1037 else
1038 {
1039 /* qq = NN / 0d */
1040
1041 if (d0 == 0)
1042 d0 = 1 / d0; /* Divide intentionally by zero. */
1043
1044 udiv_qrnnd (q1, n1, 0, n1, d0);
1045 udiv_qrnnd (q0, n0, n1, n0, d0);
1046
1047 /* Remainder in n0. */
1048 }
1049
1050 if (rp != 0)
1051 {
1052 rr.s.low = n0;
1053 rr.s.high = 0;
1054 *rp = rr.ll;
1055 }
1056 }
1057
1058#else /* UDIV_NEEDS_NORMALIZATION */
1059
1060 if (d1 == 0)
1061 {
1062 if (d0 > n1)
1063 {
1064 /* 0q = nn / 0D */
1065
1066 count_leading_zeros (bm, d0);
1067
1068 if (bm != 0)
1069 {
1070 /* Normalize, i.e. make the most significant bit of the
1071 denominator set. */
1072
1073 d0 = d0 << bm;
996ed075 1074 n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm));
203b91b9
RS
1075 n0 = n0 << bm;
1076 }
1077
1078 udiv_qrnnd (q0, n0, n1, n0, d0);
1079 q1 = 0;
1080
1081 /* Remainder in n0 >> bm. */
1082 }
1083 else
1084 {
1085 /* qq = NN / 0d */
1086
1087 if (d0 == 0)
1088 d0 = 1 / d0; /* Divide intentionally by zero. */
1089
1090 count_leading_zeros (bm, d0);
1091
1092 if (bm == 0)
1093 {
1094 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
1095 conclude (the most significant bit of n1 is set) /\ (the
1096 leading quotient digit q1 = 1).
1097
1098 This special case is necessary, not an optimization.
996ed075 1099 (Shifts counts of W_TYPE_SIZE are undefined.) */
203b91b9
RS
1100
1101 n1 -= d0;
1102 q1 = 1;
1103 }
1104 else
1105 {
1106 /* Normalize. */
1107
996ed075 1108 b = W_TYPE_SIZE - bm;
203b91b9
RS
1109
1110 d0 = d0 << bm;
1111 n2 = n1 >> b;
1112 n1 = (n1 << bm) | (n0 >> b);
1113 n0 = n0 << bm;
1114
1115 udiv_qrnnd (q1, n1, n2, n1, d0);
1116 }
1117
0f41302f 1118 /* n1 != d0... */
203b91b9
RS
1119
1120 udiv_qrnnd (q0, n0, n1, n0, d0);
1121
1122 /* Remainder in n0 >> bm. */
1123 }
1124
1125 if (rp != 0)
1126 {
1127 rr.s.low = n0 >> bm;
1128 rr.s.high = 0;
1129 *rp = rr.ll;
1130 }
1131 }
1132#endif /* UDIV_NEEDS_NORMALIZATION */
1133
1134 else
1135 {
1136 if (d1 > n1)
1137 {
1138 /* 00 = nn / DD */
1139
1140 q0 = 0;
1141 q1 = 0;
1142
1143 /* Remainder in n1n0. */
1144 if (rp != 0)
1145 {
1146 rr.s.low = n0;
1147 rr.s.high = n1;
1148 *rp = rr.ll;
1149 }
1150 }
1151 else
1152 {
1153 /* 0q = NN / dd */
1154
1155 count_leading_zeros (bm, d1);
1156 if (bm == 0)
1157 {
1158 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
1159 conclude (the most significant bit of n1 is set) /\ (the
1160 quotient digit q0 = 0 or 1).
1161
1162 This special case is necessary, not an optimization. */
1163
1164 /* The condition on the next line takes advantage of that
1165 n1 >= d1 (true due to program flow). */
1166 if (n1 > d1 || n0 >= d0)
1167 {
1168 q0 = 1;
1169 sub_ddmmss (n1, n0, n1, n0, d1, d0);
1170 }
1171 else
1172 q0 = 0;
1173
1174 q1 = 0;
1175
1176 if (rp != 0)
1177 {
1178 rr.s.low = n0;
1179 rr.s.high = n1;
1180 *rp = rr.ll;
1181 }
1182 }
1183 else
1184 {
996ed075 1185 UWtype m1, m0;
203b91b9
RS
1186 /* Normalize. */
1187
996ed075 1188 b = W_TYPE_SIZE - bm;
203b91b9
RS
1189
1190 d1 = (d1 << bm) | (d0 >> b);
1191 d0 = d0 << bm;
1192 n2 = n1 >> b;
1193 n1 = (n1 << bm) | (n0 >> b);
1194 n0 = n0 << bm;
1195
1196 udiv_qrnnd (q0, n1, n2, n1, d1);
1197 umul_ppmm (m1, m0, q0, d0);
1198
1199 if (m1 > n1 || (m1 == n1 && m0 > n0))
1200 {
1201 q0--;
1202 sub_ddmmss (m1, m0, m1, m0, d1, d0);
1203 }
1204
1205 q1 = 0;
1206
1207 /* Remainder in (n1n0 - m1m0) >> bm. */
1208 if (rp != 0)
1209 {
1210 sub_ddmmss (n1, n0, n1, n0, m1, m0);
1211 rr.s.low = (n1 << b) | (n0 >> bm);
1212 rr.s.high = n1 >> bm;
1213 *rp = rr.ll;
1214 }
1215 }
1216 }
1217 }
1218
b982024e 1219 const DWunion ww = {{.low = q0, .high = q1}};
203b91b9
RS
1220 return ww.ll;
1221}
1222#endif
30b8f78b 1223#endif
203b91b9
RS
1224
1225#ifdef L_divdi3
996ed075
JJ
1226DWtype
1227__divdi3 (DWtype u, DWtype v)
203b91b9 1228{
c7ff6e7a 1229 Wtype c = 0;
b982024e
KG
1230 DWunion uu = {.ll = u};
1231 DWunion vv = {.ll = v};
996ed075 1232 DWtype w;
203b91b9 1233
203b91b9
RS
1234 if (uu.s.high < 0)
1235 c = ~c,
b68daef4 1236 uu.ll = -uu.ll;
203b91b9
RS
1237 if (vv.s.high < 0)
1238 c = ~c,
b68daef4 1239 vv.ll = -vv.ll;
203b91b9 1240
996ed075 1241 w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0);
203b91b9 1242 if (c)
b68daef4 1243 w = -w;
203b91b9
RS
1244
1245 return w;
1246}
1247#endif
1248
1249#ifdef L_moddi3
996ed075
JJ
1250DWtype
1251__moddi3 (DWtype u, DWtype v)
203b91b9 1252{
c7ff6e7a 1253 Wtype c = 0;
b982024e
KG
1254 DWunion uu = {.ll = u};
1255 DWunion vv = {.ll = v};
996ed075 1256 DWtype w;
203b91b9 1257
203b91b9
RS
1258 if (uu.s.high < 0)
1259 c = ~c,
b68daef4 1260 uu.ll = -uu.ll;
203b91b9 1261 if (vv.s.high < 0)
b68daef4 1262 vv.ll = -vv.ll;
203b91b9 1263
9c859be1 1264 (void) __udivmoddi4 (uu.ll, vv.ll, (UDWtype*)&w);
203b91b9 1265 if (c)
b68daef4 1266 w = -w;
203b91b9
RS
1267
1268 return w;
1269}
1270#endif
1271
1272#ifdef L_umoddi3
996ed075
JJ
1273UDWtype
1274__umoddi3 (UDWtype u, UDWtype v)
203b91b9 1275{
996ed075 1276 UDWtype w;
203b91b9
RS
1277
1278 (void) __udivmoddi4 (u, v, &w);
1279
1280 return w;
1281}
1282#endif
1283
1284#ifdef L_udivdi3
996ed075
JJ
1285UDWtype
1286__udivdi3 (UDWtype n, UDWtype d)
203b91b9 1287{
996ed075 1288 return __udivmoddi4 (n, d, (UDWtype *) 0);
203b91b9
RS
1289}
1290#endif
1291\f
1292#ifdef L_cmpdi2
c7ff6e7a 1293cmp_return_type
996ed075 1294__cmpdi2 (DWtype a, DWtype b)
203b91b9 1295{
b982024e
KG
1296 const DWunion au = {.ll = a};
1297 const DWunion bu = {.ll = b};
203b91b9
RS
1298
1299 if (au.s.high < bu.s.high)
1300 return 0;
1301 else if (au.s.high > bu.s.high)
1302 return 2;
996ed075 1303 if ((UWtype) au.s.low < (UWtype) bu.s.low)
203b91b9 1304 return 0;
996ed075 1305 else if ((UWtype) au.s.low > (UWtype) bu.s.low)
203b91b9
RS
1306 return 2;
1307 return 1;
1308}
1309#endif
1310
1311#ifdef L_ucmpdi2
c7ff6e7a 1312cmp_return_type
996ed075 1313__ucmpdi2 (DWtype a, DWtype b)
203b91b9 1314{
b982024e
KG
1315 const DWunion au = {.ll = a};
1316 const DWunion bu = {.ll = b};
203b91b9 1317
996ed075 1318 if ((UWtype) au.s.high < (UWtype) bu.s.high)
203b91b9 1319 return 0;
996ed075 1320 else if ((UWtype) au.s.high > (UWtype) bu.s.high)
203b91b9 1321 return 2;
996ed075 1322 if ((UWtype) au.s.low < (UWtype) bu.s.low)
203b91b9 1323 return 0;
996ed075 1324 else if ((UWtype) au.s.low > (UWtype) bu.s.low)
203b91b9
RS
1325 return 2;
1326 return 1;
1327}
1328#endif
1329\f
4e9db8b2 1330#if defined(L_fixunstfdi) && LIBGCC2_HAS_TF_MODE
f139f5fa 1331UDWtype
6da9c622 1332__fixunstfDI (TFtype a)
ab495388 1333{
ab495388
RS
1334 if (a < 0)
1335 return 0;
1336
1337 /* Compute high word of result, as a flonum. */
4f2e0d5e 1338 const TFtype b = (a / Wtype_MAXp1_F);
996ed075 1339 /* Convert that to fixed (but not to DWtype!),
ab495388 1340 and shift it into the high word. */
b982024e 1341 UDWtype v = (UWtype) b;
4f2e0d5e 1342 v <<= W_TYPE_SIZE;
ab495388
RS
1343 /* Remove high part from the TFtype, leaving the low part as flonum. */
1344 a -= (TFtype)v;
996ed075 1345 /* Convert that to fixed (but not to DWtype!) and add it in.
ab495388
RS
1346 Sometimes A comes out negative. This is significant, since
1347 A has more bits than a long int does. */
1348 if (a < 0)
996ed075 1349 v -= (UWtype) (- a);
ab495388 1350 else
996ed075 1351 v += (UWtype) a;
ab495388
RS
1352 return v;
1353}
1354#endif
1355
4e9db8b2 1356#if defined(L_fixtfdi) && LIBGCC2_HAS_TF_MODE
996ed075 1357DWtype
37ef1054 1358__fixtfdi (TFtype a)
ab495388
RS
1359{
1360 if (a < 0)
6da9c622
RK
1361 return - __fixunstfDI (-a);
1362 return __fixunstfDI (a);
ab495388
RS
1363}
1364#endif
1365
4e9db8b2 1366#if defined(L_fixunsxfdi) && LIBGCC2_HAS_XF_MODE
f139f5fa 1367UDWtype
6da9c622 1368__fixunsxfDI (XFtype a)
e0799b34 1369{
e0799b34
RS
1370 if (a < 0)
1371 return 0;
1372
1373 /* Compute high word of result, as a flonum. */
4f2e0d5e 1374 const XFtype b = (a / Wtype_MAXp1_F);
996ed075 1375 /* Convert that to fixed (but not to DWtype!),
e0799b34 1376 and shift it into the high word. */
b982024e 1377 UDWtype v = (UWtype) b;
4f2e0d5e 1378 v <<= W_TYPE_SIZE;
e0799b34
RS
1379 /* Remove high part from the XFtype, leaving the low part as flonum. */
1380 a -= (XFtype)v;
996ed075 1381 /* Convert that to fixed (but not to DWtype!) and add it in.
e0799b34
RS
1382 Sometimes A comes out negative. This is significant, since
1383 A has more bits than a long int does. */
1384 if (a < 0)
996ed075 1385 v -= (UWtype) (- a);
e0799b34 1386 else
996ed075 1387 v += (UWtype) a;
e0799b34
RS
1388 return v;
1389}
1390#endif
1391
4e9db8b2 1392#if defined(L_fixxfdi) && LIBGCC2_HAS_XF_MODE
996ed075 1393DWtype
37ef1054 1394__fixxfdi (XFtype a)
e0799b34
RS
1395{
1396 if (a < 0)
6da9c622
RK
1397 return - __fixunsxfDI (-a);
1398 return __fixunsxfDI (a);
e0799b34
RS
1399}
1400#endif
1401
4e9db8b2 1402#if defined(L_fixunsdfdi) && LIBGCC2_HAS_DF_MODE
f139f5fa 1403UDWtype
6da9c622 1404__fixunsdfDI (DFtype a)
203b91b9 1405{
4977bab6
ZW
1406 /* Get high part of result. The division here will just moves the radix
1407 point and will not cause any rounding. Then the conversion to integral
1408 type chops result as desired. */
4f2e0d5e 1409 const UWtype hi = a / Wtype_MAXp1_F;
203b91b9 1410
4977bab6
ZW
1411 /* Get low part of result. Convert `hi' to floating type and scale it back,
1412 then subtract this from the number being converted. This leaves the low
1413 part. Convert that to integral type. */
4f2e0d5e 1414 const UWtype lo = a - (DFtype) hi * Wtype_MAXp1_F;
4977bab6
ZW
1415
1416 /* Assemble result from the two parts. */
4f2e0d5e 1417 return ((UDWtype) hi << W_TYPE_SIZE) | lo;
203b91b9
RS
1418}
1419#endif
1420
4e9db8b2 1421#if defined(L_fixdfdi) && LIBGCC2_HAS_DF_MODE
996ed075 1422DWtype
37ef1054 1423__fixdfdi (DFtype a)
203b91b9
RS
1424{
1425 if (a < 0)
6da9c622
RK
1426 return - __fixunsdfDI (-a);
1427 return __fixunsdfDI (a);
203b91b9
RS
1428}
1429#endif
1430
cfa7bd9c 1431#if defined(L_fixunssfdi) && LIBGCC2_HAS_SF_MODE
f139f5fa 1432UDWtype
4f2e0d5e 1433__fixunssfDI (SFtype a)
203b91b9 1434{
4e9db8b2 1435#if LIBGCC2_HAS_DF_MODE
ab495388 1436 /* Convert the SFtype to a DFtype, because that is surely not going
203b91b9 1437 to lose any bits. Some day someone else can write a faster version
ab495388 1438 that avoids converting to DFtype, and verify it really works right. */
4f2e0d5e 1439 const DFtype dfa = a;
203b91b9 1440
4977bab6
ZW
1441 /* Get high part of result. The division here will just moves the radix
1442 point and will not cause any rounding. Then the conversion to integral
1443 type chops result as desired. */
4f2e0d5e 1444 const UWtype hi = dfa / Wtype_MAXp1_F;
203b91b9 1445
4977bab6
ZW
1446 /* Get low part of result. Convert `hi' to floating type and scale it back,
1447 then subtract this from the number being converted. This leaves the low
1448 part. Convert that to integral type. */
4f2e0d5e 1449 const UWtype lo = dfa - (DFtype) hi * Wtype_MAXp1_F;
4977bab6
ZW
1450
1451 /* Assemble result from the two parts. */
4f2e0d5e
RH
1452 return ((UDWtype) hi << W_TYPE_SIZE) | lo;
1453#elif FLT_MANT_DIG < W_TYPE_SIZE
1454 if (a < 1)
1455 return 0;
1456 if (a < Wtype_MAXp1_F)
1457 return (UWtype)a;
1458 if (a < Wtype_MAXp1_F * Wtype_MAXp1_F)
1459 {
1460 /* Since we know that there are fewer significant bits in the SFmode
1461 quantity than in a word, we know that we can convert out all the
2e681715 1462 significant bits in one step, and thus avoid losing bits. */
4f2e0d5e
RH
1463
1464 /* ??? This following loop essentially performs frexpf. If we could
1465 use the real libm function, or poke at the actual bits of the fp
1466 format, it would be significantly faster. */
1467
1468 UWtype shift = 0, counter;
1469 SFtype msb;
1470
1471 a /= Wtype_MAXp1_F;
1472 for (counter = W_TYPE_SIZE / 2; counter != 0; counter >>= 1)
1473 {
1474 SFtype counterf = (UWtype)1 << counter;
1475 if (a >= counterf)
1476 {
1477 shift |= counter;
1478 a /= counterf;
1479 }
1480 }
1481
1482 /* Rescale into the range of one word, extract the bits of that
1483 one word, and shift the result into position. */
1484 a *= Wtype_MAXp1_F;
1485 counter = a;
1486 return (DWtype)counter << shift;
1487 }
1488 return -1;
1489#else
1490# error
1491#endif
203b91b9
RS
1492}
1493#endif
1494
cfa7bd9c 1495#if defined(L_fixsfdi) && LIBGCC2_HAS_SF_MODE
996ed075 1496DWtype
ab495388 1497__fixsfdi (SFtype a)
203b91b9
RS
1498{
1499 if (a < 0)
6da9c622
RK
1500 return - __fixunssfDI (-a);
1501 return __fixunssfDI (a);
203b91b9
RS
1502}
1503#endif
1504
4e9db8b2 1505#if defined(L_floatdixf) && LIBGCC2_HAS_XF_MODE
e0799b34 1506XFtype
996ed075 1507__floatdixf (DWtype u)
e0799b34 1508{
4a73d865
JM
1509#if W_TYPE_SIZE > XF_SIZE
1510# error
1511#endif
4f2e0d5e
RH
1512 XFtype d = (Wtype) (u >> W_TYPE_SIZE);
1513 d *= Wtype_MAXp1_F;
1514 d += (UWtype)u;
e5e809f4 1515 return d;
e0799b34
RS
1516}
1517#endif
1518
d7735880
JM
1519#if defined(L_floatundixf) && LIBGCC2_HAS_XF_MODE
1520XFtype
1521__floatundixf (UDWtype u)
1522{
4a73d865
JM
1523#if W_TYPE_SIZE > XF_SIZE
1524# error
1525#endif
d7735880
JM
1526 XFtype d = (UWtype) (u >> W_TYPE_SIZE);
1527 d *= Wtype_MAXp1_F;
1528 d += (UWtype)u;
1529 return d;
1530}
1531#endif
1532
4e9db8b2 1533#if defined(L_floatditf) && LIBGCC2_HAS_TF_MODE
ab495388 1534TFtype
996ed075 1535__floatditf (DWtype u)
ab495388 1536{
4a73d865
JM
1537#if W_TYPE_SIZE > TF_SIZE
1538# error
1539#endif
4f2e0d5e
RH
1540 TFtype d = (Wtype) (u >> W_TYPE_SIZE);
1541 d *= Wtype_MAXp1_F;
1542 d += (UWtype)u;
e5e809f4 1543 return d;
ab495388
RS
1544}
1545#endif
1546
d7735880
JM
1547#if defined(L_floatunditf) && LIBGCC2_HAS_TF_MODE
1548TFtype
1549__floatunditf (UDWtype u)
1550{
4a73d865
JM
1551#if W_TYPE_SIZE > TF_SIZE
1552# error
203b91b9 1553#endif
4a73d865 1554 TFtype d = (UWtype) (u >> W_TYPE_SIZE);
d7735880
JM
1555 d *= Wtype_MAXp1_F;
1556 d += (UWtype)u;
1557 return d;
1558}
1559#endif
1560
4a73d865
JM
1561#if (defined(L_floatdisf) && LIBGCC2_HAS_SF_MODE) \
1562 || (defined(L_floatdidf) && LIBGCC2_HAS_DF_MODE)
4f2e0d5e 1563#define DI_SIZE (W_TYPE_SIZE * 2)
b04c9063
AM
1564#define F_MODE_OK(SIZE) \
1565 (SIZE < DI_SIZE \
1566 && SIZE > (DI_SIZE - SIZE + FSSIZE) \
5fb54b91 1567 && !AVOID_FP_TYPE_CONVERSION(SIZE))
4a73d865
JM
1568#if defined(L_floatdisf)
1569#define FUNC __floatdisf
1570#define FSTYPE SFtype
1571#define FSSIZE SF_SIZE
1572#else
1573#define FUNC __floatdidf
1574#define FSTYPE DFtype
1575#define FSSIZE DF_SIZE
1576#endif
203b91b9 1577
4a73d865
JM
1578FSTYPE
1579FUNC (DWtype u)
203b91b9 1580{
4a73d865 1581#if FSSIZE >= W_TYPE_SIZE
4f2e0d5e 1582 /* When the word size is small, we never get any rounding error. */
4a73d865 1583 FSTYPE f = (Wtype) (u >> W_TYPE_SIZE);
4f2e0d5e
RH
1584 f *= Wtype_MAXp1_F;
1585 f += (UWtype)u;
1586 return f;
4a73d865
JM
1587#elif (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE)) \
1588 || (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE)) \
1589 || (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1590
1591#if (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE))
1592# define FSIZE DF_SIZE
1593# define FTYPE DFtype
1594#elif (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE))
1595# define FSIZE XF_SIZE
1596# define FTYPE XFtype
1597#elif (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1598# define FSIZE TF_SIZE
1599# define FTYPE TFtype
4f2e0d5e
RH
1600#else
1601# error
1602#endif
1603
4a73d865 1604#define REP_BIT ((UDWtype) 1 << (DI_SIZE - FSIZE))
4f2e0d5e 1605
d9e1ab8d 1606 /* Protect against double-rounding error.
4f2e0d5e
RH
1607 Represent any low-order bits, that might be truncated by a bit that
1608 won't be lost. The bit can go in anywhere below the rounding position
4a73d865
JM
1609 of the FSTYPE. A fixed mask and bit position handles all usual
1610 configurations. */
1611 if (! (- ((DWtype) 1 << FSIZE) < u
1612 && u < ((DWtype) 1 << FSIZE)))
d9e1ab8d 1613 {
4a73d865 1614 if ((UDWtype) u & (REP_BIT - 1))
d9e1ab8d 1615 {
4a73d865
JM
1616 u &= ~ (REP_BIT - 1);
1617 u |= REP_BIT;
d9e1ab8d
RK
1618 }
1619 }
203b91b9 1620
4a73d865
JM
1621 /* Do the calculation in a wider type so that we don't lose any of
1622 the precision of the high word while multiplying it. */
1623 FTYPE f = (Wtype) (u >> W_TYPE_SIZE);
4f2e0d5e
RH
1624 f *= Wtype_MAXp1_F;
1625 f += (UWtype)u;
4a73d865 1626 return (FSTYPE) f;
4f2e0d5e 1627#else
4a73d865
JM
1628#if FSSIZE >= W_TYPE_SIZE - 2
1629# error
1630#endif
1631 /* Finally, the word size is larger than the number of bits in the
1632 required FSTYPE, and we've got no suitable wider type. The only
1633 way to avoid double rounding is to special case the
1634 extraction. */
4f2e0d5e
RH
1635
1636 /* If there are no high bits set, fall back to one conversion. */
1637 if ((Wtype)u == u)
4a73d865 1638 return (FSTYPE)(Wtype)u;
4f2e0d5e
RH
1639
1640 /* Otherwise, find the power of two. */
1641 Wtype hi = u >> W_TYPE_SIZE;
1642 if (hi < 0)
1f6eac90 1643 hi = -(UWtype) hi;
4f2e0d5e
RH
1644
1645 UWtype count, shift;
1646 count_leading_zeros (count, hi);
1647
1648 /* No leading bits means u == minimum. */
1649 if (count == 0)
4a73d865 1650 return -(Wtype_MAXp1_F * (Wtype_MAXp1_F / 2));
4f2e0d5e 1651
4a73d865 1652 shift = 1 + W_TYPE_SIZE - count;
4f2e0d5e
RH
1653
1654 /* Shift down the most significant bits. */
1655 hi = u >> shift;
1656
1657 /* If we lost any nonzero bits, set the lsb to ensure correct rounding. */
5fb54b91 1658 if ((UWtype)u << (W_TYPE_SIZE - shift))
4f2e0d5e
RH
1659 hi |= 1;
1660
1661 /* Convert the one word of data, and rescale. */
5fb54b91
RH
1662 FSTYPE f = hi, e;
1663 if (shift == W_TYPE_SIZE)
1664 e = Wtype_MAXp1_F;
1665 /* The following two cases could be merged if we knew that the target
1666 supported a native unsigned->float conversion. More often, we only
1667 have a signed conversion, and have to add extra fixup code. */
1668 else if (shift == W_TYPE_SIZE - 1)
1669 e = Wtype_MAXp1_F / 2;
1670 else
1671 e = (Wtype)1 << shift;
1672 return f * e;
4f2e0d5e 1673#endif
203b91b9
RS
1674}
1675#endif
1676
4a73d865
JM
1677#if (defined(L_floatundisf) && LIBGCC2_HAS_SF_MODE) \
1678 || (defined(L_floatundidf) && LIBGCC2_HAS_DF_MODE)
d7735880 1679#define DI_SIZE (W_TYPE_SIZE * 2)
b04c9063
AM
1680#define F_MODE_OK(SIZE) \
1681 (SIZE < DI_SIZE \
1682 && SIZE > (DI_SIZE - SIZE + FSSIZE) \
5fb54b91 1683 && !AVOID_FP_TYPE_CONVERSION(SIZE))
4a73d865
JM
1684#if defined(L_floatundisf)
1685#define FUNC __floatundisf
1686#define FSTYPE SFtype
1687#define FSSIZE SF_SIZE
1688#else
1689#define FUNC __floatundidf
1690#define FSTYPE DFtype
1691#define FSSIZE DF_SIZE
1692#endif
d7735880 1693
4a73d865
JM
1694FSTYPE
1695FUNC (UDWtype u)
d7735880 1696{
4a73d865 1697#if FSSIZE >= W_TYPE_SIZE
d7735880 1698 /* When the word size is small, we never get any rounding error. */
4a73d865 1699 FSTYPE f = (UWtype) (u >> W_TYPE_SIZE);
d7735880
JM
1700 f *= Wtype_MAXp1_F;
1701 f += (UWtype)u;
1702 return f;
4a73d865
JM
1703#elif (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE)) \
1704 || (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE)) \
1705 || (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1706
1707#if (LIBGCC2_HAS_DF_MODE && F_MODE_OK (DF_SIZE))
1708# define FSIZE DF_SIZE
1709# define FTYPE DFtype
1710#elif (LIBGCC2_HAS_XF_MODE && F_MODE_OK (XF_SIZE))
1711# define FSIZE XF_SIZE
1712# define FTYPE XFtype
1713#elif (LIBGCC2_HAS_TF_MODE && F_MODE_OK (TF_SIZE))
1714# define FSIZE TF_SIZE
1715# define FTYPE TFtype
d7735880
JM
1716#else
1717# error
1718#endif
1719
4a73d865 1720#define REP_BIT ((UDWtype) 1 << (DI_SIZE - FSIZE))
d7735880
JM
1721
1722 /* Protect against double-rounding error.
1723 Represent any low-order bits, that might be truncated by a bit that
1724 won't be lost. The bit can go in anywhere below the rounding position
4a73d865
JM
1725 of the FSTYPE. A fixed mask and bit position handles all usual
1726 configurations. */
1727 if (u >= ((UDWtype) 1 << FSIZE))
d7735880 1728 {
4a73d865 1729 if ((UDWtype) u & (REP_BIT - 1))
d7735880 1730 {
4a73d865
JM
1731 u &= ~ (REP_BIT - 1);
1732 u |= REP_BIT;
d7735880
JM
1733 }
1734 }
1735
4a73d865
JM
1736 /* Do the calculation in a wider type so that we don't lose any of
1737 the precision of the high word while multiplying it. */
1738 FTYPE f = (UWtype) (u >> W_TYPE_SIZE);
d7735880
JM
1739 f *= Wtype_MAXp1_F;
1740 f += (UWtype)u;
4a73d865 1741 return (FSTYPE) f;
d7735880 1742#else
4a73d865
JM
1743#if FSSIZE == W_TYPE_SIZE - 1
1744# error
1745#endif
1746 /* Finally, the word size is larger than the number of bits in the
1747 required FSTYPE, and we've got no suitable wider type. The only
1748 way to avoid double rounding is to special case the
1749 extraction. */
d7735880
JM
1750
1751 /* If there are no high bits set, fall back to one conversion. */
1752 if ((UWtype)u == u)
4a73d865 1753 return (FSTYPE)(UWtype)u;
d7735880
JM
1754
1755 /* Otherwise, find the power of two. */
1756 UWtype hi = u >> W_TYPE_SIZE;
1757
1758 UWtype count, shift;
1759 count_leading_zeros (count, hi);
1760
1761 shift = W_TYPE_SIZE - count;
1762
1763 /* Shift down the most significant bits. */
1764 hi = u >> shift;
1765
1766 /* If we lost any nonzero bits, set the lsb to ensure correct rounding. */
5fb54b91 1767 if ((UWtype)u << (W_TYPE_SIZE - shift))
d7735880
JM
1768 hi |= 1;
1769
1770 /* Convert the one word of data, and rescale. */
5fb54b91
RH
1771 FSTYPE f = hi, e;
1772 if (shift == W_TYPE_SIZE)
1773 e = Wtype_MAXp1_F;
1774 /* The following two cases could be merged if we knew that the target
1775 supported a native unsigned->float conversion. More often, we only
1776 have a signed conversion, and have to add extra fixup code. */
1777 else if (shift == W_TYPE_SIZE - 1)
1778 e = Wtype_MAXp1_F / 2;
1779 else
1780 e = (Wtype)1 << shift;
1781 return f * e;
d7735880
JM
1782#endif
1783}
1784#endif
1785
4e9db8b2 1786#if defined(L_fixunsxfsi) && LIBGCC2_HAS_XF_MODE
996ed075 1787UWtype
6da9c622 1788__fixunsxfSI (XFtype a)
e0799b34 1789{
5d0e6486
AO
1790 if (a >= - (DFtype) Wtype_MIN)
1791 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
996ed075 1792 return (Wtype) a;
e0799b34
RS
1793}
1794#endif
1795
4e9db8b2 1796#if defined(L_fixunsdfsi) && LIBGCC2_HAS_DF_MODE
996ed075 1797UWtype
6da9c622 1798__fixunsdfSI (DFtype a)
203b91b9 1799{
5d0e6486
AO
1800 if (a >= - (DFtype) Wtype_MIN)
1801 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
996ed075 1802 return (Wtype) a;
203b91b9
RS
1803}
1804#endif
1805
cfa7bd9c 1806#if defined(L_fixunssfsi) && LIBGCC2_HAS_SF_MODE
996ed075 1807UWtype
6da9c622 1808__fixunssfSI (SFtype a)
203b91b9 1809{
5d0e6486
AO
1810 if (a >= - (SFtype) Wtype_MIN)
1811 return (Wtype) (a + Wtype_MIN) - Wtype_MIN;
996ed075 1812 return (Wtype) a;
203b91b9 1813}
17684d46
RG
1814#endif
1815\f
1816/* Integer power helper used from __builtin_powi for non-constant
1817 exponents. */
1818
cfa7bd9c 1819#if (defined(L_powisf2) && LIBGCC2_HAS_SF_MODE) \
4e9db8b2
SE
1820 || (defined(L_powidf2) && LIBGCC2_HAS_DF_MODE) \
1821 || (defined(L_powixf2) && LIBGCC2_HAS_XF_MODE) \
1822 || (defined(L_powitf2) && LIBGCC2_HAS_TF_MODE)
17684d46
RG
1823# if defined(L_powisf2)
1824# define TYPE SFtype
1825# define NAME __powisf2
1826# elif defined(L_powidf2)
1827# define TYPE DFtype
1828# define NAME __powidf2
1829# elif defined(L_powixf2)
1830# define TYPE XFtype
1831# define NAME __powixf2
1832# elif defined(L_powitf2)
1833# define TYPE TFtype
1834# define NAME __powitf2
1835# endif
1836
0b8495ae
FJ
1837#undef int
1838#undef unsigned
17684d46 1839TYPE
0b8495ae 1840NAME (TYPE x, int m)
17684d46 1841{
0b8495ae 1842 unsigned int n = m < 0 ? -m : m;
17684d46
RG
1843 TYPE y = n % 2 ? x : 1;
1844 while (n >>= 1)
1845 {
1846 x = x * x;
1847 if (n % 2)
1848 y = y * x;
1849 }
1850 return m < 0 ? 1/y : y;
1851}
1852
203b91b9
RS
1853#endif
1854\f
cfa7bd9c 1855#if ((defined(L_mulsc3) || defined(L_divsc3)) && LIBGCC2_HAS_SF_MODE) \
4e9db8b2
SE
1856 || ((defined(L_muldc3) || defined(L_divdc3)) && LIBGCC2_HAS_DF_MODE) \
1857 || ((defined(L_mulxc3) || defined(L_divxc3)) && LIBGCC2_HAS_XF_MODE) \
1858 || ((defined(L_multc3) || defined(L_divtc3)) && LIBGCC2_HAS_TF_MODE)
7e7e470f
RH
1859
1860#undef float
1861#undef double
1862#undef long
1863
1864#if defined(L_mulsc3) || defined(L_divsc3)
1865# define MTYPE SFtype
1866# define CTYPE SCtype
1867# define MODE sc
1868# define CEXT f
1869# define NOTRUNC __FLT_EVAL_METHOD__ == 0
1870#elif defined(L_muldc3) || defined(L_divdc3)
1871# define MTYPE DFtype
1872# define CTYPE DCtype
1873# define MODE dc
1874# if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 64
1875# define CEXT l
1876# define NOTRUNC 1
1877# else
1878# define CEXT
1879# define NOTRUNC __FLT_EVAL_METHOD__ == 0 || __FLT_EVAL_METHOD__ == 1
1880# endif
1881#elif defined(L_mulxc3) || defined(L_divxc3)
1882# define MTYPE XFtype
1883# define CTYPE XCtype
1884# define MODE xc
1885# define CEXT l
1886# define NOTRUNC 1
1887#elif defined(L_multc3) || defined(L_divtc3)
1888# define MTYPE TFtype
1889# define CTYPE TCtype
1890# define MODE tc
3353afbe
UB
1891# if LIBGCC2_LONG_DOUBLE_TYPE_SIZE == 128
1892# define CEXT l
1893# else
1894# define CEXT LIBGCC2_TF_CEXT
1895# endif
7e7e470f
RH
1896# define NOTRUNC 1
1897#else
1898# error
1899#endif
1900
1901#define CONCAT3(A,B,C) _CONCAT3(A,B,C)
1902#define _CONCAT3(A,B,C) A##B##C
1903
1904#define CONCAT2(A,B) _CONCAT2(A,B)
1905#define _CONCAT2(A,B) A##B
1906
1907/* All of these would be present in a full C99 implementation of <math.h>
1908 and <complex.h>. Our problem is that only a few systems have such full
647eea9d 1909 implementations. Further, libgcc_s.so isn't currently linked against
7e7e470f
RH
1910 libm.so, and even for systems that do provide full C99, the extra overhead
1911 of all programs using libgcc having to link against libm. So avoid it. */
1912
1913#define isnan(x) __builtin_expect ((x) != (x), 0)
1914#define isfinite(x) __builtin_expect (!isnan((x) - (x)), 1)
1915#define isinf(x) __builtin_expect (!isnan(x) & !isfinite(x), 0)
1916
ca22d882 1917#define INFINITY CONCAT2(__builtin_huge_val, CEXT) ()
7e7e470f
RH
1918#define I 1i
1919
1920/* Helpers to make the following code slightly less gross. */
1921#define COPYSIGN CONCAT2(__builtin_copysign, CEXT)
1922#define FABS CONCAT2(__builtin_fabs, CEXT)
1923
1924/* Verify that MTYPE matches up with CEXT. */
1925extern void *compile_type_assert[sizeof(INFINITY) == sizeof(MTYPE) ? 1 : -1];
1926
1927/* Ensure that we've lost any extra precision. */
1928#if NOTRUNC
1929# define TRUNC(x)
1930#else
1931# define TRUNC(x) __asm__ ("" : "=m"(x) : "m"(x))
1932#endif
1933
1934#if defined(L_mulsc3) || defined(L_muldc3) \
1935 || defined(L_mulxc3) || defined(L_multc3)
1936
1937CTYPE
1938CONCAT3(__mul,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
1939{
1940 MTYPE ac, bd, ad, bc, x, y;
ddef83d2 1941 CTYPE res;
7e7e470f
RH
1942
1943 ac = a * c;
1944 bd = b * d;
1945 ad = a * d;
1946 bc = b * c;
1947
1948 TRUNC (ac);
1949 TRUNC (bd);
1950 TRUNC (ad);
1951 TRUNC (bc);
1952
1953 x = ac - bd;
1954 y = ad + bc;
1955
1956 if (isnan (x) && isnan (y))
1957 {
1958 /* Recover infinities that computed as NaN + iNaN. */
1959 _Bool recalc = 0;
1960 if (isinf (a) || isinf (b))
1961 {
1962 /* z is infinite. "Box" the infinity and change NaNs in
1963 the other factor to 0. */
1964 a = COPYSIGN (isinf (a) ? 1 : 0, a);
1965 b = COPYSIGN (isinf (b) ? 1 : 0, b);
1966 if (isnan (c)) c = COPYSIGN (0, c);
1967 if (isnan (d)) d = COPYSIGN (0, d);
1968 recalc = 1;
1969 }
1970 if (isinf (c) || isinf (d))
1971 {
1972 /* w is infinite. "Box" the infinity and change NaNs in
1973 the other factor to 0. */
1974 c = COPYSIGN (isinf (c) ? 1 : 0, c);
1975 d = COPYSIGN (isinf (d) ? 1 : 0, d);
1976 if (isnan (a)) a = COPYSIGN (0, a);
1977 if (isnan (b)) b = COPYSIGN (0, b);
1978 recalc = 1;
1979 }
1980 if (!recalc
1981 && (isinf (ac) || isinf (bd)
1982 || isinf (ad) || isinf (bc)))
1983 {
1984 /* Recover infinities from overflow by changing NaNs to 0. */
1985 if (isnan (a)) a = COPYSIGN (0, a);
1986 if (isnan (b)) b = COPYSIGN (0, b);
1987 if (isnan (c)) c = COPYSIGN (0, c);
1988 if (isnan (d)) d = COPYSIGN (0, d);
1989 recalc = 1;
1990 }
1991 if (recalc)
1992 {
1993 x = INFINITY * (a * c - b * d);
1994 y = INFINITY * (a * d + b * c);
1995 }
1996 }
1997
ddef83d2
RG
1998 __real__ res = x;
1999 __imag__ res = y;
2000 return res;
7e7e470f
RH
2001}
2002#endif /* complex multiply */
2003
2004#if defined(L_divsc3) || defined(L_divdc3) \
2005 || defined(L_divxc3) || defined(L_divtc3)
2006
2007CTYPE
2008CONCAT3(__div,MODE,3) (MTYPE a, MTYPE b, MTYPE c, MTYPE d)
2009{
2010 MTYPE denom, ratio, x, y;
ddef83d2 2011 CTYPE res;
7e7e470f 2012
6300f037 2013 /* ??? We can get better behavior from logarithmic scaling instead of
7e7e470f
RH
2014 the division. But that would mean starting to link libgcc against
2015 libm. We could implement something akin to ldexp/frexp as gcc builtins
2016 fairly easily... */
2017 if (FABS (c) < FABS (d))
2018 {
2019 ratio = c / d;
2020 denom = (c * ratio) + d;
2021 x = ((a * ratio) + b) / denom;
2022 y = ((b * ratio) - a) / denom;
2023 }
2024 else
2025 {
2026 ratio = d / c;
2027 denom = (d * ratio) + c;
2028 x = ((b * ratio) + a) / denom;
2029 y = (b - (a * ratio)) / denom;
2030 }
2031
2032 /* Recover infinities and zeros that computed as NaN+iNaN; the only cases
647eea9d 2033 are nonzero/zero, infinite/finite, and finite/infinite. */
7e7e470f
RH
2034 if (isnan (x) && isnan (y))
2035 {
698ac934 2036 if (c == 0.0 && d == 0.0 && (!isnan (a) || !isnan (b)))
7e7e470f
RH
2037 {
2038 x = COPYSIGN (INFINITY, c) * a;
2039 y = COPYSIGN (INFINITY, c) * b;
2040 }
2041 else if ((isinf (a) || isinf (b)) && isfinite (c) && isfinite (d))
2042 {
2043 a = COPYSIGN (isinf (a) ? 1 : 0, a);
2044 b = COPYSIGN (isinf (b) ? 1 : 0, b);
2045 x = INFINITY * (a * c + b * d);
2046 y = INFINITY * (b * c - a * d);
2047 }
2048 else if ((isinf (c) || isinf (d)) && isfinite (a) && isfinite (b))
2049 {
2050 c = COPYSIGN (isinf (c) ? 1 : 0, c);
2051 d = COPYSIGN (isinf (d) ? 1 : 0, d);
2052 x = 0.0 * (a * c + b * d);
2053 y = 0.0 * (b * c - a * d);
2054 }
2055 }
2056
ddef83d2
RG
2057 __real__ res = x;
2058 __imag__ res = y;
2059 return res;
7e7e470f
RH
2060}
2061#endif /* complex divide */
2062
2063#endif /* all complex float routines */
2064\f
ab495388
RS
2065/* From here on down, the routines use normal data types. */
2066
2067#define SItype bogus_type
2068#define USItype bogus_type
2069#define DItype bogus_type
2070#define UDItype bogus_type
2071#define SFtype bogus_type
2072#define DFtype bogus_type
996ed075
JJ
2073#undef Wtype
2074#undef UWtype
2075#undef HWtype
2076#undef UHWtype
2077#undef DWtype
2078#undef UDWtype
ab495388
RS
2079
2080#undef char
2081#undef short
2082#undef int
2083#undef long
2084#undef unsigned
2085#undef float
2086#undef double
9bd23d2c
RS
2087\f
2088#ifdef L__gcc_bcmp
2089
2090/* Like bcmp except the sign is meaningful.
9faa82d8 2091 Result is negative if S1 is less than S2,
9bd23d2c
RS
2092 positive if S1 is greater, 0 if S1 and S2 are equal. */
2093
2094int
299b83b7 2095__gcc_bcmp (const unsigned char *s1, const unsigned char *s2, size_t size)
9bd23d2c
RS
2096{
2097 while (size > 0)
2098 {
b982024e 2099 const unsigned char c1 = *s1++, c2 = *s2++;
9bd23d2c
RS
2100 if (c1 != c2)
2101 return c1 - c2;
2102 size--;
2103 }
2104 return 0;
2105}
ab495388 2106
3fe68d0a
ZW
2107#endif
2108\f
2109/* __eprintf used to be used by GCC's private version of <assert.h>.
2110 We no longer provide that header, but this routine remains in libgcc.a
2111 for binary backward compatibility. Note that it is not included in
2112 the shared version of libgcc. */
2113#ifdef L_eprintf
2114#ifndef inhibit_libc
2115
2116#undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
2117#include <stdio.h>
2118
2119void
2120__eprintf (const char *string, const char *expression,
2121 unsigned int line, const char *filename)
2122{
2123 fprintf (stderr, string, expression, line, filename);
2124 fflush (stderr);
2125 abort ();
2126}
2127
2128#endif
203b91b9
RS
2129#endif
2130
203b91b9 2131\f
203b91b9
RS
2132#ifdef L_clear_cache
2133/* Clear part of an instruction cache. */
2134
203b91b9 2135void
139fa6f8
MM
2136__clear_cache (char *beg __attribute__((__unused__)),
2137 char *end __attribute__((__unused__)))
203b91b9 2138{
23190837 2139#ifdef CLEAR_INSN_CACHE
e1178973 2140 CLEAR_INSN_CACHE (beg, end);
e1178973 2141#endif /* CLEAR_INSN_CACHE */
203b91b9
RS
2142}
2143
2144#endif /* L_clear_cache */
2145\f
2146#ifdef L_trampoline
2147
2148/* Jump to a trampoline, loading the static chain address. */
2149
cd985f66 2150#if defined(WINNT) && ! defined(__CYGWIN__)
bf806a90 2151#include <windows.h>
0a38153f
KT
2152int getpagesize (void);
2153int mprotect (char *,int, int);
e3367a77 2154
94c1e7ac 2155int
3e7d8ef1 2156getpagesize (void)
f5ea9817
RK
2157{
2158#ifdef _ALPHA_
2159 return 8192;
2160#else
2161 return 4096;
2162#endif
2163}
2164
272e2587
RK
2165int
2166mprotect (char *addr, int len, int prot)
f5ea9817 2167{
234952b3 2168 DWORD np, op;
f5ea9817 2169
272e2587
RK
2170 if (prot == 7)
2171 np = 0x40;
2172 else if (prot == 5)
2173 np = 0x20;
2174 else if (prot == 4)
2175 np = 0x10;
2176 else if (prot == 3)
2177 np = 0x04;
2178 else if (prot == 1)
2179 np = 0x02;
2180 else if (prot == 0)
2181 np = 0x01;
234952b3
OS
2182 else
2183 return -1;
f5ea9817
RK
2184
2185 if (VirtualProtect (addr, len, np, &op))
2186 return 0;
2187 else
2188 return -1;
f5ea9817
RK
2189}
2190
cd985f66 2191#endif /* WINNT && ! __CYGWIN__ */
f5ea9817 2192
23190837
AJ
2193#ifdef TRANSFER_FROM_TRAMPOLINE
2194TRANSFER_FROM_TRAMPOLINE
203b91b9 2195#endif
203b91b9
RS
2196#endif /* L_trampoline */
2197\f
cae21ae8 2198#ifndef __CYGWIN__
203b91b9
RS
2199#ifdef L__main
2200
2201#include "gbl-ctors.h"
7abc66b1 2202
c06cff95
RS
2203/* Some systems use __main in a way incompatible with its use in gcc, in these
2204 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
2205 give the same symbol without quotes for an alternative entry point. You
0f41302f 2206 must define both, or neither. */
c06cff95
RS
2207#ifndef NAME__MAIN
2208#define NAME__MAIN "__main"
2209#define SYMBOL__MAIN __main
2210#endif
203b91b9 2211
53d68b9f
JM
2212#if defined (__LIBGCC_INIT_SECTION_ASM_OP__) \
2213 || defined (__LIBGCC_INIT_ARRAY_SECTION_ASM_OP__)
fe1fd353
JM
2214#undef HAS_INIT_SECTION
2215#define HAS_INIT_SECTION
2216#endif
2217
2218#if !defined (HAS_INIT_SECTION) || !defined (OBJECT_FORMAT_ELF)
31cf0144
JM
2219
2220/* Some ELF crosses use crtstuff.c to provide __CTOR_LIST__, but use this
2221 code to run constructors. In that case, we need to handle EH here, too. */
2222
53d68b9f 2223#ifdef __LIBGCC_EH_FRAME_SECTION_NAME__
e4b776a6 2224#include "unwind-dw2-fde.h"
31cf0144
JM
2225extern unsigned char __EH_FRAME_BEGIN__[];
2226#endif
2227
203b91b9
RS
2228/* Run all the global destructors on exit from the program. */
2229
2230void
3e7d8ef1 2231__do_global_dtors (void)
203b91b9 2232{
89cf554b
RS
2233#ifdef DO_GLOBAL_DTORS_BODY
2234 DO_GLOBAL_DTORS_BODY;
2235#else
b40b9d93
MS
2236 static func_ptr *p = __DTOR_LIST__ + 1;
2237 while (*p)
2238 {
2239 p++;
2240 (*(p-1)) ();
2241 }
89cf554b 2242#endif
53d68b9f 2243#if defined (__LIBGCC_EH_FRAME_SECTION_NAME__) && !defined (HAS_INIT_SECTION)
a4ebb0e6
GRK
2244 {
2245 static int completed = 0;
2246 if (! completed)
2247 {
2248 completed = 1;
2249 __deregister_frame_info (__EH_FRAME_BEGIN__);
2250 }
2251 }
31cf0144 2252#endif
203b91b9 2253}
68d69835 2254#endif
203b91b9 2255
fe1fd353 2256#ifndef HAS_INIT_SECTION
203b91b9
RS
2257/* Run all the global constructors on entry to the program. */
2258
203b91b9 2259void
3e7d8ef1 2260__do_global_ctors (void)
203b91b9 2261{
53d68b9f 2262#ifdef __LIBGCC_EH_FRAME_SECTION_NAME__
31cf0144
JM
2263 {
2264 static struct object object;
2265 __register_frame_info (__EH_FRAME_BEGIN__, &object);
2266 }
2267#endif
203b91b9 2268 DO_GLOBAL_CTORS_BODY;
a218d5ba 2269 atexit (__do_global_dtors);
203b91b9 2270}
fe1fd353 2271#endif /* no HAS_INIT_SECTION */
203b91b9 2272
fe1fd353 2273#if !defined (HAS_INIT_SECTION) || defined (INVOKE__main)
203b91b9
RS
2274/* Subroutine called automatically by `main'.
2275 Compiling a global function named `main'
2276 produces an automatic call to this function at the beginning.
2277
2278 For many systems, this routine calls __do_global_ctors.
2279 For systems which support a .init section we use the .init section
2280 to run __do_global_ctors, so we need not do anything here. */
2281
4043d9c1 2282extern void SYMBOL__MAIN (void);
203b91b9 2283void
4043d9c1 2284SYMBOL__MAIN (void)
203b91b9
RS
2285{
2286 /* Support recursive calls to `main': run initializers just once. */
7e6f1890 2287 static int initialized;
203b91b9
RS
2288 if (! initialized)
2289 {
2290 initialized = 1;
2291 __do_global_ctors ();
2292 }
2293}
fe1fd353 2294#endif /* no HAS_INIT_SECTION or INVOKE__main */
203b91b9
RS
2295
2296#endif /* L__main */
cae21ae8 2297#endif /* __CYGWIN__ */
203b91b9 2298\f
ad38743d 2299#ifdef L_ctors
203b91b9
RS
2300
2301#include "gbl-ctors.h"
2302
2303/* Provide default definitions for the lists of constructors and
657be7af
JL
2304 destructors, so that we don't get linker errors. These symbols are
2305 intentionally bss symbols, so that gld and/or collect will provide
2306 the right values. */
203b91b9
RS
2307
2308/* We declare the lists here with two elements each,
657be7af
JL
2309 so that they are valid empty lists if no other definition is loaded.
2310
2311 If we are using the old "set" extensions to have the gnu linker
2312 collect ctors and dtors, then we __CTOR_LIST__ and __DTOR_LIST__
2313 must be in the bss/common section.
2314
2315 Long term no port should use those extensions. But many still do. */
53d68b9f
JM
2316#if !defined(__LIBGCC_INIT_SECTION_ASM_OP__) \
2317 && !defined(CTOR_LISTS_DEFINED_EXTERNALLY)
aa6ad1a6 2318#if defined (TARGET_ASM_CONSTRUCTOR) || defined (USE_COLLECT2)
d15d0264
RS
2319func_ptr __CTOR_LIST__[2] = {0, 0};
2320func_ptr __DTOR_LIST__[2] = {0, 0};
657be7af
JL
2321#else
2322func_ptr __CTOR_LIST__[2];
2323func_ptr __DTOR_LIST__[2];
2324#endif
53d68b9f 2325#endif /* no __LIBGCC_INIT_SECTION_ASM_OP__ and not CTOR_LISTS_DEFINED_EXTERNALLY */
ad38743d 2326#endif /* L_ctors */
baffad1f 2327#endif /* LIBGCC2_UNITS_PER_WORD <= MIN_UNITS_PER_WORD */