-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbin.h
155 lines (139 loc) · 4.76 KB
/
bin.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#ifndef BIN_H
#define BIN_H
#include <cinttypes>
#include <assert.h>
#include "lin.h"
#include "util.h"
#include "padded_vector.h"
#include <limits>
#if IACA == 1
#include <iacaMarks.h>
#else
#define IACA_START
#define IACA_END
#endif
template <int record_bytes = 8, bool RETURN_EARLY = true, bool TEST_EQ = true,
bool FOR = false, int MIN_EQ_SZ = 1>
class BinaryLR {
using Index = unsigned long;
using Vector = PaddedVector<record_bytes>;
using Linear = LinearUnroll<Vector>;
const Vector& A;
int lg_v;
public:
BinaryLR(const Vector &_a) : A(_a) {
lg_v = 0;
for (auto n = A.size(); n > MIN_EQ_SZ; n -= (n / 2))
lg_v++;
}
__attribute__((always_inline)) Key operator()(const Key x) {
// use pointer
Index l = 0;
Index r = A.size();
for (int i = 0; FOR ? i < lg_v : r - l > 1; i++) {
// IACA_START
assert(l <= r); // ordering check
assert(l + r >= r); // overflow check
Index m = (l + r) / 2;
if (TEST_EQ) {
if (A[m] < x) {
l = m + 1;
} else if (A[m] > x) {
r = m;
} else {
if (RETURN_EARLY)
return A[m];
l = r = m;
}
} else {
if (A[m] <= x)
l = m;
else
r = m;
}
}
if (MIN_EQ_SZ == 1) {
// IACA_END
return A[l];
}
Index guess = (l + r) / 2;
if (A[guess] < x)
return A[Linear::forward(A, guess + 1, x)];
else
return A[Linear::reverse(A, guess, x)];
}
};
template <int record_bytes = 8, bool RETURN_EARLY = false, bool TEST_EQ = false,
bool FOR = true, bool POW_2 = false, int MIN_EQ_SZ = 32,
typename Index = unsigned long>
class Binary {
using Vector = PaddedVector<record_bytes>;
using Linear = LinearUnroll<Vector>;
const Vector &A;
int lg_v, lg_min;
public:
Binary(const Vector &_a) : A(_a) {
lg_v = lg_min = 0;
for (auto n = A.size(); n > 1; n -= (n / 2)) {
lg_v++;
if (n > MIN_EQ_SZ)
lg_min++;
}
}
__attribute__((always_inline)) Key operator()(const Key x) {
Index n = A.size();
Index left = 0L;
if (POW_2) {
Index mid = n - (1UL << (lg_v - 1));
left = A[mid] <= x ? mid : left;
n -= mid;
}
// TODO how to set unroll = 4 if MIN_EQ_SZ > 1?, else 8?
#define LOOP \
assert(left + n == A.size() || A[left + n] > x); \
assert(A[left] <= x); \
IACA_START Index half = n / 2; \
if (TEST_EQ) { \
if (x < A[left + half]) { \
n = half; \
} else if (A[left + half] < x) { \
left = left + half + 1; \
n = n - half - 1; \
} else { \
if (RETURN_EARLY) \
return A[left + half]; \
left += half; \
if (FOR) \
i = lg_min; \
else \
n = 0; \
} \
} else { \
left = A[left + half] <= x ? left + half : left; \
if (POW_2) \
n = half; \
else \
n -= half; \
}
if (MIN_EQ_SZ == 1) {
#pragma unroll(8)
for (int i = POW_2 ? 1 : 0; FOR ? i < lg_min : n > MIN_EQ_SZ; i++) {
LOOP
}
} else {
#pragma unroll(4)
for (int i = POW_2 ? 1 : 0; FOR ? i < lg_min : n > MIN_EQ_SZ; i++) {
LOOP
}
}
IACA_END
if (MIN_EQ_SZ == 1)
return A[left];
Index guess = left + n / 2;
if (A[guess] < x)
return A[Linear::forward(A, guess + 1, x)];
else
return A[Linear::reverse(A, guess, x)];
}
};
#endif