Skip to contentSkip to conversation

Fix host-utils sat; Add GVec overrides for HVX ops

PATCH Superseded Brian Cain 3 participants 11 messages Aug 6 – Aug 8

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Brian Cain

author
#0 lore raw

v2 cover Fix host-utils sat; Add GVec overrides for HVX ops

I was going to just send the host-utils change on its own but figured it might be useful to have the motivating commits that encountered the issue, so I've included those here too.

In v2:

* Updated commit message to include suggested Fixes/Cc
* Fixed comments per review suggestion

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

Brian Cain (6):
host-utils: fix ssub32/64_saturate return type and clamp direction target/hexagon: add GVec overrides for HVX saturating add/sub target/hexagon: add GVec override for HVX vmpyih multiply target/hexagon: add GVec overrides for HVX absolute difference target/hexagon: add GVec overrides for HVX vector average tests/tcg/hexagon: add HVX test for V6_vsubwsat saturation

 include/qemu/host-utils.h    |  24 ++---
 target/hexagon/gen_tcg_hvx.h | 183 +++++++++++++++++++++++++++++++++++
 tests/tcg/hexagon/hvx_misc.c |  44 +++++++++
 3 files changed, 239 insertions(+), 12 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/6 host-utils: fix ssub32/64_saturate return type and clamp direction

ssub32_saturate() and ssub64_saturate() were declared to return bool instead of int32_t/int64_t, and clamped to the wrong bound on overflow.

Fixes 16495533131 ("host-utils: Introduce signed saturation primitives")
---
 include/qemu/host-utils.h | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
include/qemu/host-utils.h +12 -12
index 2e8da7fb3d0..1db9dbb138e 100644
@@ -607,10 +607,10 @@ static inline bool umul64_overflow(uint64_t x, uint64_t y, uint64_t *ret)
 }
 
 /**
- * sadd32_saturate - addition with saturation
+ * sadd32_saturate - 32-bit signed addition with saturation
  * @x, @y: addends
  *
- * Computes @x + @y, and saturates rathern than truncating the result.
+ * Computes @x + @y, and saturates rather than truncating the result.
  */
 static inline int32_t sadd32_saturate(int32_t x, int32_t y)
 {
@@ -622,10 +622,10 @@ static inline int32_t sadd32_saturate(int32_t x, int32_t y)
 }
 
 /**
- * sadd64_saturate - addition with saturation
+ * sadd64_saturate - 64-bit signed addition with saturation
  * @x, @y: addends
  *
- * Computes @x + @y, and saturates rathern than truncating the result.
+ * Computes @x + @y, and saturates rather than truncating the result.
  */
 static inline int64_t sadd64_saturate(int64_t x, int64_t y)
 {
@@ -637,31 +637,31 @@ static inline int64_t sadd64_saturate(int64_t x, int64_t y)
 }
 
 /**
- * ssub32_saturate - subtraction with saturation
+ * ssub32_saturate - 32-bit signed subtraction with saturation
  * @x, @y: addends
  *
- * Computes @x + @y, and saturates rathern than truncating the result.
+ * Computes @x - @y, and saturates rather than truncating the result.
  */
-static inline bool ssub32_saturate(int32_t x, int32_t y)
+static inline int32_t ssub32_saturate(int32_t x, int32_t y)
 {
     int32_t ret;
     if (ssub32_overflow(x, y, &ret)) {
-        ret = x < 0 ? INT32_MAX : INT32_MIN;
+        ret = x < 0 ? INT32_MIN : INT32_MAX;
     }
     return ret;
 }
 
 /**
- * ssub64_saturate - subtraction with saturation
+ * ssub64_saturate - 64-bit signed subtraction with saturation
  * @x, @y: addends
  *
- * Computes @x + @y, and saturates rathern than truncating the result.
+ * Computes @x - @y, and saturates rather than truncating the result.
  */
-static inline bool ssub64_saturate(int64_t x, int64_t y)
+static inline int64_t ssub64_saturate(int64_t x, int64_t y)
 {
     int64_t ret;
     if (ssub64_overflow(x, y, &ret)) {
-        ret = x < 0 ? INT64_MAX : INT64_MIN;
+        ret = x < 0 ? INT64_MIN : INT64_MAX;
     }
     return ret;
 }
Signature · 1 line
2.34.1
author
#2 lore raw

Re: [PATCH v2 1/6] host-utils: fix ssub32/64_saturate return type and clamp direction

Quoted 9 lines · Brian Cain

ssub32_saturate() and ssub64_saturate() were declared to return bool instead of int32_t/int64_t, and clamped to the wrong bound on overflow.

Fixes 16495533131 ("host-utils: Introduce signed saturation primitives")

---

Michael,

    I think this patch is a good candidate fix for -stable.

-Brian

Quoted 73 lines
  include/qemu/host-utils.h | 24 ++++++++++++------------
  1 file changed, 12 insertions(+), 12 deletions(-)
include/qemu/host-utils.h +12 -12
index 2e8da7fb3d0..1db9dbb138e 100644
@@ -607,10 +607,10 @@ static inline bool umul64_overflow(uint64_t x, uint64_t y, uint64_t *ret)
  }
 
  /**
- * sadd32_saturate - addition with saturation
+ * sadd32_saturate - 32-bit signed addition with saturation
   * @x, @y: addends
   *
- * Computes @x + @y, and saturates rathern than truncating the result.
+ * Computes @x + @y, and saturates rather than truncating the result.
   */
  static inline int32_t sadd32_saturate(int32_t x, int32_t y)
  {
@@ -622,10 +622,10 @@ static inline int32_t sadd32_saturate(int32_t x, int32_t y)
  }
 
  /**
- * sadd64_saturate - addition with saturation
+ * sadd64_saturate - 64-bit signed addition with saturation
   * @x, @y: addends
   *
- * Computes @x + @y, and saturates rathern than truncating the result.
+ * Computes @x + @y, and saturates rather than truncating the result.
   */
  static inline int64_t sadd64_saturate(int64_t x, int64_t y)
  {
@@ -637,31 +637,31 @@ static inline int64_t sadd64_saturate(int64_t x, int64_t y)
  }
 
  /**
- * ssub32_saturate - subtraction with saturation
+ * ssub32_saturate - 32-bit signed subtraction with saturation
   * @x, @y: addends
   *
- * Computes @x + @y, and saturates rathern than truncating the result.
+ * Computes @x - @y, and saturates rather than truncating the result.
   */
-static inline bool ssub32_saturate(int32_t x, int32_t y)
+static inline int32_t ssub32_saturate(int32_t x, int32_t y)
  {
      int32_t ret;
      if (ssub32_overflow(x, y, &ret)) {
-        ret = x < 0 ? INT32_MAX : INT32_MIN;
+        ret = x < 0 ? INT32_MIN : INT32_MAX;
      }
      return ret;
  }
 
  /**
- * ssub64_saturate - subtraction with saturation
+ * ssub64_saturate - 64-bit signed subtraction with saturation
   * @x, @y: addends
   *
- * Computes @x + @y, and saturates rathern than truncating the result.
+ * Computes @x - @y, and saturates rather than truncating the result.
   */
-static inline bool ssub64_saturate(int64_t x, int64_t y)
+static inline int64_t ssub64_saturate(int64_t x, int64_t y)
  {
      int64_t ret;
      if (ssub64_overflow(x, y, &ret)) {
-        ret = x < 0 ? INT64_MAX : INT64_MIN;
+        ret = x < 0 ? INT64_MIN : INT64_MAX;
      }
      return ret;
  }
#3 lore raw

Re: [PATCH v2 1/6] host-utils: fix ssub32/64_saturate return type and clamp direction

Quoted 12 lines · Brian Cain
Quoted 9 lines · Brian Cain

ssub32_saturate() and ssub64_saturate() were declared to return bool instead of int32_t/int64_t, and clamped to the wrong bound on overflow.

Fixes 16495533131 ("host-utils: Introduce signed saturation primitives")

---

Quoted 3 lines

Michael,

    I think this patch is a good candidate fix for -stable.

Yeah, I noticed the Cc: qemu-stable@ tag in this patch. Thank you!

/mjt

author
#4 lore raw

PATCH v2 2/6 target/hexagon: add GVec overrides for HVX saturating add/sub

In reply to Brian Cain
---
 target/hexagon/gen_tcg_hvx.h | 93 ++++++++++++++++++++++++++++++++++++
 1 file changed, 93 insertions(+)
target/hexagon/gen_tcg_hvx.h +93 -0
index 0da64d467e6..43a0f358f00 100644
@@ -234,6 +234,99 @@ static inline void assert_vhist_tmp(DisasContext *ctx)
     tcg_gen_gvec_sub(MO_32, VddV_off, VuuV_off, VvvV_off, \
                      sizeof(MMVector) * 2, sizeof(MMVector) * 2)
 
+#define fGEN_TCG_V6_vaddbsat(SHORTCODE) \
+    tcg_gen_gvec_ssadd(MO_8, VdV_off, VuV_off, VvV_off, \
+                       VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
+
+#define fGEN_TCG_V6_vaddhsat(SHORTCODE) \
+    tcg_gen_gvec_ssadd(MO_16, VdV_off, VuV_off, VvV_off, \
+                       VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
+
+#define fGEN_TCG_V6_vaddwsat(SHORTCODE) \
+    tcg_gen_gvec_ssadd(MO_32, VdV_off, VuV_off, VvV_off, \
+                       VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
+
+#define fGEN_TCG_V6_vaddubsat(SHORTCODE) \
+    tcg_gen_gvec_usadd(MO_8, VdV_off, VuV_off, VvV_off, \
+                       VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
+
+#define fGEN_TCG_V6_vadduhsat(SHORTCODE) \
+    tcg_gen_gvec_usadd(MO_16, VdV_off, VuV_off, VvV_off, \
+                       VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
+
+#define fGEN_TCG_V6_vadduwsat(SHORTCODE) \
+    tcg_gen_gvec_usadd(MO_32, VdV_off, VuV_off, VvV_off, \
+                       VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
+
+#define fGEN_TCG_PAIR_ADDSUB(OP, VECE, DST, SRC_A, SRC_B) \
+    do { \
+        tcg_gen_gvec_##OP(VECE, DST, SRC_A, SRC_B, \
+                         VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE); \
+        tcg_gen_gvec_##OP(VECE, DST + sizeof(MMVector), \
+                         SRC_A + sizeof(MMVector), \
+                         SRC_B + sizeof(MMVector), \
+                         VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE); \
+    } while (0)
+
+#define fGEN_TCG_V6_vaddbsat_dv(SHORTCODE) \
+    fGEN_TCG_PAIR_ADDSUB(ssadd, MO_8, VddV_off, VuuV_off, VvvV_off)
+
+#define fGEN_TCG_V6_vaddhsat_dv(SHORTCODE) \
+    fGEN_TCG_PAIR_ADDSUB(ssadd, MO_16, VddV_off, VuuV_off, VvvV_off)
+
+#define fGEN_TCG_V6_vaddwsat_dv(SHORTCODE) \
+    fGEN_TCG_PAIR_ADDSUB(ssadd, MO_32, VddV_off, VuuV_off, VvvV_off)
+
+#define fGEN_TCG_V6_vaddubsat_dv(SHORTCODE) \
+    fGEN_TCG_PAIR_ADDSUB(usadd, MO_8, VddV_off, VuuV_off, VvvV_off)
+
+#define fGEN_TCG_V6_vadduhsat_dv(SHORTCODE) \
+    fGEN_TCG_PAIR_ADDSUB(usadd, MO_16, VddV_off, VuuV_off, VvvV_off)
+
+#define fGEN_TCG_V6_vadduwsat_dv(SHORTCODE) \
+    fGEN_TCG_PAIR_ADDSUB(usadd, MO_32, VddV_off, VuuV_off, VvvV_off)
+
+#define fGEN_TCG_V6_vsubbsat(SHORTCODE) \
+    tcg_gen_gvec_sssub(MO_8, VdV_off, VuV_off, VvV_off, \
+                       VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
+
+#define fGEN_TCG_V6_vsubhsat(SHORTCODE) \
+    tcg_gen_gvec_sssub(MO_16, VdV_off, VuV_off, VvV_off, \
+                       VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
+
+#define fGEN_TCG_V6_vsubwsat(SHORTCODE) \
+    tcg_gen_gvec_sssub(MO_32, VdV_off, VuV_off, VvV_off, \
+                       VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
+
+#define fGEN_TCG_V6_vsububsat(SHORTCODE) \
+    tcg_gen_gvec_ussub(MO_8, VdV_off, VuV_off, VvV_off, \
+                       VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
+
+#define fGEN_TCG_V6_vsubuhsat(SHORTCODE) \
+    tcg_gen_gvec_ussub(MO_16, VdV_off, VuV_off, VvV_off, \
+                       VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
+
+#define fGEN_TCG_V6_vsubuwsat(SHORTCODE) \
+    tcg_gen_gvec_ussub(MO_32, VdV_off, VuV_off, VvV_off, \
+                       VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
+
+#define fGEN_TCG_V6_vsubbsat_dv(SHORTCODE) \
+    fGEN_TCG_PAIR_ADDSUB(sssub, MO_8, VddV_off, VuuV_off, VvvV_off)
+
+#define fGEN_TCG_V6_vsubhsat_dv(SHORTCODE) \
+    fGEN_TCG_PAIR_ADDSUB(sssub, MO_16, VddV_off, VuuV_off, VvvV_off)
+
+#define fGEN_TCG_V6_vsubwsat_dv(SHORTCODE) \
+    fGEN_TCG_PAIR_ADDSUB(sssub, MO_32, VddV_off, VuuV_off, VvvV_off)
+
+#define fGEN_TCG_V6_vsububsat_dv(SHORTCODE) \
+    fGEN_TCG_PAIR_ADDSUB(ussub, MO_8, VddV_off, VuuV_off, VvvV_off)
+
+#define fGEN_TCG_V6_vsubuhsat_dv(SHORTCODE) \
+    fGEN_TCG_PAIR_ADDSUB(ussub, MO_16, VddV_off, VuuV_off, VvvV_off)
+
+#define fGEN_TCG_V6_vsubuwsat_dv(SHORTCODE) \
+    fGEN_TCG_PAIR_ADDSUB(ussub, MO_32, VddV_off, VuuV_off, VvvV_off)
 /* Vector shift right - various forms */
 #define fGEN_TCG_V6_vasrh(SHORTCODE) \
     do { \
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH v2 2/6] target/hexagon: add GVec overrides for HVX saturating add/sub

target/hexagon/gen_tcg_hvx.h static inline void assert_vhist_tmp(DisasContext *ctx) · line 269

Quoted 9 lines · Brian Cain
target/hexagon/gen_tcg_hvx.h +9 -0
+#define fGEN_TCG_PAIR_ADDSUB(OP, VECE, DST, SRC_A, SRC_B) \
+    do { \
+        tcg_gen_gvec_##OP(VECE, DST, SRC_A, SRC_B, \
+                         VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE); \
+        tcg_gen_gvec_##OP(VECE, DST + sizeof(MMVector), \
+                         SRC_A + sizeof(MMVector), \
+                         SRC_B + sizeof(MMVector), \
+                         VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE); \
+    } while (0)

Is this on a MMVectorPair structure? If so, you can implement this with one operation of size 2 * VECTOR_SIZE_BYTE instead of two separate operations.

r~

author
#6 lore raw

PATCH v2 3/6 target/hexagon: add GVec override for HVX vmpyih multiply

In reply to Brian Cain
---
 target/hexagon/gen_tcg_hvx.h | 5 +++++
 1 file changed, 5 insertions(+)
target/hexagon/gen_tcg_hvx.h +5 -0
index 43a0f358f00..f843fe4e2e1 100644
@@ -327,6 +327,11 @@ static inline void assert_vhist_tmp(DisasContext *ctx)
 
 #define fGEN_TCG_V6_vsubuwsat_dv(SHORTCODE) \
     fGEN_TCG_PAIR_ADDSUB(ussub, MO_32, VddV_off, VuuV_off, VvvV_off)
+
+#define fGEN_TCG_V6_vmpyih(SHORTCODE) \
+    tcg_gen_gvec_mul(MO_16, VdV_off, VuV_off, VvV_off, \
+                     VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
+
 /* Vector shift right - various forms */
 #define fGEN_TCG_V6_vasrh(SHORTCODE) \
     do { \
Signature · 1 line
2.34.1
author
#7 lore raw

PATCH v2 4/6 target/hexagon: add GVec overrides for HVX absolute difference

In reply to Brian Cain
---
 target/hexagon/gen_tcg_hvx.h | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
target/hexagon/gen_tcg_hvx.h +23 -0
index f843fe4e2e1..3cb52c761ae 100644
@@ -332,6 +332,29 @@ static inline void assert_vhist_tmp(DisasContext *ctx)
     tcg_gen_gvec_mul(MO_16, VdV_off, VuV_off, VvV_off, \
                      VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
 
+#define fGEN_TCG_VEC_ABSDIFF(VECE, MAX_FN, MIN_FN) \
+    do { \
+        intptr_t tmpoff = offsetof(CPUHexagonState, vtmp); \
+        MAX_FN(VECE, tmpoff, VuV_off, VvV_off, \
+               VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE); \
+        MIN_FN(VECE, VdV_off, VuV_off, VvV_off, \
+               VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE); \
+        tcg_gen_gvec_sub(VECE, VdV_off, tmpoff, VdV_off, \
+                         VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE); \
+    } while (0)
+
+#define fGEN_TCG_V6_vabsdiffub(SHORTCODE) \
+    fGEN_TCG_VEC_ABSDIFF(MO_8, tcg_gen_gvec_umax, tcg_gen_gvec_umin)
+
+#define fGEN_TCG_V6_vabsdiffuh(SHORTCODE) \
+    fGEN_TCG_VEC_ABSDIFF(MO_16, tcg_gen_gvec_umax, tcg_gen_gvec_umin)
+
+#define fGEN_TCG_V6_vabsdiffh(SHORTCODE) \
+    fGEN_TCG_VEC_ABSDIFF(MO_16, tcg_gen_gvec_smax, tcg_gen_gvec_smin)
+
+#define fGEN_TCG_V6_vabsdiffw(SHORTCODE) \
+    fGEN_TCG_VEC_ABSDIFF(MO_32, tcg_gen_gvec_smax, tcg_gen_gvec_smin)
+
 /* Vector shift right - various forms */
 #define fGEN_TCG_V6_vasrh(SHORTCODE) \
     do { \
Signature · 1 line
2.34.1
#8 lore raw

Re: [PATCH v2 4/6] target/hexagon: add GVec overrides for HVX absolute difference

target/hexagon/gen_tcg_hvx.h static inline void assert_vhist_tmp(DisasContext *ctx) · line 345

Quoted 11 lines · Brian Cain
target/hexagon/gen_tcg_hvx.h +11 -0
+#define fGEN_TCG_VEC_ABSDIFF(VECE, MAX_FN, MIN_FN) \
+    do { \
+        intptr_t tmpoff = offsetof(CPUHexagonState, vtmp); \
+        MAX_FN(VECE, tmpoff, VuV_off, VvV_off, \
+               VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE); \
+        MIN_FN(VECE, VdV_off, VuV_off, VvV_off, \
+               VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE); \
+        tcg_gen_gvec_sub(VECE, VdV_off, tmpoff, VdV_off, \
+                         VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE); \
+    } while (0)
+

TCG doesn't expect you to require temporaries like this, and provides for custom expanders. Compare target/arm/tcg/gengvec.c, gen_gvec_sabd.

r~

author
#9 lore raw

PATCH v2 5/6 target/hexagon: add GVec overrides for HVX vector average

In reply to Brian Cain
---
 target/hexagon/gen_tcg_hvx.h | 62 ++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)
target/hexagon/gen_tcg_hvx.h +62 -0
index 3cb52c761ae..97c373c10bf 100644
@@ -355,6 +355,68 @@ static inline void assert_vhist_tmp(DisasContext *ctx)
 #define fGEN_TCG_V6_vabsdiffw(SHORTCODE) \
     fGEN_TCG_VEC_ABSDIFF(MO_32, tcg_gen_gvec_smax, tcg_gen_gvec_smin)
 
+#define fGEN_TCG_VEC_AVG(VECE, SHIFT_FN) \
+    do { \
+        intptr_t tmpoff = offsetof(CPUHexagonState, vtmp); \
+        tcg_gen_gvec_and(MO_64, tmpoff, VuV_off, VvV_off, \
+                         VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE); \
+        tcg_gen_gvec_xor(MO_64, VdV_off, VuV_off, VvV_off, \
+                         VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE); \
+        SHIFT_FN(VECE, VdV_off, VdV_off, 1, \
+                 VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE); \
+        tcg_gen_gvec_add(VECE, VdV_off, tmpoff, VdV_off, \
+                         VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE); \
+    } while (0)
+
+#define fGEN_TCG_V6_vavgub(SHORTCODE) \
+    fGEN_TCG_VEC_AVG(MO_8, tcg_gen_gvec_shri)
+
+#define fGEN_TCG_V6_vavguh(SHORTCODE) \
+    fGEN_TCG_VEC_AVG(MO_16, tcg_gen_gvec_shri)
+
+#define fGEN_TCG_V6_vavguw(SHORTCODE) \
+    fGEN_TCG_VEC_AVG(MO_32, tcg_gen_gvec_shri)
+
+#define fGEN_TCG_V6_vavgb(SHORTCODE) \
+    fGEN_TCG_VEC_AVG(MO_8, tcg_gen_gvec_sari)
+
+#define fGEN_TCG_V6_vavgh(SHORTCODE) \
+    fGEN_TCG_VEC_AVG(MO_16, tcg_gen_gvec_sari)
+
+#define fGEN_TCG_V6_vavgw(SHORTCODE) \
+    fGEN_TCG_VEC_AVG(MO_32, tcg_gen_gvec_sari)
+
+#define fGEN_TCG_VEC_AVGRND(VECE, SHIFT_FN) \
+    do { \
+        intptr_t tmpoff = offsetof(CPUHexagonState, vtmp); \
+        tcg_gen_gvec_or(MO_64, tmpoff, VuV_off, VvV_off, \
+                        VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE); \
+        tcg_gen_gvec_xor(MO_64, VdV_off, VuV_off, VvV_off, \
+                         VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE); \
+        SHIFT_FN(VECE, VdV_off, VdV_off, 1, \
+                 VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE); \
+        tcg_gen_gvec_sub(VECE, VdV_off, tmpoff, VdV_off, \
+                         VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE); \
+    } while (0)
+
+#define fGEN_TCG_V6_vavgubrnd(SHORTCODE) \
+    fGEN_TCG_VEC_AVGRND(MO_8, tcg_gen_gvec_shri)
+
+#define fGEN_TCG_V6_vavguhrnd(SHORTCODE) \
+    fGEN_TCG_VEC_AVGRND(MO_16, tcg_gen_gvec_shri)
+
+#define fGEN_TCG_V6_vavguwrnd(SHORTCODE) \
+    fGEN_TCG_VEC_AVGRND(MO_32, tcg_gen_gvec_shri)
+
+#define fGEN_TCG_V6_vavgbrnd(SHORTCODE) \
+    fGEN_TCG_VEC_AVGRND(MO_8, tcg_gen_gvec_sari)
+
+#define fGEN_TCG_V6_vavghrnd(SHORTCODE) \
+    fGEN_TCG_VEC_AVGRND(MO_16, tcg_gen_gvec_sari)
+
+#define fGEN_TCG_V6_vavgwrnd(SHORTCODE) \
+    fGEN_TCG_VEC_AVGRND(MO_32, tcg_gen_gvec_sari)
+
 /* Vector shift right - various forms */
 #define fGEN_TCG_V6_vasrh(SHORTCODE) \
     do { \
Signature · 1 line
2.34.1
author
#10 lore raw

PATCH v2 6/6 tests/tcg/hexagon: add HVX test for V6_vsubwsat saturation

In reply to Brian Cain
---
 tests/tcg/hexagon/hvx_misc.c | 44 ++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
tests/tcg/hexagon/hvx_misc.c +44 -0
index 90c3733da07..641cff59aba 100644
@@ -20,6 +20,8 @@
 #include <stdbool.h>
 #include <string.h>
 #include <limits.h>
+#include <hexagon_types.h>
+#include <hvx_hexagon_protos.h>
 
 int err;
 
@@ -386,6 +388,47 @@ static void test_vsubuwsat_dv(void)
     check_output_w(__LINE__, 2);
 }
 
+static void test_vsubwsat(void)
+{
+    const int32_t x0 = INT32_MIN;
+    const int32_t y0 = 1;
+    const int32_t x1 = INT32_MAX;
+    const int32_t y1 = -1;
+    HVX_Vector v0;
+    HVX_Vector v1;
+    HVX_Vector vres;
+
+    /* INT32_MIN - 1 underflows and must saturate to INT32_MIN */
+    memset(expect, 0x12, sizeof(MMVector));
+    memset(output, 0x34, sizeof(MMVector));
+
+    v0 = Q6_V_vsplat_R(x0);
+    v1 = Q6_V_vsplat_R(y0);
+    vres = Q6_Vw_vsub_VwVw_sat(v0, v1);
+    memcpy(&output[0], &vres, sizeof(MMVector));
+
+    for (int j = 0; j < MAX_VEC_SIZE_BYTES / 4; j++) {
+        expect[0].w[j] = INT32_MIN;
+    }
+
+    check_output_w(__LINE__, 1);
+
+    /* INT32_MAX - (-1) overflows and must saturate to INT32_MAX */
+    memset(expect, 0x12, sizeof(MMVector));
+    memset(output, 0x34, sizeof(MMVector));
+
+    v0 = Q6_V_vsplat_R(x1);
+    v1 = Q6_V_vsplat_R(y1);
+    vres = Q6_Vw_vsub_VwVw_sat(v0, v1);
+    memcpy(&output[0], &vres, sizeof(MMVector));
+
+    for (int j = 0; j < MAX_VEC_SIZE_BYTES / 4; j++) {
+        expect[0].w[j] = INT32_MAX;
+    }
+
+    check_output_w(__LINE__, 1);
+}
+
 static void test_load_tmp_predicated(void)
 {
     void *p0 = buffer0;
@@ -530,6 +573,7 @@ int main()
 
     test_vadduwsat();
     test_vsubuwsat_dv();
+    test_vsubwsat();
 
     test_load_tmp_predicated();
     test_load_cur_predicated();
Signature · 1 line
2.34.1