2009-08-08 04:30:09 +08:00
|
|
|
/* ===-- udivsi3.c - Implement __udivsi3 -----------------------------------===
|
|
|
|
*
|
|
|
|
* The LLVM Compiler Infrastructure
|
|
|
|
*
|
2010-11-17 06:13:33 +08:00
|
|
|
* This file is dual licensed under the MIT and the University of Illinois Open
|
|
|
|
* Source Licenses. See LICENSE.TXT for details.
|
2009-08-08 04:30:09 +08:00
|
|
|
*
|
|
|
|
* ===----------------------------------------------------------------------===
|
|
|
|
*
|
|
|
|
* This file implements __udivsi3 for the compiler_rt library.
|
|
|
|
*
|
|
|
|
* ===----------------------------------------------------------------------===
|
|
|
|
*/
|
2009-06-27 00:47:03 +08:00
|
|
|
|
|
|
|
#include "int_lib.h"
|
|
|
|
|
2009-08-08 04:30:09 +08:00
|
|
|
/* Returns: a / b */
|
2009-06-27 00:47:03 +08:00
|
|
|
|
2009-08-08 04:30:09 +08:00
|
|
|
/* Translated from Figure 3-40 of The PowerPC Compiler Writer's Guide */
|
2009-06-27 00:47:03 +08:00
|
|
|
|
2012-06-22 22:44:13 +08:00
|
|
|
/* This function should not call __divsi3! */
|
2011-04-20 01:52:09 +08:00
|
|
|
COMPILER_RT_ABI su_int
|
2009-06-27 00:47:03 +08:00
|
|
|
__udivsi3(su_int n, su_int d)
|
|
|
|
{
|
|
|
|
const unsigned n_uword_bits = sizeof(su_int) * CHAR_BIT;
|
|
|
|
su_int q;
|
|
|
|
su_int r;
|
|
|
|
unsigned sr;
|
2009-08-08 04:30:09 +08:00
|
|
|
/* special cases */
|
2009-06-27 00:47:03 +08:00
|
|
|
if (d == 0)
|
2009-08-08 04:30:09 +08:00
|
|
|
return 0; /* ?! */
|
2009-06-27 00:47:03 +08:00
|
|
|
if (n == 0)
|
|
|
|
return 0;
|
|
|
|
sr = __builtin_clz(d) - __builtin_clz(n);
|
2009-08-08 04:30:09 +08:00
|
|
|
/* 0 <= sr <= n_uword_bits - 1 or sr large */
|
|
|
|
if (sr > n_uword_bits - 1) /* d > r */
|
2009-06-27 00:47:03 +08:00
|
|
|
return 0;
|
2009-08-08 04:30:09 +08:00
|
|
|
if (sr == n_uword_bits - 1) /* d == 1 */
|
2009-06-27 00:47:03 +08:00
|
|
|
return n;
|
|
|
|
++sr;
|
2009-08-08 04:30:09 +08:00
|
|
|
/* 1 <= sr <= n_uword_bits - 1 */
|
|
|
|
/* Not a special case */
|
2009-06-27 00:47:03 +08:00
|
|
|
q = n << (n_uword_bits - sr);
|
|
|
|
r = n >> sr;
|
|
|
|
su_int carry = 0;
|
|
|
|
for (; sr > 0; --sr)
|
|
|
|
{
|
2009-08-08 04:30:09 +08:00
|
|
|
/* r:q = ((r:q) << 1) | carry */
|
2009-06-27 00:47:03 +08:00
|
|
|
r = (r << 1) | (q >> (n_uword_bits - 1));
|
|
|
|
q = (q << 1) | carry;
|
2009-08-08 04:30:09 +08:00
|
|
|
/* carry = 0;
|
|
|
|
* if (r.all >= d.all)
|
|
|
|
* {
|
|
|
|
* r.all -= d.all;
|
|
|
|
* carry = 1;
|
|
|
|
* }
|
|
|
|
*/
|
2009-06-27 00:47:03 +08:00
|
|
|
const si_int s = (si_int)(d - r - 1) >> (n_uword_bits - 1);
|
|
|
|
carry = s & 1;
|
|
|
|
r -= d & s;
|
|
|
|
}
|
|
|
|
q = (q << 1) | carry;
|
|
|
|
return q;
|
|
|
|
}
|
2017-05-17 00:41:37 +08:00
|
|
|
|
|
|
|
#if defined(__ARM_EABI__)
|
2017-10-04 05:25:07 +08:00
|
|
|
AEABI_RTABI su_int __aeabi_uidiv(su_int n, su_int d) COMPILER_RT_ALIAS(__udivsi3);
|
2017-05-17 00:41:37 +08:00
|
|
|
#endif
|