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