-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathavx512f-strstr.cpp
234 lines (180 loc) · 6.14 KB
/
avx512f-strstr.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
string - pointer to the string
n - string length in bytes
needle - pointer to another string
n - needle length in bytes
*/
size_t avx512f_strstr_long(const char* string, size_t n, const char* needle, size_t k) {
assert(n > 0);
assert(k > 4);
__m512i curr;
__m512i next;
__m512i v0, v1, v2, v3;
char* haystack = const_cast<char*>(string);
char* last = haystack + n;
const uint32_t prf = *(uint32_t*)needle; // the first 4 bytes of needle
const __m512i prefix = _mm512_set1_epi32(prf);
next = _mm512_loadu_si512(haystack);
for (/**/; haystack < last; haystack += 64) {
curr = next;
next = _mm512_loadu_si512(haystack + 64);
const __m512i shft = _mm512_alignr_epi32(next, curr, 1);
v0 = curr;
{
const __m512i t1 = _mm512_srli_epi32(curr, 8);
const __m512i t2 = _mm512_slli_epi32(shft, 24);
v1 = _mm512_or_si512(t1, t2);
}
{
const __m512i t1 = _mm512_srli_epi32(curr, 16);
const __m512i t2 = _mm512_slli_epi32(shft, 16);
v2 = _mm512_or_si512(t1, t2);
}
{
const __m512i t1 = _mm512_srli_epi32(curr, 24);
const __m512i t2 = _mm512_slli_epi32(shft, 8);
v3 = _mm512_or_si512(t1, t2);
}
uint16_t m0 = _mm512_cmpeq_epi32_mask(v0, prefix);
uint16_t m1 = _mm512_cmpeq_epi32_mask(v1, prefix);
uint16_t m2 = _mm512_cmpeq_epi32_mask(v2, prefix);
uint16_t m3 = _mm512_cmpeq_epi32_mask(v3, prefix);
int index = 64;
while (m0 | m1 | m2 | m3) {
if (m0) {
int pos = __builtin_ctz(m0) * 4 + 0;
m0 = m0 & (m0 - 1);
if (pos < index && memcmp(haystack + pos + 4, needle + 4, k - 4) == 0) {
index = pos;
}
}
if (m1) {
int pos = __builtin_ctz(m1) * 4 + 1;
m1 = m1 & (m1 - 1);
if (pos < index && memcmp(haystack + pos + 4, needle + 4, k - 4) == 0) {
index = pos;
}
}
if (m2) {
int pos = __builtin_ctz(m2) * 4 + 2;
m2 = m2 & (m2 - 1);
if (pos < index && memcmp(haystack + pos + 4, needle + 4, k - 4) == 0) {
index = pos;
}
}
if (m3) {
int pos = __builtin_ctz(m3) * 4 + 3;
m3 = m3 & (m3 - 1);
if (pos < index && memcmp(haystack + pos + 4, needle + 4, k - 4) == 0) {
index = pos;
}
}
}
if (index < 64) {
return (haystack - string) + index;
}
}
return size_t(-1);
}
// ------------------------------------------------------------------------
size_t avx512f_strstr_eq4(const char* string, size_t n, const char* needle) {
assert(n > 0);
__m512i curr;
__m512i next;
__m512i v0, v1, v2, v3;
char* haystack = const_cast<char*>(string);
char* last = haystack + n;
const uint32_t prf = *(uint32_t*)needle; // the first 4 bytes of needle
const __m512i prefix = _mm512_set1_epi32(prf);
next = _mm512_loadu_si512(haystack);
for (/**/; haystack < last; haystack += 64) {
curr = next;
next = _mm512_loadu_si512(haystack + 64);
const __m512i shft = _mm512_alignr_epi32(next, curr, 1);
v0 = curr;
{
const __m512i t1 = _mm512_srli_epi32(curr, 8);
const __m512i t2 = _mm512_slli_epi32(shft, 24);
v1 = _mm512_or_si512(t1, t2);
}
{
const __m512i t1 = _mm512_srli_epi32(curr, 16);
const __m512i t2 = _mm512_slli_epi32(shft, 16);
v2 = _mm512_or_si512(t1, t2);
}
{
const __m512i t1 = _mm512_srli_epi32(curr, 24);
const __m512i t2 = _mm512_slli_epi32(shft, 8);
v3 = _mm512_or_si512(t1, t2);
}
uint16_t m0 = _mm512_cmpeq_epi32_mask(v0, prefix);
uint16_t m1 = _mm512_cmpeq_epi32_mask(v1, prefix);
uint16_t m2 = _mm512_cmpeq_epi32_mask(v2, prefix);
uint16_t m3 = _mm512_cmpeq_epi32_mask(v3, prefix);
int index = 64;
if (m0) {
int pos = __builtin_ctz(m0) * 4 + 0;
if (pos < index) {
index = pos;
}
}
if (m1) {
int pos = __builtin_ctz(m1) * 4 + 1;
if (pos < index) {
index = pos;
}
}
if (m2) {
int pos = __builtin_ctz(m2) * 4 + 2;
if (pos < index) {
index = pos;
}
}
if (m3) {
int pos = __builtin_ctz(m3) * 4 + 3;
if (pos < index) {
index = pos;
}
}
if (index < 64) {
return (haystack - string) + index;
}
assert(m0 == 0 && m1 == 0 && m2 == 0 && m3 == 0);
}
return size_t(-1);
}
// ------------------------------------------------------------------------
size_t avx512f_strstr(const char* s, size_t n, const char* needle, size_t needle_size) {
size_t result = std::string::npos;
if (n < needle_size) {
return result;
}
switch (needle_size) {
case 0:
return 0;
case 1: {
const char* res = reinterpret_cast<const char*>(strchr(s, needle[0]));
return (res != nullptr) ? res - s : std::string::npos;
}
case 2:
case 3: {
const char* res = reinterpret_cast<const char*>(strstr(s, needle));
return (res != nullptr) ? res - s : std::string::npos;
}
case 4:
result = avx512f_strstr_eq4(s, n, needle);
break;
default:
result = avx512f_strstr_long(s, n, needle, needle_size);
break;
}
if (result <= n - needle_size) {
return result;
} else {
return std::string::npos;
}
}
// --------------------------------------------------
size_t avx512f_strstr(const std::string& s, const std::string& needle) {
return avx512f_strstr(s.data(), s.size(), needle.data(), needle.size());
}