int128: Use __int128 if available

Reviewed-by: Emilio G. Cota <cota@braap.org>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Richard Henderson <rth@twiddle.net>
This commit is contained in:
Richard Henderson 2016-06-29 15:52:10 -07:00
parent 258dfaaad0
commit 0846beb366
2 changed files with 145 additions and 12 deletions

View File

@ -1,6 +1,138 @@
#ifndef INT128_H
#define INT128_H
#ifdef CONFIG_INT128
typedef __int128_t Int128;
static inline Int128 int128_make64(uint64_t a)
{
return a;
}
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;
}
static inline Int128 int128_and(Int128 a, Int128 b)
{
return a & b;
}
static inline Int128 int128_rshift(Int128 a, int n)
{
return a >> n;
}
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;
}
static inline bool int128_lt(Int128 a, Int128 b)
{
return a < b;
}
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;
}
#else /* !CONFIG_INT128 */
typedef struct Int128 Int128;
@ -153,4 +285,5 @@ static inline void int128_subfrom(Int128 *a, Int128 b)
*a = int128_sub(*a, b);
}
#endif
#endif /* CONFIG_INT128 */
#endif /* INT128_H */

View File

@ -41,7 +41,7 @@ static Int128 expand(uint32_t x)
uint64_t l, h;
l = expand16(x & 65535);
h = expand16(x >> 16);
return (Int128) {l, h};
return (Int128) int128_make128(l, h);
};
static void test_and(void)
@ -54,8 +54,8 @@ static void test_and(void)
Int128 b = expand(tests[j]);
Int128 r = expand(tests[i] & tests[j]);
Int128 s = int128_and(a, b);
g_assert_cmpuint(r.lo, ==, s.lo);
g_assert_cmpuint(r.hi, ==, s.hi);
g_assert_cmpuint(int128_getlo(r), ==, int128_getlo(s));
g_assert_cmpuint(int128_gethi(r), ==, int128_gethi(s));
}
}
}
@ -70,8 +70,8 @@ static void test_add(void)
Int128 b = expand(tests[j]);
Int128 r = expand(tests[i] + tests[j]);
Int128 s = int128_add(a, b);
g_assert_cmpuint(r.lo, ==, s.lo);
g_assert_cmpuint(r.hi, ==, s.hi);
g_assert_cmpuint(int128_getlo(r), ==, int128_getlo(s));
g_assert_cmpuint(int128_gethi(r), ==, int128_gethi(s));
}
}
}
@ -86,8 +86,8 @@ static void test_sub(void)
Int128 b = expand(tests[j]);
Int128 r = expand(tests[i] - tests[j]);
Int128 s = int128_sub(a, b);
g_assert_cmpuint(r.lo, ==, s.lo);
g_assert_cmpuint(r.hi, ==, s.hi);
g_assert_cmpuint(int128_getlo(r), ==, int128_getlo(s));
g_assert_cmpuint(int128_gethi(r), ==, int128_gethi(s));
}
}
}
@ -100,8 +100,8 @@ static void test_neg(void)
Int128 a = expand(tests[i]);
Int128 r = expand(-tests[i]);
Int128 s = int128_neg(a);
g_assert_cmpuint(r.lo, ==, s.lo);
g_assert_cmpuint(r.hi, ==, s.hi);
g_assert_cmpuint(int128_getlo(r), ==, int128_getlo(s));
g_assert_cmpuint(int128_gethi(r), ==, int128_gethi(s));
}
}
@ -180,8 +180,8 @@ test_rshift_one(uint32_t x, int n, uint64_t h, uint64_t l)
{
Int128 a = expand(x);
Int128 r = int128_rshift(a, n);
g_assert_cmpuint(r.lo, ==, l);
g_assert_cmpuint(r.hi, ==, h);
g_assert_cmpuint(int128_getlo(r), ==, l);
g_assert_cmpuint(int128_gethi(r), ==, h);
}
static void test_rshift(void)