about summary refs log tree commit diff
path: root/sysdeps/x86_64/memcmp.S
blob: 870e15c5a080162b336b13bac24cf7afbac6874b (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
/* memcmp with SSE2
   Copyright (C) 2009-2021 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/>.  */

#include <sysdep.h>

	.text
ENTRY (memcmp)
#ifdef __ILP32__
	/* Clear the upper 32 bits.  */
	movl	%edx, %edx
#endif
	test	%RDX_LP, %RDX_LP
	jz	L(finz)
	cmpq	$1, %rdx
	jbe	L(finr1b)
	subq	%rdi, %rsi
	movq	%rdx, %r10
	cmpq	$32, %r10
	jae	L(gt32)
	/* Handle small chunks and last block of less than 32 bytes.  */
L(small):
	testq	$1, %r10
	jz	L(s2b)
	movzbl	(%rdi),	%eax
	movzbl	(%rdi, %rsi), %edx
	subq    $1, %r10
	je	L(finz1)
	addq	$1, %rdi
	subl	%edx, %eax
	jnz	L(exit)
L(s2b):
	testq	$2, %r10
	jz	L(s4b)
	movzwl	(%rdi),	%eax
	movzwl	(%rdi, %rsi), %edx
	subq    $2, %r10
	je	L(fin2_7)
	addq	$2, %rdi
	cmpl	%edx, %eax
	jnz	L(fin2_7)
L(s4b):
	testq	$4, %r10
	jz	L(s8b)
	movl	(%rdi),	%eax
	movl	(%rdi, %rsi), %edx
	subq    $4, %r10
	je	L(fin2_7)
	addq	$4, %rdi
	cmpl	%edx, %eax
	jnz	L(fin2_7)
L(s8b):
	testq	$8, %r10
	jz	L(s16b)
	movq	(%rdi),	%rax
	movq	(%rdi, %rsi), %rdx
	subq    $8, %r10
	je	L(fin2_7)
	addq	$8, %rdi
	cmpq	%rdx, %rax
	jnz	L(fin2_7)
L(s16b):
	movdqu    (%rdi), %xmm1
	movdqu    (%rdi, %rsi), %xmm0
	pcmpeqb   %xmm0, %xmm1
	pmovmskb  %xmm1, %edx
	xorl	  %eax, %eax
	subl      $0xffff, %edx
	jz	  L(finz)
	bsfl      %edx, %ecx
	leaq	 (%rdi, %rcx), %rcx
	movzbl	 (%rcx), %eax
	movzbl	 (%rsi, %rcx), %edx
	jmp	 L(finz1)

	.p2align 4,, 4
L(finr1b):
	movzbl	(%rdi), %eax
	movzbl  (%rsi), %edx
L(finz1):
	subl	%edx, %eax
L(exit):
	ret

	.p2align 4,, 4
L(fin2_7):
	cmpq	%rdx, %rax
	jz	L(finz)
	movq	%rax, %r11
	subq	%rdx, %r11
	bsfq	%r11, %rcx
	sarq	$3, %rcx
	salq	$3, %rcx
	sarq	%cl, %rax
	movzbl  %al, %eax
	sarq	%cl, %rdx
	movzbl  %dl, %edx
	subl	%edx, %eax
	ret

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

	/* For blocks bigger than 32 bytes
	   1. Advance one of the addr pointer to be 16B aligned.
	   2. Treat the case of both addr pointers aligned to 16B
	      separately to avoid movdqu.
	   3. Handle any blocks of greater than 64 consecutive bytes with
	      unrolling to reduce branches.
	   4. At least one addr pointer is 16B aligned, use memory version
	      of pcmbeqb.
	*/
	.p2align 4,, 4
L(gt32):
	movq	%rdx, %r11
	addq	%rdi, %r11
	movq	%rdi, %r8

	andq	$15, %r8
	jz	L(16am)
	/* Both pointers may be misaligned.  */
	movdqu	(%rdi),	%xmm1
	movdqu	(%rdi, %rsi), %xmm0
	pcmpeqb   %xmm0, %xmm1
	pmovmskb  %xmm1, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	neg	 %r8
	leaq    16(%rdi, %r8), %rdi
L(16am):
	/* Handle two 16B aligned pointers separately.  */
	testq   $15, %rsi
	jz      L(ATR)
	testq	$16, %rdi
	jz	L(A32)
	movdqu	(%rdi, %rsi), %xmm0
	pcmpeqb   (%rdi), %xmm0
	pmovmskb  %xmm0, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	addq	$16, %rdi
L(A32):
	movq	%r11, %r10
	andq	$-32, %r10
	cmpq	%r10, %rdi
        jae	L(mt16)
	/* Pre-unroll to be ready for unrolled 64B loop.  */
	testq	$32, %rdi
	jz	L(A64)
	movdqu    (%rdi,%rsi), %xmm0
	pcmpeqb   (%rdi), %xmm0
	pmovmskb  %xmm0, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	addq       $16, %rdi

	movdqu    (%rdi,%rsi), %xmm0
	pcmpeqb  (%rdi), %xmm0
	pmovmskb  %xmm0, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	addq       $16, %rdi

L(A64):
	movq	%r11, %r10
	andq	$-64, %r10
	cmpq	%r10, %rdi
        jae	L(mt32)

L(A64main):
	movdqu    (%rdi,%rsi), %xmm0
	pcmpeqb   (%rdi), %xmm0
	pmovmskb  %xmm0, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	addq       $16, %rdi

	movdqu    (%rdi,%rsi), %xmm0
	pcmpeqb   (%rdi), %xmm0
	pmovmskb  %xmm0, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	addq       $16, %rdi

	movdqu    (%rdi,%rsi), %xmm0
	pcmpeqb   (%rdi), %xmm0
	pmovmskb  %xmm0, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	addq       $16, %rdi

	movdqu    (%rdi,%rsi), %xmm0
	pcmpeqb  (%rdi), %xmm0
	pmovmskb  %xmm0, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	addq       $16, %rdi

	cmpq       %rdi, %r10
	jne       L(A64main)

L(mt32):
	movq	%r11, %r10
	andq	$-32, %r10
	cmpq	%r10, %rdi
        jae	L(mt16)

L(A32main):
	movdqu    (%rdi,%rsi), %xmm0
	pcmpeqb   (%rdi), %xmm0
	pmovmskb  %xmm0, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	addq       $16, %rdi

	movdqu    (%rdi,%rsi), %xmm0
	pcmpeqb  (%rdi), %xmm0
	pmovmskb  %xmm0, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	addq       $16, %rdi

	cmpq       %rdi, %r10
	jne       L(A32main)
L(mt16):
	subq       %rdi, %r11
	je	  L(finz)
	movq	  %r11, %r10
	jmp	  L(small)

	.p2align 4,, 4
L(neq):
	bsfl      %edx, %ecx
	movzbl	 (%rdi, %rcx), %eax
	addq	 %rdi, %rsi
	movzbl	 (%rsi,%rcx), %edx
	jmp	 L(finz1)

	.p2align 4,, 4
L(ATR):
	movq	%r11, %r10
	andq	$-32, %r10
	cmpq	%r10, %rdi
        jae	L(mt16)
	testq	$16, %rdi
	jz	L(ATR32)

	movdqa    (%rdi,%rsi), %xmm0
	pcmpeqb   (%rdi), %xmm0
	pmovmskb  %xmm0, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	addq       $16, %rdi
	cmpq       %rdi, %r10
	je       L(mt16)

L(ATR32):
	movq	%r11, %r10
	andq	$-64, %r10
	testq	$32, %rdi
	jz	L(ATR64)

	movdqa    (%rdi,%rsi), %xmm0
	pcmpeqb   (%rdi), %xmm0
	pmovmskb  %xmm0, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	addq       $16, %rdi

	movdqa    (%rdi,%rsi), %xmm0
	pcmpeqb   (%rdi), %xmm0
	pmovmskb  %xmm0, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	addq       $16, %rdi

L(ATR64):
	cmpq       %rdi, %r10
	je	   L(mt32)

L(ATR64main):
	movdqa    (%rdi,%rsi), %xmm0
	pcmpeqb   (%rdi), %xmm0
	pmovmskb  %xmm0, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	addq       $16, %rdi

	movdqa    (%rdi,%rsi), %xmm0
	pcmpeqb   (%rdi), %xmm0
	pmovmskb  %xmm0, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	addq       $16, %rdi

	movdqa    (%rdi,%rsi), %xmm0
	pcmpeqb   (%rdi), %xmm0
	pmovmskb  %xmm0, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	addq       $16, %rdi

	movdqa    (%rdi,%rsi), %xmm0
	pcmpeqb   (%rdi), %xmm0
	pmovmskb  %xmm0, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	addq       $16, %rdi
	cmpq       %rdi, %r10
	jne       L(ATR64main)

	movq	%r11, %r10
	andq	$-32, %r10
	cmpq	%r10, %rdi
        jae	L(mt16)

L(ATR32res):
	movdqa    (%rdi,%rsi), %xmm0
	pcmpeqb   (%rdi), %xmm0
	pmovmskb  %xmm0, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	addq       $16, %rdi

	movdqa    (%rdi,%rsi), %xmm0
	pcmpeqb   (%rdi), %xmm0
	pmovmskb  %xmm0, %edx
	subl      $0xffff, %edx
	jnz       L(neq)
	addq       $16, %rdi

	cmpq	  %r10, %rdi
	jne       L(ATR32res)

	subq       %rdi, %r11
	je	  L(finz)
	movq	  %r11, %r10
	jmp	  L(small)
	/* Align to 16byte to improve instruction fetch.  */
	.p2align 4,, 4
END(memcmp)

#undef bcmp
weak_alias (memcmp, bcmp)
libc_hidden_builtin_def (memcmp)