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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/* strrchr/wcsrchr optimized with 256-bit EVEX instructions.
Copyright (C) 2021-2022 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#if IS_IN (libc)
# include <sysdep.h>
# ifndef STRRCHR
# define STRRCHR __strrchr_evex
# endif
# define VMOVU vmovdqu64
# define VMOVA vmovdqa64
# ifdef USE_AS_WCSRCHR
# define SHIFT_REG esi
# define kunpck kunpckbw
# define kmov_2x kmovd
# define maskz_2x ecx
# define maskm_2x eax
# define CHAR_SIZE 4
# define VPMIN vpminud
# define VPTESTN vptestnmd
# define VPBROADCAST vpbroadcastd
# define VPCMP vpcmpd
# else
# define SHIFT_REG edi
# define kunpck kunpckdq
# define kmov_2x kmovq
# define maskz_2x rcx
# define maskm_2x rax
# define CHAR_SIZE 1
# define VPMIN vpminub
# define VPTESTN vptestnmb
# define VPBROADCAST vpbroadcastb
# define VPCMP vpcmpb
# endif
# define XMMZERO xmm16
# define YMMZERO ymm16
# define YMMMATCH ymm17
# define YMMSAVE ymm18
# define YMM1 ymm19
# define YMM2 ymm20
# define YMM3 ymm21
# define YMM4 ymm22
# define YMM5 ymm23
# define YMM6 ymm24
# define YMM7 ymm25
# define YMM8 ymm26
# define VEC_SIZE 32
# define PAGE_SIZE 4096
.section .text.evex, "ax", @progbits
ENTRY(STRRCHR)
movl %edi, %eax
/* Broadcast CHAR to YMMMATCH. */
VPBROADCAST %esi, %YMMMATCH
andl $(PAGE_SIZE - 1), %eax
cmpl $(PAGE_SIZE - VEC_SIZE), %eax
jg L(cross_page_boundary)
L(page_cross_continue):
VMOVU (%rdi), %YMM1
/* k0 has a 1 for each zero CHAR in YMM1. */
VPTESTN %YMM1, %YMM1, %k0
kmovd %k0, %ecx
testl %ecx, %ecx
jz L(aligned_more)
/* fallthrough: zero CHAR in first VEC. */
/* K1 has a 1 for each search CHAR match in YMM1. */
VPCMP $0, %YMMMATCH, %YMM1, %k1
kmovd %k1, %eax
/* Build mask up until first zero CHAR (used to mask of
potential search CHAR matches past the end of the string).
*/
blsmskl %ecx, %ecx
andl %ecx, %eax
jz L(ret0)
/* Get last match (the `andl` removed any out of bounds
matches). */
bsrl %eax, %eax
# ifdef USE_AS_WCSRCHR
leaq (%rdi, %rax, CHAR_SIZE), %rax
# else
addq %rdi, %rax
# endif
L(ret0):
ret
/* Returns for first vec x1/x2/x3 have hard coded backward
search path for earlier matches. */
.p2align 4,, 6
L(first_vec_x1):
VPCMP $0, %YMMMATCH, %YMM2, %k1
kmovd %k1, %eax
blsmskl %ecx, %ecx
/* eax non-zero if search CHAR in range. */
andl %ecx, %eax
jnz L(first_vec_x1_return)
/* fallthrough: no match in YMM2 then need to check for earlier
matches (in YMM1). */
.p2align 4,, 4
L(first_vec_x0_test):
VPCMP $0, %YMMMATCH, %YMM1, %k1
kmovd %k1, %eax
testl %eax, %eax
jz L(ret1)
bsrl %eax, %eax
# ifdef USE_AS_WCSRCHR
leaq (%rsi, %rax, CHAR_SIZE), %rax
# else
addq %rsi, %rax
# endif
L(ret1):
ret
.p2align 4,, 10
L(first_vec_x1_or_x2):
VPCMP $0, %YMM3, %YMMMATCH, %k3
VPCMP $0, %YMM2, %YMMMATCH, %k2
/* K2 and K3 have 1 for any search CHAR match. Test if any
matches between either of them. Otherwise check YMM1. */
kortestd %k2, %k3
jz L(first_vec_x0_test)
/* Guranteed that YMM2 and YMM3 are within range so merge the
two bitmasks then get last result. */
kunpck %k2, %k3, %k3
kmovq %k3, %rax
bsrq %rax, %rax
leaq (VEC_SIZE)(%r8, %rax, CHAR_SIZE), %rax
ret
.p2align 4,, 6
L(first_vec_x3):
VPCMP $0, %YMMMATCH, %YMM4, %k1
kmovd %k1, %eax
blsmskl %ecx, %ecx
/* If no search CHAR match in range check YMM1/YMM2/YMM3. */
andl %ecx, %eax
jz L(first_vec_x1_or_x2)
bsrl %eax, %eax
leaq (VEC_SIZE * 3)(%rdi, %rax, CHAR_SIZE), %rax
ret
.p2align 4,, 6
L(first_vec_x0_x1_test):
VPCMP $0, %YMMMATCH, %YMM2, %k1
kmovd %k1, %eax
/* Check YMM2 for last match first. If no match try YMM1. */
testl %eax, %eax
jz L(first_vec_x0_test)
.p2align 4,, 4
L(first_vec_x1_return):
bsrl %eax, %eax
leaq (VEC_SIZE)(%rdi, %rax, CHAR_SIZE), %rax
ret
.p2align 4,, 10
L(first_vec_x2):
VPCMP $0, %YMMMATCH, %YMM3, %k1
kmovd %k1, %eax
blsmskl %ecx, %ecx
/* Check YMM3 for last match first. If no match try YMM2/YMM1.
*/
andl %ecx, %eax
jz L(first_vec_x0_x1_test)
bsrl %eax, %eax
leaq (VEC_SIZE * 2)(%rdi, %rax, CHAR_SIZE), %rax
ret
.p2align 4
L(aligned_more):
/* Need to keep original pointer incase YMM1 has last match. */
movq %rdi, %rsi
andq $-VEC_SIZE, %rdi
VMOVU VEC_SIZE(%rdi), %YMM2
VPTESTN %YMM2, %YMM2, %k0
kmovd %k0, %ecx
testl %ecx, %ecx
jnz L(first_vec_x1)
VMOVU (VEC_SIZE * 2)(%rdi), %YMM3
VPTESTN %YMM3, %YMM3, %k0
kmovd %k0, %ecx
testl %ecx, %ecx
jnz L(first_vec_x2)
VMOVU (VEC_SIZE * 3)(%rdi), %YMM4
VPTESTN %YMM4, %YMM4, %k0
kmovd %k0, %ecx
movq %rdi, %r8
testl %ecx, %ecx
jnz L(first_vec_x3)
andq $-(VEC_SIZE * 2), %rdi
.p2align 4
L(first_aligned_loop):
/* Preserve YMM1, YMM2, YMM3, and YMM4 until we can gurantee
they don't store a match. */
VMOVA (VEC_SIZE * 4)(%rdi), %YMM5
VMOVA (VEC_SIZE * 5)(%rdi), %YMM6
VPCMP $0, %YMM5, %YMMMATCH, %k2
vpxord %YMM6, %YMMMATCH, %YMM7
VPMIN %YMM5, %YMM6, %YMM8
VPMIN %YMM8, %YMM7, %YMM7
VPTESTN %YMM7, %YMM7, %k1
subq $(VEC_SIZE * -2), %rdi
kortestd %k1, %k2
jz L(first_aligned_loop)
VPCMP $0, %YMM6, %YMMMATCH, %k3
VPTESTN %YMM8, %YMM8, %k1
ktestd %k1, %k1
jz L(second_aligned_loop_prep)
kortestd %k2, %k3
jnz L(return_first_aligned_loop)
.p2align 4,, 6
L(first_vec_x1_or_x2_or_x3):
VPCMP $0, %YMM4, %YMMMATCH, %k4
kmovd %k4, %eax
testl %eax, %eax
jz L(first_vec_x1_or_x2)
bsrl %eax, %eax
leaq (VEC_SIZE * 3)(%r8, %rax, CHAR_SIZE), %rax
ret
.p2align 4,, 8
L(return_first_aligned_loop):
VPTESTN %YMM5, %YMM5, %k0
kunpck %k0, %k1, %k0
kmov_2x %k0, %maskz_2x
blsmsk %maskz_2x, %maskz_2x
kunpck %k2, %k3, %k3
kmov_2x %k3, %maskm_2x
and %maskz_2x, %maskm_2x
jz L(first_vec_x1_or_x2_or_x3)
bsr %maskm_2x, %maskm_2x
leaq (VEC_SIZE * 2)(%rdi, %rax, CHAR_SIZE), %rax
ret
.p2align 4
/* We can throw away the work done for the first 4x checks here
as we have a later match. This is the 'fast' path persay.
*/
L(second_aligned_loop_prep):
L(second_aligned_loop_set_furthest_match):
movq %rdi, %rsi
kunpck %k2, %k3, %k4
.p2align 4
L(second_aligned_loop):
VMOVU (VEC_SIZE * 4)(%rdi), %YMM1
VMOVU (VEC_SIZE * 5)(%rdi), %YMM2
VPCMP $0, %YMM1, %YMMMATCH, %k2
vpxord %YMM2, %YMMMATCH, %YMM3
VPMIN %YMM1, %YMM2, %YMM4
VPMIN %YMM3, %YMM4, %YMM3
VPTESTN %YMM3, %YMM3, %k1
subq $(VEC_SIZE * -2), %rdi
kortestd %k1, %k2
jz L(second_aligned_loop)
VPCMP $0, %YMM2, %YMMMATCH, %k3
VPTESTN %YMM4, %YMM4, %k1
ktestd %k1, %k1
jz L(second_aligned_loop_set_furthest_match)
kortestd %k2, %k3
/* branch here because there is a significant advantage interms
of output dependency chance in using edx. */
jnz L(return_new_match)
L(return_old_match):
kmovq %k4, %rax
bsrq %rax, %rax
leaq (VEC_SIZE * 2)(%rsi, %rax, CHAR_SIZE), %rax
ret
L(return_new_match):
VPTESTN %YMM1, %YMM1, %k0
kunpck %k0, %k1, %k0
kmov_2x %k0, %maskz_2x
blsmsk %maskz_2x, %maskz_2x
kunpck %k2, %k3, %k3
kmov_2x %k3, %maskm_2x
and %maskz_2x, %maskm_2x
jz L(return_old_match)
bsr %maskm_2x, %maskm_2x
leaq (VEC_SIZE * 2)(%rdi, %rax, CHAR_SIZE), %rax
ret
L(cross_page_boundary):
/* eax contains all the page offset bits of src (rdi). `xor rdi,
rax` sets pointer will all page offset bits cleared so
offset of (PAGE_SIZE - VEC_SIZE) will get last aligned VEC
before page cross (guranteed to be safe to read). Doing this
as opposed to `movq %rdi, %rax; andq $-VEC_SIZE, %rax` saves
a bit of code size. */
xorq %rdi, %rax
VMOVU (PAGE_SIZE - VEC_SIZE)(%rax), %YMM1
VPTESTN %YMM1, %YMM1, %k0
kmovd %k0, %ecx
/* Shift out zero CHAR matches that are before the begining of
src (rdi). */
# ifdef USE_AS_WCSRCHR
movl %edi, %esi
andl $(VEC_SIZE - 1), %esi
shrl $2, %esi
# endif
shrxl %SHIFT_REG, %ecx, %ecx
testl %ecx, %ecx
jz L(page_cross_continue)
/* Found zero CHAR so need to test for search CHAR. */
VPCMP $0, %YMMMATCH, %YMM1, %k1
kmovd %k1, %eax
/* Shift out search CHAR matches that are before the begining of
src (rdi). */
shrxl %SHIFT_REG, %eax, %eax
/* Check if any search CHAR match in range. */
blsmskl %ecx, %ecx
andl %ecx, %eax
jz L(ret3)
bsrl %eax, %eax
# ifdef USE_AS_WCSRCHR
leaq (%rdi, %rax, CHAR_SIZE), %rax
# else
addq %rdi, %rax
# endif
L(ret3):
ret
END(STRRCHR)
#endif
|