Skip to content

SPARC32-bit SDIV of negative divisor gives wrong result

Host environment

  • Operating system: - any -

  • OS/kernel version:

  • Architecture:

  • QEMU flavor:

  • QEMU version: 9.0.00

  • QEMU command line:

    qemu-system-sparc -M SS-5 -m 256 -drive file=sparc.qcow2,bus=0,unit=0,media=disk -drive file=solaris_7_1199_sparc.iso,bus=0,unit=2,media=cdrom,readonly=on -nographic -net nic,macaddr=52:54:0:12:34:58 -net tap,ifname=tap5,script=no,downscript=noEmulated/Virtualized environment
  • Operating system:

  • OS/kernel version:

  • Architecture: SPARC

Description of problem

SDIV of negative divisor gives wrong result because of typo in helper_sdiv(). This is true for QEMU 9.0.0 and earlier.

Place -1 in the Y register and -128 in another reg, then -120 in another register and do SDIV into a result register, instead of the proper value of 1 for the result, the incorrect value of 0 is produced.

There is a typo in target/sparc/helper.c that causes the divisor to be consider unsigned, this patch fixes it:

*** helper.c.ori Tue Apr 23 16:23:45 2024 --- helper.c Mon Apr 29 20:14:07 2024


*** 121,127 **** return (uint32_t)(b32 < 0 ? INT32_MAX : INT32_MIN) | (-1ull << 32); }

! a64 /= b; r = a64; if (unlikely(r != a64)) { return (uint32_t)(a64 < 0 ? INT32_MIN : INT32_MAX) | (-1ull << 32); --- 121,127 ---- return (uint32_t)(b32 < 0 ? INT32_MAX : INT32_MIN) | (-1ull << 32); }

! a64 /= b32; r = a64; if (unlikely(r != a64)) { return (uint32_t)(a64 < 0 ? INT32_MIN : INT32_MAX) | (-1ull << 32);

Edited by Dave Rivers
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information