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 // Using 8- or 16-byte parallel loops introduce a complication:
32 // if we blindly did parallel 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 // or store unnecessary bytes.
37 // The word parallel test for 0s relies on the following inobvious
38 // but very efficient test:
39 // x = dataWord + 0xFEFEFEFF
40 // y = ~dataWord & 0x80808080
41 // if (x & y) == 0 then no zero found
42 // The test maps any non-zero byte to zero, and any zero byte to 0x80,
43 // with one exception: 0x01 bytes preceeding the first zero are also
46 // On Core2 class machines, this algorithm seems to be faster than the
47 // naive byte-by-byte version for operands longer than about 11 bytes.
54 // Use SSE to find the 0-byte at current end of buffer.
55 // This is just a minor variant of strlen().
57 // %rdi = dest or buffer ptr
62 _strlcat: // size_t *strlcat(char *dst, const char *src, size_t size);
63 movl %edi,%ecx // copy buffer ptr
64 movq %rdi,%r10 // save copies of buffer ptr and length
66 andq $(-16),%rdi // 16-byte align buffer ptr
67 pxor %xmm0,%xmm0 // get some 0s
68 andl $15,%ecx // get #bytes in dq before start of buffer
71 subl %ecx,%r8d // #bytes from buffer start to end of dq
72 subq %r8,%rdx // does buffer end before end of dq?
73 jb LShortBuf1 // yes, drop into byte-by-byte mode
74 movdqa (%rdi),%xmm1 // get first aligned chunk of buffer
76 pcmpeqb %xmm0,%xmm1 // check for 0s
77 shl %cl,%eax // create mask for the bytes of aligned dq in operand
78 pmovmskb %xmm1,%ecx // collect mask of 0-bytes
79 andl %eax,%ecx // mask out any 0s that occur before buffer start
80 jnz 2f // found end of buffer
82 subq $16,%rdx // another dq in buffer?
83 jb LShortBuf2 // no, drop into byte-by-byte mode
84 movdqa (%rdi),%xmm1 // get next chunk
86 pcmpeqb %xmm0,%xmm1 // check for 0s
87 pmovmskb %xmm1,%ecx // collect mask of 0-bytes
88 testl %ecx,%ecx // any 0-bytes?
91 bsf %ecx,%ecx // find first 1-bit (ie, first 0-byte)
92 subq $16,%rdi // back up ptr into buffer
93 addq $16,%rdx // recover length remaining as of start of dq
94 addq %rcx,%rdi // point to 0-byte
95 subq %rcx,%rdx // compute #bytes remaining in buffer
98 // Copy byte-by-byte until source is 8-byte aligned.
99 // %rdi = points to 1st byte available in buffer
101 // %rdx = buffer length remaining (ie, starting at %rdi)
102 // %r10 = original buffer ptr
104 movl %esi,%ecx // copy source ptr
106 andl $7,%ecx // how many bytes to align source ptr?
107 jz LAligned // already aligned
113 // %rdx = length remaining in buffer
114 // %ecx = number of bytes to copy (>0, may not fit in buffer)
115 // %r10 = original buffer ptr
118 movzb (%rsi),%eax // get source byte before checking buffer length
119 testq %rdx,%rdx // buffer full?
123 movb %al,(%rdi) // pack into dest
125 testl %eax,%eax // 0?
126 jz LDone // yes, done
127 decl %ecx // more to go?
131 // Source is aligned. Loop over quadwords until end of buffer. We
132 // align the source, rather than the dest, to avoid getting spurious page faults.
133 // %rdi = dest ptr (unaligned)
134 // %rsi = source ptr (quadword aligned)
135 // %rdx = length remaining in buffer
136 // %r10 = original buffer ptr
139 movl $9,%ecx // if buffer almost exhausted, prepare to copy rest byte-by-byte
140 cmpq $8,%rdx // enough for at least one quadword?
142 movq $0xFEFEFEFEFEFEFEFF,%r8 // get magic constants
143 movq $0x8080808080808080,%r9
146 // Loop over quadwords.
147 // %rdi = dest ptr (unaligned)
148 // %rsi = source ptr (quadword aligned)
149 // %rdx = length remaining in buffer (>=8)
150 // %r8 = 0xFEFEFEFEFEFEFEFF
151 // %r9 = 0x8080808080808080
152 // %r10 = original buffer ptr
155 movq (%rsi),%rax // get next 8 bytes of source
158 movq %rax,%r11 // make 2 copies of quadword
160 notq %r11 // use magic word-parallel test for 0s
164 jnz L0Found // one of the bytes of %rax is a 0
165 movq %rax,(%rdi) // pack 8 bytes into destination
167 cmpq $8,%rdx // room in buffer for another quadword?
168 jae LLoopOverQuads // yes
170 movl %edx,%ecx // copy leftovers in byte loop
173 // Found a 0-byte in the quadword of source. Store a byte at a time until the 0.
174 // %rdi = dest ptr (unaligned)
175 // %rax = last word of source, known to have a 0-byte
176 // %r10 = original buffer ptr
179 shrq $8,%rax // next byte
181 movb %al,(%rdi) // pack in next byte
187 // Done storing string.
188 // %rdi = ptr to byte after 0-byte
189 // %r10 = original buffer ptr
192 subq %r10,%rdi // subtract original dest ptr to get length stored
193 lea -1(%rdi),%rax // minus one for 0-byte, and move 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)
200 // %rdx = 0 (ie, length remaining in buffer)
201 // %r10 = original buffer ptr
204 movq %rdi,%rax // buffer end...
205 subq %r10,%rax // ...minus start is buffer length
206 jz LScanSourceTo0 // buffer is null so cannot store a 0
207 movb %dl,-1(%rdi) // store a 0 at end of buffer to delimit string
210 // Scan source to end.
211 // %rsi = ptr to rest of source
212 // %rax = return value so far
215 movzb (%rsi),%ecx // get next byte of source
217 incq %rax // increment length
218 testl %ecx,%ecx // 0?
220 decq %rax // don't count the 0-byte
224 // Buffer too short to reach end of even one 16-byte aligned chunk.
226 // %r10 = original buffer ptr
227 // %r11 = original buffer length
230 movq %r10,%rdi // recover buffer ptr
231 movq %r11,%rdx // recover buffer length
235 // Out of aligned dq's of buffer, 0-byte still not found.
237 // %rdi = ptr to 1st buffer byte not checked for 0
238 // %rdx = length remaining - 16
239 // %r10 = original buffer ptr
240 // %r11 = original buffer length
243 addq $16,%rdx // recover length remaining
245 movq %r11,%rax // in case we goto LScanSourceTo0
246 movl $17,%ecx // in case we goto LLoopOverBytes
248 testq %rdx,%rdx // no 0s in buffer at all?
249 jz LScanSourceTo0 // yes, cannot store a 0
250 cmpb $0,(%rdi) // is this the 0?
251 jz LLoopOverBytes // yes, append source
254 jmp 1b // loop looking for 0