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