int128: Add int128_make128

Allows Int128 to be used more generally, rather than having to
begin with 64-bit inputs and accumulate.

Backports commit 1edaeee0955fba7d834b7c8f4e372e7eae030745 from qemu
This commit is contained in:
Richard Henderson 2018-02-27 11:06:30 -05:00 committed by Lioncash
parent 9084e5fe1b
commit d5510a546f
No known key found for this signature in database
GPG Key ID: 4E3C3CC1031BA9C7

View File

@ -12,6 +12,11 @@ static inline Int128 int128_make64(uint64_t a)
return a; return a;
} }
static inline Int128 int128_make128(uint64_t lo, uint64_t hi)
{
return (__uint128_t)hi << 64 | lo;
}
static inline uint64_t int128_get64(Int128 a) static inline uint64_t int128_get64(Int128 a)
{ {
uint64_t r = a; uint64_t r = a;
@ -149,6 +154,14 @@ static inline Int128 int128_make64(uint64_t a)
return i128; return i128;
} }
static inline Int128 int128_make128(uint64_t lo, uint64_t hi)
{
Int128 res;
res.lo = lo;
res.hi = hi;
return res;
}
static inline uint64_t int128_get64(Int128 a) static inline uint64_t int128_get64(Int128 a)
{ {
//assert(!a.hi); //assert(!a.hi);
@ -201,11 +214,9 @@ static inline Int128 int128_rshift(Int128 a, int n)
} }
h = a.hi >> (n & 63); h = a.hi >> (n & 63);
if (n >= 64) { if (n >= 64) {
Int128 i128 = { h, h >> 63 }; return int128_make128(h, h >> 63);
return i128;
} else { } else {
Int128 i128 = { (a.lo >> n) | ((uint64_t)a.hi << (64 - n)), h }; return int128_make128((a.lo >> n) | ((uint64_t)a.hi << (64 - n)), h);
return i128;
} }
} }
@ -219,21 +230,18 @@ static inline Int128 int128_add(Int128 a, Int128 b)
* *
* So the carry is lo < a.lo. * So the carry is lo < a.lo.
*/ */
Int128 i128 = { lo, (uint64_t)a.hi + b.hi + (lo < a.lo) }; return int128_make128(lo, (uint64_t)a.hi + b.hi + (lo < a.lo));
return i128;
} }
static inline Int128 int128_neg(Int128 a) static inline Int128 int128_neg(Int128 a)
{ {
uint64_t lo = 0-a.lo; uint64_t lo = 0-a.lo;
Int128 i128 = { lo, ~(uint64_t)a.hi + !lo }; return int128_make128(lo, ~(uint64_t)a.hi + !lo);
return i128;
} }
static inline Int128 int128_sub(Int128 a, Int128 b) static inline Int128 int128_sub(Int128 a, Int128 b)
{ {
Int128 i128 = { a.lo - b.lo, (uint64_t)a.hi - b.hi - (a.lo < b.lo) }; return int128_make128(a.lo - b.lo, (uint64_t)a.hi - b.hi - (a.lo < b.lo));
return i128;
} }
static inline bool int128_nonneg(Int128 a) static inline bool int128_nonneg(Int128 a)