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 static inline int divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor)
53 __uint128_t dividend = ((__uint128_t)*phigh << 64) | *plow;
54 __uint128_t result = dividend / divisor;
56 *phigh = dividend % divisor;
57 return result > UINT64_MAX;
61 void muls64(uint64_t *phigh, uint64_t *plow, int64_t a, int64_t b);
62 void mulu64(uint64_t *phigh, uint64_t *plow, uint64_t a, uint64_t b);
63 int divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor);
67 * clz32 - count leading zeros in a 32-bit value.
68 * @val: The value to search
70 * Returns 32 if the value is zero. Note that the GCC builtin is
71 * undefined if the value is zero.
73 static inline int clz32(uint32_t val)
75 #if QEMU_GNUC_PREREQ(3, 4)
76 return val ? __builtin_clz(val) : 32;
78 /* Binary search for the leading one bit. */
81 if (!(val & 0xFFFF0000U)) {
85 if (!(val & 0xFF000000U)) {
89 if (!(val & 0xF0000000U)) {
93 if (!(val & 0xC0000000U)) {
97 if (!(val & 0x80000000U)) {
101 if (!(val & 0x80000000U)) {
109 * clo32 - count leading ones in a 32-bit value.
110 * @val: The value to search
112 * Returns 32 if the value is -1.
114 static inline int clo32(uint32_t val)
120 * clz64 - count leading zeros in a 64-bit value.
121 * @val: The value to search
123 * Returns 64 if the value is zero. Note that the GCC builtin is
124 * undefined if the value is zero.
126 static inline int clz64(uint64_t val)
128 #if QEMU_GNUC_PREREQ(3, 4)
129 return val ? __builtin_clzll(val) : 64;
139 return cnt + clz32(val);
144 * clo64 - count leading ones in a 64-bit value.
145 * @val: The value to search
147 * Returns 64 if the value is -1.
149 static inline int clo64(uint64_t val)
155 * ctz32 - count trailing zeros in a 32-bit value.
156 * @val: The value to search
158 * Returns 32 if the value is zero. Note that the GCC builtin is
159 * undefined if the value is zero.
161 static inline int ctz32(uint32_t val)
163 #if QEMU_GNUC_PREREQ(3, 4)
164 return val ? __builtin_ctz(val) : 32;
166 /* Binary search for the trailing one bit. */
170 if (!(val & 0x0000FFFFUL)) {
174 if (!(val & 0x000000FFUL)) {
178 if (!(val & 0x0000000FUL)) {
182 if (!(val & 0x00000003UL)) {
186 if (!(val & 0x00000001UL)) {
190 if (!(val & 0x00000001UL)) {
199 * cto32 - count trailing ones in a 32-bit value.
200 * @val: The value to search
202 * Returns 32 if the value is -1.
204 static inline int cto32(uint32_t val)
210 * ctz64 - count trailing zeros in a 64-bit value.
211 * @val: The value to search
213 * Returns 64 if the value is zero. Note that the GCC builtin is
214 * undefined if the value is zero.
216 static inline int ctz64(uint64_t val)
218 #if QEMU_GNUC_PREREQ(3, 4)
219 return val ? __builtin_ctzll(val) : 64;
224 if (!((uint32_t)val)) {
229 return cnt + ctz32(val);
234 * cto64 - count trailing ones in a 64-bit value.
235 * @val: The value to search
237 * Returns 64 if the value is -1.
239 static inline int cto64(uint64_t val)
245 * clrsb32 - count leading redundant sign bits in a 32-bit value.
246 * @val: The value to search
248 * Returns the number of bits following the sign bit that are equal to it.
249 * No special cases; output range is [0-31].
251 static inline int clrsb32(uint32_t val)
253 #if QEMU_GNUC_PREREQ(4, 7)
254 return __builtin_clrsb(val);
256 return clz32(val ^ ((int32_t)val >> 1)) - 1;
261 * clrsb64 - count leading redundant sign bits in a 64-bit value.
262 * @val: The value to search
264 * Returns the number of bits following the sign bit that are equal to it.
265 * No special cases; output range is [0-63].
267 static inline int clrsb64(uint64_t val)
269 #if QEMU_GNUC_PREREQ(4, 7)
270 return __builtin_clrsbll(val);
272 return clz64(val ^ ((int64_t)val >> 1)) - 1;
277 * ctpop8 - count the population of one bits in an 8-bit value.
278 * @val: The value to search
280 static inline int ctpop8(uint8_t val)
282 #if QEMU_GNUC_PREREQ(3, 4)
283 return __builtin_popcount(val);
285 val = (val & 0x55) + ((val >> 1) & 0x55);
286 val = (val & 0x33) + ((val >> 2) & 0x33);
287 val = (val & 0x0f) + ((val >> 4) & 0x0f);
294 * ctpop16 - count the population of one bits in a 16-bit value.
295 * @val: The value to search
297 static inline int ctpop16(uint16_t val)
299 #if QEMU_GNUC_PREREQ(3, 4)
300 return __builtin_popcount(val);
302 val = (val & 0x5555) + ((val >> 1) & 0x5555);
303 val = (val & 0x3333) + ((val >> 2) & 0x3333);
304 val = (val & 0x0f0f) + ((val >> 4) & 0x0f0f);
305 val = (val & 0x00ff) + ((val >> 8) & 0x00ff);
312 * ctpop32 - count the population of one bits in a 32-bit value.
313 * @val: The value to search
315 static inline int ctpop32(uint32_t val)
317 #if QEMU_GNUC_PREREQ(3, 4)
318 return __builtin_popcount(val);
320 val = (val & 0x55555555) + ((val >> 1) & 0x55555555);
321 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
322 val = (val & 0x0f0f0f0f) + ((val >> 4) & 0x0f0f0f0f);
323 val = (val & 0x00ff00ff) + ((val >> 8) & 0x00ff00ff);
324 val = (val & 0x0000ffff) + ((val >> 16) & 0x0000ffff);
331 * ctpop64 - count the population of one bits in a 64-bit value.
332 * @val: The value to search
334 static inline int ctpop64(uint64_t val)
336 #if QEMU_GNUC_PREREQ(3, 4)
337 return __builtin_popcountll(val);
339 val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL);
340 val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL);
341 val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >> 4) & 0x0f0f0f0f0f0f0f0fULL);
342 val = (val & 0x00ff00ff00ff00ffULL) + ((val >> 8) & 0x00ff00ff00ff00ffULL);
343 val = (val & 0x0000ffff0000ffffULL) + ((val >> 16) & 0x0000ffff0000ffffULL);
344 val = (val & 0x00000000ffffffffULL) + ((val >> 32) & 0x00000000ffffffffULL);
350 /* Host type specific sizes of these routines. */
352 #if ULONG_MAX == UINT32_MAX
357 # define ctpopl ctpop32
358 #elif ULONG_MAX == UINT64_MAX
363 # define ctpopl ctpop64
365 # error Unknown sizeof long