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 /* This function should not call __divsi3! */
22 COMPILER_RT_ABI su_int
23 __udivsi3(su_int n, su_int d)
25 const unsigned n_uword_bits = sizeof(su_int) * CHAR_BIT;
34 sr = __builtin_clz(d) - __builtin_clz(n);
35 /* 0 <= sr <= n_uword_bits - 1 or sr large */
36 if (sr > n_uword_bits - 1) /* d > r */
38 if (sr == n_uword_bits - 1) /* d == 1 */
41 /* 1 <= sr <= n_uword_bits - 1 */
42 /* Not a special case */
43 q = n << (n_uword_bits - sr);
48 /* r:q = ((r:q) << 1) | carry */
49 r = (r << 1) | (q >> (n_uword_bits - 1));
58 const si_int s = (si_int)(d - r - 1) >> (n_uword_bits - 1);
66 #if defined(__ARM_EABI__)
67 AEABI_RTABI su_int __aeabi_uidiv(su_int n, su_int d) COMPILER_RT_ALIAS(__udivsi3);