1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 1989-2013 Free Software Foundation, Inc.
9 __ashldi3(DWtype u, shift_count_type b)
14 const DWunion uu = {.ll = u};
15 const shift_count_type bm = W_TYPE_SIZE - b;
20 w.s.high = (UWtype)uu.s.low << -bm;
22 const UWtype carries = (UWtype) uu.s.low >> bm;
24 w.s.low = (UWtype)uu.s.low << b;
25 w.s.high = ((UWtype)uu.s.high << b) | carries;
32 __ashrdi3(DWtype u, shift_count_type b)
37 const DWunion uu = {.ll = u};
38 const shift_count_type bm = W_TYPE_SIZE - b;
42 /* w.s.high = 1..1 or 0..0 */
43 w.s.high = uu.s.high >> (W_TYPE_SIZE - 1);
44 w.s.low = uu.s.high >> -bm;
46 const UWtype carries = (UWtype) uu.s.high << bm;
48 w.s.high = uu.s.high >> b;
49 w.s.low = ((UWtype)uu.s.low >> b) | carries;
56 __lshrdi3(DWtype u, shift_count_type b)
61 const DWunion uu = {.ll = u};
62 const shift_count_type bm = W_TYPE_SIZE - b;
67 w.s.low = (UWtype)uu.s.high >> -bm;
69 const UWtype carries = (UWtype)uu.s.high << bm;
71 w.s.high = (UWtype)uu.s.high >> b;
72 w.s.low = ((UWtype)uu.s.low >> b) | carries;
79 udivmodsi4(unsigned long num, unsigned long den, int modwanted)
81 unsigned long bit = 1;
82 unsigned long res = 0;
84 while (den < num && bit && !(den & (1L<<31))) {
105 __divsi3(long a, long b)
120 res = udivmodsi4(a, b, 0);
129 __modsi3(long a, long b)
142 res = udivmodsi4(a, b, 1);
151 __udivsi3(long a, long b)
153 return udivmodsi4(a, b, 0);
157 __umodsi3(long a, long b)
159 return udivmodsi4(a, b, 1);
163 __udivmoddi4(UDWtype n, UDWtype d, UDWtype *rp)
165 UDWtype q = 0, r = n, y = d;
166 UWtype lz1, lz2, i, k;
169 * Implements align divisor shift dividend method. This algorithm
170 * aligns the divisor under the dividend and then perform number of
171 * test-subtract iterations which shift the dividend left. Number of
172 * iterations is k + 1 where k is the number of bit positions the
173 * divisor must be shifted left to align it under the dividend.
174 * quotient bits can be saved in the rightmost positions of the
175 * dividend as it shifts left on each test-subtract iteration.
179 lz1 = __builtin_clzll(d);
180 lz2 = __builtin_clzll(n);
186 * Dividend can exceed 2 ^ (width - 1) - 1 but still be less
187 * than the aligned divisor. Normal iteration can drops the
188 * high order bit of the dividend. Therefore, first
189 * test-subtract iteration is a special case, saving its
190 * quotient bit in a separate location and not shifting
203 * k additional iterations where k regular test
204 * subtract shift dividend iterations are done.
209 r = ((r - y) << 1) + 1;
216 * First quotient bit is combined with the quotient
217 * bits resulting from the k regular iterations.
232 __udivdi3(UDWtype n, UDWtype d)
234 return __udivmoddi4(n, d, (UDWtype *)0);