blob: bb720f8c382b51006b33e92dc55f5da7457f1a83 [file] [log] [blame]
Edward O'Callaghan55836322009-08-07 20:30:09 +00001/* ===-- udivsi3.c - Implement __udivsi3 -----------------------------------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
Howard Hinnant5b791f62010-11-16 22:13:33 +00005 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
Edward O'Callaghan55836322009-08-07 20:30:09 +00007 *
8 * ===----------------------------------------------------------------------===
9 *
10 * This file implements __udivsi3 for the compiler_rt library.
11 *
12 * ===----------------------------------------------------------------------===
13 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000014
15#include "int_lib.h"
16
Edward O'Callaghan55836322009-08-07 20:30:09 +000017/* Returns: a / b */
Daniel Dunbarfd089992009-06-26 16:47:03 +000018
Edward O'Callaghan55836322009-08-07 20:30:09 +000019/* Translated from Figure 3-40 of The PowerPC Compiler Writer's Guide */
Daniel Dunbarfd089992009-06-26 16:47:03 +000020
Stephen Canonc3b81112012-06-22 14:44:13 +000021/* This function should not call __divsi3! */
Anton Korobeynikove63da932011-04-19 17:52:09 +000022COMPILER_RT_ABI su_int
Daniel Dunbarfd089992009-06-26 16:47:03 +000023__udivsi3(su_int n, su_int d)
24{
25 const unsigned n_uword_bits = sizeof(su_int) * CHAR_BIT;
26 su_int q;
27 su_int r;
28 unsigned sr;
Edward O'Callaghan55836322009-08-07 20:30:09 +000029 /* special cases */
Daniel Dunbarfd089992009-06-26 16:47:03 +000030 if (d == 0)
Edward O'Callaghan55836322009-08-07 20:30:09 +000031 return 0; /* ?! */
Daniel Dunbarfd089992009-06-26 16:47:03 +000032 if (n == 0)
33 return 0;
34 sr = __builtin_clz(d) - __builtin_clz(n);
Edward O'Callaghan55836322009-08-07 20:30:09 +000035 /* 0 <= sr <= n_uword_bits - 1 or sr large */
36 if (sr > n_uword_bits - 1) /* d > r */
Daniel Dunbarfd089992009-06-26 16:47:03 +000037 return 0;
Edward O'Callaghan55836322009-08-07 20:30:09 +000038 if (sr == n_uword_bits - 1) /* d == 1 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000039 return n;
40 ++sr;
Edward O'Callaghan55836322009-08-07 20:30:09 +000041 /* 1 <= sr <= n_uword_bits - 1 */
42 /* Not a special case */
Daniel Dunbarfd089992009-06-26 16:47:03 +000043 q = n << (n_uword_bits - sr);
44 r = n >> sr;
45 su_int carry = 0;
46 for (; sr > 0; --sr)
47 {
Edward O'Callaghan55836322009-08-07 20:30:09 +000048 /* r:q = ((r:q) << 1) | carry */
Daniel Dunbarfd089992009-06-26 16:47:03 +000049 r = (r << 1) | (q >> (n_uword_bits - 1));
50 q = (q << 1) | carry;
Edward O'Callaghan55836322009-08-07 20:30:09 +000051 /* carry = 0;
52 * if (r.all >= d.all)
53 * {
54 * r.all -= d.all;
55 * carry = 1;
56 * }
57 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000058 const si_int s = (si_int)(d - r - 1) >> (n_uword_bits - 1);
59 carry = s & 1;
60 r -= d & s;
61 }
62 q = (q << 1) | carry;
63 return q;
64}
Saleem Abdulrasool36ac5dd2017-05-16 16:41:37 +000065
66#if defined(__ARM_EABI__)
Eli Friedman0d586d02017-10-03 21:25:07 +000067AEABI_RTABI su_int __aeabi_uidiv(su_int n, su_int d) COMPILER_RT_ALIAS(__udivsi3);
Saleem Abdulrasool36ac5dd2017-05-16 16:41:37 +000068#endif