Skip to content

Conversation

@jackkoenig
Copy link
Contributor

@jackkoenig jackkoenig commented Mar 11, 2025

Found by @tymcauley ucb-bar/fixedpoint#11 (comment)

This bug was introduced 2 years ago when we added support for zero-width literals: #2932

The problem is that BigInt.bitlength excludes the sign bit, so we need to add 1 to it for SInts (which is what we originally did), BUT we need special case support for zero-width SInts (only when specified).

Contributor Checklist

  • Did you add Scaladoc to every public function/method?
  • Did you add at least one test demonstrating the PR?
  • Did you delete any extraneous printlns/debugging code?
  • Did you specify the type of improvement?
  • Did you add appropriate documentation in docs/src?
  • Did you request a desired merge strategy?
  • Did you add text to be included in the Release Notes for this change?

Type of Improvement

  • Bugfix

Desired Merge Strategy

  • Squash

Release Notes

For users attempting to reinterpret raw 2s complement values as SInts, use .U.asSInt.

Reviewer Checklist (only modified by reviewer)

  • Did you add the appropriate labels? (Select the most appropriate one based on the "Type of Improvement")
  • Did you mark the proper milestone (Bug fix: 3.6.x, 5.x, or 6.x depending on impact, API modification or big change: 7.0)?
  • Did you review?
  • Did you check whether all relevant Contributor checkboxes have been checked?
  • Did you do one of the following when ready to merge:
    • Squash: You/ the contributor Enable auto-merge (squash) and clean up the commit message.
    • Merge: Ensure that contributor has cleaned up their commit history, then merge with Create a merge commit.

@jackkoenig jackkoenig added the Bugfix Fixes a bug, will be included in release notes label Mar 11, 2025
@jackkoenig jackkoenig added this to the 6.x milestone Mar 11, 2025
Comment on lines +288 to +300
property("SInt literals with too small of a width should be rejected") {
// Sanity checks.
0.S.getWidth should be(1)
0.S(0.W).getWidth should be(0)
-1.S.getWidth should be(1)
1.S.getWidth should be(2)
// The real check.
-2.S.getWidth should be(2)
an[IllegalArgumentException] shouldBe thrownBy(-2.S(1.W))
0xde.S.getWidth should be(9)
an[IllegalArgumentException] shouldBe thrownBy(0xde.S(8.W))
an[IllegalArgumentException] shouldBe thrownBy(0xde.S(4.W))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests look right! I haven't thought through the other ramifications of this change, other than that the existing tests in the repo all pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm confident it's correct. Note that even before this change 0xde.S.getWidth == 9, it's just that we were accepting 1-bit less than the correct answer if the user specified it which would then coincidentally do the right thing in most circumstances due to the FIRRTL we emitted actually having the correct width semantics. Thus things would work as long as you didn't introspect on the width and do things with it.

@jackkoenig jackkoenig enabled auto-merge (squash) March 11, 2025 23:46
@jackkoenig jackkoenig merged commit db93161 into main Mar 12, 2025
15 checks passed
@jackkoenig jackkoenig deleted the jackkoenig/fix-sint-literal-width branch March 12, 2025 00:05
@mergify mergify bot added the Backported This PR has been backported label Mar 12, 2025
chiselbot pushed a commit that referenced this pull request Mar 12, 2025
Co-authored-by: Schuyler Eldridge <[email protected]>
(cherry picked from commit db93161)

# Conflicts:
#	src/test/scala/chiselTests/SIntOps.scala
@mergify
Copy link
Contributor

mergify bot commented Mar 12, 2025

backport

✅ Backports have been created

@jackkoenig jackkoenig modified the milestones: 6.x, 7.0 Mar 12, 2025
@jackkoenig jackkoenig removed the Backported This PR has been backported label Mar 12, 2025
@tymcauley
Copy link
Contributor

Awesome, thanks for the super-fast fix @jackkoenig!

tymcauley added a commit to tymcauley/fixedpoint that referenced this pull request Mar 12, 2025
An 8-bit signed integer with a binary point at 2 effectively has a 6-bit
signed number for the value to the left of the decimal point. That means
it can represent values in the range [-2^5, 2^5-1], or [-32, 31]. After
chipsalliance/chisel#4786 was merged, the `55`
and `56` literals are now correctly flagged as being out-of-range for
this type.

I've subtracted 32 from these out-of-range values (changing the MSB from
1 to 0), and the tests now pass.
tymcauley added a commit to tymcauley/fixedpoint that referenced this pull request Mar 13, 2025
An 8-bit signed integer with a binary point at 2 effectively has a 6-bit
signed number for the value to the left of the decimal point. That means
it can represent values in the range [-2^5, 2^5-1], or [-32, 31]. After
chipsalliance/chisel#4786 was merged, the `55`
and `56` literals are now correctly flagged as being out-of-range for
this type.

I've subtracted 32 from these out-of-range values (changing the MSB from
1 to 0), and the tests now pass.
tymcauley added a commit to tymcauley/fixedpoint that referenced this pull request Mar 24, 2025
An 8-bit signed integer with a binary point at 2 effectively has a 6-bit
signed number for the value to the left of the decimal point. That means
it can represent values in the range [-2^5, 2^5-1], or [-32, 31]. After
chipsalliance/chisel#4786 was merged, the `55`
and `56` literals are now correctly flagged as being out-of-range for
this type.

I've subtracted 32 from these out-of-range values (changing the MSB from
1 to 0), and the tests now pass.
tymcauley added a commit to tymcauley/fixedpoint that referenced this pull request Mar 24, 2025
An 8-bit signed integer with a binary point at 2 effectively has a 6-bit
signed number for the value to the left of the decimal point. That means
it can represent values in the range [-2^5, 2^5-1], or [-32, 31]. After
chipsalliance/chisel#4786 was merged, the `55`
and `56` literals are now correctly flagged as being out-of-range for
this type.

I've subtracted 32 from these out-of-range values (changing the MSB from
1 to 0), and the tests now pass.
tymcauley added a commit to tymcauley/fixedpoint that referenced this pull request Apr 2, 2025
An 8-bit signed integer with a binary point at 2 effectively has a 6-bit
signed number for the value to the left of the decimal point. That means
it can represent values in the range [-2^5, 2^5-1], or [-32, 31]. After
chipsalliance/chisel#4786 was merged, the `55`
and `56` literals are now correctly flagged as being out-of-range for
this type.

I've subtracted 32 from these out-of-range values (changing the MSB from
1 to 0), and the tests now pass.
tymcauley added a commit to tymcauley/fixedpoint that referenced this pull request Apr 2, 2025
An 8-bit signed integer with a binary point at 2 effectively has a 6-bit
signed number for the value to the left of the decimal point. That means
it can represent values in the range [-2^5, 2^5-1], or [-32, 31]. After
chipsalliance/chisel#4786 was merged, the `55`
and `56` literals are now correctly flagged as being out-of-range for
this type.

I've subtracted 32 from these out-of-range values (changing the MSB from
1 to 0), and the tests now pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bugfix Fixes a bug, will be included in release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants