blob: 0f688c1c00a792c53fe41a0e32539dac68501cb6 [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
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/compiler.h" /* QEMU_GNUC_PREREQ */
Richard Henderson01654372013-02-13 17:47:34 -080029#include <limits.h>
thscebdff72008-06-05 22:55:54 +000030
Richard Hendersonf5401662013-02-16 12:46:59 -080031#ifdef CONFIG_INT128
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{
Richard Hendersonf5401662013-02-16 12:46:59 -080035 __uint128_t r = (__uint128_t)a * b;
36 *plow = r;
37 *phigh = r >> 64;
j_mayer7a51ad82007-11-04 02:24:58 +000038}
Richard Hendersonf5401662013-02-16 12:46:59 -080039
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{
Richard Hendersonf5401662013-02-16 12:46:59 -080043 __int128_t r = (__int128_t)a * b;
44 *plow = r;
45 *phigh = r >> 64;
j_mayer7a51ad82007-11-04 02:24:58 +000046}
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
Richard Henderson72d81152013-02-13 17:47:35 -080052/**
53 * clz32 - count leading zeros in a 32-bit value.
54 * @val: The value to search
55 *
56 * Returns 32 if the value is zero. Note that the GCC builtin is
57 * undefined if the value is zero.
58 */
Blue Swirlfacd2852009-08-16 08:03:26 +000059static inline int clz32(uint32_t val)
ths05f778c2007-10-27 13:05:54 +000060{
aurel32bad5b1e2008-10-12 16:15:04 +000061#if QEMU_GNUC_PREREQ(3, 4)
Richard Henderson72d81152013-02-13 17:47:35 -080062 return val ? __builtin_clz(val) : 32;
aurel327d019982008-10-12 00:53:08 +000063#else
Richard Henderson72d81152013-02-13 17:47:35 -080064 /* Binary search for the leading one bit. */
ths05f778c2007-10-27 13:05:54 +000065 int cnt = 0;
66
67 if (!(val & 0xFFFF0000U)) {
68 cnt += 16;
69 val <<= 16;
70 }
71 if (!(val & 0xFF000000U)) {
72 cnt += 8;
73 val <<= 8;
74 }
75 if (!(val & 0xF0000000U)) {
76 cnt += 4;
77 val <<= 4;
78 }
79 if (!(val & 0xC0000000U)) {
80 cnt += 2;
81 val <<= 2;
82 }
83 if (!(val & 0x80000000U)) {
84 cnt++;
85 val <<= 1;
86 }
87 if (!(val & 0x80000000U)) {
88 cnt++;
89 }
90 return cnt;
aurel327d019982008-10-12 00:53:08 +000091#endif
ths05f778c2007-10-27 13:05:54 +000092}
93
Richard Henderson72d81152013-02-13 17:47:35 -080094/**
95 * clo32 - count leading ones in a 32-bit value.
96 * @val: The value to search
97 *
98 * Returns 32 if the value is -1.
99 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000100static inline int clo32(uint32_t val)
ths05f778c2007-10-27 13:05:54 +0000101{
102 return clz32(~val);
103}
104
Richard Henderson72d81152013-02-13 17:47:35 -0800105/**
106 * clz64 - count leading zeros in a 64-bit value.
107 * @val: The value to search
108 *
109 * Returns 64 if the value is zero. Note that the GCC builtin is
110 * undefined if the value is zero.
111 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000112static inline int clz64(uint64_t val)
ths05f778c2007-10-27 13:05:54 +0000113{
aurel32bad5b1e2008-10-12 16:15:04 +0000114#if QEMU_GNUC_PREREQ(3, 4)
Richard Henderson72d81152013-02-13 17:47:35 -0800115 return val ? __builtin_clzll(val) : 64;
aurel327d019982008-10-12 00:53:08 +0000116#else
ths05f778c2007-10-27 13:05:54 +0000117 int cnt = 0;
118
j_mayer7a51ad82007-11-04 02:24:58 +0000119 if (!(val >> 32)) {
ths05f778c2007-10-27 13:05:54 +0000120 cnt += 32;
j_mayer7a51ad82007-11-04 02:24:58 +0000121 } else {
122 val >>= 32;
ths05f778c2007-10-27 13:05:54 +0000123 }
j_mayer7a51ad82007-11-04 02:24:58 +0000124
125 return cnt + clz32(val);
aurel327d019982008-10-12 00:53:08 +0000126#endif
ths05f778c2007-10-27 13:05:54 +0000127}
128
Richard Henderson72d81152013-02-13 17:47:35 -0800129/**
130 * clo64 - count leading ones in a 64-bit value.
131 * @val: The value to search
132 *
133 * Returns 64 if the value is -1.
134 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000135static inline int clo64(uint64_t val)
ths05f778c2007-10-27 13:05:54 +0000136{
137 return clz64(~val);
138}
j_mayerb9ef45f2007-10-28 12:52:38 +0000139
Richard Henderson72d81152013-02-13 17:47:35 -0800140/**
141 * ctz32 - count trailing zeros in a 32-bit value.
142 * @val: The value to search
143 *
144 * Returns 32 if the value is zero. Note that the GCC builtin is
145 * undefined if the value is zero.
146 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000147static inline int ctz32(uint32_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000148{
aurel32bad5b1e2008-10-12 16:15:04 +0000149#if QEMU_GNUC_PREREQ(3, 4)
Richard Henderson72d81152013-02-13 17:47:35 -0800150 return val ? __builtin_ctz(val) : 32;
aurel327d019982008-10-12 00:53:08 +0000151#else
Richard Henderson72d81152013-02-13 17:47:35 -0800152 /* Binary search for the trailing one bit. */
j_mayerb9ef45f2007-10-28 12:52:38 +0000153 int cnt;
154
155 cnt = 0;
156 if (!(val & 0x0000FFFFUL)) {
balrogc8906842008-11-12 17:18:41 +0000157 cnt += 16;
j_mayerb9ef45f2007-10-28 12:52:38 +0000158 val >>= 16;
balrogc8906842008-11-12 17:18:41 +0000159 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000160 if (!(val & 0x000000FFUL)) {
balrogc8906842008-11-12 17:18:41 +0000161 cnt += 8;
j_mayerb9ef45f2007-10-28 12:52:38 +0000162 val >>= 8;
balrogc8906842008-11-12 17:18:41 +0000163 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000164 if (!(val & 0x0000000FUL)) {
balrogc8906842008-11-12 17:18:41 +0000165 cnt += 4;
j_mayerb9ef45f2007-10-28 12:52:38 +0000166 val >>= 4;
balrogc8906842008-11-12 17:18:41 +0000167 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000168 if (!(val & 0x00000003UL)) {
balrogc8906842008-11-12 17:18:41 +0000169 cnt += 2;
j_mayerb9ef45f2007-10-28 12:52:38 +0000170 val >>= 2;
balrogc8906842008-11-12 17:18:41 +0000171 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000172 if (!(val & 0x00000001UL)) {
balrogc8906842008-11-12 17:18:41 +0000173 cnt++;
j_mayerb9ef45f2007-10-28 12:52:38 +0000174 val >>= 1;
balrogc8906842008-11-12 17:18:41 +0000175 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000176 if (!(val & 0x00000001UL)) {
balrogc8906842008-11-12 17:18:41 +0000177 cnt++;
178 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000179
balrogc8906842008-11-12 17:18:41 +0000180 return cnt;
aurel327d019982008-10-12 00:53:08 +0000181#endif
balrogc8906842008-11-12 17:18:41 +0000182}
183
Richard Henderson72d81152013-02-13 17:47:35 -0800184/**
185 * cto32 - count trailing ones in a 32-bit value.
186 * @val: The value to search
187 *
188 * Returns 32 if the value is -1.
189 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000190static inline int cto32(uint32_t val)
balrogc8906842008-11-12 17:18:41 +0000191{
j_mayerb9ef45f2007-10-28 12:52:38 +0000192 return ctz32(~val);
193}
194
Richard Henderson72d81152013-02-13 17:47:35 -0800195/**
196 * ctz64 - count trailing zeros in a 64-bit value.
197 * @val: The value to search
198 *
199 * Returns 64 if the value is zero. Note that the GCC builtin is
200 * undefined if the value is zero.
201 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000202static inline int ctz64(uint64_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000203{
aurel32bad5b1e2008-10-12 16:15:04 +0000204#if QEMU_GNUC_PREREQ(3, 4)
Richard Henderson72d81152013-02-13 17:47:35 -0800205 return val ? __builtin_ctzll(val) : 64;
aurel327d019982008-10-12 00:53:08 +0000206#else
j_mayerb9ef45f2007-10-28 12:52:38 +0000207 int cnt;
208
209 cnt = 0;
210 if (!((uint32_t)val)) {
211 cnt += 32;
212 val >>= 32;
213 }
214
215 return cnt + ctz32(val);
aurel327d019982008-10-12 00:53:08 +0000216#endif
j_mayerb9ef45f2007-10-28 12:52:38 +0000217}
218
Richard Henderson72d81152013-02-13 17:47:35 -0800219/**
220 * ctz64 - count trailing ones in a 64-bit value.
221 * @val: The value to search
222 *
223 * Returns 64 if the value is -1.
224 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000225static inline int cto64(uint64_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000226{
227 return ctz64(~val);
228}
229
Richard Henderson72d81152013-02-13 17:47:35 -0800230/**
231 * ctpop8 - count the population of one bits in an 8-bit value.
232 * @val: The value to search
233 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000234static inline int ctpop8(uint8_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000235{
Richard Henderson72d81152013-02-13 17:47:35 -0800236#if QEMU_GNUC_PREREQ(3, 4)
237 return __builtin_popcount(val);
238#else
j_mayerb9ef45f2007-10-28 12:52:38 +0000239 val = (val & 0x55) + ((val >> 1) & 0x55);
240 val = (val & 0x33) + ((val >> 2) & 0x33);
241 val = (val & 0x0f) + ((val >> 4) & 0x0f);
242
243 return val;
Richard Henderson72d81152013-02-13 17:47:35 -0800244#endif
j_mayerb9ef45f2007-10-28 12:52:38 +0000245}
246
Richard Henderson72d81152013-02-13 17:47:35 -0800247/**
248 * ctpop16 - count the population of one bits in a 16-bit value.
249 * @val: The value to search
250 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000251static inline int ctpop16(uint16_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000252{
Richard Henderson72d81152013-02-13 17:47:35 -0800253#if QEMU_GNUC_PREREQ(3, 4)
254 return __builtin_popcount(val);
255#else
j_mayerb9ef45f2007-10-28 12:52:38 +0000256 val = (val & 0x5555) + ((val >> 1) & 0x5555);
257 val = (val & 0x3333) + ((val >> 2) & 0x3333);
258 val = (val & 0x0f0f) + ((val >> 4) & 0x0f0f);
259 val = (val & 0x00ff) + ((val >> 8) & 0x00ff);
260
261 return val;
Richard Henderson72d81152013-02-13 17:47:35 -0800262#endif
j_mayerb9ef45f2007-10-28 12:52:38 +0000263}
264
Richard Henderson72d81152013-02-13 17:47:35 -0800265/**
266 * ctpop32 - count the population of one bits in a 32-bit value.
267 * @val: The value to search
268 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000269static inline int ctpop32(uint32_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000270{
aurel32bad5b1e2008-10-12 16:15:04 +0000271#if QEMU_GNUC_PREREQ(3, 4)
aurel327d019982008-10-12 00:53:08 +0000272 return __builtin_popcount(val);
273#else
j_mayerb9ef45f2007-10-28 12:52:38 +0000274 val = (val & 0x55555555) + ((val >> 1) & 0x55555555);
275 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
276 val = (val & 0x0f0f0f0f) + ((val >> 4) & 0x0f0f0f0f);
277 val = (val & 0x00ff00ff) + ((val >> 8) & 0x00ff00ff);
278 val = (val & 0x0000ffff) + ((val >> 16) & 0x0000ffff);
279
280 return val;
aurel327d019982008-10-12 00:53:08 +0000281#endif
j_mayerb9ef45f2007-10-28 12:52:38 +0000282}
283
Richard Henderson72d81152013-02-13 17:47:35 -0800284/**
285 * ctpop64 - count the population of one bits in a 64-bit value.
286 * @val: The value to search
287 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000288static inline int ctpop64(uint64_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000289{
aurel32bad5b1e2008-10-12 16:15:04 +0000290#if QEMU_GNUC_PREREQ(3, 4)
aurel327d019982008-10-12 00:53:08 +0000291 return __builtin_popcountll(val);
292#else
j_mayerb9ef45f2007-10-28 12:52:38 +0000293 val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL);
294 val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL);
295 val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >> 4) & 0x0f0f0f0f0f0f0f0fULL);
296 val = (val & 0x00ff00ff00ff00ffULL) + ((val >> 8) & 0x00ff00ff00ff00ffULL);
297 val = (val & 0x0000ffff0000ffffULL) + ((val >> 16) & 0x0000ffff0000ffffULL);
298 val = (val & 0x00000000ffffffffULL) + ((val >> 32) & 0x00000000ffffffffULL);
299
300 return val;
aurel327d019982008-10-12 00:53:08 +0000301#endif
ths3800af92007-12-18 01:58:05 +0000302}
Paolo Bonzinicb9c3772012-12-06 12:15:58 +0100303
Richard Henderson01654372013-02-13 17:47:34 -0800304/* Host type specific sizes of these routines. */
305
306#if ULONG_MAX == UINT32_MAX
307# define clzl clz32
308# define ctzl ctz32
309# define clol clo32
310# define ctol cto32
311# define ctpopl ctpop32
312#elif ULONG_MAX == UINT64_MAX
313# define clzl clz64
314# define ctzl ctz64
315# define clol clo64
316# define ctol cto64
317# define ctpopl ctpop64
318#else
319# error Unknown sizeof long
320#endif
321
Paolo Bonzinicb9c3772012-12-06 12:15:58 +0100322#endif