-
Notifications
You must be signed in to change notification settings - Fork 615
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
Add width utility functions to avoid incorrect usage of bare log2Ceil(). #819
Conversation
Consensus from dev meeting: shorten names and general agreement to add something like this |
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.
LGTM.
Nit Question:
Should your tests include the in: chisel3.Data signature?
E.g. UnsignedBitsRequired(8.U) or UnsignedBitsRequired(UInt(8.W))
Also: UnsignedBitsRequired(x) == UnsignedBitsRequired(x.W) for all x: Integer, right?
Remove |
Remove apply(Data) method. Change name(s) to signedBitLength, unsignedBitLength.
require(in >= 0) | ||
if (in == 0) { | ||
// 0.bitLength returns 0 - thank you Java. | ||
1 |
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.
Was this the behavior we wanted? (I'm sure we brought this up, I don't remember the resolution and I don't think it was written down)
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.
I believe so, but we can review at next dev meeting.
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.
Return 0, not 1.
|
||
property ("unsignedBitLength is computed correctly") { | ||
forAll(safeUIntWidth) { case (width: Int) => | ||
for ( offset <- List(-1, 0, 1)) { |
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.
I'm not sure this test is testing anything since it's basically the duplicating the code used to implement unsignedBitLength. Consider just dropping in a few values and hand-coding the result.
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.
Corrected in ec3e030
|
||
property ("signedBitLength is computed correctly") { | ||
forAll(safeUIntWidth) { case (width: Int) => | ||
for ( offset <- List(-1, 0, 1)) { |
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.
ditto
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.
Corrected in ec3e030
Independently calculate the bit length to verify correct operation.
@ducky64 thoughts on merging thisz? |
Ping @ducky64 |
Is the zero width fix implemented? It doesn't appear so? |
Thanks @ducky64 for pointing out the zero width fix hadn't yet been implemented. Should we be doing this for signed 0 as well? (Currently |
The unsigned case looks fine. The signed zero case is a good question. My thoughts for signed: I'm still not a fan of the test style since it's not simple enough to be obvious at a glance (eg, explicit testvectors) but also doesn't give you the better correctness guarantees from a more formal approach. In fact, the test code is much harder to reason about than the main code, because there's so much complexity going on. That being said, I don't care enough about this to block it. Once we resolve the signed zero, I'll give this a green. |
I agree with @ducky64, but I think there's enough confusion over this, that we shouldn't incorporate it. (I don't see how you can represent |
Zero-width wires are fine and we have rules for removing them. The advantage of representing |
But why add the constant |
I’ll also add that zero-width zeros are particularly useful for Cat-like
operations in generator code. Avoids special-casing the case that there’s
only 1 of something, i.e., when log2(number of things) = 0.
…On Thu, Jul 11, 2019 at 12:37 PM Jim Lawson ***@***.***> wrote:
But why add the constant 0.U in the first place? I would argue that this
should be dealt with in optimizations for specific operations, not as a
side-effect of the width required to store a specific constant. Similarly, x
- 0.U, x + 0.S, x - 0.S, x * 1.U, and x * 1.S should be optimized away.
—
You are receiving this because your review was requested.
Reply to this email directly, view it on GitHub
<#819>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAH3XQSGTIZH7XYS4ZI7YCLP66DV3ANCNFSM4E5MP4NQ>
.
|
Right. I keep forgetting the assumption that a zero-width wire has the value |
Resolution from today's meeting: signed 0 is 0 bits, signed -1 is 1 bit, signed 1 is 2 bits. |
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.
looks fine.
Related issue: #818
Type of change: other enhancement
Impact: API addition (no impact on existing code)
Development Phase: proposal
Release Notes
Add helper functions
UnsignedBitsRequired
andUnsignedWidthRequired
to provide an alternative to the potentially incorrect usage oflog2Ceil
to calculate wire widths.