1*0a6a1f1dSLionel Sambuc /* ===-- udivsi3.c - Implement __udivsi3 -----------------------------------=== 2*0a6a1f1dSLionel Sambuc * 3*0a6a1f1dSLionel Sambuc * The LLVM Compiler Infrastructure 4*0a6a1f1dSLionel Sambuc * 5*0a6a1f1dSLionel Sambuc * This file is dual licensed under the MIT and the University of Illinois Open 6*0a6a1f1dSLionel Sambuc * Source Licenses. See LICENSE.TXT for details. 7*0a6a1f1dSLionel Sambuc * 8*0a6a1f1dSLionel Sambuc * ===----------------------------------------------------------------------=== 9*0a6a1f1dSLionel Sambuc * 10*0a6a1f1dSLionel Sambuc * This file implements __udivsi3 for the compiler_rt library. 11*0a6a1f1dSLionel Sambuc * 12*0a6a1f1dSLionel Sambuc * ===----------------------------------------------------------------------=== 13*0a6a1f1dSLionel Sambuc */ 14*0a6a1f1dSLionel Sambuc 15*0a6a1f1dSLionel Sambuc #include "int_lib.h" 16*0a6a1f1dSLionel Sambuc 17*0a6a1f1dSLionel Sambuc /* Returns: a / b */ 18*0a6a1f1dSLionel Sambuc 19*0a6a1f1dSLionel Sambuc /* Translated from Figure 3-40 of The PowerPC Compiler Writer's Guide */ 20*0a6a1f1dSLionel Sambuc ARM_EABI_FNALIAS(uidiv,udivsi3)21*0a6a1f1dSLionel SambucARM_EABI_FNALIAS(uidiv, udivsi3) 22*0a6a1f1dSLionel Sambuc 23*0a6a1f1dSLionel Sambuc /* This function should not call __divsi3! */ 24*0a6a1f1dSLionel Sambuc COMPILER_RT_ABI su_int 25*0a6a1f1dSLionel Sambuc __udivsi3(su_int n, su_int d) 26*0a6a1f1dSLionel Sambuc { 27*0a6a1f1dSLionel Sambuc const unsigned n_uword_bits = sizeof(su_int) * CHAR_BIT; 28*0a6a1f1dSLionel Sambuc su_int q; 29*0a6a1f1dSLionel Sambuc su_int r; 30*0a6a1f1dSLionel Sambuc unsigned sr; 31*0a6a1f1dSLionel Sambuc /* special cases */ 32*0a6a1f1dSLionel Sambuc if (d == 0) 33*0a6a1f1dSLionel Sambuc return 0; /* ?! */ 34*0a6a1f1dSLionel Sambuc if (n == 0) 35*0a6a1f1dSLionel Sambuc return 0; 36*0a6a1f1dSLionel Sambuc sr = __builtin_clz(d) - __builtin_clz(n); 37*0a6a1f1dSLionel Sambuc /* 0 <= sr <= n_uword_bits - 1 or sr large */ 38*0a6a1f1dSLionel Sambuc if (sr > n_uword_bits - 1) /* d > r */ 39*0a6a1f1dSLionel Sambuc return 0; 40*0a6a1f1dSLionel Sambuc if (sr == n_uword_bits - 1) /* d == 1 */ 41*0a6a1f1dSLionel Sambuc return n; 42*0a6a1f1dSLionel Sambuc ++sr; 43*0a6a1f1dSLionel Sambuc /* 1 <= sr <= n_uword_bits - 1 */ 44*0a6a1f1dSLionel Sambuc /* Not a special case */ 45*0a6a1f1dSLionel Sambuc q = n << (n_uword_bits - sr); 46*0a6a1f1dSLionel Sambuc r = n >> sr; 47*0a6a1f1dSLionel Sambuc su_int carry = 0; 48*0a6a1f1dSLionel Sambuc for (; sr > 0; --sr) 49*0a6a1f1dSLionel Sambuc { 50*0a6a1f1dSLionel Sambuc /* r:q = ((r:q) << 1) | carry */ 51*0a6a1f1dSLionel Sambuc r = (r << 1) | (q >> (n_uword_bits - 1)); 52*0a6a1f1dSLionel Sambuc q = (q << 1) | carry; 53*0a6a1f1dSLionel Sambuc /* carry = 0; 54*0a6a1f1dSLionel Sambuc * if (r.all >= d.all) 55*0a6a1f1dSLionel Sambuc * { 56*0a6a1f1dSLionel Sambuc * r.all -= d.all; 57*0a6a1f1dSLionel Sambuc * carry = 1; 58*0a6a1f1dSLionel Sambuc * } 59*0a6a1f1dSLionel Sambuc */ 60*0a6a1f1dSLionel Sambuc const si_int s = (si_int)(d - r - 1) >> (n_uword_bits - 1); 61*0a6a1f1dSLionel Sambuc carry = s & 1; 62*0a6a1f1dSLionel Sambuc r -= d & s; 63*0a6a1f1dSLionel Sambuc } 64*0a6a1f1dSLionel Sambuc q = (q << 1) | carry; 65*0a6a1f1dSLionel Sambuc return q; 66*0a6a1f1dSLionel Sambuc } 67