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
|
/*-
* Copyright (c) 2023, The FreeBSD Foundation
*
* SPDX-License-Expression: BSD-2-Clause
*
* Portions of this software were developed by Robert Clausecker
* <fuz@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
*
* Adapted from NetBSD's common/lib/libc/arch/x86_64/string/strcmp.S
* written by J.T. Conklin <jtc@acorntoolworks.com> that was originally
* dedicated to the public domain.
*/
#include <machine/asm.h>
#include <machine/param.h>
#if 0
RCSID("$NetBSD: strcmp.S,v 1.3 2004/07/19 20:04:41 drochner Exp $")
#endif
#include "amd64_archlevel.h"
#define ALIGN_TEXT .p2align 4, 0x90
ARCHFUNCS(strcmp)
ARCHFUNC(strcmp, scalar)
ARCHFUNC(strcmp, baseline)
ENDARCHFUNCS(strcmp)
ARCHENTRY(strcmp, scalar)
/*
* Align s1 to word boundary.
* Consider unrolling loop?
*/
.Ls1align:
testb $7,%dil
je .Ls1aligned
movb (%rdi),%al
incq %rdi
movb (%rsi),%dl
incq %rsi
testb %al,%al
je .Ldone
cmpb %al,%dl
je .Ls1align
jmp .Ldone
/*
* Check whether s2 is aligned to a word boundary. If it is, we
* can compare by words. Otherwise we have to compare by bytes.
*/
.Ls1aligned:
testb $7,%sil
jne .Lbyte_loop
movabsq $0x0101010101010101,%r8
subq $8,%rdi
movabsq $0x8080808080808080,%r9
subq $8,%rsi
ALIGN_TEXT
.Lword_loop:
movq 8(%rdi),%rax
addq $8,%rdi
movq 8(%rsi),%rdx
addq $8,%rsi
cmpq %rax,%rdx
jne .Lbyte_loop
subq %r8,%rdx
notq %rax
andq %rax,%rdx
testq %r9,%rdx
je .Lword_loop
ALIGN_TEXT
.Lbyte_loop:
movb (%rdi),%al
incq %rdi
movb (%rsi),%dl
incq %rsi
testb %al,%al
je .Ldone
cmpb %al,%dl
je .Lbyte_loop
.Ldone:
movzbq %al,%rax
movzbq %dl,%rdx
subq %rdx,%rax
ret
ARCHEND(strcmp, scalar)
ARCHENTRY(strcmp, baseline)
/* check if either string crosses a page in the head */
lea 15(%rdi), %r8d # end of head
lea 15(%rsi), %r9d
mov %edi, %eax
mov %esi, %edx
xor %edi, %r8d # bits that changed between first and last byte
xor %esi, %r9d
and $~0xf, %rdi # align heads to 16 bytes
and $~0xf, %rsi
or %r8d, %r9d # in either RSI or RDI
and $0xf, %eax # offset from alignment
and $0xf, %edx
pxor %xmm1, %xmm1
test $PAGE_SIZE, %r9d # did the page change?
jz 0f # if not, take fast path
/* heads may cross page boundary, avoid unmapped loads */
movdqa (%rdi), %xmm0 # load aligned heads
movdqa (%rsi), %xmm2
mov $-1, %r8d
mov $-1, %r9d
mov %eax, %ecx
shl %cl, %r8d # string head in XMM0
mov %edx, %ecx
shl %cl, %r9d # string head in XMM2
movdqa %xmm0, -40(%rsp) # stash copies of the heads on the stack
movdqa %xmm2, -24(%rsp)
pcmpeqb %xmm1, %xmm0
pcmpeqb %xmm1, %xmm2
pmovmskb %xmm0, %r10d
pmovmskb %xmm2, %r11d
test %r8d, %r10d # NUL byte present in first string?
lea -40(%rsp), %r8
cmovz %rdi, %r8
test %r9d, %r11d # NUL byte present in second string?
lea -24(%rsp), %r9
cmovz %rsi, %r9
movdqu (%r8, %rax, 1), %xmm0 # load true (or fake) heads
movdqu (%r9, %rdx, 1), %xmm4
jmp 1f
0: movdqu (%rdi, %rax, 1), %xmm0 # load true heads
movdqu (%rsi, %rdx, 1), %xmm4
1: pxor %xmm2, %xmm2
pcmpeqb %xmm0, %xmm2 # NUL byte present?
pcmpeqb %xmm0, %xmm4 # which bytes match?
pandn %xmm4, %xmm2 # match and not NUL byte?
pmovmskb %xmm2, %r9d
xor $0xffff, %r9d # mismatch or NUL byte?
jnz .Lhead_mismatch
/* load head and second chunk */
movdqa 16(%rdi), %xmm2 # load second chunks
movdqa 16(%rsi), %xmm3
sub %rdx, %rax # is a&0xf >= b&0xf?
jb .Lswapped # if not, proceed with swapped operands
neg %rax
movdqu 16(%rsi, %rax, 1), %xmm0
sub %rdi, %rsi # express RSI as distance from RDI
lea (%rsi, %rax, 1), %rdx # point RDX to offset in second string
neg %rax
pcmpeqb %xmm3, %xmm1 # ... corresponding to RDI
pcmpeqb %xmm2, %xmm0
pmovmskb %xmm1, %r8d
pmovmskb %xmm0, %r9d
add $16, %rdi
test %r8d, %r8d
jnz .Lnul_found
xor $0xffff, %r9d
jnz .Lmismatch
add $16, %rdi # advance aligned pointers
/*
* During the main loop, the layout of the two strings is something like:
*
* v ------1------ v ------2------ v
* RDI: AAAAAAAAAAAAABBBBBBBBBBBBBBBB...
* RSI: AAAAAAAAAAAAABBBBBBBBBBBBBBBBCCC...
*
* where v indicates the alignment boundaries and corresponding chunks
* of the strings have the same letters. Chunk A has been checked in
* the previous iteration. This iteration, we first check that string
* RSI doesn't end within region 2, then we compare chunk B between the
* two strings. As RSI is known not to hold a NUL byte in regsions 1
* and 2 at this point, this also ensures that RDI has not ended yet.
*/
ALIGN_TEXT
0: movdqu (%rdi, %rdx, 1), %xmm0 # chunk of 2nd string corresponding to RDI?
pxor %xmm1, %xmm1
pcmpeqb (%rdi, %rsi, 1), %xmm1 # end of string in RSI?
pcmpeqb (%rdi), %xmm0 # where do the chunks match?
pmovmskb %xmm1, %r8d
pmovmskb %xmm0, %r9d
test %r8d, %r8d
jnz .Lnul_found
xor $0xffff, %r9d # any mismatches?
jnz .Lmismatch
/* main loop unrolled twice */
movdqu 16(%rdi, %rdx, 1), %xmm0 # chunk of 2nd string corresponding to RDI?
pxor %xmm1, %xmm1
pcmpeqb 16(%rdi, %rsi, 1), %xmm1 # end of string in RSI?
pcmpeqb 16(%rdi), %xmm0 # where do the chunks match?
pmovmskb %xmm1, %r8d
pmovmskb %xmm0, %r9d
add $32, %rdi
test %r8d, %r8d
jnz .Lnul_found2
xor $0xffff, %r9d # any mismatches?
jz 0b
sub $16, %rdi # roll back second increment
/* a mismatch has been found between RDX and RSI */
.Lmismatch:
tzcnt %r9d, %r9d # where is the mismatch?
add %rdi, %rdx # turn RDX from offset to pointer
movzbl (%rdx, %r9, 1), %ecx
movzbl (%rdi, %r9, 1), %eax
sub %ecx, %eax # difference of the mismatching chars
ret
/* mismatch in true heads */
.Lhead_mismatch:
tzcnt %r9d, %r9d # where is the mismatch?
add %rax, %rdi # return to true heads
add %rdx, %rsi
movzbl (%rdi, %r9, 1), %eax # mismatching characters
movzbl (%rsi, %r9, 1), %ecx
sub %ecx, %eax
ret
.Lnul_found2:
sub $16, %rdi # roll back second increment
/* a NUL has been found in RSI */
.Lnul_found:
mov %eax, %ecx
mov %r8d, %r10d
shl %cl, %r8w # adjust NUL mask to positions in RDI/RDX
xor $0xffff, %r9d # mask of mismatches
or %r8d, %r9d # NUL bytes also count as mismatches
jnz .Lmismatch
/*
* (RDI) == (RSI) and NUL is past the string.
* Compare (RSI) with the corresponding part
* of the other string until the NUL byte.
*/
movdqu (%rdi, %rax, 1), %xmm0
pcmpeqb (%rdi, %rsi, 1), %xmm0
add %rdi, %rsi # restore RSI pointer
add %rax, %rdi # point RDI to chunk corresponding to (RSI)
pmovmskb %xmm0, %ecx # mask of matches
not %ecx # mask of mismatches
or %r10d, %ecx # mask of mismatches or NUL bytes
tzcnt %ecx, %ecx # location of first mismatch
movzbl (%rdi, %rcx, 1), %eax
movzbl (%rsi, %rcx, 1), %ecx
sub %ecx, %eax
ret
/*
* If (a&0xf) < (b&0xf), we do the same thing but with swapped
* operands. I found that this performs slightly better than
* using conditional moves to do the swap branchless.
*/
.Lswapped:
movdqu 16(%rdi, %rax, 1), %xmm0
sub %rsi, %rdi # express RDI as distance from RSI
lea (%rdi, %rax, 1), %rdx # point RDX to offset in RDI corresponding to RSI
neg %rax # make difference positive
pcmpeqb %xmm2, %xmm1
pcmpeqb %xmm3, %xmm0
pmovmskb %xmm1, %r8d
pmovmskb %xmm0, %r9d
add $16, %rsi # advance aligned pointers
test %r8d, %r8d
jnz .Lnul_founds
xor $0xffff, %r9d
jnz .Lmismatchs
add $16, %rsi
/*
* During the main loop, the layout of the two strings is something like:
*
* v ------1------ v ------2------ v
* RDI: AAAAAAAAAAAAABBBBBBBBBBBBBBBB...
* RSI: AAAAAAAAAAAAABBBBBBBBBBBBBBBBCCC...
*
* where v indicates the alignment boundaries and corresponding chunks
* of the strings have the same letters. Chunk A has been checked in
* the previous iteration. This iteration, we first check that string
* RSI doesn't end within region 2, then we compare chunk B between the
* two strings. As RSI is known not to hold a NUL byte in regsions 1
* and 2 at this point, this also ensures that RDI has not ended yet.
*/
ALIGN_TEXT
0: movdqu (%rsi, %rdx, 1), %xmm0 # chunk of 2nd string corresponding to RDI?
pxor %xmm1, %xmm1
pcmpeqb (%rsi, %rdi, 1), %xmm1 # end of string in RSI?
pcmpeqb (%rsi), %xmm0 # where do the chunks match?
pmovmskb %xmm1, %r8d
pmovmskb %xmm0, %r9d
test %r8d, %r8d
jnz .Lnul_founds
xor $0xffff, %r9d # any mismatches?
jnz .Lmismatchs
/* main loop unrolled twice */
movdqu 16(%rsi, %rdx, 1), %xmm0 # chunk of 2nd string corresponding to RDI?
pxor %xmm1, %xmm1
pcmpeqb 16(%rsi, %rdi, 1), %xmm1 # end of string in RSI?
pcmpeqb 16(%rsi), %xmm0 # where do the chunks match?
pmovmskb %xmm1, %r8d
pmovmskb %xmm0, %r9d
add $32, %rsi
test %r8d, %r8d
jnz .Lnul_found2s
xor $0xffff, %r9d # any mismatches?
jz 0b
sub $16, %rsi # roll back second increment
/* a mismatch has been found between RDX and RDI */
.Lmismatchs:
tzcnt %r9d, %r9d # where is the mismatch?
add %rsi, %rdx # turn RDX from offset to pointer
movzbl (%rdx, %r9, 1), %eax
movzbl (%rsi, %r9, 1), %ecx
sub %ecx, %eax # difference of the mismatching chars
ret
.Lnul_found2s:
sub $16, %rsi # roll back second increment
/* a NUL has been found in RSI */
.Lnul_founds:
mov %eax, %ecx
mov %r8d, %r10d
shl %cl, %r8w # adjust NUL mask to positions in RDI/RDX
xor $0xffff, %r9d # mask of mismatches
or %r8d, %r9d # NUL bytes also count as mismatches
jnz .Lmismatchs
/*
* (RDI) == (RSI) and NUL is past the string.
* Compare (RSI) with the corresponding part
* of the other string until the NUL byte.
*/
movdqu (%rsi, %rax, 1), %xmm0
pcmpeqb (%rsi, %rdi, 1), %xmm0
add %rsi, %rdi # restore RDI pointer
add %rax, %rsi # point RSI to chunk corresponding to (RDI)
pmovmskb %xmm0, %ecx # mask of matches
not %ecx # mask of mismatches
or %r10d, %ecx # mask of mismatches or NUL bytes
tzcnt %ecx, %ecx # location of first mismatch
movzbl (%rdi, %rcx, 1), %eax
movzbl (%rsi, %rcx, 1), %ecx
sub %ecx, %eax
ret
ARCHEND(strcmp, baseline)
.section .note.GNU-stack,"",%progbits
|