diff mbox series

[1/2] tests/tcg/multiarch: Make the system memory test work on big-endian

Message ID 20230422005808.1773015-2-iii@linux.ibm.com (mailing list archive)
State New, archived
Headers show
Series tests/tcg/s390x: Enable the multiarch system tests | expand

Commit Message

Ilya Leoshkevich April 22, 2023, 12:58 a.m. UTC
Make sure values are stored in memory as little-endian regardless of
the host endianness.

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
 tests/tcg/multiarch/system/memory.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

Comments

Thomas Weißschuh April 22, 2023, 11:02 a.m. UTC | #1
On 2023-04-22 02:58:07+0200, Ilya Leoshkevich wrote:
> Make sure values are stored in memory as little-endian regardless of
> the host endianness.
> 
> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> ---
>  tests/tcg/multiarch/system/memory.c | 24 ++++++++++++++++++++++++
>  1 file changed, 24 insertions(+)
> 
> diff --git a/tests/tcg/multiarch/system/memory.c b/tests/tcg/multiarch/system/memory.c
> index 214f7d4f54b..8ef6666b440 100644
> --- a/tests/tcg/multiarch/system/memory.c
> +++ b/tests/tcg/multiarch/system/memory.c
> @@ -121,6 +121,9 @@ static void init_test_data_u16(int offset)
>      for (i = 0; i < max; i++) {
>          uint8_t low = count++, high = count++;
>          word = BYTE_SHIFT(high, 1) | BYTE_SHIFT(low, 0);
> +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
> +        word = __builtin_bswap16(word);
> +#endif

These looks like a usecase for cpu_to_le16() and friends.

>          *ptr++ = word;
>          pdot(i);
>      }
> @@ -142,6 +145,9 @@ static void init_test_data_u32(int offset)
>          uint8_t b4 = count++, b3 = count++;
>          uint8_t b2 = count++, b1 = count++;
>          word = BYTE_SHIFT(b1, 3) | BYTE_SHIFT(b2, 2) | BYTE_SHIFT(b3, 1) | b4;
> +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
> +        word = __builtin_bswap32(word);
> +#endif
>          *ptr++ = word;
>          pdot(i);
>      }
> [..]
Peter Maydell April 22, 2023, 12:18 p.m. UTC | #2
On Sat, 22 Apr 2023 at 12:03, Thomas Weißschuh <thomas@t-8ch.de> wrote:
>
> On 2023-04-22 02:58:07+0200, Ilya Leoshkevich wrote:
> > Make sure values are stored in memory as little-endian regardless of
> > the host endianness.
> >
> > Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> > ---
> >  tests/tcg/multiarch/system/memory.c | 24 ++++++++++++++++++++++++
> >  1 file changed, 24 insertions(+)
> >
> > diff --git a/tests/tcg/multiarch/system/memory.c b/tests/tcg/multiarch/system/memory.c
> > index 214f7d4f54b..8ef6666b440 100644
> > --- a/tests/tcg/multiarch/system/memory.c
> > +++ b/tests/tcg/multiarch/system/memory.c
> > @@ -121,6 +121,9 @@ static void init_test_data_u16(int offset)
> >      for (i = 0; i < max; i++) {
> >          uint8_t low = count++, high = count++;
> >          word = BYTE_SHIFT(high, 1) | BYTE_SHIFT(low, 0);
> > +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
> > +        word = __builtin_bswap16(word);
> > +#endif
>
> These looks like a usecase for cpu_to_le16() and friends.

I'm not sure this test code has direct access to those, but
the general principle is right.

It is particularly odd that we carefully manually put
together a word by shifting-and-ORing together the
various bytes in it, and then separately do a byteswap.

thanks
-- PMM
diff mbox series

Patch

diff --git a/tests/tcg/multiarch/system/memory.c b/tests/tcg/multiarch/system/memory.c
index 214f7d4f54b..8ef6666b440 100644
--- a/tests/tcg/multiarch/system/memory.c
+++ b/tests/tcg/multiarch/system/memory.c
@@ -121,6 +121,9 @@  static void init_test_data_u16(int offset)
     for (i = 0; i < max; i++) {
         uint8_t low = count++, high = count++;
         word = BYTE_SHIFT(high, 1) | BYTE_SHIFT(low, 0);
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+        word = __builtin_bswap16(word);
+#endif
         *ptr++ = word;
         pdot(i);
     }
@@ -142,6 +145,9 @@  static void init_test_data_u32(int offset)
         uint8_t b4 = count++, b3 = count++;
         uint8_t b2 = count++, b1 = count++;
         word = BYTE_SHIFT(b1, 3) | BYTE_SHIFT(b2, 2) | BYTE_SHIFT(b3, 1) | b4;
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+        word = __builtin_bswap32(word);
+#endif
         *ptr++ = word;
         pdot(i);
     }
@@ -167,6 +173,9 @@  static void init_test_data_u64(int offset)
         word = BYTE_SHIFT(b1, 7) | BYTE_SHIFT(b2, 6) | BYTE_SHIFT(b3, 5) |
                BYTE_SHIFT(b4, 4) | BYTE_SHIFT(b5, 3) | BYTE_SHIFT(b6, 2) |
                BYTE_SHIFT(b7, 1) | b8;
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+        word = __builtin_bswap64(word);
+#endif
         *ptr++ = word;
         pdot(i);
     }
@@ -184,6 +193,9 @@  static bool read_test_data_u16(int offset)
     for (i = 0; i < max; i++) {
         uint8_t high, low;
         word = *ptr++;
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+        word = __builtin_bswap16(word);
+#endif
         high = (word >> 8) & 0xff;
         low = word & 0xff;
         if (high < low && high != 0) {
@@ -210,6 +222,9 @@  static bool read_test_data_u32(int offset)
         uint8_t b1, b2, b3, b4;
         int zeros = 0;
         word = *ptr++;
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+        word = __builtin_bswap32(word);
+#endif
 
         b1 = word >> 24 & 0xff;
         b2 = word >> 16 & 0xff;
@@ -251,6 +266,9 @@  static bool read_test_data_u64(int offset)
         uint8_t b1, b2, b3, b4, b5, b6, b7, b8;
         int zeros = 0;
         word = *ptr++;
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+        word = __builtin_bswap64(word);
+#endif
 
         b1 = ((uint64_t) (word >> 56)) & 0xff;
         b2 = ((uint64_t) (word >> 48)) & 0xff;
@@ -376,6 +394,9 @@  static bool read_test_data_s16(int offset, bool neg_first)
 
     for (i = 0; i < max; i++) {
         int32_t data = *ptr++;
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+        data = __builtin_bswap16(data);
+#endif
 
         if (neg_first && data < 0) {
             pdot(i);
@@ -401,6 +422,9 @@  static bool read_test_data_s32(int offset, bool neg_first)
 
     for (i = 0; i < max; i++) {
         int64_t data = *ptr++;
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+        data = __builtin_bswap32(data);
+#endif
 
         if (neg_first && data < 0) {
             pdot(i);