blob: a5f8464fb2baf007b899d5a18682e1263026c716 [file] [log] [blame]
ths05f778c2007-10-27 13:05:54 +00001/*
2 * Utility compute operations used by translated code.
3 *
4 * Copyright (c) 2007 Thiemo Seufer
5 * Copyright (c) 2007 Jocelyn Mayer
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * THE SOFTWARE.
24 */
Paolo Bonzinicb9c3772012-12-06 12:15:58 +010025#ifndef HOST_UTILS_H
26#define HOST_UTILS_H 1
ths05f778c2007-10-27 13:05:54 +000027
Stefan Weilf8b72752011-09-16 22:03:07 +020028#include "compiler.h" /* QEMU_GNUC_PREREQ */
thscebdff72008-06-05 22:55:54 +000029
j_mayer7a51ad82007-11-04 02:24:58 +000030#if defined(__x86_64__)
31#define __HAVE_FAST_MULU64__
Blue Swirlfacd2852009-08-16 08:03:26 +000032static inline void mulu64(uint64_t *plow, uint64_t *phigh,
33 uint64_t a, uint64_t b)
j_mayer7a51ad82007-11-04 02:24:58 +000034{
35 __asm__ ("mul %0\n\t"
36 : "=d" (*phigh), "=a" (*plow)
37 : "a" (a), "0" (b));
38}
39#define __HAVE_FAST_MULS64__
Blue Swirlfacd2852009-08-16 08:03:26 +000040static inline void muls64(uint64_t *plow, uint64_t *phigh,
41 int64_t a, int64_t b)
j_mayer7a51ad82007-11-04 02:24:58 +000042{
43 __asm__ ("imul %0\n\t"
44 : "=d" (*phigh), "=a" (*plow)
45 : "a" (a), "0" (b));
46}
47#else
j_mayer05e1d832007-11-05 13:16:23 +000048void muls64(uint64_t *phigh, uint64_t *plow, int64_t a, int64_t b);
j_mayer7a51ad82007-11-04 02:24:58 +000049void mulu64(uint64_t *phigh, uint64_t *plow, uint64_t a, uint64_t b);
50#endif
51
ths05f778c2007-10-27 13:05:54 +000052/* Binary search for leading zeros. */
53
Blue Swirlfacd2852009-08-16 08:03:26 +000054static inline int clz32(uint32_t val)
ths05f778c2007-10-27 13:05:54 +000055{
aurel32bad5b1e2008-10-12 16:15:04 +000056#if QEMU_GNUC_PREREQ(3, 4)
aurel327d019982008-10-12 00:53:08 +000057 if (val)
58 return __builtin_clz(val);
59 else
60 return 32;
61#else
ths05f778c2007-10-27 13:05:54 +000062 int cnt = 0;
63
64 if (!(val & 0xFFFF0000U)) {
65 cnt += 16;
66 val <<= 16;
67 }
68 if (!(val & 0xFF000000U)) {
69 cnt += 8;
70 val <<= 8;
71 }
72 if (!(val & 0xF0000000U)) {
73 cnt += 4;
74 val <<= 4;
75 }
76 if (!(val & 0xC0000000U)) {
77 cnt += 2;
78 val <<= 2;
79 }
80 if (!(val & 0x80000000U)) {
81 cnt++;
82 val <<= 1;
83 }
84 if (!(val & 0x80000000U)) {
85 cnt++;
86 }
87 return cnt;
aurel327d019982008-10-12 00:53:08 +000088#endif
ths05f778c2007-10-27 13:05:54 +000089}
90
Blue Swirlfacd2852009-08-16 08:03:26 +000091static inline int clo32(uint32_t val)
ths05f778c2007-10-27 13:05:54 +000092{
93 return clz32(~val);
94}
95
Blue Swirlfacd2852009-08-16 08:03:26 +000096static inline int clz64(uint64_t val)
ths05f778c2007-10-27 13:05:54 +000097{
aurel32bad5b1e2008-10-12 16:15:04 +000098#if QEMU_GNUC_PREREQ(3, 4)
aurel327d019982008-10-12 00:53:08 +000099 if (val)
100 return __builtin_clzll(val);
101 else
102 return 64;
103#else
ths05f778c2007-10-27 13:05:54 +0000104 int cnt = 0;
105
j_mayer7a51ad82007-11-04 02:24:58 +0000106 if (!(val >> 32)) {
ths05f778c2007-10-27 13:05:54 +0000107 cnt += 32;
j_mayer7a51ad82007-11-04 02:24:58 +0000108 } else {
109 val >>= 32;
ths05f778c2007-10-27 13:05:54 +0000110 }
j_mayer7a51ad82007-11-04 02:24:58 +0000111
112 return cnt + clz32(val);
aurel327d019982008-10-12 00:53:08 +0000113#endif
ths05f778c2007-10-27 13:05:54 +0000114}
115
Blue Swirlfacd2852009-08-16 08:03:26 +0000116static inline int clo64(uint64_t val)
ths05f778c2007-10-27 13:05:54 +0000117{
118 return clz64(~val);
119}
j_mayerb9ef45f2007-10-28 12:52:38 +0000120
Blue Swirlfacd2852009-08-16 08:03:26 +0000121static inline int ctz32(uint32_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000122{
aurel32bad5b1e2008-10-12 16:15:04 +0000123#if QEMU_GNUC_PREREQ(3, 4)
aurel327d019982008-10-12 00:53:08 +0000124 if (val)
125 return __builtin_ctz(val);
126 else
127 return 32;
128#else
j_mayerb9ef45f2007-10-28 12:52:38 +0000129 int cnt;
130
131 cnt = 0;
132 if (!(val & 0x0000FFFFUL)) {
balrogc8906842008-11-12 17:18:41 +0000133 cnt += 16;
j_mayerb9ef45f2007-10-28 12:52:38 +0000134 val >>= 16;
balrogc8906842008-11-12 17:18:41 +0000135 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000136 if (!(val & 0x000000FFUL)) {
balrogc8906842008-11-12 17:18:41 +0000137 cnt += 8;
j_mayerb9ef45f2007-10-28 12:52:38 +0000138 val >>= 8;
balrogc8906842008-11-12 17:18:41 +0000139 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000140 if (!(val & 0x0000000FUL)) {
balrogc8906842008-11-12 17:18:41 +0000141 cnt += 4;
j_mayerb9ef45f2007-10-28 12:52:38 +0000142 val >>= 4;
balrogc8906842008-11-12 17:18:41 +0000143 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000144 if (!(val & 0x00000003UL)) {
balrogc8906842008-11-12 17:18:41 +0000145 cnt += 2;
j_mayerb9ef45f2007-10-28 12:52:38 +0000146 val >>= 2;
balrogc8906842008-11-12 17:18:41 +0000147 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000148 if (!(val & 0x00000001UL)) {
balrogc8906842008-11-12 17:18:41 +0000149 cnt++;
j_mayerb9ef45f2007-10-28 12:52:38 +0000150 val >>= 1;
balrogc8906842008-11-12 17:18:41 +0000151 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000152 if (!(val & 0x00000001UL)) {
balrogc8906842008-11-12 17:18:41 +0000153 cnt++;
154 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000155
balrogc8906842008-11-12 17:18:41 +0000156 return cnt;
aurel327d019982008-10-12 00:53:08 +0000157#endif
balrogc8906842008-11-12 17:18:41 +0000158}
159
Blue Swirlfacd2852009-08-16 08:03:26 +0000160static inline int cto32(uint32_t val)
balrogc8906842008-11-12 17:18:41 +0000161{
j_mayerb9ef45f2007-10-28 12:52:38 +0000162 return ctz32(~val);
163}
164
Blue Swirlfacd2852009-08-16 08:03:26 +0000165static inline int ctz64(uint64_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000166{
aurel32bad5b1e2008-10-12 16:15:04 +0000167#if QEMU_GNUC_PREREQ(3, 4)
aurel327d019982008-10-12 00:53:08 +0000168 if (val)
Richard Henderson06445242009-12-13 17:47:25 -0800169 return __builtin_ctzll(val);
aurel327d019982008-10-12 00:53:08 +0000170 else
171 return 64;
172#else
j_mayerb9ef45f2007-10-28 12:52:38 +0000173 int cnt;
174
175 cnt = 0;
176 if (!((uint32_t)val)) {
177 cnt += 32;
178 val >>= 32;
179 }
180
181 return cnt + ctz32(val);
aurel327d019982008-10-12 00:53:08 +0000182#endif
j_mayerb9ef45f2007-10-28 12:52:38 +0000183}
184
Blue Swirlfacd2852009-08-16 08:03:26 +0000185static inline int cto64(uint64_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000186{
187 return ctz64(~val);
188}
189
Blue Swirlfacd2852009-08-16 08:03:26 +0000190static inline int ctpop8(uint8_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000191{
192 val = (val & 0x55) + ((val >> 1) & 0x55);
193 val = (val & 0x33) + ((val >> 2) & 0x33);
194 val = (val & 0x0f) + ((val >> 4) & 0x0f);
195
196 return val;
197}
198
Blue Swirlfacd2852009-08-16 08:03:26 +0000199static inline int ctpop16(uint16_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000200{
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);
205
206 return val;
207}
208
Blue Swirlfacd2852009-08-16 08:03:26 +0000209static inline int ctpop32(uint32_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000210{
aurel32bad5b1e2008-10-12 16:15:04 +0000211#if QEMU_GNUC_PREREQ(3, 4)
aurel327d019982008-10-12 00:53:08 +0000212 return __builtin_popcount(val);
213#else
j_mayerb9ef45f2007-10-28 12:52:38 +0000214 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);
219
220 return val;
aurel327d019982008-10-12 00:53:08 +0000221#endif
j_mayerb9ef45f2007-10-28 12:52:38 +0000222}
223
Blue Swirlfacd2852009-08-16 08:03:26 +0000224static inline int ctpop64(uint64_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000225{
aurel32bad5b1e2008-10-12 16:15:04 +0000226#if QEMU_GNUC_PREREQ(3, 4)
aurel327d019982008-10-12 00:53:08 +0000227 return __builtin_popcountll(val);
228#else
j_mayerb9ef45f2007-10-28 12:52:38 +0000229 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);
235
236 return val;
aurel327d019982008-10-12 00:53:08 +0000237#endif
ths3800af92007-12-18 01:58:05 +0000238}
Paolo Bonzinicb9c3772012-12-06 12:15:58 +0100239
240#endif