blob: 7d36ebfd5b6102b9f3a88ca546f0bc2df4b151f2 [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>
Peter Maydell8f1ed5f2015-07-24 13:33:12 +010030#include <stdbool.h>
thscebdff72008-06-05 22:55:54 +000031
Richard Hendersonf5401662013-02-16 12:46:59 -080032#ifdef CONFIG_INT128
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{
Richard Hendersonf5401662013-02-16 12:46:59 -080036 __uint128_t r = (__uint128_t)a * b;
37 *plow = r;
38 *phigh = r >> 64;
j_mayer7a51ad82007-11-04 02:24:58 +000039}
Richard Hendersonf5401662013-02-16 12:46:59 -080040
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{
Richard Hendersonf5401662013-02-16 12:46:59 -080044 __int128_t r = (__int128_t)a * b;
45 *plow = r;
46 *phigh = r >> 64;
j_mayer7a51ad82007-11-04 02:24:58 +000047}
Tom Musta98d1eb22014-01-07 10:05:51 -060048
Peter Maydell49caffe2015-08-19 16:20:20 +010049/* compute with 96 bit intermediate result: (a*b)/c */
50static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
51{
52 return (__int128_t)a * b / c;
53}
54
Tom Musta98d1eb22014-01-07 10:05:51 -060055static inline int divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor)
56{
57 if (divisor == 0) {
58 return 1;
59 } else {
60 __uint128_t dividend = ((__uint128_t)*phigh << 64) | *plow;
61 __uint128_t result = dividend / divisor;
62 *plow = result;
63 *phigh = dividend % divisor;
64 return result > UINT64_MAX;
65 }
66}
Tom Mustae44259b2014-01-07 10:05:52 -060067
68static inline int divs128(int64_t *plow, int64_t *phigh, int64_t divisor)
69{
70 if (divisor == 0) {
71 return 1;
72 } else {
73 __int128_t dividend = ((__int128_t)*phigh << 64) | *plow;
74 __int128_t result = dividend / divisor;
75 *plow = result;
76 *phigh = dividend % divisor;
77 return result != *plow;
78 }
79}
j_mayer7a51ad82007-11-04 02:24:58 +000080#else
j_mayer05e1d832007-11-05 13:16:23 +000081void muls64(uint64_t *phigh, uint64_t *plow, int64_t a, int64_t b);
j_mayer7a51ad82007-11-04 02:24:58 +000082void mulu64(uint64_t *phigh, uint64_t *plow, uint64_t a, uint64_t b);
Tom Musta98d1eb22014-01-07 10:05:51 -060083int divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor);
Tom Mustae44259b2014-01-07 10:05:52 -060084int divs128(int64_t *plow, int64_t *phigh, int64_t divisor);
Peter Maydell49caffe2015-08-19 16:20:20 +010085
86static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
87{
88 union {
89 uint64_t ll;
90 struct {
91#ifdef HOST_WORDS_BIGENDIAN
92 uint32_t high, low;
93#else
94 uint32_t low, high;
95#endif
96 } l;
97 } u, res;
98 uint64_t rl, rh;
99
100 u.ll = a;
101 rl = (uint64_t)u.l.low * (uint64_t)b;
102 rh = (uint64_t)u.l.high * (uint64_t)b;
103 rh += (rl >> 32);
104 res.l.high = rh / c;
105 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
106 return res.ll;
107}
j_mayer7a51ad82007-11-04 02:24:58 +0000108#endif
109
Richard Henderson72d81152013-02-13 17:47:35 -0800110/**
111 * clz32 - count leading zeros in a 32-bit value.
112 * @val: The value to search
113 *
114 * Returns 32 if the value is zero. Note that the GCC builtin is
115 * undefined if the value is zero.
116 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000117static inline int clz32(uint32_t val)
ths05f778c2007-10-27 13:05:54 +0000118{
aurel32bad5b1e2008-10-12 16:15:04 +0000119#if QEMU_GNUC_PREREQ(3, 4)
Richard Henderson72d81152013-02-13 17:47:35 -0800120 return val ? __builtin_clz(val) : 32;
aurel327d019982008-10-12 00:53:08 +0000121#else
Richard Henderson72d81152013-02-13 17:47:35 -0800122 /* Binary search for the leading one bit. */
ths05f778c2007-10-27 13:05:54 +0000123 int cnt = 0;
124
125 if (!(val & 0xFFFF0000U)) {
126 cnt += 16;
127 val <<= 16;
128 }
129 if (!(val & 0xFF000000U)) {
130 cnt += 8;
131 val <<= 8;
132 }
133 if (!(val & 0xF0000000U)) {
134 cnt += 4;
135 val <<= 4;
136 }
137 if (!(val & 0xC0000000U)) {
138 cnt += 2;
139 val <<= 2;
140 }
141 if (!(val & 0x80000000U)) {
142 cnt++;
143 val <<= 1;
144 }
145 if (!(val & 0x80000000U)) {
146 cnt++;
147 }
148 return cnt;
aurel327d019982008-10-12 00:53:08 +0000149#endif
ths05f778c2007-10-27 13:05:54 +0000150}
151
Richard Henderson72d81152013-02-13 17:47:35 -0800152/**
153 * clo32 - count leading ones in a 32-bit value.
154 * @val: The value to search
155 *
156 * Returns 32 if the value is -1.
157 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000158static inline int clo32(uint32_t val)
ths05f778c2007-10-27 13:05:54 +0000159{
160 return clz32(~val);
161}
162
Richard Henderson72d81152013-02-13 17:47:35 -0800163/**
164 * clz64 - count leading zeros in a 64-bit value.
165 * @val: The value to search
166 *
167 * Returns 64 if the value is zero. Note that the GCC builtin is
168 * undefined if the value is zero.
169 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000170static inline int clz64(uint64_t val)
ths05f778c2007-10-27 13:05:54 +0000171{
aurel32bad5b1e2008-10-12 16:15:04 +0000172#if QEMU_GNUC_PREREQ(3, 4)
Richard Henderson72d81152013-02-13 17:47:35 -0800173 return val ? __builtin_clzll(val) : 64;
aurel327d019982008-10-12 00:53:08 +0000174#else
ths05f778c2007-10-27 13:05:54 +0000175 int cnt = 0;
176
j_mayer7a51ad82007-11-04 02:24:58 +0000177 if (!(val >> 32)) {
ths05f778c2007-10-27 13:05:54 +0000178 cnt += 32;
j_mayer7a51ad82007-11-04 02:24:58 +0000179 } else {
180 val >>= 32;
ths05f778c2007-10-27 13:05:54 +0000181 }
j_mayer7a51ad82007-11-04 02:24:58 +0000182
183 return cnt + clz32(val);
aurel327d019982008-10-12 00:53:08 +0000184#endif
ths05f778c2007-10-27 13:05:54 +0000185}
186
Richard Henderson72d81152013-02-13 17:47:35 -0800187/**
188 * clo64 - count leading ones in a 64-bit value.
189 * @val: The value to search
190 *
191 * Returns 64 if the value is -1.
192 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000193static inline int clo64(uint64_t val)
ths05f778c2007-10-27 13:05:54 +0000194{
195 return clz64(~val);
196}
j_mayerb9ef45f2007-10-28 12:52:38 +0000197
Richard Henderson72d81152013-02-13 17:47:35 -0800198/**
199 * ctz32 - count trailing zeros in a 32-bit value.
200 * @val: The value to search
201 *
202 * Returns 32 if the value is zero. Note that the GCC builtin is
203 * undefined if the value is zero.
204 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000205static inline int ctz32(uint32_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000206{
aurel32bad5b1e2008-10-12 16:15:04 +0000207#if QEMU_GNUC_PREREQ(3, 4)
Richard Henderson72d81152013-02-13 17:47:35 -0800208 return val ? __builtin_ctz(val) : 32;
aurel327d019982008-10-12 00:53:08 +0000209#else
Richard Henderson72d81152013-02-13 17:47:35 -0800210 /* Binary search for the trailing one bit. */
j_mayerb9ef45f2007-10-28 12:52:38 +0000211 int cnt;
212
213 cnt = 0;
214 if (!(val & 0x0000FFFFUL)) {
balrogc8906842008-11-12 17:18:41 +0000215 cnt += 16;
j_mayerb9ef45f2007-10-28 12:52:38 +0000216 val >>= 16;
balrogc8906842008-11-12 17:18:41 +0000217 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000218 if (!(val & 0x000000FFUL)) {
balrogc8906842008-11-12 17:18:41 +0000219 cnt += 8;
j_mayerb9ef45f2007-10-28 12:52:38 +0000220 val >>= 8;
balrogc8906842008-11-12 17:18:41 +0000221 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000222 if (!(val & 0x0000000FUL)) {
balrogc8906842008-11-12 17:18:41 +0000223 cnt += 4;
j_mayerb9ef45f2007-10-28 12:52:38 +0000224 val >>= 4;
balrogc8906842008-11-12 17:18:41 +0000225 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000226 if (!(val & 0x00000003UL)) {
balrogc8906842008-11-12 17:18:41 +0000227 cnt += 2;
j_mayerb9ef45f2007-10-28 12:52:38 +0000228 val >>= 2;
balrogc8906842008-11-12 17:18:41 +0000229 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000230 if (!(val & 0x00000001UL)) {
balrogc8906842008-11-12 17:18:41 +0000231 cnt++;
j_mayerb9ef45f2007-10-28 12:52:38 +0000232 val >>= 1;
balrogc8906842008-11-12 17:18:41 +0000233 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000234 if (!(val & 0x00000001UL)) {
balrogc8906842008-11-12 17:18:41 +0000235 cnt++;
236 }
j_mayerb9ef45f2007-10-28 12:52:38 +0000237
balrogc8906842008-11-12 17:18:41 +0000238 return cnt;
aurel327d019982008-10-12 00:53:08 +0000239#endif
balrogc8906842008-11-12 17:18:41 +0000240}
241
Richard Henderson72d81152013-02-13 17:47:35 -0800242/**
243 * cto32 - count trailing ones in a 32-bit value.
244 * @val: The value to search
245 *
246 * Returns 32 if the value is -1.
247 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000248static inline int cto32(uint32_t val)
balrogc8906842008-11-12 17:18:41 +0000249{
j_mayerb9ef45f2007-10-28 12:52:38 +0000250 return ctz32(~val);
251}
252
Richard Henderson72d81152013-02-13 17:47:35 -0800253/**
254 * ctz64 - count trailing zeros in a 64-bit value.
255 * @val: The value to search
256 *
257 * Returns 64 if the value is zero. Note that the GCC builtin is
258 * undefined if the value is zero.
259 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000260static inline int ctz64(uint64_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000261{
aurel32bad5b1e2008-10-12 16:15:04 +0000262#if QEMU_GNUC_PREREQ(3, 4)
Richard Henderson72d81152013-02-13 17:47:35 -0800263 return val ? __builtin_ctzll(val) : 64;
aurel327d019982008-10-12 00:53:08 +0000264#else
j_mayerb9ef45f2007-10-28 12:52:38 +0000265 int cnt;
266
267 cnt = 0;
268 if (!((uint32_t)val)) {
269 cnt += 32;
270 val >>= 32;
271 }
272
273 return cnt + ctz32(val);
aurel327d019982008-10-12 00:53:08 +0000274#endif
j_mayerb9ef45f2007-10-28 12:52:38 +0000275}
276
Richard Henderson72d81152013-02-13 17:47:35 -0800277/**
Dr. David Alan Gilbert1c884ab2014-02-12 17:14:33 +0000278 * cto64 - count trailing ones in a 64-bit value.
Richard Henderson72d81152013-02-13 17:47:35 -0800279 * @val: The value to search
280 *
281 * Returns 64 if the value is -1.
282 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000283static inline int cto64(uint64_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000284{
285 return ctz64(~val);
286}
287
Richard Henderson72d81152013-02-13 17:47:35 -0800288/**
Claudio Fontanaafd3fe42013-12-17 19:42:35 +0000289 * clrsb32 - count leading redundant sign bits in a 32-bit value.
290 * @val: The value to search
291 *
292 * Returns the number of bits following the sign bit that are equal to it.
293 * No special cases; output range is [0-31].
294 */
295static inline int clrsb32(uint32_t val)
296{
297#if QEMU_GNUC_PREREQ(4, 7)
298 return __builtin_clrsb(val);
299#else
300 return clz32(val ^ ((int32_t)val >> 1)) - 1;
301#endif
302}
303
304/**
305 * clrsb64 - count leading redundant sign bits in a 64-bit value.
306 * @val: The value to search
307 *
308 * Returns the number of bits following the sign bit that are equal to it.
309 * No special cases; output range is [0-63].
310 */
311static inline int clrsb64(uint64_t val)
312{
313#if QEMU_GNUC_PREREQ(4, 7)
314 return __builtin_clrsbll(val);
315#else
316 return clz64(val ^ ((int64_t)val >> 1)) - 1;
317#endif
318}
319
320/**
Richard Henderson72d81152013-02-13 17:47:35 -0800321 * ctpop8 - count the population of one bits in an 8-bit value.
322 * @val: The value to search
323 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000324static inline int ctpop8(uint8_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000325{
Richard Henderson72d81152013-02-13 17:47:35 -0800326#if QEMU_GNUC_PREREQ(3, 4)
327 return __builtin_popcount(val);
328#else
j_mayerb9ef45f2007-10-28 12:52:38 +0000329 val = (val & 0x55) + ((val >> 1) & 0x55);
330 val = (val & 0x33) + ((val >> 2) & 0x33);
331 val = (val & 0x0f) + ((val >> 4) & 0x0f);
332
333 return val;
Richard Henderson72d81152013-02-13 17:47:35 -0800334#endif
j_mayerb9ef45f2007-10-28 12:52:38 +0000335}
336
Richard Henderson72d81152013-02-13 17:47:35 -0800337/**
338 * ctpop16 - count the population of one bits in a 16-bit value.
339 * @val: The value to search
340 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000341static inline int ctpop16(uint16_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000342{
Richard Henderson72d81152013-02-13 17:47:35 -0800343#if QEMU_GNUC_PREREQ(3, 4)
344 return __builtin_popcount(val);
345#else
j_mayerb9ef45f2007-10-28 12:52:38 +0000346 val = (val & 0x5555) + ((val >> 1) & 0x5555);
347 val = (val & 0x3333) + ((val >> 2) & 0x3333);
348 val = (val & 0x0f0f) + ((val >> 4) & 0x0f0f);
349 val = (val & 0x00ff) + ((val >> 8) & 0x00ff);
350
351 return val;
Richard Henderson72d81152013-02-13 17:47:35 -0800352#endif
j_mayerb9ef45f2007-10-28 12:52:38 +0000353}
354
Richard Henderson72d81152013-02-13 17:47:35 -0800355/**
356 * ctpop32 - count the population of one bits in a 32-bit value.
357 * @val: The value to search
358 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000359static inline int ctpop32(uint32_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000360{
aurel32bad5b1e2008-10-12 16:15:04 +0000361#if QEMU_GNUC_PREREQ(3, 4)
aurel327d019982008-10-12 00:53:08 +0000362 return __builtin_popcount(val);
363#else
j_mayerb9ef45f2007-10-28 12:52:38 +0000364 val = (val & 0x55555555) + ((val >> 1) & 0x55555555);
365 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
366 val = (val & 0x0f0f0f0f) + ((val >> 4) & 0x0f0f0f0f);
367 val = (val & 0x00ff00ff) + ((val >> 8) & 0x00ff00ff);
368 val = (val & 0x0000ffff) + ((val >> 16) & 0x0000ffff);
369
370 return val;
aurel327d019982008-10-12 00:53:08 +0000371#endif
j_mayerb9ef45f2007-10-28 12:52:38 +0000372}
373
Richard Henderson72d81152013-02-13 17:47:35 -0800374/**
375 * ctpop64 - count the population of one bits in a 64-bit value.
376 * @val: The value to search
377 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000378static inline int ctpop64(uint64_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000379{
aurel32bad5b1e2008-10-12 16:15:04 +0000380#if QEMU_GNUC_PREREQ(3, 4)
aurel327d019982008-10-12 00:53:08 +0000381 return __builtin_popcountll(val);
382#else
j_mayerb9ef45f2007-10-28 12:52:38 +0000383 val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL);
384 val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL);
385 val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >> 4) & 0x0f0f0f0f0f0f0f0fULL);
386 val = (val & 0x00ff00ff00ff00ffULL) + ((val >> 8) & 0x00ff00ff00ff00ffULL);
387 val = (val & 0x0000ffff0000ffffULL) + ((val >> 16) & 0x0000ffff0000ffffULL);
388 val = (val & 0x00000000ffffffffULL) + ((val >> 32) & 0x00000000ffffffffULL);
389
390 return val;
aurel327d019982008-10-12 00:53:08 +0000391#endif
ths3800af92007-12-18 01:58:05 +0000392}
Paolo Bonzinicb9c3772012-12-06 12:15:58 +0100393
Richard Henderson01654372013-02-13 17:47:34 -0800394/* Host type specific sizes of these routines. */
395
396#if ULONG_MAX == UINT32_MAX
397# define clzl clz32
398# define ctzl ctz32
399# define clol clo32
400# define ctol cto32
401# define ctpopl ctpop32
402#elif ULONG_MAX == UINT64_MAX
403# define clzl clz64
404# define ctzl ctz64
405# define clol clo64
406# define ctol cto64
407# define ctpopl ctpop64
408#else
409# error Unknown sizeof long
410#endif
411
Peter Maydell8f1ed5f2015-07-24 13:33:12 +0100412static inline bool is_power_of_2(uint64_t value)
413{
414 if (!value) {
415 return 0;
416 }
417
418 return !(value & (value - 1));
419}
420
421/* round down to the nearest power of 2*/
422static inline int64_t pow2floor(int64_t value)
423{
424 if (!is_power_of_2(value)) {
425 value = 0x8000000000000000ULL >> clz64(value);
426 }
427 return value;
428}
429
430/* round up to the nearest power of 2 (0 if overflow) */
431static inline uint64_t pow2ceil(uint64_t value)
432{
433 uint8_t nlz = clz64(value);
434
435 if (is_power_of_2(value)) {
436 return value;
437 }
438 if (!nlz) {
439 return 0;
440 }
441 return 1ULL << (64 - nlz);
442}
443
Paolo Bonzinicb9c3772012-12-06 12:15:58 +0100444#endif