]>
Commit | Line | Data |
---|---|---|
1e57a462 | 1 | /** @file\r |
2 | Compiler intrinsic for 64-bit compare, ported from LLVM code.\r | |
3 | \r | |
4 | Copyright (c) 2008-2009, Apple Inc. All rights reserved.<BR>\r | |
5 | \r | |
6 | This program and the accompanying materials\r | |
7 | are licensed and made available under the terms and conditions of the BSD License\r | |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | /**\r | |
16 | University of Illinois/NCSA\r | |
17 | Open Source License\r | |
18 | \r | |
19 | Copyright (c) 2003-2008 University of Illinois at Urbana-Champaign.\r | |
20 | All rights reserved.\r | |
21 | \r | |
22 | Developed by:\r | |
23 | \r | |
24 | LLVM Team\r | |
25 | \r | |
26 | University of Illinois at Urbana-Champaign\r | |
27 | \r | |
28 | http://llvm.org\r | |
29 | \r | |
30 | Permission is hereby granted, free of charge, to any person obtaining a copy of\r | |
31 | this software and associated documentation files (the "Software"), to deal with\r | |
32 | the Software without restriction, including without limitation the rights to\r | |
33 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies\r | |
34 | of the Software, and to permit persons to whom the Software is furnished to do\r | |
35 | so, subject to the following conditions:\r | |
36 | \r | |
37 | * Redistributions of source code must retain the above copyright notice,\r | |
38 | this list of conditions and the following disclaimers.\r | |
39 | \r | |
40 | * Redistributions in binary form must reproduce the above copyright notice,\r | |
41 | this list of conditions and the following disclaimers in the\r | |
42 | documentation and/or other materials provided with the distribution.\r | |
43 | \r | |
44 | * Neither the names of the LLVM Team, University of Illinois at\r | |
45 | Urbana-Champaign, nor the names of its contributors may be used to\r | |
46 | endorse or promote products derived from this Software without specific\r | |
47 | prior written permission.\r | |
48 | \r | |
49 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r | |
50 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r | |
51 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r | |
52 | CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r | |
53 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r | |
54 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE\r | |
55 | SOFTWARE.\r | |
56 | **/\r | |
57 | \r | |
58 | \r | |
59 | #include "Llvm_int_lib.h"\r | |
60 | \r | |
61 | // Effects: if rem != 0, *rem = a % b\r | |
62 | // Returns: a / b\r | |
63 | \r | |
64 | // Translated from Figure 3-40 of The PowerPC Compiler Writer's Guide\r | |
65 | \r | |
66 | UINT64\r | |
67 | __udivmoddi4 (UINT64 a, UINT64 b, UINT64* rem)\r | |
68 | {\r | |
69 | const unsigned n_uword_bits = sizeof(UINT32) * CHAR_BIT;\r | |
70 | const unsigned n_udword_bits = sizeof(UINT64) * CHAR_BIT;\r | |
71 | udwords n;\r | |
72 | n.all = a;\r | |
73 | udwords d;\r | |
74 | d.all = b;\r | |
75 | udwords q;\r | |
76 | udwords r;\r | |
77 | unsigned sr;\r | |
78 | \r | |
79 | if (b == 0) {\r | |
80 | // ASSERT (FALSE);\r | |
81 | return 0;\r | |
82 | }\r | |
83 | \r | |
84 | // special cases, X is unknown, K != 0\r | |
85 | if (n.high == 0)\r | |
86 | {\r | |
87 | if (d.high == 0)\r | |
88 | {\r | |
89 | // 0 X\r | |
90 | // ---\r | |
91 | // 0 X\r | |
92 | if (rem)\r | |
93 | *rem = n.low % d.low;\r | |
94 | return n.low / d.low;\r | |
95 | }\r | |
96 | // 0 X\r | |
97 | // ---\r | |
98 | // K X\r | |
99 | if (rem)\r | |
100 | *rem = n.low;\r | |
101 | return 0;\r | |
102 | }\r | |
103 | // n.high != 0\r | |
104 | if (d.low == 0)\r | |
105 | {\r | |
106 | if (d.high == 0)\r | |
107 | {\r | |
108 | // K X\r | |
109 | // ---\r | |
110 | // 0 0\r | |
111 | if (rem)\r | |
112 | *rem = n.high % d.low;\r | |
113 | return n.high / d.low;\r | |
114 | }\r | |
115 | // d.high != 0\r | |
116 | if (n.low == 0)\r | |
117 | {\r | |
118 | // K 0\r | |
119 | // ---\r | |
120 | // K 0\r | |
121 | if (rem)\r | |
122 | {\r | |
123 | r.high = n.high % d.high;\r | |
124 | r.low = 0;\r | |
125 | *rem = r.all;\r | |
126 | }\r | |
127 | return n.high / d.high;\r | |
128 | }\r | |
129 | // K K\r | |
130 | // ---\r | |
131 | // K 0\r | |
132 | if ((d.high & (d.high - 1)) == 0) // if d is a power of 2\r | |
133 | {\r | |
134 | if (rem)\r | |
135 | {\r | |
136 | r.low = n.low;\r | |
137 | r.high = n.high & (d.high - 1);\r | |
138 | *rem = r.all;\r | |
139 | }\r | |
140 | return n.high >> COUNT_TRAILING_ZEROS(d.high);\r | |
141 | }\r | |
142 | // K K\r | |
143 | // ---\r | |
144 | // K 0\r | |
145 | sr = COUNT_LEADING_ZEROS(d.high) - COUNT_LEADING_ZEROS(n.high);\r | |
146 | // 0 <= sr <= n_uword_bits - 2 or sr large\r | |
147 | if (sr > n_uword_bits - 2)\r | |
148 | {\r | |
149 | if (rem)\r | |
150 | *rem = n.all;\r | |
151 | return 0;\r | |
152 | }\r | |
153 | ++sr;\r | |
154 | // 1 <= sr <= n_uword_bits - 1\r | |
155 | // q.all = n.all << (n_udword_bits - sr);\r | |
156 | q.low = 0;\r | |
157 | q.high = n.low << (n_uword_bits - sr);\r | |
158 | // r.all = n.all >> sr;\r | |
159 | r.high = n.high >> sr;\r | |
160 | r.low = (n.high << (n_uword_bits - sr)) | (n.low >> sr);\r | |
161 | }\r | |
162 | else // d.low != 0\r | |
163 | {\r | |
164 | if (d.high == 0)\r | |
165 | {\r | |
166 | // K X\r | |
167 | // ---\r | |
168 | // 0 K\r | |
169 | if ((d.low & (d.low - 1)) == 0) // if d is a power of 2\r | |
170 | {\r | |
171 | if (rem)\r | |
172 | *rem = n.low & (d.low - 1);\r | |
173 | if (d.low == 1)\r | |
174 | return n.all;\r | |
175 | unsigned sr = COUNT_TRAILING_ZEROS(d.low);\r | |
176 | q.high = n.high >> sr;\r | |
177 | q.low = (n.high << (n_uword_bits - sr)) | (n.low >> sr);\r | |
178 | return q.all;\r | |
179 | }\r | |
180 | // K X\r | |
181 | // ---\r | |
182 | // 0 K\r | |
183 | sr = 1 + n_uword_bits + COUNT_LEADING_ZEROS(d.low) - COUNT_LEADING_ZEROS(n.high);\r | |
184 | // 2 <= sr <= n_udword_bits - 1\r | |
185 | // q.all = n.all << (n_udword_bits - sr);\r | |
186 | // r.all = n.all >> sr;\r | |
187 | // if (sr == n_uword_bits)\r | |
188 | // {\r | |
189 | // q.low = 0;\r | |
190 | // q.high = n.low;\r | |
191 | // r.high = 0;\r | |
192 | // r.low = n.high;\r | |
193 | // }\r | |
194 | // else if (sr < n_uword_bits) // 2 <= sr <= n_uword_bits - 1\r | |
195 | // {\r | |
196 | // q.low = 0;\r | |
197 | // q.high = n.low << (n_uword_bits - sr);\r | |
198 | // r.high = n.high >> sr;\r | |
199 | // r.low = (n.high << (n_uword_bits - sr)) | (n.low >> sr);\r | |
200 | // }\r | |
201 | // else // n_uword_bits + 1 <= sr <= n_udword_bits - 1\r | |
202 | // {\r | |
203 | // q.low = n.low << (n_udword_bits - sr);\r | |
204 | // q.high = (n.high << (n_udword_bits - sr)) |\r | |
205 | // (n.low >> (sr - n_uword_bits));\r | |
206 | // r.high = 0;\r | |
207 | // r.low = n.high >> (sr - n_uword_bits);\r | |
208 | // }\r | |
209 | q.low = (n.low << (n_udword_bits - sr)) &\r | |
210 | ((INT32)(n_uword_bits - sr) >> (n_uword_bits-1));\r | |
211 | q.high = ((n.low << ( n_uword_bits - sr)) &\r | |
212 | ((INT32)(sr - n_uword_bits - 1) >> (n_uword_bits-1))) |\r | |
213 | (((n.high << (n_udword_bits - sr)) |\r | |
214 | (n.low >> (sr - n_uword_bits))) &\r | |
215 | ((INT32)(n_uword_bits - sr) >> (n_uword_bits-1)));\r | |
216 | r.high = (n.high >> sr) &\r | |
217 | ((INT32)(sr - n_uword_bits) >> (n_uword_bits-1));\r | |
218 | r.low = ((n.high >> (sr - n_uword_bits)) &\r | |
219 | ((INT32)(n_uword_bits - sr - 1) >> (n_uword_bits-1))) |\r | |
220 | (((n.high << (n_uword_bits - sr)) |\r | |
221 | (n.low >> sr)) &\r | |
222 | ((INT32)(sr - n_uword_bits) >> (n_uword_bits-1)));\r | |
223 | }\r | |
224 | else\r | |
225 | {\r | |
226 | // K X\r | |
227 | // ---\r | |
228 | // K K\r | |
229 | sr = COUNT_LEADING_ZEROS(d.high) - COUNT_LEADING_ZEROS(n.high);\r | |
230 | // 0 <= sr <= n_uword_bits - 1 or sr large\r | |
231 | if (sr > n_uword_bits - 1)\r | |
232 | {\r | |
233 | if (rem)\r | |
234 | *rem = n.all;\r | |
235 | return 0;\r | |
236 | }\r | |
237 | ++sr;\r | |
238 | // 1 <= sr <= n_uword_bits\r | |
239 | // q.all = n.all << (n_udword_bits - sr);\r | |
240 | q.low = 0;\r | |
241 | q.high = n.low << (n_uword_bits - sr);\r | |
242 | // r.all = n.all >> sr;\r | |
243 | // if (sr < n_uword_bits)\r | |
244 | // {\r | |
245 | // r.high = n.high >> sr;\r | |
246 | // r.low = (n.high << (n_uword_bits - sr)) | (n.low >> sr);\r | |
247 | // }\r | |
248 | // else\r | |
249 | // {\r | |
250 | // r.high = 0;\r | |
251 | // r.low = n.high;\r | |
252 | // }\r | |
253 | r.high = (n.high >> sr) &\r | |
254 | ((INT32)(sr - n_uword_bits) >> (n_uword_bits-1));\r | |
255 | r.low = (n.high << (n_uword_bits - sr)) |\r | |
256 | ((n.low >> sr) &\r | |
257 | ((INT32)(sr - n_uword_bits) >> (n_uword_bits-1)));\r | |
258 | }\r | |
259 | }\r | |
260 | // Not a special case\r | |
261 | // q and r are initialized with:\r | |
262 | // q.all = n.all << (n_udword_bits - sr);\r | |
263 | // r.all = n.all >> sr;\r | |
264 | // 1 <= sr <= n_udword_bits - 1\r | |
265 | UINT32 carry = 0;\r | |
266 | for (; sr > 0; --sr)\r | |
267 | {\r | |
268 | // r:q = ((r:q) << 1) | carry\r | |
269 | r.high = (r.high << 1) | (r.low >> (n_uword_bits - 1));\r | |
270 | r.low = (r.low << 1) | (q.high >> (n_uword_bits - 1));\r | |
271 | q.high = (q.high << 1) | (q.low >> (n_uword_bits - 1));\r | |
272 | q.low = (q.low << 1) | carry;\r | |
273 | // carry = 0;\r | |
274 | // if (r.all >= d.all)\r | |
275 | // {\r | |
276 | // r.all -= d.all;\r | |
277 | // carry = 1;\r | |
278 | // }\r | |
279 | const INT64 s = (INT64)(d.all - r.all - 1) >> (n_udword_bits - 1);\r | |
280 | carry = s & 1;\r | |
281 | r.all -= d.all & s;\r | |
282 | }\r | |
283 | q.all = (q.all << 1) | carry;\r | |
284 | if (rem)\r | |
285 | *rem = r.all;\r | |
286 | return q.all;\r | |
287 | }\r |