3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-09-12 12:41:28 +00:00

Make Const::Const(long long) constructor use packed bits internally if possible

This commit is contained in:
Robert O'Callahan 2025-08-29 03:08:31 +00:00
parent 31fc0f53e5
commit e206b059f6
3 changed files with 52 additions and 1 deletions

View file

@ -221,9 +221,34 @@ RTLIL::Const::Const(const std::string &str)
tag = backing_tag::string;
}
RTLIL::Const::Const(long long val) // default width 32
{
flags = RTLIL::CONST_FLAG_NONE;
char bytes[] = {
(char)(val >> 24), (char)(val >> 16), (char)(val >> 8), (char)val
};
new ((void*)&str_) std::string(bytes, 4);
tag = backing_tag::string;
}
RTLIL::Const::Const(long long val, int width)
{
flags = RTLIL::CONST_FLAG_NONE;
if ((width & 7) == 0) {
new ((void*)&str_) std::string();
tag = backing_tag::string;
std::string& str = get_str();
int bytes = width >> 3;
signed char sign_byte = val < 0 ? -1 : 0;
str.resize(bytes, sign_byte);
bytes = std::min<int>(bytes, sizeof(val));
for (int i = 0; i < bytes; i++) {
str[str.size() - 1 - i] = val;
val = val >> 8;
}
return;
}
new ((void*)&bits_) bitvectype();
tag = backing_tag::bits;
bitvectype& bv = get_bits();

View file

@ -744,7 +744,8 @@ private:
public:
Const() : flags(RTLIL::CONST_FLAG_NONE), tag(backing_tag::bits), bits_(std::vector<RTLIL::State>()) {}
Const(const std::string &str);
Const(long long val, int width = 32);
Const(long long val); // default width is 32
Const(long long val, int width);
Const(RTLIL::State bit, int width = 1);
Const(std::vector<RTLIL::State> bits) : flags(RTLIL::CONST_FLAG_NONE), tag(backing_tag::bits), bits_(std::move(bits)) {}
Const(const std::vector<bool> &bits);

View file

@ -88,6 +88,31 @@ namespace RTLIL {
EXPECT_TRUE(cs1.is_bits());
}
{
Const c(0x12345678);
EXPECT_TRUE(c.is_str());
EXPECT_EQ(c.as_int(), 0x12345678);
}
{
Const c(0xab, 8);
EXPECT_TRUE(c.is_str());
EXPECT_EQ(c.as_int(), 0xab);
}
{
Const c(0x12345678, 80);
EXPECT_TRUE(c.is_str());
EXPECT_EQ(c.as_int(), 0x12345678);
EXPECT_EQ(c[79], S0);
}
{
Const c(-1, 80);
EXPECT_TRUE(c.is_str());
EXPECT_EQ(c.as_int(), -1);
EXPECT_EQ(c[79], S1);
}
}
TEST_F(KernelRtlilTest, ConstConstIteratorWorks) {