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

Brian Cain <[email protected]> Wed, 5 Aug 2026 09:52:25 -0700
Newsgroups gmane.comp.emulators.qemu
Message-ID <[email protected]>
Signed-off-by: Brian Cain <[email protected]>
---
 tests/tcg/hexagon/hvx_misc.c | 44 ++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/tests/tcg/hexagon/hvx_misc.c b/tests/tcg/hexagon/hvx_misc.c
index 90c3733da07..641cff59aba 100644
--- a/tests/tcg/hexagon/hvx_misc.c
+++ b/tests/tcg/hexagon/hvx_misc.c
@@ -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();
-- 
2.34.1