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 strlcpy(char *dst, const char *src, size_t size);
31 // We optimize the move by doing it quadword parallel. This introduces
32 // a complication: if we blindly did quadword 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 test for 0s relies on the following inobvious but very efficient
38 // word-parallel 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
49 // When initially entered:
50 // %rdi = dest buffer ptr
55 _strlcpy: // size_t *strlcpy(char *dst, const char *src, size_t size);
56 movl %esi,%ecx // copy source ptr
57 movq %rdi,%r10 // copy dest ptr
59 andl $7,%ecx // how many bytes to align source ptr?
60 jz LAligned // already aligned
66 // %rdx = length remaining in buffer
67 // %ecx = number of bytes to copy (>0, may not fit in buffer)
68 // %r10 = original dest ptr
71 movzb (%rsi),%eax // get source byte before checking buffer length
72 testq %rdx,%rdx // buffer full?
76 movb %al,(%rdi) // pack into dest
80 decl %ecx // more to go?
84 // Source is aligned. Loop over quadwords until end of buffer. We
85 // align the source, rather than the dest, to avoid getting spurious page faults.
86 // %rdi = dest ptr (unaligned)
87 // %rsi = source ptr (quadword aligned)
88 // %rdx = length remaining in buffer
89 // %r10 = original dest ptr
92 movl $9,%ecx // if buffer almost exhausted, prepare to copy rest byte-by-byte
93 cmpq $8,%rdx // enough for at least one word?
95 movq $0xFEFEFEFEFEFEFEFF,%rcx // load magic constants
96 movq $0x8080808080808080,%r11
99 // Loop over quadwords.
100 // %rdi = dest ptr (unaligned)
101 // %rsi = source ptr (word aligned)
102 // %rdx = length remaining in buffer (>=8)
103 // %rcx = 0xFEFEFEFEFEFEFEFF
104 // %r11 = 0x8080808080808080
105 // %r10 = original dest ptr
108 movq (%rsi),%rax // get next 8 bytes of source
111 movq %rax,%r8 // make 2 copies of quadword
113 notq %r8 // use magic word-parallel test for 0s
117 jnz L0Found // one of the bytes of %rax is a 0
118 movq %rax,(%rdi) // pack 8 bytes into destination
120 cmpq $8,%rdx // room in buffer for another quadword?
121 jae LLoopOverQuads // yes
123 movl %edx,%ecx // copy leftovers in byte loop
126 // Found a 0-byte in the quadword of source. Store a byte at a time until the 0.
127 // %rdi = dest ptr (unaligned)
128 // %rax = last quadword of source, known to have a 0-byte
129 // %r10 = original dest ptr
132 shrq $8,%rax // next byte
134 movb %al,(%rdi) // pack in next byte
139 // Done storing string.
140 // %rdi = ptr to byte after 0-byte
141 // %r10 = original dest ptr
144 subq %r10,%rdi // subtract original dest ptr to get length stored
145 lea -1(%rdi),%rax // minus one for 0-byte, and move to return value
148 // Buffer filled but 0-byte not found. We return the length of the source string.
149 // This is not optimized, as it is an error condition.
150 // %rdi = dest ptr (ie, 1 past end of buffer)
151 // %rsi = source ptr (ptr to 1st byte that does not fit)
152 // %r10 = original dest ptr
155 movq %rdi,%rax // end of buffer...
156 subq %r10,%rax // ...minus start is buffer length
157 jz 1f // 0-length buffer, cannot store a 0
158 xorl %edx,%edx // get a 0
159 movb %dl,-1(%rdi) // store a 0 at end of buffer to delimit string
161 movzb (%rsi),%ecx // get next byte of source
164 testl %ecx,%ecx // 0?
166 decq %rax // don't count the 0-byte