]> git.saurik.com Git - apple/libc.git/blame - x86_64/string/strlcpy.s
Libc-594.9.5.tar.gz
[apple/libc.git] / x86_64 / string / strlcpy.s
CommitLineData
224c7076
A
1/*
2 * Copyright (c) 2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25// *****************
26// * S T R L C P Y *
27// *****************
28//
29// size_t strlcpy(char *dst, const char *src, size_t size);
30//
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.
36//
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
44// mapped to 0x80.
45
46 .text
47 .globl _strlcpy
48
49// When initially entered:
50// %rdi = dest buffer ptr
51// %rsi = source ptr
52// %rdx = length
53
54 .align 4
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
58 negl %ecx
59 andl $7,%ecx // how many bytes to align source ptr?
60 jz LAligned // already aligned
61
62
63// Loop over bytes.
64// %rdi = dest ptr
65// %rsi = source ptr
66// %rdx = length remaining in buffer
67// %ecx = number of bytes to copy (>0, may not fit in buffer)
68// %r10 = original dest ptr
69
70LLoopOverBytes:
71 movzb (%rsi),%eax // get source byte before checking buffer length
72 testq %rdx,%rdx // buffer full?
73 jz L0NotFound // yes
74 incq %rsi
75 decq %rdx
76 movb %al,(%rdi) // pack into dest
77 incq %rdi
78 testl %eax,%eax // 0?
79 jz LDone // yes, done
80 decl %ecx // more to go?
81 jnz LLoopOverBytes
82
83
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
90
91LAligned:
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?
94 jb LLoopOverBytes
95 movq $0xFEFEFEFEFEFEFEFF,%rcx // load magic constants
96 movq $0x8080808080808080,%r11
97
98
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
106
107LLoopOverQuads:
108 movq (%rsi),%rax // get next 8 bytes of source
109 subq $8,%rdx
110 addq $8,%rsi
111 movq %rax,%r8 // make 2 copies of quadword
112 movq %rax,%r9
113 notq %r8 // use magic word-parallel test for 0s
114 addq %rcx,%r9
115 andq %r11,%r8
116 testq %r8,%r9
117 jnz L0Found // one of the bytes of %rax is a 0
118 movq %rax,(%rdi) // pack 8 bytes into destination
119 addq $8,%rdi
120 cmpq $8,%rdx // room in buffer for another quadword?
121 jae LLoopOverQuads // yes
122
123 movl %edx,%ecx // copy leftovers in byte loop
124 jmp LLoopOverBytes
125
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
130
131LNextByte:
132 shrq $8,%rax // next byte
133L0Found:
134 movb %al,(%rdi) // pack in next byte
135 incq %rdi
136 testb %al,%al // 0?
137 jnz LNextByte
138
139// Done storing string.
140// %rdi = ptr to byte after 0-byte
141// %r10 = original dest ptr
142
143LDone:
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
146 ret
147
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
153
154L0NotFound:
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
1601:
161 movzb (%rsi),%ecx // get next byte of source
162 incq %rsi
163 incq %rax
164 testl %ecx,%ecx // 0?
165 jnz 1b
166 decq %rax // don't count the 0-byte
167 ret