blob: dcf186df111b80e83a4e18ff15c8a320f444988d [file] [log] [blame]
Edward O'Callaghan37a6a452009-08-07 20:30:09 +00001/* ===-- udivmoddi4.c - Implement __udivmoddi4 -----------------------------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
Howard Hinnant9ad441f2010-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'Callaghan37a6a452009-08-07 20:30:09 +00007 *
8 * ===----------------------------------------------------------------------===
9 *
10 * This file implements __udivmoddi4 for the compiler_rt library.
11 *
12 * ===----------------------------------------------------------------------===
13 */
Anton Korobeynikov1c5f89b2011-04-19 17:52:09 +000014#include "abi.h"
Daniel Dunbarb3a69012009-06-26 16:47:03 +000015
16#include "int_lib.h"
17
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000018/* Effects: if rem != 0, *rem = a % b
19 * Returns: a / b
20 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000021
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000022/* Translated from Figure 3-40 of The PowerPC Compiler Writer's Guide */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000023
Anton Korobeynikov37b97d12011-04-19 17:51:24 +000024ARM_EABI_FNALIAS(uldivmod, udivmoddi4);
25
Anton Korobeynikov1c5f89b2011-04-19 17:52:09 +000026COMPILER_RT_ABI du_int
Daniel Dunbarb3a69012009-06-26 16:47:03 +000027__udivmoddi4(du_int a, du_int b, du_int* rem)
28{
29 const unsigned n_uword_bits = sizeof(su_int) * CHAR_BIT;
30 const unsigned n_udword_bits = sizeof(du_int) * CHAR_BIT;
31 udwords n;
32 n.all = a;
33 udwords d;
34 d.all = b;
35 udwords q;
36 udwords r;
37 unsigned sr;
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000038 /* special cases, X is unknown, K != 0 */
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000039 if (n.s.high == 0)
Daniel Dunbarb3a69012009-06-26 16:47:03 +000040 {
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000041 if (d.s.high == 0)
Daniel Dunbarb3a69012009-06-26 16:47:03 +000042 {
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000043 /* 0 X
44 * ---
45 * 0 X
46 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000047 if (rem)
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000048 *rem = n.s.low % d.s.low;
49 return n.s.low / d.s.low;
Daniel Dunbarb3a69012009-06-26 16:47:03 +000050 }
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000051 /* 0 X
52 * ---
53 * K X
54 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000055 if (rem)
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000056 *rem = n.s.low;
Daniel Dunbarb3a69012009-06-26 16:47:03 +000057 return 0;
58 }
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000059 /* n.s.high != 0 */
60 if (d.s.low == 0)
Daniel Dunbarb3a69012009-06-26 16:47:03 +000061 {
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000062 if (d.s.high == 0)
Daniel Dunbarb3a69012009-06-26 16:47:03 +000063 {
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000064 /* K X
65 * ---
66 * 0 0
67 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000068 if (rem)
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000069 *rem = n.s.high % d.s.low;
70 return n.s.high / d.s.low;
Daniel Dunbarb3a69012009-06-26 16:47:03 +000071 }
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000072 /* d.s.high != 0 */
73 if (n.s.low == 0)
Daniel Dunbarb3a69012009-06-26 16:47:03 +000074 {
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000075 /* K 0
76 * ---
77 * K 0
78 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000079 if (rem)
80 {
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000081 r.s.high = n.s.high % d.s.high;
82 r.s.low = 0;
Daniel Dunbarb3a69012009-06-26 16:47:03 +000083 *rem = r.all;
84 }
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000085 return n.s.high / d.s.high;
Daniel Dunbarb3a69012009-06-26 16:47:03 +000086 }
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000087 /* K K
88 * ---
89 * K 0
90 */
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000091 if ((d.s.high & (d.s.high - 1)) == 0) /* if d is a power of 2 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000092 {
93 if (rem)
94 {
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000095 r.s.low = n.s.low;
96 r.s.high = n.s.high & (d.s.high - 1);
Daniel Dunbarb3a69012009-06-26 16:47:03 +000097 *rem = r.all;
98 }
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +000099 return n.s.high >> __builtin_ctz(d.s.high);
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000100 }
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000101 /* K K
102 * ---
103 * K 0
104 */
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000105 sr = __builtin_clz(d.s.high) - __builtin_clz(n.s.high);
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000106 /* 0 <= sr <= n_uword_bits - 2 or sr large */
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000107 if (sr > n_uword_bits - 2)
108 {
109 if (rem)
110 *rem = n.all;
111 return 0;
112 }
113 ++sr;
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000114 /* 1 <= sr <= n_uword_bits - 1 */
115 /* q.all = n.all << (n_udword_bits - sr); */
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000116 q.s.low = 0;
117 q.s.high = n.s.low << (n_uword_bits - sr);
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000118 /* r.all = n.all >> sr; */
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000119 r.s.high = n.s.high >> sr;
120 r.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr);
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000121 }
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000122 else /* d.s.low != 0 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000123 {
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000124 if (d.s.high == 0)
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000125 {
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000126 /* K X
127 * ---
128 * 0 K
129 */
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000130 if ((d.s.low & (d.s.low - 1)) == 0) /* if d is a power of 2 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000131 {
132 if (rem)
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000133 *rem = n.s.low & (d.s.low - 1);
134 if (d.s.low == 1)
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000135 return n.all;
Joerg Sonnenbergerd4260912011-07-28 19:47:33 +0000136 sr = __builtin_ctz(d.s.low);
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000137 q.s.high = n.s.high >> sr;
138 q.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr);
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000139 return q.all;
140 }
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000141 /* K X
142 * ---
143 *0 K
144 */
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000145 sr = 1 + n_uword_bits + __builtin_clz(d.s.low) - __builtin_clz(n.s.high);
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000146 /* 2 <= sr <= n_udword_bits - 1
147 * q.all = n.all << (n_udword_bits - sr);
148 * r.all = n.all >> sr;
149 * if (sr == n_uword_bits)
150 * {
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000151 * q.s.low = 0;
152 * q.s.high = n.s.low;
153 * r.s.high = 0;
154 * r.s.low = n.s.high;
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000155 * }
156 * else if (sr < n_uword_bits) // 2 <= sr <= n_uword_bits - 1
157 * {
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000158 * q.s.low = 0;
159 * q.s.high = n.s.low << (n_uword_bits - sr);
160 * r.s.high = n.s.high >> sr;
161 * r.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr);
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000162 * }
163 * else // n_uword_bits + 1 <= sr <= n_udword_bits - 1
164 * {
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000165 * q.s.low = n.s.low << (n_udword_bits - sr);
166 * q.s.high = (n.s.high << (n_udword_bits - sr)) |
167 * (n.s.low >> (sr - n_uword_bits));
168 * r.s.high = 0;
169 * r.s.low = n.s.high >> (sr - n_uword_bits);
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000170 * }
171 */
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000172 q.s.low = (n.s.low << (n_udword_bits - sr)) &
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000173 ((si_int)(n_uword_bits - sr) >> (n_uword_bits-1));
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000174 q.s.high = ((n.s.low << ( n_uword_bits - sr)) &
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000175 ((si_int)(sr - n_uword_bits - 1) >> (n_uword_bits-1))) |
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000176 (((n.s.high << (n_udword_bits - sr)) |
177 (n.s.low >> (sr - n_uword_bits))) &
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000178 ((si_int)(n_uword_bits - sr) >> (n_uword_bits-1)));
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000179 r.s.high = (n.s.high >> sr) &
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000180 ((si_int)(sr - n_uword_bits) >> (n_uword_bits-1));
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000181 r.s.low = ((n.s.high >> (sr - n_uword_bits)) &
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000182 ((si_int)(n_uword_bits - sr - 1) >> (n_uword_bits-1))) |
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000183 (((n.s.high << (n_uword_bits - sr)) |
184 (n.s.low >> sr)) &
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000185 ((si_int)(sr - n_uword_bits) >> (n_uword_bits-1)));
186 }
187 else
188 {
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000189 /* K X
190 * ---
191 * K K
192 */
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000193 sr = __builtin_clz(d.s.high) - __builtin_clz(n.s.high);
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000194 /* 0 <= sr <= n_uword_bits - 1 or sr large */
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000195 if (sr > n_uword_bits - 1)
196 {
197 if (rem)
198 *rem = n.all;
199 return 0;
200 }
201 ++sr;
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000202 /* 1 <= sr <= n_uword_bits */
203 /* q.all = n.all << (n_udword_bits - sr); */
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000204 q.s.low = 0;
205 q.s.high = n.s.low << (n_uword_bits - sr);
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000206 /* r.all = n.all >> sr;
207 * if (sr < n_uword_bits)
208 * {
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000209 * r.s.high = n.s.high >> sr;
210 * r.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr);
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000211 * }
212 * else
213 * {
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000214 * r.s.high = 0;
215 * r.s.low = n.s.high;
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000216 * }
217 */
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000218 r.s.high = (n.s.high >> sr) &
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000219 ((si_int)(sr - n_uword_bits) >> (n_uword_bits-1));
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000220 r.s.low = (n.s.high << (n_uword_bits - sr)) |
221 ((n.s.low >> sr) &
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000222 ((si_int)(sr - n_uword_bits) >> (n_uword_bits-1)));
223 }
224 }
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000225 /* Not a special case
226 * q and r are initialized with:
227 * q.all = n.all << (n_udword_bits - sr);
228 * r.all = n.all >> sr;
229 * 1 <= sr <= n_udword_bits - 1
230 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000231 su_int carry = 0;
232 for (; sr > 0; --sr)
233 {
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000234 /* r:q = ((r:q) << 1) | carry */
Edward O'Callaghan8bf1e092009-08-09 18:41:02 +0000235 r.s.high = (r.s.high << 1) | (r.s.low >> (n_uword_bits - 1));
236 r.s.low = (r.s.low << 1) | (q.s.high >> (n_uword_bits - 1));
237 q.s.high = (q.s.high << 1) | (q.s.low >> (n_uword_bits - 1));
238 q.s.low = (q.s.low << 1) | carry;
Edward O'Callaghan37a6a452009-08-07 20:30:09 +0000239 /* carry = 0;
240 * if (r.all >= d.all)
241 * {
242 * r.all -= d.all;
243 * carry = 1;
244 * }
245 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +0000246 const di_int s = (di_int)(d.all - r.all - 1) >> (n_udword_bits - 1);
247 carry = s & 1;
248 r.all -= d.all & s;
249 }
250 q.all = (q.all << 1) | carry;
251 if (rem)
252 *rem = r.all;
253 return q.all;
254}