]>
git.proxmox.com Git - qemu.git/blob - include/qemu/host-utils.h
2 * Utility compute operations used by translated code.
4 * Copyright (c) 2007 Thiemo Seufer
5 * Copyright (c) 2007 Jocelyn Mayer
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
26 #define HOST_UTILS_H 1
28 #include "qemu/compiler.h" /* QEMU_GNUC_PREREQ */
32 static inline void mulu64(uint64_t *plow
, uint64_t *phigh
,
33 uint64_t a
, uint64_t b
)
35 __uint128_t r
= (__uint128_t
)a
* b
;
40 static inline void muls64(uint64_t *plow
, uint64_t *phigh
,
43 __int128_t r
= (__int128_t
)a
* b
;
48 void muls64(uint64_t *phigh
, uint64_t *plow
, int64_t a
, int64_t b
);
49 void mulu64(uint64_t *phigh
, uint64_t *plow
, uint64_t a
, uint64_t b
);
53 * clz32 - count leading zeros in a 32-bit value.
54 * @val: The value to search
56 * Returns 32 if the value is zero. Note that the GCC builtin is
57 * undefined if the value is zero.
59 static inline int clz32(uint32_t val
)
61 #if QEMU_GNUC_PREREQ(3, 4)
62 return val
? __builtin_clz(val
) : 32;
64 /* Binary search for the leading one bit. */
67 if (!(val
& 0xFFFF0000U
)) {
71 if (!(val
& 0xFF000000U
)) {
75 if (!(val
& 0xF0000000U
)) {
79 if (!(val
& 0xC0000000U
)) {
83 if (!(val
& 0x80000000U
)) {
87 if (!(val
& 0x80000000U
)) {
95 * clo32 - count leading ones in a 32-bit value.
96 * @val: The value to search
98 * Returns 32 if the value is -1.
100 static inline int clo32(uint32_t val
)
106 * clz64 - count leading zeros in a 64-bit value.
107 * @val: The value to search
109 * Returns 64 if the value is zero. Note that the GCC builtin is
110 * undefined if the value is zero.
112 static inline int clz64(uint64_t val
)
114 #if QEMU_GNUC_PREREQ(3, 4)
115 return val
? __builtin_clzll(val
) : 64;
125 return cnt
+ clz32(val
);
130 * clo64 - count leading ones in a 64-bit value.
131 * @val: The value to search
133 * Returns 64 if the value is -1.
135 static inline int clo64(uint64_t val
)
141 * ctz32 - count trailing zeros in a 32-bit value.
142 * @val: The value to search
144 * Returns 32 if the value is zero. Note that the GCC builtin is
145 * undefined if the value is zero.
147 static inline int ctz32(uint32_t val
)
149 #if QEMU_GNUC_PREREQ(3, 4)
150 return val
? __builtin_ctz(val
) : 32;
152 /* Binary search for the trailing one bit. */
156 if (!(val
& 0x0000FFFFUL
)) {
160 if (!(val
& 0x000000FFUL
)) {
164 if (!(val
& 0x0000000FUL
)) {
168 if (!(val
& 0x00000003UL
)) {
172 if (!(val
& 0x00000001UL
)) {
176 if (!(val
& 0x00000001UL
)) {
185 * cto32 - count trailing ones in a 32-bit value.
186 * @val: The value to search
188 * Returns 32 if the value is -1.
190 static inline int cto32(uint32_t val
)
196 * ctz64 - count trailing zeros in a 64-bit value.
197 * @val: The value to search
199 * Returns 64 if the value is zero. Note that the GCC builtin is
200 * undefined if the value is zero.
202 static inline int ctz64(uint64_t val
)
204 #if QEMU_GNUC_PREREQ(3, 4)
205 return val
? __builtin_ctzll(val
) : 64;
210 if (!((uint32_t)val
)) {
215 return cnt
+ ctz32(val
);
220 * ctz64 - count trailing ones in a 64-bit value.
221 * @val: The value to search
223 * Returns 64 if the value is -1.
225 static inline int cto64(uint64_t val
)
231 * ctpop8 - count the population of one bits in an 8-bit value.
232 * @val: The value to search
234 static inline int ctpop8(uint8_t val
)
236 #if QEMU_GNUC_PREREQ(3, 4)
237 return __builtin_popcount(val
);
239 val
= (val
& 0x55) + ((val
>> 1) & 0x55);
240 val
= (val
& 0x33) + ((val
>> 2) & 0x33);
241 val
= (val
& 0x0f) + ((val
>> 4) & 0x0f);
248 * ctpop16 - count the population of one bits in a 16-bit value.
249 * @val: The value to search
251 static inline int ctpop16(uint16_t val
)
253 #if QEMU_GNUC_PREREQ(3, 4)
254 return __builtin_popcount(val
);
256 val
= (val
& 0x5555) + ((val
>> 1) & 0x5555);
257 val
= (val
& 0x3333) + ((val
>> 2) & 0x3333);
258 val
= (val
& 0x0f0f) + ((val
>> 4) & 0x0f0f);
259 val
= (val
& 0x00ff) + ((val
>> 8) & 0x00ff);
266 * ctpop32 - count the population of one bits in a 32-bit value.
267 * @val: The value to search
269 static inline int ctpop32(uint32_t val
)
271 #if QEMU_GNUC_PREREQ(3, 4)
272 return __builtin_popcount(val
);
274 val
= (val
& 0x55555555) + ((val
>> 1) & 0x55555555);
275 val
= (val
& 0x33333333) + ((val
>> 2) & 0x33333333);
276 val
= (val
& 0x0f0f0f0f) + ((val
>> 4) & 0x0f0f0f0f);
277 val
= (val
& 0x00ff00ff) + ((val
>> 8) & 0x00ff00ff);
278 val
= (val
& 0x0000ffff) + ((val
>> 16) & 0x0000ffff);
285 * ctpop64 - count the population of one bits in a 64-bit value.
286 * @val: The value to search
288 static inline int ctpop64(uint64_t val
)
290 #if QEMU_GNUC_PREREQ(3, 4)
291 return __builtin_popcountll(val
);
293 val
= (val
& 0x5555555555555555ULL
) + ((val
>> 1) & 0x5555555555555555ULL
);
294 val
= (val
& 0x3333333333333333ULL
) + ((val
>> 2) & 0x3333333333333333ULL
);
295 val
= (val
& 0x0f0f0f0f0f0f0f0fULL
) + ((val
>> 4) & 0x0f0f0f0f0f0f0f0fULL
);
296 val
= (val
& 0x00ff00ff00ff00ffULL
) + ((val
>> 8) & 0x00ff00ff00ff00ffULL
);
297 val
= (val
& 0x0000ffff0000ffffULL
) + ((val
>> 16) & 0x0000ffff0000ffffULL
);
298 val
= (val
& 0x00000000ffffffffULL
) + ((val
>> 32) & 0x00000000ffffffffULL
);
304 /* Host type specific sizes of these routines. */
306 #if ULONG_MAX == UINT32_MAX
311 # define ctpopl ctpop32
312 #elif ULONG_MAX == UINT64_MAX
317 # define ctpopl ctpop64
319 # error Unknown sizeof long