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@
25 // *************** ***********
26 // * M E M C M P * and * B C M P *
27 // *************** ***********
29 // int memcmp(const char *s1, const char *s2, size_t len);
30 // int bcmp(const char *s1, const char *s2, size_t len);
32 // Bcmp returns (+,0,-), whereas memcmp returns the true difference
33 // between the first differing bytes, but we treat them identically.
35 // We optimize the compare by doing it with SSE. This introduces
36 // a complication: if we blindly did vector loads from both sides until
37 // finding a difference, we might get a spurious page fault by
38 // reading bytes past the difference. To avoid this, we never do a load
39 // that crosses a page boundary.
41 #define kShort 18 // too short for vectors (must be >16)
49 _memcmp: // int memcmp(const char *s1,const char *s2,size_t len);
50 _bcmp: // int bcmp(const char *s1,const char *s2,size_t len);
51 cmpq $(kShort),%rdx // worth accelerating?
55 // Too short to bother with parallel compares. Loop over bytes.
58 // %edx = length (<= kShort)
61 testl %edx,%edx // 0-length?
63 xorq %rax,%rax // return 0
65 .align 4,0x90 // align inner loops to optimize I-fetch
66 LShortLoop: // loop over bytes
67 movzb (%rdi),%eax // get LHS byte
68 movzb (%rsi),%ecx // get RHS byte
71 subl %ecx,%eax // compare them
72 jnz LExit // done if not equal
73 subq $1,%rdx // decrement length
75 LExit: // return value is in %eax
78 LNotEqual: // here from LLoopOverBytes with LHS in eax
79 movzb (%rsi),%ecx // get RHS byte
80 subl %ecx,%eax // generate return value (nonzero)
84 // Loop over bytes until we reach end of a page.
87 // %rdx = length remaining after end of loop (i.e., already adjusted)
88 // %ecx = #bytes until next page (1..15)
90 .align 4,0x90 // align inner loops to optimize I-fetch
92 movzb (%rdi),%eax // get LHS byte
94 cmpb (%rsi),%al // compare to RHS byte
95 jnz LNotEqual // done if not equal
97 subl $1,%ecx // more to go?
101 // Long enough to justify overhead of setting up vector compares. In order to
102 // avoid spurious page faults, we loop over:
104 // min( length, bytes_in_LHS_page, bytes_in_RHS_page) >> 4
106 // 16-byte chunks. When we near a page end, we have to revert to a byte-by-byte
107 // comparison until reaching the next page, then resume the vector comparison.
110 // %rdx = length (> kShort)
113 movq %rdi,%rax // copy ptrs
115 andq $4095,%rax // mask down to page offsets
117 cmpq %rax,%rcx // which is bigger?
118 cmova %rcx,%rax // %eax = max(LHS offset, RHS offset);
120 subl %eax,%ecx // get #bytes to next page crossing
121 cmpq %rdx,%rcx // will operand run out first?
122 cmova %edx,%ecx // get min(length remaining, bytes to page end)
124 shrl $4,%ecx // get #chunks till end of operand or page
125 jnz LLoopOverChunks // enter vector loop
127 // Too near page end for vectors.
129 subq %rax,%rdx // adjust length remaining
130 movl %eax,%ecx // %ecx <- #bytes to page end
131 cmpq $(kShort),%rdx // will there be enough after we cross page for vectors?
132 ja LLoopOverBytes // yes
133 addq %rax,%rdx // no, restore total length remaining
134 jmp LShortLoop // compare rest byte-by-byte (%ecx != 0)
137 // Loop over 16-byte chunks.
140 // %rdx = length remaining
141 // %ecx = chunk count
143 .align 4,0x90 // align inner loops to optimize I-fetch
145 movdqu (%rdi),%xmm0 // get LHS
146 movdqu (%rsi),%xmm1 // get RHS
148 pcmpeqb %xmm1,%xmm0 // compare LHS to RHS
150 pmovmskb %xmm0,%eax // collect comparison result bits (1 if equal)
151 subq $16,%rdx // adjust length remaining
152 xorl $0xFFFF,%eax // all equal?
153 jne LDifferent // no, we found differing bytes
154 subl $1,%ecx // more to go?
157 cmpq $(kShort),%rdx // a lot more to compare?
159 jmp LNotShort // compute distance to next page crossing etc
162 // Found a difference.
163 // %rdi = LHS ptr, already advanced by 16
164 // %rsi = RHS ptr, already advanced by 16
165 // %eax = complemented compare vector (ie, 0 == equal)
168 bsf %eax,%edx // which byte differed?
169 subq $16,%rdi // point to byte 0 while we wait for bit scan
171 movzb (%rdi,%rdx),%eax // get LHS byte
172 movzb (%rsi,%rdx),%ecx // get RHS byte
173 subl %ecx,%eax // compute difference (ie, return value)