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
28 #if defined(__x86_64__)
29 #define __HAVE_FAST_MULU64__
30 static always_inline
void mulu64 (uint64_t *plow
, uint64_t *phigh
,
31 uint64_t a
, uint64_t b
)
34 : "=d" (*phigh
), "=a" (*plow
)
37 #define __HAVE_FAST_MULS64__
38 static always_inline
void muls64 (uint64_t *plow
, uint64_t *phigh
,
41 __asm__ ("imul %0\n\t"
42 : "=d" (*phigh
), "=a" (*plow
)
46 void muls64(uint64_t *phigh
, uint64_t *plow
, int64_t a
, int64_t b
);
47 void mulu64(uint64_t *phigh
, uint64_t *plow
, uint64_t a
, uint64_t b
);
50 /* Binary search for leading zeros. */
52 static always_inline
int clz32(uint32_t val
)
54 #if QEMU_GNUC_PREREQ(3, 4)
56 return __builtin_clz(val
);
62 if (!(val
& 0xFFFF0000U
)) {
66 if (!(val
& 0xFF000000U
)) {
70 if (!(val
& 0xF0000000U
)) {
74 if (!(val
& 0xC0000000U
)) {
78 if (!(val
& 0x80000000U
)) {
82 if (!(val
& 0x80000000U
)) {
89 static always_inline
int clo32(uint32_t val
)
94 static always_inline
int clz64(uint64_t val
)
96 #if QEMU_GNUC_PREREQ(3, 4)
98 return __builtin_clzll(val
);
110 return cnt
+ clz32(val
);
114 static always_inline
int clo64(uint64_t val
)
119 static always_inline
int ctz32(uint32_t val
)
121 #if QEMU_GNUC_PREREQ(3, 4)
123 return __builtin_ctz(val
);
130 if (!(val
& 0x0000FFFFUL
)) {
134 if (!(val
& 0x000000FFUL
)) {
138 if (!(val
& 0x0000000FUL
)) {
142 if (!(val
& 0x00000003UL
)) {
146 if (!(val
& 0x00000001UL
)) {
150 if (!(val
& 0x00000001UL
)) {
158 static always_inline
int cto32(uint32_t val
)
163 static always_inline
int ctz64(uint64_t val
)
165 #if QEMU_GNUC_PREREQ(3, 4)
167 return __builtin_ctz(val
);
174 if (!((uint32_t)val
)) {
179 return cnt
+ ctz32(val
);
183 static always_inline
int cto64(uint64_t val
)
188 static always_inline
int ctpop8(uint8_t val
)
190 val
= (val
& 0x55) + ((val
>> 1) & 0x55);
191 val
= (val
& 0x33) + ((val
>> 2) & 0x33);
192 val
= (val
& 0x0f) + ((val
>> 4) & 0x0f);
197 static always_inline
int ctpop16(uint16_t val
)
199 val
= (val
& 0x5555) + ((val
>> 1) & 0x5555);
200 val
= (val
& 0x3333) + ((val
>> 2) & 0x3333);
201 val
= (val
& 0x0f0f) + ((val
>> 4) & 0x0f0f);
202 val
= (val
& 0x00ff) + ((val
>> 8) & 0x00ff);
207 static always_inline
int ctpop32(uint32_t val
)
209 #if QEMU_GNUC_PREREQ(3, 4)
210 return __builtin_popcount(val
);
212 val
= (val
& 0x55555555) + ((val
>> 1) & 0x55555555);
213 val
= (val
& 0x33333333) + ((val
>> 2) & 0x33333333);
214 val
= (val
& 0x0f0f0f0f) + ((val
>> 4) & 0x0f0f0f0f);
215 val
= (val
& 0x00ff00ff) + ((val
>> 8) & 0x00ff00ff);
216 val
= (val
& 0x0000ffff) + ((val
>> 16) & 0x0000ffff);
222 static always_inline
int ctpop64(uint64_t val
)
224 #if QEMU_GNUC_PREREQ(3, 4)
225 return __builtin_popcountll(val
);
227 val
= (val
& 0x5555555555555555ULL
) + ((val
>> 1) & 0x5555555555555555ULL
);
228 val
= (val
& 0x3333333333333333ULL
) + ((val
>> 2) & 0x3333333333333333ULL
);
229 val
= (val
& 0x0f0f0f0f0f0f0f0fULL
) + ((val
>> 4) & 0x0f0f0f0f0f0f0f0fULL
);
230 val
= (val
& 0x00ff00ff00ff00ffULL
) + ((val
>> 8) & 0x00ff00ff00ff00ffULL
);
231 val
= (val
& 0x0000ffff0000ffffULL
) + ((val
>> 16) & 0x0000ffff0000ffffULL
);
232 val
= (val
& 0x00000000ffffffffULL
) + ((val
>> 32) & 0x00000000ffffffffULL
);