target-i386: exception handling for memory helpers
[qemu/ar7.git] / include / qemu / host-utils.h
blob7d36ebfd5b6102b9f3a88ca546f0bc2df4b151f2
1 /*
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
23 * THE SOFTWARE.
25 #ifndef HOST_UTILS_H
26 #define HOST_UTILS_H 1
28 #include "qemu/compiler.h" /* QEMU_GNUC_PREREQ */
29 #include <limits.h>
30 #include <stdbool.h>
32 #ifdef CONFIG_INT128
33 static inline void mulu64(uint64_t *plow, uint64_t *phigh,
34 uint64_t a, uint64_t b)
36 __uint128_t r = (__uint128_t)a * b;
37 *plow = r;
38 *phigh = r >> 64;
41 static inline void muls64(uint64_t *plow, uint64_t *phigh,
42 int64_t a, int64_t b)
44 __int128_t r = (__int128_t)a * b;
45 *plow = r;
46 *phigh = r >> 64;
49 /* compute with 96 bit intermediate result: (a*b)/c */
50 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
52 return (__int128_t)a * b / c;
55 static inline int divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor)
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;
68 static inline int divs128(int64_t *plow, int64_t *phigh, int64_t divisor)
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;
80 #else
81 void muls64(uint64_t *phigh, uint64_t *plow, int64_t a, int64_t b);
82 void mulu64(uint64_t *phigh, uint64_t *plow, uint64_t a, uint64_t b);
83 int divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor);
84 int divs128(int64_t *plow, int64_t *phigh, int64_t divisor);
86 static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
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;
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;
108 #endif
111 * clz32 - count leading zeros in a 32-bit value.
112 * @val: The value to search
114 * Returns 32 if the value is zero. Note that the GCC builtin is
115 * undefined if the value is zero.
117 static inline int clz32(uint32_t val)
119 #if QEMU_GNUC_PREREQ(3, 4)
120 return val ? __builtin_clz(val) : 32;
121 #else
122 /* Binary search for the leading one bit. */
123 int cnt = 0;
125 if (!(val & 0xFFFF0000U)) {
126 cnt += 16;
127 val <<= 16;
129 if (!(val & 0xFF000000U)) {
130 cnt += 8;
131 val <<= 8;
133 if (!(val & 0xF0000000U)) {
134 cnt += 4;
135 val <<= 4;
137 if (!(val & 0xC0000000U)) {
138 cnt += 2;
139 val <<= 2;
141 if (!(val & 0x80000000U)) {
142 cnt++;
143 val <<= 1;
145 if (!(val & 0x80000000U)) {
146 cnt++;
148 return cnt;
149 #endif
153 * clo32 - count leading ones in a 32-bit value.
154 * @val: The value to search
156 * Returns 32 if the value is -1.
158 static inline int clo32(uint32_t val)
160 return clz32(~val);
164 * clz64 - count leading zeros in a 64-bit value.
165 * @val: The value to search
167 * Returns 64 if the value is zero. Note that the GCC builtin is
168 * undefined if the value is zero.
170 static inline int clz64(uint64_t val)
172 #if QEMU_GNUC_PREREQ(3, 4)
173 return val ? __builtin_clzll(val) : 64;
174 #else
175 int cnt = 0;
177 if (!(val >> 32)) {
178 cnt += 32;
179 } else {
180 val >>= 32;
183 return cnt + clz32(val);
184 #endif
188 * clo64 - count leading ones in a 64-bit value.
189 * @val: The value to search
191 * Returns 64 if the value is -1.
193 static inline int clo64(uint64_t val)
195 return clz64(~val);
199 * ctz32 - count trailing zeros in a 32-bit value.
200 * @val: The value to search
202 * Returns 32 if the value is zero. Note that the GCC builtin is
203 * undefined if the value is zero.
205 static inline int ctz32(uint32_t val)
207 #if QEMU_GNUC_PREREQ(3, 4)
208 return val ? __builtin_ctz(val) : 32;
209 #else
210 /* Binary search for the trailing one bit. */
211 int cnt;
213 cnt = 0;
214 if (!(val & 0x0000FFFFUL)) {
215 cnt += 16;
216 val >>= 16;
218 if (!(val & 0x000000FFUL)) {
219 cnt += 8;
220 val >>= 8;
222 if (!(val & 0x0000000FUL)) {
223 cnt += 4;
224 val >>= 4;
226 if (!(val & 0x00000003UL)) {
227 cnt += 2;
228 val >>= 2;
230 if (!(val & 0x00000001UL)) {
231 cnt++;
232 val >>= 1;
234 if (!(val & 0x00000001UL)) {
235 cnt++;
238 return cnt;
239 #endif
243 * cto32 - count trailing ones in a 32-bit value.
244 * @val: The value to search
246 * Returns 32 if the value is -1.
248 static inline int cto32(uint32_t val)
250 return ctz32(~val);
254 * ctz64 - count trailing zeros in a 64-bit value.
255 * @val: The value to search
257 * Returns 64 if the value is zero. Note that the GCC builtin is
258 * undefined if the value is zero.
260 static inline int ctz64(uint64_t val)
262 #if QEMU_GNUC_PREREQ(3, 4)
263 return val ? __builtin_ctzll(val) : 64;
264 #else
265 int cnt;
267 cnt = 0;
268 if (!((uint32_t)val)) {
269 cnt += 32;
270 val >>= 32;
273 return cnt + ctz32(val);
274 #endif
278 * cto64 - count trailing ones in a 64-bit value.
279 * @val: The value to search
281 * Returns 64 if the value is -1.
283 static inline int cto64(uint64_t val)
285 return ctz64(~val);
289 * clrsb32 - count leading redundant sign bits in a 32-bit value.
290 * @val: The value to search
292 * Returns the number of bits following the sign bit that are equal to it.
293 * No special cases; output range is [0-31].
295 static inline int clrsb32(uint32_t val)
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
305 * clrsb64 - count leading redundant sign bits in a 64-bit value.
306 * @val: The value to search
308 * Returns the number of bits following the sign bit that are equal to it.
309 * No special cases; output range is [0-63].
311 static inline int clrsb64(uint64_t val)
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
321 * ctpop8 - count the population of one bits in an 8-bit value.
322 * @val: The value to search
324 static inline int ctpop8(uint8_t val)
326 #if QEMU_GNUC_PREREQ(3, 4)
327 return __builtin_popcount(val);
328 #else
329 val = (val & 0x55) + ((val >> 1) & 0x55);
330 val = (val & 0x33) + ((val >> 2) & 0x33);
331 val = (val & 0x0f) + ((val >> 4) & 0x0f);
333 return val;
334 #endif
338 * ctpop16 - count the population of one bits in a 16-bit value.
339 * @val: The value to search
341 static inline int ctpop16(uint16_t val)
343 #if QEMU_GNUC_PREREQ(3, 4)
344 return __builtin_popcount(val);
345 #else
346 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);
351 return val;
352 #endif
356 * ctpop32 - count the population of one bits in a 32-bit value.
357 * @val: The value to search
359 static inline int ctpop32(uint32_t val)
361 #if QEMU_GNUC_PREREQ(3, 4)
362 return __builtin_popcount(val);
363 #else
364 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);
370 return val;
371 #endif
375 * ctpop64 - count the population of one bits in a 64-bit value.
376 * @val: The value to search
378 static inline int ctpop64(uint64_t val)
380 #if QEMU_GNUC_PREREQ(3, 4)
381 return __builtin_popcountll(val);
382 #else
383 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);
390 return val;
391 #endif
394 /* Host type specific sizes of these routines. */
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
412 static inline bool is_power_of_2(uint64_t value)
414 if (!value) {
415 return 0;
418 return !(value & (value - 1));
421 /* round down to the nearest power of 2*/
422 static inline int64_t pow2floor(int64_t value)
424 if (!is_power_of_2(value)) {
425 value = 0x8000000000000000ULL >> clz64(value);
427 return value;
430 /* round up to the nearest power of 2 (0 if overflow) */
431 static inline uint64_t pow2ceil(uint64_t value)
433 uint8_t nlz = clz64(value);
435 if (is_power_of_2(value)) {
436 return value;
438 if (!nlz) {
439 return 0;
441 return 1ULL << (64 - nlz);
444 #endif