]> git.saurik.com Git - apple/libc.git/blob - ppc/string/strcat.s
Libc-391.tar.gz
[apple/libc.git] / ppc / string / strcat.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 #define __APPLE_API_PRIVATE
28 #include <machine/cpu_capabilities.h>
29 #undef __APPLE_API_PRIVATE
30
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__.
34 */
35 #include <architecture/ppc/mode_independent_asm.h>
36
37
38 // ***************
39 // * S T R C A T *
40 // ***************
41 //
42 // char* strcat(const char *dst, const char *src);
43 //
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 load that crosses a page boundary,
48 // and never store a byte we don't have to.
49 //
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
57 // mapped to 0x80.
58 //
59 // In 64-bit mode, this algorithm is doubleword parallel.
60
61 .text
62 .globl EXT(strcat)
63
64 .align 5
65 LEXT(strcat) // char* strcat(const char *s, const char *append);
66 clrrgi r9,r3,LOG2_GPR_BYTES// align pointer by zeroing right LOG2_GPR_BYTES bits
67 li r10,-1 // get 0xFFs
68 lg r8,0(r9) // get word or doubleword with 1st operand byte
69 rlwinm r11,r3,3,(GPR_BYTES-1)*8 // get starting bit position of operand
70 #if defined(__ppc__)
71 lis r6,hi16(0xFEFEFEFF) // start to generate 32-bit magic constants
72 lis r7,hi16(0x80808080)
73 srw r10,r10,r11 // create a mask of 0xFF bytes for operand in r8
74 ori r6,r6,lo16(0xFEFEFEFF)
75 ori r7,r7,lo16(0x80808080)
76 #else
77 ld r6,_COMM_PAGE_MAGIC_FE(0) // get 0xFEFEFEFE FEFEFEFF from commpage
78 ld r7,_COMM_PAGE_MAGIC_80(0) // get 0x80808080 80808080 from commpage
79 srd r10,r10,r11 // create a mask of 0xFF bytes for operand in r8
80 #endif
81 orc r8,r8,r10 // make sure bytes preceeding operand are nonzero
82 b Lword0loopEnter
83
84 // Loop over words or doublewords looking for 0-byte marking end of dest.
85 // r4 = source ptr (unaligned)
86 // r6 = 0xFEFEFEFF
87 // r7 = 0x80808080
88 // r9 = dest ptr (aligned)
89
90 .align 5 // align inner loops for speed
91 Lword0loop:
92 lgu r8,GPR_BYTES(r9) // r8 <- next dest word or doubleword
93 Lword0loopEnter: // initial entry
94 add r10,r8,r6 // r10 <- word + 0xFEFEFEFF
95 andc r12,r7,r8 // r12 <- ~word & 0x80808080
96 and. r11,r10,r12 // r11 <- nonzero iff word has a 0-byte
97 beq Lword0loop // loop until 0 found
98
99 // Now we know one of the bytes in r8 is zero, we just have to figure out which one.
100 // We have mapped 0 bytes to 0x80, and nonzero bytes to 0x00, with one exception:
101 // 0x01 bytes preceeding the first zero are also mapped to 0x80. So we have to mask
102 // out the 0x80s caused by 0x01s before searching for the 0x80 byte. Once the 0 is
103 // found, we can start appending source. We align the source, which allows us to
104 // avoid worrying about spurious page faults.
105 // r4 = source ptr (unaligned)
106 // r6 = 0xFEFEFEFF
107 // r7 = 0x80808080
108 // r8 = word or doubleword with a 0-byte
109 // r9 = ptr to the word or doubleword in r8 (aligned)
110 // r11 = mapped word or doubleword
111
112 slgi r10,r8,7 // move 0x01 bits (false hits) into 0x80 position
113 andi. r0,r4,GPR_BYTES-1 // is source aligned?
114 andc r11,r11,r10 // mask out false hits
115 cntlzg r10,r11 // find 0 byte (r0 = 0, 8, 16, or 24)
116 subfic r0,r0,GPR_BYTES // get #bytes to align r4
117 srwi r10,r10,3 // now r0 = 0, 1, 2, or 3
118 add r9,r9,r10 // now r9 points to the 0-byte in dest
119 beq LwordloopEnter // skip if source is already aligned
120
121 mtctr r0 // set up loop
122
123 // Loop over bytes.
124 // r4 = source ptr (unaligned)
125 // r6 = 0xFEFEFEFF
126 // r7 = 0x80808080
127 // r9 = dest ptr (unaligned)
128 // ctr = byte count
129
130 Lbyteloop:
131 lbz r8,0(r4) // r8 <- next source byte
132 addi r4,r4,1
133 cmpwi r8,0 // 0 ?
134 stb r8,0(r9) // pack into dest
135 addi r9,r9,1
136 bdnzf eq,Lbyteloop // loop until (ctr==0) | (r8==0)
137
138 bne LwordloopEnter // 0-byte not found, so enter word loop
139 blr // 0-byte found, done
140
141 // Word loop: move a word or doubleword at a time until 0-byte found.
142 // r4 = source ptr (aligned)
143 // r6 = 0xFEFEFEFF
144 // r7 = 0x80808080
145 // r9 = dest ptr (unaligned)
146
147 .align 5 // align inner loop, which is 8 words ling
148 Lwordloop:
149 stg r8,0(r9) // pack word or doubleword into destination
150 addi r9,r9,GPR_BYTES
151 LwordloopEnter:
152 lg r8,0(r4) // r8 <- next 4 or 8 source bytes
153 addi r4,r4,GPR_BYTES
154 add r10,r8,r6 // r10 <- word + 0xFEFEFEFF
155 andc r12,r7,r8 // r12 <- ~word & 0x80808080
156 and. r0,r10,r12 // r0 <- nonzero iff word has a 0-byte
157 beq Lwordloop // loop if no 0-byte
158
159 // Found a 0-byte. Store last word up to and including the 0, a byte at a time.
160 // r8 = last word or doubleword, known to have a 0-byte
161 // r9 = dest ptr
162
163 Lstorelastbytes:
164 srgi. r0,r8,GPR_BYTES*8-8 // shift leftmost byte into bottom so we can "stb"
165 slgi r8,r8,8 // move on to next
166 stb r0,0(r9) // pack into dest
167 addi r9,r9,1
168 bne Lstorelastbytes // loop until 0 stored
169
170 blr
171