1 /* strrchr/wcsrchr optimized with 256-bit EVEX instructions.
2 Copyright (C) 2021-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <isa-level.h>
21 #if ISA_SHOULD_BUILD (4)
26 # define STRRCHR __strrchr_evex
29 # include "x86-evex256-vecs.h"
31 # ifdef USE_AS_WCSRCHR
33 # define SHIFT_REG rcx
34 # define VPCOMPRESS vpcompressd
35 # define kunpck_2x kunpckbw
36 # define kmov_2x kmovd
40 # define VPMIN vpminud
41 # define VPTESTN vptestnmd
42 # define VPTEST vptestmd
43 # define VPBROADCAST vpbroadcastd
44 # define VPCMPEQ vpcmpeqd
47 # define USE_WIDE_CHAR
50 # define SHIFT_REG rdi
51 # define VPCOMPRESS vpcompressb
52 # define kunpck_2x kunpckdq
53 # define kmov_2x kmovq
58 # define VPMIN vpminub
59 # define VPTESTN vptestnmb
60 # define VPTEST vptestmb
61 # define VPBROADCAST vpbroadcastb
62 # define VPCMPEQ vpcmpeqb
66 # include "reg-macros.h"
68 # define VMATCH VMM(0)
69 # define CHAR_PER_VEC (VEC_SIZE / CHAR_SIZE)
70 # define PAGE_SIZE 4096
72 .section SECTION(.text), "ax", @progbits
73 ENTRY_P2ALIGN(STRRCHR, 6)
75 /* Broadcast CHAR to VMATCH. */
76 VPBROADCAST %esi, %VMATCH
78 andl $(PAGE_SIZE - 1), %eax
79 cmpl $(PAGE_SIZE - VEC_SIZE), %eax
80 jg L(cross_page_boundary)
83 /* k0 has a 1 for each zero CHAR in VEC(1). */
84 VPTESTN %VMM(1), %VMM(1), %k0
88 /* fallthrough: zero CHAR in first VEC. */
90 /* K1 has a 1 for each search CHAR match in VEC(1). */
91 VPCMPEQ %VMATCH, %VMM(1), %k1
93 /* Build mask up until first zero CHAR (used to mask of
94 potential search CHAR matches past the end of the string).
99 /* Get last match (the `and` removed any out of bounds matches).
102 # ifdef USE_AS_WCSRCHR
103 leaq (%rdi, %rax, CHAR_SIZE), %rax
110 /* Returns for first vec x1/x2/x3 have hard coded backward
111 search path for earlier matches. */
114 VPCMPEQ %VMATCH, %VMM(2), %k1
117 /* eax non-zero if search CHAR in range. */
119 jnz L(first_vec_x1_return)
121 /* fallthrough: no match in VEC(2) then need to check for
122 earlier matches (in VEC(1)). */
124 L(first_vec_x0_test):
125 VPCMPEQ %VMATCH, %VMM(1), %k1
130 # ifdef USE_AS_WCSRCHR
131 leaq (%rsi, %rax, CHAR_SIZE), %rax
139 L(first_vec_x1_or_x2):
140 VPCMPEQ %VMM(3), %VMATCH, %k3
141 VPCMPEQ %VMM(2), %VMATCH, %k2
142 /* K2 and K3 have 1 for any search CHAR match. Test if any
143 matches between either of them. Otherwise check VEC(1). */
145 jz L(first_vec_x0_test)
147 /* Guranteed that VEC(2) and VEC(3) are within range so merge
148 the two bitmasks then get last result. */
149 kunpck_2x %k2, %k3, %k3
150 kmov_2x %k3, %maskm_2x
151 bsr %maskm_2x, %maskm_2x
152 leaq (VEC_SIZE * 1)(%r8, %rax, CHAR_SIZE), %rax
157 VPCMPEQ %VMATCH, %VMM(4), %k1
160 /* If no search CHAR match in range check VEC(1)/VEC(2)/VEC(3).
163 jz L(first_vec_x1_or_x2)
165 leaq (VEC_SIZE * 3)(%rdi, %rax, CHAR_SIZE), %rax
170 L(first_vec_x0_x1_test):
171 VPCMPEQ %VMATCH, %VMM(2), %k1
173 /* Check VEC(2) for last match first. If no match try VEC(1).
176 jz L(first_vec_x0_test)
178 L(first_vec_x1_return):
180 leaq (VEC_SIZE)(%rdi, %rax, CHAR_SIZE), %rax
186 VPCMPEQ %VMATCH, %VMM(3), %k1
189 /* Check VEC(3) for last match first. If no match try
192 jz L(first_vec_x0_x1_test)
194 leaq (VEC_SIZE * 2)(%rdi, %rax, CHAR_SIZE), %rax
200 L(page_cross_continue):
201 /* Need to keep original pointer incase VEC(1) has last match.
204 andq $-VEC_SIZE, %rdi
206 VMOVU VEC_SIZE(%rdi), %VMM(2)
207 VPTESTN %VMM(2), %VMM(2), %k0
213 VMOVU (VEC_SIZE * 2)(%rdi), %VMM(3)
214 VPTESTN %VMM(3), %VMM(3), %k0
220 VMOVU (VEC_SIZE * 3)(%rdi), %VMM(4)
221 VPTESTN %VMM(4), %VMM(4), %k0
227 andq $-(VEC_SIZE * 2), %rdi
229 L(first_aligned_loop):
230 /* Preserve VEC(1), VEC(2), VEC(3), and VEC(4) until we can
231 gurantee they don't store a match. */
232 VMOVA (VEC_SIZE * 4)(%rdi), %VMM(5)
233 VMOVA (VEC_SIZE * 5)(%rdi), %VMM(6)
235 VPCMPEQ %VMM(5), %VMATCH, %k2
236 vpxord %VMM(6), %VMATCH, %VMM(7)
238 VPMIN %VMM(5), %VMM(6), %VMM(8)
239 VPMIN %VMM(8), %VMM(7), %VMM(7)
241 VPTESTN %VMM(7), %VMM(7), %k1
242 subq $(VEC_SIZE * -2), %rdi
244 jz L(first_aligned_loop)
246 VPCMPEQ %VMM(6), %VMATCH, %k3
247 VPTESTN %VMM(8), %VMM(8), %k1
249 /* If k1 is zero, then we found a CHAR match but no null-term.
250 We can now safely throw out VEC1-4. */
252 jz L(second_aligned_loop_prep)
255 jnz L(return_first_aligned_loop)
259 L(first_vec_x1_or_x2_or_x3):
260 VPCMPEQ %VMM(4), %VMATCH, %k4
263 jz L(first_vec_x1_or_x2)
264 leaq (VEC_SIZE * 3)(%r8, %rax, CHAR_SIZE), %rax
269 L(return_first_aligned_loop):
270 VPTESTN %VMM(5), %VMM(5), %k0
272 /* Combined results from VEC5/6. */
273 kunpck_2x %k0, %k1, %k0
274 kmov_2x %k0, %maskz_2x
276 blsmsk %maskz_2x, %maskz_2x
277 kunpck_2x %k2, %k3, %k3
278 kmov_2x %k3, %maskm_2x
279 and %maskz_2x, %maskm_2x
280 jz L(first_vec_x1_or_x2_or_x3)
282 bsr %maskm_2x, %maskm_2x
283 leaq (VEC_SIZE * 2)(%rdi, %rax, CHAR_SIZE), %rax
287 /* We can throw away the work done for the first 4x checks here
288 as we have a later match. This is the 'fast' path persay.
290 L(second_aligned_loop_prep):
291 L(second_aligned_loop_set_furthest_match):
293 /* Ideally we would safe k2/k3 but `kmov/kunpck` take uops on
294 port0 and have noticable overhead in the loop. */
295 VMOVA %VMM(5), %VMM(7)
296 VMOVA %VMM(6), %VMM(8)
298 L(second_aligned_loop):
299 VMOVU (VEC_SIZE * 4)(%rdi), %VMM(5)
300 VMOVU (VEC_SIZE * 5)(%rdi), %VMM(6)
301 VPCMPEQ %VMM(5), %VMATCH, %k2
302 vpxord %VMM(6), %VMATCH, %VMM(3)
304 VPMIN %VMM(5), %VMM(6), %VMM(4)
305 VPMIN %VMM(3), %VMM(4), %VMM(3)
307 VPTESTN %VMM(3), %VMM(3), %k1
308 subq $(VEC_SIZE * -2), %rdi
310 jz L(second_aligned_loop)
311 VPCMPEQ %VMM(6), %VMATCH, %k3
312 VPTESTN %VMM(4), %VMM(4), %k1
314 jz L(second_aligned_loop_set_furthest_match)
316 /* branch here because we know we have a match in VEC7/8 but
317 might not in VEC5/6 so the latter is expected to be less
320 jnz L(return_new_match)
323 VPCMPEQ %VMM(8), %VMATCH, %k0
326 jnz L(return_old_match_ret)
328 VPCMPEQ %VMM(7), %VMATCH, %k0
332 L(return_old_match_ret):
333 leaq (VEC_SIZE * 3)(%rsi, %rcx, CHAR_SIZE), %rax
338 VPTESTN %VMM(5), %VMM(5), %k0
340 /* Combined results from VEC5/6. */
341 kunpck_2x %k0, %k1, %k0
342 kmov_2x %k0, %maskz_2x
344 blsmsk %maskz_2x, %maskz_2x
345 kunpck_2x %k2, %k3, %k3
346 kmov_2x %k3, %maskm_2x
348 /* Match at end was out-of-bounds so use last known match. */
349 and %maskz_2x, %maskm_2x
350 jz L(return_old_match)
352 bsr %maskm_2x, %maskm_2x
353 leaq (VEC_SIZE * 2)(%rdi, %rax, CHAR_SIZE), %rax
357 L(cross_page_boundary):
360 VMOVU (PAGE_SIZE - VEC_SIZE)(%rax), %VMM(6)
361 VPTESTN %VMM(6), %VMM(6), %k0
364 # ifdef USE_AS_WCSRCHR
366 and $(VEC_SIZE - 1), %ecx
369 shlx %VGPR(SHIFT_REG), %VRDX, %VRDX
371 # ifdef USE_AS_WCSRCHR
377 /* Need to adjust result to VEC(1) so it can be re-used by
378 L(return_vec_x0_test). The alternative is to collect VEC(1)
379 will a page cross load which is far more expensive. */
380 VPCOMPRESS %VMM(6), %VMM(1){%k1}{z}
382 /* We could technically just jmp back after the vpcompress but
383 it doesn't save any 16-byte blocks. */
384 shrx %VGPR(SHIFT_REG), %VRSI, %VRSI
386 jz L(page_cross_continue)
388 /* Duplicate of return logic from ENTRY. Doesn't cause spill to
389 next cache line so might as well copy it here. */
390 VPCMPEQ %VMATCH, %VMM(1), %k1
396 # ifdef USE_AS_WCSRCHR
397 leaq (%rdi, %rax, CHAR_SIZE), %rax
403 /* 1 byte till next cache line. */