2 * Copyright (c) 2005-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
29 // int strncmp(const char *s1, const char *s2, size_t len);
31 // We optimize the compare by doing it vector parallel. This introduces
32 // a complication: if we blindly did vector loads from both sides until
33 // finding a difference (or 0), we might get a spurious page fault by
34 // reading bytes past the difference. To avoid this, we never do a load
35 // that crosses a page boundary.
37 #define kShort 20 // too short for vectors (must be >16)
43 _strncmp: // int strncmp(const char *s1, const char *s2, size_t len);
44 cmpq $(kShort),%rdx // worth accelerating?
48 // Too short to bother with parallel compares. Loop over bytes.
51 // %edx = length (<= kShort)
54 testl %edx,%edx // 0-length?
56 jmp LReturn0 // yes, return 0
57 .align 4,0x90 // align inner loops to optimize I-fetch
58 LShortLoop: // loop over bytes
59 movzb (%rdi),%eax // get LHS byte
60 movzb (%rsi),%ecx // get RHS byte
63 testl %eax,%eax // LHS==0 ?
64 jz LNotEqual // yes, this terminates comparison
65 cmpl %ecx,%eax // compare them (sub won't fuse, but cmp will)
66 jnz LNotEqual // done if not equal
67 decl %edx // decrement length
70 xorl %eax,%eax // all bytes equal, so return 0
73 LNotEqual: // LHS in eax, RHS in ecx
74 subl %ecx,%eax // generate return value (nonzero)
78 // Loop over bytes until we reach end of a page.
81 // %rdx = length remaining after end of loop (ie, already adjusted)
82 // %r8d = #bytes until next page (1..15)
84 .align 4,0x90 // align inner loops to optimize I-fetch
86 movzb (%rdi),%eax // get LHS byte
87 movzb (%rsi),%ecx // get RHS byte
90 testl %eax,%eax // LHS==0 ?
91 jz LNotEqual // yes, this terminates comparison
92 cmpl %ecx,%eax // compare them (sub won't fuse, but cmp will)
93 jnz LNotEqual // done if not equal
94 decl %r8d // more to go?
98 // Long enough to justify overhead of setting up vector compares. In order to
99 // avoid spurious page faults, we loop over:
101 // min( length, bytes_in_LHS_page, bytes_in_RHS_page) >> 4
103 // 16-byte chunks. When we near a page end, we have to revert to a byte-by-byte
104 // comparison until reaching the next page, then resume the vector comparison.
107 // %rdx = length (> kShort)
110 movl %edi,%eax // copy ptrs
112 andl $4095,%eax // mask down to page offsets
114 cmpl %eax,%r8d // which is bigger?
115 cmova %r8d,%eax // %eax = max(LHS offset, RHS offset);
117 subl %eax,%r8d // get #bytes to next page crossing
118 cmpq %rdx,%r8 // will operand run out first?
119 cmova %rdx,%r8 // get min(length remaining, bytes to page end)
120 movl %r8d,%eax // copy #bytes
121 shrl $4,%r8d // get #chunks till end of operand or page
122 testl %r8d,%r8d // test %r8d for 0 without partial flag update stall
123 jnz LLoopOverChunks // enter vector loop
125 // Too near page end for vectors.
127 subq %rax,%rdx // adjust length remaining
128 movl %eax,%r8d // %r8d <- #bytes to page end
129 cmpq $(kShort),%rdx // will there be enough after we cross page for vectors?
130 ja LLoopOverBytes // yes
131 addq %rax,%rdx // no, restore total length remaining
132 jmp LShortLoop // compare rest byte-by-byte (%rdx != 0)
135 // Loop over 16-byte chunks.
138 // %rdx = length remaining
139 // %r8d = chunk count
141 .align 4,0x90 // align inner loops to optimize I-fetch
143 movdqu (%rdi),%xmm1 // get LHS
144 movdqu (%rsi),%xmm2 // get RHS
145 pxor %xmm0,%xmm0 // get some 0s in the shadow of the loads
147 pcmpeqb %xmm1,%xmm2 // compare LHS to RHS
148 pcmpeqb %xmm1,%xmm0 // compare LHS to 0s
150 pmovmskb %xmm2,%eax // get result mask for comparison of LHS and RHS
151 pmovmskb %xmm0,%ecx // get result mask for 0 check
152 subq $16,%rdx // decrement length remaining
153 xorl $0xFFFF,%eax // complement compare mask so 1 means "not equal"
154 orl %ecx,%eax // combine the masks and check for 1-bits
155 jnz LFoundDiffOr0 // we found differing bytes or a 0-byte
156 decl %r8d // more to go?
157 jnz LLoopOverChunks // yes
159 cmpq $(kShort),%rdx // a lot more to compare?
161 jmp LNotShort // compute distance to next page crossing etc
164 // Found a zero and/or a difference in vector compare.
165 // %rdi = LHS ptr, already advanced by 16
166 // %rsi = RHS ptr, already advanced by 16
167 // %eax = bit n set if bytes n differed or were 0
170 bsf %eax,%edx // which byte differed or was 0?
171 movzb -16(%rdi,%rdx),%eax // get LHS byte
172 movzb -16(%rsi,%rdx),%edx // get RHS byte
173 subl %edx,%eax // compute difference (ie, return value)