1 /* ===-- udivsi3.c - Implement __udivsi3 -----------------------------------===
3 * The LLVM Compiler Infrastructure
5 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
8 * ===----------------------------------------------------------------------===
10 * This file implements __udivsi3 for the compiler_rt library.
12 * ===----------------------------------------------------------------------===
19 /* Translated from Figure 3-40 of The PowerPC Compiler Writer's Guide */
21 ARM_EABI_FNALIAS(uidiv, udivsi3);
23 COMPILER_RT_ABI su_int
24 __udivsi3(su_int n, su_int d)
26 const unsigned n_uword_bits = sizeof(su_int) * CHAR_BIT;
35 sr = __builtin_clz(d) - __builtin_clz(n);
36 /* 0 <= sr <= n_uword_bits - 1 or sr large */
37 if (sr > n_uword_bits - 1) /* d > r */
39 if (sr == n_uword_bits - 1) /* d == 1 */
42 /* 1 <= sr <= n_uword_bits - 1 */
43 /* Not a special case */
44 q = n << (n_uword_bits - sr);
49 /* r:q = ((r:q) << 1) | carry */
50 r = (r << 1) | (q >> (n_uword_bits - 1));
59 const si_int s = (si_int)(d - r - 1) >> (n_uword_bits - 1);