11 template <
typename Key,
typename Entry,
int NEntries = 3>
14 constexpr
static bool enabled = NEntries > 0;
17 tlb(Key invalid_key, Entry invalid_entry) {
18 entries_.fill({invalid_key, invalid_entry, 0});
21 void add(
const Key& key,
const Entry& entry) {
25 Key invalid_key = entries_[0].key;
26 for (
int i = 1; i <= NEntries; i++) {
27 if (entries_[i].key == invalid_key) {
28 entries_[i] = {key, entry, timestamp_++};
35 entries_.begin() + 1, entries_.end(),
36 [](
const auto& te1,
const auto& te2) {
37 return te1.timestamp < te2.timestamp;
39 victim = {key, entry, timestamp_++};
42 Entry
get(
const Key& key) {
43 return get([&](
const Key& k) {
return k == key; });
46 template <
typename Fn>
48 if constexpr (!
enabled)
return entries_[0].entry;
51 for (
int i = 1; i <= NEntries; i++) {
52 if (fn(entries_[i].key)) {
57 entries_[found_index].timestamp = timestamp_++;
58 return entries_[found_index].entry;
64 tlb_entry invalid_te = entries_[0];
65 entries_.fill(invalid_te);
70 using timestamp_t = int;
75 timestamp_t timestamp;
78 std::array<tlb_entry, NEntries + 1> entries_;
79 timestamp_t timestamp_ = 0;
82 ITYR_TEST_CASE(
"[ityr::ori::tlb] test TLB") {
84 using element_t = int;
85 constexpr
int n_elements = 5;
87 key_t invalid_key = -1;
88 element_t invalid_element = -1;
90 tlb<key_t, element_t, n_elements> tlb_(invalid_key, invalid_element);
93 for (
int i = 0; i < n; i++) {
97 for (
int i = 0; i < n; i++) {
98 element_t ret = tlb_.get(i);
99 if (i < n - n_elements) {
108 for (
int i = 0; i < n; i++) {
109 element_t ret = tlb_.get(i);
void clear()
Definition: tlb.hpp:61
tlb()
Definition: tlb.hpp:16
Entry get(const Key &key)
Definition: tlb.hpp:42
constexpr static bool enabled
Definition: tlb.hpp:14
tlb(Key invalid_key, Entry invalid_entry)
Definition: tlb.hpp:17
Entry get(Fn fn)
Definition: tlb.hpp:47
void add(const Key &key, const Entry &entry)
Definition: tlb.hpp:21
#define ITYR_CHECK(cond)
Definition: util.hpp:48
Definition: block_region_set.hpp:9
ForwardIterator min_element(const ExecutionPolicy &policy, ForwardIterator first, ForwardIterator last, Compare comp)
Search for the minimum element in a range.
Definition: parallel_search.hpp:88