Clean up header guards that don't match their file name
[qemu/kevin.git] / include / qemu / host-utils.h
blob3de7d4ec55082deb800b58582952a510799375be
1 /*
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
23 * THE SOFTWARE.
25 #ifndef HOST_UTILS_H
26 #define HOST_UTILS_H 1
28 #include "qemu/bswap.h"
30 #ifdef CONFIG_INT128
31 static inline void mulu64(uint64_t *plow, uint64_t *phigh,
32 uint64_t a, uint64_t b)
34 __uint128_t r = (__uint128_t)a * b;
35 *plow = r;
36 *phigh = r >> 64;
39 static inline void muls64(uint64_t *plow, uint64_t *phigh,
40 int64_t a, int64_t b)
42 __int128_t r = (__int128_t)a * b;
43 *plow = r;
44 *phigh = r >> 64;
47 /* compute with 96 bit intermediate result: (a*b)/c */
48 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
50 return (__int128_t)a * b / c;
53 static inline int divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor)
55 if (divisor == 0) {
56 return 1;
57 } else {
58 __uint128_t dividend = ((__uint128_t)*phigh << 64) | *plow;
59 __uint128_t result = dividend / divisor;
60 *plow = result;
61 *phigh = dividend % divisor;
62 return result > UINT64_MAX;
66 static inline int divs128(int64_t *plow, int64_t *phigh, int64_t divisor)
68 if (divisor == 0) {
69 return 1;
70 } else {
71 __int128_t dividend = ((__int128_t)*phigh << 64) | *plow;
72 __int128_t result = dividend / divisor;
73 *plow = result;
74 *phigh = dividend % divisor;
75 return result != *plow;
78 #else
79 void muls64(uint64_t *phigh, uint64_t *plow, int64_t a, int64_t b);
80 void mulu64(uint64_t *phigh, uint64_t *plow, uint64_t a, uint64_t b);
81 int divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor);
82 int divs128(int64_t *plow, int64_t *phigh, int64_t divisor);
84 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
86 union {
87 uint64_t ll;
88 struct {
89 #ifdef HOST_WORDS_BIGENDIAN
90 uint32_t high, low;
91 #else
92 uint32_t low, high;
93 #endif
94 } l;
95 } u, res;
96 uint64_t rl, rh;
98 u.ll = a;
99 rl = (uint64_t)u.l.low * (uint64_t)b;
100 rh = (uint64_t)u.l.high * (uint64_t)b;
101 rh += (rl >> 32);
102 res.l.high = rh / c;
103 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
104 return res.ll;
106 #endif
109 * clz32 - count leading zeros in a 32-bit value.
110 * @val: The value to search
112 * Returns 32 if the value is zero. Note that the GCC builtin is
113 * undefined if the value is zero.
115 static inline int clz32(uint32_t val)
117 #if QEMU_GNUC_PREREQ(3, 4)
118 return val ? __builtin_clz(val) : 32;
119 #else
120 /* Binary search for the leading one bit. */
121 int cnt = 0;
123 if (!(val & 0xFFFF0000U)) {
124 cnt += 16;
125 val <<= 16;
127 if (!(val & 0xFF000000U)) {
128 cnt += 8;
129 val <<= 8;
131 if (!(val & 0xF0000000U)) {
132 cnt += 4;
133 val <<= 4;
135 if (!(val & 0xC0000000U)) {
136 cnt += 2;
137 val <<= 2;
139 if (!(val & 0x80000000U)) {
140 cnt++;
141 val <<= 1;
143 if (!(val & 0x80000000U)) {
144 cnt++;
146 return cnt;
147 #endif
151 * clo32 - count leading ones in a 32-bit value.
152 * @val: The value to search
154 * Returns 32 if the value is -1.
156 static inline int clo32(uint32_t val)
158 return clz32(~val);
162 * clz64 - count leading zeros in a 64-bit value.
163 * @val: The value to search
165 * Returns 64 if the value is zero. Note that the GCC builtin is
166 * undefined if the value is zero.
168 static inline int clz64(uint64_t val)
170 #if QEMU_GNUC_PREREQ(3, 4)
171 return val ? __builtin_clzll(val) : 64;
172 #else
173 int cnt = 0;
175 if (!(val >> 32)) {
176 cnt += 32;
177 } else {
178 val >>= 32;
181 return cnt + clz32(val);
182 #endif
186 * clo64 - count leading ones in a 64-bit value.
187 * @val: The value to search
189 * Returns 64 if the value is -1.
191 static inline int clo64(uint64_t val)
193 return clz64(~val);
197 * ctz32 - count trailing zeros in a 32-bit value.
198 * @val: The value to search
200 * Returns 32 if the value is zero. Note that the GCC builtin is
201 * undefined if the value is zero.
203 static inline int ctz32(uint32_t val)
205 #if QEMU_GNUC_PREREQ(3, 4)
206 return val ? __builtin_ctz(val) : 32;
207 #else
208 /* Binary search for the trailing one bit. */
209 int cnt;
211 cnt = 0;
212 if (!(val & 0x0000FFFFUL)) {
213 cnt += 16;
214 val >>= 16;
216 if (!(val & 0x000000FFUL)) {
217 cnt += 8;
218 val >>= 8;
220 if (!(val & 0x0000000FUL)) {
221 cnt += 4;
222 val >>= 4;
224 if (!(val & 0x00000003UL)) {
225 cnt += 2;
226 val >>= 2;
228 if (!(val & 0x00000001UL)) {
229 cnt++;
230 val >>= 1;
232 if (!(val & 0x00000001UL)) {
233 cnt++;
236 return cnt;
237 #endif
241 * cto32 - count trailing ones in a 32-bit value.
242 * @val: The value to search
244 * Returns 32 if the value is -1.
246 static inline int cto32(uint32_t val)
248 return ctz32(~val);
252 * ctz64 - count trailing zeros in a 64-bit value.
253 * @val: The value to search
255 * Returns 64 if the value is zero. Note that the GCC builtin is
256 * undefined if the value is zero.
258 static inline int ctz64(uint64_t val)
260 #if QEMU_GNUC_PREREQ(3, 4)
261 return val ? __builtin_ctzll(val) : 64;
262 #else
263 int cnt;
265 cnt = 0;
266 if (!((uint32_t)val)) {
267 cnt += 32;
268 val >>= 32;
271 return cnt + ctz32(val);
272 #endif
276 * cto64 - count trailing ones in a 64-bit value.
277 * @val: The value to search
279 * Returns 64 if the value is -1.
281 static inline int cto64(uint64_t val)
283 return ctz64(~val);
287 * clrsb32 - count leading redundant sign bits in a 32-bit value.
288 * @val: The value to search
290 * Returns the number of bits following the sign bit that are equal to it.
291 * No special cases; output range is [0-31].
293 static inline int clrsb32(uint32_t val)
295 #if QEMU_GNUC_PREREQ(4, 7)
296 return __builtin_clrsb(val);
297 #else
298 return clz32(val ^ ((int32_t)val >> 1)) - 1;
299 #endif
303 * clrsb64 - count leading redundant sign bits in a 64-bit value.
304 * @val: The value to search
306 * Returns the number of bits following the sign bit that are equal to it.
307 * No special cases; output range is [0-63].
309 static inline int clrsb64(uint64_t val)
311 #if QEMU_GNUC_PREREQ(4, 7)
312 return __builtin_clrsbll(val);
313 #else
314 return clz64(val ^ ((int64_t)val >> 1)) - 1;
315 #endif
319 * ctpop8 - count the population of one bits in an 8-bit value.
320 * @val: The value to search
322 static inline int ctpop8(uint8_t val)
324 #if QEMU_GNUC_PREREQ(3, 4)
325 return __builtin_popcount(val);
326 #else
327 val = (val & 0x55) + ((val >> 1) & 0x55);
328 val = (val & 0x33) + ((val >> 2) & 0x33);
329 val = (val & 0x0f) + ((val >> 4) & 0x0f);
331 return val;
332 #endif
336 * ctpop16 - count the population of one bits in a 16-bit value.
337 * @val: The value to search
339 static inline int ctpop16(uint16_t val)
341 #if QEMU_GNUC_PREREQ(3, 4)
342 return __builtin_popcount(val);
343 #else
344 val = (val & 0x5555) + ((val >> 1) & 0x5555);
345 val = (val & 0x3333) + ((val >> 2) & 0x3333);
346 val = (val & 0x0f0f) + ((val >> 4) & 0x0f0f);
347 val = (val & 0x00ff) + ((val >> 8) & 0x00ff);
349 return val;
350 #endif
354 * ctpop32 - count the population of one bits in a 32-bit value.
355 * @val: The value to search
357 static inline int ctpop32(uint32_t val)
359 #if QEMU_GNUC_PREREQ(3, 4)
360 return __builtin_popcount(val);
361 #else
362 val = (val & 0x55555555) + ((val >> 1) & 0x55555555);
363 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
364 val = (val & 0x0f0f0f0f) + ((val >> 4) & 0x0f0f0f0f);
365 val = (val & 0x00ff00ff) + ((val >> 8) & 0x00ff00ff);
366 val = (val & 0x0000ffff) + ((val >> 16) & 0x0000ffff);
368 return val;
369 #endif
373 * ctpop64 - count the population of one bits in a 64-bit value.
374 * @val: The value to search
376 static inline int ctpop64(uint64_t val)
378 #if QEMU_GNUC_PREREQ(3, 4)
379 return __builtin_popcountll(val);
380 #else
381 val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL);
382 val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL);
383 val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >> 4) & 0x0f0f0f0f0f0f0f0fULL);
384 val = (val & 0x00ff00ff00ff00ffULL) + ((val >> 8) & 0x00ff00ff00ff00ffULL);
385 val = (val & 0x0000ffff0000ffffULL) + ((val >> 16) & 0x0000ffff0000ffffULL);
386 val = (val & 0x00000000ffffffffULL) + ((val >> 32) & 0x00000000ffffffffULL);
388 return val;
389 #endif
393 * revbit8 - reverse the bits in an 8-bit value.
394 * @x: The value to modify.
396 static inline uint8_t revbit8(uint8_t x)
398 /* Assign the correct nibble position. */
399 x = ((x & 0xf0) >> 4)
400 | ((x & 0x0f) << 4);
401 /* Assign the correct bit position. */
402 x = ((x & 0x88) >> 3)
403 | ((x & 0x44) >> 1)
404 | ((x & 0x22) << 1)
405 | ((x & 0x11) << 3);
406 return x;
410 * revbit16 - reverse the bits in a 16-bit value.
411 * @x: The value to modify.
413 static inline uint16_t revbit16(uint16_t x)
415 /* Assign the correct byte position. */
416 x = bswap16(x);
417 /* Assign the correct nibble position. */
418 x = ((x & 0xf0f0) >> 4)
419 | ((x & 0x0f0f) << 4);
420 /* Assign the correct bit position. */
421 x = ((x & 0x8888) >> 3)
422 | ((x & 0x4444) >> 1)
423 | ((x & 0x2222) << 1)
424 | ((x & 0x1111) << 3);
425 return x;
429 * revbit32 - reverse the bits in a 32-bit value.
430 * @x: The value to modify.
432 static inline uint32_t revbit32(uint32_t x)
434 /* Assign the correct byte position. */
435 x = bswap32(x);
436 /* Assign the correct nibble position. */
437 x = ((x & 0xf0f0f0f0u) >> 4)
438 | ((x & 0x0f0f0f0fu) << 4);
439 /* Assign the correct bit position. */
440 x = ((x & 0x88888888u) >> 3)
441 | ((x & 0x44444444u) >> 1)
442 | ((x & 0x22222222u) << 1)
443 | ((x & 0x11111111u) << 3);
444 return x;
448 * revbit64 - reverse the bits in a 64-bit value.
449 * @x: The value to modify.
451 static inline uint64_t revbit64(uint64_t x)
453 /* Assign the correct byte position. */
454 x = bswap64(x);
455 /* Assign the correct nibble position. */
456 x = ((x & 0xf0f0f0f0f0f0f0f0ull) >> 4)
457 | ((x & 0x0f0f0f0f0f0f0f0full) << 4);
458 /* Assign the correct bit position. */
459 x = ((x & 0x8888888888888888ull) >> 3)
460 | ((x & 0x4444444444444444ull) >> 1)
461 | ((x & 0x2222222222222222ull) << 1)
462 | ((x & 0x1111111111111111ull) << 3);
463 return x;
466 /* Host type specific sizes of these routines. */
468 #if ULONG_MAX == UINT32_MAX
469 # define clzl clz32
470 # define ctzl ctz32
471 # define clol clo32
472 # define ctol cto32
473 # define ctpopl ctpop32
474 # define revbitl revbit32
475 #elif ULONG_MAX == UINT64_MAX
476 # define clzl clz64
477 # define ctzl ctz64
478 # define clol clo64
479 # define ctol cto64
480 # define ctpopl ctpop64
481 # define revbitl revbit64
482 #else
483 # error Unknown sizeof long
484 #endif
486 static inline bool is_power_of_2(uint64_t value)
488 if (!value) {
489 return false;
492 return !(value & (value - 1));
495 /* round down to the nearest power of 2*/
496 static inline int64_t pow2floor(int64_t value)
498 if (!is_power_of_2(value)) {
499 value = 0x8000000000000000ULL >> clz64(value);
501 return value;
504 /* round up to the nearest power of 2 (0 if overflow) */
505 static inline uint64_t pow2ceil(uint64_t value)
507 uint8_t nlz = clz64(value);
509 if (is_power_of_2(value)) {
510 return value;
512 if (!nlz) {
513 return 0;
515 return 1ULL << (64 - nlz);
518 #endif