From: fxlin0261 <fxlin0261@gmail.com>
To: qemu-devel@nongnu.org
Cc: philmd@oss.qualcomm.com, aurelien@aurel32.net,
jiaxun.yang@flygoat.com, arikalo@gmail.com,
qemu-stable@nongnu.org, fxlin0261 <fxlin0261@gmail.com>
Subject: [PATCH] target/mips: Clamp negative Loongson PACKUSHB inputs to zero
Date: Sat, 5 Dec 2026 23:03:06 +0800 [thread overview]
Message-ID: <20261205150306.194227-1-fxlin0261@gmail.com> (raw)
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
reply other threads:[~2026-12-05 20:54 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261205150306.194227-1-fxlin0261@gmail.com \
--to=fxlin0261@gmail.com \
--cc=arikalo@gmail.com \
--cc=aurelien@aurel32.net \
--cc=jiaxun.yang@flygoat.com \
--cc=philmd@oss.qualcomm.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-stable@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.