about summary refs log tree commit diff
path: root/sysdeps/x86_64/multiarch/strstr-sse2-unaligned.S
blob: 4ead1dfaf530f70b80c67ba52ca74f0864d7e733 (plain) (blame)
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
/* strstr with unaligned loads
   Copyright (C) 2009-2016 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
   <http://www.gnu.org/licenses/>.  */

#include <sysdep.h>

ENTRY(__strstr_sse2_unaligned)
	movzbl	(%rsi), %eax
	testb	%al, %al
	je	L(empty)
	movzbl	1(%rsi), %edx
	testb	%dl, %dl
	je	L(strchr)
	movd	%eax, %xmm1
	movd	%edx, %xmm2
	movq	%rdi, %rax
	andl	$4095, %eax
	punpcklbw	%xmm1, %xmm1
	cmpq	$4031, %rax
	punpcklbw	%xmm2, %xmm2
	punpcklwd	%xmm1, %xmm1
	punpcklwd	%xmm2, %xmm2
	pshufd	$0, %xmm1, %xmm1
	pshufd	$0, %xmm2, %xmm2
	ja	L(cross_page)
	movdqu	(%rdi), %xmm3
	pxor	%xmm5, %xmm5
	movdqu	1(%rdi), %xmm4
	movdqa	%xmm3, %xmm6
	pcmpeqb	%xmm1, %xmm3
	pcmpeqb	%xmm2, %xmm4
	movdqu	16(%rdi), %xmm0
	pcmpeqb	%xmm5, %xmm6
	pminub	%xmm4, %xmm3
	movdqa	%xmm3, %xmm4
	movdqu	17(%rdi), %xmm3
	pcmpeqb	%xmm0, %xmm5
	pcmpeqb	%xmm2, %xmm3
	por	%xmm6, %xmm4
	pcmpeqb	%xmm1, %xmm0
	pminub	%xmm3, %xmm0
	por	%xmm5, %xmm0
	pmovmskb	%xmm4, %r8d
	pmovmskb	%xmm0, %eax
	salq	$16, %rax
	orq	%rax, %r8
	je	L(next_32_bytes)
L(next_pair_index):
	bsf	%r8, %rax
	addq	%rdi, %rax
	cmpb	$0, (%rax)
	je	L(zero1)
	movzbl	2(%rsi), %edx
	testb	%dl, %dl
	je	L(found1)
	cmpb	2(%rax), %dl
	jne	L(next_pair)
	xorl	%edx, %edx
	jmp	L(pair_loop_start)

	.p2align 4
L(strchr):
	movzbl	%al, %esi
	jmp	__strchr_sse2

	.p2align 4
L(pair_loop):
	addq	$1, %rdx
	cmpb	2(%rax,%rdx), %cl
	jne	L(next_pair)
L(pair_loop_start):
	movzbl	3(%rsi,%rdx), %ecx
	testb	%cl, %cl
	jne	L(pair_loop)
L(found1):
	ret
L(zero1):
	xorl	%eax, %eax
	ret

	.p2align 4
L(next_pair):
	leaq	-1(%r8), %rax
	andq	%rax, %r8
	jne	L(next_pair_index)

	.p2align 4
L(next_32_bytes):
	movdqu	32(%rdi), %xmm3
	pxor	%xmm5, %xmm5
	movdqu	33(%rdi), %xmm4
	movdqa	%xmm3, %xmm6
	pcmpeqb	%xmm1, %xmm3
	pcmpeqb	%xmm2, %xmm4
	movdqu	48(%rdi), %xmm0
	pcmpeqb	%xmm5, %xmm6
	pminub	%xmm4, %xmm3
	movdqa	%xmm3, %xmm4
	movdqu	49(%rdi), %xmm3
	pcmpeqb	%xmm0, %xmm5
	pcmpeqb	%xmm2, %xmm3
	por	%xmm6, %xmm4
	pcmpeqb	%xmm1, %xmm0
	pminub	%xmm3, %xmm0
	por	%xmm5, %xmm0
	pmovmskb	%xmm4, %eax
	salq	$32, %rax
	pmovmskb	%xmm0, %r8d
	salq	$48, %r8
	orq	%rax, %r8
	je	L(loop_header)
L(next_pair2_index):
	bsfq	%r8, %rax
	addq	%rdi, %rax
	cmpb	$0, (%rax)
	je	L(zero2)
	movzbl	2(%rsi), %edx
	testb	%dl, %dl
	je	L(found2)
	cmpb	2(%rax), %dl
	jne	L(next_pair2)
	xorl	%edx, %edx
	jmp	L(pair_loop2_start)

	.p2align 4
L(pair_loop2):
	addq	$1, %rdx
	cmpb	2(%rax,%rdx), %cl
	jne	L(next_pair2)
L(pair_loop2_start):
	movzbl	3(%rsi,%rdx), %ecx
	testb	%cl, %cl
	jne	L(pair_loop2)
L(found2):
	ret
	L(zero2):
	xorl	%eax, %eax
	ret
L(empty):
	mov %rdi, %rax
	ret

	.p2align 4
L(next_pair2):
	leaq	-1(%r8), %rax
	andq	%rax, %r8
	jne	L(next_pair2_index)
L(loop_header):
	movq	$-512, %r11
	movq	%rdi, %r9

	pxor	%xmm7, %xmm7
	andq	$-64, %rdi

	.p2align 4
L(loop):
	movdqa	64(%rdi), %xmm3
	movdqu	63(%rdi), %xmm6
	movdqa	%xmm3, %xmm0
	pxor	%xmm2, %xmm3
	pxor	%xmm1, %xmm6
	movdqa	80(%rdi), %xmm10
	por	%xmm3, %xmm6
	pminub	%xmm10, %xmm0
	movdqu	79(%rdi), %xmm3
	pxor	%xmm2, %xmm10
	pxor	%xmm1, %xmm3
	movdqa	96(%rdi), %xmm9
	por	%xmm10, %xmm3
	pminub	%xmm9, %xmm0
	pxor	%xmm2, %xmm9
	movdqa	112(%rdi), %xmm8
	addq	$64, %rdi
	pminub	%xmm6, %xmm3
	movdqu	31(%rdi), %xmm4
	pminub	%xmm8, %xmm0
	pxor	%xmm2, %xmm8
	pxor	%xmm1, %xmm4
	por	%xmm9, %xmm4
	pminub	%xmm4, %xmm3
	movdqu	47(%rdi), %xmm5
	pxor	%xmm1, %xmm5
	por	%xmm8, %xmm5
	pminub	%xmm5, %xmm3
	pminub	%xmm3, %xmm0
	pcmpeqb	%xmm7, %xmm0
	pmovmskb	%xmm0, %eax
	testl	%eax, %eax
	je	L(loop)
	pminub (%rdi), %xmm6
	pminub 32(%rdi),%xmm4
	pminub 48(%rdi),%xmm5
	pcmpeqb %xmm7, %xmm6
	pcmpeqb %xmm7, %xmm5
	pmovmskb	%xmm6, %edx
	movdqa	16(%rdi), %xmm8
	pcmpeqb %xmm7, %xmm4
	movdqu  15(%rdi), %xmm0
	pmovmskb	%xmm5, %r8d
	movdqa  %xmm8, %xmm3
	pmovmskb	%xmm4, %ecx
	pcmpeqb %xmm1,%xmm0
	pcmpeqb %xmm2,%xmm3
	salq	$32, %rcx
	pcmpeqb %xmm7,%xmm8
	salq	$48, %r8
	pminub  %xmm0,%xmm3
	orq	%rcx, %rdx
	por	%xmm3,%xmm8
	orq	%rdx, %r8
	pmovmskb	%xmm8, %eax
	salq	$16, %rax
	orq	%rax, %r8
	je	L(loop)
L(next_pair_index3):
	bsfq	%r8, %rcx
	addq	%rdi, %rcx
	cmpb	$0, (%rcx)
	je	L(zero)
	xorl	%eax, %eax
	movzbl	2(%rsi), %edx
	testb	%dl, %dl
	je	L(success3)
	cmpb	1(%rcx), %dl
	jne	L(next_pair3)
	jmp	L(pair_loop_start3)

	.p2align 4
L(pair_loop3):
	addq	$1, %rax
	cmpb	1(%rcx,%rax), %dl
	jne	L(next_pair3)
L(pair_loop_start3):
	movzbl	3(%rsi,%rax), %edx
	testb	%dl, %dl
	jne	L(pair_loop3)
L(success3):
	lea	-1(%rcx), %rax
	ret

	.p2align 4
L(next_pair3):
	addq	%rax, %r11
	movq	%rdi,  %rax
	subq	%r9, %rax
	cmpq	%r11, %rax
	jl	L(switch_strstr)
	leaq	-1(%r8), %rax
	andq	%rax, %r8
	jne	L(next_pair_index3)
	jmp	L(loop)

	.p2align 4
L(switch_strstr):
	movq	%rdi, %rdi
	jmp	__strstr_sse2

	.p2align 4
L(cross_page):

	movq	%rdi, %rax
	pxor	%xmm0, %xmm0
	andq	$-64, %rax
	movdqa	(%rax), %xmm3
	movdqu	-1(%rax), %xmm4
	movdqa	%xmm3, %xmm8
	movdqa	16(%rax), %xmm5
	pcmpeqb	%xmm1, %xmm4
	pcmpeqb	%xmm0, %xmm8
	pcmpeqb	%xmm2, %xmm3
	movdqa	%xmm5, %xmm7
	pminub	%xmm4, %xmm3
	movdqu	15(%rax), %xmm4
	pcmpeqb	%xmm0, %xmm7
	por	%xmm3, %xmm8
	movdqa	%xmm5, %xmm3
	movdqa	32(%rax), %xmm5
	pcmpeqb	%xmm1, %xmm4
	pcmpeqb	%xmm2, %xmm3
	movdqa	%xmm5, %xmm6
	pmovmskb	%xmm8, %ecx
	pminub	%xmm4, %xmm3
	movdqu	31(%rax), %xmm4
	por	%xmm3, %xmm7
	movdqa	%xmm5, %xmm3
	pcmpeqb	%xmm0, %xmm6
	movdqa	48(%rax), %xmm5
	pcmpeqb	%xmm1, %xmm4
	pmovmskb	%xmm7, %r8d
	pcmpeqb	%xmm2, %xmm3
	pcmpeqb	%xmm5, %xmm0
	pminub	%xmm4, %xmm3
	movdqu	47(%rax), %xmm4
	por	%xmm3, %xmm6
	movdqa	%xmm5, %xmm3
	salq	$16, %r8
	pcmpeqb	%xmm1, %xmm4
	pcmpeqb	%xmm2, %xmm3
	pmovmskb	%xmm6, %r10d
	pminub	%xmm4, %xmm3
	por	%xmm3, %xmm0
	salq	$32, %r10
	orq	%r10, %r8
	orq	%rcx, %r8
	movl	%edi, %ecx
	pmovmskb	%xmm0, %edx
	subl	%eax, %ecx
	salq	$48, %rdx
	orq	%rdx, %r8
	shrq	%cl, %r8
	je	L(loop_header)
L(next_pair_index4):
	bsfq	%r8, %rax
	addq	%rdi, %rax
	cmpb	$0, (%rax)
	je	L(zero)

	cmpq	%rax,%rdi
	je	L(next_pair4)

	movzbl	2(%rsi), %edx
	testb	%dl, %dl
	je	L(found3)
	cmpb	1(%rax), %dl
	jne	L(next_pair4)
	xorl	%edx, %edx
	jmp	L(pair_loop_start4)

	.p2align 4
L(pair_loop4):
	addq	$1, %rdx
	cmpb	1(%rax,%rdx), %cl
	jne	L(next_pair4)
L(pair_loop_start4):
	movzbl	3(%rsi,%rdx), %ecx
	testb	%cl, %cl
	jne	L(pair_loop4)
L(found3):
	subq $1, %rax
	ret

	.p2align 4
L(next_pair4):
	leaq	-1(%r8), %rax
	andq	%rax, %r8
	jne	L(next_pair_index4)
	jmp	L(loop_header)

	.p2align 4
L(found):
	rep
	ret

	.p2align 4
L(zero):
	xorl	%eax, %eax
	ret


END(__strstr_sse2_unaligned)