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 */
33 static inline void mulu64(uint64_t *plow
, uint64_t *phigh
,
34 uint64_t a
, uint64_t b
)
36 __uint128_t r
= (__uint128_t
)a
* b
;
41 static inline void muls64(uint64_t *plow
, uint64_t *phigh
,
44 __int128_t r
= (__int128_t
)a
* b
;
49 /* compute with 96 bit intermediate result: (a*b)/c */
50 static inline uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
52 return (__int128_t
)a
* b
/ c
;
55 static inline int divu128(uint64_t *plow
, uint64_t *phigh
, uint64_t divisor
)
60 __uint128_t dividend
= ((__uint128_t
)*phigh
<< 64) | *plow
;
61 __uint128_t result
= dividend
/ divisor
;
63 *phigh
= dividend
% divisor
;
64 return result
> UINT64_MAX
;
68 static inline int divs128(int64_t *plow
, int64_t *phigh
, int64_t divisor
)
73 __int128_t dividend
= ((__int128_t
)*phigh
<< 64) | *plow
;
74 __int128_t result
= dividend
/ divisor
;
76 *phigh
= dividend
% divisor
;
77 return result
!= *plow
;
81 void muls64(uint64_t *phigh
, uint64_t *plow
, int64_t a
, int64_t b
);
82 void mulu64(uint64_t *phigh
, uint64_t *plow
, uint64_t a
, uint64_t b
);
83 int divu128(uint64_t *plow
, uint64_t *phigh
, uint64_t divisor
);
84 int divs128(int64_t *plow
, int64_t *phigh
, int64_t divisor
);
86 static inline uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
91 #ifdef HOST_WORDS_BIGENDIAN
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 * clz32 - count leading zeros in a 32-bit value.
112 * @val: The value to search
114 * Returns 32 if the value is zero. Note that the GCC builtin is
115 * undefined if the value is zero.
117 static inline int clz32(uint32_t val
)
119 #if QEMU_GNUC_PREREQ(3, 4)
120 return val
? __builtin_clz(val
) : 32;
122 /* Binary search for the leading one bit. */
125 if (!(val
& 0xFFFF0000U
)) {
129 if (!(val
& 0xFF000000U
)) {
133 if (!(val
& 0xF0000000U
)) {
137 if (!(val
& 0xC0000000U
)) {
141 if (!(val
& 0x80000000U
)) {
145 if (!(val
& 0x80000000U
)) {
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 #if QEMU_GNUC_PREREQ(3, 4)
173 return val
? __builtin_clzll(val
) : 64;
183 return cnt
+ clz32(val
);
188 * clo64 - count leading ones in a 64-bit value.
189 * @val: The value to search
191 * Returns 64 if the value is -1.
193 static inline int clo64(uint64_t val
)
199 * ctz32 - count trailing zeros in a 32-bit value.
200 * @val: The value to search
202 * Returns 32 if the value is zero. Note that the GCC builtin is
203 * undefined if the value is zero.
205 static inline int ctz32(uint32_t val
)
207 #if QEMU_GNUC_PREREQ(3, 4)
208 return val
? __builtin_ctz(val
) : 32;
210 /* Binary search for the trailing one bit. */
214 if (!(val
& 0x0000FFFFUL
)) {
218 if (!(val
& 0x000000FFUL
)) {
222 if (!(val
& 0x0000000FUL
)) {
226 if (!(val
& 0x00000003UL
)) {
230 if (!(val
& 0x00000001UL
)) {
234 if (!(val
& 0x00000001UL
)) {
243 * cto32 - count trailing ones in a 32-bit value.
244 * @val: The value to search
246 * Returns 32 if the value is -1.
248 static inline int cto32(uint32_t val
)
254 * ctz64 - count trailing zeros in a 64-bit value.
255 * @val: The value to search
257 * Returns 64 if the value is zero. Note that the GCC builtin is
258 * undefined if the value is zero.
260 static inline int ctz64(uint64_t val
)
262 #if QEMU_GNUC_PREREQ(3, 4)
263 return val
? __builtin_ctzll(val
) : 64;
268 if (!((uint32_t)val
)) {
273 return cnt
+ ctz32(val
);
278 * cto64 - count trailing ones in a 64-bit value.
279 * @val: The value to search
281 * Returns 64 if the value is -1.
283 static inline int cto64(uint64_t val
)
289 * clrsb32 - count leading redundant sign bits in a 32-bit value.
290 * @val: The value to search
292 * Returns the number of bits following the sign bit that are equal to it.
293 * No special cases; output range is [0-31].
295 static inline int clrsb32(uint32_t val
)
297 #if QEMU_GNUC_PREREQ(4, 7)
298 return __builtin_clrsb(val
);
300 return clz32(val
^ ((int32_t)val
>> 1)) - 1;
305 * clrsb64 - count leading redundant sign bits in a 64-bit value.
306 * @val: The value to search
308 * Returns the number of bits following the sign bit that are equal to it.
309 * No special cases; output range is [0-63].
311 static inline int clrsb64(uint64_t val
)
313 #if QEMU_GNUC_PREREQ(4, 7)
314 return __builtin_clrsbll(val
);
316 return clz64(val
^ ((int64_t)val
>> 1)) - 1;
321 * ctpop8 - count the population of one bits in an 8-bit value.
322 * @val: The value to search
324 static inline int ctpop8(uint8_t val
)
326 #if QEMU_GNUC_PREREQ(3, 4)
327 return __builtin_popcount(val
);
329 val
= (val
& 0x55) + ((val
>> 1) & 0x55);
330 val
= (val
& 0x33) + ((val
>> 2) & 0x33);
331 val
= (val
& 0x0f) + ((val
>> 4) & 0x0f);
338 * ctpop16 - count the population of one bits in a 16-bit value.
339 * @val: The value to search
341 static inline int ctpop16(uint16_t val
)
343 #if QEMU_GNUC_PREREQ(3, 4)
344 return __builtin_popcount(val
);
346 val
= (val
& 0x5555) + ((val
>> 1) & 0x5555);
347 val
= (val
& 0x3333) + ((val
>> 2) & 0x3333);
348 val
= (val
& 0x0f0f) + ((val
>> 4) & 0x0f0f);
349 val
= (val
& 0x00ff) + ((val
>> 8) & 0x00ff);
356 * ctpop32 - count the population of one bits in a 32-bit value.
357 * @val: The value to search
359 static inline int ctpop32(uint32_t val
)
361 #if QEMU_GNUC_PREREQ(3, 4)
362 return __builtin_popcount(val
);
364 val
= (val
& 0x55555555) + ((val
>> 1) & 0x55555555);
365 val
= (val
& 0x33333333) + ((val
>> 2) & 0x33333333);
366 val
= (val
& 0x0f0f0f0f) + ((val
>> 4) & 0x0f0f0f0f);
367 val
= (val
& 0x00ff00ff) + ((val
>> 8) & 0x00ff00ff);
368 val
= (val
& 0x0000ffff) + ((val
>> 16) & 0x0000ffff);
375 * ctpop64 - count the population of one bits in a 64-bit value.
376 * @val: The value to search
378 static inline int ctpop64(uint64_t val
)
380 #if QEMU_GNUC_PREREQ(3, 4)
381 return __builtin_popcountll(val
);
383 val
= (val
& 0x5555555555555555ULL
) + ((val
>> 1) & 0x5555555555555555ULL
);
384 val
= (val
& 0x3333333333333333ULL
) + ((val
>> 2) & 0x3333333333333333ULL
);
385 val
= (val
& 0x0f0f0f0f0f0f0f0fULL
) + ((val
>> 4) & 0x0f0f0f0f0f0f0f0fULL
);
386 val
= (val
& 0x00ff00ff00ff00ffULL
) + ((val
>> 8) & 0x00ff00ff00ff00ffULL
);
387 val
= (val
& 0x0000ffff0000ffffULL
) + ((val
>> 16) & 0x0000ffff0000ffffULL
);
388 val
= (val
& 0x00000000ffffffffULL
) + ((val
>> 32) & 0x00000000ffffffffULL
);
394 /* Host type specific sizes of these routines. */
396 #if ULONG_MAX == UINT32_MAX
401 # define ctpopl ctpop32
402 #elif ULONG_MAX == UINT64_MAX
407 # define ctpopl ctpop64
409 # error Unknown sizeof long
412 static inline bool is_power_of_2(uint64_t value
)
418 return !(value
& (value
- 1));
421 /* round down to the nearest power of 2*/
422 static inline int64_t pow2floor(int64_t value
)
424 if (!is_power_of_2(value
)) {
425 value
= 0x8000000000000000ULL
>> clz64(value
);
430 /* round up to the nearest power of 2 (0 if overflow) */
431 static inline uint64_t pow2ceil(uint64_t value
)
433 uint8_t nlz
= clz64(value
);
435 if (is_power_of_2(value
)) {
441 return 1ULL << (64 - nlz
);