1 | /*
|
---|
2 | * Utility compute operations used by translated code.
|
---|
3 | *
|
---|
4 | * Copyright (c) 2007 Thiemo Seufer
|
---|
5 | * Copyright (c) 2007 Jocelyn Mayer
|
---|
6 | *
|
---|
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
8 | * of this software and associated documentation files (the "Software"), to deal
|
---|
9 | * in the Software without restriction, including without limitation the rights
|
---|
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
11 | * copies of the Software, and to permit persons to whom the Software is
|
---|
12 | * furnished to do so, subject to the following conditions:
|
---|
13 | *
|
---|
14 | * The above copyright notice and this permission notice shall be included in
|
---|
15 | * all copies or substantial portions of the Software.
|
---|
16 | *
|
---|
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
---|
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
---|
23 | * THE SOFTWARE.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "osdep.h"
|
---|
27 |
|
---|
28 | #if defined(__x86_64__)
|
---|
29 | #define __HAVE_FAST_MULU64__
|
---|
30 | static always_inline void mulu64 (uint64_t *plow, uint64_t *phigh,
|
---|
31 | uint64_t a, uint64_t b)
|
---|
32 | {
|
---|
33 | __asm__ ("mul %0\n\t"
|
---|
34 | : "=d" (*phigh), "=a" (*plow)
|
---|
35 | : "a" (a), "0" (b));
|
---|
36 | }
|
---|
37 | #define __HAVE_FAST_MULS64__
|
---|
38 | static always_inline void muls64 (uint64_t *plow, uint64_t *phigh,
|
---|
39 | int64_t a, int64_t b)
|
---|
40 | {
|
---|
41 | __asm__ ("imul %0\n\t"
|
---|
42 | : "=d" (*phigh), "=a" (*plow)
|
---|
43 | : "a" (a), "0" (b));
|
---|
44 | }
|
---|
45 | #else
|
---|
46 | void muls64(uint64_t *phigh, uint64_t *plow, int64_t a, int64_t b);
|
---|
47 | void mulu64(uint64_t *phigh, uint64_t *plow, uint64_t a, uint64_t b);
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | /* Binary search for leading zeros. */
|
---|
51 |
|
---|
52 | #ifndef VBOX
|
---|
53 | static always_inline int clz32(uint32_t val)
|
---|
54 | #else
|
---|
55 | DECLALWAYSINLINE(int) clz32(uint32_t val)
|
---|
56 | #endif
|
---|
57 | {
|
---|
58 | #if QEMU_GNUC_PREREQ(3, 4)
|
---|
59 | if (val)
|
---|
60 | return __builtin_clz(val);
|
---|
61 | else
|
---|
62 | return 32;
|
---|
63 | #else
|
---|
64 | int cnt = 0;
|
---|
65 |
|
---|
66 | if (!(val & 0xFFFF0000U)) {
|
---|
67 | cnt += 16;
|
---|
68 | val <<= 16;
|
---|
69 | }
|
---|
70 | if (!(val & 0xFF000000U)) {
|
---|
71 | cnt += 8;
|
---|
72 | val <<= 8;
|
---|
73 | }
|
---|
74 | if (!(val & 0xF0000000U)) {
|
---|
75 | cnt += 4;
|
---|
76 | val <<= 4;
|
---|
77 | }
|
---|
78 | if (!(val & 0xC0000000U)) {
|
---|
79 | cnt += 2;
|
---|
80 | val <<= 2;
|
---|
81 | }
|
---|
82 | if (!(val & 0x80000000U)) {
|
---|
83 | cnt++;
|
---|
84 | val <<= 1;
|
---|
85 | }
|
---|
86 | if (!(val & 0x80000000U)) {
|
---|
87 | cnt++;
|
---|
88 | }
|
---|
89 | return cnt;
|
---|
90 | #endif
|
---|
91 | }
|
---|
92 |
|
---|
93 | #ifndef VBOX
|
---|
94 | static always_inline int clo32(uint32_t val)
|
---|
95 | #else
|
---|
96 | DECLALWAYSINLINE(int) clo32(uint32_t val)
|
---|
97 | #endif
|
---|
98 | {
|
---|
99 | return clz32(~val);
|
---|
100 | }
|
---|
101 |
|
---|
102 | #ifndef VBOX
|
---|
103 | static always_inline int clz64(uint64_t val)
|
---|
104 | #else
|
---|
105 | DECLALWAYSINLINE(int) clz64(uint64_t val)
|
---|
106 | #endif
|
---|
107 | {
|
---|
108 | #if QEMU_GNUC_PREREQ(3, 4)
|
---|
109 | if (val)
|
---|
110 | return __builtin_clzll(val);
|
---|
111 | else
|
---|
112 | return 64;
|
---|
113 | #else
|
---|
114 | int cnt = 0;
|
---|
115 |
|
---|
116 | if (!(val >> 32)) {
|
---|
117 | cnt += 32;
|
---|
118 | } else {
|
---|
119 | val >>= 32;
|
---|
120 | }
|
---|
121 |
|
---|
122 | return cnt + clz32(val);
|
---|
123 | #endif
|
---|
124 | }
|
---|
125 |
|
---|
126 | #ifndef VBOX
|
---|
127 | static always_inline int clo64(uint64_t val)
|
---|
128 | #else
|
---|
129 | DECLALWAYSINLINE(int) clo64(uint64_t val)
|
---|
130 | #endif
|
---|
131 | {
|
---|
132 | return clz64(~val);
|
---|
133 | }
|
---|
134 |
|
---|
135 | #ifndef VBOX
|
---|
136 | static always_inline int ctz32 (uint32_t val)
|
---|
137 | #else
|
---|
138 | DECLALWAYSINLINE(int) ctz32 (uint32_t val)
|
---|
139 | #endif
|
---|
140 | {
|
---|
141 | #if QEMU_GNUC_PREREQ(3, 4)
|
---|
142 | if (val)
|
---|
143 | return __builtin_ctz(val);
|
---|
144 | else
|
---|
145 | return 32;
|
---|
146 | #else
|
---|
147 | int cnt;
|
---|
148 |
|
---|
149 | cnt = 0;
|
---|
150 | if (!(val & 0x0000FFFFUL)) {
|
---|
151 | cnt += 16;
|
---|
152 | val >>= 16;
|
---|
153 | }
|
---|
154 | if (!(val & 0x000000FFUL)) {
|
---|
155 | cnt += 8;
|
---|
156 | val >>= 8;
|
---|
157 | }
|
---|
158 | if (!(val & 0x0000000FUL)) {
|
---|
159 | cnt += 4;
|
---|
160 | val >>= 4;
|
---|
161 | }
|
---|
162 | if (!(val & 0x00000003UL)) {
|
---|
163 | cnt += 2;
|
---|
164 | val >>= 2;
|
---|
165 | }
|
---|
166 | if (!(val & 0x00000001UL)) {
|
---|
167 | cnt++;
|
---|
168 | val >>= 1;
|
---|
169 | }
|
---|
170 | if (!(val & 0x00000001UL)) {
|
---|
171 | cnt++;
|
---|
172 | }
|
---|
173 |
|
---|
174 | return cnt;
|
---|
175 | #endif
|
---|
176 | }
|
---|
177 |
|
---|
178 | #ifndef VBOX
|
---|
179 | static always_inline int cto32 (uint32_t val)
|
---|
180 | #else
|
---|
181 | DECLALWAYSINLINE(int) cto32 (uint32_t val)
|
---|
182 | #endif
|
---|
183 | {
|
---|
184 | return ctz32(~val);
|
---|
185 | }
|
---|
186 |
|
---|
187 | #ifndef VBOX
|
---|
188 | static always_inline int ctz64 (uint64_t val)
|
---|
189 | #else
|
---|
190 | DECLALWAYSINLINE(int) ctz64 (uint64_t val)
|
---|
191 | #endif
|
---|
192 | {
|
---|
193 | #if QEMU_GNUC_PREREQ(3, 4)
|
---|
194 | if (val)
|
---|
195 | return __builtin_ctz(val);
|
---|
196 | else
|
---|
197 | return 64;
|
---|
198 | #else
|
---|
199 | int cnt;
|
---|
200 |
|
---|
201 | cnt = 0;
|
---|
202 | if (!((uint32_t)val)) {
|
---|
203 | cnt += 32;
|
---|
204 | val >>= 32;
|
---|
205 | }
|
---|
206 |
|
---|
207 | return cnt + ctz32(val);
|
---|
208 | #endif
|
---|
209 | }
|
---|
210 |
|
---|
211 | #ifndef VBOX
|
---|
212 | static always_inline int cto64 (uint64_t val)
|
---|
213 | #else
|
---|
214 | DECLALWAYSINLINE(int) cto64 (uint64_t val)
|
---|
215 | #endif
|
---|
216 | {
|
---|
217 | return ctz64(~val);
|
---|
218 | }
|
---|
219 |
|
---|
220 | #ifndef VBOX
|
---|
221 | static always_inline int ctpop8 (uint8_t val)
|
---|
222 | #else
|
---|
223 | DECLALWAYSINLINE(int) ctpop8 (uint8_t val)
|
---|
224 | #endif
|
---|
225 | {
|
---|
226 | val = (val & 0x55) + ((val >> 1) & 0x55);
|
---|
227 | val = (val & 0x33) + ((val >> 2) & 0x33);
|
---|
228 | val = (val & 0x0f) + ((val >> 4) & 0x0f);
|
---|
229 |
|
---|
230 | return val;
|
---|
231 | }
|
---|
232 |
|
---|
233 | #ifndef VBOX
|
---|
234 | static always_inline int ctpop16 (uint16_t val)
|
---|
235 | #else
|
---|
236 | DECLALWAYSINLINE(int) ctpop16 (uint16_t val)
|
---|
237 | #endif
|
---|
238 | {
|
---|
239 | val = (val & 0x5555) + ((val >> 1) & 0x5555);
|
---|
240 | val = (val & 0x3333) + ((val >> 2) & 0x3333);
|
---|
241 | val = (val & 0x0f0f) + ((val >> 4) & 0x0f0f);
|
---|
242 | val = (val & 0x00ff) + ((val >> 8) & 0x00ff);
|
---|
243 |
|
---|
244 | return val;
|
---|
245 | }
|
---|
246 |
|
---|
247 | #ifndef VBOX
|
---|
248 | static always_inline int ctpop32 (uint32_t val)
|
---|
249 | #else
|
---|
250 | DECLALWAYSINLINE(int) ctpop32 (uint32_t val)
|
---|
251 | #endif
|
---|
252 | {
|
---|
253 | #if QEMU_GNUC_PREREQ(3, 4)
|
---|
254 | return __builtin_popcount(val);
|
---|
255 | #else
|
---|
256 | val = (val & 0x55555555) + ((val >> 1) & 0x55555555);
|
---|
257 | val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
|
---|
258 | val = (val & 0x0f0f0f0f) + ((val >> 4) & 0x0f0f0f0f);
|
---|
259 | val = (val & 0x00ff00ff) + ((val >> 8) & 0x00ff00ff);
|
---|
260 | val = (val & 0x0000ffff) + ((val >> 16) & 0x0000ffff);
|
---|
261 |
|
---|
262 | return val;
|
---|
263 | #endif
|
---|
264 | }
|
---|
265 |
|
---|
266 | #ifndef VBOX
|
---|
267 | static always_inline int ctpop64 (uint64_t val)
|
---|
268 | #else
|
---|
269 | DECLALWAYSINLINE(int) ctpop64 (uint64_t val)
|
---|
270 | #endif
|
---|
271 | {
|
---|
272 | #if QEMU_GNUC_PREREQ(3, 4)
|
---|
273 | return __builtin_popcountll(val);
|
---|
274 | #else
|
---|
275 | val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL);
|
---|
276 | val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL);
|
---|
277 | val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >> 4) & 0x0f0f0f0f0f0f0f0fULL);
|
---|
278 | val = (val & 0x00ff00ff00ff00ffULL) + ((val >> 8) & 0x00ff00ff00ff00ffULL);
|
---|
279 | val = (val & 0x0000ffff0000ffffULL) + ((val >> 16) & 0x0000ffff0000ffffULL);
|
---|
280 | val = (val & 0x00000000ffffffffULL) + ((val >> 32) & 0x00000000ffffffffULL);
|
---|
281 |
|
---|
282 | return val;
|
---|
283 | #endif
|
---|
284 | }
|
---|