mirror of https://github.com/YosysHQ/yosys.git
rtlil: add Const::compress helper function
Compresses the current bits to the minimum width representation by removing leading bits.
This commit is contained in:
parent
038e262332
commit
4cd2e04da4
|
@ -280,6 +280,30 @@ int RTLIL::Const::as_int(bool is_signed) const
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RTLIL::Const::compress(bool is_signed)
|
||||||
|
{
|
||||||
|
if (bits.empty()) return;
|
||||||
|
|
||||||
|
// back to front (MSB to LSB)
|
||||||
|
RTLIL::State leading_bit;
|
||||||
|
if(is_signed)
|
||||||
|
leading_bit = (bits.back() == RTLIL::State::Sx) ? RTLIL::State::S0 : bits.back();
|
||||||
|
else
|
||||||
|
leading_bit = RTLIL::State::S0;
|
||||||
|
|
||||||
|
size_t idx = bits.size();
|
||||||
|
while (idx > 0 && bits[idx -1] == leading_bit) {
|
||||||
|
--idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
// signed needs one leading bit
|
||||||
|
if (is_signed && idx < bits.size()) {
|
||||||
|
++idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
bits.erase(bits.begin() + idx, bits.end());
|
||||||
|
}
|
||||||
|
|
||||||
std::string RTLIL::Const::as_string() const
|
std::string RTLIL::Const::as_string() const
|
||||||
{
|
{
|
||||||
std::string ret;
|
std::string ret;
|
||||||
|
|
|
@ -703,6 +703,9 @@ struct RTLIL::Const
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// compress representation to the minimum required bits
|
||||||
|
void compress(bool is_signed = false);
|
||||||
|
|
||||||
void extu(int width) {
|
void extu(int width) {
|
||||||
bits.resize(width, RTLIL::State::S0);
|
bits.resize(width, RTLIL::State::S0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue