yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
bitpattern.h
Go to the documentation of this file.
1 /*
2  * yosys -- Yosys Open SYnthesis Suite
3  *
4  * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  */
19 
20 #ifndef BITPATTERN_H
21 #define BITPATTERN_H
22 
23 #include "kernel/log.h"
24 #include "kernel/rtlil.h"
25 
27 
29 {
30  int width;
31  typedef std::vector<RTLIL::State> bits_t;
32  std::set<bits_t> pool;
33 
35  {
36  width = sig.size();
37  if (width > 0) {
38  std::vector<RTLIL::State> pattern(width);
39  for (int i = 0; i < width; i++) {
40  if (sig[i].wire == NULL && sig[i].data <= RTLIL::State::S1)
41  pattern[i] = sig[i].data;
42  else
43  pattern[i] = RTLIL::State::Sa;
44  }
45  pool.insert(pattern);
46  }
47  }
48 
50  {
51  this->width = width;
52  if (width > 0) {
53  std::vector<RTLIL::State> pattern(width);
54  for (int i = 0; i < width; i++)
55  pattern[i] = RTLIL::State::Sa;
56  pool.insert(pattern);
57  }
58  }
59 
61  {
62  bits_t bits = sig.as_const().bits;
63  for (auto &b : bits)
64  if (b > RTLIL::State::S1)
65  b = RTLIL::State::Sa;
66  return bits;
67  }
68 
69  bool match(bits_t a, bits_t b)
70  {
71  log_assert(int(a.size()) == width);
72  log_assert(int(b.size()) == width);
73  for (int i = 0; i < width; i++)
74  if (a[i] <= RTLIL::State::S1 && b[i] <= RTLIL::State::S1 && a[i] != b[i])
75  return false;
76  return true;
77  }
78 
80  {
81  bits_t bits = sig2bits(sig);
82  for (auto &it : pool)
83  if (match(it, bits))
84  return true;
85  return false;
86  }
87 
89  {
90  bits_t bits = sig2bits(sig);
91  for (auto &it : pool)
92  if (match(it, bits)) {
93  for (int i = 0; i < width; i++)
94  if (bits[i] > RTLIL::State::S1 && it[i] <= RTLIL::State::S1)
95  goto next_pool_entry;
96  return true;
97  next_pool_entry:;
98  }
99  return false;
100  }
101 
103  {
104  bool status = false;
105  bits_t bits = sig2bits(sig);
106  std::vector<bits_t> pattern_list;
107  for (auto &it : pool)
108  if (match(it, bits))
109  pattern_list.push_back(it);
110  for (auto pattern : pattern_list) {
111  pool.erase(pattern);
112  for (int i = 0; i < width; i++) {
113  if (pattern[i] != RTLIL::State::Sa || bits[i] == RTLIL::State::Sa)
114  continue;
115  bits_t new_pattern = pattern;
116  new_pattern[i] = bits[i] == RTLIL::State::S1 ? RTLIL::State::S0 : RTLIL::State::S1;
117  pool.insert(new_pattern);
118  }
119  status = true;
120  }
121  return status;
122  }
123 
124  bool take_all()
125  {
126  if (pool.empty())
127  return false;
128  pool.clear();
129  return true;
130  }
131 
132  bool empty()
133  {
134  return pool.empty();
135  }
136 };
137 
139 
140 #endif
bool take_all()
Definition: bitpattern.h:124
#define YOSYS_NAMESPACE_END
Definition: yosys.h:100
RTLIL::Const as_const() const
Definition: rtlil.cc:2857
BitPatternPool(RTLIL::SigSpec sig)
Definition: bitpattern.h:34
std::set< bits_t > pool
Definition: bitpattern.h:32
int size() const
Definition: rtlil.h:1019
std::vector< RTLIL::State > bits_t
Definition: bitpattern.h:31
#define log_assert(_assert_expr_)
Definition: log.h:85
bool match(bits_t a, bits_t b)
Definition: bitpattern.h:69
#define NULL
#define YOSYS_NAMESPACE_BEGIN
Definition: yosys.h:99
bool has_all(RTLIL::SigSpec sig)
Definition: bitpattern.h:88
BitPatternPool(int width)
Definition: bitpattern.h:49
std::vector< RTLIL::State > bits
Definition: rtlil.h:438
bits_t sig2bits(RTLIL::SigSpec sig)
Definition: bitpattern.h:60
bool take(RTLIL::SigSpec sig)
Definition: bitpattern.h:102
bool has_any(RTLIL::SigSpec sig)
Definition: bitpattern.h:79