CMS 3D CMS Logo

imath_HLS.cc
Go to the documentation of this file.
2 
3 using namespace trklet;
4 
5 void VarInv::writeLUT(std::ofstream& fs, HLS) const {
6  for (int i = 0; i < Nelements_ - 1; ++i)
7  fs << "0x" << std::hex << (LUT[i] & ((1 << nbits_) - 1)) << std::dec << ",\n";
8  fs << "0x" << std::hex << (LUT[Nelements_ - 1] & ((1 << nbits_) - 1)) << std::dec << "\n";
9 }
10 
11 void VarBase::print_truncation(std::string& t, const std::string& o1, const int ps, HLS) const {
12  if (ps > 0) {
13  t += "const ap_int<" + itos(nbits_ + ps) + "> " + name_ + "_tmp = " + o1 + ";\n";
14  t += "const ap_int<" + itos(nbits_) + "> " + name_ + " = " + name_ + "_tmp >> " + itos(ps) + ";\n";
15  } else
16  t += "const ap_int<" + itos(nbits_) + "> " + name_ + " = " + o1 + ";\n";
17 }
18 
19 //
20 // print functions
21 //
22 
23 void VarCut::print(std::map<const VarBase*, std::set<std::string> >& cut_strings,
24  const int step,
25  HLS,
26  const std::map<const VarBase*, std::set<std::string> >* const previous_cut_strings) const {
27  assert(step > -1);
29 
30  const int lower_cut = lower_cut_ / cut_var_->K();
31  const int upper_cut = upper_cut_ / cut_var_->K();
32  if (!previous_cut_strings || (previous_cut_strings && !previous_cut_strings->count(cut_var_))) {
33  if (!cut_strings.count(cut_var_))
34  cut_strings[cut_var_];
35  cut_strings.at(cut_var_).insert(name + " > " + itos(lower_cut) + " && " + name + " < " + itos(upper_cut));
36  }
37 }
38 
39 void VarBase::print_cuts(std::map<const VarBase*, std::set<std::string> >& cut_strings,
40  const int step,
41  HLS,
42  const std::map<const VarBase*, std::set<std::string> >* const previous_cut_strings) const {
43  if (p1_)
44  p1_->print_cuts(cut_strings, step, hls, previous_cut_strings);
45  if (p2_)
46  p2_->print_cuts(cut_strings, step, hls, previous_cut_strings);
47  if (p3_)
48  p3_->print_cuts(cut_strings, step, hls, previous_cut_strings);
49 
51 
52  for (const auto& cut : cuts_) {
53  const VarCut* const cast_cut = (VarCut*)cut;
54  const int lower_cut = cast_cut->lower_cut() / K_;
55  const int upper_cut = cast_cut->upper_cut() / K_;
56  if (!previous_cut_strings || (previous_cut_strings && !previous_cut_strings->count(this))) {
57  if (!cut_strings.count(this))
58  cut_strings[this];
59  cut_strings.at(this).insert(name + " > " + itos(lower_cut) + " && " + name + " < " + itos(upper_cut));
60  }
61  }
62 }
63 
64 void VarAdjustK::print(std::ofstream& fs, HLS, int l1, int l2, int l3) {
65  assert(p1_);
66  assert(l1 == 0);
67  assert(l2 == 0);
68  assert(l3 == 0);
69 
70  std::string shift = "";
71  if (lr_ > 0)
72  shift = " >> " + itos(lr_);
73  else if (lr_ < 0)
74  shift = " << " + itos(-lr_);
75 
76  std::string n1 = p1_->name();
77 
78  fs << "// " << nbits_ << " bits \t " << kstring() << "\t" << K_ << "\n";
79  fs << "const ap_int<" << nbits_ << "> " << name_ << " = " << n1 << shift << ";\n";
80 }
81 
82 void VarAdjustKR::print(std::ofstream& fs, HLS, int l1, int l2, int l3) {
83  assert(p1_);
84  assert(l1 == 0);
85  assert(l2 == 0);
86  assert(l3 == 0);
87 
88  std::string n1 = p1_->name();
89 
90  std::string o1 = n1;
91  if (lr_ == 1)
92  o1 = "(" + o1 + "+1)>>1";
93  if (lr_ > 1)
94  o1 = "( (" + o1 + ">>" + itos(lr_ - 1) + ")+1)>>1";
95  if (lr_ < 0)
96  o1 = "ap_int<" + itos(nbits_) + ">(" + o1 + ")<<" + itos(-lr_);
97 
98  fs << "// " << nbits_ << " bits \t " << kstring() << "\t" << K_ << "\n";
99  fs << "const ap_int<" << nbits_ << "> " << name_ << " = " << o1 << ";\n";
100 }
101 
102 void VarDef::print(std::ofstream& fs, HLS, int l1, int l2, int l3) {
103  assert(l1 == 0);
104  assert(l2 == 0);
105  assert(l3 == 0);
106 
107  fs << "// units " << kstring() << "\t" << K_ << "\n";
108  fs << "const ap_int<" << nbits_ << "> " << name_ << " = " << name_ << "_wire;\n";
109 }
110 
111 void VarParam::print(std::ofstream& fs, HLS, int l1, int l2, int l3) {
112  assert(l1 == 0);
113  assert(l2 == 0);
114  assert(l3 == 0);
115 
116  fs << "// " << nbits_ << " bits \t " << kstring() << "\t" << K_ << "\n";
117  fs << "static const ap_int<" << nbits_ << "> " << name_ << " = " << ival_ << ";\n";
118 }
119 
120 void VarAdd::print(std::ofstream& fs, HLS, int l1, int l2, int l3) {
121  assert(p1_);
122  assert(p2_);
123  assert(l1 == 0);
124  assert(l2 == 0);
125  assert(l3 == 0);
126  std::string o1 = p1_->name();
127  if (shift1 > 0) {
128  o1 = "ap_int<" + itos(nbits_ + ps_) + ">(" + o1 + ")";
129  o1 += "<<" + itos(shift1);
130  o1 = "(" + o1 + ")";
131  }
132 
133  std::string o2 = p2_->name();
134  if (shift2 > 0) {
135  o2 = "ap_int<" + itos(nbits_ + ps_) + ">(" + o2 + ")";
136  o2 += "<<" + itos(shift2);
137  o2 = "(" + o2 + ")";
138  }
139 
140  o1 = o1 + " + " + o2;
141 
142  std::string t = "";
144  fs << "// " << nbits_ << " bits \t " << kstring() << "\t" << K_ << "\n" << t;
145 }
146 
147 void VarSubtract::print(std::ofstream& fs, HLS, int l1, int l2, int l3) {
148  assert(p1_);
149  assert(p2_);
150  assert(l1 == 0);
151  assert(l2 == 0);
152  assert(l3 == 0);
153  std::string o1 = p1_->name();
154  if (shift1 > 0) {
155  o1 = "ap_int<" + itos(nbits_ + ps_) + ">(" + o1 + ")";
156  o1 += "<<" + itos(shift1);
157  o1 = "(" + o1 + ")";
158  }
159 
160  std::string o2 = p2_->name();
161  if (shift2 > 0) {
162  o2 = "ap_int<" + itos(nbits_ + ps_) + ">(" + o2 + ")";
163  o2 += "<<" + itos(shift2);
164  o2 = "(" + o2 + ")";
165  }
166 
167  o1 = o1 + " - " + o2;
168 
169  std::string t = "";
171  fs << "// " << nbits_ << " bits \t " << kstring() << "\t" << K_ << "\n" << t;
172 }
173 
174 void VarNounits::print(std::ofstream& fs, HLS, int l1, int l2, int l3) {
175  assert(p1_);
176  assert(l1 == 0);
177  assert(l2 == 0);
178  assert(l3 == 0);
179  std::string n1 = p1_->name();
180  std::string o1 = "(" + n1 + " * " + itos(cI_) + ")";
181 
182  std::string t = "";
184  fs << "// " << nbits_ << " bits \t " << kstring() << "\t" << K_ << "\n" << t;
185 }
186 
187 void VarTimesC::print(std::ofstream& fs, HLS, int l1, int l2, int l3) {
188  assert(p1_);
189  assert(l1 == 0);
190  assert(l2 == 0);
191  assert(l3 == 0);
192  std::string n1 = p1_->name();
193  std::string o1 = "(" + n1 + " * " + itos(cI_) + ")";
194 
195  std::string t = "";
197  fs << "// " << nbits_ << " bits \t " << kstring() << "\t" << K_ << "\n" << t;
198 }
199 
200 void VarNeg::print(std::ofstream& fs, HLS, int l1, int l2, int l3) {
201  assert(p1_);
202  assert(l1 == 0);
203  assert(l2 == 0);
204  assert(l3 == 0);
205  std::string n1 = p1_->name();
206 
207  std::string t = "const ap_int<" + itos(nbits_) + "> " + name_ + " = -" + n1 + ";\n";
208  fs << "// " << nbits_ << " bits \t " << kstring() << "\t" << K_ << "\n" << t << ";\n";
209 }
210 
211 void VarShiftround::print(std::ofstream& fs, HLS, int l1, int l2, int l3) {
212  assert(p1_);
213  assert(l1 == 0);
214  assert(l2 == 0);
215  assert(l3 == 0);
216  std::string n1 = p1_->name();
217  std::string o1 = n1;
218  if (shift_ == 1)
219  o1 = "(" + o1 + "+1)>>1";
220  if (shift_ > 1)
221  o1 = "( (" + o1 + ">>" + itos(shift_ - 1) + ")+1)>>1";
222  if (shift_ < 0)
223  o1 = "ap_int<" + itos(nbits_) + ">(" + o1 + ")<<" + itos(-shift_);
224 
225  std::string t = "const ap_int<" + itos(nbits_) + "> " + name_ + " = " + o1 + ";\n";
226  fs << "// " << nbits_ << " bits \t " << kstring() << "\t" << K_ << "\n" << t << ";\n";
227 }
228 
229 void VarShift::print(std::ofstream& fs, HLS, int l1, int l2, int l3) {
230  assert(p1_);
231  assert(l1 == 0);
232  assert(l2 == 0);
233  assert(l3 == 0);
234  std::string n1 = p1_->name();
235  std::string o1 = n1;
236  if (shift_ > 0)
237  o1 = o1 + ">>" + itos(shift_);
238  if (shift_ < 0)
239  o1 = "ap_int<" + itos(nbits_) + ">(" + o1 + ")<<" + itos(-shift_);
240 
241  std::string t = "const ap_int<" + itos(nbits_) + "> " + name_ + " = " + o1 + ";\n";
242  fs << "// " << nbits_ << " bits \t " << kstring() << "\t" << K_ << "\n" << t << ";\n";
243 }
244 
245 void VarMult::print(std::ofstream& fs, HLS, int l1, int l2, int l3) {
246  assert(l1 == 0);
247  assert(l2 == 0);
248  assert(l3 == 0);
249  assert(p1_);
250  std::string n1 = p1_->name();
251  assert(p2_);
252  std::string n2 = p2_->name();
253  std::string o1 = n1 + " * " + n2;
254 
255  std::string t = "";
257  fs << "// " << nbits_ << " bits \t " << kstring() << "\t" << K_ << "\n" << t;
258 }
259 
260 void VarInv::print(std::ofstream& fs, HLS, int l1, int l2, int l3) {
261  assert(p1_);
262  assert(l1 == 0);
263  assert(l2 == 0);
264  assert(l3 == 0);
265 
266  fs << "static const ap_int<" << itos(nbits_) << "> LUT_" << name_ << "[" << Nelements_ << "] = {\n";
267  fs << "#include \"LUT_" << name_ << ".h\"\n";
268  fs << "};\n";
269 
270  std::string n1 = p1_->name();
271  //first calculate address
272  std::string t1 = "addr_" + name_;
273  std::string t = "const ap_uint<" + itos(nbaddr_) + "> " + t1 + " = ";
274  if (shift_ > 0)
275  t = t + "(" + n1 + ">>" + itos(shift_) + ") & " + itos(mask_);
276  else
277  t = t + n1 + " & " + itos(mask_);
278  fs << t << "; // address for the LUT\n";
279 
280  t = "const ap_int<" + itos(nbits_) + "> " + name_ + " = LUT_" + name_ + "[addr_" + name_ + "];\n";
281  fs << t;
282 }
283 
284 void VarFlag::print(std::ofstream& fs, HLS, int l1, int l2, int l3) {
285  assert(l1 == 0);
286  assert(l2 == 0);
287  assert(l3 == 0);
288 
289  fs << "const ap_int<1> " << name_ << " = (";
290  std::map<const VarBase*, std::set<std::string> > cut_strings0, cut_strings1;
291  for (const auto& cut : cuts_) {
292  if (cut->op() != "cut")
293  continue;
294  const VarCut* const cast_cut = (VarCut*)cut;
295  cast_cut->print(cut_strings0, step_, hls);
296  }
297  for (const auto& cut : cuts_) {
298  if (cut->op() != "cut")
299  cut->print_cuts(cut_strings1, step_, hls, &cut_strings0);
300  else {
301  if (cut->cut_var()->p1())
302  cut->cut_var()->p1()->print_cuts(cut_strings1, step_, hls, &cut_strings1);
303  if (cut->cut_var()->p2())
304  cut->cut_var()->p2()->print_cuts(cut_strings1, step_, hls, &cut_strings1);
305  if (cut->cut_var()->p3())
306  cut->cut_var()->p3()->print_cuts(cut_strings1, step_, hls, &cut_strings1);
307  }
308  }
309 
310  std::string separator = "";
311  for (const auto& cut_var : cut_strings0) {
312  separator += "((";
313  for (const auto& cut_string : cut_var.second) {
314  fs << separator << cut_string;
315  separator = ") || (";
316  }
317  separator = ")) && ";
318  }
319  for (const auto& cut_var : cut_strings1) {
320  separator += "((";
321  for (const auto& cut_string : cut_var.second) {
322  fs << separator << cut_string;
323  separator = ") || (";
324  }
325  separator = ")) && ";
326  }
327 
328  fs << ")));";
329 }
330 
331 void VarBase::print_step(int step, std::ofstream& fs, HLS) {
332  if (!readytoprint_)
333  return;
334  if (step > step_)
335  return;
336  if (p1_)
337  p1_->print_step(step, fs, hls);
338  if (p2_)
339  p2_->print_step(step, fs, hls);
340  if (p3_)
341  p3_->print_step(step, fs, hls);
342  if (step == step_) {
343  print(fs, hls, 0, 0, 0);
344  readytoprint_ = false;
345  }
346 }
347 
348 void VarBase::print_all(std::ofstream& fs, HLS) {
349  for (int i = 0; i <= step_; ++i) {
350  fs << "//\n// STEP " << i << "\n\n";
351  print_step(i, fs, hls);
352  }
353 }
354 
355 void VarBase::design_print(const std::vector<VarBase*>& v, std::ofstream& fs, HLS) {
356  //header of the module
357 
358  //inputs
359  std::vector<VarBase*> vd;
360  vd.clear();
361  int imax = v.size();
362  for (int i = 0; i < imax; ++i)
363  (v[i])->inputs(&vd);
364 
365  //print header
366  fs << "#include \"ap_int.h\"\n\n";
367  fs << "void XXX (\n";
368 
369  imax = vd.size();
370  for (int i = 0; i < imax; ++i)
371  fs << " const ap_int<" << (vd[i])->nbits() << "> " << (vd[i])->name() << "_wire,\n";
372  fs << "\n";
373 
374  imax = v.size() - 1;
375  for (int i = 0; i < imax; ++i)
376  fs << " ap_int<" << (v[i])->nbits() << "> * const " << (v[i])->name() << "_wire,\n";
377  if (imax >= 0)
378  fs << " ap_int<" << (v[imax])->nbits() << "> * const " << (v[imax])->name() << "_wire\n";
379  fs << ")\n{\n";
380  fs << "#pragma HLS pipeline II=1\n";
381  fs << "#pragma HLS latency max=25\n";
382 
383  //body of the module
384  imax = v.size();
385  for (int i = 0; i < imax; ++i) {
386  fs << "\n//\n";
387  fs << "// calculating " << (v[i])->name() << "\n";
388  fs << "//\n";
389  (v[i])->print_all(fs, hls);
390  }
391  fs << "\n";
392 
393  //trailer
394  fs << "\n";
395  fs << "\n//\n";
396  fs << "// wiring the outputs \n";
397  fs << "//\n";
398  for (int i = 0; i < imax; ++i) {
399  std::string n = v[i]->name() + "_wire";
400  fs << "*" << n << " = " << (v[i])->name() << ";\n";
401  }
402 
403  fs << "}\n";
404 }
std::string name() const
Definition: imath.h:207
void print(std::ofstream &fs, Verilog, int l1=0, int l2=0, int l3=0) override
string separator
Definition: mps_merge.py:79
void print(std::ofstream &fs, Verilog, int l1=0, int l2=0, int l3=0) override
unsigned int mask_
Definition: imath.h:1046
void print_step(int step, std::ofstream &fs, Verilog)
void print_all(std::ofstream &fs, Verilog)
int nbits() const
Definition: imath.h:243
VarBase * cut_var()
Definition: imath.cc:388
void print(std::ofstream &fs, Verilog, int l1=0, int l2=0, int l3=0) override
VarBase * p1_
Definition: imath.h:297
VarBase * cut_var_
Definition: imath.h:309
VarBase * p2_
Definition: imath.h:298
std::string kstring() const
Definition: imath.cc:18
void print(std::ofstream &fs, Verilog, int l1=0, int l2=0, int l3=0) override
assert(be >=bs)
void print(std::map< const VarBase *, std::set< std::string > > &cut_strings, const int step, Verilog, const std::map< const VarBase *, std::set< std::string > > *const previous_cut_strings=nullptr) const
int shift() const
Definition: imath.h:247
VarBase * p3_
Definition: imath.h:299
int Nelements_
Definition: imath.h:1048
void print(std::ofstream &fs, Verilog, int l1=0, int l2=0, int l3=0) override
double lower_cut_
Definition: imath.h:1090
void print(std::ofstream &fs, Verilog, int l1=0, int l2=0, int l3=0) override
void print(std::ofstream &fs, Verilog, int l1=0, int l2=0, int l3=0) override
static std::string itos(int i)
Definition: imath.cc:16
long int ival_
Definition: imath.h:305
std::vector< DeviationSensor2D * > vd
double upper_cut() const
Definition: imath.h:1071
double K() const
Definition: imath.h:246
void print(std::ofstream &fs, Verilog, int l1=0, int l2=0, int l3=0) override
void print(std::ofstream &fs, Verilog, int l1=0, int l2=0, int l3=0) override
virtual void print(std::ofstream &fs, Verilog, int l1=0, int l2=0, int l3=0)
Definition: imath.h:256
static struct trklet::VarBase::HLS hls
bool readytoprint_
Definition: imath.h:319
void print(std::ofstream &fs, Verilog, int l1=0, int l2=0, int l3=0) override
void print(std::ofstream &fs, Verilog, int l1=0, int l2=0, int l3=0) override
void print_truncation(std::string &t, const std::string &o1, const int ps, Verilog) const
double upper_cut_
Definition: imath.h:1091
std::vector< VarBase * > cuts_
Definition: imath.h:308
std::string name_
Definition: imath.h:296
static void design_print(const std::vector< VarBase *> &v, std::ofstream &fs, Verilog)
double lower_cut() const
Definition: imath.h:1070
double K_
Definition: imath.h:312
void print(std::ofstream &fs, Verilog, int l1=0, int l2=0, int l3=0) override
int shift1
Definition: imath.h:594
int shift2
Definition: imath.h:595
void print(std::ofstream &fs, Verilog, int l1=0, int l2=0, int l3=0) override
step
Definition: StallMonitor.cc:98
void writeLUT(std::ofstream &fs) const
Definition: imath.h:1007
std::vector< unsigned short int > LUT
Definition: DTTracoLUTs.h:31
unsigned int shift_
Definition: imath.h:1045
void print(std::ofstream &fs, Verilog, int l1=0, int l2=0, int l3=0) override
void print_cuts(std::map< const VarBase *, std::set< std::string > > &cut_strings, const int step, Verilog, const std::map< const VarBase *, std::set< std::string > > *const previous_cut_strings=nullptr) const