26
26
#define HOST_UTILS_H 1
28
28
#include "qemu/compiler.h" /* QEMU_GNUC_PREREQ */
30
#if defined(__x86_64__)
31
#define __HAVE_FAST_MULU64__
32
32
static inline void mulu64(uint64_t *plow, uint64_t *phigh,
33
33
uint64_t a, uint64_t b)
36
: "=d" (*phigh), "=a" (*plow)
35
__uint128_t r = (__uint128_t)a * b;
39
#define __HAVE_FAST_MULS64__
40
40
static inline void muls64(uint64_t *plow, uint64_t *phigh,
41
41
int64_t a, int64_t b)
43
__asm__ ("imul %0\n\t"
44
: "=d" (*phigh), "=a" (*plow)
43
__int128_t r = (__int128_t)a * b;
48
48
void muls64(uint64_t *phigh, uint64_t *plow, int64_t a, int64_t b);
49
49
void mulu64(uint64_t *phigh, uint64_t *plow, uint64_t a, uint64_t b);
52
/* Binary search for leading zeros. */
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.
54
59
static inline int clz32(uint32_t val)
56
61
#if QEMU_GNUC_PREREQ(3, 4)
58
return __builtin_clz(val);
62
return val ? __builtin_clz(val) : 32;
64
/* Binary search for the leading one bit. */
64
67
if (!(val & 0xFFFF0000U)) {
95
* clo32 - count leading ones in a 32-bit value.
96
* @val: The value to search
98
* Returns 32 if the value is -1.
91
100
static inline int clo32(uint32_t val)
93
102
return clz32(~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.
96
112
static inline int clz64(uint64_t val)
98
114
#if QEMU_GNUC_PREREQ(3, 4)
100
return __builtin_clzll(val);
115
return val ? __builtin_clzll(val) : 64;
130
* clo64 - count leading ones in a 64-bit value.
131
* @val: The value to search
133
* Returns 64 if the value is -1.
116
135
static inline int clo64(uint64_t val)
118
137
return clz64(~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.
121
147
static inline int ctz32(uint32_t val)
123
149
#if QEMU_GNUC_PREREQ(3, 4)
125
return __builtin_ctz(val);
150
return val ? __builtin_ctz(val) : 32;
152
/* Binary search for the trailing one bit. */
185
* cto32 - count trailing ones in a 32-bit value.
186
* @val: The value to search
188
* Returns 32 if the value is -1.
160
190
static inline int cto32(uint32_t val)
162
192
return ctz32(~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.
165
202
static inline int ctz64(uint64_t val)
167
204
#if QEMU_GNUC_PREREQ(3, 4)
169
return __builtin_ctzll(val);
205
return val ? __builtin_ctzll(val) : 64;
220
* ctz64 - count trailing ones in a 64-bit value.
221
* @val: The value to search
223
* Returns 64 if the value is -1.
185
225
static inline int cto64(uint64_t val)
187
227
return ctz64(~val);
231
* ctpop8 - count the population of one bits in an 8-bit value.
232
* @val: The value to search
190
234
static inline int ctpop8(uint8_t val)
236
#if QEMU_GNUC_PREREQ(3, 4)
237
return __builtin_popcount(val);
192
239
val = (val & 0x55) + ((val >> 1) & 0x55);
193
240
val = (val & 0x33) + ((val >> 2) & 0x33);
194
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
199
251
static inline int ctpop16(uint16_t val)
253
#if QEMU_GNUC_PREREQ(3, 4)
254
return __builtin_popcount(val);
201
256
val = (val & 0x5555) + ((val >> 1) & 0x5555);
202
257
val = (val & 0x3333) + ((val >> 2) & 0x3333);
203
258
val = (val & 0x0f0f) + ((val >> 4) & 0x0f0f);
204
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
209
269
static inline int ctpop32(uint32_t val)
211
271
#if QEMU_GNUC_PREREQ(3, 4)
285
* ctpop64 - count the population of one bits in a 64-bit value.
286
* @val: The value to search
224
288
static inline int ctpop64(uint64_t val)
226
290
#if QEMU_GNUC_PREREQ(3, 4)