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

Improve ezsat onehot encoding scheme

Signed-off-by: Claire Wolf <claire@symbioticeda.com>
This commit is contained in:
Claire Wolf 2020-04-02 12:22:28 +02:00
parent f72b65b2a5
commit 65a3ff69bd

View file

@ -1371,6 +1371,19 @@ int ezSAT::onehot(const std::vector<int> &vec, bool max_only)
if (max_only == false) if (max_only == false)
formula.push_back(expression(OpOr, vec)); formula.push_back(expression(OpOr, vec));
if (vec.size() < 8)
{
// fall-back to simple O(n^2) solution for small cases
for (size_t i = 0; i < vec.size(); i++)
for (size_t j = i+1; j < vec.size(); j++) {
std::vector<int> clause;
clause.push_back(NOT(vec[i]));
clause.push_back(NOT(vec[j]));
formula.push_back(expression(OpOr, clause));
}
}
else
{
// create binary vector // create binary vector
int num_bits = clog2(vec.size()); int num_bits = clog2(vec.size());
std::vector<int> bits; std::vector<int> bits;
@ -1385,6 +1398,7 @@ int ezSAT::onehot(const std::vector<int> &vec, bool max_only)
clause.push_back((i & (1 << k)) != 0 ? bits[k] : NOT(bits[k])); clause.push_back((i & (1 << k)) != 0 ? bits[k] : NOT(bits[k]));
formula.push_back(expression(OpOr, clause)); formula.push_back(expression(OpOr, clause));
} }
}
return expression(OpAnd, formula); return expression(OpAnd, formula);
} }