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);
44 movq %rdi,%rcx // preserve dest ptr so we can return it
45 movl %edi,%edx // copy low 4 bytes of dest ptr
47 andl $15,%edx // how many bytes to align dest ptr?
48 jnz LLoopOverBytes // not aligned, so go do so
51 // In order to avoid spurious page faults, we loop until nearing the source page
52 // end. Then we revert to a byte-by-byte loop for 16 bytes until the page is crossed,
53 // then resume the vector loop.
54 // %rsi = source ptr (unaligned)
55 // %rdi = dest ptr (aligned)
58 movl %esi,%eax // copy low 4 bytes of source ptr
60 andl $4095,%eax // get offset into source page
61 subl %eax,%edx // get #bytes remaining in source page
62 shrl $4,%edx // get #chunks till end of page
63 jnz LLoopOverChunks // enter vector loop
64 movl $16,%edx // move 16 bytes to cross page but keep dest aligned
73 .align 4,0x90 // align inner loops to optimize I-fetch
75 movzb (%rsi),%eax // get source byte
77 movb %al,(%rdi) // pack into dest
80 jz LDone // yes, we're done
81 subl $1,%edx // more to go?
84 jmp LNextChunk // we've come to end of page
87 // Loop over 16-byte chunks.
88 // %rsi = source ptr (unaligned)
89 // %rdi = dest ptr (aligned)
92 .align 4,0x90 // align inner loops to optimize I-fetch
94 movdqu (%rsi),%xmm1 // get source
95 pxor %xmm0,%xmm0 // get some 0s
97 pcmpeqb %xmm1,%xmm0 // compare source to 0s
98 pmovmskb %xmm0,%eax // get result mask for 0 check
99 testl %eax,%eax // any 0s?
100 jnz LFound0 // yes, exit loop
101 movdqa %xmm1,(%rdi) // no 0s so do aligned store into destination
103 subl $1,%edx // more to go?
106 movl $16,%edx // move 16 bytes
107 jmp LLoopOverBytes // cross page but keep dest aligned
110 // Found a zero in the vector. Figure out where it is, and store the bytes
112 // %rdi = dest ptr (aligned)
113 // %eax = result mask
114 // %xmm1 = source vector
117 bsf %eax,%edx // find first 0
118 addl $1,%edx // we need to store the 0 too
119 test $16,%dl // was 0 last byte?
121 movdqa %xmm1,(%rdi) // yes, store entire vector
124 test $8,%dl // 8-byte store required?
126 movq %xmm1,(%rdi) // pack in 8 low bytes
127 psrldq $8,%xmm1 // then shift vector down 8 bytes
130 test $4,%dl // 4-byte store required?
132 movd %xmm1,(%rdi) // pack in 4 low bytes
133 psrldq $4,%xmm1 // then shift vector down 4 bytes
136 andl $3,%edx // more to go?
138 movd %xmm1,%eax // move remainders out of vector into %eax
139 1: // loop on up to three bytes
140 movb %al,(%rdi) // pack in next byte
141 shrl $8,%eax // shift next byte into position
147 movq %rcx,%rax // original dest ptr is return value