2 * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <mach/ppc/asm.h>
27 #define __APPLE_API_PRIVATE
28 #include <machine/cpu_capabilities.h>
29 #undef __APPLE_API_PRIVATE
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__.
35 #include <architecture/ppc/mode_independent_asm.h>
42 // size_t strlcpy(char *dst, const char *src, size_t size);
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.
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
59 // This algorithm is doubleword parallel in 64-bit mode.
65 LEXT(strlcpy) // size_t strlcpy(char *dst, const char *src, size_t size);
66 andi. r0,r4,GPR_BYTES-1 // is source aligned?
68 lis r6,hi16(0xFEFEFEFF) // start to generate 32-bit magic constants
69 lis r7,hi16(0x80808080)
70 ori r6,r6,lo16(0xFEFEFEFF)
71 ori r7,r7,lo16(0x80808080)
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
76 mr r9,r3 // use r9 for dest ptr (must return r3 intact)
77 beq Laligned // source is aligned
78 subfic r0,r0,GPR_BYTES // r0 <- #bytes to align source
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)
86 // r9 = dest ptr (unaligned)
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
94 subi r5,r5,1 // decrement buffer length remaining
95 stb r8,0(r9) // pack into dest
96 cmpwi cr1,r8,0 // 0-byte?
98 beq cr1,L0found // byte was 0
99 bne Lbyteloop // r0!=0, source not yet aligned
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)
104 // r5 = length remaining in buffer
107 // r9 = dest ptr (unaligned)
110 srgi. r8,r5,LOG2_GPR_BYTES// get #words or doublewords in buffer
111 addi r0,r5,1 // if no words, compare rest of buffer
112 beq-- Lbyteloop // r8==0, no words
113 mtctr r8 // set up word loop count
114 rlwinm r5,r5,0,GPR_BYTES-1 // mask buffer length down to leftover bytes
117 // Move a word or doubleword at a time, until one of two conditions:
118 // - a zero byte is found
120 // At this point, registers are as follows:
121 // r4 = source ptr (aligned)
122 // r5 = leftover bytes in buffer (0..GPR_BYTES-1)
125 // r9 = dest ptr (unaligned)
126 // ctr = whole words or doublewords left in buffer
128 .align 5 // align inner loop, which is 8 words long
130 stg r8,0(r9) // pack word or doubleword into destination
133 lg r8,0(r4) // r8 <- next 4 or 8 source bytes
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
140 beq Lleftovers // 0-byte not found in aligned words
142 // Found a 0-byte. Store last word up to and including the 0, a byte at a time.
143 // r8 = last word or doubleword, known to have a 0-byte
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
149 stb r0,0(r9) // pack into dest
151 bne Lstorelastbytes // loop until 0 stored
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)
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)
164 // r8 = last full word or doubleword of source
165 // r9 = dest ptr (unaligned)
168 stg r8,0(r9) // store last word or doubleword
170 addi r0,r5,1 // make sure r5 terminates byte loop (not r0)
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
179 sub. r3,r9,r3 // get #bytes stored, ie original buffer length
180 beq Lfind0 // skip if buffer 0-length
182 stb r0,-1(r9) // always store 0-byte unless buffer was 0-length
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
190 lbz r0,0(r4) // get next byte
192 addi r3,r3,1 // increment strlen
194 bne Lfind0 // loop if not 0
196 subi r3,r3,1 // don't count the 0-byte
197 blr // return strlen(source)