ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonzini | cb9c377 | 2012-12-06 12:15:58 +0100 | [diff] [blame] | 25 | #ifndef HOST_UTILS_H |
| 26 | #define HOST_UTILS_H 1 |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 27 | |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 28 | #include "qemu/compiler.h" /* QEMU_GNUC_PREREQ */ |
Richard Henderson | 0165437 | 2013-02-13 17:47:34 -0800 | [diff] [blame] | 29 | #include <limits.h> |
Peter Maydell | 8f1ed5f | 2015-07-24 13:33:12 +0100 | [diff] [blame] | 30 | #include <stdbool.h> |
ths | cebdff7 | 2008-06-05 22:55:54 +0000 | [diff] [blame] | 31 | |
Richard Henderson | f540166 | 2013-02-16 12:46:59 -0800 | [diff] [blame] | 32 | #ifdef CONFIG_INT128 |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 33 | static inline void mulu64(uint64_t *plow, uint64_t *phigh, |
| 34 | uint64_t a, uint64_t b) |
j_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 35 | { |
Richard Henderson | f540166 | 2013-02-16 12:46:59 -0800 | [diff] [blame] | 36 | __uint128_t r = (__uint128_t)a * b; |
| 37 | *plow = r; |
| 38 | *phigh = r >> 64; |
j_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 39 | } |
Richard Henderson | f540166 | 2013-02-16 12:46:59 -0800 | [diff] [blame] | 40 | |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 41 | static inline void muls64(uint64_t *plow, uint64_t *phigh, |
| 42 | int64_t a, int64_t b) |
j_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 43 | { |
Richard Henderson | f540166 | 2013-02-16 12:46:59 -0800 | [diff] [blame] | 44 | __int128_t r = (__int128_t)a * b; |
| 45 | *plow = r; |
| 46 | *phigh = r >> 64; |
j_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 47 | } |
Tom Musta | 98d1eb2 | 2014-01-07 10:05:51 -0600 | [diff] [blame] | 48 | |
Peter Maydell | 49caffe | 2015-08-19 16:20:20 +0100 | [diff] [blame] | 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) |
| 51 | { |
| 52 | return (__int128_t)a * b / c; |
| 53 | } |
| 54 | |
Tom Musta | 98d1eb2 | 2014-01-07 10:05:51 -0600 | [diff] [blame] | 55 | static 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 Musta | e44259b | 2014-01-07 10:05:52 -0600 | [diff] [blame] | 67 | |
| 68 | static 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_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 80 | #else |
j_mayer | 05e1d83 | 2007-11-05 13:16:23 +0000 | [diff] [blame] | 81 | void muls64(uint64_t *phigh, uint64_t *plow, int64_t a, int64_t b); |
j_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 82 | void mulu64(uint64_t *phigh, uint64_t *plow, uint64_t a, uint64_t b); |
Tom Musta | 98d1eb2 | 2014-01-07 10:05:51 -0600 | [diff] [blame] | 83 | int divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor); |
Tom Musta | e44259b | 2014-01-07 10:05:52 -0600 | [diff] [blame] | 84 | int divs128(int64_t *plow, int64_t *phigh, int64_t divisor); |
Peter Maydell | 49caffe | 2015-08-19 16:20:20 +0100 | [diff] [blame] | 85 | |
| 86 | static 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_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 108 | #endif |
| 109 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 110 | /** |
| 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 Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 117 | static inline int clz32(uint32_t val) |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 118 | { |
aurel32 | bad5b1e | 2008-10-12 16:15:04 +0000 | [diff] [blame] | 119 | #if QEMU_GNUC_PREREQ(3, 4) |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 120 | return val ? __builtin_clz(val) : 32; |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 121 | #else |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 122 | /* Binary search for the leading one bit. */ |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 123 | 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; |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 149 | #endif |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 152 | /** |
| 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 Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 158 | static inline int clo32(uint32_t val) |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 159 | { |
| 160 | return clz32(~val); |
| 161 | } |
| 162 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 163 | /** |
| 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 Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 170 | static inline int clz64(uint64_t val) |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 171 | { |
aurel32 | bad5b1e | 2008-10-12 16:15:04 +0000 | [diff] [blame] | 172 | #if QEMU_GNUC_PREREQ(3, 4) |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 173 | return val ? __builtin_clzll(val) : 64; |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 174 | #else |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 175 | int cnt = 0; |
| 176 | |
j_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 177 | if (!(val >> 32)) { |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 178 | cnt += 32; |
j_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 179 | } else { |
| 180 | val >>= 32; |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 181 | } |
j_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 182 | |
| 183 | return cnt + clz32(val); |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 184 | #endif |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 187 | /** |
| 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 Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 193 | static inline int clo64(uint64_t val) |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 194 | { |
| 195 | return clz64(~val); |
| 196 | } |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 197 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 198 | /** |
| 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 Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 205 | static inline int ctz32(uint32_t val) |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 206 | { |
aurel32 | bad5b1e | 2008-10-12 16:15:04 +0000 | [diff] [blame] | 207 | #if QEMU_GNUC_PREREQ(3, 4) |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 208 | return val ? __builtin_ctz(val) : 32; |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 209 | #else |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 210 | /* Binary search for the trailing one bit. */ |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 211 | int cnt; |
| 212 | |
| 213 | cnt = 0; |
| 214 | if (!(val & 0x0000FFFFUL)) { |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 215 | cnt += 16; |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 216 | val >>= 16; |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 217 | } |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 218 | if (!(val & 0x000000FFUL)) { |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 219 | cnt += 8; |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 220 | val >>= 8; |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 221 | } |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 222 | if (!(val & 0x0000000FUL)) { |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 223 | cnt += 4; |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 224 | val >>= 4; |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 225 | } |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 226 | if (!(val & 0x00000003UL)) { |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 227 | cnt += 2; |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 228 | val >>= 2; |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 229 | } |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 230 | if (!(val & 0x00000001UL)) { |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 231 | cnt++; |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 232 | val >>= 1; |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 233 | } |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 234 | if (!(val & 0x00000001UL)) { |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 235 | cnt++; |
| 236 | } |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 237 | |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 238 | return cnt; |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 239 | #endif |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 242 | /** |
| 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 Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 248 | static inline int cto32(uint32_t val) |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 249 | { |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 250 | return ctz32(~val); |
| 251 | } |
| 252 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 253 | /** |
| 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 Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 260 | static inline int ctz64(uint64_t val) |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 261 | { |
aurel32 | bad5b1e | 2008-10-12 16:15:04 +0000 | [diff] [blame] | 262 | #if QEMU_GNUC_PREREQ(3, 4) |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 263 | return val ? __builtin_ctzll(val) : 64; |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 264 | #else |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 265 | int cnt; |
| 266 | |
| 267 | cnt = 0; |
| 268 | if (!((uint32_t)val)) { |
| 269 | cnt += 32; |
| 270 | val >>= 32; |
| 271 | } |
| 272 | |
| 273 | return cnt + ctz32(val); |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 274 | #endif |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 277 | /** |
Dr. David Alan Gilbert | 1c884ab | 2014-02-12 17:14:33 +0000 | [diff] [blame] | 278 | * cto64 - count trailing ones in a 64-bit value. |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 279 | * @val: The value to search |
| 280 | * |
| 281 | * Returns 64 if the value is -1. |
| 282 | */ |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 283 | static inline int cto64(uint64_t val) |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 284 | { |
| 285 | return ctz64(~val); |
| 286 | } |
| 287 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 288 | /** |
Claudio Fontana | afd3fe4 | 2013-12-17 19:42:35 +0000 | [diff] [blame] | 289 | * 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 | */ |
| 295 | static 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 | */ |
| 311 | static 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 Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 321 | * ctpop8 - count the population of one bits in an 8-bit value. |
| 322 | * @val: The value to search |
| 323 | */ |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 324 | static inline int ctpop8(uint8_t val) |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 325 | { |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 326 | #if QEMU_GNUC_PREREQ(3, 4) |
| 327 | return __builtin_popcount(val); |
| 328 | #else |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 329 | 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 Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 334 | #endif |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 337 | /** |
| 338 | * ctpop16 - count the population of one bits in a 16-bit value. |
| 339 | * @val: The value to search |
| 340 | */ |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 341 | static inline int ctpop16(uint16_t val) |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 342 | { |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 343 | #if QEMU_GNUC_PREREQ(3, 4) |
| 344 | return __builtin_popcount(val); |
| 345 | #else |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 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); |
| 350 | |
| 351 | return val; |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 352 | #endif |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 355 | /** |
| 356 | * ctpop32 - count the population of one bits in a 32-bit value. |
| 357 | * @val: The value to search |
| 358 | */ |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 359 | static inline int ctpop32(uint32_t val) |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 360 | { |
aurel32 | bad5b1e | 2008-10-12 16:15:04 +0000 | [diff] [blame] | 361 | #if QEMU_GNUC_PREREQ(3, 4) |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 362 | return __builtin_popcount(val); |
| 363 | #else |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 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); |
| 369 | |
| 370 | return val; |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 371 | #endif |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 374 | /** |
| 375 | * ctpop64 - count the population of one bits in a 64-bit value. |
| 376 | * @val: The value to search |
| 377 | */ |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 378 | static inline int ctpop64(uint64_t val) |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 379 | { |
aurel32 | bad5b1e | 2008-10-12 16:15:04 +0000 | [diff] [blame] | 380 | #if QEMU_GNUC_PREREQ(3, 4) |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 381 | return __builtin_popcountll(val); |
| 382 | #else |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 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); |
| 389 | |
| 390 | return val; |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 391 | #endif |
ths | 3800af9 | 2007-12-18 01:58:05 +0000 | [diff] [blame] | 392 | } |
Paolo Bonzini | cb9c377 | 2012-12-06 12:15:58 +0100 | [diff] [blame] | 393 | |
Richard Henderson | 0165437 | 2013-02-13 17:47:34 -0800 | [diff] [blame] | 394 | /* 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 Maydell | 8f1ed5f | 2015-07-24 13:33:12 +0100 | [diff] [blame] | 412 | static 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*/ |
| 422 | static 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) */ |
| 431 | static 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 Bonzini | cb9c377 | 2012-12-06 12:15:58 +0100 | [diff] [blame] | 444 | #endif |