2 * Copyright (c) 2005 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 strcmp(const char *s1, const char *s2);
31 // We optimize the compare by doing it in parallel, using SSE. 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.
41 _strcmp: // int strcmp(const char *s1,const char *s2);
44 movl 12(%esp),%esi // get LHS ptr
45 movl 16(%esp),%edi // get RHS ptr
48 // In order to avoid spurious page faults, we loop over:
50 // min( bytes_in_LHS_page, bytes_in_RHS_page) >> 4
52 // 16-byte chunks. When we near a page end, we have to revert to a byte-by-byte
53 // comparison until reaching the next page, then resume the vector comparison.
58 movl %esi,%eax // copy ptrs
60 andl $4095,%eax // mask down to page offsets
62 cmpl %eax,%edx // which is bigger?
63 cmova %edx,%eax // %eax = max(LHS offset, RHS offset);
65 subl %eax,%edx // get #bytes to next page crossing
67 shrl $4,%edx // get #chunks till end of operand or page
68 jnz LLoopOverChunks // enter vector loop
69 movl %eax,%edx // no chunks...
70 jmp LLoopOverBytes // ...so loop over bytes until page end
78 .align 4,0x90 // align inner loops to optimize I-fetch
80 movzb (%esi),%eax // get LHS byte
81 movzb (%edi),%ecx // get RHS byte
85 jz LExit0 // yes, we're done
86 subl %ecx,%eax // compare them
87 jnz LExit // done if not equal
88 dec %edx // more to go?
91 jmp LNextChunk // we've come to end of page
94 // Loop over 16-byte chunks.
99 .align 4,0x90 // align inner loops to optimize I-fetch
101 movdqu (%esi),%xmm1 // get LHS
102 movdqu (%edi),%xmm2 // get RHS
103 pxor %xmm0,%xmm0 // get some 0s in the shadow of the loads
105 pcmpeqb %xmm1,%xmm2 // compare LHS to RHS
106 pcmpeqb %xmm1,%xmm0 // compare LHS to 0s
108 pmovmskb %xmm2,%eax // get result mask for comparison of LHS and RHS
109 pmovmskb %xmm0,%ecx // get result mask for 0 check
110 xorl $0xFFFF,%eax // complement compare mask so 1 means "not equal"
111 orl %ecx,%eax // combine the masks and check for 1-bits
112 jnz LFoundDiffOr0 // we found differing bytes or a 0-byte
113 dec %edx // more to go?
116 jmp LNextChunk // compare up to next page boundary
119 // Found a zero and/or a difference in vector compare.
120 // %esi = LHS ptr, already advanced by 16
121 // %edi = RHS ptr, already advanced by 16
122 // %eax = bit n set if bytes n differed or were 0
125 bsf %eax,%edx // which byte differed or was 0?
126 subl $16,%esi // point to start of vectors while we wait for bit scan
128 movzb (%esi,%edx),%eax // get LHS byte
129 movzb (%edi,%edx),%ecx // get RHS byte
130 subl %ecx,%eax // compute difference (ie, return value)
136 // Found a zero and/or difference in byte loop.
141 subl %ecx,%eax // compute difference (ie, return value)
142 LExit: // here with difference already in %eax