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> |
ths | cebdff7 | 2008-06-05 22:55:54 +0000 | [diff] [blame] | 30 | |
Richard Henderson | f540166 | 2013-02-16 12:46:59 -0800 | [diff] [blame] | 31 | #ifdef CONFIG_INT128 |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 32 | static inline void mulu64(uint64_t *plow, uint64_t *phigh, |
| 33 | uint64_t a, uint64_t b) |
j_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 34 | { |
Richard Henderson | f540166 | 2013-02-16 12:46:59 -0800 | [diff] [blame] | 35 | __uint128_t r = (__uint128_t)a * b; |
| 36 | *plow = r; |
| 37 | *phigh = r >> 64; |
j_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 38 | } |
Richard Henderson | f540166 | 2013-02-16 12:46:59 -0800 | [diff] [blame] | 39 | |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 40 | static inline void muls64(uint64_t *plow, uint64_t *phigh, |
| 41 | int64_t a, int64_t b) |
j_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 42 | { |
Richard Henderson | f540166 | 2013-02-16 12:46:59 -0800 | [diff] [blame] | 43 | __int128_t r = (__int128_t)a * b; |
| 44 | *plow = r; |
| 45 | *phigh = r >> 64; |
j_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 46 | } |
Tom Musta | 98d1eb2 | 2014-01-07 10:05:51 -0600 | [diff] [blame^] | 47 | |
| 48 | static inline int divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor) |
| 49 | { |
| 50 | if (divisor == 0) { |
| 51 | return 1; |
| 52 | } else { |
| 53 | __uint128_t dividend = ((__uint128_t)*phigh << 64) | *plow; |
| 54 | __uint128_t result = dividend / divisor; |
| 55 | *plow = result; |
| 56 | *phigh = dividend % divisor; |
| 57 | return result > UINT64_MAX; |
| 58 | } |
| 59 | } |
j_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 60 | #else |
j_mayer | 05e1d83 | 2007-11-05 13:16:23 +0000 | [diff] [blame] | 61 | 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] | 62 | 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^] | 63 | int divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor); |
j_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 64 | #endif |
| 65 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 66 | /** |
| 67 | * clz32 - count leading zeros in a 32-bit value. |
| 68 | * @val: The value to search |
| 69 | * |
| 70 | * Returns 32 if the value is zero. Note that the GCC builtin is |
| 71 | * undefined if the value is zero. |
| 72 | */ |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 73 | static inline int clz32(uint32_t val) |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 74 | { |
aurel32 | bad5b1e | 2008-10-12 16:15:04 +0000 | [diff] [blame] | 75 | #if QEMU_GNUC_PREREQ(3, 4) |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 76 | return val ? __builtin_clz(val) : 32; |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 77 | #else |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 78 | /* Binary search for the leading one bit. */ |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 79 | int cnt = 0; |
| 80 | |
| 81 | if (!(val & 0xFFFF0000U)) { |
| 82 | cnt += 16; |
| 83 | val <<= 16; |
| 84 | } |
| 85 | if (!(val & 0xFF000000U)) { |
| 86 | cnt += 8; |
| 87 | val <<= 8; |
| 88 | } |
| 89 | if (!(val & 0xF0000000U)) { |
| 90 | cnt += 4; |
| 91 | val <<= 4; |
| 92 | } |
| 93 | if (!(val & 0xC0000000U)) { |
| 94 | cnt += 2; |
| 95 | val <<= 2; |
| 96 | } |
| 97 | if (!(val & 0x80000000U)) { |
| 98 | cnt++; |
| 99 | val <<= 1; |
| 100 | } |
| 101 | if (!(val & 0x80000000U)) { |
| 102 | cnt++; |
| 103 | } |
| 104 | return cnt; |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 105 | #endif |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 108 | /** |
| 109 | * clo32 - count leading ones in a 32-bit value. |
| 110 | * @val: The value to search |
| 111 | * |
| 112 | * Returns 32 if the value is -1. |
| 113 | */ |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 114 | static inline int clo32(uint32_t val) |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 115 | { |
| 116 | return clz32(~val); |
| 117 | } |
| 118 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 119 | /** |
| 120 | * clz64 - count leading zeros in a 64-bit value. |
| 121 | * @val: The value to search |
| 122 | * |
| 123 | * Returns 64 if the value is zero. Note that the GCC builtin is |
| 124 | * undefined if the value is zero. |
| 125 | */ |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 126 | static inline int clz64(uint64_t val) |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 127 | { |
aurel32 | bad5b1e | 2008-10-12 16:15:04 +0000 | [diff] [blame] | 128 | #if QEMU_GNUC_PREREQ(3, 4) |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 129 | return val ? __builtin_clzll(val) : 64; |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 130 | #else |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 131 | int cnt = 0; |
| 132 | |
j_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 133 | if (!(val >> 32)) { |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 134 | cnt += 32; |
j_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 135 | } else { |
| 136 | val >>= 32; |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 137 | } |
j_mayer | 7a51ad8 | 2007-11-04 02:24:58 +0000 | [diff] [blame] | 138 | |
| 139 | return cnt + clz32(val); |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 140 | #endif |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 143 | /** |
| 144 | * clo64 - count leading ones in a 64-bit value. |
| 145 | * @val: The value to search |
| 146 | * |
| 147 | * Returns 64 if the value is -1. |
| 148 | */ |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 149 | static inline int clo64(uint64_t val) |
ths | 05f778c | 2007-10-27 13:05:54 +0000 | [diff] [blame] | 150 | { |
| 151 | return clz64(~val); |
| 152 | } |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 153 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 154 | /** |
| 155 | * ctz32 - count trailing zeros in a 32-bit value. |
| 156 | * @val: The value to search |
| 157 | * |
| 158 | * Returns 32 if the value is zero. Note that the GCC builtin is |
| 159 | * undefined if the value is zero. |
| 160 | */ |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 161 | static inline int ctz32(uint32_t val) |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 162 | { |
aurel32 | bad5b1e | 2008-10-12 16:15:04 +0000 | [diff] [blame] | 163 | #if QEMU_GNUC_PREREQ(3, 4) |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 164 | return val ? __builtin_ctz(val) : 32; |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 165 | #else |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 166 | /* Binary search for the trailing one bit. */ |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 167 | int cnt; |
| 168 | |
| 169 | cnt = 0; |
| 170 | if (!(val & 0x0000FFFFUL)) { |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 171 | cnt += 16; |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 172 | val >>= 16; |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 173 | } |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 174 | if (!(val & 0x000000FFUL)) { |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 175 | cnt += 8; |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 176 | val >>= 8; |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 177 | } |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 178 | if (!(val & 0x0000000FUL)) { |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 179 | cnt += 4; |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 180 | val >>= 4; |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 181 | } |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 182 | if (!(val & 0x00000003UL)) { |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 183 | cnt += 2; |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 184 | val >>= 2; |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 185 | } |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 186 | if (!(val & 0x00000001UL)) { |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 187 | cnt++; |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 188 | val >>= 1; |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 189 | } |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 190 | if (!(val & 0x00000001UL)) { |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 191 | cnt++; |
| 192 | } |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 193 | |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 194 | return cnt; |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 195 | #endif |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 198 | /** |
| 199 | * cto32 - count trailing ones in a 32-bit value. |
| 200 | * @val: The value to search |
| 201 | * |
| 202 | * Returns 32 if the value is -1. |
| 203 | */ |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 204 | static inline int cto32(uint32_t val) |
balrog | c890684 | 2008-11-12 17:18:41 +0000 | [diff] [blame] | 205 | { |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 206 | return ctz32(~val); |
| 207 | } |
| 208 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 209 | /** |
| 210 | * ctz64 - count trailing zeros in a 64-bit value. |
| 211 | * @val: The value to search |
| 212 | * |
| 213 | * Returns 64 if the value is zero. Note that the GCC builtin is |
| 214 | * undefined if the value is zero. |
| 215 | */ |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 216 | static inline int ctz64(uint64_t val) |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 217 | { |
aurel32 | bad5b1e | 2008-10-12 16:15:04 +0000 | [diff] [blame] | 218 | #if QEMU_GNUC_PREREQ(3, 4) |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 219 | return val ? __builtin_ctzll(val) : 64; |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 220 | #else |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 221 | int cnt; |
| 222 | |
| 223 | cnt = 0; |
| 224 | if (!((uint32_t)val)) { |
| 225 | cnt += 32; |
| 226 | val >>= 32; |
| 227 | } |
| 228 | |
| 229 | return cnt + ctz32(val); |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 230 | #endif |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 233 | /** |
Dr. David Alan Gilbert | 1c884ab | 2014-02-12 17:14:33 +0000 | [diff] [blame] | 234 | * cto64 - count trailing ones in a 64-bit value. |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 235 | * @val: The value to search |
| 236 | * |
| 237 | * Returns 64 if the value is -1. |
| 238 | */ |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 239 | static inline int cto64(uint64_t val) |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 240 | { |
| 241 | return ctz64(~val); |
| 242 | } |
| 243 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 244 | /** |
Claudio Fontana | afd3fe4 | 2013-12-17 19:42:35 +0000 | [diff] [blame] | 245 | * clrsb32 - count leading redundant sign bits in a 32-bit value. |
| 246 | * @val: The value to search |
| 247 | * |
| 248 | * Returns the number of bits following the sign bit that are equal to it. |
| 249 | * No special cases; output range is [0-31]. |
| 250 | */ |
| 251 | static inline int clrsb32(uint32_t val) |
| 252 | { |
| 253 | #if QEMU_GNUC_PREREQ(4, 7) |
| 254 | return __builtin_clrsb(val); |
| 255 | #else |
| 256 | return clz32(val ^ ((int32_t)val >> 1)) - 1; |
| 257 | #endif |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * clrsb64 - count leading redundant sign bits in a 64-bit value. |
| 262 | * @val: The value to search |
| 263 | * |
| 264 | * Returns the number of bits following the sign bit that are equal to it. |
| 265 | * No special cases; output range is [0-63]. |
| 266 | */ |
| 267 | static inline int clrsb64(uint64_t val) |
| 268 | { |
| 269 | #if QEMU_GNUC_PREREQ(4, 7) |
| 270 | return __builtin_clrsbll(val); |
| 271 | #else |
| 272 | return clz64(val ^ ((int64_t)val >> 1)) - 1; |
| 273 | #endif |
| 274 | } |
| 275 | |
| 276 | /** |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 277 | * ctpop8 - count the population of one bits in an 8-bit value. |
| 278 | * @val: The value to search |
| 279 | */ |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 280 | static inline int ctpop8(uint8_t val) |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 281 | { |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 282 | #if QEMU_GNUC_PREREQ(3, 4) |
| 283 | return __builtin_popcount(val); |
| 284 | #else |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 285 | val = (val & 0x55) + ((val >> 1) & 0x55); |
| 286 | val = (val & 0x33) + ((val >> 2) & 0x33); |
| 287 | val = (val & 0x0f) + ((val >> 4) & 0x0f); |
| 288 | |
| 289 | return val; |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 290 | #endif |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 293 | /** |
| 294 | * ctpop16 - count the population of one bits in a 16-bit value. |
| 295 | * @val: The value to search |
| 296 | */ |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 297 | static inline int ctpop16(uint16_t val) |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 298 | { |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 299 | #if QEMU_GNUC_PREREQ(3, 4) |
| 300 | return __builtin_popcount(val); |
| 301 | #else |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 302 | val = (val & 0x5555) + ((val >> 1) & 0x5555); |
| 303 | val = (val & 0x3333) + ((val >> 2) & 0x3333); |
| 304 | val = (val & 0x0f0f) + ((val >> 4) & 0x0f0f); |
| 305 | val = (val & 0x00ff) + ((val >> 8) & 0x00ff); |
| 306 | |
| 307 | return val; |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 308 | #endif |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 311 | /** |
| 312 | * ctpop32 - count the population of one bits in a 32-bit value. |
| 313 | * @val: The value to search |
| 314 | */ |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 315 | static inline int ctpop32(uint32_t val) |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 316 | { |
aurel32 | bad5b1e | 2008-10-12 16:15:04 +0000 | [diff] [blame] | 317 | #if QEMU_GNUC_PREREQ(3, 4) |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 318 | return __builtin_popcount(val); |
| 319 | #else |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 320 | val = (val & 0x55555555) + ((val >> 1) & 0x55555555); |
| 321 | val = (val & 0x33333333) + ((val >> 2) & 0x33333333); |
| 322 | val = (val & 0x0f0f0f0f) + ((val >> 4) & 0x0f0f0f0f); |
| 323 | val = (val & 0x00ff00ff) + ((val >> 8) & 0x00ff00ff); |
| 324 | val = (val & 0x0000ffff) + ((val >> 16) & 0x0000ffff); |
| 325 | |
| 326 | return val; |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 327 | #endif |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Richard Henderson | 72d8115 | 2013-02-13 17:47:35 -0800 | [diff] [blame] | 330 | /** |
| 331 | * ctpop64 - count the population of one bits in a 64-bit value. |
| 332 | * @val: The value to search |
| 333 | */ |
Blue Swirl | facd285 | 2009-08-16 08:03:26 +0000 | [diff] [blame] | 334 | static inline int ctpop64(uint64_t val) |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 335 | { |
aurel32 | bad5b1e | 2008-10-12 16:15:04 +0000 | [diff] [blame] | 336 | #if QEMU_GNUC_PREREQ(3, 4) |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 337 | return __builtin_popcountll(val); |
| 338 | #else |
j_mayer | b9ef45f | 2007-10-28 12:52:38 +0000 | [diff] [blame] | 339 | val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL); |
| 340 | val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL); |
| 341 | val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >> 4) & 0x0f0f0f0f0f0f0f0fULL); |
| 342 | val = (val & 0x00ff00ff00ff00ffULL) + ((val >> 8) & 0x00ff00ff00ff00ffULL); |
| 343 | val = (val & 0x0000ffff0000ffffULL) + ((val >> 16) & 0x0000ffff0000ffffULL); |
| 344 | val = (val & 0x00000000ffffffffULL) + ((val >> 32) & 0x00000000ffffffffULL); |
| 345 | |
| 346 | return val; |
aurel32 | 7d01998 | 2008-10-12 00:53:08 +0000 | [diff] [blame] | 347 | #endif |
ths | 3800af9 | 2007-12-18 01:58:05 +0000 | [diff] [blame] | 348 | } |
Paolo Bonzini | cb9c377 | 2012-12-06 12:15:58 +0100 | [diff] [blame] | 349 | |
Richard Henderson | 0165437 | 2013-02-13 17:47:34 -0800 | [diff] [blame] | 350 | /* Host type specific sizes of these routines. */ |
| 351 | |
| 352 | #if ULONG_MAX == UINT32_MAX |
| 353 | # define clzl clz32 |
| 354 | # define ctzl ctz32 |
| 355 | # define clol clo32 |
| 356 | # define ctol cto32 |
| 357 | # define ctpopl ctpop32 |
| 358 | #elif ULONG_MAX == UINT64_MAX |
| 359 | # define clzl clz64 |
| 360 | # define ctzl ctz64 |
| 361 | # define clol clo64 |
| 362 | # define ctol cto64 |
| 363 | # define ctpopl ctpop64 |
| 364 | #else |
| 365 | # error Unknown sizeof long |
| 366 | #endif |
| 367 | |
Paolo Bonzini | cb9c377 | 2012-12-06 12:15:58 +0100 | [diff] [blame] | 368 | #endif |