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