Revision 72d81155 include/qemu/host-utils.h
b/include/qemu/host-utils.h | ||
---|---|---|
50 | 50 |
void mulu64(uint64_t *phigh, uint64_t *plow, uint64_t a, uint64_t b); |
51 | 51 |
#endif |
52 | 52 |
|
53 |
/* Binary search for leading zeros. */ |
|
54 |
|
|
53 |
/** |
|
54 |
* clz32 - count leading zeros in a 32-bit value. |
|
55 |
* @val: The value to search |
|
56 |
* |
|
57 |
* Returns 32 if the value is zero. Note that the GCC builtin is |
|
58 |
* undefined if the value is zero. |
|
59 |
*/ |
|
55 | 60 |
static inline int clz32(uint32_t val) |
56 | 61 |
{ |
57 | 62 |
#if QEMU_GNUC_PREREQ(3, 4) |
58 |
if (val) |
|
59 |
return __builtin_clz(val); |
|
60 |
else |
|
61 |
return 32; |
|
63 |
return val ? __builtin_clz(val) : 32; |
|
62 | 64 |
#else |
65 |
/* Binary search for the leading one bit. */ |
|
63 | 66 |
int cnt = 0; |
64 | 67 |
|
65 | 68 |
if (!(val & 0xFFFF0000U)) { |
... | ... | |
89 | 92 |
#endif |
90 | 93 |
} |
91 | 94 |
|
95 |
/** |
|
96 |
* clo32 - count leading ones in a 32-bit value. |
|
97 |
* @val: The value to search |
|
98 |
* |
|
99 |
* Returns 32 if the value is -1. |
|
100 |
*/ |
|
92 | 101 |
static inline int clo32(uint32_t val) |
93 | 102 |
{ |
94 | 103 |
return clz32(~val); |
95 | 104 |
} |
96 | 105 |
|
106 |
/** |
|
107 |
* clz64 - count leading zeros in a 64-bit value. |
|
108 |
* @val: The value to search |
|
109 |
* |
|
110 |
* Returns 64 if the value is zero. Note that the GCC builtin is |
|
111 |
* undefined if the value is zero. |
|
112 |
*/ |
|
97 | 113 |
static inline int clz64(uint64_t val) |
98 | 114 |
{ |
99 | 115 |
#if QEMU_GNUC_PREREQ(3, 4) |
100 |
if (val) |
|
101 |
return __builtin_clzll(val); |
|
102 |
else |
|
103 |
return 64; |
|
116 |
return val ? __builtin_clzll(val) : 64; |
|
104 | 117 |
#else |
105 | 118 |
int cnt = 0; |
106 | 119 |
|
... | ... | |
114 | 127 |
#endif |
115 | 128 |
} |
116 | 129 |
|
130 |
/** |
|
131 |
* clo64 - count leading ones in a 64-bit value. |
|
132 |
* @val: The value to search |
|
133 |
* |
|
134 |
* Returns 64 if the value is -1. |
|
135 |
*/ |
|
117 | 136 |
static inline int clo64(uint64_t val) |
118 | 137 |
{ |
119 | 138 |
return clz64(~val); |
120 | 139 |
} |
121 | 140 |
|
141 |
/** |
|
142 |
* ctz32 - count trailing zeros in a 32-bit value. |
|
143 |
* @val: The value to search |
|
144 |
* |
|
145 |
* Returns 32 if the value is zero. Note that the GCC builtin is |
|
146 |
* undefined if the value is zero. |
|
147 |
*/ |
|
122 | 148 |
static inline int ctz32(uint32_t val) |
123 | 149 |
{ |
124 | 150 |
#if QEMU_GNUC_PREREQ(3, 4) |
125 |
if (val) |
|
126 |
return __builtin_ctz(val); |
|
127 |
else |
|
128 |
return 32; |
|
151 |
return val ? __builtin_ctz(val) : 32; |
|
129 | 152 |
#else |
153 |
/* Binary search for the trailing one bit. */ |
|
130 | 154 |
int cnt; |
131 | 155 |
|
132 | 156 |
cnt = 0; |
... | ... | |
158 | 182 |
#endif |
159 | 183 |
} |
160 | 184 |
|
185 |
/** |
|
186 |
* cto32 - count trailing ones in a 32-bit value. |
|
187 |
* @val: The value to search |
|
188 |
* |
|
189 |
* Returns 32 if the value is -1. |
|
190 |
*/ |
|
161 | 191 |
static inline int cto32(uint32_t val) |
162 | 192 |
{ |
163 | 193 |
return ctz32(~val); |
164 | 194 |
} |
165 | 195 |
|
196 |
/** |
|
197 |
* ctz64 - count trailing zeros in a 64-bit value. |
|
198 |
* @val: The value to search |
|
199 |
* |
|
200 |
* Returns 64 if the value is zero. Note that the GCC builtin is |
|
201 |
* undefined if the value is zero. |
|
202 |
*/ |
|
166 | 203 |
static inline int ctz64(uint64_t val) |
167 | 204 |
{ |
168 | 205 |
#if QEMU_GNUC_PREREQ(3, 4) |
169 |
if (val) |
|
170 |
return __builtin_ctzll(val); |
|
171 |
else |
|
172 |
return 64; |
|
206 |
return val ? __builtin_ctzll(val) : 64; |
|
173 | 207 |
#else |
174 | 208 |
int cnt; |
175 | 209 |
|
... | ... | |
183 | 217 |
#endif |
184 | 218 |
} |
185 | 219 |
|
220 |
/** |
|
221 |
* ctz64 - count trailing ones in a 64-bit value. |
|
222 |
* @val: The value to search |
|
223 |
* |
|
224 |
* Returns 64 if the value is -1. |
|
225 |
*/ |
|
186 | 226 |
static inline int cto64(uint64_t val) |
187 | 227 |
{ |
188 | 228 |
return ctz64(~val); |
189 | 229 |
} |
190 | 230 |
|
231 |
/** |
|
232 |
* ctpop8 - count the population of one bits in an 8-bit value. |
|
233 |
* @val: The value to search |
|
234 |
*/ |
|
191 | 235 |
static inline int ctpop8(uint8_t val) |
192 | 236 |
{ |
237 |
#if QEMU_GNUC_PREREQ(3, 4) |
|
238 |
return __builtin_popcount(val); |
|
239 |
#else |
|
193 | 240 |
val = (val & 0x55) + ((val >> 1) & 0x55); |
194 | 241 |
val = (val & 0x33) + ((val >> 2) & 0x33); |
195 | 242 |
val = (val & 0x0f) + ((val >> 4) & 0x0f); |
196 | 243 |
|
197 | 244 |
return val; |
245 |
#endif |
|
198 | 246 |
} |
199 | 247 |
|
248 |
/** |
|
249 |
* ctpop16 - count the population of one bits in a 16-bit value. |
|
250 |
* @val: The value to search |
|
251 |
*/ |
|
200 | 252 |
static inline int ctpop16(uint16_t val) |
201 | 253 |
{ |
254 |
#if QEMU_GNUC_PREREQ(3, 4) |
|
255 |
return __builtin_popcount(val); |
|
256 |
#else |
|
202 | 257 |
val = (val & 0x5555) + ((val >> 1) & 0x5555); |
203 | 258 |
val = (val & 0x3333) + ((val >> 2) & 0x3333); |
204 | 259 |
val = (val & 0x0f0f) + ((val >> 4) & 0x0f0f); |
205 | 260 |
val = (val & 0x00ff) + ((val >> 8) & 0x00ff); |
206 | 261 |
|
207 | 262 |
return val; |
263 |
#endif |
|
208 | 264 |
} |
209 | 265 |
|
266 |
/** |
|
267 |
* ctpop32 - count the population of one bits in a 32-bit value. |
|
268 |
* @val: The value to search |
|
269 |
*/ |
|
210 | 270 |
static inline int ctpop32(uint32_t val) |
211 | 271 |
{ |
212 | 272 |
#if QEMU_GNUC_PREREQ(3, 4) |
... | ... | |
222 | 282 |
#endif |
223 | 283 |
} |
224 | 284 |
|
285 |
/** |
|
286 |
* ctpop64 - count the population of one bits in a 64-bit value. |
|
287 |
* @val: The value to search |
|
288 |
*/ |
|
225 | 289 |
static inline int ctpop64(uint64_t val) |
226 | 290 |
{ |
227 | 291 |
#if QEMU_GNUC_PREREQ(3, 4) |
Also available in: Unified diff