using pool<> in bitpattern.h

This commit is contained in:
Clifford Wolf 2014-12-30 23:45:43 +01:00
parent 1909edfa9c
commit 6fef4b82a2
3 changed files with 32 additions and 14 deletions

View File

@ -29,7 +29,7 @@ struct BitPatternPool
{ {
int width; int width;
typedef std::vector<RTLIL::State> bits_t; typedef std::vector<RTLIL::State> bits_t;
std::set<bits_t> pool; pool<bits_t> database;
BitPatternPool(RTLIL::SigSpec sig) BitPatternPool(RTLIL::SigSpec sig)
{ {
@ -42,7 +42,7 @@ struct BitPatternPool
else else
pattern[i] = RTLIL::State::Sa; pattern[i] = RTLIL::State::Sa;
} }
pool.insert(pattern); database.insert(pattern);
} }
} }
@ -53,7 +53,7 @@ struct BitPatternPool
std::vector<RTLIL::State> pattern(width); std::vector<RTLIL::State> pattern(width);
for (int i = 0; i < width; i++) for (int i = 0; i < width; i++)
pattern[i] = RTLIL::State::Sa; pattern[i] = RTLIL::State::Sa;
pool.insert(pattern); database.insert(pattern);
} }
} }
@ -79,7 +79,7 @@ struct BitPatternPool
bool has_any(RTLIL::SigSpec sig) bool has_any(RTLIL::SigSpec sig)
{ {
bits_t bits = sig2bits(sig); bits_t bits = sig2bits(sig);
for (auto &it : pool) for (auto &it : database)
if (match(it, bits)) if (match(it, bits))
return true; return true;
return false; return false;
@ -88,13 +88,13 @@ struct BitPatternPool
bool has_all(RTLIL::SigSpec sig) bool has_all(RTLIL::SigSpec sig)
{ {
bits_t bits = sig2bits(sig); bits_t bits = sig2bits(sig);
for (auto &it : pool) for (auto &it : database)
if (match(it, bits)) { if (match(it, bits)) {
for (int i = 0; i < width; i++) for (int i = 0; i < width; i++)
if (bits[i] > RTLIL::State::S1 && it[i] <= RTLIL::State::S1) if (bits[i] > RTLIL::State::S1 && it[i] <= RTLIL::State::S1)
goto next_pool_entry; goto next_database_entry;
return true; return true;
next_pool_entry:; next_database_entry:;
} }
return false; return false;
} }
@ -104,17 +104,17 @@ struct BitPatternPool
bool status = false; bool status = false;
bits_t bits = sig2bits(sig); bits_t bits = sig2bits(sig);
std::vector<bits_t> pattern_list; std::vector<bits_t> pattern_list;
for (auto &it : pool) for (auto &it : database)
if (match(it, bits)) if (match(it, bits))
pattern_list.push_back(it); pattern_list.push_back(it);
for (auto pattern : pattern_list) { for (auto pattern : pattern_list) {
pool.erase(pattern); database.erase(pattern);
for (int i = 0; i < width; i++) { for (int i = 0; i < width; i++) {
if (pattern[i] != RTLIL::State::Sa || bits[i] == RTLIL::State::Sa) if (pattern[i] != RTLIL::State::Sa || bits[i] == RTLIL::State::Sa)
continue; continue;
bits_t new_pattern = pattern; bits_t new_pattern = pattern;
new_pattern[i] = bits[i] == RTLIL::State::S1 ? RTLIL::State::S0 : RTLIL::State::S1; new_pattern[i] = bits[i] == RTLIL::State::S1 ? RTLIL::State::S0 : RTLIL::State::S1;
pool.insert(new_pattern); database.insert(new_pattern);
} }
status = true; status = true;
} }
@ -123,15 +123,15 @@ struct BitPatternPool
bool take_all() bool take_all()
{ {
if (pool.empty()) if (database.empty())
return false; return false;
pool.clear(); database.clear();
return true; return true;
} }
bool empty() bool empty()
{ {
return pool.empty(); return database.empty();
} }
}; };

View File

@ -62,7 +62,8 @@ template<> struct hash_ops<int> {
bool cmp(T a, T b) const { bool cmp(T a, T b) const {
return a == b; return a == b;
} }
unsigned int hash(unsigned int a) const { template<typename T>
unsigned int hash(T a) const {
return a; return a;
} }
}; };
@ -90,6 +91,19 @@ template<typename P, typename Q> struct hash_ops<std::pair<P, Q>> {
} }
}; };
template<typename T> struct hash_ops<std::vector<T>> {
bool cmp(std::vector<T> a, std::vector<T> b) const {
return a == b;
}
unsigned int hash(std::vector<T> a) const {
hash_ops<T> t_ops;
unsigned int h = mkhash_init;
for (auto k : a)
h = mkhash(h, t_ops.hash(k));
return h;
}
};
struct hash_cstr_ops { struct hash_cstr_ops {
bool cmp(const char *a, const char *b) const { bool cmp(const char *a, const char *b) const {
for (int i = 0; a[i] || b[i]; i++) for (int i = 0; a[i] || b[i]; i++)

View File

@ -221,6 +221,10 @@ YOSYS_NAMESPACE_END
YOSYS_NAMESPACE_BEGIN YOSYS_NAMESPACE_BEGIN
namespace hashlib {
template<> struct hash_ops<RTLIL::State> : hash_ops<int> {};
}
void yosys_setup(); void yosys_setup();
void yosys_shutdown(); void yosys_shutdown();