-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbin_eyt.h
55 lines (44 loc) · 1.36 KB
/
bin_eyt.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
#ifndef BIN_EYT_H
#define BIN_EYT_H
template <int record_bytes = 8, bool prefetch = false,
typename Index = unsigned long>
class b_eyt {
using Vector = PaddedVector<record_bytes>;
using Linear = LinearUnroll<Vector>;
const Vector A;
static const Index multiplier = 64 / sizeof(record_bytes);
static const Index offset = multiplier + multiplier / 2;
template <typename T, typename ForwardIterator>
ForwardIterator copy_data(ForwardIterator a0, size_t i, T &out) {
if (i >= out.size())
return a0;
// visit left child
a0 = copy_data(a0, 2 * i + 1, out);
// put data at the root
out[i] = *a0++;
// visit right child
a0 = copy_data(a0, 2 * i + 2, out);
return a0;
}
template <typename T> T eytzinger_array(T in) {
T rv(in.size());
copy_data(in.begin(), 0, rv);
return rv;
}
public:
b_eyt(const Vector &_a) : A(eytzinger_array(_a)) {}
// Branch-free code with or without prefetching
__attribute__((always_inline)) Key operator()(const Key x) {
// template<typename T, typename I, bool aligned>
// template<bool prefetch>
Index i = 0;
while (i < A.size()) {
if (prefetch)
__builtin_prefetch(&A[0] + (multiplier * i + offset));
i = (x <= A[i]) ? (2 * i + 1) : (2 * i + 2);
}
Index j = (i + 1) >> __builtin_ffs(~(i + 1));
return A[j - 1];
}
};
#endif