2 * This file is dual licensed under the MIT and the University of Illinois
3 * Open Source Licenses.
6 * University of Illinois/NCSA
9 * Copyright (c) 2009-2014 by the contributors listed below
11 * All rights reserved.
17 * University of Illinois at Urbana-Champaign
21 * Permission is hereby granted, free of charge, to any person obtaining a
22 * copy of this software and associated documentation files (the "Software"),
23 * to deal with the Software without restriction, including without limitation
24 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
25 * and/or sell copies of the Software, and to permit persons to whom the
26 * Software is furnished to do so, subject to the following conditions:
28 * * Redistributions of source code must retain the above copyright notice,
29 * this list of conditions and the following disclaimers.
31 * * Redistributions in binary form must reproduce the above copyright notice,
32 * this list of conditions and the following disclaimers in the
33 * documentation and/or other materials provided with the distribution.
35 * * Neither the names of the LLVM Team, University of Illinois at
36 * Urbana-Champaign, nor the names of its contributors may be used to
37 * endorse or promote products derived from this Software without specific
38 * prior written permission.
40 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
41 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
42 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
43 * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
44 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
45 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
49 * Copyright (c) 2009-2014 by the contributors listed below
51 * Permission is hereby granted, free of charge, to any person obtaining a copy
52 * of this software and associated documentation files (the "Software"), to
53 * deal in the Software without restriction, including without limitation the
54 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
55 * sell copies of the Software, and to permit persons to whom the Software is
56 * furnished to do so, subject to the following conditions:
58 * The above copyright notice and this permission notice shall be included in
59 * all copies or substantial portions of the Software.
61 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
62 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
63 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
64 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
65 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
66 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
67 * DEALINGS IN THE SOFTWARE.
70 * List of contributors:
72 * Craig van Vliet <cvanvliet@auroraux.org>
73 * Edward O'Callaghan <eocallaghan@auroraux.org>
74 * Howard Hinnant <hhinnant@apple.com>
75 * Matt Thomas <matt@NetBSD.org>
76 * Joerg Sonnenberger <joerg@NetBSD.org>
79 #include <machine/limits.h>
80 #include <machine/stdint.h>
91 /* Effects: if rem != 0, *rem = a % b
95 /* Translated from Figure 3-40 of The PowerPC Compiler Writer's Guide */
98 __udivmodti4(_uint128_t a, _uint128_t b, _uint128_t *rem)
100 const unsigned n_udword_bits = sizeof(unsigned long) * CHAR_BIT;
101 const unsigned n_utword_bits = sizeof(_uint128_t) * CHAR_BIT;
108 /* special cases, X is unknown, K != 0 */
116 *rem = n.s.low % d.s.low;
117 return n.s.low / d.s.low;
135 *rem = n.s.high % d.s.low;
136 return n.s.high / d.s.low;
145 r.s.high = n.s.high % d.s.high;
149 return n.s.high / d.s.high;
155 if ((d.s.high & (d.s.high - 1)) == 0) { /* if d is a power of 2 */
158 r.s.high = n.s.high & (d.s.high - 1);
161 return n.s.high >> __builtin_ctzl(d.s.high);
167 sr = __builtin_clzl(d.s.high) - __builtin_clzl(n.s.high);
168 /* 0 <= sr <= n_udword_bits - 2 or sr large */
169 if (sr > n_udword_bits - 2) {
175 /* 1 <= sr <= n_udword_bits - 1 */
176 /* q.all = n.all << (n_utword_bits - sr); */
178 q.s.high = n.s.low << (n_udword_bits - sr);
179 /* r.all = n.all >> sr; */
180 r.s.high = n.s.high >> sr;
181 r.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr);
182 } else { /* d.s.low != 0 */
188 if ((d.s.low & (d.s.low - 1)) == 0) { /* if d is a power of 2 */
190 *rem = n.s.low & (d.s.low - 1);
193 sr = __builtin_ctzl(d.s.low);
194 q.s.high = n.s.high >> sr;
195 q.s.low = (n.s.high << (n_udword_bits - sr)) |
203 sr = 1 + n_udword_bits + __builtin_clzl(d.s.low) -
204 __builtin_clzl(n.s.high);
205 /* 2 <= sr <= n_utword_bits - 1
206 * q.all = n.all << (n_utword_bits - sr);
207 * r.all = n.all >> sr;
208 * if (sr == n_udword_bits)
211 * q.s.high = n.s.low;
213 * r.s.low = n.s.high;
215 * else if (sr < n_udword_bits) // 2 <= sr <= n_udword_bits - 1
218 * q.s.high = n.s.low << (n_udword_bits - sr);
219 * r.s.high = n.s.high >> sr;
220 * r.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr);
222 * else // n_udword_bits + 1 <= sr <= n_utword_bits - 1
224 * q.s.low = n.s.low << (n_utword_bits - sr);
225 * q.s.high = (n.s.high << (n_utword_bits - sr)) |
226 * (n.s.low >> (sr - n_udword_bits));
228 * r.s.low = n.s.high >> (sr - n_udword_bits);
231 q.s.low = (n.s.low << (n_utword_bits - sr)) &
232 ((long)(int)(n_udword_bits - sr) >> (n_udword_bits-1));
233 q.s.high = ((n.s.low << ( n_udword_bits - sr)) &
234 ((long)(int)(sr - n_udword_bits - 1) >> (n_udword_bits-1))) |
235 (((n.s.high << (n_utword_bits - sr)) |
236 (n.s.low >> (sr - n_udword_bits))) &
237 ((long)(int)(n_udword_bits - sr) >> (n_udword_bits-1)));
238 r.s.high = (n.s.high >> sr) &
239 ((long)(int)(sr - n_udword_bits) >> (n_udword_bits-1));
240 r.s.low = ((n.s.high >> (sr - n_udword_bits)) &
241 ((long)(int)(n_udword_bits - sr - 1) >> (n_udword_bits-1))) |
242 (((n.s.high << (n_udword_bits - sr)) |
244 ((long)(int)(sr - n_udword_bits) >> (n_udword_bits-1)));
250 sr = __builtin_clzl(d.s.high) - __builtin_clzl(n.s.high);
251 /*0 <= sr <= n_udword_bits - 1 or sr large */
252 if (sr > n_udword_bits - 1) {
258 /* 1 <= sr <= n_udword_bits */
259 /* q.all = n.all << (n_utword_bits - sr); */
261 q.s.high = n.s.low << (n_udword_bits - sr);
262 /* r.all = n.all >> sr;
263 * if (sr < n_udword_bits)
265 * r.s.high = n.s.high >> sr;
266 * r.s.low = (n.s.high << (n_udword_bits - sr)) | (n.s.low >> sr);
271 * r.s.low = n.s.high;
274 r.s.high = (n.s.high >> sr) &
275 ((long)(int)(sr - n_udword_bits) >> (n_udword_bits-1));
276 r.s.low = (n.s.high << (n_udword_bits - sr)) |
278 ((long)(int)(sr - n_udword_bits) >> (n_udword_bits-1)));
281 /* Not a special case
282 * q and r are initialized with:
283 * q.all = n.all << (n_utword_bits - sr);
284 * r.all = n.all >> sr;
285 * 1 <= sr <= n_utword_bits - 1
288 for (; sr > 0; --sr) {
289 /* r:q = ((r:q) << 1) | carry */
290 r.s.high = (r.s.high << 1) | (r.s.low >> (n_udword_bits - 1));
291 r.s.low = (r.s.low << 1) | (q.s.high >> (n_udword_bits - 1));
292 q.s.high = (q.s.high << 1) | (q.s.low >> (n_udword_bits - 1));
293 q.s.low = (q.s.low << 1) | carry;
295 * if (r.all >= d.all)
302 (_int128_t)(d.all - r.all - 1) >> (n_utword_bits - 1);
306 q.all = (q.all << 1) | carry;
312 _uint128_t __udivti3(_uint128_t a, _uint128_t b);
315 __udivti3(_uint128_t a, _uint128_t b)
317 return __udivmodti4(a, b, 0);