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>
31 // char* strcpy(const char *dst, const char *src);
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 // and never store a byte we don't have to.
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
48 // We align the _source_, which allows us to avoid all worries about
49 // spurious page faults. Doing so is faster than aligning the dest.
55 LEXT(strcpy) // char* strcpy(const char *dst, const char *src);
56 andi. r0,r4,3 // is source aligned?
57 dcbt 0,r4 // touch in source
58 lis r6,hi16(0xFEFEFEFF) // start to load magic constants
59 lis r7,hi16(0x80808080)
60 dcbtst 0,r3 // touch in dst
61 ori r6,r6,lo16(0xFEFEFEFF)
62 ori r7,r7,lo16(0x80808080)
63 mr r9,r3 // use r9 for dest ptr (must return r3 intact)
64 beq LwordloopEnter // source is aligned
65 subfic r0,r0,4 // r0 <- #bytes to word align source
69 // r4 = source ptr (unaligned)
72 // r9 = dest ptr (unaligned)
76 lbz r8,0(r4) // r8 <- next source byte
79 stb r8,0(r9) // pack into dest
81 bdnzf eq,Lbyteloop // loop until (ctr==0) | (r8==0)
83 bne LwordloopEnter // 0-byte not found, so enter word loop
84 blr // 0-byte found, done
86 // Word loop: move a word at a time until 0-byte found.
87 // r4 = source ptr (word aligned)
90 // r9 = dest ptr (unaligned)
92 .align 5 // align inner loop, which is 8 words ling
94 stw r8,0(r9) // pack word into destination
97 lwz r8,0(r4) // r8 <- next 4 source bytes
99 add r10,r8,r6 // r10 <- word + 0xFEFEFEFF
100 andc r12,r7,r8 // r12 <- ~word & 0x80808080
101 and. r0,r10,r12 // r0 <- nonzero iff word has a 0-byte
102 beq Lwordloop // loop if ctr!=0 and cr0_eq
104 // Found a 0-byte. Store last word up to and including the 0, a byte at a time.
105 // r8 = last word, known to have a 0-byte
109 srwi. r0,r8,24 // right justify next byte and test for 0
110 slwi r8,r8,8 // shift next byte into position
111 stb r0,0(r9) // pack into dest
113 bne Lstorelastbytes // loop until 0 stored