[PATCH 1/2] include/qemu/int128.h: define struct Int128 according to the host endianness

matheus.ferst@eldorado.org.br posted 2 patches 4 years, 5 months ago
Maintainers: Greg Kurz <groug@kaod.org>, David Gibson <david@gibson.dropbear.id.au>
There is a newer version of this series
[PATCH 1/2] include/qemu/int128.h: define struct Int128 according to the host endianness
Posted by matheus.ferst@eldorado.org.br 4 years, 5 months ago
From: Matheus Ferst <matheus.ferst@eldorado.org.br>

Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Matheus Ferst <matheus.ferst@eldorado.org.br>
---
 include/qemu/int128.h | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/include/qemu/int128.h b/include/qemu/int128.h
index 64500385e3..e36c6e6db5 100644
--- a/include/qemu/int128.h
+++ b/include/qemu/int128.h
@@ -163,23 +163,28 @@ static inline Int128 bswap128(Int128 a)
 typedef struct Int128 Int128;
 
 struct Int128 {
+#ifdef HOST_WORDS_BIGENDIAN
+    int64_t hi;
+    uint64_t lo;
+#else
     uint64_t lo;
     int64_t hi;
+#endif
 };
 
 static inline Int128 int128_make64(uint64_t a)
 {
-    return (Int128) { a, 0 };
+    return (Int128) { .lo = a, .hi = 0 };
 }
 
 static inline Int128 int128_makes64(int64_t a)
 {
-    return (Int128) { a, a >> 63 };
+    return (Int128) { .lo = a, .hi = a >> 63 };
 }
 
 static inline Int128 int128_make128(uint64_t lo, uint64_t hi)
 {
-    return (Int128) { lo, hi };
+    return (Int128) { .lo = lo, .hi = hi };
 }
 
 static inline uint64_t int128_get64(Int128 a)
@@ -210,22 +215,22 @@ static inline Int128 int128_one(void)
 
 static inline Int128 int128_2_64(void)
 {
-    return (Int128) { 0, 1 };
+    return int128_make128(0, 1);
 }
 
 static inline Int128 int128_exts64(int64_t a)
 {
-    return (Int128) { .lo = a, .hi = (a < 0) ? -1 : 0 };
+    return int128_make128(a, (a < 0) ? -1 : 0);
 }
 
 static inline Int128 int128_and(Int128 a, Int128 b)
 {
-    return (Int128) { a.lo & b.lo, a.hi & b.hi };
+    return int128_make128(a.lo & b.lo, a.hi & b.hi);
 }
 
 static inline Int128 int128_or(Int128 a, Int128 b)
 {
-    return (Int128) { a.lo | b.lo, a.hi | b.hi };
+    return int128_make128(a.lo | b.lo, a.hi | b.hi);
 }
 
 static inline Int128 int128_rshift(Int128 a, int n)
-- 
2.25.1


Re: [PATCH 1/2] include/qemu/int128.h: define struct Int128 according to the host endianness
Posted by Peter Maydell 4 years, 5 months ago
On Tue, 24 Aug 2021 at 21:12, <matheus.ferst@eldorado.org.br> wrote:
>
> From: Matheus Ferst <matheus.ferst@eldorado.org.br>
>
> Suggested-by: Peter Maydell <peter.maydell@linaro.org>
> Signed-off-by: Matheus Ferst <matheus.ferst@eldorado.org.br>
> ---

Reviewed-by: Peter Maydell <peter.maydell@linaro.org>

thanks
-- PMM

Re: [PATCH 1/2] include/qemu/int128.h: define struct Int128 according to the host endianness
Posted by Peter Maydell 4 years, 5 months ago
On Thu, 26 Aug 2021 at 14:11, Peter Maydell <peter.maydell@linaro.org> wrote:
>
> On Tue, 24 Aug 2021 at 21:12, <matheus.ferst@eldorado.org.br> wrote:
> >
> > From: Matheus Ferst <matheus.ferst@eldorado.org.br>
> >
> > Suggested-by: Peter Maydell <peter.maydell@linaro.org>
> > Signed-off-by: Matheus Ferst <matheus.ferst@eldorado.org.br>
> > ---
>
> Reviewed-by: Peter Maydell <peter.maydell@linaro.org>

Oh, you could add a comment before the struct definition:

 /*
  * 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).
  */
 struct Int128 {
     ....

so we don't forget why we put this ifdef in.

-- PMM