[ASan] Remove an unused ChunkBase field
[blocksruntime.git] / lib / udivmodti4.c
blobf619c74955e44a76fae01584cdb15456b1be3f06
1 /* ===-- udivmodti4.c - Implement __udivmodti4 -----------------------------===
3 * The LLVM Compiler Infrastructure
5 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
8 * ===----------------------------------------------------------------------===
10 * This file implements __udivmodti4 for the compiler_rt library.
12 * ===----------------------------------------------------------------------===
13 */
15 #include "int_lib.h"
17 #if __x86_64
19 /* Effects: if rem != 0, *rem = a % b
20 * Returns: a / b
23 /* Translated from Figure 3-40 of The PowerPC Compiler Writer's Guide */
25 tu_int
26 __udivmodti4(tu_int a, tu_int b, tu_int* rem)
28 const unsigned n_udword_bits = sizeof(du_int) * CHAR_BIT;
29 const unsigned n_utword_bits = sizeof(tu_int) * CHAR_BIT;
30 utwords n;
31 n.all = a;
32 utwords d;
33 d.all = b;
34 utwords q;
35 utwords r;
36 unsigned sr;
37 /* special cases, X is unknown, K != 0 */
38 if (n.s.high == 0)
40 if (d.s.high == 0)
42 /* 0 X
43 * ---
44 * 0 X
46 if (rem)
47 *rem = n.s.low % d.s.low;
48 return n.s.low / d.s.low;
50 /* 0 X
51 * ---
52 * K X
54 if (rem)
55 *rem = n.s.low;
56 return 0;
58 /* n.s.high != 0 */
59 if (d.s.low == 0)
61 if (d.s.high == 0)
63 /* K X
64 * ---
65 * 0 0
67 if (rem)
68 *rem = n.s.high % d.s.low;
69 return n.s.high / d.s.low;
71 /* d.s.high != 0 */
72 if (n.s.low == 0)
74 /* K 0
75 * ---
76 * K 0
78 if (rem)
80 r.s.high = n.s.high % d.s.high;
81 r.s.low = 0;
82 *rem = r.all;
84 return n.s.high / d.s.high;
86 /* K K
87 * ---
88 * K 0
90 if ((d.s.high & (d.s.high - 1)) == 0) /* if d is a power of 2 */
92 if (rem)
94 r.s.low = n.s.low;
95 r.s.high = n.s.high & (d.s.high - 1);
96 *rem = r.all;
98 return n.s.high >> __builtin_ctzll(d.s.high);
100 /* K K
101 * ---
102 * K 0
104 sr = __builtin_clzll(d.s.high) - __builtin_clzll(n.s.high);
105 /* 0 <= sr <= n_udword_bits - 2 or sr large */
106 if (sr > n_udword_bits - 2)
108 if (rem)
109 *rem = n.all;
110 return 0;
112 ++sr;
113 /* 1 <= sr <= n_udword_bits - 1 */
114 /* q.all = n.all << (n_utword_bits - sr); */
115 q.s.low = 0;
116 q.s.high = n.s.low << (n_udword_bits - sr);
117 /* r.all = n.all >> sr; */
118 r.s.high = n.s.high >> sr;
119 r.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr);
121 else /* d.s.low != 0 */
123 if (d.s.high == 0)
125 /* K X
126 * ---
127 * 0 K
129 if ((d.s.low & (d.s.low - 1)) == 0) /* if d is a power of 2 */
131 if (rem)
132 *rem = n.s.low & (d.s.low - 1);
133 if (d.s.low == 1)
134 return n.all;
135 sr = __builtin_ctzll(d.s.low);
136 q.s.high = n.s.high >> sr;
137 q.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr);
138 return q.all;
140 /* K X
141 * ---
142 * 0 K
144 sr = 1 + n_udword_bits + __builtin_clzll(d.s.low)
145 - __builtin_clzll(n.s.high);
146 /* 2 <= sr <= n_utword_bits - 1
147 * q.all = n.all << (n_utword_bits - sr);
148 * r.all = n.all >> sr;
149 * if (sr == n_udword_bits)
151 * q.s.low = 0;
152 * q.s.high = n.s.low;
153 * r.s.high = 0;
154 * r.s.low = n.s.high;
156 * else if (sr < n_udword_bits) // 2 <= sr <= n_udword_bits - 1
158 * q.s.low = 0;
159 * q.s.high = n.s.low << (n_udword_bits - sr);
160 * r.s.high = n.s.high >> sr;
161 * r.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr);
163 * else // n_udword_bits + 1 <= sr <= n_utword_bits - 1
165 * q.s.low = n.s.low << (n_utword_bits - sr);
166 * q.s.high = (n.s.high << (n_utword_bits - sr)) |
167 * (n.s.low >> (sr - n_udword_bits));
168 * r.s.high = 0;
169 * r.s.low = n.s.high >> (sr - n_udword_bits);
172 q.s.low = (n.s.low << (n_utword_bits - sr)) &
173 ((di_int)(int)(n_udword_bits - sr) >> (n_udword_bits-1));
174 q.s.high = ((n.s.low << ( n_udword_bits - sr)) &
175 ((di_int)(int)(sr - n_udword_bits - 1) >> (n_udword_bits-1))) |
176 (((n.s.high << (n_utword_bits - sr)) |
177 (n.s.low >> (sr - n_udword_bits))) &
178 ((di_int)(int)(n_udword_bits - sr) >> (n_udword_bits-1)));
179 r.s.high = (n.s.high >> sr) &
180 ((di_int)(int)(sr - n_udword_bits) >> (n_udword_bits-1));
181 r.s.low = ((n.s.high >> (sr - n_udword_bits)) &
182 ((di_int)(int)(n_udword_bits - sr - 1) >> (n_udword_bits-1))) |
183 (((n.s.high << (n_udword_bits - sr)) |
184 (n.s.low >> sr)) &
185 ((di_int)(int)(sr - n_udword_bits) >> (n_udword_bits-1)));
187 else
189 /* K X
190 * ---
191 * K K
193 sr = __builtin_clzll(d.s.high) - __builtin_clzll(n.s.high);
194 /*0 <= sr <= n_udword_bits - 1 or sr large */
195 if (sr > n_udword_bits - 1)
197 if (rem)
198 *rem = n.all;
199 return 0;
201 ++sr;
202 /* 1 <= sr <= n_udword_bits */
203 /* q.all = n.all << (n_utword_bits - sr); */
204 q.s.low = 0;
205 q.s.high = n.s.low << (n_udword_bits - sr);
206 /* r.all = n.all >> sr;
207 * if (sr < n_udword_bits)
209 * r.s.high = n.s.high >> sr;
210 * r.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr);
212 * else
214 * r.s.high = 0;
215 * r.s.low = n.s.high;
218 r.s.high = (n.s.high >> sr) &
219 ((di_int)(int)(sr - n_udword_bits) >> (n_udword_bits-1));
220 r.s.low = (n.s.high << (n_udword_bits - sr)) |
221 ((n.s.low >> sr) &
222 ((di_int)(int)(sr - n_udword_bits) >> (n_udword_bits-1)));
225 /* Not a special case
226 * q and r are initialized with:
227 * q.all = n.all << (n_utword_bits - sr);
228 * r.all = n.all >> sr;
229 * 1 <= sr <= n_utword_bits - 1
231 su_int carry = 0;
232 for (; sr > 0; --sr)
234 /* r:q = ((r:q) << 1) | carry */
235 r.s.high = (r.s.high << 1) | (r.s.low >> (n_udword_bits - 1));
236 r.s.low = (r.s.low << 1) | (q.s.high >> (n_udword_bits - 1));
237 q.s.high = (q.s.high << 1) | (q.s.low >> (n_udword_bits - 1));
238 q.s.low = (q.s.low << 1) | carry;
239 /* carry = 0;
240 * if (r.all >= d.all)
242 * r.all -= d.all;
243 * carry = 1;
246 const ti_int s = (ti_int)(d.all - r.all - 1) >> (n_utword_bits - 1);
247 carry = s & 1;
248 r.all -= d.all & s;
250 q.all = (q.all << 1) | carry;
251 if (rem)
252 *rem = r.all;
253 return q.all;
256 #endif /* __x86_64 */