]>
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 | ||
27 | // *************** | |
28 | // * S T R C A T * | |
29 | // *************** | |
30 | // | |
31 | // char* strcat(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 | .text | |
49 | .globl EXT(strcat) | |
50 | ||
51 | .align 5 | |
52 | LEXT(strcat) // char* strcat(const char *s, const char *append); | |
53 | andi. r0,r3,3 // is dst aligned? | |
54 | dcbtst 0,r3 // touch in dst | |
55 | lis r6,hi16(0xFEFEFEFF) // start to load magic constants | |
56 | lis r7,hi16(0x80808080) | |
57 | dcbt 0,r4 // touch in source | |
58 | ori r6,r6,lo16(0xFEFEFEFF) | |
59 | ori r7,r7,lo16(0x80808080) | |
60 | mr r9,r3 // use r9 for dest ptr (must return r3 intact) | |
61 | beq Lword0loop // dest is aligned | |
62 | subfic r0,r0,4 // r0 <- #bytes to word align dest | |
63 | mtctr r0 | |
64 | ||
65 | // Loop over bytes looking for 0-byte marking end of dest. | |
66 | // r4 = source ptr (unalaigned) | |
67 | // r6 = 0xFEFEFEFF | |
68 | // r7 = 0x80808080 | |
69 | // r9 = dest ptr (unaligned) | |
70 | // ctr = byte count | |
71 | ||
72 | Lbyte0loop: | |
73 | lbz r8,0(r9) // r8 <- next dest byte | |
74 | addi r9,r9,1 | |
75 | cmpwi r8,0 // test for 0 | |
76 | bdnzf eq,Lbyte0loop // loop until (ctr==0) | (r8==0) | |
77 | ||
78 | bne Lword0loop // enter word loop if we haven't found the 0-byte | |
79 | subi r9,r9,1 // point to 0-byte | |
80 | b L0found // start to append the source | |
81 | ||
82 | // Loop over words looking for 0-byte marking end of dest. | |
83 | // r4 = source ptr (unalaigned) | |
84 | // r6 = 0xFEFEFEFF | |
85 | // r7 = 0x80808080 | |
86 | // r9 = dest ptr (word aligned) | |
87 | ||
88 | .align 5 // align inner loops for speed | |
89 | Lword0loop: | |
90 | lwz r8,0(r9) // r8 <- next dest word | |
91 | addi r9,r9,4 | |
92 | add r10,r8,r6 // r10 <- word + 0xFEFEFEFF | |
93 | andc r12,r7,r8 // r12 <- ~word & 0x80808080 | |
94 | and. r11,r10,r12 // r11 <- nonzero iff word has a 0-byte | |
95 | beq Lword0loop // loop until 0 found | |
96 | ||
97 | slwi r0,r8,7 // move 0x01 bits (false hits) into 0x80 position | |
98 | subi r9,r9,4 // back r9 up to beginning of word | |
99 | andc r11,r11,r0 // mask out false hits | |
100 | cntlzw r0,r11 // find 0 byte (r0 = 0, 8, 16, or 24) | |
101 | srwi r0,r0,3 // now r0 = 0, 1, 2, or 3 | |
102 | add r9,r9,r0 // now r9 points to the 0-byte in dest | |
103 | ||
104 | // End of dest found, so we can start appending source. | |
105 | // We align the _source_, which allows us to avoid all worries about | |
106 | // spurious page faults. Doing so is faster than aligning the dest. | |
107 | // r4 = source ptr (unaligned) | |
108 | // r6 = 0xFEFEFEFF | |
109 | // r7 = 0x80808080 | |
110 | // r9 = ptr to 0-byte (unaligned) | |
111 | ||
112 | L0found: | |
113 | andi. r0,r4,3 // is source aligned? | |
114 | beq LwordloopEnter // skip if so | |
115 | subfic r0,r0,4 // not aligned, get #bytes to align r4 | |
116 | mtctr r0 // set up loop | |
117 | ||
118 | // Loop over bytes. | |
119 | // r4 = source ptr (unaligned) | |
120 | // r6 = 0xFEFEFEFF | |
121 | // r7 = 0x80808080 | |
122 | // r9 = dest ptr (unaligned) | |
123 | // ctr = byte count | |
124 | ||
125 | Lbyteloop: | |
126 | lbz r8,0(r4) // r8 <- next source byte | |
127 | addi r4,r4,1 | |
128 | cmpwi r8,0 // 0 ? | |
129 | stb r8,0(r9) // pack into dest | |
130 | addi r9,r9,1 | |
131 | bdnzf eq,Lbyteloop // loop until (ctr==0) | (r8==0) | |
132 | ||
133 | bne LwordloopEnter // 0-byte not found, so enter word loop | |
134 | blr // 0-byte found, done | |
135 | ||
136 | // Word loop: move a word at a time until 0-byte found. | |
137 | // r4 = source ptr (word aligned) | |
138 | // r6 = 0xFEFEFEFF | |
139 | // r7 = 0x80808080 | |
140 | // r9 = dest ptr (unaligned) | |
141 | ||
142 | .align 5 // align inner loop, which is 8 words ling | |
143 | Lwordloop: | |
144 | stw r8,0(r9) // pack word into destination | |
145 | addi r9,r9,4 | |
146 | LwordloopEnter: | |
147 | lwz r8,0(r4) // r8 <- next 4 source bytes | |
148 | addi r4,r4,4 | |
149 | add r10,r8,r6 // r10 <- word + 0xFEFEFEFF | |
150 | andc r12,r7,r8 // r12 <- ~word & 0x80808080 | |
151 | and. r0,r10,r12 // r0 <- nonzero iff word has a 0-byte | |
152 | beq Lwordloop // loop if ctr!=0 and cr0_eq | |
153 | ||
154 | // Found a 0-byte. Store last word up to and including the 0, a byte at a time. | |
155 | // r8 = last word, known to have a 0-byte | |
156 | // r9 = dest ptr | |
157 | ||
158 | Lstorelastbytes: | |
159 | srwi. r0,r8,24 // right justify next byte and test for 0 | |
160 | slwi r8,r8,8 // shift next byte into position | |
161 | stb r0,0(r9) // pack into dest | |
162 | addi r9,r9,1 | |
163 | bne Lstorelastbytes // loop until 0 stored | |
164 | ||
165 | blr | |
166 |