Path: blob/master/libs/compiler-rt/lib/builtins/udivsi3.c
4395 views
/* ===-- udivsi3.c - Implement __udivsi3 -----------------------------------===1*2* The LLVM Compiler Infrastructure3*4* This file is dual licensed under the MIT and the University of Illinois Open5* Source Licenses. See LICENSE.TXT for details.6*7* ===----------------------------------------------------------------------===8*9* This file implements __udivsi3 for the compiler_rt library.10*11* ===----------------------------------------------------------------------===12*/1314#include "int_lib.h"1516/* Returns: a / b */1718/* Translated from Figure 3-40 of The PowerPC Compiler Writer's Guide */1920/* This function should not call __divsi3! */21COMPILER_RT_ABI su_int22__udivsi3(su_int n, su_int d)23{24const unsigned n_uword_bits = sizeof(su_int) * CHAR_BIT;25su_int q;26su_int r;27unsigned sr;28/* special cases */29if (d == 0)30return 0; /* ?! */31if (n == 0)32return 0;33sr = __builtin_clz(d) - __builtin_clz(n);34/* 0 <= sr <= n_uword_bits - 1 or sr large */35if (sr > n_uword_bits - 1) /* d > r */36return 0;37if (sr == n_uword_bits - 1) /* d == 1 */38return n;39++sr;40/* 1 <= sr <= n_uword_bits - 1 */41/* Not a special case */42q = n << (n_uword_bits - sr);43r = n >> sr;44su_int carry = 0;45for (; sr > 0; --sr)46{47/* r:q = ((r:q) << 1) | carry */48r = (r << 1) | (q >> (n_uword_bits - 1));49q = (q << 1) | carry;50/* carry = 0;51* if (r.all >= d.all)52* {53* r.all -= d.all;54* carry = 1;55* }56*/57const si_int s = (si_int)(d - r - 1) >> (n_uword_bits - 1);58carry = s & 1;59r -= d & s;60}61q = (q << 1) | carry;62return q;63}6465#if defined(__ARM_EABI__)66AEABI_RTABI su_int __aeabi_uidiv(su_int n, su_int d) COMPILER_RT_ALIAS(__udivsi3);67#endif686970