1 //===-- udivsi3.c - Implement __udivsi3 -----------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements __udivsi3 for the compiler_rt library.
11 //===----------------------------------------------------------------------===//
17 // Translated from Figure 3-40 of The PowerPC Compiler Writer's Guide
19 // This function should not call __divsi3!
20 COMPILER_RT_ABI su_int __udivsi3(su_int n, su_int d) {
21 const unsigned n_uword_bits = sizeof(su_int) * CHAR_BIT;
30 sr = __builtin_clz(d) - __builtin_clz(n);
31 // 0 <= sr <= n_uword_bits - 1 or sr large
32 if (sr > n_uword_bits - 1) // d > r
34 if (sr == n_uword_bits - 1) // d == 1
37 // 1 <= sr <= n_uword_bits - 1
39 q = n << (n_uword_bits - sr);
42 for (; sr > 0; --sr) {
43 // r:q = ((r:q) << 1) | carry
44 r = (r << 1) | (q >> (n_uword_bits - 1));
47 // if (r.all >= d.all)
52 const si_int s = (si_int)(d - r - 1) >> (n_uword_bits - 1);
60 #if defined(__ARM_EABI__)
61 COMPILER_RT_ALIAS(__udivsi3, __aeabi_uidiv)