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