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