2011-10-16 13:13:05 +02:00
|
|
|
#ifndef INT128_H
|
|
|
|
#define INT128_H
|
|
|
|
|
2016-06-30 06:10:59 +02:00
|
|
|
#include "qemu/bswap.h"
|
2016-06-30 00:52:10 +02:00
|
|
|
|
2022-10-17 01:17:20 +02:00
|
|
|
/*
|
|
|
|
* With TCI, we need to use libffi for interfacing with TCG helpers.
|
|
|
|
* But libffi does not support __int128_t, and therefore cannot pass
|
|
|
|
* or return values of this type, force use of the Int128 struct.
|
|
|
|
*/
|
|
|
|
#if defined(CONFIG_INT128) && !defined(CONFIG_TCG_INTERPRETER)
|
2016-06-30 00:52:10 +02:00
|
|
|
typedef __int128_t Int128;
|
|
|
|
|
|
|
|
static inline Int128 int128_make64(uint64_t a)
|
|
|
|
{
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2021-06-14 17:09:43 +02:00
|
|
|
static inline Int128 int128_makes64(int64_t a)
|
|
|
|
{
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2016-06-30 01:57:26 +02:00
|
|
|
static inline Int128 int128_make128(uint64_t lo, uint64_t hi)
|
|
|
|
{
|
|
|
|
return (__uint128_t)hi << 64 | lo;
|
|
|
|
}
|
|
|
|
|
2016-06-30 00:52:10 +02:00
|
|
|
static inline uint64_t int128_get64(Int128 a)
|
|
|
|
{
|
|
|
|
uint64_t r = a;
|
|
|
|
assert(r == a);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t int128_getlo(Int128 a)
|
|
|
|
{
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int64_t int128_gethi(Int128 a)
|
|
|
|
{
|
|
|
|
return a >> 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Int128 int128_zero(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Int128 int128_one(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Int128 int128_2_64(void)
|
|
|
|
{
|
|
|
|
return (Int128)1 << 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Int128 int128_exts64(int64_t a)
|
|
|
|
{
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2021-10-25 14:28:03 +02:00
|
|
|
static inline Int128 int128_not(Int128 a)
|
|
|
|
{
|
|
|
|
return ~a;
|
|
|
|
}
|
|
|
|
|
2016-06-30 00:52:10 +02:00
|
|
|
static inline Int128 int128_and(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return a & b;
|
|
|
|
}
|
|
|
|
|
2020-10-21 06:51:35 +02:00
|
|
|
static inline Int128 int128_or(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return a | b;
|
|
|
|
}
|
|
|
|
|
2021-10-25 14:28:03 +02:00
|
|
|
static inline Int128 int128_xor(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return a ^ b;
|
|
|
|
}
|
|
|
|
|
2016-06-30 00:52:10 +02:00
|
|
|
static inline Int128 int128_rshift(Int128 a, int n)
|
|
|
|
{
|
|
|
|
return a >> n;
|
|
|
|
}
|
|
|
|
|
2022-03-30 19:59:26 +02:00
|
|
|
static inline Int128 int128_urshift(Int128 a, int n)
|
|
|
|
{
|
|
|
|
return (__uint128_t)a >> n;
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:02:47 +02:00
|
|
|
static inline Int128 int128_lshift(Int128 a, int n)
|
|
|
|
{
|
|
|
|
return a << n;
|
|
|
|
}
|
|
|
|
|
2016-06-30 00:52:10 +02:00
|
|
|
static inline Int128 int128_add(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Int128 int128_neg(Int128 a)
|
|
|
|
{
|
|
|
|
return -a;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Int128 int128_sub(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return a - b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool int128_nonneg(Int128 a)
|
|
|
|
{
|
|
|
|
return a >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool int128_eq(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool int128_ne(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return a != b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool int128_ge(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return a >= b;
|
|
|
|
}
|
|
|
|
|
2022-05-25 15:49:50 +02:00
|
|
|
static inline bool int128_uge(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return ((__uint128_t)a) >= ((__uint128_t)b);
|
|
|
|
}
|
|
|
|
|
2016-06-30 00:52:10 +02:00
|
|
|
static inline bool int128_lt(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return a < b;
|
|
|
|
}
|
|
|
|
|
2022-05-25 15:49:50 +02:00
|
|
|
static inline bool int128_ult(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return (__uint128_t)a < (__uint128_t)b;
|
|
|
|
}
|
|
|
|
|
2016-06-30 00:52:10 +02:00
|
|
|
static inline bool int128_le(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return a <= b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool int128_gt(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return a > b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool int128_nz(Int128 a)
|
|
|
|
{
|
|
|
|
return a != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Int128 int128_min(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return a < b ? a : b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Int128 int128_max(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return a > b ? a : b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void int128_addto(Int128 *a, Int128 b)
|
|
|
|
{
|
|
|
|
*a += b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void int128_subfrom(Int128 *a, Int128 b)
|
|
|
|
{
|
|
|
|
*a -= b;
|
|
|
|
}
|
|
|
|
|
2016-06-30 06:10:59 +02:00
|
|
|
static inline Int128 bswap128(Int128 a)
|
|
|
|
{
|
2021-08-26 16:56:55 +02:00
|
|
|
#if __has_builtin(__builtin_bswap128)
|
|
|
|
return __builtin_bswap128(a);
|
|
|
|
#else
|
2016-06-30 06:10:59 +02:00
|
|
|
return int128_make128(bswap64(int128_gethi(a)), bswap64(int128_getlo(a)));
|
2021-08-26 16:56:55 +02:00
|
|
|
#endif
|
2016-06-30 06:10:59 +02:00
|
|
|
}
|
|
|
|
|
2022-05-25 15:49:50 +02:00
|
|
|
static inline int clz128(Int128 a)
|
|
|
|
{
|
|
|
|
if (a >> 64) {
|
|
|
|
return __builtin_clzll(a >> 64);
|
|
|
|
} else {
|
|
|
|
return (a) ? __builtin_clzll((uint64_t)a) + 64 : 128;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 22:00:53 +01:00
|
|
|
static inline Int128 int128_divu(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return (__uint128_t)a / (__uint128_t)b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Int128 int128_remu(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return (__uint128_t)a % (__uint128_t)b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Int128 int128_divs(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return a / b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Int128 int128_rems(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return a % b;
|
|
|
|
}
|
|
|
|
|
2016-06-30 00:52:10 +02:00
|
|
|
#else /* !CONFIG_INT128 */
|
2013-06-20 16:19:32 +02:00
|
|
|
|
2011-10-16 13:13:05 +02:00
|
|
|
typedef struct Int128 Int128;
|
|
|
|
|
2021-08-26 16:14:45 +02:00
|
|
|
/*
|
|
|
|
* We guarantee that the in-memory byte representation of an
|
|
|
|
* Int128 is that of a host-endian-order 128-bit integer
|
|
|
|
* (whether using this struct or the __int128_t version of the type).
|
|
|
|
* Some code using this type relies on this (eg when copying it into
|
|
|
|
* guest memory or a gdb protocol buffer, or by using Int128 in
|
|
|
|
* a union with other integer types).
|
|
|
|
*/
|
2011-10-16 13:13:05 +02:00
|
|
|
struct Int128 {
|
2022-03-23 16:57:17 +01:00
|
|
|
#if HOST_BIG_ENDIAN
|
2021-08-26 16:14:45 +02:00
|
|
|
int64_t hi;
|
|
|
|
uint64_t lo;
|
|
|
|
#else
|
2011-10-16 13:13:05 +02:00
|
|
|
uint64_t lo;
|
|
|
|
int64_t hi;
|
2021-08-26 16:14:45 +02:00
|
|
|
#endif
|
2011-10-16 13:13:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline Int128 int128_make64(uint64_t a)
|
|
|
|
{
|
2021-08-26 16:14:45 +02:00
|
|
|
return (Int128) { .lo = a, .hi = 0 };
|
2011-10-16 13:13:05 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 17:09:43 +02:00
|
|
|
static inline Int128 int128_makes64(int64_t a)
|
|
|
|
{
|
2021-08-26 16:14:45 +02:00
|
|
|
return (Int128) { .lo = a, .hi = a >> 63 };
|
2021-06-14 17:09:43 +02:00
|
|
|
}
|
|
|
|
|
2016-06-30 01:57:26 +02:00
|
|
|
static inline Int128 int128_make128(uint64_t lo, uint64_t hi)
|
|
|
|
{
|
2021-08-26 16:14:45 +02:00
|
|
|
return (Int128) { .lo = lo, .hi = hi };
|
2016-06-30 01:57:26 +02:00
|
|
|
}
|
|
|
|
|
2011-10-16 13:13:05 +02:00
|
|
|
static inline uint64_t int128_get64(Int128 a)
|
|
|
|
{
|
|
|
|
assert(!a.hi);
|
|
|
|
return a.lo;
|
|
|
|
}
|
|
|
|
|
2016-06-30 00:48:03 +02:00
|
|
|
static inline uint64_t int128_getlo(Int128 a)
|
|
|
|
{
|
|
|
|
return a.lo;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int64_t int128_gethi(Int128 a)
|
|
|
|
{
|
|
|
|
return a.hi;
|
|
|
|
}
|
|
|
|
|
2011-10-16 13:13:05 +02:00
|
|
|
static inline Int128 int128_zero(void)
|
|
|
|
{
|
|
|
|
return int128_make64(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Int128 int128_one(void)
|
|
|
|
{
|
|
|
|
return int128_make64(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Int128 int128_2_64(void)
|
|
|
|
{
|
2021-08-26 16:14:45 +02:00
|
|
|
return int128_make128(0, 1);
|
2011-10-16 13:13:05 +02:00
|
|
|
}
|
|
|
|
|
2014-05-30 21:00:28 +02:00
|
|
|
static inline Int128 int128_exts64(int64_t a)
|
|
|
|
{
|
2021-08-26 16:14:45 +02:00
|
|
|
return int128_make128(a, (a < 0) ? -1 : 0);
|
2014-05-30 21:00:28 +02:00
|
|
|
}
|
|
|
|
|
2021-10-25 14:28:03 +02:00
|
|
|
static inline Int128 int128_not(Int128 a)
|
|
|
|
{
|
|
|
|
return int128_make128(~a.lo, ~a.hi);
|
|
|
|
}
|
|
|
|
|
2013-05-27 10:08:27 +02:00
|
|
|
static inline Int128 int128_and(Int128 a, Int128 b)
|
|
|
|
{
|
2021-08-26 16:14:45 +02:00
|
|
|
return int128_make128(a.lo & b.lo, a.hi & b.hi);
|
2013-05-27 10:08:27 +02:00
|
|
|
}
|
|
|
|
|
2020-10-21 06:51:35 +02:00
|
|
|
static inline Int128 int128_or(Int128 a, Int128 b)
|
|
|
|
{
|
2021-08-26 16:14:45 +02:00
|
|
|
return int128_make128(a.lo | b.lo, a.hi | b.hi);
|
2020-10-21 06:51:35 +02:00
|
|
|
}
|
|
|
|
|
2021-10-25 14:28:03 +02:00
|
|
|
static inline Int128 int128_xor(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return int128_make128(a.lo ^ b.lo, a.hi ^ b.hi);
|
|
|
|
}
|
|
|
|
|
2013-05-27 10:08:27 +02:00
|
|
|
static inline Int128 int128_rshift(Int128 a, int n)
|
|
|
|
{
|
|
|
|
int64_t h;
|
|
|
|
if (!n) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
h = a.hi >> (n & 63);
|
|
|
|
if (n >= 64) {
|
2016-06-30 01:57:26 +02:00
|
|
|
return int128_make128(h, h >> 63);
|
2013-05-27 10:08:27 +02:00
|
|
|
} else {
|
2016-06-30 01:57:26 +02:00
|
|
|
return int128_make128((a.lo >> n) | ((uint64_t)a.hi << (64 - n)), h);
|
2013-05-27 10:08:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 19:59:26 +02:00
|
|
|
static inline Int128 int128_urshift(Int128 a, int n)
|
|
|
|
{
|
|
|
|
uint64_t h = a.hi;
|
|
|
|
if (!n) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
h = h >> (n & 63);
|
|
|
|
if (n >= 64) {
|
|
|
|
return int128_make64(h);
|
|
|
|
} else {
|
|
|
|
return int128_make128((a.lo >> n) | ((uint64_t)a.hi << (64 - n)), h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:02:47 +02:00
|
|
|
static inline Int128 int128_lshift(Int128 a, int n)
|
|
|
|
{
|
|
|
|
uint64_t l = a.lo << (n & 63);
|
|
|
|
if (n >= 64) {
|
|
|
|
return int128_make128(0, l);
|
|
|
|
} else if (n > 0) {
|
|
|
|
return int128_make128(l, (a.hi << n) | (a.lo >> (64 - n)));
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2011-10-16 13:13:05 +02:00
|
|
|
static inline Int128 int128_add(Int128 a, Int128 b)
|
|
|
|
{
|
2013-06-20 16:19:32 +02:00
|
|
|
uint64_t lo = a.lo + b.lo;
|
|
|
|
|
|
|
|
/* a.lo <= a.lo + b.lo < a.lo + k (k is the base, 2^64). Hence,
|
|
|
|
* a.lo + b.lo >= k implies 0 <= lo = a.lo + b.lo - k < a.lo.
|
|
|
|
* Similarly, a.lo + b.lo < k implies a.lo <= lo = a.lo + b.lo < k.
|
|
|
|
*
|
|
|
|
* So the carry is lo < a.lo.
|
|
|
|
*/
|
2016-06-30 01:57:26 +02:00
|
|
|
return int128_make128(lo, (uint64_t)a.hi + b.hi + (lo < a.lo));
|
2011-10-16 13:13:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline Int128 int128_neg(Int128 a)
|
|
|
|
{
|
2013-06-20 16:19:32 +02:00
|
|
|
uint64_t lo = -a.lo;
|
2016-06-30 01:57:26 +02:00
|
|
|
return int128_make128(lo, ~(uint64_t)a.hi + !lo);
|
2011-10-16 13:13:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline Int128 int128_sub(Int128 a, Int128 b)
|
|
|
|
{
|
2016-06-30 01:57:26 +02:00
|
|
|
return int128_make128(a.lo - b.lo, (uint64_t)a.hi - b.hi - (a.lo < b.lo));
|
2011-10-16 13:13:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool int128_nonneg(Int128 a)
|
|
|
|
{
|
|
|
|
return a.hi >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool int128_eq(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return a.lo == b.lo && a.hi == b.hi;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool int128_ne(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return !int128_eq(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool int128_ge(Int128 a, Int128 b)
|
|
|
|
{
|
2013-06-20 16:19:32 +02:00
|
|
|
return a.hi > b.hi || (a.hi == b.hi && a.lo >= b.lo);
|
2011-10-16 13:13:05 +02:00
|
|
|
}
|
|
|
|
|
2022-05-25 15:49:50 +02:00
|
|
|
static inline bool int128_uge(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return (uint64_t)a.hi > (uint64_t)b.hi || (a.hi == b.hi && a.lo >= b.lo);
|
|
|
|
}
|
|
|
|
|
2011-10-16 13:13:05 +02:00
|
|
|
static inline bool int128_lt(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return !int128_ge(a, b);
|
|
|
|
}
|
|
|
|
|
2022-05-25 15:49:50 +02:00
|
|
|
static inline bool int128_ult(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return !int128_uge(a, b);
|
|
|
|
}
|
|
|
|
|
2011-10-16 13:13:05 +02:00
|
|
|
static inline bool int128_le(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return int128_ge(b, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool int128_gt(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return !int128_le(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool int128_nz(Int128 a)
|
|
|
|
{
|
|
|
|
return a.lo || a.hi;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Int128 int128_min(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return int128_le(a, b) ? a : b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Int128 int128_max(Int128 a, Int128 b)
|
|
|
|
{
|
|
|
|
return int128_ge(a, b) ? a : b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void int128_addto(Int128 *a, Int128 b)
|
|
|
|
{
|
|
|
|
*a = int128_add(*a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void int128_subfrom(Int128 *a, Int128 b)
|
|
|
|
{
|
|
|
|
*a = int128_sub(*a, b);
|
|
|
|
}
|
|
|
|
|
2021-08-26 16:56:55 +02:00
|
|
|
static inline Int128 bswap128(Int128 a)
|
|
|
|
{
|
|
|
|
return int128_make128(bswap64(a.hi), bswap64(a.lo));
|
|
|
|
}
|
|
|
|
|
2022-05-25 15:49:50 +02:00
|
|
|
static inline int clz128(Int128 a)
|
|
|
|
{
|
|
|
|
if (a.hi) {
|
|
|
|
return __builtin_clzll(a.hi);
|
|
|
|
} else {
|
|
|
|
return (a.lo) ? __builtin_clzll(a.lo) + 64 : 128;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 22:00:53 +01:00
|
|
|
Int128 int128_divu(Int128, Int128);
|
|
|
|
Int128 int128_remu(Int128, Int128);
|
|
|
|
Int128 int128_divs(Int128, Int128);
|
|
|
|
Int128 int128_rems(Int128, Int128);
|
2022-10-17 01:17:20 +02:00
|
|
|
#endif /* CONFIG_INT128 && !CONFIG_TCG_INTERPRETER */
|
2021-08-26 16:56:55 +02:00
|
|
|
|
|
|
|
static inline void bswap128s(Int128 *s)
|
|
|
|
{
|
|
|
|
*s = bswap128(*s);
|
|
|
|
}
|
|
|
|
|
2022-01-06 22:00:53 +01:00
|
|
|
#define UINT128_MAX int128_make128(~0LL, ~0LL)
|
2022-03-30 19:59:30 +02:00
|
|
|
#define INT128_MAX int128_make128(UINT64_MAX, INT64_MAX)
|
|
|
|
#define INT128_MIN int128_make128(0, INT64_MIN)
|
2022-01-06 22:00:53 +01:00
|
|
|
|
2022-10-17 01:17:20 +02:00
|
|
|
/*
|
|
|
|
* When compiler supports a 128-bit type, define a combination of
|
|
|
|
* a possible structure and the native types. Ease parameter passing
|
|
|
|
* via use of the transparent union extension.
|
|
|
|
*/
|
|
|
|
#ifdef CONFIG_INT128
|
|
|
|
typedef union {
|
|
|
|
__uint128_t u;
|
2023-05-01 22:46:25 +02:00
|
|
|
__int128_t i;
|
|
|
|
Int128 s;
|
2022-10-17 01:17:20 +02:00
|
|
|
} Int128Alias __attribute__((transparent_union));
|
|
|
|
#else
|
|
|
|
typedef Int128 Int128Alias;
|
|
|
|
#endif /* CONFIG_INT128 */
|
|
|
|
|
2016-06-30 00:52:10 +02:00
|
|
|
#endif /* INT128_H */
|