2009-08-08 04:30:09 +08:00
|
|
|
/* ===-- udivmoddi4.c - Implement __udivmoddi4 -----------------------------===
|
|
|
|
*
|
|
|
|
* 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 __udivmoddi4 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
|
|
|
/* Effects: if rem != 0, *rem = a % b
|
|
|
|
* 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
|
|
|
|
2011-04-20 01:52:09 +08:00
|
|
|
COMPILER_RT_ABI du_int
|
2009-06-27 00:47:03 +08:00
|
|
|
__udivmoddi4(du_int a, du_int b, du_int* rem)
|
|
|
|
{
|
|
|
|
const unsigned n_uword_bits = sizeof(su_int) * CHAR_BIT;
|
|
|
|
const unsigned n_udword_bits = sizeof(du_int) * CHAR_BIT;
|
|
|
|
udwords n;
|
|
|
|
n.all = a;
|
|
|
|
udwords d;
|
|
|
|
d.all = b;
|
|
|
|
udwords q;
|
|
|
|
udwords r;
|
|
|
|
unsigned sr;
|
2009-08-08 04:30:09 +08:00
|
|
|
/* special cases, X is unknown, K != 0 */
|
2009-08-10 02:41:02 +08:00
|
|
|
if (n.s.high == 0)
|
2009-06-27 00:47:03 +08:00
|
|
|
{
|
2009-08-10 02:41:02 +08:00
|
|
|
if (d.s.high == 0)
|
2009-06-27 00:47:03 +08:00
|
|
|
{
|
2009-08-08 04:30:09 +08:00
|
|
|
/* 0 X
|
|
|
|
* ---
|
|
|
|
* 0 X
|
|
|
|
*/
|
2009-06-27 00:47:03 +08:00
|
|
|
if (rem)
|
2009-08-10 02:41:02 +08:00
|
|
|
*rem = n.s.low % d.s.low;
|
|
|
|
return n.s.low / d.s.low;
|
2009-06-27 00:47:03 +08:00
|
|
|
}
|
2009-08-08 04:30:09 +08:00
|
|
|
/* 0 X
|
|
|
|
* ---
|
|
|
|
* K X
|
|
|
|
*/
|
2009-06-27 00:47:03 +08:00
|
|
|
if (rem)
|
2009-08-10 02:41:02 +08:00
|
|
|
*rem = n.s.low;
|
2009-06-27 00:47:03 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2009-08-10 02:41:02 +08:00
|
|
|
/* n.s.high != 0 */
|
|
|
|
if (d.s.low == 0)
|
2009-06-27 00:47:03 +08:00
|
|
|
{
|
2009-08-10 02:41:02 +08:00
|
|
|
if (d.s.high == 0)
|
2009-06-27 00:47:03 +08:00
|
|
|
{
|
2009-08-08 04:30:09 +08:00
|
|
|
/* K X
|
|
|
|
* ---
|
|
|
|
* 0 0
|
|
|
|
*/
|
2009-06-27 00:47:03 +08:00
|
|
|
if (rem)
|
2009-08-10 02:41:02 +08:00
|
|
|
*rem = n.s.high % d.s.low;
|
|
|
|
return n.s.high / d.s.low;
|
2009-06-27 00:47:03 +08:00
|
|
|
}
|
2009-08-10 02:41:02 +08:00
|
|
|
/* d.s.high != 0 */
|
|
|
|
if (n.s.low == 0)
|
2009-06-27 00:47:03 +08:00
|
|
|
{
|
2009-08-08 04:30:09 +08:00
|
|
|
/* K 0
|
|
|
|
* ---
|
|
|
|
* K 0
|
|
|
|
*/
|
2009-06-27 00:47:03 +08:00
|
|
|
if (rem)
|
|
|
|
{
|
2009-08-10 02:41:02 +08:00
|
|
|
r.s.high = n.s.high % d.s.high;
|
|
|
|
r.s.low = 0;
|
2009-06-27 00:47:03 +08:00
|
|
|
*rem = r.all;
|
|
|
|
}
|
2009-08-10 02:41:02 +08:00
|
|
|
return n.s.high / d.s.high;
|
2009-06-27 00:47:03 +08:00
|
|
|
}
|
2009-08-08 04:30:09 +08:00
|
|
|
/* K K
|
|
|
|
* ---
|
|
|
|
* K 0
|
|
|
|
*/
|
2009-08-10 02:41:02 +08:00
|
|
|
if ((d.s.high & (d.s.high - 1)) == 0) /* if d is a power of 2 */
|
2009-06-27 00:47:03 +08:00
|
|
|
{
|
|
|
|
if (rem)
|
|
|
|
{
|
2009-08-10 02:41:02 +08:00
|
|
|
r.s.low = n.s.low;
|
|
|
|
r.s.high = n.s.high & (d.s.high - 1);
|
2009-06-27 00:47:03 +08:00
|
|
|
*rem = r.all;
|
|
|
|
}
|
2009-08-10 02:41:02 +08:00
|
|
|
return n.s.high >> __builtin_ctz(d.s.high);
|
2009-06-27 00:47:03 +08:00
|
|
|
}
|
2009-08-08 04:30:09 +08:00
|
|
|
/* K K
|
|
|
|
* ---
|
|
|
|
* K 0
|
|
|
|
*/
|
2009-08-10 02:41:02 +08:00
|
|
|
sr = __builtin_clz(d.s.high) - __builtin_clz(n.s.high);
|
2009-08-08 04:30:09 +08:00
|
|
|
/* 0 <= sr <= n_uword_bits - 2 or sr large */
|
2009-06-27 00:47:03 +08:00
|
|
|
if (sr > n_uword_bits - 2)
|
|
|
|
{
|
|
|
|
if (rem)
|
|
|
|
*rem = n.all;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
++sr;
|
2009-08-08 04:30:09 +08:00
|
|
|
/* 1 <= sr <= n_uword_bits - 1 */
|
|
|
|
/* q.all = n.all << (n_udword_bits - sr); */
|
2009-08-10 02:41:02 +08:00
|
|
|
q.s.low = 0;
|
|
|
|
q.s.high = n.s.low << (n_uword_bits - sr);
|
2009-08-08 04:30:09 +08:00
|
|
|
/* r.all = n.all >> sr; */
|
2009-08-10 02:41:02 +08:00
|
|
|
r.s.high = n.s.high >> sr;
|
|
|
|
r.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr);
|
2009-06-27 00:47:03 +08:00
|
|
|
}
|
2009-08-10 02:41:02 +08:00
|
|
|
else /* d.s.low != 0 */
|
2009-06-27 00:47:03 +08:00
|
|
|
{
|
2009-08-10 02:41:02 +08:00
|
|
|
if (d.s.high == 0)
|
2009-06-27 00:47:03 +08:00
|
|
|
{
|
2009-08-08 04:30:09 +08:00
|
|
|
/* K X
|
|
|
|
* ---
|
|
|
|
* 0 K
|
|
|
|
*/
|
2009-08-10 02:41:02 +08:00
|
|
|
if ((d.s.low & (d.s.low - 1)) == 0) /* if d is a power of 2 */
|
2009-06-27 00:47:03 +08:00
|
|
|
{
|
|
|
|
if (rem)
|
2009-08-10 02:41:02 +08:00
|
|
|
*rem = n.s.low & (d.s.low - 1);
|
|
|
|
if (d.s.low == 1)
|
2009-06-27 00:47:03 +08:00
|
|
|
return n.all;
|
2011-07-29 03:47:33 +08:00
|
|
|
sr = __builtin_ctz(d.s.low);
|
2009-08-10 02:41:02 +08:00
|
|
|
q.s.high = n.s.high >> sr;
|
|
|
|
q.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr);
|
2009-06-27 00:47:03 +08:00
|
|
|
return q.all;
|
|
|
|
}
|
2009-08-08 04:30:09 +08:00
|
|
|
/* K X
|
|
|
|
* ---
|
2014-03-19 04:41:31 +08:00
|
|
|
* 0 K
|
2009-08-08 04:30:09 +08:00
|
|
|
*/
|
2009-08-10 02:41:02 +08:00
|
|
|
sr = 1 + n_uword_bits + __builtin_clz(d.s.low) - __builtin_clz(n.s.high);
|
2009-08-08 04:30:09 +08:00
|
|
|
/* 2 <= sr <= n_udword_bits - 1
|
|
|
|
* q.all = n.all << (n_udword_bits - sr);
|
|
|
|
* r.all = n.all >> sr;
|
|
|
|
*/
|
2014-03-19 06:10:36 +08:00
|
|
|
if (sr == n_uword_bits)
|
|
|
|
{
|
|
|
|
q.s.low = 0;
|
|
|
|
q.s.high = n.s.low;
|
|
|
|
r.s.high = 0;
|
|
|
|
r.s.low = n.s.high;
|
|
|
|
}
|
|
|
|
else if (sr < n_uword_bits) // 2 <= sr <= n_uword_bits - 1
|
|
|
|
{
|
|
|
|
q.s.low = 0;
|
|
|
|
q.s.high = n.s.low << (n_uword_bits - sr);
|
|
|
|
r.s.high = n.s.high >> sr;
|
|
|
|
r.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr);
|
|
|
|
}
|
|
|
|
else // n_uword_bits + 1 <= sr <= n_udword_bits - 1
|
|
|
|
{
|
|
|
|
q.s.low = n.s.low << (n_udword_bits - sr);
|
|
|
|
q.s.high = (n.s.high << (n_udword_bits - sr)) |
|
|
|
|
(n.s.low >> (sr - n_uword_bits));
|
|
|
|
r.s.high = 0;
|
|
|
|
r.s.low = n.s.high >> (sr - n_uword_bits);
|
|
|
|
}
|
2009-06-27 00:47:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-08-08 04:30:09 +08:00
|
|
|
/* K X
|
|
|
|
* ---
|
|
|
|
* K K
|
|
|
|
*/
|
2009-08-10 02:41:02 +08:00
|
|
|
sr = __builtin_clz(d.s.high) - __builtin_clz(n.s.high);
|
2009-08-08 04:30:09 +08:00
|
|
|
/* 0 <= sr <= n_uword_bits - 1 or sr large */
|
2009-06-27 00:47:03 +08:00
|
|
|
if (sr > n_uword_bits - 1)
|
|
|
|
{
|
2014-03-19 06:10:36 +08:00
|
|
|
if (rem)
|
2009-06-27 00:47:03 +08:00
|
|
|
*rem = n.all;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
++sr;
|
2009-08-08 04:30:09 +08:00
|
|
|
/* 1 <= sr <= n_uword_bits */
|
|
|
|
/* q.all = n.all << (n_udword_bits - sr); */
|
2009-08-10 02:41:02 +08:00
|
|
|
q.s.low = 0;
|
2014-03-19 06:10:36 +08:00
|
|
|
if (sr == n_uword_bits)
|
|
|
|
{
|
|
|
|
q.s.high = n.s.low;
|
|
|
|
r.s.high = 0;
|
|
|
|
r.s.low = n.s.high;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
q.s.high = n.s.low << (n_uword_bits - sr);
|
|
|
|
r.s.high = n.s.high >> sr;
|
|
|
|
r.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr);
|
|
|
|
}
|
2009-06-27 00:47:03 +08:00
|
|
|
}
|
|
|
|
}
|
2009-08-08 04:30:09 +08:00
|
|
|
/* Not a special case
|
|
|
|
* q and r are initialized with:
|
|
|
|
* q.all = n.all << (n_udword_bits - sr);
|
|
|
|
* r.all = n.all >> sr;
|
|
|
|
* 1 <= sr <= n_udword_bits - 1
|
|
|
|
*/
|
2009-06-27 00:47:03 +08:00
|
|
|
su_int carry = 0;
|
|
|
|
for (; sr > 0; --sr)
|
|
|
|
{
|
2009-08-08 04:30:09 +08:00
|
|
|
/* r:q = ((r:q) << 1) | carry */
|
2009-08-10 02:41:02 +08:00
|
|
|
r.s.high = (r.s.high << 1) | (r.s.low >> (n_uword_bits - 1));
|
|
|
|
r.s.low = (r.s.low << 1) | (q.s.high >> (n_uword_bits - 1));
|
|
|
|
q.s.high = (q.s.high << 1) | (q.s.low >> (n_uword_bits - 1));
|
|
|
|
q.s.low = (q.s.low << 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 di_int s = (di_int)(d.all - r.all - 1) >> (n_udword_bits - 1);
|
|
|
|
carry = s & 1;
|
|
|
|
r.all -= d.all & s;
|
|
|
|
}
|
|
|
|
q.all = (q.all << 1) | carry;
|
|
|
|
if (rem)
|
|
|
|
*rem = r.all;
|
|
|
|
return q.all;
|
|
|
|
}
|