Skip to contentSkip to conversation

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

PATCH In review Brian Cain 2 participants 9 messages Aug 7 – Aug 10

lore
Share & tools
lore

Brian Cain

author
#0 lore raw

v3 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 v3:

* Created custom expanders for absdiff
* New commit with tests for absdiff (only commit w/o R-b)
* Switched addsub to use a single 2x tcg_gen_gvec op for the pair

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

Brian Cain (7):
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 tests/tcg/hexagon: add HVX tests for vabsdiff

 include/qemu/host-utils.h    |  24 ++---
 target/hexagon/gen_tcg_hvx.h | 170 +++++++++++++++++++++++++++++++++++
 target/hexagon/genptr.h      |   4 +
 target/hexagon/helper.h      |   5 ++
 target/hexagon/genptr.c      |  70 +++++++++++++++
 target/hexagon/op_helper.c   |  21 +++++
 tests/tcg/hexagon/hvx_misc.c |  77 ++++++++++++++++
 7 files changed, 359 insertions(+), 12 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 1/7 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

PATCH v3 2/7 target/hexagon: add GVec overrides for HVX saturating add/sub

In reply to Brian Cain
---
 target/hexagon/gen_tcg_hvx.h | 87 ++++++++++++++++++++++++++++++++++++
 1 file changed, 87 insertions(+)
target/hexagon/gen_tcg_hvx.h +87 -0
index 0da64d467e6..289ce899598 100644
@@ -234,6 +234,93 @@ 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) \
+    tcg_gen_gvec_##OP(VECE, DST, SRC_A, SRC_B, \
+                     2 * VECTOR_SIZE_BYTE, 2 * VECTOR_SIZE_BYTE)
+
+#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
author
#3 lore raw

PATCH v3 3/7 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 289ce899598..7e5372a7642 100644
@@ -321,6 +321,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
#4 lore raw

PATCH v3 4/7 target/hexagon: add GVec overrides for HVX absolute difference

In reply to Brian Cain
---
 target/hexagon/gen_tcg_hvx.h | 16 +++++++++
 target/hexagon/genptr.h      |  4 +++
 target/hexagon/helper.h      |  5 +++
 target/hexagon/genptr.c      | 70 ++++++++++++++++++++++++++++++++++++
 target/hexagon/op_helper.c   | 21 +++++++++++
 5 files changed, 116 insertions(+)
target/hexagon/gen_tcg_hvx.h +16 -0
index 7e5372a7642..cbf84a8caee 100644
@@ -326,6 +326,22 @@ 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_V6_vabsdiffub(SHORTCODE) \
+    gen_gvec_uabsdiff(MO_8, VdV_off, VuV_off, VvV_off, \
+                     VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
+
+#define fGEN_TCG_V6_vabsdiffuh(SHORTCODE) \
+    gen_gvec_uabsdiff(MO_16, VdV_off, VuV_off, VvV_off, \
+                     VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
+
+#define fGEN_TCG_V6_vabsdiffh(SHORTCODE) \
+    gen_gvec_sabsdiff(MO_16, VdV_off, VuV_off, VvV_off, \
+                     VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
+
+#define fGEN_TCG_V6_vabsdiffw(SHORTCODE) \
+    gen_gvec_sabsdiff(MO_32, VdV_off, VuV_off, VvV_off, \
+                     VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
+
 /* Vector shift right - various forms */
 #define fGEN_TCG_V6_vasrh(SHORTCODE) \
     do { \
target/hexagon/genptr.h +4 -0
index 45ee038ca94..172d5cff3af 100644
@@ -58,6 +58,10 @@ TCGv gen_get_half(TCGv result, int N, TCGv src, bool sign);
 void gen_set_half(int N, TCGv result, TCGv src);
 void gen_set_half_i64(int N, TCGv_i64 result, TCGv src);
 void probe_noshuf_load(TCGv va, int s, int mi);
+void gen_gvec_sabsdiff(unsigned vece, uint32_t dofs, uint32_t aofs,
+                       uint32_t bofs, uint32_t oprsz, uint32_t maxsz);
+void gen_gvec_uabsdiff(unsigned vece, uint32_t dofs, uint32_t aofs,
+                       uint32_t bofs, uint32_t oprsz, uint32_t maxsz);
 
 extern const target_ulong reg_immut_masks[TOTAL_PER_THREAD_REGS];
 
target/hexagon/helper.h +5 -0
index 033e5619d6b..78dc28ca9e5 100644
@@ -108,6 +108,11 @@ DEF_HELPER_2(probe_pkt_scalar_store_s0, void, env, int)
 DEF_HELPER_2(probe_hvx_stores, void, env, int)
 DEF_HELPER_2(probe_pkt_scalar_hvx_stores, void, env, int)
 
+DEF_HELPER_FLAGS_4(gvec_sabsdiff_h, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, i32)
+DEF_HELPER_FLAGS_4(gvec_sabsdiff_w, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, i32)
+DEF_HELPER_FLAGS_4(gvec_uabsdiff_b, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, i32)
+DEF_HELPER_FLAGS_4(gvec_uabsdiff_h, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, i32)
+
 #if !defined(CONFIG_USER_ONLY)
 DEF_HELPER_3(raise_stack_overflow, void, env, i32, i32)
 DEF_HELPER_2(swi, void, env, i32)
target/hexagon/genptr.c +70 -0

76 lines not on this page · raw message

target/hexagon/op_helper.c +21 -0
index 3ce223caba3..f0574dae5ad 100644
@@ -35,6 +35,7 @@
 #include "mmvec/macros.h"
 #include "op_helper.h"
 #include "cpu_helper.h"
+#include "tcg/tcg-gvec-desc.h"
 #include "translate.h"
 #ifndef CONFIG_USER_ONLY
 #include "hw/hexagon/hexagon_globalreg.h"
@@ -1947,3 +1948,23 @@ void HELPER(pending_interrupt)(CPUHexagonState *env)
     printf("ERROR: bogus helper: " #tag "\n")
 
 #include "helper_funcs_generated.c.inc"
+
+#define DO_ABSDIFF(NAME, TYPE, UTYPE) \
+void HELPER(NAME)(void *vd, void *vn, void *vm, uint32_t desc) \
+{ \
+    intptr_t i, oprsz = simd_oprsz(desc); \
+    UTYPE *d = vd; \
+    TYPE *n = vn, *m = vm; \
+    \
+    for (i = 0; i < oprsz / sizeof(TYPE); i++) { \
+        d[i] = n[i] < m[i] ? (UTYPE)m[i] - (UTYPE)n[i] \
+                           : (UTYPE)n[i] - (UTYPE)m[i]; \
+    } \
+}
+
+DO_ABSDIFF(gvec_sabsdiff_h, int16_t, uint16_t)
+DO_ABSDIFF(gvec_sabsdiff_w, int32_t, uint32_t)
+DO_ABSDIFF(gvec_uabsdiff_b, uint8_t, uint8_t)
+DO_ABSDIFF(gvec_uabsdiff_h, uint16_t, uint16_t)
+
+#undef DO_ABSDIFF
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH v3 5/7 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 cbf84a8caee..2a342cdee69 100644
@@ -342,6 +342,68 @@ static inline void assert_vhist_tmp(DisasContext *ctx)
     gen_gvec_sabsdiff(MO_32, VdV_off, VuV_off, VvV_off, \
                      VECTOR_SIZE_BYTE, VECTOR_SIZE_BYTE)
 
+#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
#6 lore raw

PATCH v3 6/7 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
author
#7 lore raw

PATCH v3 7/7 tests/tcg/hexagon: add HVX tests for vabsdiff

In reply to Brian Cain

Cover the four vabsdiff variants.

The expected value is computed in int64_t to avoid overflowing the source element type.

---
 tests/tcg/hexagon/hvx_misc.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
tests/tcg/hexagon/hvx_misc.c +33 -0
index 641cff59aba..32a3661a86f 100644
@@ -317,6 +317,34 @@ TEST_VEC_OP2(vand, vand, , d, 8, &)
 TEST_VEC_OP2(vor, vor, , d, 8, |)
 TEST_VEC_OP1(vnot, vnot, , d, 8, ~)
 
+#define TEST_VEC_ABSDIFF(NAME, INTRINSIC, SRC_FIELD, DST_FIELD, \
+                         CHECK_FIELD, FIELDSZ) \
+static inline void test_##NAME(void) \
+{ \
+    HVX_Vector v0; \
+    HVX_Vector v1; \
+    HVX_Vector vres; \
+    for (int i = 0; i < BUFSIZE; i++) { \
+        memcpy(&v0, &buffer0[i], sizeof(MMVector)); \
+        memcpy(&v1, &buffer1[i], sizeof(MMVector)); \
+        vres = INTRINSIC(v0, v1); \
+        memcpy(&output[i], &vres, sizeof(MMVector)); \
+    } \
+    for (int i = 0; i < BUFSIZE; i++) { \
+        for (int j = 0; j < MAX_VEC_SIZE_BYTES / FIELDSZ; j++) { \
+            int64_t diff = (int64_t)buffer0[i].SRC_FIELD[j] - \
+                           (int64_t)buffer1[i].SRC_FIELD[j]; \
+            expect[i].DST_FIELD[j] = diff < 0 ? -diff : diff; \
+        } \
+    } \
+    check_output_##CHECK_FIELD(__LINE__, BUFSIZE); \
+}
+
+TEST_VEC_ABSDIFF(vabsdiffub, Q6_Vub_vabsdiff_VubVub, ub, ub, b, 1)
+TEST_VEC_ABSDIFF(vabsdiffuh, Q6_Vuh_vabsdiff_VuhVuh, uh, uh, h, 2)
+TEST_VEC_ABSDIFF(vabsdiffh,  Q6_Vuh_vabsdiff_VhVh,   h,  uh, h, 2)
+TEST_VEC_ABSDIFF(vabsdiffw,  Q6_Vuw_vabsdiff_VwVw,   w,  uw, w, 4)
+
 TEST_PRED_OP2(pred_or, or, |, "")
 TEST_PRED_OP2(pred_or_n, or, |, "!")
 TEST_PRED_OP2(pred_and, and, &, "")
@@ -575,6 +603,11 @@ int main()
     test_vsubuwsat_dv();
     test_vsubwsat();
 
+    test_vabsdiffub();
+    test_vabsdiffuh();
+    test_vabsdiffh();
+    test_vabsdiffw();
+
     test_load_tmp_predicated();
     test_load_cur_predicated();
 
Signature · 1 line
2.34.1
Reviewed-by on v3 7/7
#8 lore raw

Re: [PATCH v3 7/7] tests/tcg/hexagon: add HVX tests for vabsdiff

Quoted 10 lines · Brian Cain

Cover the four vabsdiff variants.

The expected value is computed in int64_t to avoid overflowing the source element type.

---
 tests/tcg/hexagon/hvx_misc.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)