blob: 08a17e16e5cbd9df72d1c8728ca606ac4af1dd5e [file] [log] [blame]
ths05f778c2007-10-27 13:05:54 +00001/*
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 */
Markus Armbruster175de522016-06-29 15:29:06 +020025
Luis Pires8ac2d6c2021-10-25 16:11:37 -030026/* 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
Paolo Bonzinicb9c3772012-12-06 12:15:58 +010030#ifndef HOST_UTILS_H
Markus Armbruster175de522016-06-29 15:29:06 +020031#define HOST_UTILS_H
ths05f778c2007-10-27 13:05:54 +000032
Richard Henderson1ec80702020-11-13 03:22:23 +000033#include "qemu/compiler.h"
Richard Henderson652a4b72015-09-14 13:00:34 -070034#include "qemu/bswap.h"
thscebdff72008-06-05 22:55:54 +000035
Richard Hendersonf5401662013-02-16 12:46:59 -080036#ifdef CONFIG_INT128
Blue Swirlfacd2852009-08-16 08:03:26 +000037static inline void mulu64(uint64_t *plow, uint64_t *phigh,
38 uint64_t a, uint64_t b)
j_mayer7a51ad82007-11-04 02:24:58 +000039{
Richard Hendersonf5401662013-02-16 12:46:59 -080040 __uint128_t r = (__uint128_t)a * b;
41 *plow = r;
42 *phigh = r >> 64;
j_mayer7a51ad82007-11-04 02:24:58 +000043}
Richard Hendersonf5401662013-02-16 12:46:59 -080044
Blue Swirlfacd2852009-08-16 08:03:26 +000045static inline void muls64(uint64_t *plow, uint64_t *phigh,
46 int64_t a, int64_t b)
j_mayer7a51ad82007-11-04 02:24:58 +000047{
Richard Hendersonf5401662013-02-16 12:46:59 -080048 __int128_t r = (__int128_t)a * b;
49 *plow = r;
50 *phigh = r >> 64;
j_mayer7a51ad82007-11-04 02:24:58 +000051}
Tom Musta98d1eb22014-01-07 10:05:51 -060052
Peter Maydell49caffe2015-08-19 16:20:20 +010053/* compute with 96 bit intermediate result: (a*b)/c */
54static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
55{
56 return (__int128_t)a * b / c;
57}
58
Luis Pires9276a312021-10-25 16:11:36 -030059static inline void divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor)
Tom Musta98d1eb22014-01-07 10:05:51 -060060{
Luis Pires9276a312021-10-25 16:11:36 -030061 __uint128_t dividend = ((__uint128_t)*phigh << 64) | *plow;
62 __uint128_t result = dividend / divisor;
63 *plow = result;
64 *phigh = dividend % divisor;
Tom Musta98d1eb22014-01-07 10:05:51 -060065}
Tom Mustae44259b2014-01-07 10:05:52 -060066
Luis Pires9276a312021-10-25 16:11:36 -030067static inline void divs128(int64_t *plow, int64_t *phigh, int64_t divisor)
Tom Mustae44259b2014-01-07 10:05:52 -060068{
Luis Pires9276a312021-10-25 16:11:36 -030069 __int128_t dividend = ((__int128_t)*phigh << 64) | (uint64_t)*plow;
70 __int128_t result = dividend / divisor;
71 *plow = result;
72 *phigh = dividend % divisor;
Tom Mustae44259b2014-01-07 10:05:52 -060073}
j_mayer7a51ad82007-11-04 02:24:58 +000074#else
Lijun Pandb7b62e2020-07-01 18:43:44 -050075void muls64(uint64_t *plow, uint64_t *phigh, int64_t a, int64_t b);
76void mulu64(uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b);
Luis Pires9276a312021-10-25 16:11:36 -030077void divu128(uint64_t *plow, uint64_t *phigh, uint64_t divisor);
78void divs128(int64_t *plow, int64_t *phigh, int64_t divisor);
Peter Maydell49caffe2015-08-19 16:20:20 +010079
80static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
81{
82 union {
83 uint64_t ll;
84 struct {
85#ifdef HOST_WORDS_BIGENDIAN
86 uint32_t high, low;
87#else
88 uint32_t low, high;
89#endif
90 } l;
91 } u, res;
92 uint64_t rl, rh;
93
94 u.ll = a;
95 rl = (uint64_t)u.l.low * (uint64_t)b;
96 rh = (uint64_t)u.l.high * (uint64_t)b;
97 rh += (rl >> 32);
98 res.l.high = rh / c;
99 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
100 return res.ll;
101}
j_mayer7a51ad82007-11-04 02:24:58 +0000102#endif
103
Richard Henderson72d81152013-02-13 17:47:35 -0800104/**
105 * clz32 - count leading zeros in a 32-bit value.
106 * @val: The value to search
107 *
108 * Returns 32 if the value is zero. Note that the GCC builtin is
109 * undefined if the value is zero.
110 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000111static inline int clz32(uint32_t val)
ths05f778c2007-10-27 13:05:54 +0000112{
Richard Henderson72d81152013-02-13 17:47:35 -0800113 return val ? __builtin_clz(val) : 32;
ths05f778c2007-10-27 13:05:54 +0000114}
115
Richard Henderson72d81152013-02-13 17:47:35 -0800116/**
117 * clo32 - count leading ones in a 32-bit value.
118 * @val: The value to search
119 *
120 * Returns 32 if the value is -1.
121 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000122static inline int clo32(uint32_t val)
ths05f778c2007-10-27 13:05:54 +0000123{
124 return clz32(~val);
125}
126
Richard Henderson72d81152013-02-13 17:47:35 -0800127/**
128 * clz64 - count leading zeros in a 64-bit value.
129 * @val: The value to search
130 *
131 * Returns 64 if the value is zero. Note that the GCC builtin is
132 * undefined if the value is zero.
133 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000134static inline int clz64(uint64_t val)
ths05f778c2007-10-27 13:05:54 +0000135{
Richard Henderson72d81152013-02-13 17:47:35 -0800136 return val ? __builtin_clzll(val) : 64;
ths05f778c2007-10-27 13:05:54 +0000137}
138
Richard Henderson72d81152013-02-13 17:47:35 -0800139/**
140 * clo64 - count leading ones in a 64-bit value.
141 * @val: The value to search
142 *
143 * Returns 64 if the value is -1.
144 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000145static inline int clo64(uint64_t val)
ths05f778c2007-10-27 13:05:54 +0000146{
147 return clz64(~val);
148}
j_mayerb9ef45f2007-10-28 12:52:38 +0000149
Richard Henderson72d81152013-02-13 17:47:35 -0800150/**
151 * ctz32 - count trailing zeros in a 32-bit value.
152 * @val: The value to search
153 *
154 * Returns 32 if the value is zero. Note that the GCC builtin is
155 * undefined if the value is zero.
156 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000157static inline int ctz32(uint32_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000158{
Richard Henderson72d81152013-02-13 17:47:35 -0800159 return val ? __builtin_ctz(val) : 32;
balrogc8906842008-11-12 17:18:41 +0000160}
161
Richard Henderson72d81152013-02-13 17:47:35 -0800162/**
163 * cto32 - count trailing ones in a 32-bit value.
164 * @val: The value to search
165 *
166 * Returns 32 if the value is -1.
167 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000168static inline int cto32(uint32_t val)
balrogc8906842008-11-12 17:18:41 +0000169{
j_mayerb9ef45f2007-10-28 12:52:38 +0000170 return ctz32(~val);
171}
172
Richard Henderson72d81152013-02-13 17:47:35 -0800173/**
174 * ctz64 - count trailing zeros in a 64-bit value.
175 * @val: The value to search
176 *
177 * Returns 64 if the value is zero. Note that the GCC builtin is
178 * undefined if the value is zero.
179 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000180static inline int ctz64(uint64_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000181{
Richard Henderson72d81152013-02-13 17:47:35 -0800182 return val ? __builtin_ctzll(val) : 64;
j_mayerb9ef45f2007-10-28 12:52:38 +0000183}
184
Richard Henderson72d81152013-02-13 17:47:35 -0800185/**
Dr. David Alan Gilbert1c884ab2014-02-12 17:14:33 +0000186 * cto64 - count trailing ones in a 64-bit value.
Richard Henderson72d81152013-02-13 17:47:35 -0800187 * @val: The value to search
188 *
189 * Returns 64 if the value is -1.
190 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000191static inline int cto64(uint64_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000192{
193 return ctz64(~val);
194}
195
Richard Henderson72d81152013-02-13 17:47:35 -0800196/**
Claudio Fontanaafd3fe42013-12-17 19:42:35 +0000197 * clrsb32 - count leading redundant sign bits in a 32-bit value.
198 * @val: The value to search
199 *
200 * Returns the number of bits following the sign bit that are equal to it.
201 * No special cases; output range is [0-31].
202 */
203static inline int clrsb32(uint32_t val)
204{
Thomas Huthf773b422018-12-03 14:33:12 +0100205#if __has_builtin(__builtin_clrsb) || !defined(__clang__)
Claudio Fontanaafd3fe42013-12-17 19:42:35 +0000206 return __builtin_clrsb(val);
207#else
208 return clz32(val ^ ((int32_t)val >> 1)) - 1;
209#endif
210}
211
212/**
213 * clrsb64 - count leading redundant sign bits in a 64-bit value.
214 * @val: The value to search
215 *
216 * Returns the number of bits following the sign bit that are equal to it.
217 * No special cases; output range is [0-63].
218 */
219static inline int clrsb64(uint64_t val)
220{
Thomas Huthf773b422018-12-03 14:33:12 +0100221#if __has_builtin(__builtin_clrsbll) || !defined(__clang__)
Claudio Fontanaafd3fe42013-12-17 19:42:35 +0000222 return __builtin_clrsbll(val);
223#else
224 return clz64(val ^ ((int64_t)val >> 1)) - 1;
225#endif
226}
227
228/**
Richard Henderson72d81152013-02-13 17:47:35 -0800229 * ctpop8 - count the population of one bits in an 8-bit value.
230 * @val: The value to search
231 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000232static inline int ctpop8(uint8_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000233{
Richard Henderson72d81152013-02-13 17:47:35 -0800234 return __builtin_popcount(val);
j_mayerb9ef45f2007-10-28 12:52:38 +0000235}
236
Richard Henderson72d81152013-02-13 17:47:35 -0800237/**
238 * ctpop16 - count the population of one bits in a 16-bit value.
239 * @val: The value to search
240 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000241static inline int ctpop16(uint16_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000242{
Richard Henderson72d81152013-02-13 17:47:35 -0800243 return __builtin_popcount(val);
j_mayerb9ef45f2007-10-28 12:52:38 +0000244}
245
Richard Henderson72d81152013-02-13 17:47:35 -0800246/**
247 * ctpop32 - count the population of one bits in a 32-bit value.
248 * @val: The value to search
249 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000250static inline int ctpop32(uint32_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000251{
aurel327d019982008-10-12 00:53:08 +0000252 return __builtin_popcount(val);
j_mayerb9ef45f2007-10-28 12:52:38 +0000253}
254
Richard Henderson72d81152013-02-13 17:47:35 -0800255/**
256 * ctpop64 - count the population of one bits in a 64-bit value.
257 * @val: The value to search
258 */
Blue Swirlfacd2852009-08-16 08:03:26 +0000259static inline int ctpop64(uint64_t val)
j_mayerb9ef45f2007-10-28 12:52:38 +0000260{
aurel327d019982008-10-12 00:53:08 +0000261 return __builtin_popcountll(val);
ths3800af92007-12-18 01:58:05 +0000262}
Paolo Bonzinicb9c3772012-12-06 12:15:58 +0100263
Richard Henderson652a4b72015-09-14 13:00:34 -0700264/**
265 * revbit8 - reverse the bits in an 8-bit value.
266 * @x: The value to modify.
267 */
268static inline uint8_t revbit8(uint8_t x)
269{
Richard Henderson5140d6b2020-11-06 10:59:36 -0800270#if __has_builtin(__builtin_bitreverse8)
271 return __builtin_bitreverse8(x);
272#else
Richard Henderson652a4b72015-09-14 13:00:34 -0700273 /* Assign the correct nibble position. */
274 x = ((x & 0xf0) >> 4)
275 | ((x & 0x0f) << 4);
276 /* Assign the correct bit position. */
277 x = ((x & 0x88) >> 3)
278 | ((x & 0x44) >> 1)
279 | ((x & 0x22) << 1)
280 | ((x & 0x11) << 3);
281 return x;
Richard Henderson5140d6b2020-11-06 10:59:36 -0800282#endif
Richard Henderson652a4b72015-09-14 13:00:34 -0700283}
284
285/**
286 * revbit16 - reverse the bits in a 16-bit value.
287 * @x: The value to modify.
288 */
289static inline uint16_t revbit16(uint16_t x)
290{
Richard Henderson5140d6b2020-11-06 10:59:36 -0800291#if __has_builtin(__builtin_bitreverse16)
292 return __builtin_bitreverse16(x);
293#else
Richard Henderson652a4b72015-09-14 13:00:34 -0700294 /* Assign the correct byte position. */
295 x = bswap16(x);
296 /* Assign the correct nibble position. */
297 x = ((x & 0xf0f0) >> 4)
298 | ((x & 0x0f0f) << 4);
299 /* Assign the correct bit position. */
300 x = ((x & 0x8888) >> 3)
301 | ((x & 0x4444) >> 1)
302 | ((x & 0x2222) << 1)
303 | ((x & 0x1111) << 3);
304 return x;
Richard Henderson5140d6b2020-11-06 10:59:36 -0800305#endif
Richard Henderson652a4b72015-09-14 13:00:34 -0700306}
307
308/**
309 * revbit32 - reverse the bits in a 32-bit value.
310 * @x: The value to modify.
311 */
312static inline uint32_t revbit32(uint32_t x)
313{
Richard Henderson5140d6b2020-11-06 10:59:36 -0800314#if __has_builtin(__builtin_bitreverse32)
315 return __builtin_bitreverse32(x);
316#else
Richard Henderson652a4b72015-09-14 13:00:34 -0700317 /* Assign the correct byte position. */
318 x = bswap32(x);
319 /* Assign the correct nibble position. */
320 x = ((x & 0xf0f0f0f0u) >> 4)
321 | ((x & 0x0f0f0f0fu) << 4);
322 /* Assign the correct bit position. */
323 x = ((x & 0x88888888u) >> 3)
324 | ((x & 0x44444444u) >> 1)
325 | ((x & 0x22222222u) << 1)
326 | ((x & 0x11111111u) << 3);
327 return x;
Richard Henderson5140d6b2020-11-06 10:59:36 -0800328#endif
Richard Henderson652a4b72015-09-14 13:00:34 -0700329}
330
331/**
332 * revbit64 - reverse the bits in a 64-bit value.
333 * @x: The value to modify.
334 */
335static inline uint64_t revbit64(uint64_t x)
336{
Richard Henderson5140d6b2020-11-06 10:59:36 -0800337#if __has_builtin(__builtin_bitreverse64)
338 return __builtin_bitreverse64(x);
339#else
Richard Henderson652a4b72015-09-14 13:00:34 -0700340 /* Assign the correct byte position. */
341 x = bswap64(x);
342 /* Assign the correct nibble position. */
343 x = ((x & 0xf0f0f0f0f0f0f0f0ull) >> 4)
344 | ((x & 0x0f0f0f0f0f0f0f0full) << 4);
345 /* Assign the correct bit position. */
346 x = ((x & 0x8888888888888888ull) >> 3)
347 | ((x & 0x4444444444444444ull) >> 1)
348 | ((x & 0x2222222222222222ull) << 1)
349 | ((x & 0x1111111111111111ull) << 3);
350 return x;
Richard Henderson5140d6b2020-11-06 10:59:36 -0800351#endif
Richard Henderson652a4b72015-09-14 13:00:34 -0700352}
353
Richard Hendersoncec07c02020-11-06 17:42:36 -0800354/**
Luis Piresd03bba02021-09-10 08:26:05 -0300355 * Return the absolute value of a 64-bit integer as an unsigned 64-bit value
356 */
357static inline uint64_t uabs64(int64_t v)
358{
359 return v < 0 ? -v : v;
360}
361
362/**
Richard Hendersoncec07c02020-11-06 17:42:36 -0800363 * sadd32_overflow - addition with overflow indication
364 * @x, @y: addends
365 * @ret: Output for sum
366 *
367 * Computes *@ret = @x + @y, and returns true if and only if that
368 * value has been truncated.
369 */
370static inline bool sadd32_overflow(int32_t x, int32_t y, int32_t *ret)
371{
372#if __has_builtin(__builtin_add_overflow) || __GNUC__ >= 5
373 return __builtin_add_overflow(x, y, ret);
374#else
375 *ret = x + y;
376 return ((*ret ^ x) & ~(x ^ y)) < 0;
377#endif
378}
379
380/**
381 * sadd64_overflow - addition with overflow indication
382 * @x, @y: addends
383 * @ret: Output for sum
384 *
385 * Computes *@ret = @x + @y, and returns true if and only if that
386 * value has been truncated.
387 */
388static inline bool sadd64_overflow(int64_t x, int64_t y, int64_t *ret)
389{
390#if __has_builtin(__builtin_add_overflow) || __GNUC__ >= 5
391 return __builtin_add_overflow(x, y, ret);
392#else
393 *ret = x + y;
394 return ((*ret ^ x) & ~(x ^ y)) < 0;
395#endif
396}
397
398/**
399 * uadd32_overflow - addition with overflow indication
400 * @x, @y: addends
401 * @ret: Output for sum
402 *
403 * Computes *@ret = @x + @y, and returns true if and only if that
404 * value has been truncated.
405 */
406static inline bool uadd32_overflow(uint32_t x, uint32_t y, uint32_t *ret)
407{
408#if __has_builtin(__builtin_add_overflow) || __GNUC__ >= 5
409 return __builtin_add_overflow(x, y, ret);
410#else
411 *ret = x + y;
412 return *ret < x;
413#endif
414}
415
416/**
417 * uadd64_overflow - addition with overflow indication
418 * @x, @y: addends
419 * @ret: Output for sum
420 *
421 * Computes *@ret = @x + @y, and returns true if and only if that
422 * value has been truncated.
423 */
424static inline bool uadd64_overflow(uint64_t x, uint64_t y, uint64_t *ret)
425{
426#if __has_builtin(__builtin_add_overflow) || __GNUC__ >= 5
427 return __builtin_add_overflow(x, y, ret);
428#else
429 *ret = x + y;
430 return *ret < x;
431#endif
432}
433
434/**
435 * ssub32_overflow - subtraction with overflow indication
436 * @x: Minuend
437 * @y: Subtrahend
438 * @ret: Output for difference
439 *
440 * Computes *@ret = @x - @y, and returns true if and only if that
441 * value has been truncated.
442 */
443static inline bool ssub32_overflow(int32_t x, int32_t y, int32_t *ret)
444{
445#if __has_builtin(__builtin_sub_overflow) || __GNUC__ >= 5
446 return __builtin_sub_overflow(x, y, ret);
447#else
448 *ret = x - y;
449 return ((*ret ^ x) & (x ^ y)) < 0;
450#endif
451}
452
453/**
454 * ssub64_overflow - subtraction with overflow indication
455 * @x: Minuend
456 * @y: Subtrahend
457 * @ret: Output for sum
458 *
459 * Computes *@ret = @x - @y, and returns true if and only if that
460 * value has been truncated.
461 */
462static inline bool ssub64_overflow(int64_t x, int64_t y, int64_t *ret)
463{
464#if __has_builtin(__builtin_sub_overflow) || __GNUC__ >= 5
465 return __builtin_sub_overflow(x, y, ret);
466#else
467 *ret = x - y;
468 return ((*ret ^ x) & (x ^ y)) < 0;
469#endif
470}
471
472/**
473 * usub32_overflow - subtraction with overflow indication
474 * @x: Minuend
475 * @y: Subtrahend
476 * @ret: Output for sum
477 *
478 * Computes *@ret = @x - @y, and returns true if and only if that
479 * value has been truncated.
480 */
481static inline bool usub32_overflow(uint32_t x, uint32_t y, uint32_t *ret)
482{
483#if __has_builtin(__builtin_sub_overflow) || __GNUC__ >= 5
484 return __builtin_sub_overflow(x, y, ret);
485#else
486 *ret = x - y;
487 return x < y;
488#endif
489}
490
491/**
492 * usub64_overflow - subtraction with overflow indication
493 * @x: Minuend
494 * @y: Subtrahend
495 * @ret: Output for sum
496 *
497 * Computes *@ret = @x - @y, and returns true if and only if that
498 * value has been truncated.
499 */
500static inline bool usub64_overflow(uint64_t x, uint64_t y, uint64_t *ret)
501{
502#if __has_builtin(__builtin_sub_overflow) || __GNUC__ >= 5
503 return __builtin_sub_overflow(x, y, ret);
504#else
505 *ret = x - y;
506 return x < y;
507#endif
508}
509
510/**
511 * smul32_overflow - multiplication with overflow indication
512 * @x, @y: Input multipliers
513 * @ret: Output for product
514 *
515 * Computes *@ret = @x * @y, and returns true if and only if that
516 * value has been truncated.
517 */
518static inline bool smul32_overflow(int32_t x, int32_t y, int32_t *ret)
519{
520#if __has_builtin(__builtin_mul_overflow) || __GNUC__ >= 5
521 return __builtin_mul_overflow(x, y, ret);
522#else
523 int64_t z = (int64_t)x * y;
524 *ret = z;
525 return *ret != z;
526#endif
527}
528
529/**
530 * smul64_overflow - multiplication with overflow indication
531 * @x, @y: Input multipliers
532 * @ret: Output for product
533 *
534 * Computes *@ret = @x * @y, and returns true if and only if that
535 * value has been truncated.
536 */
537static inline bool smul64_overflow(int64_t x, int64_t y, int64_t *ret)
538{
539#if __has_builtin(__builtin_mul_overflow) || __GNUC__ >= 5
540 return __builtin_mul_overflow(x, y, ret);
541#else
542 uint64_t hi, lo;
543 muls64(&lo, &hi, x, y);
544 *ret = lo;
545 return hi != ((int64_t)lo >> 63);
546#endif
547}
548
549/**
550 * umul32_overflow - multiplication with overflow indication
551 * @x, @y: Input multipliers
552 * @ret: Output for product
553 *
554 * Computes *@ret = @x * @y, and returns true if and only if that
555 * value has been truncated.
556 */
557static inline bool umul32_overflow(uint32_t x, uint32_t y, uint32_t *ret)
558{
559#if __has_builtin(__builtin_mul_overflow) || __GNUC__ >= 5
560 return __builtin_mul_overflow(x, y, ret);
561#else
562 uint64_t z = (uint64_t)x * y;
563 *ret = z;
564 return z > UINT32_MAX;
565#endif
566}
567
568/**
569 * umul64_overflow - multiplication with overflow indication
570 * @x, @y: Input multipliers
571 * @ret: Output for product
572 *
573 * Computes *@ret = @x * @y, and returns true if and only if that
574 * value has been truncated.
575 */
576static inline bool umul64_overflow(uint64_t x, uint64_t y, uint64_t *ret)
577{
578#if __has_builtin(__builtin_mul_overflow) || __GNUC__ >= 5
579 return __builtin_mul_overflow(x, y, ret);
580#else
581 uint64_t hi;
582 mulu64(ret, &hi, x, y);
583 return hi != 0;
584#endif
585}
586
Richard Henderson1ec80702020-11-13 03:22:23 +0000587/**
588 * uadd64_carry - addition with carry-in and carry-out
589 * @x, @y: addends
590 * @pcarry: in-out carry value
591 *
592 * Computes @x + @y + *@pcarry, placing the carry-out back
593 * into *@pcarry and returning the 64-bit sum.
594 */
595static inline uint64_t uadd64_carry(uint64_t x, uint64_t y, bool *pcarry)
596{
597#if __has_builtin(__builtin_addcll)
598 unsigned long long c = *pcarry;
599 x = __builtin_addcll(x, y, c, &c);
600 *pcarry = c & 1;
601 return x;
602#else
603 bool c = *pcarry;
604 /* This is clang's internal expansion of __builtin_addc. */
605 c = uadd64_overflow(x, c, &x);
606 c |= uadd64_overflow(x, y, &x);
607 *pcarry = c;
608 return x;
609#endif
610}
611
612/**
613 * usub64_borrow - subtraction with borrow-in and borrow-out
614 * @x, @y: addends
615 * @pborrow: in-out borrow value
616 *
617 * Computes @x - @y - *@pborrow, placing the borrow-out back
618 * into *@pborrow and returning the 64-bit sum.
619 */
620static inline uint64_t usub64_borrow(uint64_t x, uint64_t y, bool *pborrow)
621{
622#if __has_builtin(__builtin_subcll)
623 unsigned long long b = *pborrow;
624 x = __builtin_subcll(x, y, b, &b);
625 *pborrow = b & 1;
626 return x;
627#else
628 bool b = *pborrow;
629 b = usub64_overflow(x, b, &x);
630 b |= usub64_overflow(x, y, &x);
631 *pborrow = b;
632 return x;
633#endif
634}
635
Richard Henderson01654372013-02-13 17:47:34 -0800636/* Host type specific sizes of these routines. */
637
638#if ULONG_MAX == UINT32_MAX
639# define clzl clz32
640# define ctzl ctz32
641# define clol clo32
642# define ctol cto32
643# define ctpopl ctpop32
Richard Henderson652a4b72015-09-14 13:00:34 -0700644# define revbitl revbit32
Richard Henderson01654372013-02-13 17:47:34 -0800645#elif ULONG_MAX == UINT64_MAX
646# define clzl clz64
647# define ctzl ctz64
648# define clol clo64
649# define ctol cto64
650# define ctpopl ctpop64
Richard Henderson652a4b72015-09-14 13:00:34 -0700651# define revbitl revbit64
Richard Henderson01654372013-02-13 17:47:34 -0800652#else
653# error Unknown sizeof long
654#endif
655
Peter Maydell8f1ed5f2015-07-24 13:33:12 +0100656static inline bool is_power_of_2(uint64_t value)
657{
658 if (!value) {
Eric Blakee52eeb42016-05-31 12:33:31 -0600659 return false;
Peter Maydell8f1ed5f2015-07-24 13:33:12 +0100660 }
661
662 return !(value & (value - 1));
663}
664
Markus Armbruster43c64a02017-07-27 11:46:15 +0200665/**
666 * Return @value rounded down to the nearest power of two or zero.
667 */
668static inline uint64_t pow2floor(uint64_t value)
Peter Maydell8f1ed5f2015-07-24 13:33:12 +0100669{
Markus Armbruster43c64a02017-07-27 11:46:15 +0200670 if (!value) {
671 /* Avoid undefined shift by 64 */
672 return 0;
Peter Maydell8f1ed5f2015-07-24 13:33:12 +0100673 }
Markus Armbruster43c64a02017-07-27 11:46:15 +0200674 return 0x8000000000000000ull >> clz64(value);
Peter Maydell8f1ed5f2015-07-24 13:33:12 +0100675}
676
Markus Armbruster362aaf12017-07-27 11:46:16 +0200677/*
678 * Return @value rounded up to the nearest power of two modulo 2^64.
679 * This is *zero* for @value > 2^63, so be careful.
680 */
Peter Maydell8f1ed5f2015-07-24 13:33:12 +0100681static inline uint64_t pow2ceil(uint64_t value)
682{
Markus Armbruster362aaf12017-07-27 11:46:16 +0200683 int n = clz64(value - 1);
Peter Maydell8f1ed5f2015-07-24 13:33:12 +0100684
Markus Armbruster362aaf12017-07-27 11:46:16 +0200685 if (!n) {
686 /*
687 * @value - 1 has no leading zeroes, thus @value - 1 >= 2^63
688 * Therefore, either @value == 0 or @value > 2^63.
689 * If it's 0, return 1, else return 0.
690 */
691 return !value;
Peter Maydell8f1ed5f2015-07-24 13:33:12 +0100692 }
Markus Armbruster362aaf12017-07-27 11:46:16 +0200693 return 0x8000000000000000ull >> (n - 1);
Peter Maydell8f1ed5f2015-07-24 13:33:12 +0100694}
695
Yuval Shaia37e626c2018-01-14 11:01:43 +0200696static inline uint32_t pow2roundup32(uint32_t x)
697{
698 x |= (x >> 1);
699 x |= (x >> 2);
700 x |= (x >> 4);
701 x |= (x >> 8);
702 x |= (x >> 16);
703 return x + 1;
704}
705
Jose Ricardo Zivianif539fbe2017-01-10 00:10:09 -0200706/**
707 * urshift - 128-bit Unsigned Right Shift.
708 * @plow: in/out - lower 64-bit integer.
709 * @phigh: in/out - higher 64-bit integer.
710 * @shift: in - bytes to shift, between 0 and 127.
711 *
712 * Result is zero-extended and stored in plow/phigh, which are
713 * input/output variables. Shift values outside the range will
714 * be mod to 128. In other words, the caller is responsible to
715 * verify/assert both the shift range and plow/phigh pointers.
716 */
717void urshift(uint64_t *plow, uint64_t *phigh, int32_t shift);
718
719/**
720 * ulshift - 128-bit Unsigned Left Shift.
721 * @plow: in/out - lower 64-bit integer.
722 * @phigh: in/out - higher 64-bit integer.
723 * @shift: in - bytes to shift, between 0 and 127.
724 * @overflow: out - true if any 1-bit is shifted out.
725 *
726 * Result is zero-extended and stored in plow/phigh, which are
727 * input/output variables. Shift values outside the range will
728 * be mod to 128. In other words, the caller is responsible to
729 * verify/assert both the shift range and plow/phigh pointers.
730 */
731void ulshift(uint64_t *plow, uint64_t *phigh, int32_t shift, bool *overflow);
732
Luis Pires8ac2d6c2021-10-25 16:11:37 -0300733/* From the GNU Multi Precision Library - longlong.h __udiv_qrnnd
734 * (https://gmplib.org/repo/gmp/file/tip/longlong.h)
735 *
736 * Licensed under the GPLv2/LGPLv3
737 */
738static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1,
739 uint64_t n0, uint64_t d)
740{
741#if defined(__x86_64__)
742 uint64_t q;
743 asm("divq %4" : "=a"(q), "=d"(*r) : "0"(n0), "1"(n1), "rm"(d));
744 return q;
745#elif defined(__s390x__) && !defined(__clang__)
746 /* Need to use a TImode type to get an even register pair for DLGR. */
747 unsigned __int128 n = (unsigned __int128)n1 << 64 | n0;
748 asm("dlgr %0, %1" : "+r"(n) : "r"(d));
749 *r = n >> 64;
750 return n;
751#elif defined(_ARCH_PPC64) && defined(_ARCH_PWR7)
752 /* From Power ISA 2.06, programming note for divdeu. */
753 uint64_t q1, q2, Q, r1, r2, R;
754 asm("divdeu %0,%2,%4; divdu %1,%3,%4"
755 : "=&r"(q1), "=r"(q2)
756 : "r"(n1), "r"(n0), "r"(d));
757 r1 = -(q1 * d); /* low part of (n1<<64) - (q1 * d) */
758 r2 = n0 - (q2 * d);
759 Q = q1 + q2;
760 R = r1 + r2;
761 if (R >= d || R < r2) { /* overflow implies R > d */
762 Q += 1;
763 R -= d;
764 }
765 *r = R;
766 return Q;
767#else
768 uint64_t d0, d1, q0, q1, r1, r0, m;
769
770 d0 = (uint32_t)d;
771 d1 = d >> 32;
772
773 r1 = n1 % d1;
774 q1 = n1 / d1;
775 m = q1 * d0;
776 r1 = (r1 << 32) | (n0 >> 32);
777 if (r1 < m) {
778 q1 -= 1;
779 r1 += d;
780 if (r1 >= d) {
781 if (r1 < m) {
782 q1 -= 1;
783 r1 += d;
784 }
785 }
786 }
787 r1 -= m;
788
789 r0 = r1 % d1;
790 q0 = r1 / d1;
791 m = q0 * d0;
792 r0 = (r0 << 32) | (uint32_t)n0;
793 if (r0 < m) {
794 q0 -= 1;
795 r0 += d;
796 if (r0 >= d) {
797 if (r0 < m) {
798 q0 -= 1;
799 r0 += d;
800 }
801 }
802 }
803 r0 -= m;
804
805 *r = r0;
806 return (q1 << 32) | q0;
807#endif
808}
809
Paolo Bonzinicb9c3772012-12-06 12:15:58 +0100810#endif