-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo_v3_avx2.cpp
184 lines (175 loc) · 6.48 KB
/
algo_v3_avx2.cpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <immintrin.h>
#include <stdint.h>
#include <cassert>
#include "algo_intersection.h"
#ifdef __GNUC__
#define COMPILER_LIKELY(x) __builtin_expect((x), 1)
#define COMPILER_RARELY(x) __builtin_expect((x), 0)
#else
#define COMPILER_LIKELY(x) x
#define COMPILER_RARELY(x) x
#endif
size_t v3_avx2_intersection(const uint32_t *freq, const size_t lenFreq, const uint32_t *rare, const size_t lenRare, uint32_t *out) {
if (lenFreq == 0 || lenRare == 0)
return 0;
assert(lenRare <= lenFreq);
const uint32_t *const initout(out);
typedef __m128i vec;
const uint32_t veclen = sizeof(vec) / sizeof(uint32_t);
const size_t vecmax = veclen - 1;
const size_t freqspace = 32 * veclen;
const size_t rarespace = 1;
const uint32_t *stopFreq = freq + lenFreq - freqspace;
const uint32_t *stopRare = rare + lenRare - rarespace;
if (freq > stopFreq) {
return scalar(freq, lenFreq, rare, lenRare, out);
}
while (freq[veclen * 31 + vecmax] < *rare) {
freq += veclen * 32;
if (freq > stopFreq)
goto FINISH_SCALAR;
}
for (; rare < stopRare; ++rare) {
const uint32_t matchRare = *rare; // nextRare;
const vec Match = _mm_set1_epi32(matchRare);
while (freq[veclen * 31 + vecmax] < matchRare) { // if no match possible
freq += veclen * 32; // advance 32 vectors
if (freq > stopFreq)
goto FINISH_SCALAR;
}
vec Q0, Q1, Q2, Q3;
if (freq[veclen * 15 + vecmax] >= matchRare) {
if (freq[veclen * 7 + vecmax] < matchRare) {
Q0 = _mm_or_si128(
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 8),
Match),
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 9),
Match));
Q1 = _mm_or_si128(
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 10),
Match),
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 11),
Match));
Q2 = _mm_or_si128(
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 12),
Match),
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 13),
Match));
Q3 = _mm_or_si128(
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 14),
Match),
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 15),
Match));
} else {
Q0 = _mm_or_si128(
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 4),
Match),
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 5),
Match));
Q1 = _mm_or_si128(
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 6),
Match),
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 7),
Match));
Q2 = _mm_or_si128(
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 0),
Match),
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 1),
Match));
Q3 = _mm_or_si128(
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 2),
Match),
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 3),
Match));
}
} else {
if (freq[veclen * 23 + vecmax] < matchRare) {
Q0 = _mm_or_si128(
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 8 + 16),
Match),
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 9 + 16),
Match));
Q1 = _mm_or_si128(
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 10 + 16),
Match),
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 11 + 16),
Match));
Q2 = _mm_or_si128(
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 12 + 16),
Match),
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 13 + 16),
Match));
Q3 = _mm_or_si128(
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 14 + 16),
Match),
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 15 + 16),
Match));
} else {
Q0 = _mm_or_si128(
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 4 + 16),
Match),
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 5 + 16),
Match));
Q1 = _mm_or_si128(
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 6 + 16),
Match),
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 7 + 16),
Match));
Q2 = _mm_or_si128(
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 0 + 16),
Match),
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 1 + 16),
Match));
Q3 = _mm_or_si128(
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 2 + 16),
Match),
_mm_cmpeq_epi32(
_mm_loadu_si128(reinterpret_cast<const vec *>(freq) + 3 + 16),
Match));
}
}
const vec F0 = _mm_or_si128(_mm_or_si128(Q0, Q1), _mm_or_si128(Q2, Q3));
#ifdef __SSE4_1__
if (_mm_testz_si128(F0, F0)) {
#else
if (!_mm_movemask_epi8(F0)) {
#endif
} else {
*out++ = matchRare;
}
}
FINISH_SCALAR:
return (out - initout) + scalar(freq, stopFreq + freqspace - freq, rare,
stopRare + rarespace - rare, out);
}