1 // SPDX-License-Identifier: GPL-2.0+
3 * This file is part of GNU CC.
6 typedef unsigned int UWtype;
7 typedef unsigned int UHWtype;
8 typedef unsigned long long UDWtype;
11 typedef unsigned char UQItype;
13 typedef unsigned long USItype;
14 typedef long long DItype;
15 typedef unsigned long long DSItype;
20 typedef int word_type;
22 typedef long long DWtype;
24 struct DWstruct { Wtype low, high;};
32 #define BITS_PER_UNIT 8
35 __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp);
37 const UQItype __clz_tab[256] =
39 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,
40 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,
41 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,
42 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,
43 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,
44 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,
45 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,
46 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
51 __ashldi3 (DWtype u, word_type b)
56 const DWunion uu = {.ll = u};
57 const word_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
63 w.s.high = (UWtype) uu.s.low << -bm;
67 const UWtype carries = (UWtype) uu.s.low >> bm;
69 w.s.low = (UWtype) uu.s.low << b;
70 w.s.high = ((UWtype) uu.s.high << b) | carries;
77 __ashrdi3 (DWtype u, word_type b)
82 const DWunion uu = {.ll = u};
83 const word_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
88 /* w.s.high = 1..1 or 0..0 */
89 w.s.high = uu.s.high >> (sizeof (Wtype) * BITS_PER_UNIT - 1);
90 w.s.low = uu.s.high >> -bm;
94 const UWtype carries = (UWtype) uu.s.high << bm;
96 w.s.high = uu.s.high >> b;
97 w.s.low = ((UWtype) uu.s.low >> b) | carries;
104 __lshrdi3 (DWtype u, word_type b)
109 const DWunion uu = {.ll = u};
110 const word_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
116 w.s.low = (UWtype) uu.s.high >> -bm;
120 const UWtype carries = (UWtype) uu.s.high << bm;
122 w.s.high = (UWtype) uu.s.high >> b;
123 w.s.low = ((UWtype) uu.s.low >> b) | carries;
130 __cmpdi2 (DWtype a, DWtype b)
132 const DWunion au = {.ll = a};
133 const DWunion bu = {.ll = b};
135 if (au.s.high < bu.s.high)
137 else if (au.s.high > bu.s.high)
139 if ((UWtype) au.s.low < (UWtype) bu.s.low)
141 else if ((UWtype) au.s.low > (UWtype) bu.s.low)
147 __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
149 const DWunion nn = {.ll = n};
150 const DWunion dd = {.ll = d};
152 UWtype d0, d1, n0, n1, n2;
161 #if !UDIV_NEEDS_NORMALIZATION
168 udiv_qrnnd (q0, n0, n1, n0, d0);
171 /* Remainder in n0. */
178 d0 = 1 / d0; /* Divide intentionally by zero. */
180 udiv_qrnnd (q1, n1, 0, n1, d0);
181 udiv_qrnnd (q0, n0, n1, n0, d0);
183 /* Remainder in n0. */
194 #else /* UDIV_NEEDS_NORMALIZATION */
202 count_leading_zeros (bm, d0);
206 /* Normalize, i.e. make the most significant bit of the
210 n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm));
214 udiv_qrnnd (q0, n0, n1, n0, d0);
217 /* Remainder in n0 >> bm. */
224 d0 = 1 / d0; /* Divide intentionally by zero. */
226 count_leading_zeros (bm, d0);
230 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
231 conclude (the most significant bit of n1 is set) /\ (the
232 leading quotient digit q1 = 1).
234 This special case is necessary, not an optimization.
235 (Shifts counts of W_TYPE_SIZE are undefined.) */
244 b = W_TYPE_SIZE - bm;
248 n1 = (n1 << bm) | (n0 >> b);
251 udiv_qrnnd (q1, n1, n2, n1, d0);
256 udiv_qrnnd (q0, n0, n1, n0, d0);
258 /* Remainder in n0 >> bm. */
268 #endif /* UDIV_NEEDS_NORMALIZATION */
279 /* Remainder in n1n0. */
291 count_leading_zeros (bm, d1);
294 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
295 conclude (the most significant bit of n1 is set) /\ (the
296 quotient digit q0 = 0 or 1).
298 This special case is necessary, not an optimization. */
300 /* The condition on the next line takes advantage of that
301 n1 >= d1 (true due to program flow). */
302 if (n1 > d1 || n0 >= d0)
305 sub_ddmmss (n1, n0, n1, n0, d1, d0);
324 b = W_TYPE_SIZE - bm;
326 d1 = (d1 << bm) | (d0 >> b);
329 n1 = (n1 << bm) | (n0 >> b);
332 udiv_qrnnd (q0, n1, n2, n1, d1);
333 umul_ppmm (m1, m0, q0, d0);
335 if (m1 > n1 || (m1 == n1 && m0 > n0))
338 sub_ddmmss (m1, m0, m1, m0, d1, d0);
343 /* Remainder in (n1n0 - m1m0) >> bm. */
346 sub_ddmmss (n1, n0, n1, n0, m1, m0);
347 rr.s.low = (n1 << b) | (n0 >> bm);
348 rr.s.high = n1 >> bm;
355 const DWunion ww = {{.low = q0, .high = q1}};
360 __divdi3 (DWtype u, DWtype v)
363 DWunion uu = {.ll = u};
364 DWunion vv = {.ll = v};
374 w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0);
384 const DWunion uu = {.ll = u};
385 const DWunion w = { {.low = -uu.s.low,
386 .high = -uu.s.high - ((UWtype) -uu.s.low > 0) } };
393 __muldi3 (DWtype u, DWtype v)
395 const DWunion uu = {.ll = u};
396 const DWunion vv = {.ll = v};
397 DWunion w = {.ll = __umulsidi3 (uu.s.low, vv.s.low)};
399 w.s.high += ((UWtype) uu.s.low * (UWtype) vv.s.high
400 + (UWtype) uu.s.high * (UWtype) vv.s.low);
406 __moddi3 (DWtype u, DWtype v)
409 DWunion uu = {.ll = u};
410 DWunion vv = {.ll = v};
419 (void) __udivmoddi4 (uu.ll, vv.ll, (UDWtype*)&w);
427 __ucmpdi2 (DWtype a, DWtype b)
429 const DWunion au = {.ll = a};
430 const DWunion bu = {.ll = b};
432 if ((UWtype) au.s.high < (UWtype) bu.s.high)
434 else if ((UWtype) au.s.high > (UWtype) bu.s.high)
436 if ((UWtype) au.s.low < (UWtype) bu.s.low)
438 else if ((UWtype) au.s.low > (UWtype) bu.s.low)
445 __udivdi3 (UDWtype n, UDWtype d)
447 return __udivmoddi4 (n, d, (UDWtype *) 0);
451 __umoddi3 (UDWtype u, UDWtype v)
454 (void) __udivmoddi4 (u, v, &w);
460 udivmodsi4(USItype num, USItype den, word_type modwanted)
465 while (den < num && bit && !(den & (1L<<31)))
480 if (modwanted) return num;
485 __divsi3 (SItype a, SItype b)
502 res = udivmodsi4 (a, b, 0);
512 __udivsi3 (SItype a, SItype b)
514 return udivmodsi4 (a, b, 0);
519 __modsi3 (SItype a, SItype b)
533 res = udivmodsi4 (a, b, 1);
542 __mulsi3 (SItype a, SItype b)
561 __umodsi3 (SItype a, SItype b)
564 return udivmodsi4 (a, b, 1);
568 __gcc_bcmp (const unsigned char *s1, const unsigned char *s2, unsigned long size)
572 const unsigned char c1 = *s1++, c2 = *s2++;