target/mips: Loongson MMI PACKUSHB converts -1 to 255 instead of saturating to zero
OpenAI Codex assisted with diagnosing this issue and writing the test harness. We verified the instruction output on three host platforms using QEMU built from upstream master at e138ee8ccdda733d6742914727a74d884f75f8e1.
Description of problem
PACKUSHB packs signed 16-bit lanes into unsigned 8-bit lanes. Negative values should saturate to zero and values above 255 should saturate to 255. On the emulated Loongson-3A4000, negative inputs retain their low eight bits. Loongson instruction manual, section C.6
| Lane, least significant first | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| Signed 16-bit input | -2 | -1 | 1 | 256 |
| Expected unsigned byte | 0 | 0 | 1 | 255 |
| Actual unsigned byte | 254 | 255 | 1 | 255 |
Host and guest environments
All three hosts ran a locally built qemu-system-mips64el from the commit above. The guest kernel and the two guest test binaries had identical SHA-256 hashes across hosts.
| QEMU host | Host architecture | GCC 16.1.0 guest binary | Clang 23.1.6 guest binary |
|---|---|---|---|
| macOS on Apple Silicon | arm64 | 254 255 1 255 |
254 255 1 255 |
| Linux with Ubuntu 24.04 userspace | aarch64 | 254 255 1 255 |
254 255 1 255 |
| Linux with Ubuntu 24.04 userspace | x86_64 | 254 255 1 255 |
254 255 1 255 |
The guest ran Debian Linux 6.1.0-50-loongson-3, little-endian MIPS64, N64 ABI. We used TCG with the loongson3-virt machine and Loongson-3A4000 CPU.
Steps to reproduce
-
Save the following instruction test as
packushb-test.c. Both source operands contain the same four lanes. The program prints the low four result bytes in lane order.#include <stdint.h> #include <stdio.h> int main(void) { union { uint64_t bits; double reg; } src, dst; src.bits = UINT64_C(0x01000001fffffffe); __asm__ volatile ("packushb %0, %1, %1" : "=f"(dst.reg) : "f"(src.reg)); for (unsigned i = 0; i < 4; i++) { unsigned byte = (unsigned)((dst.bits >> (8 * i)) & 0xff); printf("%u%c", byte, i == 3 ? '\n' : ' '); } return 0; } -
Cross-compile the test and place the executable in a MIPS64EL Linux guest or initramfs.
mips64el-linux-gnuabi64-gcc-16 -O2 -static \ -Wa,-mloongson-mmi,-mloongson-ext \ packushb-test.c -o packushb-test -
Boot the guest and run
./packushb-test, or invoke it from the initramfs init script. Our QEMU invocation used the following settings, withvmlinuzandprobe.cpio.gzreferring to the guest kernel and test initramfs../qemu-system-mips64el -M loongson3-virt -cpu Loongson-3A4000 \ -accel tcg -m 512 -nic none -nographic -monitor none -no-reboot \ -vga none -kernel vmlinuz -initrd probe.cpio.gz \ -append 'console=ttyS0 rdinit=/init panic=-1 loglevel=4'
Additional information
In HELPER(packushb), each source lane is read as an int16_t, passed through SATUB, then masked with 0xff. The macro clamps only values above 255, so an input of -1 reaches the mask unchanged and produces 0xff. SATUB definition · PACKUSHB helper
We found this while investigating libjpeg-turbo color conversion. For eight identical pixels with YCbCr=(226,0,149), scalar conversion produces yellow, RGB=(255,255,0). The MMI path under QEMU produces white, RGB=(255,255,255). Clamping negative lanes to zero immediately before packing restores yellow on all three hosts. libjpeg-turbo issue #947