-
Notifications
You must be signed in to change notification settings - Fork 640
Fix SInt literals to reject too small of widths #4786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| 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)) | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Co-authored-by: Schuyler Eldridge <[email protected]>
Co-authored-by: Schuyler Eldridge <[email protected]> (cherry picked from commit db93161) # Conflicts: # src/test/scala/chiselTests/SIntOps.scala
✅ Backports have been created
|
|
Awesome, thanks for the super-fast fix @jackkoenig! |
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.
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.
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.
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.
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.
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.
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.bitlengthexcludes 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
docs/src?Type of Improvement
Desired Merge Strategy
Release Notes
For users attempting to reinterpret raw 2s complement values as SInts, use
.U.asSInt.Reviewer Checklist (only modified by reviewer)
3.6.x,5.x, or6.xdepending on impact, API modification or big change:7.0)?Enable auto-merge (squash)and clean up the commit message.Create a merge commit.