yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
MaccmapWorker Struct Reference
+ Collaboration diagram for MaccmapWorker:

Public Member Functions

 MaccmapWorker (RTLIL::Module *module, int width)
 
void add (RTLIL::SigBit bit, int position)
 
void add (RTLIL::SigSpec a, bool is_signed, bool do_subtract)
 
void add (RTLIL::SigSpec a, RTLIL::SigSpec b, bool is_signed, bool do_subtract)
 
void fulladd (RTLIL::SigSpec &in1, RTLIL::SigSpec &in2, RTLIL::SigSpec &in3, RTLIL::SigSpec &out1, RTLIL::SigSpec &out2)
 
int tree_bit_slots (int n)
 
RTLIL::SigSpec synth ()
 

Data Fields

std::vector< std::set
< RTLIL::SigBit > > 
bits
 
RTLIL::Modulemodule
 
int width
 

Detailed Description

Definition at line 26 of file maccmap.cc.

Constructor & Destructor Documentation

MaccmapWorker::MaccmapWorker ( RTLIL::Module module,
int  width 
)
inline

Definition at line 32 of file maccmap.cc.

32  : module(module), width(width)
33  {
34  bits.resize(width);
35  }
RTLIL::Module * module
Definition: maccmap.cc:29
std::vector< std::set< RTLIL::SigBit > > bits
Definition: maccmap.cc:28

Member Function Documentation

void MaccmapWorker::add ( RTLIL::SigBit  bit,
int  position 
)
inline

Definition at line 37 of file maccmap.cc.

38  {
39  if (position >= width || bit == RTLIL::S0)
40  return;
41 
42  if (bits.at(position).count(bit)) {
43  bits.at(position).erase(bit);
44  add(bit, position+1);
45  } else {
46  bits.at(position).insert(bit);
47  }
48  }
std::vector< std::set< RTLIL::SigBit > > bits
Definition: maccmap.cc:28
void add(RTLIL::SigBit bit, int position)
Definition: maccmap.cc:37

+ Here is the caller graph for this function:

void MaccmapWorker::add ( RTLIL::SigSpec  a,
bool  is_signed,
bool  do_subtract 
)
inline

Definition at line 50 of file maccmap.cc.

51  {
52  a.extend(width, is_signed);
53 
54  if (do_subtract) {
55  a = module->Not(NEW_ID, a);
56  add(RTLIL::S1, 0);
57  }
58 
59  for (int i = 0; i < width; i++)
60  add(a[i], i);
61  }
#define NEW_ID
Definition: yosys.h:166
RTLIL::Module * module
Definition: maccmap.cc:29
void extend(int width, bool is_signed=false)
Definition: rtlil.cc:2593
void add(RTLIL::SigBit bit, int position)
Definition: maccmap.cc:37
RTLIL::SigSpec Not(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed=false)

+ Here is the call graph for this function:

void MaccmapWorker::add ( RTLIL::SigSpec  a,
RTLIL::SigSpec  b,
bool  is_signed,
bool  do_subtract 
)
inline

Definition at line 63 of file maccmap.cc.

64  {
65  if (GetSize(a) < GetSize(b))
66  std::swap(a, b);
67 
68  a.extend(width, is_signed);
69 
70  if (GetSize(b) > width)
71  b.extend(width, is_signed);
72 
73  for (int i = 0; i < GetSize(b); i++)
74  if (is_signed && i+1 == GetSize(b))
75  {
76  a = {module->Not(NEW_ID, a.extract(i, width-i)), RTLIL::SigSpec(0, i)};
77  add(module->And(NEW_ID, a, RTLIL::SigSpec(b[i], width)), false, do_subtract);
78  add({b[i], RTLIL::SigSpec(0, i)}, false, do_subtract);
79  }
80  else
81  {
82  add(module->And(NEW_ID, a, RTLIL::SigSpec(b[i], width)), false, do_subtract);
83  a = {a.extract(0, width-1), RTLIL::S0};
84  }
85  }
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
#define NEW_ID
Definition: yosys.h:166
RTLIL::Module * module
Definition: maccmap.cc:29
RTLIL::SigSpec extract(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec *other=NULL) const
Definition: rtlil.cc:2414
RTLIL::SigSpec And(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, bool is_signed=false)
void extend(int width, bool is_signed=false)
Definition: rtlil.cc:2593
void add(RTLIL::SigBit bit, int position)
Definition: maccmap.cc:37
RTLIL::SigSpec Not(RTLIL::IdString name, RTLIL::SigSpec sig_a, bool is_signed=false)

+ Here is the call graph for this function:

void MaccmapWorker::fulladd ( RTLIL::SigSpec in1,
RTLIL::SigSpec in2,
RTLIL::SigSpec in3,
RTLIL::SigSpec out1,
RTLIL::SigSpec out2 
)
inline

Definition at line 87 of file maccmap.cc.

88  {
89  int start_index = 0, stop_index = GetSize(in1);
90 
91  while (start_index < stop_index && in1[start_index] == RTLIL::S0 && in2[start_index] == RTLIL::S0 && in3[start_index] == RTLIL::S0)
92  start_index++;
93 
94  while (start_index < stop_index && in1[stop_index-1] == RTLIL::S0 && in2[stop_index-1] == RTLIL::S0 && in3[stop_index-1] == RTLIL::S0)
95  stop_index--;
96 
97  if (start_index == stop_index)
98  {
99  out1 = RTLIL::SigSpec(0, GetSize(in1));
100  out2 = RTLIL::SigSpec(0, GetSize(in1));
101  }
102  else
103  {
104  RTLIL::SigSpec out_zeros_lsb(0, start_index), out_zeros_msb(0, GetSize(in1)-stop_index);
105 
106  in1 = in1.extract(start_index, stop_index-start_index);
107  in2 = in2.extract(start_index, stop_index-start_index);
108  in3 = in3.extract(start_index, stop_index-start_index);
109 
110  int width = GetSize(in1);
111  RTLIL::Wire *w1 = module->addWire(NEW_ID, width);
112  RTLIL::Wire *w2 = module->addWire(NEW_ID, width);
113 
114  RTLIL::Cell *cell = module->addCell(NEW_ID, "$fa");
115  cell->setParam("\\WIDTH", width);
116  cell->setPort("\\A", in1);
117  cell->setPort("\\B", in2);
118  cell->setPort("\\C", in3);
119  cell->setPort("\\Y", w1);
120  cell->setPort("\\X", w2);
121 
122  out1 = {out_zeros_msb, w1, out_zeros_lsb};
123  out2 = {out_zeros_msb, w2, out_zeros_lsb};
124  }
125  }
RTLIL::Cell * addCell(RTLIL::IdString name, RTLIL::IdString type)
Definition: rtlil.cc:1353
void setParam(RTLIL::IdString paramname, RTLIL::Const value)
Definition: rtlil.cc:1829
void setPort(RTLIL::IdString portname, RTLIL::SigSpec signal)
Definition: rtlil.cc:1789
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
RTLIL::Wire * addWire(RTLIL::IdString name, int width=1)
Definition: rtlil.cc:1331
#define NEW_ID
Definition: yosys.h:166
RTLIL::Module * module
Definition: maccmap.cc:29
RTLIL::SigSpec extract(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec *other=NULL) const
Definition: rtlil.cc:2414

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RTLIL::SigSpec MaccmapWorker::synth ( )
inline

Definition at line 141 of file maccmap.cc.

142  {
143  std::vector<RTLIL::SigSpec> summands;
144  std::vector<RTLIL::SigBit> tree_sum_bits;
145  int unique_tree_bits = 0;
146  int count_tree_words = 0;
147 
148  while (1)
149  {
150  RTLIL::SigSpec summand(0, width);
151  bool got_data_bits = false;
152 
153  for (int i = 0; i < width; i++)
154  if (!bits.at(i).empty()) {
155  auto it = bits.at(i).begin();
156  summand[i] = *it;
157  bits.at(i).erase(it);
158  got_data_bits = true;
159  }
160 
161  if (!got_data_bits)
162  break;
163 
164  summands.push_back(summand);
165 
166  while (1)
167  {
168  int free_bit_slots = tree_bit_slots(GetSize(summands)) - GetSize(tree_sum_bits);
169 
170  int max_depth = 0, max_position = 0;
171  for (int i = 0; i < width; i++)
172  if (max_depth <= GetSize(bits.at(i))) {
173  max_depth = GetSize(bits.at(i));
174  max_position = i;
175  }
176 
177  if (max_depth == 0 || max_position > 4)
178  break;
179 
180  int required_bits = 0;
181  for (int i = 0; i <= max_position; i++)
182  if (GetSize(bits.at(i)) == max_depth)
183  required_bits += 1 << i;
184 
185  if (required_bits > free_bit_slots)
186  break;
187 
188  for (int i = 0; i <= max_position; i++)
189  if (GetSize(bits.at(i)) == max_depth) {
190  auto it = bits.at(i).begin();
191  RTLIL::SigBit bit = *it;
192  for (int k = 0; k < (1 << i); k++, free_bit_slots--)
193  tree_sum_bits.push_back(bit);
194  bits.at(i).erase(it);
195  unique_tree_bits++;
196  }
197 
198  count_tree_words++;
199  }
200  }
201 
202  if (!tree_sum_bits.empty())
203  log(" packed %d (%d) bits / %d words into adder tree\n", GetSize(tree_sum_bits), unique_tree_bits, count_tree_words);
204 
205  if (GetSize(summands) == 0) {
206  log_assert(tree_sum_bits.empty());
207  return RTLIL::SigSpec(0, width);
208  }
209 
210  if (GetSize(summands) == 1) {
211  log_assert(tree_sum_bits.empty());
212  return summands.front();
213  }
214 
215  while (GetSize(summands) > 2)
216  {
217  std::vector<RTLIL::SigSpec> new_summands;
218  for (int i = 0; i < GetSize(summands); i += 3)
219  if (i+2 < GetSize(summands)) {
220  RTLIL::SigSpec in1 = summands[i];
221  RTLIL::SigSpec in2 = summands[i+1];
222  RTLIL::SigSpec in3 = summands[i+2];
223  RTLIL::SigSpec out1, out2;
224  fulladd(in1, in2, in3, out1, out2);
225  RTLIL::SigBit extra_bit = RTLIL::S0;
226  if (!tree_sum_bits.empty()) {
227  extra_bit = tree_sum_bits.back();
228  tree_sum_bits.pop_back();
229  }
230  new_summands.push_back(out1);
231  new_summands.push_back({out2.extract(0, width-1), extra_bit});
232  } else {
233  new_summands.push_back(summands[i]);
234  i -= 2;
235  }
236  summands.swap(new_summands);
237  }
238 
239 
240  RTLIL::Cell *c = module->addCell(NEW_ID, "$alu");
241  c->setPort("\\A", summands.front());
242  c->setPort("\\B", summands.back());
243  c->setPort("\\CI", RTLIL::S0);
244  c->setPort("\\BI", RTLIL::S0);
245  c->setPort("\\Y", module->addWire(NEW_ID, width));
246  c->setPort("\\X", module->addWire(NEW_ID, width));
247  c->setPort("\\CO", module->addWire(NEW_ID, width));
248  c->fixup_parameters();
249 
250  if (!tree_sum_bits.empty()) {
251  c->setPort("\\CI", tree_sum_bits.back());
252  tree_sum_bits.pop_back();
253  }
254  log_assert(tree_sum_bits.empty());
255 
256  return c->getPort("\\Y");
257  }
void fixup_parameters(bool set_a_signed=false, bool set_b_signed=false)
Definition: rtlil.cc:1847
int tree_bit_slots(int n)
Definition: maccmap.cc:127
RTLIL::Cell * addCell(RTLIL::IdString name, RTLIL::IdString type)
Definition: rtlil.cc:1353
void setPort(RTLIL::IdString portname, RTLIL::SigSpec signal)
Definition: rtlil.cc:1789
void fulladd(RTLIL::SigSpec &in1, RTLIL::SigSpec &in2, RTLIL::SigSpec &in3, RTLIL::SigSpec &out1, RTLIL::SigSpec &out2)
Definition: maccmap.cc:87
const RTLIL::SigSpec & getPort(RTLIL::IdString portname) const
Definition: rtlil.cc:1809
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
#define log_assert(_assert_expr_)
Definition: log.h:85
RTLIL::Wire * addWire(RTLIL::IdString name, int width=1)
Definition: rtlil.cc:1331
#define NEW_ID
Definition: yosys.h:166
RTLIL::Module * module
Definition: maccmap.cc:29
void log(const char *format,...)
Definition: log.cc:180
RTLIL::SigSpec extract(const RTLIL::SigSpec &pattern, const RTLIL::SigSpec *other=NULL) const
Definition: rtlil.cc:2414
std::vector< std::set< RTLIL::SigBit > > bits
Definition: maccmap.cc:28

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int MaccmapWorker::tree_bit_slots ( int  n)
inline

Definition at line 127 of file maccmap.cc.

128  {
129  #if 0
130  int retval = 1;
131  while (n > 2) {
132  retval += n / 3;
133  n = 2*(n / 3) + (n % 3);
134  }
135  return retval;
136  #else
137  return std::max(n - 1, 0);
138  #endif
139  }
tuple n
Definition: fsm/generate.py:59

+ Here is the caller graph for this function:

Field Documentation

std::vector<std::set<RTLIL::SigBit> > MaccmapWorker::bits

Definition at line 28 of file maccmap.cc.

RTLIL::Module* MaccmapWorker::module

Definition at line 29 of file maccmap.cc.

int MaccmapWorker::width

Definition at line 30 of file maccmap.cc.


The documentation for this struct was generated from the following file: