1 /* memrchr optimized with AVX2.
2 Copyright (C) 2017-2024 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 (3)
26 # define MEMRCHR __memrchr_avx2
30 # define VZEROUPPER vzeroupper
34 # define SECTION(p) p##.avx
38 # define PAGE_SIZE 4096
39 .section SECTION(.text), "ax", @progbits
40 ENTRY_P2ALIGN(MEMRCHR, 6)
42 /* Clear upper bits. */
50 /* Get end pointer. Minus one for two reasons. 1) It is necessary for a
51 correct page cross check and 2) it correctly sets up end ptr to be
52 subtract by lzcnt aligned. */
53 leaq -1(%rdx, %rdi), %rax
55 vpbroadcastb %xmm0, %ymm0
57 /* Check if we can load 1x VEC without cross a page. */
58 testl $(PAGE_SIZE - VEC_SIZE), %eax
61 vpcmpeqb -(VEC_SIZE - 1)(%rax), %ymm0, %ymm1
67 /* If ecx is zero (no matches) lzcnt will set it 32 (VEC_SIZE) which
68 will guarantee edx (len) is less than it. */
71 /* Hoist vzeroupper (not great for RTM) to save code size. This allows
72 all logic for edx (len) <= VEC_SIZE to fit in first cache line. */
79 /* Fits in aligning bytes of first cache line. */
89 ZERO_UPPER_VEC_REGISTERS_RETURN
96 /* Align rax (string pointer). */
99 /* Recompute remaining length after aligning. */
101 /* Need this comparison next no matter what. */
102 vpcmpeqb -(VEC_SIZE)(%rax), %ymm0, %ymm1
105 vpmovmskb %ymm1, %ecx
106 /* Fall through for short (hotter than length). */
107 cmpq $(VEC_SIZE * 2), %rdx
111 jbe L(ret_vec_x0_test)
116 vpcmpeqb -(VEC_SIZE * 2 - 1)(%rax), %ymm0, %ymm1
117 vpmovmskb %ymm1, %ecx
118 /* 64-bit lzcnt. This will naturally add 32 to position. */
127 /* Inexpensive place to put this regarding code size / target alignments
128 / ICache NLP. Necessary for 2-byte encoding of jump to page cross
129 case which in turn is necessary for hot path (len <= VEC_SIZE) to fit
130 in first cache line. */
133 andq $-VEC_SIZE, %rsi
134 vpcmpeqb (%rsi), %ymm0, %ymm1
135 vpmovmskb %ymm1, %ecx
136 /* Shift out negative alignment (because we are starting from endptr and
137 working backwards). */
139 /* notl because eax already has endptr - 1. (-x = ~(x - 1)). */
141 shlxl %r8d, %ecx, %ecx
152 /* This will naturally add 32 to position. */
161 vpcmpeqb -(VEC_SIZE * 2 - 1)(%rax), %ymm0, %ymm1
162 vpmovmskb %ymm1, %ecx
167 /* Needed no matter what. */
168 vpcmpeqb -(VEC_SIZE * 3 - 1)(%rax), %ymm0, %ymm1
169 vpmovmskb %ymm1, %ecx
171 subq $(VEC_SIZE * 4), %rdx
174 cmpl $(VEC_SIZE * -1), %edx
175 jle L(ret_vec_x2_test)
181 /* Needed no matter what. */
182 vpcmpeqb -(VEC_SIZE * 4 - 1)(%rax), %ymm0, %ymm1
183 vpmovmskb %ymm1, %ecx
185 subq $(VEC_SIZE * 3), %rax
192 /* First in aligning bytes. */
200 subq $(VEC_SIZE * 2), %rax
210 /* ecx must be non-zero. */
212 leaq (VEC_SIZE * -3 + 1)(%rcx, %rax), %rax
217 /* ecx must be non-zero. */
219 leaq (VEC_SIZE * -4 + 1)(%rcx, %rax), %rax
229 vpcmpeqb -(VEC_SIZE * 4 - 1)(%rax), %ymm0, %ymm1
230 vpmovmskb %ymm1, %ecx
235 /* Check if near end before re-aligning (otherwise might do an
236 unnecessary loop iteration). */
237 addq $-(VEC_SIZE * 4), %rax
238 cmpq $(VEC_SIZE * 4), %rdx
241 /* Align rax to (VEC_SIZE - 1). */
242 orq $(VEC_SIZE * 4 - 1), %rax
244 /* Get endptr for loop in rdx. NB: Can't just do while rax > rdi because
245 lengths that overflow can be valid and break the comparison. */
246 orq $(VEC_SIZE * 4 - 1), %rdx
250 /* Need this comparison next no matter what. */
251 vpcmpeqb -(VEC_SIZE * 1 - 1)(%rax), %ymm0, %ymm1
252 vpcmpeqb -(VEC_SIZE * 2 - 1)(%rax), %ymm0, %ymm2
253 vpcmpeqb -(VEC_SIZE * 3 - 1)(%rax), %ymm0, %ymm3
254 vpcmpeqb -(VEC_SIZE * 4 - 1)(%rax), %ymm0, %ymm4
256 vpor %ymm1, %ymm2, %ymm2
257 vpor %ymm3, %ymm4, %ymm4
258 vpor %ymm2, %ymm4, %ymm4
259 vpmovmskb %ymm4, %esi
264 addq $(VEC_SIZE * -4), %rax
272 /* Used no matter what. */
273 vpcmpeqb -(VEC_SIZE * 1 - 1)(%rax), %ymm0, %ymm1
274 vpmovmskb %ymm1, %ecx
276 cmpl $(VEC_SIZE * 2), %edx
280 jnz L(ret_vec_x0_end)
282 vpcmpeqb -(VEC_SIZE * 2 - 1)(%rax), %ymm0, %ymm1
283 vpmovmskb %ymm1, %ecx
285 jnz L(ret_vec_x1_end)
287 /* Used no matter what. */
288 vpcmpeqb -(VEC_SIZE * 3 - 1)(%rax), %ymm0, %ymm1
289 vpmovmskb %ymm1, %ecx
291 cmpl $(VEC_SIZE * 3), %edx
295 subq $(VEC_SIZE * 2), %rax
307 vpmovmskb %ymm1, %ecx
309 jnz L(ret_vec_x0_end)
311 vpmovmskb %ymm2, %ecx
313 jnz L(ret_vec_x1_end)
315 vpmovmskb %ymm3, %ecx
316 /* Combine last 2 VEC matches. If ecx (VEC3) is zero (no CHAR in VEC3)
317 then it won't affect the result in esi (VEC4). If ecx is non-zero
318 then CHAR in VEC3 and bsrq will use that position. */
322 leaq (VEC_SIZE * -4 + 1)(%rcx, %rax), %rax
327 /* 64-bit version will automatically add 32 (VEC_SIZE). */
338 /* 2 bytes until next cache line. */