Skip to content

vsmul: saturation bounds are 32-bit at SEW=64 (Scala Int overflow) - #98

Merged
jerryz123 merged 1 commit into
ucb-bar:masterfrom
cwssemi:cwssemi/vsmul-clip-bounds-sew64
Sep 2, 2026
Merged

jerryz123 merged 1 commit into
ucb-bar:masterfrom
cwssemi:cwssemi/vsmul-clip-bounds-sew64

Conversation

@cwssemi

@cwssemi cwssemi commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Fixes the SEW=64 vsmul saturation bounds reported in #95.

(-1 << ((8 << sew)-1)).S and ((1 << ((8 << sew)-1)) - 1).S are built with Scala Int
arithmetic. Int is 32 bits and << masks the shift distance to 5 bits, so at sew=3 the
distance 63 becomes 31 and both bounds come out ±2^31 instead of ±2^63.

Two wrong behaviours follow at SEW=64: a genuine saturation clips to INT32_MAX instead of
INT64_MAX, and a result merely larger than INT32_MAX is clipped although the spec does not
saturate it — raising vxsat when it should not.

sew=2 is correct only by accident: 1 << 31 overflows to Int.MinValue and the - 1
wraps back to exactly 2147483647. So checking "does SEW=32 work" gives the right answer for the
wrong reason, which is presumably how it survived review.

The same constant pair appears in four places and all four are fixed here, but only
SegmentedMultiplyPipe is instantiated by the config measured — the other three are the same
defect by inspection, not by measurement. Parameters.scala's maxPosSInt/minNegSInt have no
call sites at all; they are worth taking because the adjacent unsigned helpers build their
widths in Chisel and are correct, so the broken idiom sits directly beneath the safe one under a
name a future caller would reach for.

Measured on REFV256D128RocketConfig at c39102c: riscv-vector-tests vsmul_vv-4,5,6,7 and
vsmul_vx-17,18,23,24 go 8/8 fail → 8/8 pass, and the multiply-family regression (vmul,
vmulh{,u,su}, vwmul{,u,su}, vmacc, vnmsac, vmadd, vnmsub, vwmacc) is 143/143.

A standalone reproducer will be attached below — prebuilt ELF, sources and disassembly. It reports
bad=3 on stock c39102c and bad=0 with this patch, same ELF both runs, and is clean on Spike
(--isa=rv64gcv_zvl256b) so the RTL results are an implementation difference rather than a
program error.

The vsmul clip bounds are built with SCALA Int arithmetic inside .S:

    val clip_neg = (-1 << ((8 << sew)-1)).S
    val clip_pos = ((1 << ((8 << sew)-1)) - 1).S

Scala's Int is 32 bits and << masks the shift distance to 5 bits, so at sew=3
the distance 63 becomes 31 and both bounds come out 32-bit -- +2147483647 and
-2147483648 instead of +-2^63.

  sew=0 SEW=8    127 / -128            ok
  sew=1 SEW=16   32767 / -32768        ok
  sew=2 SEW=32   2147483647 / ...      ok BY ACCIDENT: 1 << 31 overflows to
                                       Int.MinValue and the - 1 wraps back to
                                       exactly 2147483647
  sew=3 SEW=64   2147483647 / ...      WRONG

Two distinct wrong behaviours follow at SEW=64:

  * a genuine saturation clips to INT32_MAX instead of INT64_MAX
  * a result merely LARGER than INT32_MAX is clipped although the spec does not
    saturate it, and vxsat is raised when it should not be

The sew=2 accident is why this survives review: checking "does SEW=32 work"
gives the right answer for the wrong reason.

Measured on REFV256D128RocketConfig, VLEN=256, MODE=machine, TEST_MODE=self.
Re-measured 2026-08-30 on master c39102c with a standalone reproducer, same
ELF both arms: bad=3 before, bad=0 after, and vxsat correct on the two rows
that were spuriously saturating. So the patch still applies and still fixes at
the head it merges into.
Expected values were hand-derived from the spec and confirmed against Spike
(8/8) before running on hardware; the "predicted" column was written into the
probe before the run.

                              before              predicted           after
  INT_MIN x INT_MIN           000000007fffffff    000000007fffffff    7fffffffffffffff
  INT_MIN x -2^31   vxsat=1   000000007fffffff    000000007fffffff    0000000080000000  vxsat=0
  INT_MIN x (2^31+1) vxsat=1  ffffffff80000000    ffffffff80000000    ffffffff7fffffff  vxsat=0
  controls (INT_MIN x -1, -1 x -1, SEW 8/16/32)   ok                  ok

  riscv-vector-tests: vsmul_vv-4,5,6,7 and vsmul_vx-17,18,23,24
                      8/8 fail -> 8/8 pass
  multiply family regression (vmul, vmulh{,u,su}, vwmul{,u,su}, vmacc,
                      vnmsac, vmadd, vnmsub, vwmacc): 143/143 pass

The same constant pair appears in four places. All four are fixed here, but only
SegmentedMultiplyPipe is instantiated by the config measured above --
ElementwiseMultiplyPipe and IntegerDivide are not in that build, and
Parameters.scala's maxPosSInt/minNegSInt have NO call sites anywhere in the
repository. So the fix is applied in four places and measured in one; the other
three are the same defect by inspection, not by measurement.

The Parameters.scala pair is worth taking anyway because the adjacent unsigned
helpers build their widths in Chisel -- Cat(0.U, ~(0.U(((8 << sew)-1).W))) --
and are correct, so the broken idiom sits directly beneath the safe one under a
name a future caller would reach for.

Found by sweeping the tree for Scala literal shifts with a width-derived
distance: 52 sites, of which these four are the only ones whose distance can
reach 31. The rest are bounded well below it (eew/sew 0..3, FP exponents <= 11,
pgIdxBits 12).
@cwssemi

cwssemi commented Aug 30, 2026

Copy link
Copy Markdown
Contributor Author

A reproducer for this.

Five arms at SEW=64 against a scalar oracle, plus three narrower controls. Same ELF both runs, on
REFV256D128RocketConfig under Verilator:

master c39102c, stock
  e64 INT_MIN x INT_MIN      got=000000007fffffff want=7fffffffffffffff  vxsat got=1 want=1  FAIL (32-bit clip bound)
  e64 INT_MIN x -2^31        got=000000007fffffff want=0000000080000000  vxsat got=1 want=0  FAIL (32-bit clip bound)
  e64 INT_MIN x (2^31+1)     got=ffffffff80000000 want=ffffffff7fffffff  vxsat got=1 want=0  FAIL (32-bit clip bound)
  e64 INT_MIN x -1           got=0000000000000001 want=0000000000000001  vxsat got=0 want=0  ok
  e64 -1 x -1                got=0000000000000000 want=0000000000000000  vxsat got=0 want=0  ok
  e8   INT_MIN x INT_MIN     got=7f want=7f  vxsat=1  ok
  e16  INT_MIN x INT_MIN     got=7fff want=7fff  vxsat=1  ok
  e32  INT_MIN x INT_MIN     got=7fffffff want=7fffffff  vxsat=1  ok
  QS20-REPRO bad=3

master c39102c + the patch in the issue body
  e64 INT_MIN x INT_MIN      got=7fffffffffffffff want=7fffffffffffffff  vxsat got=1 want=1  ok
  e64 INT_MIN x -2^31        got=0000000080000000 want=0000000080000000  vxsat got=0 want=0  ok
  e64 INT_MIN x (2^31+1)     got=ffffffff7fffffff want=ffffffff7fffffff  vxsat got=0 want=0  ok
  e64 INT_MIN x -1           got=0000000000000001 want=0000000000000001  vxsat got=0 want=0  ok
  e64 -1 x -1                got=0000000000000000 want=0000000000000000  vxsat got=0 want=0  ok
  e8   INT_MIN x INT_MIN     got=7f want=7f  vxsat=1  ok
  e16  INT_MIN x INT_MIN     got=7fff want=7fff  vxsat=1  ok
  e32  INT_MIN x INT_MIN     got=7fffffff want=7fffffff  vxsat=1  ok
  QS20-REPRO bad=0

The three failures are the two distinct behaviours. One is a genuine saturation clipping to
INT32_MAX instead of INT64_MAX. The other is sharper: vxsat raised on results the spec does
not saturate at all
— rows 2 and 3 have want=0 got=1 before the patch and got=0 after.

e32 is correct only by accident, which is why the narrow controls are in the binary rather
than trimmed: (1 << 31) - 1 as a Scala Int overflows to exactly the right value, while
e8 and e16 are correct properly. That is what shows the bounds are an arithmetic overflow
rather than a deliberate 32-bit clip — (-1 << ((8 << eew)-1)).S with Scala's 32-bit Int, and
<< masking the shift distance to 5 bits, so eew=3 shifts by 31 rather than 63.

The controls show vxsat=1 in both runs and that is correct: INT_MIN × INT_MIN genuinely
saturates at every width.

Functional control: the same ELF is clean on Spike (--isa=rv64gcv_zvl256b, bad=0), so the
RTL results are an implementation difference rather than a program error.

Attached: prebuilt ELF, disassembly, source, and how to build and run it.

qs20-repro-bundle.tar.gz

@cwssemi

cwssemi commented Aug 30, 2026

Copy link
Copy Markdown
Contributor Author

This is another test escape from riscv-vector-tests that is addressed in chipsalliance/riscv-vector-tests#97

@jerryz123

Copy link
Copy Markdown
Contributor

Thank you!

@jerryz123
jerryz123 merged commit 59f870f into ucb-bar:master Sep 2, 2026
@cwssemi
cwssemi deleted the cwssemi/vsmul-clip-bounds-sew64 branch September 2, 2026 03:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants