]> git.saurik.com Git - apple/libc.git/blob - ppc/string/strcpy.s
Libc-320.1.3.tar.gz
[apple/libc.git] / ppc / string / strcpy.s
1 /*
2 * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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 C P Y *
29 // ***************
30 //
31 // char* strcpy(const char *dst, const char *src);
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 // and never store a byte we don't have to.
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 // We align the _source_, which allows us to avoid all worries about
49 // spurious page faults. Doing so is faster than aligning the dest.
50
51 .text
52 .globl EXT(strcpy)
53
54 .align 5
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
66 mtctr r0
67
68 // Loop over bytes.
69 // r4 = source ptr (unaligned)
70 // r6 = 0xFEFEFEFF
71 // r7 = 0x80808080
72 // r9 = dest ptr (unaligned)
73 // ctr = byte count
74
75 Lbyteloop:
76 lbz r8,0(r4) // r8 <- next source byte
77 addi r4,r4,1
78 cmpwi r8,0 // 0 ?
79 stb r8,0(r9) // pack into dest
80 addi r9,r9,1
81 bdnzf eq,Lbyteloop // loop until (ctr==0) | (r8==0)
82
83 bne LwordloopEnter // 0-byte not found, so enter word loop
84 blr // 0-byte found, done
85
86 // Word loop: move a word at a time until 0-byte found.
87 // r4 = source ptr (word aligned)
88 // r6 = 0xFEFEFEFF
89 // r7 = 0x80808080
90 // r9 = dest ptr (unaligned)
91
92 .align 5 // align inner loop, which is 8 words ling
93 Lwordloop:
94 stw r8,0(r9) // pack word into destination
95 addi r9,r9,4
96 LwordloopEnter:
97 lwz r8,0(r4) // r8 <- next 4 source bytes
98 addi r4,r4,4
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
103
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
106 // r9 = dest ptr
107
108 Lstorelastbytes:
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
112 addi r9,r9,1
113 bne Lstorelastbytes // loop until 0 stored
114
115 blr
116