qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 1/7] int128: use unsigned 128 bit arithmetic


From: Pierre Morel
Subject: [Qemu-devel] [PATCH 1/7] int128: use unsigned 128 bit arithmetic
Date: Thu, 5 Nov 2015 17:18:53 +0100

Let's change Int128 definition to UInt128,
modify the internal element to uint64_t and
the arithmetic functions accordingly.

Signed-off-by: Pierre Morel <address@hidden>
---
 include/qemu/int128.h |   95 +++++++++++++++++++++++--------------------------
 1 files changed, 45 insertions(+), 50 deletions(-)

diff --git a/include/qemu/int128.h b/include/qemu/int128.h
index fb782aa..c0465a2 100644
--- a/include/qemu/int128.h
+++ b/include/qemu/int128.h
@@ -5,64 +5,64 @@
 #include <stdint.h>
 #include <stdbool.h>
 
-typedef struct Int128 Int128;
+typedef struct UInt128 UInt128;
 
-struct Int128 {
+struct UInt128 {
     uint64_t lo;
-    int64_t hi;
+    uint64_t hi;
 };
 
-static inline Int128 int128_make64(uint64_t a)
+static inline UInt128 uint128_from_64(uint64_t a)
 {
-    return (Int128) { a, 0 };
+    return (UInt128) { a, 0 };
 }
 
-static inline uint64_t int128_get64(Int128 a)
+static inline uint64_t uint128_to_64(UInt128 a)
 {
     assert(!a.hi);
     return a.lo;
 }
 
-static inline Int128 int128_zero(void)
+static inline UInt128 uint128_zero(void)
 {
-    return int128_make64(0);
+    return uint128_from_64(0);
 }
 
-static inline Int128 int128_one(void)
+static inline UInt128 uint128_one(void)
 {
-    return int128_make64(1);
+    return uint128_from_64(1);
 }
 
-static inline Int128 int128_2_64(void)
+static inline UInt128 uint128_2_64(void)
 {
-    return (Int128) { 0, 1 };
+    return (UInt128) { UINT64_MAX, 0 };
 }
 
-static inline Int128 int128_exts64(int64_t a)
+static inline UInt128 uint128_exts64(int64_t a)
 {
-    return (Int128) { .lo = a, .hi = (a < 0) ? -1 : 0 };
+    return (UInt128) { .lo = a, .hi = (a < 0) ? -1 : 0 };
 }
 
-static inline Int128 int128_and(Int128 a, Int128 b)
+static inline UInt128 uint128_and(UInt128 a, UInt128 b)
 {
-    return (Int128) { a.lo & b.lo, a.hi & b.hi };
+    return (UInt128) { a.lo & b.lo, a.hi & b.hi };
 }
 
-static inline Int128 int128_rshift(Int128 a, int n)
+static inline UInt128 uint128_rshift(UInt128 a, unsigned int n)
 {
-    int64_t h;
+    uint64_t h;
     if (!n) {
         return a;
     }
     h = a.hi >> (n & 63);
     if (n >= 64) {
-        return (Int128) { h, h >> 63 };
+        return (UInt128) { h, h >> 63 };
     } else {
-        return (Int128) { (a.lo >> n) | ((uint64_t)a.hi << (64 - n)), h };
+        return (UInt128) { (a.lo >> n) | ((uint64_t)a.hi << (64 - n)), h };
     }
 }
 
-static inline Int128 int128_add(Int128 a, Int128 b)
+static inline UInt128 uint128_add(UInt128 a, UInt128 b)
 {
     uint64_t lo = a.lo + b.lo;
 
@@ -72,78 +72,73 @@ static inline Int128 int128_add(Int128 a, Int128 b)
      *
      * So the carry is lo < a.lo.
      */
-    return (Int128) { lo, (uint64_t)a.hi + b.hi + (lo < a.lo) };
+    return (UInt128) { lo, a.hi + b.hi + (lo < a.lo) };
 }
 
-static inline Int128 int128_neg(Int128 a)
+static inline UInt128 uint128_neg(UInt128 a)
 {
     uint64_t lo = -a.lo;
-    return (Int128) { lo, ~(uint64_t)a.hi + !lo };
+    return (UInt128) { lo, ~a.hi + !lo };
 }
 
-static inline Int128 int128_sub(Int128 a, Int128 b)
+static inline UInt128 uint128_sub(UInt128 a, UInt128 b)
 {
-    return (Int128){ a.lo - b.lo, (uint64_t)a.hi - b.hi - (a.lo < b.lo) };
+    return (UInt128){ a.lo - b.lo, a.hi - b.hi - (a.lo < b.lo) };
 }
 
-static inline bool int128_nonneg(Int128 a)
-{
-    return a.hi >= 0;
-}
-
-static inline bool int128_eq(Int128 a, Int128 b)
+static inline bool uint128_eq(UInt128 a, UInt128 b)
 {
     return a.lo == b.lo && a.hi == b.hi;
 }
 
-static inline bool int128_ne(Int128 a, Int128 b)
+static inline bool uint128_ne(UInt128 a, UInt128 b)
 {
-    return !int128_eq(a, b);
+    return !uint128_eq(a, b);
 }
 
-static inline bool int128_ge(Int128 a, Int128 b)
+static inline bool uint128_ge(UInt128 a, UInt128 b)
 {
     return a.hi > b.hi || (a.hi == b.hi && a.lo >= b.lo);
 }
 
-static inline bool int128_lt(Int128 a, Int128 b)
+static inline bool uint128_lt(UInt128 a, UInt128 b)
 {
-    return !int128_ge(a, b);
+    return !uint128_ge(a, b);
 }
 
-static inline bool int128_le(Int128 a, Int128 b)
+static inline bool uint128_le(UInt128 a, UInt128 b)
 {
-    return int128_ge(b, a);
+    return uint128_ge(b, a);
 }
 
-static inline bool int128_gt(Int128 a, Int128 b)
+static inline bool uint128_gt(UInt128 a, UInt128 b)
 {
-    return !int128_le(a, b);
+    return !uint128_le(a, b);
 }
 
-static inline bool int128_nz(Int128 a)
+static inline bool uint128_nz(UInt128 a)
 {
     return a.lo || a.hi;
 }
 
-static inline Int128 int128_min(Int128 a, Int128 b)
+static inline UInt128 uint128_min(UInt128 a, UInt128 b)
 {
-    return int128_le(a, b) ? a : b;
+    return uint128_le(a, b) ? a : b;
 }
 
-static inline Int128 int128_max(Int128 a, Int128 b)
+static inline UInt128 uint128_max(UInt128 a, UInt128 b)
 {
-    return int128_ge(a, b) ? a : b;
+    return uint128_ge(a, b) ? a : b;
 }
 
-static inline void int128_addto(Int128 *a, Int128 b)
+static inline void uint128_addto(UInt128 *a, UInt128 b)
 {
-    *a = int128_add(*a, b);
+    *a = uint128_add(*a, b);
 }
 
-static inline void int128_subfrom(Int128 *a, Int128 b)
+static inline void uint128_subfrom(UInt128 *a, UInt128 b)
 {
-    *a = int128_sub(*a, b);
+    *a = uint128_sub(*a, b);
 }
 
 #endif
-- 
1.7.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]