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 // char *strcpy(const char *dst, const char *src);
31 // We optimize the move by doing it vector parallel. This introduces
32 // a complication: if we blindly did vector load/stores until finding
33 // a 0, we might get a spurious page fault by touching bytes past it.
34 // To avoid this, we never do a load that crosses a page boundary,
35 // and never store a byte we don't have to.
37 // We align the destination, because unaligned vector stores are slow.
43 _strcpy: // char *strcpy(const char *dst, const char *src);
45 movl 8(%esp),%edi // get dest ptr
46 movl 12(%esp),%ecx // get source ptr
47 movl %edi,%edx // copy dest ptr
49 andl $15,%edx // how many bytes to align dest ptr?
50 jnz LLoopOverBytes // not aligned, so go do so
53 // In order to avoid spurious page faults, we loop until nearing the source page
54 // end. Then we revert to a byte-by-byte loop for 16 bytes until the page is crossed,
55 // then resume the vector loop.
56 // %ecx = source ptr (unaligned)
57 // %edi = dest ptr (aligned)
60 movl %ecx,%eax // copy source ptr
62 andl $4095,%eax // get offset into source page
63 subl %eax,%edx // get #bytes remaining in source page
64 shrl $4,%edx // get #chunks till end of page
65 jnz LLoopOverChunks // enter vector loop
66 movl $16,%edx // move 16 bytes to cross page but keep dest aligned
75 .align 4,0x90 // align inner loops to optimize I-fetch
77 movzb (%ecx),%eax // get source byte
79 movb %al,(%edi) // pack into dest
82 jz LDone // yes, we're done
83 dec %edx // more to go?
86 jmp LNextChunk // we've come to end of page
89 // Loop over 16-byte chunks.
90 // %ecx = source ptr (unaligned)
91 // %edi = dest ptr (aligned)
94 .align 4,0x90 // align inner loops to optimize I-fetch
96 movdqu (%ecx),%xmm1 // get source
97 pxor %xmm0,%xmm0 // get some 0s
99 pcmpeqb %xmm1,%xmm0 // compare source to 0s
100 pmovmskb %xmm0,%eax // get result mask for 0 check
101 testl %eax,%eax // any 0s?
102 jnz LFound0 // yes, exit loop
103 movdqa %xmm1,(%edi) // no 0s so do aligned store into destination
105 dec %edx // more to go?
108 movl $16,%edx // move 16 bytes
109 jmp LLoopOverBytes // cross page but keep dest aligned
112 // Found a zero in the vector. Figure out where it is, and store the bytes
114 // %edi = dest ptr (aligned)
115 // %eax = result mask
116 // %xmm1 = source vector
119 bsf %eax,%edx // find first 0
120 inc %edx // we need to store the 0 too
121 test $16,%dl // was 0 last byte?
123 movdqa %xmm1,(%edi) // yes, store entire vector
126 test $8,%dl // 8-byte store required?
128 movq %xmm1,(%edi) // pack in 8 low bytes
129 psrldq $8,%xmm1 // then shift vector down 8 bytes
132 test $4,%dl // 4-byte store required?
134 movd %xmm1,(%edi) // pack in 4 low bytes
135 psrldq $4,%xmm1 // then shift vector down 4 bytes
138 andl $3,%edx // more to go?
140 movd %xmm1,%eax // move remainders out of vector into %eax
141 1: // loop on up to three bytes
142 movb %al,(%edi) // pack in next byte
143 shrl $8,%eax // shift next byte into position
149 movl 8(%esp),%eax // original dest ptr is return value