]>
Commit | Line | Data |
---|---|---|
9385eb3d A |
1 | /* |
2 | * Copyright (c) 2002 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
9385eb3d A |
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 | #define ASSEMBLER | |
24 | #include <mach/ppc/asm.h> | |
25 | #undef ASSEMBLER | |
26 | ||
27 | // ***************** | |
28 | // * S T R L C P Y * | |
29 | // ***************** | |
30 | // | |
31 | // size_t strlcpy(char *dst, const char *src, size_t size); | |
32 | // | |
33 | // We optimize the move by doing it word parallel. This introduces | |
34 | // a complication: if we blindly did word load/stores until finding | |
35 | // a 0, we might get a spurious page fault by touching bytes past it. | |
36 | // To avoid this, we never do a "lwz" that crosses a page boundary, | |
37 | // or store unnecessary bytes. | |
38 | // | |
39 | // The test for 0s relies on the following inobvious but very efficient | |
40 | // word-parallel test: | |
41 | // x = dataWord + 0xFEFEFEFF | |
42 | // y = ~dataWord & 0x80808080 | |
43 | // if (x & y) == 0 then no zero found | |
44 | // The test maps any non-zero byte to zero, and any zero byte to 0x80, | |
45 | // with one exception: 0x01 bytes preceeding the first zero are also | |
46 | // mapped to 0x80. | |
47 | ||
48 | .text | |
49 | .globl EXT(strlcpy) | |
50 | ||
51 | .align 5 | |
52 | LEXT(strlcpy) | |
53 | andi. r0,r4,3 // is source aligned? | |
54 | dcbt 0,r4 // touch in source | |
55 | lis r6,hi16(0xFEFEFEFF) // start to load magic constants | |
56 | lis r7,hi16(0x80808080) | |
57 | dcbtst 0,r3 // touch in dst | |
58 | ori r6,r6,lo16(0xFEFEFEFF) | |
59 | ori r7,r7,lo16(0x80808080) | |
60 | mr r9,r3 // use r9 for dest ptr (r3 remembers dst start) | |
61 | beq Laligned // source is aligned | |
62 | subfic r0,r0,4 // r0 <- #bytes to word align source | |
63 | ||
64 | // Copy min(r0,r5) bytes, until 0-byte found. | |
65 | // r0 = #bytes we propose to copy (NOTE: must be >0) | |
66 | // r4 = source ptr (unaligned) | |
67 | // r5 = length remaining in buffer (may be 0) | |
68 | // r6 = 0xFEFEFEFF | |
69 | // r7 = 0x80808080 | |
70 | // r9 = dest ptr (unaligned) | |
71 | ||
72 | Lbyteloop: | |
73 | cmpwi r5,0 // buffer empty? | |
74 | beq-- L0notfound // buffer full but 0 not found | |
75 | lbz r8,0(r4) // r8 <- next source byte | |
76 | subic. r0,r0,1 // decrement count of bytes to move | |
77 | addi r4,r4,1 | |
78 | subi r5,r5,1 // decrement buffer length remaining | |
79 | stb r8,0(r9) // pack into dest | |
80 | cmpwi cr1,r8,0 // 0-byte? | |
81 | addi r9,r9,1 | |
82 | beq cr1,L0found // byte was 0 | |
83 | bne Lbyteloop // r0!=0, source not yet aligned | |
84 | ||
85 | // Source is word aligned. Loop over words until end of buffer. We align | |
86 | // the source, rather than the dest, to avoid getting spurious page faults. | |
87 | // r4 = source ptr (word aligned) | |
88 | // r5 = length remaining in buffer | |
89 | // r6 = 0xFEFEFEFF | |
90 | // r7 = 0x80808080 | |
91 | // r9 = dest ptr (unaligned) | |
92 | ||
93 | Laligned: | |
94 | srwi. r8,r5,2 // get #words in buffer | |
95 | addi r0,r5,1 // if no words, compare rest of buffer | |
96 | beq Lbyteloop // r8==0, no words | |
97 | mtctr r8 // set up word loop count | |
98 | rlwinm r5,r5,0,0x3 // mask buffer length down to leftover bytes | |
99 | b LwordloopEnter | |
100 | ||
101 | // Move a word at a time, until one of two conditions: | |
102 | // - a zero byte is found | |
103 | // - end of buffer | |
104 | // At this point, registers are as follows: | |
105 | // r4 = source ptr (word aligned) | |
106 | // r5 = leftover bytes in buffer (0..3) | |
107 | // r6 = 0xFEFEFEFF | |
108 | // r7 = 0x80808080 | |
109 | // r9 = dest ptr (unaligned) | |
110 | // ctr = whole words left in buffer | |
111 | ||
112 | .align 5 // align inner loop, which is 8 words long | |
113 | Lwordloop: | |
114 | stw r8,0(r9) // pack word into destination | |
115 | addi r9,r9,4 | |
116 | LwordloopEnter: | |
117 | lwz r8,0(r4) // r8 <- next 4 source bytes | |
118 | addi r4,r4,4 | |
119 | add r10,r8,r6 // r10 <- word + 0xFEFEFEFF | |
120 | andc r12,r7,r8 // r12 <- ~word & 0x80808080 | |
121 | and. r11,r10,r12 // r11 <- nonzero iff word has a 0-byte | |
122 | bdnzt eq,Lwordloop // loop if ctr!=0 and cr0_eq | |
123 | ||
124 | beq Lleftovers // 0-byte not found in aligned words | |
125 | ||
126 | // Found a 0-byte. Store last word up to and including the 0, a byte at a time. | |
127 | // r8 = last word, known to have a 0-byte | |
128 | // r9 = dest ptr | |
129 | ||
130 | Lstorelastbytes: | |
131 | srwi. r0,r8,24 // right justify next byte and test for 0 | |
132 | slwi r8,r8,8 // shift next byte into position | |
133 | stb r0,0(r9) // pack into dest | |
134 | addi r9,r9,1 | |
135 | bne Lstorelastbytes // loop until 0 stored | |
136 | ||
137 | L0found: | |
138 | sub r3,r9,r3 // get #bytes stored, including 0 | |
139 | subi r3,r3,1 // don't count the 0 | |
140 | blr // return strlen(src) | |
141 | ||
142 | // 0-byte not found in aligned source words. There are up to 3 leftover source | |
143 | // bytes, hopefully the 0-byte is among them. | |
144 | // r4 = source ptr (word aligned) | |
145 | // r5 = leftover bytes in buffer (0..3) | |
146 | // r6 = 0xFEFEFEFF | |
147 | // r7 = 0x80808080 | |
148 | // r8 = last full word of source | |
149 | // r9 = dest ptr (unaligned) | |
150 | ||
151 | Lleftovers: | |
152 | stw r8,0(r9) // store last word | |
153 | addi r9,r9,4 | |
154 | addi r0,r5,1 // make sure r5 terminate byte loop (not r0) | |
155 | b Lbyteloop | |
156 | ||
157 | // Buffer full but 0-byte not found. Stuff a 0 into last byte of buffer. | |
158 | // r3 = start of buffer | |
159 | // r4 = ptr to next byte in source | |
160 | // r9 = ptr to first byte past end of buffer | |
161 | ||
162 | L0notfound: | |
163 | sub. r3,r9,r3 // get #bytes stored, ie original buffer length | |
164 | beq Lfind0 // skip if buffer 0-length | |
165 | li r0,0 // get a 0 | |
166 | stb r0,-1(r9) // always store 0-byte unless buffer was 0-length | |
167 | ||
168 | // Keep searching for 0-byte ending source, so we can return strlen(source). | |
169 | // Not optimized, since this is an error condition. | |
170 | // r3 = number of bytes already copied | |
171 | // r4 = ptr to next byte in source | |
172 | ||
173 | Lfind0: | |
174 | lbz r0,0(r4) // get next byte | |
175 | addi r4,r4,1 | |
176 | addi r3,r3,1 // increment strlen | |
177 | cmpwi r0,0 | |
178 | bne Lfind0 // loop if not 0 | |
179 | ||
180 | subi r3,r3,1 // don't count the 0-byte | |
181 | blr // return strlen(source) |