All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] target/mips: Clamp negative Loongson PACKUSHB inputs to zero
@ 2026-12-05 15:03 fxlin0261
  0 siblings, 0 replies; only message in thread
From: fxlin0261 @ 2026-12-05 15:03 UTC (permalink / raw)
  To: qemu-devel; +Cc: philmd, aurelien, jiaxun.yang, arikalo, qemu-stable, fxlin0261

Loongson PACKUSHB packs signed 16-bit values into unsigned bytes.
The helper clamps values above 255 but leaves negative values unchanged
before taking the low eight bits. As a result, -1 produces 255 when it
should produce 0.

Clamp each halfword to the range 0 to 255 before packing it. Add a TCG
regression test with negative values in both source registers so that
it catches the bug in either half of the packed result.

Fixes: bd277fa1966b ("target-mips: Implement Loongson Multimedia Instructions")
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/5083
AI-used-for: code, tests
Signed-off-by: fxlin0261 <fxlin0261@gmail.com>
---
 target/mips/tcg/lmmi_helper.c        |  6 ++----
 tests/tcg/mips64/meson.build         |  4 ++++
 tests/tcg/mips64/test_mmi_packushb.c | 31 ++++++++++++++++++++++++++++
 3 files changed, 37 insertions(+), 4 deletions(-)
 create mode 100644 tests/tcg/mips64/test_mmi_packushb.c

diff --git a/target/mips/tcg/lmmi_helper.c b/target/mips/tcg/lmmi_helper.c
index acda1b69be..a2f3b50d99 100644
--- a/target/mips/tcg/lmmi_helper.c
+++ b/target/mips/tcg/lmmi_helper.c
@@ -307,13 +307,11 @@ uint64_t HELPER(packushb)(uint64_t fs, uint64_t ft)
 
     for (i = 0; i < 4; ++i) {
         int16_t tmp = fs >> (i * 16);
-        tmp = SATUB(tmp);
-        fd |= (uint64_t)(tmp & 0xff) << (i * 8);
+        fd |= (uint64_t)MIN(MAX(tmp, 0), UINT8_MAX) << (i * 8);
     }
     for (i = 0; i < 4; ++i) {
         int16_t tmp = ft >> (i * 16);
-        tmp = SATUB(tmp);
-        fd |= (uint64_t)(tmp & 0xff) << (i * 8 + 32);
+        fd |= (uint64_t)MIN(MAX(tmp, 0), UINT8_MAX) << (i * 8 + 32);
     }
 
     return fd;
diff --git a/tests/tcg/mips64/meson.build b/tests/tcg/mips64/meson.build
index ad69e36d91..49ecaac1fc 100644
--- a/tests/tcg/mips64/meson.build
+++ b/tests/tcg/mips64/meson.build
@@ -3,6 +3,10 @@ tests = []
 tests += tcg_tests['multiarch-linux-user']['tests']
 
 tests += {
+  '../mips64/test_mmi_packushb.c': {
+    'qemu_args': ['-cpu', 'Loongson-3A1000'],
+    'protocol': 'tap',
+  },
   '../mips64/octeon-insns.c': {
     'qemu_args': ['-cpu', 'Octeon68XX'],
     'protocol': 'tap',
diff --git a/tests/tcg/mips64/test_mmi_packushb.c b/tests/tcg/mips64/test_mmi_packushb.c
new file mode 100644
index 0000000000..3129eb2b54
--- /dev/null
+++ b/tests/tcg/mips64/test_mmi_packushb.c
@@ -0,0 +1,31 @@
+/*
+ * Test signed halfword to unsigned byte saturation in Loongson MMI PACKUSHB.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <inttypes.h>
+#include "../include/tap.h"
+
+int main(void)
+{
+    /* Least significant lane first: -2, -1, 1, 256. */
+    uint64_t fs = UINT64_C(0x01000001fffffffe);
+    /* Least significant lane first: -32767, 0, 255, 32767. */
+    uint64_t ft = UINT64_C(0x7fff00ff00008001);
+    uint64_t result;
+
+    TAP_START();
+    /* Use the encoding for assemblers without Loongson MMI support. */
+    asm volatile(
+        "dmtc1 %[fs], $f0\n\t"
+        "dmtc1 %[ft], $f2\n\t"
+        ".word 0x4b620102\n\t" /* packushb $f4, $f0, $f2 */
+        "dmfc1 %[result], $f4"
+        : [result] "=r" (result)
+        : [fs] "r" (fs), [ft] "r" (ft)
+        : "$f0", "$f2", "$f4");
+    tap_report(result == UINT64_C(0xffff0000ff010000),
+               "PACKUSHB result = 0x%016" PRIx64, result);
+    return tap_done();
+}
-- 
2.43.0



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-05 20:54 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 15:03 [PATCH] target/mips: Clamp negative Loongson PACKUSHB inputs to zero fxlin0261

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.