-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.h
88 lines (74 loc) · 1.56 KB
/
library.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef __LIBRARY_H__
#define __LIBRARY_H__
#include "kernel/log.h"
#include <vector>
struct LutLibrary {
struct LutVariety {
int width;
int cost;
std::vector<int> delays;
int arrival(int in, int t)
{
log_assert(in < width);
return t + delays[in];
}
bool operator<(const LutVariety &other) const
{
return cost < other.cost;
}
};
std::vector<LutVariety> varieties;
std::vector<LutVariety*> by_width;
static LutLibrary academic_luts(int k)
{
LutLibrary lib;
lib.add(k, 1, std::vector<int>(k, 1));
return lib;
}
LutVariety &lookup(int width)
{
log_assert(width > 0);
log_assert(width <= (int) by_width.size());
return *by_width[width - 1];
}
const LutVariety &lookup(int width) const
{
log_assert(width > 0);
log_assert(width <= (int) by_width.size());
return *by_width[width - 1];
}
int max_width()
{
return (int) by_width.size();
}
void add(int width, int cost, std::vector<int> delays)
{
log_assert((int) delays.size() == width);
varieties.emplace_back();
LutVariety &variety = varieties.back();
variety.width = width;
variety.cost = cost;
variety.delays = delays;
index();
}
void index()
{
int max_width = 0;
for (auto &v : varieties)
max_width = std::max(max_width, v.width);
std::sort(varieties.begin(), varieties.end());
by_width.clear();
for (int j = 1; j <= max_width; j++) {
bool found = false;
for (auto &v : varieties) {
if (v.width >= j) {
found = true;
by_width.push_back(&v);
break;
}
}
log_assert(found);
}
}
};
#endif /* __LIBRARY_H__ */