1 /* strchr/strchrnul 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 STRCHR __strchr_evex
29 # define VMOVU vmovdqu64
30 # define VMOVA vmovdqa64
33 # define VPBROADCAST vpbroadcastd
35 # define VPTESTN vptestnmd
36 # define VPMINU vpminud
38 # define SHIFT_REG ecx
41 # define VPBROADCAST vpbroadcastb
43 # define VPTESTN vptestnmb
44 # define VPMINU vpminub
46 # define SHIFT_REG edx
50 # define XMMZERO xmm16
52 # define YMMZERO ymm16
64 # define PAGE_SIZE 4096
65 # define CHAR_PER_VEC (VEC_SIZE / CHAR_SIZE)
67 .section .text.evex,"ax",@progbits
68 ENTRY_P2ALIGN (STRCHR, 5)
69 /* Broadcast CHAR to YMM0. */
70 VPBROADCAST %esi, %YMM0
72 andl $(PAGE_SIZE - 1), %eax
73 /* Check if we cross page boundary with one vector load.
74 Otherwise it is safe to use an unaligned load. */
75 cmpl $(PAGE_SIZE - VEC_SIZE), %eax
76 ja L(cross_page_boundary)
78 /* Check the first VEC_SIZE bytes. Search for both CHAR and the
82 /* Leaves only CHARS matching esi as 0. */
83 vpxorq %YMM1, %YMM0, %YMM2
84 VPMINU %YMM2, %YMM1, %YMM2
85 /* Each bit in K0 represents a CHAR or a null byte in YMM1. */
86 VPTESTN %YMM2, %YMM2, %k0
91 # ifndef USE_AS_STRCHRNUL
92 /* Found CHAR or the null byte. */
93 cmp (%rdi, %rax, CHAR_SIZE), %CHAR_REG
94 /* NB: Use a branch instead of cmovcc here. The expectation is
95 that with strchr the user will branch based on input being
96 null. Since this branch will be 100% predictive of the user
97 branch a branch miss here should save what otherwise would
98 be branch miss in the user code. Otherwise using a branch 1)
99 saves code size and 2) is faster in highly predictable
103 # ifdef USE_AS_WCSCHR
104 /* NB: Multiply wchar_t count by 4 to get the number of bytes.
106 leaq (%rdi, %rax, CHAR_SIZE), %rax
116 # ifndef USE_AS_STRCHRNUL
117 /* Check to see if first match was CHAR (k0) or null (k1). */
121 /* bzhil will not be 0 if first match was null. */
122 bzhil %eax, %ecx, %ecx
125 /* Combine CHAR and null matches. */
130 /* NB: Multiply sizeof char type (1 or 4) to get the number of
132 leaq (VEC_SIZE * 4)(%rdi, %rax, CHAR_SIZE), %rax
135 # ifndef USE_AS_STRCHRNUL
144 /* Use bsf here to save 1-byte keeping keeping the block in 1x
145 fetch block. eax guranteed non-zero. */
147 # ifndef USE_AS_STRCHRNUL
148 /* Found CHAR or the null byte. */
149 cmp (VEC_SIZE)(%rdi, %rax, CHAR_SIZE), %CHAR_REG
153 /* NB: Multiply sizeof char type (1 or 4) to get the number of
155 leaq (VEC_SIZE)(%rdi, %rax, CHAR_SIZE), %rax
160 # ifndef USE_AS_STRCHRNUL
161 /* Check to see if first match was CHAR (k0) or null (k1). */
165 /* bzhil will not be 0 if first match was null. */
166 bzhil %eax, %ecx, %ecx
169 /* Combine CHAR and null matches. */
174 /* NB: Multiply sizeof char type (1 or 4) to get the number of
176 leaq (VEC_SIZE * 2)(%rdi, %rax, CHAR_SIZE), %rax
181 /* Use bsf here to save 1-byte keeping keeping the block in 1x
182 fetch block. eax guranteed non-zero. */
184 # ifndef USE_AS_STRCHRNUL
185 /* Found CHAR or the null byte. */
186 cmp (VEC_SIZE * 3)(%rdi, %rax, CHAR_SIZE), %CHAR_REG
189 /* NB: Multiply sizeof char type (1 or 4) to get the number of
191 leaq (VEC_SIZE * 3)(%rdi, %rax, CHAR_SIZE), %rax
196 /* Align data to VEC_SIZE. */
197 andq $-VEC_SIZE, %rdi
198 L(cross_page_continue):
199 /* Check the next 4 * VEC_SIZE. Only one VEC_SIZE at a time since
200 data is only aligned to VEC_SIZE. Use two alternating methods
201 for checking VEC to balance latency and port contention. */
203 /* This method has higher latency but has better port
205 VMOVA (VEC_SIZE)(%rdi), %YMM1
206 /* Leaves only CHARS matching esi as 0. */
207 vpxorq %YMM1, %YMM0, %YMM2
208 VPMINU %YMM2, %YMM1, %YMM2
209 /* Each bit in K0 represents a CHAR or a null byte in YMM1. */
210 VPTESTN %YMM2, %YMM2, %k0
215 /* This method has higher latency but has better port
217 VMOVA (VEC_SIZE * 2)(%rdi), %YMM1
218 /* Each bit in K0 represents a CHAR in YMM1. */
219 VPCMP $0, %YMM1, %YMM0, %k0
220 /* Each bit in K1 represents a CHAR in YMM1. */
221 VPTESTN %YMM1, %YMM1, %k1
225 VMOVA (VEC_SIZE * 3)(%rdi), %YMM1
226 /* Leaves only CHARS matching esi as 0. */
227 vpxorq %YMM1, %YMM0, %YMM2
228 VPMINU %YMM2, %YMM1, %YMM2
229 /* Each bit in K0 represents a CHAR or a null byte in YMM1. */
230 VPTESTN %YMM2, %YMM2, %k0
235 VMOVA (VEC_SIZE * 4)(%rdi), %YMM1
236 /* Each bit in K0 represents a CHAR in YMM1. */
237 VPCMP $0, %YMM1, %YMM0, %k0
238 /* Each bit in K1 represents a CHAR in YMM1. */
239 VPTESTN %YMM1, %YMM1, %k1
243 /* Align data to VEC_SIZE * 4 for the loop. */
245 andq $-(VEC_SIZE * 4), %rdi
249 /* Check 4x VEC at a time. No penalty to imm32 offset with evex
251 VMOVA (VEC_SIZE * 4)(%rdi), %YMM1
252 VMOVA (VEC_SIZE * 5)(%rdi), %YMM2
253 VMOVA (VEC_SIZE * 6)(%rdi), %YMM3
254 VMOVA (VEC_SIZE * 7)(%rdi), %YMM4
256 /* For YMM1 and YMM3 use xor to set the CHARs matching esi to
258 vpxorq %YMM1, %YMM0, %YMM5
259 /* For YMM2 and YMM4 cmp not equals to CHAR and store result in
260 k register. Its possible to save either 1 or 2 instructions
261 using cmp no equals method for either YMM1 or YMM1 and YMM3
262 respectively but bottleneck on p5 makes it not worth it. */
263 VPCMP $4, %YMM0, %YMM2, %k2
264 vpxorq %YMM3, %YMM0, %YMM7
265 VPCMP $4, %YMM0, %YMM4, %k4
267 /* Use min to select all zeros from either xor or end of string).
269 VPMINU %YMM1, %YMM5, %YMM1
270 VPMINU %YMM3, %YMM7, %YMM3
272 /* Use min + zeromask to select for zeros. Since k2 and k4 will
273 have 0 as positions that matched with CHAR which will set
274 zero in the corresponding destination bytes in YMM2 / YMM4.
276 VPMINU %YMM1, %YMM2, %YMM2{%k2}{z}
277 VPMINU %YMM3, %YMM4, %YMM4
278 VPMINU %YMM2, %YMM4, %YMM4{%k4}{z}
280 VPTESTN %YMM4, %YMM4, %k1
282 subq $-(VEC_SIZE * 4), %rdi
286 VPTESTN %YMM1, %YMM1, %k0
291 VPTESTN %YMM2, %YMM2, %k0
296 VPTESTN %YMM3, %YMM3, %k0
298 /* Combine YMM3 matches (eax) with YMM4 matches (ecx). */
299 # ifdef USE_AS_WCSCHR
308 # ifndef USE_AS_STRCHRNUL
309 /* Check if match was CHAR or null. */
310 cmp (VEC_SIZE * 2)(%rdi, %rax, CHAR_SIZE), %CHAR_REG
313 /* NB: Multiply sizeof char type (1 or 4) to get the number of
315 leaq (VEC_SIZE * 2)(%rdi, %rax, CHAR_SIZE), %rax
321 # ifdef USE_AS_WCSCHR
322 /* NB: Multiply wchar_t count by 4 to get the number of bytes.
324 leaq (%rdi, %rax, CHAR_SIZE), %rax
329 # ifndef USE_AS_STRCHRNUL
330 /* Check if match was null. */
331 cmp (%rax), %CHAR_REG
340 # ifndef USE_AS_STRCHRNUL
341 /* Check if match was null. */
342 cmp (VEC_SIZE)(%rdi, %rax, CHAR_SIZE), %CHAR_REG
345 /* NB: Multiply sizeof char type (1 or 4) to get the number of
347 leaq (VEC_SIZE)(%rdi, %rax, CHAR_SIZE), %rax
350 /* Cold case for crossing page with first load. */
352 L(cross_page_boundary):
355 andq $-VEC_SIZE, %rdi
357 /* Leaves only CHARS matching esi as 0. */
358 vpxorq %YMM1, %YMM0, %YMM2
359 VPMINU %YMM2, %YMM1, %YMM2
360 /* Each bit in K0 represents a CHAR or a null byte in YMM1. */
361 VPTESTN %YMM2, %YMM2, %k0
363 /* Remove the leading bits. */
364 # ifdef USE_AS_WCSCHR
365 movl %edx, %SHIFT_REG
366 /* NB: Divide shift count by 4 since each bit in K1 represent 4
369 andl $(CHAR_PER_VEC - 1), %SHIFT_REG
371 sarxl %SHIFT_REG, %eax, %eax
372 /* If eax is zero continue. */
374 jz L(cross_page_continue)
377 # ifdef USE_AS_WCSCHR
378 /* NB: Multiply wchar_t count by 4 to get the number of
380 leaq (%rdx, %rax, CHAR_SIZE), %rax
384 # ifndef USE_AS_STRCHRNUL
385 /* Check to see if match was CHAR or null. */
386 cmp (%rax), %CHAR_REG