Repository navigation
vsmul: saturation bounds are 32-bit at SEW=64 (Scala Int overflow) - #98
Conversation
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).
|
A reproducer for this. Five arms at SEW=64 against a scalar oracle, plus three narrower controls. Same ELF both runs, on The three failures are the two distinct behaviours. One is a genuine saturation clipping to
The controls show Functional control: the same ELF is clean on Spike ( Attached: prebuilt ELF, disassembly, source, and how to build and run it. |
|
This is another test escape from riscv-vector-tests that is addressed in chipsalliance/riscv-vector-tests#97 |
|
Thank you! |
Fixes the SEW=64
vsmulsaturation bounds reported in #95.(-1 << ((8 << sew)-1)).Sand((1 << ((8 << sew)-1)) - 1).Sare built with ScalaIntarithmetic.
Intis 32 bits and<<masks the shift distance to 5 bits, so atsew=3thedistance 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_MAXinstead ofINT64_MAX, and a result merely larger thanINT32_MAXis clipped although the spec does notsaturate it — raising
vxsatwhen it should not.sew=2is correct only by accident:1 << 31overflows toInt.MinValueand the- 1wraps back to exactly
2147483647. So checking "does SEW=32 work" gives the right answer for thewrong reason, which is presumably how it survived review.
The same constant pair appears in four places and all four are fixed here, but only
SegmentedMultiplyPipeis instantiated by the config measured — the other three are the samedefect by inspection, not by measurement.
Parameters.scala'smaxPosSInt/minNegSInthave nocall 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
REFV256D128RocketConfigatc39102c:riscv-vector-testsvsmul_vv-4,5,6,7andvsmul_vx-17,18,23,24go 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=3on stockc39102candbad=0with this patch, same ELF both runs, and is clean on Spike(
--isa=rv64gcv_zvl256b) so the RTL results are an implementation difference rather than aprogram error.