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