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