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/bswap.h"
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
;
39 static inline void muls64(uint64_t *plow
, uint64_t *phigh
,
42 __int128_t r
= (__int128_t
)a
* b
;
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
)
58 __uint128_t dividend
= ((__uint128_t
)*phigh
<< 64) | *plow
;
59 __uint128_t result
= dividend
/ divisor
;
61 *phigh
= dividend
% divisor
;
62 return result
> UINT64_MAX
;
66 static inline int divs128(int64_t *plow
, int64_t *phigh
, int64_t divisor
)
71 __int128_t dividend
= ((__int128_t
)*phigh
<< 64) | *plow
;
72 __int128_t result
= dividend
/ divisor
;
74 *phigh
= dividend
% divisor
;
75 return result
!= *plow
;
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
)
89 #ifdef HOST_WORDS_BIGENDIAN
99 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
100 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
103 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
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;
120 /* Binary search for the leading one bit. */
123 if (!(val
& 0xFFFF0000U
)) {
127 if (!(val
& 0xFF000000U
)) {
131 if (!(val
& 0xF0000000U
)) {
135 if (!(val
& 0xC0000000U
)) {
139 if (!(val
& 0x80000000U
)) {
143 if (!(val
& 0x80000000U
)) {
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
)
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;
181 return cnt
+ clz32(val
);
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
)
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;
208 /* Binary search for the trailing one bit. */
212 if (!(val
& 0x0000FFFFUL
)) {
216 if (!(val
& 0x000000FFUL
)) {
220 if (!(val
& 0x0000000FUL
)) {
224 if (!(val
& 0x00000003UL
)) {
228 if (!(val
& 0x00000001UL
)) {
232 if (!(val
& 0x00000001UL
)) {
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
)
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;
266 if (!((uint32_t)val
)) {
271 return cnt
+ ctz32(val
);
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
)
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
);
298 return clz32(val
^ ((int32_t)val
>> 1)) - 1;
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
);
314 return clz64(val
^ ((int64_t)val
>> 1)) - 1;
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
);
327 val
= (val
& 0x55) + ((val
>> 1) & 0x55);
328 val
= (val
& 0x33) + ((val
>> 2) & 0x33);
329 val
= (val
& 0x0f) + ((val
>> 4) & 0x0f);
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
);
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);
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
);
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);
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
);
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
);
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)
401 /* Assign the correct bit position. */
402 x
= ((x
& 0x88) >> 3)
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. */
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);
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. */
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);
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. */
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);
466 /* Host type specific sizes of these routines. */
468 #if ULONG_MAX == UINT32_MAX
473 # define ctpopl ctpop32
474 # define revbitl revbit32
475 #elif ULONG_MAX == UINT64_MAX
480 # define ctpopl ctpop64
481 # define revbitl revbit64
483 # error Unknown sizeof long
486 static inline bool is_power_of_2(uint64_t value
)
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
);
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
)) {
515 return 1ULL << (64 - nlz
);