| 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 | */ |
| 25 | |
| 26 | /* Portions of this work are licensed under the terms of the GNU GPL, |
| 27 | * version 2 or later. See the COPYING file in the top-level directory. |
| 28 | */ |
| 29 | |
| 30 | #ifndef HOST_UTILS_H |
| 31 | #define HOST_UTILS_H |
| 32 | |
| 33 | #include "qemu/int128.h" |
| 34 | |
| 35 | #ifdef CONFIG_INT128 |
| 36 | static inline void mulu64(uint64_t *plow, uint64_t *phigh, |
| 37 | uint64_t a, uint64_t b) |
| 38 | { |
| 39 | __uint128_t r = (__uint128_t)a * b; |
| 40 | *plow = r; |
| 41 | *phigh = r >> 64; |
| 42 | } |
| 43 | |
| 44 | static inline void muls64(uint64_t *plow, uint64_t *phigh, |
| 45 | int64_t a, int64_t b) |
| 46 | { |
| 47 | __int128_t r = (__int128_t)a * b; |
| 48 | *plow = r; |
| 49 | *phigh = r >> 64; |
| 50 | } |
| 51 | |
| 52 | /* compute with 96 bit intermediate result: (a*b)/c */ |
| 53 | static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
| 54 | { |
| 55 | return (__int128_t)a * b / c; |
| 56 | } |
| 57 | |
| 58 | static inline uint64_t muldiv64_round_up(uint64_t a, uint32_t b, uint32_t c) |
| 59 | { |
| 60 | return ((__int128_t)a * b + c - 1) / c; |
| 61 | } |
| 62 | |
| 63 | static inline uint64_t divu128(uint64_t *plow, uint64_t *phigh, |
| 64 | uint64_t divisor) |
| 65 | { |
| 66 | __uint128_t dividend = ((__uint128_t)*phigh << 64) | *plow; |
| 67 | __uint128_t result = dividend / divisor; |
| 68 | |
| 69 | *plow = result; |
| 70 | *phigh = result >> 64; |
| 71 | return dividend % divisor; |
| 72 | } |
| 73 | |
| 74 | static inline int64_t divs128(uint64_t *plow, int64_t *phigh, |
| 75 | int64_t divisor) |
| 76 | { |
| 77 | __int128_t dividend = ((__int128_t)*phigh << 64) | *plow; |
| 78 | __int128_t result = dividend / divisor; |
| 79 | |
| 80 | *plow = result; |
| 81 | *phigh = result >> 64; |
| 82 | return dividend % divisor; |
| 83 | } |
| 84 | #else |
| 85 | void muls64(uint64_t *plow, uint64_t *phigh, int64_t a, int64_t b); |
| 86 | void mulu64(uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b); |
| 87 | uint64_t divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor); |
| 88 | int64_t divs128(uint64_t *plow, int64_t *phigh, int64_t divisor); |
| 89 | |
| 90 | static inline uint64_t muldiv64_rounding(uint64_t a, uint32_t b, uint32_t c, |
| 91 | bool round_up) |
| 92 | { |
| 93 | union { |
| 94 | uint64_t ll; |
| 95 | struct { |
| 96 | #if HOST_BIG_ENDIAN |
| 97 | uint32_t high, low; |
| 98 | #else |
| 99 | uint32_t low, high; |
| 100 | #endif |
| 101 | } l; |
| 102 | } u, res; |
| 103 | uint64_t rl, rh; |
| 104 | |
| 105 | u.ll = a; |
| 106 | rl = (uint64_t)u.l.low * (uint64_t)b; |
| 107 | if (round_up) { |
| 108 | rl += c - 1; |
| 109 | } |
| 110 | rh = (uint64_t)u.l.high * (uint64_t)b; |
| 111 | rh += (rl >> 32); |
| 112 | res.l.high = rh / c; |
| 113 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
| 114 | return res.ll; |
| 115 | } |
| 116 | |
| 117 | static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
| 118 | { |
| 119 | return muldiv64_rounding(a, b, c, false); |
| 120 | } |
| 121 | |
| 122 | static inline uint64_t muldiv64_round_up(uint64_t a, uint32_t b, uint32_t c) |
| 123 | { |
| 124 | return muldiv64_rounding(a, b, c, true); |
| 125 | } |
| 126 | #endif |
| 127 | |
| 128 | /** |
| 129 | * clz8 - count leading zeros in a 8-bit value. |
| 130 | * @val: The value to search |
| 131 | * |
| 132 | * Returns 8 if the value is zero. Note that the GCC builtin is |
| 133 | * undefined if the value is zero. |
| 134 | * |
| 135 | * Note that the GCC builtin will upcast its argument to an `unsigned int` |
| 136 | * so this function subtracts off the number of prepended zeroes. |
| 137 | */ |
| 138 | static inline int clz8(uint8_t val) |
| 139 | { |
| 140 | return val ? __builtin_clz(val) - 24 : 8; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * clz16 - count leading zeros in a 16-bit value. |
| 145 | * @val: The value to search |
| 146 | * |
| 147 | * Returns 16 if the value is zero. Note that the GCC builtin is |
| 148 | * undefined if the value is zero. |
| 149 | * |
| 150 | * Note that the GCC builtin will upcast its argument to an `unsigned int` |
| 151 | * so this function subtracts off the number of prepended zeroes. |
| 152 | */ |
| 153 | static inline int clz16(uint16_t val) |
| 154 | { |
| 155 | return val ? __builtin_clz(val) - 16 : 16; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * clz32 - count leading zeros in a 32-bit value. |
| 160 | * @val: The value to search |
| 161 | * |
| 162 | * Returns 32 if the value is zero. Note that the GCC builtin is |
| 163 | * undefined if the value is zero. |
| 164 | */ |
| 165 | static inline int clz32(uint32_t val) |
| 166 | { |
| 167 | return val ? __builtin_clz(val) : 32; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * clo32 - count leading ones in a 32-bit value. |
| 172 | * @val: The value to search |
| 173 | * |
| 174 | * Returns 32 if the value is -1. |
| 175 | */ |
| 176 | static inline int clo32(uint32_t val) |
| 177 | { |
| 178 | return clz32(~val); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * clz64 - count leading zeros in a 64-bit value. |
| 183 | * @val: The value to search |
| 184 | * |
| 185 | * Returns 64 if the value is zero. Note that the GCC builtin is |
| 186 | * undefined if the value is zero. |
| 187 | */ |
| 188 | static inline int clz64(uint64_t val) |
| 189 | { |
| 190 | return val ? __builtin_clzll(val) : 64; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * clo64 - count leading ones in a 64-bit value. |
| 195 | * @val: The value to search |
| 196 | * |
| 197 | * Returns 64 if the value is -1. |
| 198 | */ |
| 199 | static inline int clo64(uint64_t val) |
| 200 | { |
| 201 | return clz64(~val); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * ctz8 - count trailing zeros in a 8-bit value. |
| 206 | * @val: The value to search |
| 207 | * |
| 208 | * Returns 8 if the value is zero. Note that the GCC builtin is |
| 209 | * undefined if the value is zero. |
| 210 | */ |
| 211 | static inline int ctz8(uint8_t val) |
| 212 | { |
| 213 | return val ? __builtin_ctz(val) : 8; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * ctz16 - count trailing zeros in a 16-bit value. |
| 218 | * @val: The value to search |
| 219 | * |
| 220 | * Returns 16 if the value is zero. Note that the GCC builtin is |
| 221 | * undefined if the value is zero. |
| 222 | */ |
| 223 | static inline int ctz16(uint16_t val) |
| 224 | { |
| 225 | return val ? __builtin_ctz(val) : 16; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * ctz32 - count trailing zeros in a 32-bit value. |
| 230 | * @val: The value to search |
| 231 | * |
| 232 | * Returns 32 if the value is zero. Note that the GCC builtin is |
| 233 | * undefined if the value is zero. |
| 234 | */ |
| 235 | static inline int ctz32(uint32_t val) |
| 236 | { |
| 237 | return val ? __builtin_ctz(val) : 32; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * cto32 - count trailing ones in a 32-bit value. |
| 242 | * @val: The value to search |
| 243 | * |
| 244 | * Returns 32 if the value is -1. |
| 245 | */ |
| 246 | static inline int cto32(uint32_t val) |
| 247 | { |
| 248 | return ctz32(~val); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * ctz64 - count trailing zeros in a 64-bit value. |
| 253 | * @val: The value to search |
| 254 | * |
| 255 | * Returns 64 if the value is zero. Note that the GCC builtin is |
| 256 | * undefined if the value is zero. |
| 257 | */ |
| 258 | static inline int ctz64(uint64_t val) |
| 259 | { |
| 260 | return val ? __builtin_ctzll(val) : 64; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * cto64 - count trailing ones in a 64-bit value. |
| 265 | * @val: The value to search |
| 266 | * |
| 267 | * Returns 64 if the value is -1. |
| 268 | */ |
| 269 | static inline int cto64(uint64_t val) |
| 270 | { |
| 271 | return ctz64(~val); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * clrsb32 - count leading redundant sign bits in a 32-bit value. |
| 276 | * @val: The value to search |
| 277 | * |
| 278 | * Returns the number of bits following the sign bit that are equal to it. |
| 279 | * No special cases; output range is [0-31]. |
| 280 | */ |
| 281 | static inline int clrsb32(uint32_t val) |
| 282 | { |
| 283 | #if __has_builtin(__builtin_clrsb) || !defined(__clang__) |
| 284 | return __builtin_clrsb(val); |
| 285 | #else |
| 286 | return clz32(val ^ ((int32_t)val >> 1)) - 1; |
| 287 | #endif |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * clrsb64 - count leading redundant sign bits in a 64-bit value. |
| 292 | * @val: The value to search |
| 293 | * |
| 294 | * Returns the number of bits following the sign bit that are equal to it. |
| 295 | * No special cases; output range is [0-63]. |
| 296 | */ |
| 297 | static inline int clrsb64(uint64_t val) |
| 298 | { |
| 299 | #if __has_builtin(__builtin_clrsbll) || !defined(__clang__) |
| 300 | return __builtin_clrsbll(val); |
| 301 | #else |
| 302 | return clz64(val ^ ((int64_t)val >> 1)) - 1; |
| 303 | #endif |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * ctpop8 - count the population of one bits in an 8-bit value. |
| 308 | * @val: The value to search |
| 309 | */ |
| 310 | static inline int ctpop8(uint8_t val) |
| 311 | { |
| 312 | return __builtin_popcount(val); |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * parity8 - return the parity (1 = odd) of an 8-bit value. |
| 317 | * @val: The value to search |
| 318 | */ |
| 319 | static inline int parity8(uint8_t val) |
| 320 | { |
| 321 | return __builtin_parity(val); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * ctpop16 - count the population of one bits in a 16-bit value. |
| 326 | * @val: The value to search |
| 327 | */ |
| 328 | static inline int ctpop16(uint16_t val) |
| 329 | { |
| 330 | return __builtin_popcount(val); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * ctpop32 - count the population of one bits in a 32-bit value. |
| 335 | * @val: The value to search |
| 336 | */ |
| 337 | static inline int ctpop32(uint32_t val) |
| 338 | { |
| 339 | return __builtin_popcount(val); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * ctpop64 - count the population of one bits in a 64-bit value. |
| 344 | * @val: The value to search |
| 345 | */ |
| 346 | static inline int ctpop64(uint64_t val) |
| 347 | { |
| 348 | return __builtin_popcountll(val); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * revbit8 - reverse the bits in an 8-bit value. |
| 353 | * @x: The value to modify. |
| 354 | */ |
| 355 | static inline uint8_t revbit8(uint8_t x) |
| 356 | { |
| 357 | #if __has_builtin(__builtin_bitreverse8) |
| 358 | return __builtin_bitreverse8(x); |
| 359 | #else |
| 360 | /* Assign the correct nibble position. */ |
| 361 | x = ((x & 0xf0) >> 4) |
| 362 | | ((x & 0x0f) << 4); |
| 363 | /* Assign the correct bit position. */ |
| 364 | x = ((x & 0x88) >> 3) |
| 365 | | ((x & 0x44) >> 1) |
| 366 | | ((x & 0x22) << 1) |
| 367 | | ((x & 0x11) << 3); |
| 368 | return x; |
| 369 | #endif |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * revbit16 - reverse the bits in a 16-bit value. |
| 374 | * @x: The value to modify. |
| 375 | */ |
| 376 | static inline uint16_t revbit16(uint16_t x) |
| 377 | { |
| 378 | #if __has_builtin(__builtin_bitreverse16) |
| 379 | return __builtin_bitreverse16(x); |
| 380 | #else |
| 381 | /* Assign the correct byte position. */ |
| 382 | x = __builtin_bswap16(x); |
| 383 | /* Assign the correct nibble position. */ |
| 384 | x = ((x & 0xf0f0) >> 4) |
| 385 | | ((x & 0x0f0f) << 4); |
| 386 | /* Assign the correct bit position. */ |
| 387 | x = ((x & 0x8888) >> 3) |
| 388 | | ((x & 0x4444) >> 1) |
| 389 | | ((x & 0x2222) << 1) |
| 390 | | ((x & 0x1111) << 3); |
| 391 | return x; |
| 392 | #endif |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * revbit32 - reverse the bits in a 32-bit value. |
| 397 | * @x: The value to modify. |
| 398 | */ |
| 399 | static inline uint32_t revbit32(uint32_t x) |
| 400 | { |
| 401 | #if __has_builtin(__builtin_bitreverse32) |
| 402 | return __builtin_bitreverse32(x); |
| 403 | #else |
| 404 | /* Assign the correct byte position. */ |
| 405 | x = __builtin_bswap32(x); |
| 406 | /* Assign the correct nibble position. */ |
| 407 | x = ((x & 0xf0f0f0f0u) >> 4) |
| 408 | | ((x & 0x0f0f0f0fu) << 4); |
| 409 | /* Assign the correct bit position. */ |
| 410 | x = ((x & 0x88888888u) >> 3) |
| 411 | | ((x & 0x44444444u) >> 1) |
| 412 | | ((x & 0x22222222u) << 1) |
| 413 | | ((x & 0x11111111u) << 3); |
| 414 | return x; |
| 415 | #endif |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * revbit64 - reverse the bits in a 64-bit value. |
| 420 | * @x: The value to modify. |
| 421 | */ |
| 422 | static inline uint64_t revbit64(uint64_t x) |
| 423 | { |
| 424 | #if __has_builtin(__builtin_bitreverse64) |
| 425 | return __builtin_bitreverse64(x); |
| 426 | #else |
| 427 | /* Assign the correct byte position. */ |
| 428 | x = __builtin_bswap64(x); |
| 429 | /* Assign the correct nibble position. */ |
| 430 | x = ((x & 0xf0f0f0f0f0f0f0f0ull) >> 4) |
| 431 | | ((x & 0x0f0f0f0f0f0f0f0full) << 4); |
| 432 | /* Assign the correct bit position. */ |
| 433 | x = ((x & 0x8888888888888888ull) >> 3) |
| 434 | | ((x & 0x4444444444444444ull) >> 1) |
| 435 | | ((x & 0x2222222222222222ull) << 1) |
| 436 | | ((x & 0x1111111111111111ull) << 3); |
| 437 | return x; |
| 438 | #endif |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Return the absolute value of a 64-bit integer as an unsigned 64-bit value |
| 443 | */ |
| 444 | static inline uint64_t uabs64(int64_t v) |
| 445 | { |
| 446 | return v < 0 ? -v : v; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * sadd32_overflow - addition with overflow indication |
| 451 | * @x, @y: addends |
| 452 | * @ret: Output for sum |
| 453 | * |
| 454 | * Computes *@ret = @x + @y, and returns true if and only if that |
| 455 | * value has been truncated. |
| 456 | */ |
| 457 | static inline bool sadd32_overflow(int32_t x, int32_t y, int32_t *ret) |
| 458 | { |
| 459 | return __builtin_add_overflow(x, y, ret); |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * sadd64_overflow - addition with overflow indication |
| 464 | * @x, @y: addends |
| 465 | * @ret: Output for sum |
| 466 | * |
| 467 | * Computes *@ret = @x + @y, and returns true if and only if that |
| 468 | * value has been truncated. |
| 469 | */ |
| 470 | static inline bool sadd64_overflow(int64_t x, int64_t y, int64_t *ret) |
| 471 | { |
| 472 | return __builtin_add_overflow(x, y, ret); |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * uadd32_overflow - addition with overflow indication |
| 477 | * @x, @y: addends |
| 478 | * @ret: Output for sum |
| 479 | * |
| 480 | * Computes *@ret = @x + @y, and returns true if and only if that |
| 481 | * value has been truncated. |
| 482 | */ |
| 483 | static inline bool uadd32_overflow(uint32_t x, uint32_t y, uint32_t *ret) |
| 484 | { |
| 485 | return __builtin_add_overflow(x, y, ret); |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * uadd64_overflow - addition with overflow indication |
| 490 | * @x, @y: addends |
| 491 | * @ret: Output for sum |
| 492 | * |
| 493 | * Computes *@ret = @x + @y, and returns true if and only if that |
| 494 | * value has been truncated. |
| 495 | */ |
| 496 | static inline bool uadd64_overflow(uint64_t x, uint64_t y, uint64_t *ret) |
| 497 | { |
| 498 | return __builtin_add_overflow(x, y, ret); |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * ssub32_overflow - subtraction with overflow indication |
| 503 | * @x: Minuend |
| 504 | * @y: Subtrahend |
| 505 | * @ret: Output for difference |
| 506 | * |
| 507 | * Computes *@ret = @x - @y, and returns true if and only if that |
| 508 | * value has been truncated. |
| 509 | */ |
| 510 | static inline bool ssub32_overflow(int32_t x, int32_t y, int32_t *ret) |
| 511 | { |
| 512 | return __builtin_sub_overflow(x, y, ret); |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * ssub64_overflow - subtraction with overflow indication |
| 517 | * @x: Minuend |
| 518 | * @y: Subtrahend |
| 519 | * @ret: Output for sum |
| 520 | * |
| 521 | * Computes *@ret = @x - @y, and returns true if and only if that |
| 522 | * value has been truncated. |
| 523 | */ |
| 524 | static inline bool ssub64_overflow(int64_t x, int64_t y, int64_t *ret) |
| 525 | { |
| 526 | return __builtin_sub_overflow(x, y, ret); |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * usub32_overflow - subtraction with overflow indication |
| 531 | * @x: Minuend |
| 532 | * @y: Subtrahend |
| 533 | * @ret: Output for sum |
| 534 | * |
| 535 | * Computes *@ret = @x - @y, and returns true if and only if that |
| 536 | * value has been truncated. |
| 537 | */ |
| 538 | static inline bool usub32_overflow(uint32_t x, uint32_t y, uint32_t *ret) |
| 539 | { |
| 540 | return __builtin_sub_overflow(x, y, ret); |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * usub64_overflow - subtraction with overflow indication |
| 545 | * @x: Minuend |
| 546 | * @y: Subtrahend |
| 547 | * @ret: Output for sum |
| 548 | * |
| 549 | * Computes *@ret = @x - @y, and returns true if and only if that |
| 550 | * value has been truncated. |
| 551 | */ |
| 552 | static inline bool usub64_overflow(uint64_t x, uint64_t y, uint64_t *ret) |
| 553 | { |
| 554 | return __builtin_sub_overflow(x, y, ret); |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * smul32_overflow - multiplication with overflow indication |
| 559 | * @x, @y: Input multipliers |
| 560 | * @ret: Output for product |
| 561 | * |
| 562 | * Computes *@ret = @x * @y, and returns true if and only if that |
| 563 | * value has been truncated. |
| 564 | */ |
| 565 | static inline bool smul32_overflow(int32_t x, int32_t y, int32_t *ret) |
| 566 | { |
| 567 | return __builtin_mul_overflow(x, y, ret); |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * smul64_overflow - multiplication with overflow indication |
| 572 | * @x, @y: Input multipliers |
| 573 | * @ret: Output for product |
| 574 | * |
| 575 | * Computes *@ret = @x * @y, and returns true if and only if that |
| 576 | * value has been truncated. |
| 577 | */ |
| 578 | static inline bool smul64_overflow(int64_t x, int64_t y, int64_t *ret) |
| 579 | { |
| 580 | return __builtin_mul_overflow(x, y, ret); |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * umul32_overflow - multiplication with overflow indication |
| 585 | * @x, @y: Input multipliers |
| 586 | * @ret: Output for product |
| 587 | * |
| 588 | * Computes *@ret = @x * @y, and returns true if and only if that |
| 589 | * value has been truncated. |
| 590 | */ |
| 591 | static inline bool umul32_overflow(uint32_t x, uint32_t y, uint32_t *ret) |
| 592 | { |
| 593 | return __builtin_mul_overflow(x, y, ret); |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * umul64_overflow - multiplication with overflow indication |
| 598 | * @x, @y: Input multipliers |
| 599 | * @ret: Output for product |
| 600 | * |
| 601 | * Computes *@ret = @x * @y, and returns true if and only if that |
| 602 | * value has been truncated. |
| 603 | */ |
| 604 | static inline bool umul64_overflow(uint64_t x, uint64_t y, uint64_t *ret) |
| 605 | { |
| 606 | return __builtin_mul_overflow(x, y, ret); |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * sadd32_saturate - 32-bit signed addition with saturation |
| 611 | * @x, @y: addends |
| 612 | * |
| 613 | * Computes @x + @y, and saturates rather than truncating the result. |
| 614 | */ |
| 615 | static inline int32_t sadd32_saturate(int32_t x, int32_t y) |
| 616 | { |
| 617 | int32_t ret; |
| 618 | if (sadd32_overflow(x, y, &ret)) { |
| 619 | ret = y < 0 ? INT32_MIN : INT32_MAX; |
| 620 | } |
| 621 | return ret; |
| 622 | } |
| 623 | |
| 624 | /** |
| 625 | * sadd64_saturate - 64-bit signed addition with saturation |
| 626 | * @x, @y: addends |
| 627 | * |
| 628 | * Computes @x + @y, and saturates rather than truncating the result. |
| 629 | */ |
| 630 | static inline int64_t sadd64_saturate(int64_t x, int64_t y) |
| 631 | { |
| 632 | int64_t ret; |
| 633 | if (sadd64_overflow(x, y, &ret)) { |
| 634 | ret = y < 0 ? INT64_MIN : INT64_MAX; |
| 635 | } |
| 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * ssub32_saturate - 32-bit signed subtraction with saturation |
| 641 | * @x, @y: addends |
| 642 | * |
| 643 | * Computes @x - @y, and saturates rather than truncating the result. |
| 644 | */ |
| 645 | static inline int32_t ssub32_saturate(int32_t x, int32_t y) |
| 646 | { |
| 647 | int32_t ret; |
| 648 | if (ssub32_overflow(x, y, &ret)) { |
| 649 | ret = x < 0 ? INT32_MIN : INT32_MAX; |
| 650 | } |
| 651 | return ret; |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * ssub64_saturate - 64-bit signed subtraction with saturation |
| 656 | * @x, @y: addends |
| 657 | * |
| 658 | * Computes @x - @y, and saturates rather than truncating the result. |
| 659 | */ |
| 660 | static inline int64_t ssub64_saturate(int64_t x, int64_t y) |
| 661 | { |
| 662 | int64_t ret; |
| 663 | if (ssub64_overflow(x, y, &ret)) { |
| 664 | ret = x < 0 ? INT64_MIN : INT64_MAX; |
| 665 | } |
| 666 | return ret; |
| 667 | } |
| 668 | |
| 669 | /* |
| 670 | * Unsigned 128x64 multiplication. |
| 671 | * Returns true if the result got truncated to 128 bits. |
| 672 | * Otherwise, returns false and the multiplication result via plow and phigh. |
| 673 | */ |
| 674 | static inline bool mulu128(uint64_t *plow, uint64_t *phigh, uint64_t factor) |
| 675 | { |
| 676 | #if defined(CONFIG_INT128) |
| 677 | bool res; |
| 678 | __uint128_t r; |
| 679 | __uint128_t f = ((__uint128_t)*phigh << 64) | *plow; |
| 680 | res = __builtin_mul_overflow(f, factor, &r); |
| 681 | |
| 682 | *plow = r; |
| 683 | *phigh = r >> 64; |
| 684 | |
| 685 | return res; |
| 686 | #else |
| 687 | uint64_t dhi = *phigh; |
| 688 | uint64_t dlo = *plow; |
| 689 | uint64_t ahi; |
| 690 | uint64_t blo, bhi; |
| 691 | |
| 692 | if (dhi == 0) { |
| 693 | mulu64(plow, phigh, dlo, factor); |
| 694 | return false; |
| 695 | } |
| 696 | |
| 697 | mulu64(plow, &ahi, dlo, factor); |
| 698 | mulu64(&blo, &bhi, dhi, factor); |
| 699 | |
| 700 | return uadd64_overflow(ahi, blo, phigh) || bhi != 0; |
| 701 | #endif |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * uadd64_carry - addition with carry-in and carry-out |
| 706 | * @x, @y: addends |
| 707 | * @pcarry: in-out carry value |
| 708 | * |
| 709 | * Computes @x + @y + *@pcarry, placing the carry-out back |
| 710 | * into *@pcarry and returning the 64-bit sum. |
| 711 | */ |
| 712 | static inline uint64_t uadd64_carry(uint64_t x, uint64_t y, bool *pcarry) |
| 713 | { |
| 714 | #if __has_builtin(__builtin_addcll) |
| 715 | unsigned long long c = *pcarry; |
| 716 | x = __builtin_addcll(x, y, c, &c); |
| 717 | *pcarry = c & 1; |
| 718 | return x; |
| 719 | #else |
| 720 | bool c = *pcarry; |
| 721 | /* This is clang's internal expansion of __builtin_addc. */ |
| 722 | c = uadd64_overflow(x, c, &x); |
| 723 | c |= uadd64_overflow(x, y, &x); |
| 724 | *pcarry = c; |
| 725 | return x; |
| 726 | #endif |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * usub64_borrow - subtraction with borrow-in and borrow-out |
| 731 | * @x, @y: addends |
| 732 | * @pborrow: in-out borrow value |
| 733 | * |
| 734 | * Computes @x - @y - *@pborrow, placing the borrow-out back |
| 735 | * into *@pborrow and returning the 64-bit sum. |
| 736 | */ |
| 737 | static inline uint64_t usub64_borrow(uint64_t x, uint64_t y, bool *pborrow) |
| 738 | { |
| 739 | #if __has_builtin(__builtin_subcll) |
| 740 | unsigned long long b = *pborrow; |
| 741 | x = __builtin_subcll(x, y, b, &b); |
| 742 | *pborrow = b & 1; |
| 743 | return x; |
| 744 | #else |
| 745 | bool b = *pborrow; |
| 746 | b = usub64_overflow(x, b, &x); |
| 747 | b |= usub64_overflow(x, y, &x); |
| 748 | *pborrow = b; |
| 749 | return x; |
| 750 | #endif |
| 751 | } |
| 752 | |
| 753 | /* Host type specific sizes of these routines. */ |
| 754 | |
| 755 | #if ULONG_MAX == UINT32_MAX |
| 756 | # define clzl clz32 |
| 757 | # define ctzl ctz32 |
| 758 | # define clol clo32 |
| 759 | # define ctol cto32 |
| 760 | # define ctpopl ctpop32 |
| 761 | # define revbitl revbit32 |
| 762 | #elif ULONG_MAX == UINT64_MAX |
| 763 | # define clzl clz64 |
| 764 | # define ctzl ctz64 |
| 765 | # define clol clo64 |
| 766 | # define ctol cto64 |
| 767 | # define ctpopl ctpop64 |
| 768 | # define revbitl revbit64 |
| 769 | #else |
| 770 | # error Unknown sizeof long |
| 771 | #endif |
| 772 | |
| 773 | static inline bool is_power_of_2(uint64_t value) |
| 774 | { |
| 775 | if (!value) { |
| 776 | return false; |
| 777 | } |
| 778 | |
| 779 | return !(value & (value - 1)); |
| 780 | } |
| 781 | |
| 782 | /** |
| 783 | * Return @value rounded down to the nearest power of two or zero. |
| 784 | */ |
| 785 | static inline uint64_t pow2floor(uint64_t value) |
| 786 | { |
| 787 | if (!value) { |
| 788 | /* Avoid undefined shift by 64 */ |
| 789 | return 0; |
| 790 | } |
| 791 | return 0x8000000000000000ull >> clz64(value); |
| 792 | } |
| 793 | |
| 794 | /* |
| 795 | * Return @value rounded up to the nearest power of two modulo 2^64. |
| 796 | * This is *zero* for @value > 2^63, so be careful. |
| 797 | */ |
| 798 | static inline uint64_t pow2ceil(uint64_t value) |
| 799 | { |
| 800 | int n = clz64(value - 1); |
| 801 | |
| 802 | if (!n) { |
| 803 | /* |
| 804 | * @value - 1 has no leading zeroes, thus @value - 1 >= 2^63 |
| 805 | * Therefore, either @value == 0 or @value > 2^63. |
| 806 | * If it's 0, return 1, else return 0. |
| 807 | */ |
| 808 | return !value; |
| 809 | } |
| 810 | return 0x8000000000000000ull >> (n - 1); |
| 811 | } |
| 812 | |
| 813 | static inline uint32_t pow2roundup32(uint32_t x) |
| 814 | { |
| 815 | x |= (x >> 1); |
| 816 | x |= (x >> 2); |
| 817 | x |= (x >> 4); |
| 818 | x |= (x >> 8); |
| 819 | x |= (x >> 16); |
| 820 | return x + 1; |
| 821 | } |
| 822 | |
| 823 | /** |
| 824 | * urshift - 128-bit Unsigned Right Shift. |
| 825 | * @plow: in/out - lower 64-bit integer. |
| 826 | * @phigh: in/out - higher 64-bit integer. |
| 827 | * @shift: in - bytes to shift, between 0 and 127. |
| 828 | * |
| 829 | * Result is zero-extended and stored in plow/phigh, which are |
| 830 | * input/output variables. Shift values outside the range will |
| 831 | * be mod to 128. In other words, the caller is responsible to |
| 832 | * verify/assert both the shift range and plow/phigh pointers. |
| 833 | */ |
| 834 | void urshift(uint64_t *plow, uint64_t *phigh, int32_t shift); |
| 835 | |
| 836 | /** |
| 837 | * ulshift - 128-bit Unsigned Left Shift. |
| 838 | * @plow: in/out - lower 64-bit integer. |
| 839 | * @phigh: in/out - higher 64-bit integer. |
| 840 | * @shift: in - bytes to shift, between 0 and 127. |
| 841 | * @overflow: out - true if any 1-bit is shifted out. |
| 842 | * |
| 843 | * Result is zero-extended and stored in plow/phigh, which are |
| 844 | * input/output variables. Shift values outside the range will |
| 845 | * be mod to 128. In other words, the caller is responsible to |
| 846 | * verify/assert both the shift range and plow/phigh pointers. |
| 847 | */ |
| 848 | void ulshift(uint64_t *plow, uint64_t *phigh, int32_t shift, bool *overflow); |
| 849 | |
| 850 | /* From the GNU Multi Precision Library - longlong.h __udiv_qrnnd |
| 851 | * (https://gmplib.org/repo/gmp/file/tip/longlong.h) |
| 852 | * |
| 853 | * Licensed under the GPLv2/LGPLv3 |
| 854 | */ |
| 855 | static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1, |
| 856 | uint64_t n0, uint64_t d) |
| 857 | { |
| 858 | #if defined(__x86_64__) |
| 859 | uint64_t q; |
| 860 | asm("divq %4" : "=a"(q), "=d"(*r) : "0"(n0), "1"(n1), "rm"(d)); |
| 861 | return q; |
| 862 | #elif defined(__s390x__) && !defined(__clang__) |
| 863 | /* Need to use a TImode type to get an even register pair for DLGR. */ |
| 864 | unsigned __int128 n = (unsigned __int128)n1 << 64 | n0; |
| 865 | asm("dlgr %0, %1" : "+r"(n) : "r"(d)); |
| 866 | *r = n >> 64; |
| 867 | return n; |
| 868 | #elif defined(_ARCH_PPC64) && defined(_ARCH_PWR7) |
| 869 | /* From Power ISA 2.06, programming note for divdeu. */ |
| 870 | uint64_t q1, q2, Q, r1, r2, R; |
| 871 | asm("divdeu %0,%2,%4; divdu %1,%3,%4" |
| 872 | : "=&r"(q1), "=r"(q2) |
| 873 | : "r"(n1), "r"(n0), "r"(d)); |
| 874 | r1 = -(q1 * d); /* low part of (n1<<64) - (q1 * d) */ |
| 875 | r2 = n0 - (q2 * d); |
| 876 | Q = q1 + q2; |
| 877 | R = r1 + r2; |
| 878 | if (R >= d || R < r2) { /* overflow implies R > d */ |
| 879 | Q += 1; |
| 880 | R -= d; |
| 881 | } |
| 882 | *r = R; |
| 883 | return Q; |
| 884 | #else |
| 885 | uint64_t d0, d1, q0, q1, r1, r0, m; |
| 886 | |
| 887 | d0 = (uint32_t)d; |
| 888 | d1 = d >> 32; |
| 889 | |
| 890 | r1 = n1 % d1; |
| 891 | q1 = n1 / d1; |
| 892 | m = q1 * d0; |
| 893 | r1 = (r1 << 32) | (n0 >> 32); |
| 894 | if (r1 < m) { |
| 895 | q1 -= 1; |
| 896 | r1 += d; |
| 897 | if (r1 >= d) { |
| 898 | if (r1 < m) { |
| 899 | q1 -= 1; |
| 900 | r1 += d; |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | r1 -= m; |
| 905 | |
| 906 | r0 = r1 % d1; |
| 907 | q0 = r1 / d1; |
| 908 | m = q0 * d0; |
| 909 | r0 = (r0 << 32) | (uint32_t)n0; |
| 910 | if (r0 < m) { |
| 911 | q0 -= 1; |
| 912 | r0 += d; |
| 913 | if (r0 >= d) { |
| 914 | if (r0 < m) { |
| 915 | q0 -= 1; |
| 916 | r0 += d; |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | r0 -= m; |
| 921 | |
| 922 | *r = r0; |
| 923 | return (q1 << 32) | q0; |
| 924 | #endif |
| 925 | } |
| 926 | |
| 927 | Int128 divu256(Int128 *plow, Int128 *phigh, Int128 divisor); |
| 928 | Int128 divs256(Int128 *plow, Int128 *phigh, Int128 divisor); |
| 929 | #endif |