Skip to contentSkip to conversation

tcg: Document *swap/deposit helpers

PATCH Superseded Philippe Mathieu-Daudé 4 participants 23 messages Aug 22, 2023 – Aug 23, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover tcg: Document *swap/deposit helpers

While reviewing a recent patch from Richard optimizing deposit() [*] I ended looking at the *swap friends, taking some notes, which then evolved to proper documentation.

[*] https://lore.kernel.org/qemu-devel/[email protected]/

Philippe Mathieu-Daudé (7):
tcg/tcg-op: Document bswap16() byte pattern
tcg/tcg-op: Document bswap32() byte pattern
tcg/tcg-op: Document bswap64() byte pattern
tcg/tcg-op: Document hswap() byte pattern
tcg/tcg-op: Document wswap() byte pattern
tcg/tcg-op: Document deposit_z()
target/cris: Fix a typo in gen_swapr()

 docs/devel/tcg-ops.rst  | 12 ++++++
 target/cris/translate.c | 20 +++++----
 tcg/tcg-op.c            | 96 +++++++++++++++++++++++++++++++----------
 3 files changed, 96 insertions(+), 32 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/7 tcg/tcg-op: Document bswap16() byte pattern

---
 tcg/tcg-op.c | 48 ++++++++++++++++++++++++++++++++----------------
 1 file changed, 32 insertions(+), 16 deletions(-)
tcg/tcg-op.c +32 -16
index 7aadb37756..f164ddc95e 100644
@@ -1021,6 +1021,13 @@ void tcg_gen_ext16u_i32(TCGv_i32 ret, TCGv_i32 arg)
     }
 }
 
+/*
+ * bswap16_i32: 16-bit byte swap on the low bits of a 32-bit value.
+ *
+ * Byte pattern:  bswap16_i32(xxab) -> ..ba             (TCG_BSWAP_OZ)
+ *                bswap16_i32(xxab) -> ssba             (TCG_BSWAP_OS)
+ *                bswap16_i32(xxab) -> xxba
+ */
 void tcg_gen_bswap16_i32(TCGv_i32 ret, TCGv_i32 arg, int flags)
 {
     /* Only one extension flag may be present. */
@@ -1032,22 +1039,23 @@ void tcg_gen_bswap16_i32(TCGv_i32 ret, TCGv_i32 arg, int flags)
         TCGv_i32 t0 = tcg_temp_ebb_new_i32();
         TCGv_i32 t1 = tcg_temp_ebb_new_i32();
 
-        tcg_gen_shri_i32(t0, arg, 8);
+                                            /* arg = xxab */
+        tcg_gen_shri_i32(t0, arg, 8);       /*  t0 = .xxa */
         if (!(flags & TCG_BSWAP_IZ)) {
-            tcg_gen_ext8u_i32(t0, t0);
+            tcg_gen_ext8u_i32(t0, t0);      /*  t0 = ...a */
         }
 
         if (flags & TCG_BSWAP_OS) {
-            tcg_gen_shli_i32(t1, arg, 24);
-            tcg_gen_sari_i32(t1, t1, 16);
+            tcg_gen_shli_i32(t1, arg, 24);  /*  t1 = b... */
+            tcg_gen_sari_i32(t1, t1, 16);   /*  t1 = ssb. */
         } else if (flags & TCG_BSWAP_OZ) {
-            tcg_gen_ext8u_i32(t1, arg);
-            tcg_gen_shli_i32(t1, t1, 8);
+            tcg_gen_ext8u_i32(t1, arg);     /*  t1 = ...b */
+            tcg_gen_shli_i32(t1, t1, 8);    /*  t1 = ..b. */
         } else {
-            tcg_gen_shli_i32(t1, arg, 8);
+            tcg_gen_shli_i32(t1, arg, 8);   /*  t1 = xab. */
         }
 
-        tcg_gen_or_i32(ret, t0, t1);
+        tcg_gen_or_i32(ret, t0, t1);        /* ret = ssba */
         tcg_temp_free_i32(t0);
         tcg_temp_free_i32(t1);
     }
@@ -1721,6 +1729,13 @@ void tcg_gen_ext32u_i64(TCGv_i64 ret, TCGv_i64 arg)
     }
 }
 
+/*
+ * bswap16_i64: 16-bit byte swap on the low bits of a 64-bit value.
+ *
+ * Byte pattern:  bswap16_i32(xxxxxxab) -> ......ba     (TCG_BSWAP_OZ)
+ *                bswap16_i32(xxxxxxab) -> ssssssba     (TCG_BSWAP_OS)
+ *                bswap16_i32(xxxxxxab) -> xxxxxxba
+ */
 void tcg_gen_bswap16_i64(TCGv_i64 ret, TCGv_i64 arg, int flags)
 {
     /* Only one extension flag may be present. */
@@ -1739,22 +1754,23 @@ void tcg_gen_bswap16_i64(TCGv_i64 ret, TCGv_i64 arg, int flags)
         TCGv_i64 t0 = tcg_temp_ebb_new_i64();
         TCGv_i64 t1 = tcg_temp_ebb_new_i64();
 
-        tcg_gen_shri_i64(t0, arg, 8);
+                                            /* arg = xxxxxxab */
+        tcg_gen_shri_i64(t0, arg, 8);       /*  t0 = .xxxxxxa */
         if (!(flags & TCG_BSWAP_IZ)) {
-            tcg_gen_ext8u_i64(t0, t0);
+            tcg_gen_ext8u_i64(t0, t0);      /*  t0 = .......a */
         }
 
         if (flags & TCG_BSWAP_OS) {
-            tcg_gen_shli_i64(t1, arg, 56);
-            tcg_gen_sari_i64(t1, t1, 48);
+            tcg_gen_shli_i64(t1, arg, 56);  /*  t1 = b....... */
+            tcg_gen_sari_i64(t1, t1, 48);   /*  t1 = ssssssb. */
         } else if (flags & TCG_BSWAP_OZ) {
-            tcg_gen_ext8u_i64(t1, arg);
-            tcg_gen_shli_i64(t1, t1, 8);
+            tcg_gen_ext8u_i64(t1, arg);     /*  t1 = .......b */
+            tcg_gen_shli_i64(t1, t1, 8);    /*  t1 = ......b. */
         } else {
-            tcg_gen_shli_i64(t1, arg, 8);
+            tcg_gen_shli_i64(t1, arg, 8);   /*  t1 = xxxxxxb. */
         }
 
-        tcg_gen_or_i64(ret, t0, t1);
+        tcg_gen_or_i64(ret, t0, t1);        /* ret = ssssssba */
         tcg_temp_free_i64(t0);
         tcg_temp_free_i64(t1);
     }
Signature · 1 line
2.41.0
#2 lore raw

Re: [PATCH 1/7] tcg/tcg-op: Document bswap16() byte pattern

Quoted 10 lines · Philippe Mathieu-Daudé
---
  tcg/tcg-op.c | 48 ++++++++++++++++++++++++++++++++----------------
  1 file changed, 32 insertions(+), 16 deletions(-)
tcg/tcg-op.c +0 -0
index 7aadb37756..f164ddc95e 100644

tcg/tcg-op.c void tcg_gen_ext16u_i32(TCGv_i32 ret, TCGv_i32 arg) · line 1030

Quoted 10 lines
tcg/tcg-op.c +7 -0
@@ -1021,6 +1021,13 @@ void tcg_gen_ext16u_i32(TCGv_i32 ret, TCGv_i32 arg)
      }
  }
 
+/*
+ * bswap16_i32: 16-bit byte swap on the low bits of a 32-bit value.
+ *
+ * Byte pattern:  bswap16_i32(xxab) -> ..ba             (TCG_BSWAP_OZ)
+ *                bswap16_i32(xxab) -> ssba             (TCG_BSWAP_OS)
+ *                bswap16_i32(xxab) -> xxba
+ */

Don't forget TCG_BSWAP_IZ, which means the input is already zero-extended. Which makes

tcg/tcg-op.c void tcg_gen_bswap16_i32(TCGv_i32 ret, TCGv_i32 arg, int flags) · line 1043

Quoted 2 lines
tcg/tcg-op.c +2 -0
+                                            /* arg = xxab */
+        tcg_gen_shri_i32(t0, arg, 8);       /*  t0 = .xxa */

this

Quoted 8 lines
tcg/tcg-op.c +1 -3
          if (!(flags & TCG_BSWAP_IZ)) {
-            tcg_gen_ext8u_i32(t0, t0);
+            tcg_gen_ext8u_i32(t0, t0);      /*  t0 = ...a */
          }
 
          if (flags & TCG_BSWAP_OS) {
-            tcg_gen_shli_i32(t1, arg, 24);
-            tcg_gen_sari_i32(t1, t1, 16);

tcg/tcg-op.c void tcg_gen_bswap16_i32(TCGv_i32 ret, TCGv_i32 arg, int flags) · line 1055

Quoted 10 lines
tcg/tcg-op.c +5 -3
+            tcg_gen_shli_i32(t1, arg, 24);  /*  t1 = b... */
+            tcg_gen_sari_i32(t1, t1, 16);   /*  t1 = ssb. */
          } else if (flags & TCG_BSWAP_OZ) {
-            tcg_gen_ext8u_i32(t1, arg);
-            tcg_gen_shli_i32(t1, t1, 8);
+            tcg_gen_ext8u_i32(t1, arg);     /*  t1 = ...b */
+            tcg_gen_shli_i32(t1, t1, 8);    /*  t1 = ..b. */
          } else {
-            tcg_gen_shli_i32(t1, arg, 8);
+            tcg_gen_shli_i32(t1, arg, 8);   /*  t1 = xab. */

and this slightly inaccurate.

tcg/tcg-op.c void tcg_gen_bswap16_i32(TCGv_i32 ret, TCGv_i32 arg, int flags) · line 1058

Quoted 4 lines
          }
tcg/tcg-op.c +1 -1
-        tcg_gen_or_i32(ret, t0, t1);
+        tcg_gen_or_i32(ret, t0, t1);        /* ret = ssba */

This one is just confusing, since each of the three cases above have different outputs.

r~

r~

#3 lore raw

Re: [PATCH 1/7] tcg/tcg-op: Document bswap16() byte pattern

Quoted 48 lines · Richard Henderson
Quoted 10 lines · Philippe Mathieu-Daudé

---
  tcg/tcg-op.c | 48 ++++++++++++++++++++++++++++++++----------------

  1 file changed, 32 insertions(+), 16 deletions(-)
tcg/tcg-op.c +0 -0
index 7aadb37756..f164ddc95e 100644

tcg/tcg-op.c void tcg_gen_ext16u_i32(TCGv_i32 ret, TCGv_i32 · line 1030

Quoted 10 lines
tcg/tcg-op.c +7 -0
@@ -1021,6 +1021,13 @@ void tcg_gen_ext16u_i32(TCGv_i32 ret, TCGv_i32 
 arg)
       }
   }
+/*
+ * bswap16_i32: 16-bit byte swap on the low bits of a 32-bit value.
+ *
+ * Byte pattern:  bswap16_i32(xxab) -> ..ba             (TCG_BSWAP_OZ)
+ *                bswap16_i32(xxab) -> ssba             (TCG_BSWAP_OS)
+ *                bswap16_i32(xxab) -> xxba
+ */

Don't forget TCG_BSWAP_IZ, which means the input is already zero-extended. Which makes

tcg/tcg-op.c void tcg_gen_bswap16_i32(TCGv_i32 ret, TCGv_i32 arg, int flags) · line 1043

Quoted 2 lines
tcg/tcg-op.c +2 -0
+                                            /* arg = xxab */
+        tcg_gen_shri_i32(t0, arg, 8);       /*  t0 = .xxa */

this

Quoted 9 lines
tcg/tcg-op.c +1 -1
           if (!(flags & TCG_BSWAP_IZ)) {
-            tcg_gen_ext8u_i32(t0, t0);
+            tcg_gen_ext8u_i32(t0, t0);      /*  t0 = ...a */

          }
          if (flags & TCG_BSWAP_OS) {
-            tcg_gen_shli_i32(t1, arg, 24);
-            tcg_gen_sari_i32(t1, t1, 16);
+            tcg_gen_shli_i32(t1, arg, 24);  /*  t1 = b... */ +            tcg_gen_sari_i32(t1, t1, 16);   /*  t1 = ssb. */

Quoted 8 lines

          } else if (flags & TCG_BSWAP_OZ) {
-            tcg_gen_ext8u_i32(t1, arg);
-            tcg_gen_shli_i32(t1, t1, 8);
+            tcg_gen_ext8u_i32(t1, arg);     /*  t1 = ...b */ +            tcg_gen_shli_i32(t1, t1, 8);    /*  t1 = ..b. */ } else {
-            tcg_gen_shli_i32(t1, arg, 8);
+            tcg_gen_shli_i32(t1, arg, 8);   /*  t1 = xab. */

Quoted 7 lines

and this slightly inaccurate.

Quoted 3 lines

          }
-        tcg_gen_or_i32(ret, t0, t1);
+        tcg_gen_or_i32(ret, t0, t1);        /* ret = ssba */

This one is just confusing, since each of the three cases above have different outputs.

Is that formatting OK with you?

/*
* bswap16_i32: 16-bit byte swap on the low bits of a 32-bit value. *

 * Byte pattern:  bswap16_i32(..ab) -> ..ba             (TCG_BSWAP_IZ)
 *                bswap16_i32(xxab) -> ..ba             (TCG_BSWAP_OZ)
 *                bswap16_i32(xxab) -> ssba             (TCG_BSWAP_OS)
 *                bswap16_i32(xxab) -> xxba
 */
void tcg_gen_bswap16_i32(TCGv_i32 ret, TCGv_i32 arg, int flags)
{
    /* Only one extension flag may be present. */
    tcg_debug_assert(!(flags & TCG_BSWAP_OS) || !(flags & TCG_BSWAP_OZ));

    if (TCG_TARGET_HAS_bswap16_i32) {
        tcg_gen_op3i_i32(INDEX_op_bswap16_i32, ret, arg, flags);
    } else {
        TCGv_i32 t0 = tcg_temp_ebb_new_i32();
        TCGv_i32 t1 = tcg_temp_ebb_new_i32();

                                            /* arg = xxab (IZ=0) */
                                            /*       ..ab (IZ=1) */
        tcg_gen_shri_i32(t0, arg, 8);       /*  t0 = .xxa (IZ=0) */
                                            /*       ...a (IZ=1) */
        if (!(flags & TCG_BSWAP_IZ)) {
            tcg_gen_ext8u_i32(t0, t0);      /*  t0 = ...a */
        }

        if (flags & TCG_BSWAP_OS) {
            tcg_gen_shli_i32(t1, arg, 24);  /*  t1 = b... */
            tcg_gen_sari_i32(t1, t1, 16);   /*  t1 = ssb. */
        } else if (flags & TCG_BSWAP_OZ) {
            tcg_gen_ext8u_i32(t1, arg);     /*  t1 = ...b */
            tcg_gen_shli_i32(t1, t1, 8);    /*  t1 = ..b. */
        } else {
            tcg_gen_shli_i32(t1, arg, 8);   /*  t1 = xab. (IZ=0) */
                                            /*       .ab. (IZ=1) */
        }

        tcg_gen_or_i32(ret, t0, t1);        /* ret = ..ba (IZ=1 or OZ=1) */
                                            /*     = ssba (OS=1)         */
                                            /*     = xxba (no flag)      */
        tcg_temp_free_i32(t0);
        tcg_temp_free_i32(t1);
    }
}

---

#4 lore raw

Re: [PATCH 1/7] tcg/tcg-op: Document bswap16() byte pattern

Quoted 8 lines · Philippe Mathieu-Daudé

        } else {
            tcg_gen_shli_i32(t1, arg, 8);   /*  t1 = xab. (IZ=0) */ /*       .ab. (IZ=1) */ }

        tcg_gen_or_i32(ret, t0, t1);        /* ret = ..ba (IZ=1 or OZ=1) */ /*     = ssba (OS=1)         */ /*     = xxba (no flag)      */

Clearer with xaba for no flag?

Otherwise it looks ok.

r~

#5 lore raw

Re: [PATCH 1/7] tcg/tcg-op: Document bswap16() byte pattern

Quoted 11 lines · Richard Henderson
Quoted 8 lines · Philippe Mathieu-Daudé

         } else {
             tcg_gen_shli_i32(t1, arg, 8);   /*  t1 = xab. (IZ=0) */ /*       .ab. (IZ=1) */ }

         tcg_gen_or_i32(ret, t0, t1);        /* ret = ..ba (IZ=1 or OZ=1) */ /*     = ssba (OS=1)         */ /*     = xxba (no flag)      */

Clearer with xaba for no flag?

Right :)

Quoted 1 line

Otherwise it looks ok.

Thanks!

#6 lore raw

PATCH 2/7 tcg/tcg-op: Document bswap32() byte pattern

In reply to Philippe Mathieu-Daudé
---
 tcg/tcg-op.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
tcg/tcg-op.c +12 -0
index f164ddc95e..e6b0d74a46 100644
@@ -1061,6 +1061,11 @@ void tcg_gen_bswap16_i32(TCGv_i32 ret, TCGv_i32 arg, int flags)
     }
 }
 
+/*
+ * bswap32_i32: 32-bit byte swap on a 32-bit value.
+ *
+ * Byte pattern:  bswap32_i32(abcd) -> dcba
+ */
 void tcg_gen_bswap32_i32(TCGv_i32 ret, TCGv_i32 arg)
 {
     if (TCG_TARGET_HAS_bswap32_i32) {
@@ -1776,6 +1781,13 @@ void tcg_gen_bswap16_i64(TCGv_i64 ret, TCGv_i64 arg, int flags)
     }
 }
 
+/*
+ * bswap32_i64: 32-bit byte swap on the low bits of a 64-bit value.
+ *
+ * Byte pattern:  bswap32_i64(xxxxabcd) -> ....dcba     (TCG_BSWAP_OZ)
+ *                bswap32_i64(xxxxabcd) -> ssssdcba     (TCG_BSWAP_OS)
+ *                bswap32_i64(xxxxabcd) -> xxxxdcba
+ */
 void tcg_gen_bswap32_i64(TCGv_i64 ret, TCGv_i64 arg, int flags)
 {
     /* Only one extension flag may be present. */
Signature · 1 line
2.41.0
#7 lore raw

Re: [PATCH 2/7] tcg/tcg-op: Document bswap32() byte pattern

Quoted 10 lines · Philippe Mathieu-Daudé
---
  tcg/tcg-op.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
tcg/tcg-op.c +0 -0
index f164ddc95e..e6b0d74a46 100644

tcg/tcg-op.c void tcg_gen_bswap16_i32(TCGv_i32 ret, TCGv_i32 arg, int flags) · line 1068

Quoted 8 lines
tcg/tcg-op.c +5 -0
@@ -1061,6 +1061,11 @@ void tcg_gen_bswap16_i32(TCGv_i32 ret, TCGv_i32 arg, int flags)
      }
  }
 
+/*
+ * bswap32_i32: 32-bit byte swap on a 32-bit value.
+ *
+ * Byte pattern:  bswap32_i32(abcd) -> dcba
+ */

Ok.

Quoted 4 lines
tcg/tcg-op.c +0 -0
  void tcg_gen_bswap32_i32(TCGv_i32 ret, TCGv_i32 arg)
  {
      if (TCG_TARGET_HAS_bswap32_i32) {

tcg/tcg-op.c void tcg_gen_bswap16_i64(TCGv_i64 ret, TCGv_i64 arg, int flags) · line 1790

Quoted 10 lines
tcg/tcg-op.c +7 -0
@@ -1776,6 +1781,13 @@ void tcg_gen_bswap16_i64(TCGv_i64 ret, TCGv_i64 arg, int flags)
      }
  }
 
+/*
+ * bswap32_i64: 32-bit byte swap on the low bits of a 64-bit value.
+ *
+ * Byte pattern:  bswap32_i64(xxxxabcd) -> ....dcba     (TCG_BSWAP_OZ)
+ *                bswap32_i64(xxxxabcd) -> ssssdcba     (TCG_BSWAP_OS)
+ *                bswap32_i64(xxxxabcd) -> xxxxdcba
+ */

Again, TCG_BSWAP_IZ.

r~

#8 lore raw

Re: [PATCH 2/7] tcg/tcg-op: Document bswap32() byte pattern

Quoted 15 lines · Richard Henderson
Quoted 6 lines · Philippe Mathieu-Daudé

---
  tcg/tcg-op.c | 12 ++++++++++++

  1 file changed, 12 insertions(+)
Quoted 7 lines

+/*
+ * bswap32_i64: 32-bit byte swap on the low bits of a 64-bit value.
+ *
+ * Byte pattern:  bswap32_i64(xxxxabcd) -> ....dcba     (TCG_BSWAP_OZ)
+ *                bswap32_i64(xxxxabcd) -> ssssdcba     (TCG_BSWAP_OS)
+ *                bswap32_i64(xxxxabcd) -> xxxxdcba
+ */

Quoted 1 line

Again, TCG_BSWAP_IZ.

Whether TCG_BSWAP_IZ is set or not doesn't change the result, at the end we have:

bswap32_i64(xxxxabcd) -> ....dcba (TCG_BSWAP_OS not set)
bswap32_i64(xxxxabcd) -> ssssdcba (TCG_BSWAP_OS set)

#9 lore raw

Re: [PATCH 2/7] tcg/tcg-op: Document bswap32() byte pattern

Quoted 16 lines · Philippe Mathieu-Daudé
Quoted 15 lines · Richard Henderson
Quoted 13 lines · Philippe Mathieu-Daudé

---
  tcg/tcg-op.c | 12 ++++++++++++

  1 file changed, 12 insertions(+)

+/*
+ * bswap32_i64: 32-bit byte swap on the low bits of a 64-bit value.
+ *
+ * Byte pattern:  bswap32_i64(xxxxabcd) -> ....dcba     (TCG_BSWAP_OZ)
+ *                bswap32_i64(xxxxabcd) -> ssssdcba     (TCG_BSWAP_OS)
+ *                bswap32_i64(xxxxabcd) -> xxxxdcba
+ */

Quoted 7 lines
Quoted 1 line

Again, TCG_BSWAP_IZ.

Whether TCG_BSWAP_IZ is set or not doesn't change the result, at the end we have:

  bswap32_i64(xxxxabcd) -> ....dcba     (TCG_BSWAP_OS not set) bswap32_i64(xxxxabcd) -> ssssdcba     (TCG_BSWAP_OS set)

It changes the input: not 'x' but '.'.

r~

#10 lore raw

PATCH 3/7 tcg/tcg-op: Document bswap64() byte pattern

In reply to Philippe Mathieu-Daudé
---
 tcg/tcg-op.c | 5 +++++
 1 file changed, 5 insertions(+)
tcg/tcg-op.c +5 -0
index e6b0d74a46..f4fe13e040 100644
@@ -1828,6 +1828,11 @@ void tcg_gen_bswap32_i64(TCGv_i64 ret, TCGv_i64 arg, int flags)
     }
 }
 
+/*
+ * bswap64_i64: 64-bit byte swap on a 64-bit value.
+ *
+ * Byte pattern:  bswap64_i64(abcdefgh) -> hgfedcba
+ */
 void tcg_gen_bswap64_i64(TCGv_i64 ret, TCGv_i64 arg)
 {
     if (TCG_TARGET_REG_BITS == 32) {
Signature · 1 line
2.41.0
Reviewed-by on v1 3/7
#11 lore raw

Re: [PATCH 3/7] tcg/tcg-op: Document bswap64() byte pattern

Quoted 11 lines · Philippe Mathieu-Daudé
---
  tcg/tcg-op.c | 5 +++++
  1 file changed, 5 insertions(+)
tcg/tcg-op.c +0 -0
index e6b0d74a46..f4fe13e040 100644
@@ -1828,6 +1828,11 @@ void tcg_gen_bswap32_i64(TCGv_i64 ret, TCGv_i64 arg, int flags)
      }

tcg/tcg-op.c void tcg_gen_bswap32_i64(TCGv_i64 ret, TCGv_i64 arg, int flags) · line 1838

Quoted 10 lines
tcg/tcg-op.c +5 -0
  }
 
+/*
+ * bswap64_i64: 64-bit byte swap on a 64-bit value.
+ *
+ * Byte pattern:  bswap64_i64(abcdefgh) -> hgfedcba
+ */
  void tcg_gen_bswap64_i64(TCGv_i64 ret, TCGv_i64 arg)
  {
      if (TCG_TARGET_REG_BITS == 32) {

r~

#12 lore raw

PATCH 4/7 tcg/tcg-op: Document hswap() byte pattern

In reply to Philippe Mathieu-Daudé

Document hswap_i32() and hswap_i64(), added in commit 46be8425ff ("tcg: Implement tcg_gen_{h,w}swap_{i32,i64}").

---
 docs/devel/tcg-ops.rst |  4 ++++
 tcg/tcg-op.c           | 26 +++++++++++++++++++-------
 2 files changed, 23 insertions(+), 7 deletions(-)
docs/devel/tcg-ops.rst +4 -0
index 6a166c5665..d9364effd2 100644
@@ -486,6 +486,10 @@ Misc
          into 32-bit output *t0*.  Depending on the host, this may be a simple shift,
          or may require additional canonicalization.
 
+   * - hswap_i32/i64 *t0*, *t1*
+
+     - | Swap 16-bit halfwords within a 32/64-bit value.
+
 
 Conditional moves
 -----------------
tcg/tcg-op.c +19 -7
index f4fe13e040..bb64326a1a 100644
@@ -1091,6 +1091,11 @@ void tcg_gen_bswap32_i32(TCGv_i32 ret, TCGv_i32 arg)
     }
 }
 
+/*
+ * hswap_i32: Swap 16-bit halfwords within a 32-bit value.
+ *
+ * Byte pattern:  hswap_i32(abcd) -> cdab
+ */
 void tcg_gen_hswap_i32(TCGv_i32 ret, TCGv_i32 arg)
 {
     /* Swapping 2 16-bit elements is a rotate. */
@@ -1878,19 +1883,26 @@ void tcg_gen_bswap64_i64(TCGv_i64 ret, TCGv_i64 arg)
     }
 }
 
+/*
+ * hswap_i64: Swap 16-bit halfwords within a 64-bit value.
+ *
+ * See hswap64() in include/qemu/bitops.h
+ *
+ * Byte pattern:  hswap_i64(abcdefgh) -> ghefcdab
+ */
 void tcg_gen_hswap_i64(TCGv_i64 ret, TCGv_i64 arg)
 {
     uint64_t m = 0x0000ffff0000ffffull;
     TCGv_i64 t0 = tcg_temp_ebb_new_i64();
     TCGv_i64 t1 = tcg_temp_ebb_new_i64();
 
-    /* See include/qemu/bitops.h, hswap64. */
-    tcg_gen_rotli_i64(t1, arg, 32);
-    tcg_gen_andi_i64(t0, t1, m);
-    tcg_gen_shli_i64(t0, t0, 16);
-    tcg_gen_shri_i64(t1, t1, 16);
-    tcg_gen_andi_i64(t1, t1, m);
-    tcg_gen_or_i64(ret, t0, t1);
+                                        /* arg = abcdefgh */
+    tcg_gen_rotli_i64(t1, arg, 32);     /*  t1 = efghabcd */
+    tcg_gen_andi_i64(t0, t1, m);        /*  t0 = ..gh..cd */
+    tcg_gen_shli_i64(t0, t0, 16);       /*  t0 = gh..cd.. */
+    tcg_gen_shri_i64(t1, t1, 16);       /*  t1 = ..efghab */
+    tcg_gen_andi_i64(t1, t1, m);        /*  t1 = ..ef..ab */
+    tcg_gen_or_i64(ret, t0, t1);        /* ret = ghefcdab */
 
     tcg_temp_free_i64(t0);
     tcg_temp_free_i64(t1);
Signature · 1 line
2.41.0
Reviewed-by on v1 4/7
#13 lore raw

Re: [PATCH 4/7] tcg/tcg-op: Document hswap() byte pattern

Quoted 8 lines · Philippe Mathieu-Daudé

Document hswap_i32() and hswap_i64(), added in commit 46be8425ff ("tcg: Implement tcg_gen_{h,w}swap_{i32,i64}").

---
  docs/devel/tcg-ops.rst |  4 ++++
  tcg/tcg-op.c           | 26 +++++++++++++++++++-------
  2 files changed, 23 insertions(+), 7 deletions(-)

r~

#14 lore raw

Re: [PATCH 4/7] tcg/tcg-op: Document hswap() byte pattern

In reply to Philippe Mathieu-Daudé
Quoted 14 lines · Philippe Mathieu-Daudé

Document hswap_i32() and hswap_i64(), added in commit 46be8425ff ("tcg: Implement tcg_gen_{h,w}swap_{i32,i64}").

---
  docs/devel/tcg-ops.rst |  4 ++++
  tcg/tcg-op.c           | 26 +++++++++++++++++++-------
  2 files changed, 23 insertions(+), 7 deletions(-)
docs/devel/tcg-ops.rst +0 -0
index 6a166c5665..d9364effd2 100644

docs/devel/tcg-ops.rst Misc · line 491

Quoted 6 lines
docs/devel/tcg-ops.rst +3 -0
@@ -486,6 +486,10 @@ Misc
           into 32-bit output *t0*.  Depending on the host, this may be a simple shift,
           or may require additional canonicalization.
 
+   * - hswap_i32/i64 *t0*, *t1*
+
+     - | Swap 16-bit halfwords within a 32/64-bit value.

hswap is not a tcg opcode, so this is incorrect.

This falls into the part of TCG that Peter has mentioned many times: we have opcode documentation, but no separate translator front end documentation.

r~

#15 lore raw

PATCH 5/7 tcg/tcg-op: Document wswap() byte pattern

In reply to Philippe Mathieu-Daudé

Document wswap_i64(), added in commit 46be8425ff
("tcg: Implement tcg_gen_{h,w}swap_{i32,i64}").

---
 docs/devel/tcg-ops.rst | 4 ++++
 tcg/tcg-op.c           | 5 +++++
 2 files changed, 9 insertions(+)
docs/devel/tcg-ops.rst +4 -0
index d9364effd2..306025ece7 100644
@@ -490,6 +490,10 @@ Misc
 
      - | Swap 16-bit halfwords within a 32/64-bit value.
 
+   * - wswap_i64 *t0*, *t1*
+
+     - | Swap 32-bit words within a 64-bit value.
+
 
 Conditional moves
 -----------------
tcg/tcg-op.c +5 -0
index bb64326a1a..c436c5e263 100644
@@ -1908,6 +1908,11 @@ void tcg_gen_hswap_i64(TCGv_i64 ret, TCGv_i64 arg)
     tcg_temp_free_i64(t1);
 }
 
+/*
+ * wswap_i64: Swap 32-bit words within a 64-bit value.
+ *
+ * Byte pattern:  wswap_i64(abcdefgh) -> efghabcd
+ */
 void tcg_gen_wswap_i64(TCGv_i64 ret, TCGv_i64 arg)
 {
     /* Swapping 2 32-bit elements is a rotate. */
Signature · 1 line
2.41.0
#17 lore raw

PATCH 6/7 tcg/tcg-op: Document deposit_z()

In reply to Philippe Mathieu-Daudé

Document deposit_z_i32() and deposit_z_i64(), added in commit 07cc68d528 ("tcg: Add deposit_z expander").

---
 docs/devel/tcg-ops.rst | 4 ++++
 1 file changed, 4 insertions(+)
docs/devel/tcg-ops.rst +4 -0
index 306025ece7..7ea6aba502 100644
@@ -449,6 +449,10 @@ Misc
        |
        |     *dest* = (*t1* & ~0x0f00) | ((*t2* << 8) & 0x0f00)
 
+   * - deposit_z_i32/i64 *dest*, *t1*, *pos*, *len*
+
+     - | Similar to deposit, except that a zero value is deposited.
+
    * - extract_i32/i64 *dest*, *t1*, *pos*, *len*
 
        sextract_i32/i64 *dest*, *t1*, *pos*, *len*
Signature · 1 line
2.41.0
#18 lore raw

Re: [PATCH 6/7] tcg/tcg-op: Document deposit_z()

Quoted 13 lines · Philippe Mathieu-Daudé

Document deposit_z_i32() and deposit_z_i64(), added in commit 07cc68d528 ("tcg: Add deposit_z expander").

---
  docs/devel/tcg-ops.rst | 4 ++++
  1 file changed, 4 insertions(+)
docs/devel/tcg-ops.rst +0 -0
index 306025ece7..7ea6aba502 100644

docs/devel/tcg-ops.rst Misc · line 454

Quoted 6 lines
docs/devel/tcg-ops.rst +3 -0
@@ -449,6 +449,10 @@ Misc
         |
         |     *dest* = (*t1* & ~0x0f00) | ((*t2* << 8) & 0x0f00)
 
+   * - deposit_z_i32/i64 *dest*, *t1*, *pos*, *len*
+
+     - | Similar to deposit, except that a zero value is deposited.

Nack -- deposit_z is not a tcg opcode.

r~

#19 lore raw

PATCH 7/7 target/cris: Fix a typo in gen_swapr()

In reply to Philippe Mathieu-Daudé
---
 target/cris/translate.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
target/cris/translate.c +11 -9
index 0b3d724281..42103b5558 100644
@@ -411,15 +411,17 @@ static inline void t_gen_swapw(TCGv d, TCGv s)
     tcg_gen_or_tl(d, d, t);
 }
 
-/* Reverse the within each byte.
-   T0 = (((T0 << 7) & 0x80808080) |
-   ((T0 << 5) & 0x40404040) |
-   ((T0 << 3) & 0x20202020) |
-   ((T0 << 1) & 0x10101010) |
-   ((T0 >> 1) & 0x08080808) |
-   ((T0 >> 3) & 0x04040404) |
-   ((T0 >> 5) & 0x02020202) |
-   ((T0 >> 7) & 0x01010101));
+/*
+ * Reverse the bits within each byte.
+ *
+ *  T0 = ((T0 << 7) & 0x80808080)
+ *     | ((T0 << 5) & 0x40404040)
+ *     | ((T0 << 3) & 0x20202020)
+ *     | ((T0 << 1) & 0x10101010)
+ *     | ((T0 >> 1) & 0x08080808)
+ *     | ((T0 >> 3) & 0x04040404)
+ *     | ((T0 >> 5) & 0x02020202)
+ *     | ((T0 >> 7) & 0x01010101);
  */
 static void t_gen_swapr(TCGv d, TCGv s)
 {
Signature · 1 line
2.41.0
#20 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 6 lines · Philippe Mathieu-Daudé

While reviewing a recent patch from Richard optimizing deposit() [*] I ended looking at the *swap friends, taking some notes, which then evolved to proper documentation.

[*]
https://lore.kernel.org/qemu-devel/[email protected]/

We already have some documentation in tcg.rst:

* - bswap16_i32/i64 *t0*, *t1*, *flags*

     - | 16 bit byte swap on the low bits of a 32/64 bit input.
       |
       | If *flags* & ``TCG_BSWAP_IZ``, then *t1* is known to be zero-extended from bit 15.
       | If *flags* & ``TCG_BSWAP_OZ``, then *t0* will be zero-extended from bit 15.
       | If *flags* & ``TCG_BSWAP_OS``, then *t0* will be sign-extended from bit 15.
       |
       | If neither ``TCG_BSWAP_OZ`` nor ``TCG_BSWAP_OS`` are set, then the bits of *t0* above bit 15 may contain any value.

* - bswap32_i64 *t0*, *t1*, *flags*

- | 32 bit byte swap on a 64-bit value. The flags are the same as for bswap16, except they apply from bit 31 instead of bit 15.

* - bswap32_i32 *t0*, *t1*, *flags*

bswap64_i64 *t0*, *t1*, *flags*

- | 32/64 bit byte swap. The flags are ignored, but still present for consistency with the other bswap opcodes.

In an ideal world we could generate kdoc from the source file and include it in the rest of the tcg docs. I'm not sure if it worth the churn though? Richard?

https://qemu.readthedocs.io/en/master/devel/tcg-ops.html

Quoted 14 lines

Philippe Mathieu-Daudé (7):
tcg/tcg-op: Document bswap16() byte pattern
tcg/tcg-op: Document bswap32() byte pattern
tcg/tcg-op: Document bswap64() byte pattern
tcg/tcg-op: Document hswap() byte pattern
tcg/tcg-op: Document wswap() byte pattern
tcg/tcg-op: Document deposit_z()
target/cris: Fix a typo in gen_swapr()

 docs/devel/tcg-ops.rst  | 12 ++++++
 target/cris/translate.c | 20 +++++----
 tcg/tcg-op.c            | 96 +++++++++++++++++++++++++++++++----------
 3 files changed, 96 insertions(+), 32 deletions(-)
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#21 lore raw
Quoted 30 lines · Alex Bennée
Quoted 6 lines · Philippe Mathieu-Daudé

While reviewing a recent patch from Richard optimizing deposit() [*] I ended looking at the *swap friends, taking some notes, which then evolved to proper documentation.

[*]
https://lore.kernel.org/qemu-devel/[email protected]/

We already have some documentation in tcg.rst:

* - bswap16_i32/i64 *t0*, *t1*, *flags*

     - | 16 bit byte swap on the low bits of a 32/64 bit input.
       |
       | If *flags* & ``TCG_BSWAP_IZ``, then *t1* is known to be zero-extended from bit 15.
       | If *flags* & ``TCG_BSWAP_OZ``, then *t0* will be zero-extended from bit 15.
       | If *flags* & ``TCG_BSWAP_OS``, then *t0* will be sign-extended from bit 15.
       |
       | If neither ``TCG_BSWAP_OZ`` nor ``TCG_BSWAP_OS`` are set, then the bits of *t0* above bit 15 may contain any value.

* - bswap32_i64 *t0*, *t1*, *flags*

- | 32 bit byte swap on a 64-bit value. The flags are the same as for bswap16, except they apply from bit 31 instead of bit 15.

* - bswap32_i32 *t0*, *t1*, *flags*

Quoted 8 lines

bswap64_i64 *t0*, *t1*, *flags*

- | 32/64 bit byte swap. The flags are ignored, but still present for consistency with the other bswap opcodes.

In an ideal world we could generate kdoc from the source file and include it in the rest of the tcg docs. I'm not sure if it worth the churn though? Richard?

I do think it would be useful to have documentation of the set of APIs you use as a writer of a TCG frontend. This is often not exactly the same as the TCG IR opcodes. (Similarly what you have to do as a backend isn't exactly the same, but the documentation need is less pressing because fewer people need to work on the backends.)

thanks
-- PMM

#22 lore raw
In reply to Alex Bennée
Quoted 27 lines · Alex Bennée
Quoted 6 lines · Philippe Mathieu-Daudé

While reviewing a recent patch from Richard optimizing deposit() [*] I ended looking at the *swap friends, taking some notes, which then evolved to proper documentation.

[*]
https://lore.kernel.org/qemu-devel/[email protected]/

We already have some documentation in tcg.rst:

* - bswap16_i32/i64 *t0*, *t1*, *flags*

- | 16 bit byte swap on the low bits of a 32/64 bit input.
|
| If *flags* & ``TCG_BSWAP_IZ``, then *t1* is known to be zero-extended from bit 15.
| If *flags* & ``TCG_BSWAP_OZ``, then *t0* will be zero-extended from bit 15.
| If *flags* & ``TCG_BSWAP_OS``, then *t0* will be sign-extended from bit 15.
|
| If neither ``TCG_BSWAP_OZ`` nor ``TCG_BSWAP_OS`` are set, then the bits of *t0* above bit 15 may contain any value.

* - bswap32_i64 *t0*, *t1*, *flags*

- | 32 bit byte swap on a 64-bit value. The flags are the same as for bswap16, except they apply from bit 31 instead of bit 15.

Quoted 6 lines

* - bswap32_i32 *t0*, *t1*, *flags*

bswap64_i64 *t0*, *t1*, *flags*

- | 32/64 bit byte swap. The flags are ignored, but still present for consistency with the other bswap opcodes.

I guess I wasn't clear enough: I mostly documented the implementation, not the API.

That said, maybe the bytes movement pattern belong to the API doc (at least I find it very useful to find which TCG opcode implement the frontend code, when the TCG opcode name isn't trivial, although documented).