]> git.saurik.com Git - apple/libc.git/blame - ppc/string/strlcpy.s
Libc-320.tar.gz
[apple/libc.git] / ppc / string / strlcpy.s
CommitLineData
9385eb3d
A
1/*
2 * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25#define ASSEMBLER
26#include <mach/ppc/asm.h>
27#undef ASSEMBLER
28
29// *****************
30// * S T R L C P Y *
31// *****************
32//
33// size_t strlcpy(char *dst, const char *src, size_t size);
34//
35// We optimize the move by doing it word parallel. This introduces
36// a complication: if we blindly did word 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 "lwz" that crosses a page boundary,
39// or store unnecessary bytes.
40//
41// The test for 0s relies on the following inobvious but very efficient
42// word-parallel 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
48// mapped to 0x80.
49
50 .text
51 .globl EXT(strlcpy)
52
53 .align 5
54LEXT(strlcpy)
55 andi. r0,r4,3 // is source aligned?
56 dcbt 0,r4 // touch in source
57 lis r6,hi16(0xFEFEFEFF) // start to load magic constants
58 lis r7,hi16(0x80808080)
59 dcbtst 0,r3 // touch in dst
60 ori r6,r6,lo16(0xFEFEFEFF)
61 ori r7,r7,lo16(0x80808080)
62 mr r9,r3 // use r9 for dest ptr (r3 remembers dst start)
63 beq Laligned // source is aligned
64 subfic r0,r0,4 // r0 <- #bytes to word align source
65
66// Copy min(r0,r5) bytes, until 0-byte found.
67// r0 = #bytes we propose to copy (NOTE: must be >0)
68// r4 = source ptr (unaligned)
69// r5 = length remaining in buffer (may be 0)
70// r6 = 0xFEFEFEFF
71// r7 = 0x80808080
72// r9 = dest ptr (unaligned)
73
74Lbyteloop:
75 cmpwi r5,0 // buffer empty?
76 beq-- L0notfound // buffer full but 0 not found
77 lbz r8,0(r4) // r8 <- next source byte
78 subic. r0,r0,1 // decrement count of bytes to move
79 addi r4,r4,1
80 subi r5,r5,1 // decrement buffer length remaining
81 stb r8,0(r9) // pack into dest
82 cmpwi cr1,r8,0 // 0-byte?
83 addi r9,r9,1
84 beq cr1,L0found // byte was 0
85 bne Lbyteloop // r0!=0, source not yet aligned
86
87// Source is word aligned. Loop over words until end of buffer. We align
88// the source, rather than the dest, to avoid getting spurious page faults.
89// r4 = source ptr (word aligned)
90// r5 = length remaining in buffer
91// r6 = 0xFEFEFEFF
92// r7 = 0x80808080
93// r9 = dest ptr (unaligned)
94
95Laligned:
96 srwi. r8,r5,2 // get #words in buffer
97 addi r0,r5,1 // if no words, compare rest of buffer
98 beq Lbyteloop // r8==0, no words
99 mtctr r8 // set up word loop count
100 rlwinm r5,r5,0,0x3 // mask buffer length down to leftover bytes
101 b LwordloopEnter
102
103// Move a word at a time, until one of two conditions:
104// - a zero byte is found
105// - end of buffer
106// At this point, registers are as follows:
107// r4 = source ptr (word aligned)
108// r5 = leftover bytes in buffer (0..3)
109// r6 = 0xFEFEFEFF
110// r7 = 0x80808080
111// r9 = dest ptr (unaligned)
112// ctr = whole words left in buffer
113
114 .align 5 // align inner loop, which is 8 words long
115Lwordloop:
116 stw r8,0(r9) // pack word into destination
117 addi r9,r9,4
118LwordloopEnter:
119 lwz r8,0(r4) // r8 <- next 4 source bytes
120 addi r4,r4,4
121 add r10,r8,r6 // r10 <- word + 0xFEFEFEFF
122 andc r12,r7,r8 // r12 <- ~word & 0x80808080
123 and. r11,r10,r12 // r11 <- nonzero iff word has a 0-byte
124 bdnzt eq,Lwordloop // loop if ctr!=0 and cr0_eq
125
126 beq Lleftovers // 0-byte not found in aligned words
127
128// Found a 0-byte. Store last word up to and including the 0, a byte at a time.
129// r8 = last word, known to have a 0-byte
130// r9 = dest ptr
131
132Lstorelastbytes:
133 srwi. r0,r8,24 // right justify next byte and test for 0
134 slwi r8,r8,8 // shift next byte into position
135 stb r0,0(r9) // pack into dest
136 addi r9,r9,1
137 bne Lstorelastbytes // loop until 0 stored
138
139L0found:
140 sub r3,r9,r3 // get #bytes stored, including 0
141 subi r3,r3,1 // don't count the 0
142 blr // return strlen(src)
143
144// 0-byte not found in aligned source words. There are up to 3 leftover source
145// bytes, hopefully the 0-byte is among them.
146// r4 = source ptr (word aligned)
147// r5 = leftover bytes in buffer (0..3)
148// r6 = 0xFEFEFEFF
149// r7 = 0x80808080
150// r8 = last full word of source
151// r9 = dest ptr (unaligned)
152
153Lleftovers:
154 stw r8,0(r9) // store last word
155 addi r9,r9,4
156 addi r0,r5,1 // make sure r5 terminate byte loop (not r0)
157 b Lbyteloop
158
159// Buffer full but 0-byte not found. Stuff a 0 into last byte of buffer.
160// r3 = start of buffer
161// r4 = ptr to next byte in source
162// r9 = ptr to first byte past end of buffer
163
164L0notfound:
165 sub. r3,r9,r3 // get #bytes stored, ie original buffer length
166 beq Lfind0 // skip if buffer 0-length
167 li r0,0 // get a 0
168 stb r0,-1(r9) // always store 0-byte unless buffer was 0-length
169
170// Keep searching for 0-byte ending source, so we can return strlen(source).
171// Not optimized, since this is an error condition.
172// r3 = number of bytes already copied
173// r4 = ptr to next byte in source
174
175Lfind0:
176 lbz r0,0(r4) // get next byte
177 addi r4,r4,1
178 addi r3,r3,1 // increment strlen
179 cmpwi r0,0
180 bne Lfind0 // loop if not 0
181
182 subi r3,r3,1 // don't count the 0-byte
183 blr // return strlen(source)