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