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
|
/* Placeholder function, not used by any processor at the moment.
Copyright (C) 2022-2023 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/>. */
/* UNUSED. Exists purely as reference implementation. */
#include <isa-level.h>
#if ISA_SHOULD_BUILD (4)
# include <sysdep.h>
# ifdef USE_AS_WMEMCHR
# define CHAR_SIZE 4
# define VPBROADCAST vpbroadcastd
# define VPCMPEQ vpcmpeqd
# define VPCMPNE vpcmpneqd
# define VPMINU vpminud
# define VPTESTNM vptestnmd
# else
# define CHAR_SIZE 1
# define VPBROADCAST vpbroadcastb
# define VPCMPEQ vpcmpeqb
# define VPCMPNE vpcmpneqb
# define VPMINU vpminub
# define VPTESTNM vptestnmb
# endif
# define PAGE_SIZE 4096
# define CHAR_PER_VEC (VEC_SIZE / CHAR_SIZE)
.section SECTION(.text), "ax", @progbits
/* Aligning entry point to 64 byte, provides better performance for
one vector length string. */
ENTRY_P2ALIGN (MEMCHR, 6)
# ifndef USE_AS_RAWMEMCHR
/* Check for zero length. */
test %RDX_LP, %RDX_LP
jz L(zero)
# ifdef __ILP32__
/* Clear the upper 32 bits. */
movl %edx, %edx
# endif
# endif
/* Broadcast CHAR to VMM(1). */
VPBROADCAST %esi, %VMM(1)
movl %edi, %eax
andl $(PAGE_SIZE - 1), %eax
cmpl $(PAGE_SIZE - VEC_SIZE), %eax
ja L(page_cross)
/* Compare [w]char for null, mask bit will be set for match. */
VPCMPEQ (%rdi), %VMM(1), %k0
KMOV %k0, %VRCX
# ifndef USE_AS_RAWMEMCHR
mov %rdx, %rsi
/* Need to use bsfq here as upper 32 bit of rsi may zero out
for 'bsf %ecx, %esi', if %ecx is 0. */
bsfq %rcx, %rsi
cmp $CHAR_PER_VEC, %rsi
ja L(align_more)
# ifdef USE_AS_WMEMCHR
leaq (%rdi, %rsi, CHAR_SIZE), %rdi
# else
addq %rsi, %rdi
# endif
xor %eax, %eax
cmp %rsi, %rdx
cmova %rdi, %rax
# else
bsf %VRCX, %VRAX
jz L(align_more)
add %rdi, %rax
# endif
ret
.p2align 5,,5
L(page_cross):
movl %eax, %ecx
andl $(VEC_SIZE - 1), %ecx
# ifdef USE_AS_WMEMCHR
shrl $2, %ecx
# endif
xorq %rdi, %rax
VPCMPEQ (PAGE_SIZE - VEC_SIZE)(%rax), %VMM(1), %k0
KMOV %k0, %VRSI
shr %cl, %VRSI
# ifndef USE_AS_RAWMEMCHR
jnz L(page_cross_end)
movl $CHAR_PER_VEC, %eax
sub %ecx, %eax
cmp %rax, %rdx
ja L(align_more)
# else
jz L(align_more)
# endif
L(page_cross_end):
# ifndef USE_AS_RAWMEMCHR
bsf %VRSI, %VRCX
jz L(zero)
leaq (%rdi, %rcx, CHAR_SIZE), %rdi
xor %eax, %eax
cmp %rcx, %rdx
cmova %rdi, %rax
# else
bsf %VRSI, %VRAX
add %rdi, %rax
# endif
ret
# ifndef USE_AS_RAWMEMCHR
L(zero):
xorl %eax, %eax
ret
# endif
L(ret_vec_x2):
subq $-VEC_SIZE, %rdi
L(ret_vec_x1):
bsf %VRAX, %VRAX
# ifndef USE_AS_RAWMEMCHR
cmp %rax, %rdx
jbe L(zero)
# endif
# ifdef USE_AS_WMEMCHR
leaq (%rdi, %rax, CHAR_SIZE), %rax
# else
add %rdi, %rax
# endif
ret
.p2align 5,,5
L(align_more):
# ifndef USE_AS_RAWMEMCHR
mov %rdi, %rax
# endif
subq $-VEC_SIZE, %rdi
/* Align rdi to VEC_SIZE. */
andq $-VEC_SIZE, %rdi
# ifndef USE_AS_RAWMEMCHR
subq %rdi, %rax
# ifdef USE_AS_WMEMCHR
sar $2, %rax
# endif
addq %rax, %rdx
# endif
/* Loop unroll 4 times for 4 vector loop. */
VPCMPEQ (%rdi), %VMM(1), %k0
KMOV %k0, %VRAX
test %VRAX, %VRAX
jnz L(ret_vec_x1)
# ifndef USE_AS_RAWMEMCHR
subq $CHAR_PER_VEC, %rdx
jbe L(zero)
# endif
VPCMPEQ VEC_SIZE(%rdi), %VMM(1), %k0
KMOV %k0, %VRAX
test %VRAX, %VRAX
jnz L(ret_vec_x2)
# ifndef USE_AS_RAWMEMCHR
subq $CHAR_PER_VEC, %rdx
jbe L(zero)
# endif
VPCMPEQ (VEC_SIZE * 2)(%rdi), %VMM(1), %k0
KMOV %k0, %VRAX
test %VRAX, %VRAX
jnz L(ret_vec_x3)
# ifndef USE_AS_RAWMEMCHR
subq $CHAR_PER_VEC, %rdx
jbe L(zero)
# endif
VPCMPEQ (VEC_SIZE * 3)(%rdi), %VMM(1), %k0
KMOV %k0, %VRAX
test %VRAX, %VRAX
jnz L(ret_vec_x4)
# ifndef USE_AS_RAWMEMCHR
subq $CHAR_PER_VEC, %rdx
jbe L(zero)
/* Save pointer to find alignment adjustment. */
movq %rdi, %rax
# endif
/* Align address to VEC_SIZE * 4 for loop. */
andq $-(VEC_SIZE * 4), %rdi
/* Add alignment difference to rdx. */
# ifndef USE_AS_RAWMEMCHR
subq %rdi, %rax
# ifdef USE_AS_WMEMCHR
shr $2, %VRAX
# endif
addq %rax, %rdx
# endif
/* 4 vector loop. */
.p2align 5,,11
L(loop):
VPCMPNE (VEC_SIZE * 4)(%rdi), %VMM(1), %k1
vpxorq (VEC_SIZE * 5)(%rdi), %VMM(1), %VMM(2)
vpxorq (VEC_SIZE * 6)(%rdi), %VMM(1), %VMM(3)
VPCMPEQ (VEC_SIZE * 7)(%rdi), %VMM(1), %k3
VPMINU %VMM(2), %VMM(3), %VMM(3){%k1}{z}
VPTESTNM %VMM(3), %VMM(3), %k2
subq $-(VEC_SIZE * 4), %rdi
KORTEST %k2, %k3
# ifdef USE_AS_RAWMEMCHR
jz L(loop)
# else
jnz L(loopend)
subq $(CHAR_PER_VEC * 4), %rdx
ja L(loop)
L(zero_2):
xor %eax, %eax
ret
# endif
L(loopend):
VPCMPEQ (%rdi), %VMM(1), %k1
KMOV %k1, %VRAX
test %VRAX, %VRAX
jnz L(ret_vec_x1)
# ifndef USE_AS_RAWMEMCHR
subq $CHAR_PER_VEC, %rdx
jbe L(zero_2)
# endif
VPCMPEQ VEC_SIZE(%rdi), %VMM(1), %k1
KMOV %k1, %VRAX
test %VRAX, %VRAX
jnz L(ret_vec_x2)
# ifndef USE_AS_RAWMEMCHR
subq $CHAR_PER_VEC, %rdx
jbe L(zero_2)
# endif
VPCMPEQ (VEC_SIZE * 2)(%rdi), %VMM(1), %k1
KMOV %k1, %VRAX
test %VRAX, %VRAX
jnz L(ret_vec_x3)
# ifndef USE_AS_RAWMEMCHR
subq $CHAR_PER_VEC, %rdx
jbe L(zero_2)
# endif
/* At this point null [w]char must be in the fourth vector so no
need to check. */
KMOV %k3, %VRAX
L(ret_vec_x4):
bsf %VRAX, %VRAX
# ifndef USE_AS_RAWMEMCHR
cmp %rax, %rdx
jbe L(zero)
# endif
leaq (VEC_SIZE * 3)(%rdi, %rax, CHAR_SIZE), %rax
ret
.p2align 5,,5
L(ret_vec_x3):
bsf %VRAX, %VRAX
# ifndef USE_AS_RAWMEMCHR
cmp %rax, %rdx
jbe L(zero)
# endif
leaq (VEC_SIZE * 2)(%rdi, %rax, CHAR_SIZE), %rax
ret
END (MEMCHR)
#endif
|