]>
git.proxmox.com Git - mirror_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 /* Portions of this work are licensed under the terms of the GNU GPL,
27 * version 2 or later. See the COPYING file in the top-level directory.
33 #include "qemu/bswap.h"
34 #include "qemu/int128.h"
37 static inline void mulu64(uint64_t *plow
, uint64_t *phigh
,
38 uint64_t a
, uint64_t b
)
40 __uint128_t r
= (__uint128_t
)a
* b
;
45 static inline void muls64(uint64_t *plow
, uint64_t *phigh
,
48 __int128_t r
= (__int128_t
)a
* b
;
53 /* compute with 96 bit intermediate result: (a*b)/c */
54 static inline uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
56 return (__int128_t
)a
* b
/ c
;
59 static inline uint64_t divu128(uint64_t *plow
, uint64_t *phigh
,
62 __uint128_t dividend
= ((__uint128_t
)*phigh
<< 64) | *plow
;
63 __uint128_t result
= dividend
/ divisor
;
66 *phigh
= result
>> 64;
67 return dividend
% divisor
;
70 static inline int64_t divs128(uint64_t *plow
, int64_t *phigh
,
73 __int128_t dividend
= ((__int128_t
)*phigh
<< 64) | *plow
;
74 __int128_t result
= dividend
/ divisor
;
77 *phigh
= result
>> 64;
78 return dividend
% divisor
;
81 void muls64(uint64_t *plow
, uint64_t *phigh
, int64_t a
, int64_t b
);
82 void mulu64(uint64_t *plow
, uint64_t *phigh
, uint64_t a
, uint64_t b
);
83 uint64_t divu128(uint64_t *plow
, uint64_t *phigh
, uint64_t divisor
);
84 int64_t divs128(uint64_t *plow
, int64_t *phigh
, int64_t divisor
);
86 static inline uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
101 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
102 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
105 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
111 * clz8 - count leading zeros in a 8-bit value.
112 * @val: The value to search
114 * Returns 8 if the value is zero. Note that the GCC builtin is
115 * undefined if the value is zero.
117 * Note that the GCC builtin will upcast its argument to an `unsigned int`
118 * so this function subtracts off the number of prepended zeroes.
120 static inline int clz8(uint8_t val
)
122 return val
? __builtin_clz(val
) - 24 : 8;
126 * clz16 - count leading zeros in a 16-bit value.
127 * @val: The value to search
129 * Returns 16 if the value is zero. Note that the GCC builtin is
130 * undefined if the value is zero.
132 * Note that the GCC builtin will upcast its argument to an `unsigned int`
133 * so this function subtracts off the number of prepended zeroes.
135 static inline int clz16(uint16_t val
)
137 return val
? __builtin_clz(val
) - 16 : 16;
141 * clz32 - count leading 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 clz32(uint32_t val
)
149 return val
? __builtin_clz(val
) : 32;
153 * clo32 - count leading ones in a 32-bit value.
154 * @val: The value to search
156 * Returns 32 if the value is -1.
158 static inline int clo32(uint32_t val
)
164 * clz64 - count leading zeros in a 64-bit value.
165 * @val: The value to search
167 * Returns 64 if the value is zero. Note that the GCC builtin is
168 * undefined if the value is zero.
170 static inline int clz64(uint64_t val
)
172 return val
? __builtin_clzll(val
) : 64;
176 * clo64 - count leading ones in a 64-bit value.
177 * @val: The value to search
179 * Returns 64 if the value is -1.
181 static inline int clo64(uint64_t val
)
187 * ctz8 - count trailing zeros in a 8-bit value.
188 * @val: The value to search
190 * Returns 8 if the value is zero. Note that the GCC builtin is
191 * undefined if the value is zero.
193 static inline int ctz8(uint8_t val
)
195 return val
? __builtin_ctz(val
) : 8;
199 * ctz16 - count trailing zeros in a 16-bit value.
200 * @val: The value to search
202 * Returns 16 if the value is zero. Note that the GCC builtin is
203 * undefined if the value is zero.
205 static inline int ctz16(uint16_t val
)
207 return val
? __builtin_ctz(val
) : 16;
211 * ctz32 - count trailing zeros in a 32-bit value.
212 * @val: The value to search
214 * Returns 32 if the value is zero. Note that the GCC builtin is
215 * undefined if the value is zero.
217 static inline int ctz32(uint32_t val
)
219 return val
? __builtin_ctz(val
) : 32;
223 * cto32 - count trailing ones in a 32-bit value.
224 * @val: The value to search
226 * Returns 32 if the value is -1.
228 static inline int cto32(uint32_t val
)
234 * ctz64 - count trailing zeros in a 64-bit value.
235 * @val: The value to search
237 * Returns 64 if the value is zero. Note that the GCC builtin is
238 * undefined if the value is zero.
240 static inline int ctz64(uint64_t val
)
242 return val
? __builtin_ctzll(val
) : 64;
246 * cto64 - count trailing ones in a 64-bit value.
247 * @val: The value to search
249 * Returns 64 if the value is -1.
251 static inline int cto64(uint64_t val
)
257 * clrsb32 - count leading redundant sign bits in a 32-bit value.
258 * @val: The value to search
260 * Returns the number of bits following the sign bit that are equal to it.
261 * No special cases; output range is [0-31].
263 static inline int clrsb32(uint32_t val
)
265 #if __has_builtin(__builtin_clrsb) || !defined(__clang__)
266 return __builtin_clrsb(val
);
268 return clz32(val
^ ((int32_t)val
>> 1)) - 1;
273 * clrsb64 - count leading redundant sign bits in a 64-bit value.
274 * @val: The value to search
276 * Returns the number of bits following the sign bit that are equal to it.
277 * No special cases; output range is [0-63].
279 static inline int clrsb64(uint64_t val
)
281 #if __has_builtin(__builtin_clrsbll) || !defined(__clang__)
282 return __builtin_clrsbll(val
);
284 return clz64(val
^ ((int64_t)val
>> 1)) - 1;
289 * ctpop8 - count the population of one bits in an 8-bit value.
290 * @val: The value to search
292 static inline int ctpop8(uint8_t val
)
294 return __builtin_popcount(val
);
298 * ctpop16 - count the population of one bits in a 16-bit value.
299 * @val: The value to search
301 static inline int ctpop16(uint16_t val
)
303 return __builtin_popcount(val
);
307 * ctpop32 - count the population of one bits in a 32-bit value.
308 * @val: The value to search
310 static inline int ctpop32(uint32_t val
)
312 return __builtin_popcount(val
);
316 * ctpop64 - count the population of one bits in a 64-bit value.
317 * @val: The value to search
319 static inline int ctpop64(uint64_t val
)
321 return __builtin_popcountll(val
);
325 * revbit8 - reverse the bits in an 8-bit value.
326 * @x: The value to modify.
328 static inline uint8_t revbit8(uint8_t x
)
330 #if __has_builtin(__builtin_bitreverse8)
331 return __builtin_bitreverse8(x
);
333 /* Assign the correct nibble position. */
334 x
= ((x
& 0xf0) >> 4)
336 /* Assign the correct bit position. */
337 x
= ((x
& 0x88) >> 3)
346 * revbit16 - reverse the bits in a 16-bit value.
347 * @x: The value to modify.
349 static inline uint16_t revbit16(uint16_t x
)
351 #if __has_builtin(__builtin_bitreverse16)
352 return __builtin_bitreverse16(x
);
354 /* Assign the correct byte position. */
356 /* Assign the correct nibble position. */
357 x
= ((x
& 0xf0f0) >> 4)
358 | ((x
& 0x0f0f) << 4);
359 /* Assign the correct bit position. */
360 x
= ((x
& 0x8888) >> 3)
361 | ((x
& 0x4444) >> 1)
362 | ((x
& 0x2222) << 1)
363 | ((x
& 0x1111) << 3);
369 * revbit32 - reverse the bits in a 32-bit value.
370 * @x: The value to modify.
372 static inline uint32_t revbit32(uint32_t x
)
374 #if __has_builtin(__builtin_bitreverse32)
375 return __builtin_bitreverse32(x
);
377 /* Assign the correct byte position. */
379 /* Assign the correct nibble position. */
380 x
= ((x
& 0xf0f0f0f0u
) >> 4)
381 | ((x
& 0x0f0f0f0fu
) << 4);
382 /* Assign the correct bit position. */
383 x
= ((x
& 0x88888888u
) >> 3)
384 | ((x
& 0x44444444u
) >> 1)
385 | ((x
& 0x22222222u
) << 1)
386 | ((x
& 0x11111111u
) << 3);
392 * revbit64 - reverse the bits in a 64-bit value.
393 * @x: The value to modify.
395 static inline uint64_t revbit64(uint64_t x
)
397 #if __has_builtin(__builtin_bitreverse64)
398 return __builtin_bitreverse64(x
);
400 /* Assign the correct byte position. */
402 /* Assign the correct nibble position. */
403 x
= ((x
& 0xf0f0f0f0f0f0f0f0ull
) >> 4)
404 | ((x
& 0x0f0f0f0f0f0f0f0full
) << 4);
405 /* Assign the correct bit position. */
406 x
= ((x
& 0x8888888888888888ull
) >> 3)
407 | ((x
& 0x4444444444444444ull
) >> 1)
408 | ((x
& 0x2222222222222222ull
) << 1)
409 | ((x
& 0x1111111111111111ull
) << 3);
415 * Return the absolute value of a 64-bit integer as an unsigned 64-bit value
417 static inline uint64_t uabs64(int64_t v
)
419 return v
< 0 ? -v
: v
;
423 * sadd32_overflow - addition with overflow indication
425 * @ret: Output for sum
427 * Computes *@ret = @x + @y, and returns true if and only if that
428 * value has been truncated.
430 static inline bool sadd32_overflow(int32_t x
, int32_t y
, int32_t *ret
)
432 return __builtin_add_overflow(x
, y
, ret
);
436 * sadd64_overflow - addition with overflow indication
438 * @ret: Output for sum
440 * Computes *@ret = @x + @y, and returns true if and only if that
441 * value has been truncated.
443 static inline bool sadd64_overflow(int64_t x
, int64_t y
, int64_t *ret
)
445 return __builtin_add_overflow(x
, y
, ret
);
449 * uadd32_overflow - addition with overflow indication
451 * @ret: Output for sum
453 * Computes *@ret = @x + @y, and returns true if and only if that
454 * value has been truncated.
456 static inline bool uadd32_overflow(uint32_t x
, uint32_t y
, uint32_t *ret
)
458 return __builtin_add_overflow(x
, y
, ret
);
462 * uadd64_overflow - addition with overflow indication
464 * @ret: Output for sum
466 * Computes *@ret = @x + @y, and returns true if and only if that
467 * value has been truncated.
469 static inline bool uadd64_overflow(uint64_t x
, uint64_t y
, uint64_t *ret
)
471 return __builtin_add_overflow(x
, y
, ret
);
475 * ssub32_overflow - subtraction with overflow indication
478 * @ret: Output for difference
480 * Computes *@ret = @x - @y, and returns true if and only if that
481 * value has been truncated.
483 static inline bool ssub32_overflow(int32_t x
, int32_t y
, int32_t *ret
)
485 return __builtin_sub_overflow(x
, y
, ret
);
489 * ssub64_overflow - subtraction with overflow indication
492 * @ret: Output for sum
494 * Computes *@ret = @x - @y, and returns true if and only if that
495 * value has been truncated.
497 static inline bool ssub64_overflow(int64_t x
, int64_t y
, int64_t *ret
)
499 return __builtin_sub_overflow(x
, y
, ret
);
503 * usub32_overflow - subtraction with overflow indication
506 * @ret: Output for sum
508 * Computes *@ret = @x - @y, and returns true if and only if that
509 * value has been truncated.
511 static inline bool usub32_overflow(uint32_t x
, uint32_t y
, uint32_t *ret
)
513 return __builtin_sub_overflow(x
, y
, ret
);
517 * usub64_overflow - subtraction with overflow indication
520 * @ret: Output for sum
522 * Computes *@ret = @x - @y, and returns true if and only if that
523 * value has been truncated.
525 static inline bool usub64_overflow(uint64_t x
, uint64_t y
, uint64_t *ret
)
527 return __builtin_sub_overflow(x
, y
, ret
);
531 * smul32_overflow - multiplication with overflow indication
532 * @x, @y: Input multipliers
533 * @ret: Output for product
535 * Computes *@ret = @x * @y, and returns true if and only if that
536 * value has been truncated.
538 static inline bool smul32_overflow(int32_t x
, int32_t y
, int32_t *ret
)
540 return __builtin_mul_overflow(x
, y
, ret
);
544 * smul64_overflow - multiplication with overflow indication
545 * @x, @y: Input multipliers
546 * @ret: Output for product
548 * Computes *@ret = @x * @y, and returns true if and only if that
549 * value has been truncated.
551 static inline bool smul64_overflow(int64_t x
, int64_t y
, int64_t *ret
)
553 return __builtin_mul_overflow(x
, y
, ret
);
557 * umul32_overflow - multiplication with overflow indication
558 * @x, @y: Input multipliers
559 * @ret: Output for product
561 * Computes *@ret = @x * @y, and returns true if and only if that
562 * value has been truncated.
564 static inline bool umul32_overflow(uint32_t x
, uint32_t y
, uint32_t *ret
)
566 return __builtin_mul_overflow(x
, y
, ret
);
570 * umul64_overflow - multiplication with overflow indication
571 * @x, @y: Input multipliers
572 * @ret: Output for product
574 * Computes *@ret = @x * @y, and returns true if and only if that
575 * value has been truncated.
577 static inline bool umul64_overflow(uint64_t x
, uint64_t y
, uint64_t *ret
)
579 return __builtin_mul_overflow(x
, y
, ret
);
583 * Unsigned 128x64 multiplication.
584 * Returns true if the result got truncated to 128 bits.
585 * Otherwise, returns false and the multiplication result via plow and phigh.
587 static inline bool mulu128(uint64_t *plow
, uint64_t *phigh
, uint64_t factor
)
589 #if defined(CONFIG_INT128)
592 __uint128_t f
= ((__uint128_t
)*phigh
<< 64) | *plow
;
593 res
= __builtin_mul_overflow(f
, factor
, &r
);
600 uint64_t dhi
= *phigh
;
601 uint64_t dlo
= *plow
;
606 mulu64(plow
, phigh
, dlo
, factor
);
610 mulu64(plow
, &ahi
, dlo
, factor
);
611 mulu64(&blo
, &bhi
, dhi
, factor
);
613 return uadd64_overflow(ahi
, blo
, phigh
) || bhi
!= 0;
618 * uadd64_carry - addition with carry-in and carry-out
620 * @pcarry: in-out carry value
622 * Computes @x + @y + *@pcarry, placing the carry-out back
623 * into *@pcarry and returning the 64-bit sum.
625 static inline uint64_t uadd64_carry(uint64_t x
, uint64_t y
, bool *pcarry
)
627 #if __has_builtin(__builtin_addcll)
628 unsigned long long c
= *pcarry
;
629 x
= __builtin_addcll(x
, y
, c
, &c
);
634 /* This is clang's internal expansion of __builtin_addc. */
635 c
= uadd64_overflow(x
, c
, &x
);
636 c
|= uadd64_overflow(x
, y
, &x
);
643 * usub64_borrow - subtraction with borrow-in and borrow-out
645 * @pborrow: in-out borrow value
647 * Computes @x - @y - *@pborrow, placing the borrow-out back
648 * into *@pborrow and returning the 64-bit sum.
650 static inline uint64_t usub64_borrow(uint64_t x
, uint64_t y
, bool *pborrow
)
652 #if __has_builtin(__builtin_subcll) && !defined(BUILTIN_SUBCLL_BROKEN)
653 unsigned long long b
= *pborrow
;
654 x
= __builtin_subcll(x
, y
, b
, &b
);
659 b
= usub64_overflow(x
, b
, &x
);
660 b
|= usub64_overflow(x
, y
, &x
);
666 /* Host type specific sizes of these routines. */
668 #if ULONG_MAX == UINT32_MAX
673 # define ctpopl ctpop32
674 # define revbitl revbit32
675 #elif ULONG_MAX == UINT64_MAX
680 # define ctpopl ctpop64
681 # define revbitl revbit64
683 # error Unknown sizeof long
686 static inline bool is_power_of_2(uint64_t value
)
692 return !(value
& (value
- 1));
696 * Return @value rounded down to the nearest power of two or zero.
698 static inline uint64_t pow2floor(uint64_t value
)
701 /* Avoid undefined shift by 64 */
704 return 0x8000000000000000ull
>> clz64(value
);
708 * Return @value rounded up to the nearest power of two modulo 2^64.
709 * This is *zero* for @value > 2^63, so be careful.
711 static inline uint64_t pow2ceil(uint64_t value
)
713 int n
= clz64(value
- 1);
717 * @value - 1 has no leading zeroes, thus @value - 1 >= 2^63
718 * Therefore, either @value == 0 or @value > 2^63.
719 * If it's 0, return 1, else return 0.
723 return 0x8000000000000000ull
>> (n
- 1);
726 static inline uint32_t pow2roundup32(uint32_t x
)
737 * urshift - 128-bit Unsigned Right Shift.
738 * @plow: in/out - lower 64-bit integer.
739 * @phigh: in/out - higher 64-bit integer.
740 * @shift: in - bytes to shift, between 0 and 127.
742 * Result is zero-extended and stored in plow/phigh, which are
743 * input/output variables. Shift values outside the range will
744 * be mod to 128. In other words, the caller is responsible to
745 * verify/assert both the shift range and plow/phigh pointers.
747 void urshift(uint64_t *plow
, uint64_t *phigh
, int32_t shift
);
750 * ulshift - 128-bit Unsigned Left Shift.
751 * @plow: in/out - lower 64-bit integer.
752 * @phigh: in/out - higher 64-bit integer.
753 * @shift: in - bytes to shift, between 0 and 127.
754 * @overflow: out - true if any 1-bit is shifted out.
756 * Result is zero-extended and stored in plow/phigh, which are
757 * input/output variables. Shift values outside the range will
758 * be mod to 128. In other words, the caller is responsible to
759 * verify/assert both the shift range and plow/phigh pointers.
761 void ulshift(uint64_t *plow
, uint64_t *phigh
, int32_t shift
, bool *overflow
);
763 /* From the GNU Multi Precision Library - longlong.h __udiv_qrnnd
764 * (https://gmplib.org/repo/gmp/file/tip/longlong.h)
766 * Licensed under the GPLv2/LGPLv3
768 static inline uint64_t udiv_qrnnd(uint64_t *r
, uint64_t n1
,
769 uint64_t n0
, uint64_t d
)
771 #if defined(__x86_64__)
773 asm("divq %4" : "=a"(q
), "=d"(*r
) : "0"(n0
), "1"(n1
), "rm"(d
));
775 #elif defined(__s390x__) && !defined(__clang__)
776 /* Need to use a TImode type to get an even register pair for DLGR. */
777 unsigned __int128 n
= (unsigned __int128
)n1
<< 64 | n0
;
778 asm("dlgr %0, %1" : "+r"(n
) : "r"(d
));
781 #elif defined(_ARCH_PPC64) && defined(_ARCH_PWR7)
782 /* From Power ISA 2.06, programming note for divdeu. */
783 uint64_t q1
, q2
, Q
, r1
, r2
, R
;
784 asm("divdeu %0,%2,%4; divdu %1,%3,%4"
785 : "=&r"(q1
), "=r"(q2
)
786 : "r"(n1
), "r"(n0
), "r"(d
));
787 r1
= -(q1
* d
); /* low part of (n1<<64) - (q1 * d) */
791 if (R
>= d
|| R
< r2
) { /* overflow implies R > d */
798 uint64_t d0
, d1
, q0
, q1
, r1
, r0
, m
;
806 r1
= (r1
<< 32) | (n0
>> 32);
822 r0
= (r0
<< 32) | (uint32_t)n0
;
836 return (q1
<< 32) | q0
;
840 Int128
divu256(Int128
*plow
, Int128
*phigh
, Int128 divisor
);
841 Int128
divs256(Int128
*plow
, Int128
*phigh
, Int128 divisor
);