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