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 "compiler.h" /* QEMU_GNUC_PREREQ */
30 #if defined(__x86_64__)
31 #define __HAVE_FAST_MULU64__
32 static inline void mulu64(uint64_t *plow
, uint64_t *phigh
,
33 uint64_t a
, uint64_t b
)
36 : "=d" (*phigh
), "=a" (*plow
)
39 #define __HAVE_FAST_MULS64__
40 static inline void muls64(uint64_t *plow
, uint64_t *phigh
,
43 __asm__ ("imul %0\n\t"
44 : "=d" (*phigh
), "=a" (*plow
)
48 void muls64(uint64_t *phigh
, uint64_t *plow
, int64_t a
, int64_t b
);
49 void mulu64(uint64_t *phigh
, uint64_t *plow
, uint64_t a
, uint64_t b
);
52 /* Binary search for leading zeros. */
54 static inline int clz32(uint32_t val
)
56 #if QEMU_GNUC_PREREQ(3, 4)
58 return __builtin_clz(val
);
64 if (!(val
& 0xFFFF0000U
)) {
68 if (!(val
& 0xFF000000U
)) {
72 if (!(val
& 0xF0000000U
)) {
76 if (!(val
& 0xC0000000U
)) {
80 if (!(val
& 0x80000000U
)) {
84 if (!(val
& 0x80000000U
)) {
91 static inline int clo32(uint32_t val
)
96 static inline int clz64(uint64_t val
)
98 #if QEMU_GNUC_PREREQ(3, 4)
100 return __builtin_clzll(val
);
112 return cnt
+ clz32(val
);
116 static inline int clo64(uint64_t val
)
121 static inline int ctz32(uint32_t val
)
123 #if QEMU_GNUC_PREREQ(3, 4)
125 return __builtin_ctz(val
);
132 if (!(val
& 0x0000FFFFUL
)) {
136 if (!(val
& 0x000000FFUL
)) {
140 if (!(val
& 0x0000000FUL
)) {
144 if (!(val
& 0x00000003UL
)) {
148 if (!(val
& 0x00000001UL
)) {
152 if (!(val
& 0x00000001UL
)) {
160 static inline int cto32(uint32_t val
)
165 static inline int ctz64(uint64_t val
)
167 #if QEMU_GNUC_PREREQ(3, 4)
169 return __builtin_ctzll(val
);
176 if (!((uint32_t)val
)) {
181 return cnt
+ ctz32(val
);
185 static inline int cto64(uint64_t val
)
190 static inline int ctpop8(uint8_t val
)
192 val
= (val
& 0x55) + ((val
>> 1) & 0x55);
193 val
= (val
& 0x33) + ((val
>> 2) & 0x33);
194 val
= (val
& 0x0f) + ((val
>> 4) & 0x0f);
199 static inline int ctpop16(uint16_t val
)
201 val
= (val
& 0x5555) + ((val
>> 1) & 0x5555);
202 val
= (val
& 0x3333) + ((val
>> 2) & 0x3333);
203 val
= (val
& 0x0f0f) + ((val
>> 4) & 0x0f0f);
204 val
= (val
& 0x00ff) + ((val
>> 8) & 0x00ff);
209 static inline int ctpop32(uint32_t val
)
211 #if QEMU_GNUC_PREREQ(3, 4)
212 return __builtin_popcount(val
);
214 val
= (val
& 0x55555555) + ((val
>> 1) & 0x55555555);
215 val
= (val
& 0x33333333) + ((val
>> 2) & 0x33333333);
216 val
= (val
& 0x0f0f0f0f) + ((val
>> 4) & 0x0f0f0f0f);
217 val
= (val
& 0x00ff00ff) + ((val
>> 8) & 0x00ff00ff);
218 val
= (val
& 0x0000ffff) + ((val
>> 16) & 0x0000ffff);
224 static inline int ctpop64(uint64_t val
)
226 #if QEMU_GNUC_PREREQ(3, 4)
227 return __builtin_popcountll(val
);
229 val
= (val
& 0x5555555555555555ULL
) + ((val
>> 1) & 0x5555555555555555ULL
);
230 val
= (val
& 0x3333333333333333ULL
) + ((val
>> 2) & 0x3333333333333333ULL
);
231 val
= (val
& 0x0f0f0f0f0f0f0f0fULL
) + ((val
>> 4) & 0x0f0f0f0f0f0f0f0fULL
);
232 val
= (val
& 0x00ff00ff00ff00ffULL
) + ((val
>> 8) & 0x00ff00ff00ff00ffULL
);
233 val
= (val
& 0x0000ffff0000ffffULL
) + ((val
>> 16) & 0x0000ffff0000ffffULL
);
234 val
= (val
& 0x00000000ffffffffULL
) + ((val
>> 32) & 0x00000000ffffffffULL
);