blob: d72b72d88f3770d8426fc391e867b6bee2b7c2c6 [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
j_mayer7a51ad82007-11-04 02:24:58 +000031#if defined(__x86_64__)
32#define __HAVE_FAST_MULU64__
Blue Swirlfacd2852009-08-16 08:03:26 +000033static inline void mulu64(uint64_t *plow, uint64_t *phigh,
34 uint64_t a, uint64_t b)
j_mayer7a51ad82007-11-04 02:24:58 +000035{
36 __asm__ ("mul %0\n\t"
37 : "=d" (*phigh), "=a" (*plow)
38 : "a" (a), "0" (b));
39}
40#define __HAVE_FAST_MULS64__
Blue Swirlfacd2852009-08-16 08:03:26 +000041static inline void muls64(uint64_t *plow, uint64_t *phigh,
42 int64_t a, int64_t b)
j_mayer7a51ad82007-11-04 02:24:58 +000043{
44 __asm__ ("imul %0\n\t"
45 : "=d" (*phigh), "=a" (*plow)
46 : "a" (a), "0" (b));
47}
48#else
j_mayer05e1d832007-11-05 13:16:23 +000049void muls64(uint64_t *phigh, uint64_t *plow, int64_t a, int64_t b);
j_mayer7a51ad82007-11-04 02:24:58 +000050void mulu64(uint64_t *phigh, uint64_t *plow, uint64_t a, uint64_t b);
51#endif
52
ths05f778c2007-10-27 13:05:54 +000053/* Binary search for leading zeros. */
54
Blue Swirlfacd2852009-08-16 08:03:26 +000055static inline int clz32(uint32_t val)
ths05f778c2007-10-27 13:05:54 +000056{
aurel32bad5b1e2008-10-12 16:15:04 +000057#if QEMU_GNUC_PREREQ(3, 4)
aurel327d019982008-10-12 00:53:08 +000058 if (val)
59 return __builtin_clz(val);
60 else
61 return 32;
62#else
ths05f778c2007-10-27 13:05:54 +000063 int cnt = 0;
64
65 if (!(val & 0xFFFF0000U)) {
66 cnt += 16;
67 val <<= 16;
68 }
69 if (!(val & 0xFF000000U)) {
70 cnt += 8;
71 val <<= 8;
72 }
73 if (!(val & 0xF0000000U)) {
74 cnt += 4;
75 val <<= 4;
76 }
77 if (!(val & 0xC0000000U)) {
78 cnt += 2;
79 val <<= 2;
80 }
81 if (!(val & 0x80000000U)) {
82 cnt++;
83 val <<= 1;
84 }
85 if (!(val & 0x80000000U)) {
86 cnt++;
87 }
88 return cnt;
aurel327d019982008-10-12 00:53:08 +000089#endif
ths05f778c2007-10-27 13:05:54 +000090}
91
Blue Swirlfacd2852009-08-16 08:03:26 +000092static inline int clo32(uint32_t val)
ths05f778c2007-10-27 13:05:54 +000093{
94 return clz32(~val);
95}
96
Blue Swirlfacd2852009-08-16 08:03:26 +000097static inline int clz64(uint64_t val)
ths05f778c2007-10-27 13:05:54 +000098{
aurel32bad5b1e2008-10-12 16:15:04 +000099#if QEMU_GNUC_PREREQ(3, 4)
aurel327d019982008-10-12 00:53:08 +0000100 if (val)
101 return __builtin_clzll(val);
102 else
103 return 64;
104#else
ths05f778c2007-10-27 13:05:54 +0000105 int cnt = 0;
106
j_mayer7a51ad82007-11-04 02:24:58 +0000107 if (!(val >> 32)) {
ths05f778c2007-10-27 13:05:54 +0000108 cnt += 32;
j_mayer7a51ad82007-11-04 02:24:58 +0000109 } else {
110 val >>= 32;
ths05f778c2007-10-27 13:05:54 +0000111 }
j_mayer7a51ad82007-11-04 02:24:58 +0000112
113 return cnt + clz32(val);
aurel327d019982008-10-12 00:53:08 +0000114#endif
ths05f778c2007-10-27 13:05:54 +0000115}
116
Blue Swirlfacd2852009-08-16 08:03:26 +0000117static inline int clo64(uint64_t val)
ths05f778c2007-10-27 13:05:54 +0000118{
119 return clz64(~val);
120}
j_mayerb9ef45f2007-10-28 12:52:38 +0000121
Blue Swirlfacd2852009-08-16 08:03:26 +0000122static inline int ctz32(uint32_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000123{
aurel32bad5b1e2008-10-12 16:15:04 +0000124#if QEMU_GNUC_PREREQ(3, 4)
aurel327d019982008-10-12 00:53:08 +0000125 if (val)
126 return __builtin_ctz(val);
127 else
128 return 32;
129#else
j_mayerb9ef45f2007-10-28 12:52:38 +0000130 int cnt;
131
132 cnt = 0;
133 if (!(val & 0x0000FFFFUL)) {
balrogc8906842008-11-12 17:18:41 +0000134 cnt += 16;
j_mayerb9ef45f2007-10-28 12:52:38 +0000135 val >>= 16;
balrogc8906842008-11-12 17:18:41 +0000136 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000137 if (!(val & 0x000000FFUL)) {
balrogc8906842008-11-12 17:18:41 +0000138 cnt += 8;
j_mayerb9ef45f2007-10-28 12:52:38 +0000139 val >>= 8;
balrogc8906842008-11-12 17:18:41 +0000140 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000141 if (!(val & 0x0000000FUL)) {
balrogc8906842008-11-12 17:18:41 +0000142 cnt += 4;
j_mayerb9ef45f2007-10-28 12:52:38 +0000143 val >>= 4;
balrogc8906842008-11-12 17:18:41 +0000144 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000145 if (!(val & 0x00000003UL)) {
balrogc8906842008-11-12 17:18:41 +0000146 cnt += 2;
j_mayerb9ef45f2007-10-28 12:52:38 +0000147 val >>= 2;
balrogc8906842008-11-12 17:18:41 +0000148 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000149 if (!(val & 0x00000001UL)) {
balrogc8906842008-11-12 17:18:41 +0000150 cnt++;
j_mayerb9ef45f2007-10-28 12:52:38 +0000151 val >>= 1;
balrogc8906842008-11-12 17:18:41 +0000152 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000153 if (!(val & 0x00000001UL)) {
balrogc8906842008-11-12 17:18:41 +0000154 cnt++;
155 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000156
balrogc8906842008-11-12 17:18:41 +0000157 return cnt;
aurel327d019982008-10-12 00:53:08 +0000158#endif
balrogc8906842008-11-12 17:18:41 +0000159}
160
Blue Swirlfacd2852009-08-16 08:03:26 +0000161static inline int cto32(uint32_t val)
balrogc8906842008-11-12 17:18:41 +0000162{
j_mayerb9ef45f2007-10-28 12:52:38 +0000163 return ctz32(~val);
164}
165
Blue Swirlfacd2852009-08-16 08:03:26 +0000166static inline int ctz64(uint64_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000167{
aurel32bad5b1e2008-10-12 16:15:04 +0000168#if QEMU_GNUC_PREREQ(3, 4)
aurel327d019982008-10-12 00:53:08 +0000169 if (val)
Richard Henderson06445242009-12-13 17:47:25 -0800170 return __builtin_ctzll(val);
aurel327d019982008-10-12 00:53:08 +0000171 else
172 return 64;
173#else
j_mayerb9ef45f2007-10-28 12:52:38 +0000174 int cnt;
175
176 cnt = 0;
177 if (!((uint32_t)val)) {
178 cnt += 32;
179 val >>= 32;
180 }
181
182 return cnt + ctz32(val);
aurel327d019982008-10-12 00:53:08 +0000183#endif
j_mayerb9ef45f2007-10-28 12:52:38 +0000184}
185
Blue Swirlfacd2852009-08-16 08:03:26 +0000186static inline int cto64(uint64_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000187{
188 return ctz64(~val);
189}
190
Blue Swirlfacd2852009-08-16 08:03:26 +0000191static inline int ctpop8(uint8_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000192{
193 val = (val & 0x55) + ((val >> 1) & 0x55);
194 val = (val & 0x33) + ((val >> 2) & 0x33);
195 val = (val & 0x0f) + ((val >> 4) & 0x0f);
196
197 return val;
198}
199
Blue Swirlfacd2852009-08-16 08:03:26 +0000200static inline int ctpop16(uint16_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000201{
202 val = (val & 0x5555) + ((val >> 1) & 0x5555);
203 val = (val & 0x3333) + ((val >> 2) & 0x3333);
204 val = (val & 0x0f0f) + ((val >> 4) & 0x0f0f);
205 val = (val & 0x00ff) + ((val >> 8) & 0x00ff);
206
207 return val;
208}
209
Blue Swirlfacd2852009-08-16 08:03:26 +0000210static inline int ctpop32(uint32_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000211{
aurel32bad5b1e2008-10-12 16:15:04 +0000212#if QEMU_GNUC_PREREQ(3, 4)
aurel327d019982008-10-12 00:53:08 +0000213 return __builtin_popcount(val);
214#else
j_mayerb9ef45f2007-10-28 12:52:38 +0000215 val = (val & 0x55555555) + ((val >> 1) & 0x55555555);
216 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
217 val = (val & 0x0f0f0f0f) + ((val >> 4) & 0x0f0f0f0f);
218 val = (val & 0x00ff00ff) + ((val >> 8) & 0x00ff00ff);
219 val = (val & 0x0000ffff) + ((val >> 16) & 0x0000ffff);
220
221 return val;
aurel327d019982008-10-12 00:53:08 +0000222#endif
j_mayerb9ef45f2007-10-28 12:52:38 +0000223}
224
Blue Swirlfacd2852009-08-16 08:03:26 +0000225static inline int ctpop64(uint64_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000226{
aurel32bad5b1e2008-10-12 16:15:04 +0000227#if QEMU_GNUC_PREREQ(3, 4)
aurel327d019982008-10-12 00:53:08 +0000228 return __builtin_popcountll(val);
229#else
j_mayerb9ef45f2007-10-28 12:52:38 +0000230 val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL);
231 val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL);
232 val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >> 4) & 0x0f0f0f0f0f0f0f0fULL);
233 val = (val & 0x00ff00ff00ff00ffULL) + ((val >> 8) & 0x00ff00ff00ff00ffULL);
234 val = (val & 0x0000ffff0000ffffULL) + ((val >> 16) & 0x0000ffff0000ffffULL);
235 val = (val & 0x00000000ffffffffULL) + ((val >> 32) & 0x00000000ffffffffULL);
236
237 return val;
aurel327d019982008-10-12 00:53:08 +0000238#endif
ths3800af92007-12-18 01:58:05 +0000239}
Paolo Bonzinicb9c3772012-12-06 12:15:58 +0100240
Richard Henderson01654372013-02-13 17:47:34 -0800241/* Host type specific sizes of these routines. */
242
243#if ULONG_MAX == UINT32_MAX
244# define clzl clz32
245# define ctzl ctz32
246# define clol clo32
247# define ctol cto32
248# define ctpopl ctpop32
249#elif ULONG_MAX == UINT64_MAX
250# define clzl clz64
251# define ctzl ctz64
252# define clol clo64
253# define ctol cto64
254# define ctpopl ctpop64
255#else
256# error Unknown sizeof long
257#endif
258
Paolo Bonzinicb9c3772012-12-06 12:15:58 +0100259#endif