Skip to content

Commit

Permalink
Lookup tables v4
Browse files Browse the repository at this point in the history
  • Loading branch information
romaf5 committed Oct 27, 2024
1 parent bc4d40e commit 4ad58b4
Showing 1 changed file with 18 additions and 37 deletions.
55 changes: 18 additions & 37 deletions labs/bad_speculation/lookup_tables_1/solution.cpp
Original file line number Diff line number Diff line change
@@ -1,48 +1,29 @@
#include "solution.hpp"

int bucket_map[151] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0-12
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 13-28
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 29-40
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,// 41-52
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 53-70
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, // 71-82
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, // 83-99
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 100-109
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7
};

static std::size_t mapToBucket(std::size_t v) {
// size of a bucket
if (v < 13) return 0; // 13
else if (v < 29) return 1; // 16
else if (v < 41) return 2; // 12
else if (v < 53) return 3; // 12
else if (v < 71) return 4; // 18
else if (v < 83) return 5; // 12
else if (v < 100) return 6; // 17
return DEFAULT_BUCKET;
return bucket_map[v];
}

std::array<std::size_t, NUM_BUCKETS> histogram(const std::vector<int> &values) {
std::array<std::size_t, NUM_BUCKETS> retBuckets{0};
std::array<std::size_t, 151> bucket_map;

for (int i = 0; i < 13; ++i) {
bucket_map[i] = 0;
}
for (int i = 13; i < 29; ++i) {
bucket_map[i] = 1;
}
for (int i = 29; i < 41; ++i) {
bucket_map[i] = 2;
}
for (int i = 41; i < 53; ++i) {
bucket_map[i] = 3;
}
for (int i = 53; i < 71; ++i) {
bucket_map[i] = 4;
}
for (int i = 71; i < 83; ++i) {
bucket_map[i] = 5;
}
for (int i = 83; i < 100; ++i) {
bucket_map[i] = 6;
}

for (int i = 100; i <= 150; ++i) {
bucket_map[i] = DEFAULT_BUCKET;
}
for (auto v : values) {
retBuckets[bucket_map[v]]++;
retBuckets[mapToBucket(v)]++;
}
return retBuckets;
}

0 comments on commit 4ad58b4

Please sign in to comment.