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