blob: 0c8b4ff46474718231017223f3fe8ad7d778e26f [file] [log] [blame]
Edward O'Callaghan55836322009-08-07 20:30:09 +00001/* ===-- udivmoddi4.c - Implement __udivmoddi4 -----------------------------===
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 __udivmoddi4 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/* Effects: if rem != 0, *rem = a % b
18 * Returns: a / b
19 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000020
Edward O'Callaghan55836322009-08-07 20:30:09 +000021/* Translated from Figure 3-40 of The PowerPC Compiler Writer's Guide */
Daniel Dunbarfd089992009-06-26 16:47:03 +000022
Anton Korobeynikove63da932011-04-19 17:52:09 +000023COMPILER_RT_ABI du_int
Daniel Dunbarfd089992009-06-26 16:47:03 +000024__udivmoddi4(du_int a, du_int b, du_int* rem)
25{
26 const unsigned n_uword_bits = sizeof(su_int) * CHAR_BIT;
27 const unsigned n_udword_bits = sizeof(du_int) * CHAR_BIT;
28 udwords n;
29 n.all = a;
30 udwords d;
31 d.all = b;
32 udwords q;
33 udwords r;
34 unsigned sr;
Edward O'Callaghan55836322009-08-07 20:30:09 +000035 /* special cases, X is unknown, K != 0 */
Edward O'Callaghanccf48132009-08-09 18:41:02 +000036 if (n.s.high == 0)
Daniel Dunbarfd089992009-06-26 16:47:03 +000037 {
Edward O'Callaghanccf48132009-08-09 18:41:02 +000038 if (d.s.high == 0)
Daniel Dunbarfd089992009-06-26 16:47:03 +000039 {
Edward O'Callaghan55836322009-08-07 20:30:09 +000040 /* 0 X
41 * ---
42 * 0 X
43 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000044 if (rem)
Edward O'Callaghanccf48132009-08-09 18:41:02 +000045 *rem = n.s.low % d.s.low;
46 return n.s.low / d.s.low;
Daniel Dunbarfd089992009-06-26 16:47:03 +000047 }
Edward O'Callaghan55836322009-08-07 20:30:09 +000048 /* 0 X
49 * ---
50 * K X
51 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000052 if (rem)
Edward O'Callaghanccf48132009-08-09 18:41:02 +000053 *rem = n.s.low;
Daniel Dunbarfd089992009-06-26 16:47:03 +000054 return 0;
55 }
Edward O'Callaghanccf48132009-08-09 18:41:02 +000056 /* n.s.high != 0 */
57 if (d.s.low == 0)
Daniel Dunbarfd089992009-06-26 16:47:03 +000058 {
Edward O'Callaghanccf48132009-08-09 18:41:02 +000059 if (d.s.high == 0)
Daniel Dunbarfd089992009-06-26 16:47:03 +000060 {
Edward O'Callaghan55836322009-08-07 20:30:09 +000061 /* K X
62 * ---
63 * 0 0
64 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000065 if (rem)
Edward O'Callaghanccf48132009-08-09 18:41:02 +000066 *rem = n.s.high % d.s.low;
67 return n.s.high / d.s.low;
Daniel Dunbarfd089992009-06-26 16:47:03 +000068 }
Edward O'Callaghanccf48132009-08-09 18:41:02 +000069 /* d.s.high != 0 */
70 if (n.s.low == 0)
Daniel Dunbarfd089992009-06-26 16:47:03 +000071 {
Edward O'Callaghan55836322009-08-07 20:30:09 +000072 /* K 0
73 * ---
74 * K 0
75 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000076 if (rem)
77 {
Edward O'Callaghanccf48132009-08-09 18:41:02 +000078 r.s.high = n.s.high % d.s.high;
79 r.s.low = 0;
Daniel Dunbarfd089992009-06-26 16:47:03 +000080 *rem = r.all;
81 }
Edward O'Callaghanccf48132009-08-09 18:41:02 +000082 return n.s.high / d.s.high;
Daniel Dunbarfd089992009-06-26 16:47:03 +000083 }
Edward O'Callaghan55836322009-08-07 20:30:09 +000084 /* K K
85 * ---
86 * K 0
87 */
Edward O'Callaghanccf48132009-08-09 18:41:02 +000088 if ((d.s.high & (d.s.high - 1)) == 0) /* if d is a power of 2 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000089 {
90 if (rem)
91 {
Edward O'Callaghanccf48132009-08-09 18:41:02 +000092 r.s.low = n.s.low;
93 r.s.high = n.s.high & (d.s.high - 1);
Daniel Dunbarfd089992009-06-26 16:47:03 +000094 *rem = r.all;
95 }
Edward O'Callaghanccf48132009-08-09 18:41:02 +000096 return n.s.high >> __builtin_ctz(d.s.high);
Daniel Dunbarfd089992009-06-26 16:47:03 +000097 }
Edward O'Callaghan55836322009-08-07 20:30:09 +000098 /* K K
99 * ---
100 * K 0
101 */
Edward O'Callaghanccf48132009-08-09 18:41:02 +0000102 sr = __builtin_clz(d.s.high) - __builtin_clz(n.s.high);
Edward O'Callaghan55836322009-08-07 20:30:09 +0000103 /* 0 <= sr <= n_uword_bits - 2 or sr large */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000104 if (sr > n_uword_bits - 2)
105 {
106 if (rem)
107 *rem = n.all;
108 return 0;
109 }
110 ++sr;
Edward O'Callaghan55836322009-08-07 20:30:09 +0000111 /* 1 <= sr <= n_uword_bits - 1 */
112 /* q.all = n.all << (n_udword_bits - sr); */
Edward O'Callaghanccf48132009-08-09 18:41:02 +0000113 q.s.low = 0;
114 q.s.high = n.s.low << (n_uword_bits - sr);
Edward O'Callaghan55836322009-08-07 20:30:09 +0000115 /* r.all = n.all >> sr; */
Edward O'Callaghanccf48132009-08-09 18:41:02 +0000116 r.s.high = n.s.high >> sr;
117 r.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr);
Daniel Dunbarfd089992009-06-26 16:47:03 +0000118 }
Edward O'Callaghanccf48132009-08-09 18:41:02 +0000119 else /* d.s.low != 0 */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000120 {
Edward O'Callaghanccf48132009-08-09 18:41:02 +0000121 if (d.s.high == 0)
Daniel Dunbarfd089992009-06-26 16:47:03 +0000122 {
Edward O'Callaghan55836322009-08-07 20:30:09 +0000123 /* K X
124 * ---
125 * 0 K
126 */
Edward O'Callaghanccf48132009-08-09 18:41:02 +0000127 if ((d.s.low & (d.s.low - 1)) == 0) /* if d is a power of 2 */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000128 {
129 if (rem)
Edward O'Callaghanccf48132009-08-09 18:41:02 +0000130 *rem = n.s.low & (d.s.low - 1);
131 if (d.s.low == 1)
Daniel Dunbarfd089992009-06-26 16:47:03 +0000132 return n.all;
Joerg Sonnenbergercf2996d2011-07-28 19:47:33 +0000133 sr = __builtin_ctz(d.s.low);
Edward O'Callaghanccf48132009-08-09 18:41:02 +0000134 q.s.high = n.s.high >> sr;
135 q.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr);
Daniel Dunbarfd089992009-06-26 16:47:03 +0000136 return q.all;
137 }
Edward O'Callaghan55836322009-08-07 20:30:09 +0000138 /* K X
139 * ---
Joerg Sonnenbergerbbc979b2014-03-18 20:41:31 +0000140 * 0 K
Edward O'Callaghan55836322009-08-07 20:30:09 +0000141 */
Edward O'Callaghanccf48132009-08-09 18:41:02 +0000142 sr = 1 + n_uword_bits + __builtin_clz(d.s.low) - __builtin_clz(n.s.high);
Edward O'Callaghan55836322009-08-07 20:30:09 +0000143 /* 2 <= sr <= n_udword_bits - 1
144 * q.all = n.all << (n_udword_bits - sr);
145 * r.all = n.all >> sr;
Edward O'Callaghan55836322009-08-07 20:30:09 +0000146 */
Joerg Sonnenberger4e08a632014-03-18 22:10:36 +0000147 if (sr == n_uword_bits)
148 {
149 q.s.low = 0;
150 q.s.high = n.s.low;
151 r.s.high = 0;
152 r.s.low = n.s.high;
153 }
154 else if (sr < n_uword_bits) // 2 <= sr <= n_uword_bits - 1
155 {
156 q.s.low = 0;
157 q.s.high = n.s.low << (n_uword_bits - sr);
158 r.s.high = n.s.high >> sr;
159 r.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr);
160 }
161 else // n_uword_bits + 1 <= sr <= n_udword_bits - 1
162 {
163 q.s.low = n.s.low << (n_udword_bits - sr);
164 q.s.high = (n.s.high << (n_udword_bits - sr)) |
165 (n.s.low >> (sr - n_uword_bits));
166 r.s.high = 0;
167 r.s.low = n.s.high >> (sr - n_uword_bits);
168 }
Daniel Dunbarfd089992009-06-26 16:47:03 +0000169 }
170 else
171 {
Edward O'Callaghan55836322009-08-07 20:30:09 +0000172 /* K X
173 * ---
174 * K K
175 */
Edward O'Callaghanccf48132009-08-09 18:41:02 +0000176 sr = __builtin_clz(d.s.high) - __builtin_clz(n.s.high);
Edward O'Callaghan55836322009-08-07 20:30:09 +0000177 /* 0 <= sr <= n_uword_bits - 1 or sr large */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000178 if (sr > n_uword_bits - 1)
179 {
Joerg Sonnenberger4e08a632014-03-18 22:10:36 +0000180 if (rem)
Daniel Dunbarfd089992009-06-26 16:47:03 +0000181 *rem = n.all;
182 return 0;
183 }
184 ++sr;
Edward O'Callaghan55836322009-08-07 20:30:09 +0000185 /* 1 <= sr <= n_uword_bits */
186 /* q.all = n.all << (n_udword_bits - sr); */
Edward O'Callaghanccf48132009-08-09 18:41:02 +0000187 q.s.low = 0;
Joerg Sonnenberger4e08a632014-03-18 22:10:36 +0000188 if (sr == n_uword_bits)
189 {
190 q.s.high = n.s.low;
191 r.s.high = 0;
192 r.s.low = n.s.high;
193 }
194 else
195 {
196 q.s.high = n.s.low << (n_uword_bits - sr);
197 r.s.high = n.s.high >> sr;
198 r.s.low = (n.s.high << (n_uword_bits - sr)) | (n.s.low >> sr);
199 }
Daniel Dunbarfd089992009-06-26 16:47:03 +0000200 }
201 }
Edward O'Callaghan55836322009-08-07 20:30:09 +0000202 /* Not a special case
203 * q and r are initialized with:
204 * q.all = n.all << (n_udword_bits - sr);
205 * r.all = n.all >> sr;
206 * 1 <= sr <= n_udword_bits - 1
207 */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000208 su_int carry = 0;
209 for (; sr > 0; --sr)
210 {
Edward O'Callaghan55836322009-08-07 20:30:09 +0000211 /* r:q = ((r:q) << 1) | carry */
Edward O'Callaghanccf48132009-08-09 18:41:02 +0000212 r.s.high = (r.s.high << 1) | (r.s.low >> (n_uword_bits - 1));
213 r.s.low = (r.s.low << 1) | (q.s.high >> (n_uword_bits - 1));
214 q.s.high = (q.s.high << 1) | (q.s.low >> (n_uword_bits - 1));
215 q.s.low = (q.s.low << 1) | carry;
Edward O'Callaghan55836322009-08-07 20:30:09 +0000216 /* carry = 0;
217 * if (r.all >= d.all)
218 * {
219 * r.all -= d.all;
220 * carry = 1;
221 * }
222 */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000223 const di_int s = (di_int)(d.all - r.all - 1) >> (n_udword_bits - 1);
224 carry = s & 1;
225 r.all -= d.all & s;
226 }
227 q.all = (q.all << 1) | carry;
228 if (rem)
229 *rem = r.all;
230 return q.all;
231}