3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2026-07-18 05:05:45 +00:00
This commit is contained in:
nella 2026-07-13 17:37:32 +02:00 committed by GitHub
commit 3f44ddff33
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 65 additions and 1 deletions

View file

@ -1,4 +1,5 @@
#include <gtest/gtest.h>
#include <limits>
#include "kernel/rtlil.h"
YOSYS_NAMESPACE_BEGIN
@ -35,6 +36,39 @@ namespace RTLIL {
EXPECT_FALSE(c2 < c3);
}
TEST_F(KernelRtlilTest, ConstConvertibleToInt) {
// A Const whose minimal unsigned size is exactly 32 bits (bit 31 set)
// but whose full width is larger must not be reported as convertible
// to a (signed) int when treated as unsigned
{
// 33 bits: bits 0..31 = 1, bit 32 = 0 => value 0xffffffff
std::vector<State> v(33, S1);
v[32] = S0;
Const c(v);
EXPECT_FALSE(c.convertible_to_int(false));
EXPECT_EQ(c.as_int_saturating(false), std::numeric_limits<int>::max());
EXPECT_FALSE(c.convertible_to_int(true));
EXPECT_EQ(c.as_int_saturating(true), std::numeric_limits<int>::max());
}
{
// 32-bit unsigned 0x7fffffff fits in a signed int
std::vector<State> v(32, S1);
v[31] = S0;
Const c(v);
EXPECT_TRUE(c.convertible_to_int(false));
EXPECT_EQ(c.as_int(false), 0x7fffffff);
}
{
// 32-bit unsigned 0xffffffff does not fit in a signed int
std::vector<State> v(32, S1);
Const c(v);
EXPECT_FALSE(c.convertible_to_int(false));
EXPECT_EQ(c.as_int_saturating(false), std::numeric_limits<int>::max());
}
}
TEST_F(KernelRtlilTest, ConstStr) {
// We have multiple distinct sections since it's annoying
// to list multiple testcases as friends of Const in kernel/rtlil.h