-
Notifications
You must be signed in to change notification settings - Fork 894
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
rtlil: add Const::compress helper function #4608
Conversation
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.
Thanks! Some details need fixing
kernel/rtlil.cc
Outdated
@@ -280,6 +280,32 @@ int RTLIL::Const::as_int(bool is_signed) const | |||
return ret; | |||
} | |||
|
|||
void RTLIL::Const::compress(bool is_signed) |
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.
What's the motivation for not respecting/setting flags & CONST_FLAG_SIGNED
?
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 am not sure we want to defer to that. That flag isn't set to a useful value in all contexts, especially when you convert a SigSpec
with as_const()
, which is the use case which prompted writing the helper.
Should I also add a |
For that one to be useful you need to know when you can trust the result and when not, so maybe it should return a pair of |
That's what |
If it is larger than 32-bit then it fails as it is not representable but yes, std::optional is a good option there. |
Does this solution look reasonable? |
As far as I can tell, it does |
Compresses the current bits to the minimum width representation by removing leading bits.
9951124
to
c53c87e
Compare
If one has a Sigspec that is fully constant it is currently non-trivial to get its value.
The main blocking point here is that the SigSpec may be wider than 32bit (an integer).
However, this doesn't mean the value stored in it is necessarily this large/small.
Eg something like
33'd4
or33'd-2
cannot be easily converted to an int.This adds a
compress(is_signed)
function.It tries to minimize the number of bits used for the representation of the number.
It does so by removing the leading bits while taking signedness into consideration.
With this function, the above can be successfully converted to an integer by first calling compress.
Edit:
Here is a possible application.
https://github.com/povik/yosys-slang/blob/28cea56840a6bc8a4b6398d36609500d5960ad79/src/builder.cc#L134
With
compress
it is easy to check if the actual value has a larger representation than 24-bits instead of checking the SigSpec, which again may not use the minimum width.I am fairly convinced there are places in the Yosys codebase itself this would be helpful, finding them is a bit difficult.