2 * Copyright (c) 2007 Apple 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 // size_t strlcat(char *dst, const char *src, size_t size);
31 // We use SSE to do the initial strlen(), and word-parallel copies
32 // to do the move. This appears to be faster than either all SSE
33 // or all word-parallel, at least on Core2 class machines.
35 // Using 4- or 16-byte parallel loops introduce a complication:
36 // if we blindly did parallel load/stores until finding
37 // a 0, we might get a spurious page fault by touching bytes past it.
38 // To avoid this, we never do a load that crosses a page boundary,
39 // or store unnecessary bytes.
41 // The word parallel test for 0s relies on the following inobvious
42 // but very efficient test:
43 // x = dataWord + 0xFEFEFEFF
44 // y = ~dataWord & 0x80808080
45 // if (x & y) == 0 then no zero found
46 // The test maps any non-zero byte to zero, and any zero byte to 0x80,
47 // with one exception: 0x01 bytes preceeding the first zero are also
50 // On Core2 class machines, this algorithm seems to be faster than the
51 // naive byte-by-byte version for operands longer than about 10 bytes.
57 _strlcat: // size_t *strlcat(char *dst, const char *src, size_t size);
61 movl 16(%esp),%edi // get dest ptr
62 movl 20(%esp),%esi // get source ptr
63 movl 24(%esp),%ebx // get length of buffer
66 // Use SSE to find the 0-byte at current end of buffer.
67 // This is just a minor variant of strlen().
69 movl %edi,%ecx // copy buffer ptr
70 andl $(-16),%edi // 16-byte align buffer ptr
71 pxor %xmm0,%xmm0 // get some 0s
72 andl $15,%ecx // get #bytes in dq before start of buffer
75 subl %ecx,%edx // #bytes from buffer start to end of dq
76 subl %edx,%ebx // does buffer end before end of dq?
77 jb LShortBuf1 // yes, drop into byte-by-byte mode
78 movdqa (%edi),%xmm1 // get first aligned chunk of buffer
80 pcmpeqb %xmm0,%xmm1 // check for 0s
81 shl %cl,%eax // create mask for the bytes of aligned dq in operand
82 pmovmskb %xmm1,%ecx // collect mask of 0-bytes
83 andl %eax,%ecx // mask out any 0s that occur before buffer start
84 jnz 2f // found end of buffer
86 subl $16,%ebx // another dq in buffer?
87 jb LShortBuf2 // no, drop into byte-by-byte mode
88 movdqa (%edi),%xmm1 // get next chunk
90 pcmpeqb %xmm0,%xmm1 // check for 0s
91 pmovmskb %xmm1,%ecx // collect mask of 0-bytes
92 testl %ecx,%ecx // any 0-bytes?
95 bsf %ecx,%edx // find first 1-bit (ie, first 0-byte)
96 subl $16,%edi // back up ptr into buffer
97 addl $16,%ebx // recover length remaining as of start of dq
98 addl %edx,%edi // point to 0-byte
99 subl %edx,%ebx // compute #bytes remaining in buffer
102 // Copy byte-by-byte until source is 4-byte aligned.
103 // %edi = points to 1st byte available in buffer
105 // %ebx = buffer length remaining (ie, starting at %edi)
107 // NB: the rest of this code is cut-and-pasted from strlcpy().
109 movl %esi,%edx // copy source ptr
111 andl $3,%edx // how many bytes to align source ptr?
112 jz LAligned // already aligned
118 // %ebx = length remaining in buffer
119 // %edx = number of bytes to copy (>0, may not fit in buffer)
122 movzb (%esi),%eax // get source byte before checking buffer length
123 testl %ebx,%ebx // buffer full?
127 movb %al,(%edi) // pack into dest
129 testl %eax,%eax // 0?
130 jz LDone // yes, done
131 dec %edx // more to go?
135 // Source is aligned. Loop over words until end of buffer. We
136 // align the source, rather than the dest, to avoid getting spurious page faults.
137 // %edi = dest ptr (unaligned)
138 // %esi = source ptr (word aligned)
139 // %ebx = length remaining in buffer
142 movl $5,%edx // if buffer almost exhausted, prepare to copy rest byte-by-byte
143 cmpl $4,%ebx // enough for at least one word?
148 // %edi = dest ptr (unaligned)
149 // %esi = source ptr (word aligned)
150 // %ebx = length remaining in buffer (>=4)
153 movl (%esi),%eax // get next 4 bytes of source
156 movl %eax,%edx // make 2 copies of word
158 notl %edx // use magic word-parallel test for 0s
159 addl $0xFEFEFEFF,%ecx
160 andl $0x80808080,%edx
162 jnz L0Found // one of the bytes of %eax is a 0
163 movl %eax,(%edi) // pack 4 bytes into destination
165 cmpl $4,%ebx // room in buffer for another word?
166 jae LLoopOverWords // yes
168 movl %ebx,%edx // copy leftovers in byte loop
171 // Found a 0-byte in the word of source. Store a byte at a time until the 0.
172 // %edi = dest ptr (unaligned)
173 // %eax = last word of source, known to have a 0-byte
176 shrl $8,%eax // next byte
178 movb %al,(%edi) // pack in next byte
183 // Done storing string.
184 // %edi = ptr to byte after 0-byte
187 subl 16(%esp),%edi // subtract original dest ptr to get length stored
188 decl %edi // don't count the 0-byte
189 movl %edi,%eax // copy to return value
196 // Buffer filled but 0-byte not found. We return the length of the buffer plus the length
197 // of the source string. This is not optimized, as it is an error condition.
198 // %edi = dest ptr (ie, 1 past end of buffer)
199 // %esi = source ptr (ptr to 1st byte that does not fit)
202 movl 24(%esp),%eax // reload buffer length
203 testl %eax,%eax // null?
204 jz LScanSourceTo0 // yes, cannot store a 0
205 xorl %edx,%edx // get a 0
206 movb %dl,-1(%edi) // store a 0 at end of buffer to delimit string
208 movzb (%esi),%edx // get next byte of source
211 testl %edx,%edx // 0?
213 decl %eax // don't count the 0-byte
217 // Buffer too short to reach end of even one 16-byte aligned chunk.
221 movl 16(%esp),%edi // recover ptr to start of buffer
222 movl 24(%esp),%ebx // recover buffer length
226 // Out of aligned dq's of buffer, 0-byte still not found.
228 // %edi = 1st buffer byte not checked for 0
229 // %ebx = length remaining - 16
232 addl $16,%ebx // length remaining
234 movl 24(%esp),%eax // recover original buffer length, in case 0-byte not found
235 movl $17,%edx // buffer almost exhausted, prepare to copy byte-by-byte
237 testl %ebx,%ebx // no 0s in buffer at all?
238 jz LScanSourceTo0 // yes, cannot store a 0
239 cmpb $0,(%edi) // is this the 0?
240 jz LLoopOverBytes // yes, append source
243 jmp 1b // loop looking for 0