-
Notifications
You must be signed in to change notification settings - Fork 643
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 <schuyler.eldridge@sifive.com>
Co-authored-by: Schuyler Eldridge <schuyler.eldridge@sifive.com> (cherry picked from commit db93161) # Conflicts: # src/test/scala/chiselTests/SIntOps.scala
✅ Backports have been createdDetails
|
|
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.
* Update Chisel from 6.5.0 to 7.0.0 `UnknownWidth` became a `case object` in this Chisel PR: chipsalliance/chisel#4242 The `connectFromBits` method was removed in this Chisel PR: chipsalliance/chisel#4168 The `connectFromBits` API was replaced with the `_fromUInt` API in this Chisel PR: chipsalliance/chisel#4782 Wrapping unary negation (`unary_-%`) was deprecated in this PR: chipsalliance/chisel#4829 The sbt update is necessary to maintain compatibility with the latest Scala compiler version. * Migrate from Chisel testers to ChiselSim The `chisel3.testers` package was removed here: chipsalliance/chisel#4746 It doesn't look like ChiselSim has the same `Boolean` result from `simulate`, so we must check for simulation errors by catching exceptions. * Replace ChiselRunners trait with direct use of ChiselSim * Update to latest scalafmt * lint: Apply latest scalafmt settings * Update to latest scalatest version * Fix missing sbt command in GitHub CI For background: actions/setup-java#712 Also updated JDK to 21 (latest LTS), updated some GitHub actions to their latest versions, and using install-circt GitHub action to get firtool. * Fix out-of-range literals in tests 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. * Update Chisel 7 snapshot from 639-5df5515f to 678-dca5fc11 This update includes this Chisel PR, which deprecated everything in the `firrtl` package: chipsalliance/chisel#4878 Bump `firtool` version to latest as well. * Update Chisel version to 7.0.0-RC1 Also update CI versions of firtool and verilator to latest. * Update Chisel version to 7.5.0
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.