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