]> git.saurik.com Git - apple/libc.git/blame - ppc/string/strcat.s
Libc-262.2.12.tar.gz
[apple/libc.git] / ppc / string / strcat.s
CommitLineData
734aad71
A
1/*
2 * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25#define ASSEMBLER
26#include <mach/ppc/asm.h>
27#undef ASSEMBLER
28
29// ***************
30// * S T R C A T *
31// ***************
32//
33// char* strcat(const char *dst, const char *src);
34//
35// We optimize the move by doing it word parallel. This introduces
36// a complication: if we blindly did word load/stores until finding
37// a 0, we might get a spurious page fault by touching bytes past it.
38// To avoid this, we never do a "lwz" that crosses a page boundary,
39// and never store a byte we don't have to.
40//
41// The test for 0s relies on the following inobvious but very efficient
42// word-parallel test:
43// x = dataWord + 0xFEFEFEFF
44// y = ~dataWord & 0x80808080
45// if (x & y) == 0 then no zero found
46// The test maps any non-zero byte to zero, and any zero byte to 0x80,
47// with one exception: 0x01 bytes preceeding the first zero are also
48// mapped to 0x80.
49
50 .text
51 .globl EXT(strcat)
52
53 .align 5
54LEXT(strcat) // char* strcat(const char *s, const char *append);
55 andi. r0,r3,3 // is dst aligned?
56 dcbtst 0,r3 // touch in dst
57 lis r6,hi16(0xFEFEFEFF) // start to load magic constants
58 lis r7,hi16(0x80808080)
59 dcbt 0,r4 // touch in source
60 ori r6,r6,lo16(0xFEFEFEFF)
61 ori r7,r7,lo16(0x80808080)
62 mr r9,r3 // use r9 for dest ptr (must return r3 intact)
63 beq Lword0loop // dest is aligned
64 subfic r0,r0,4 // r0 <- #bytes to word align dest
65 mtctr r0
66
67// Loop over bytes looking for 0-byte marking end of dest.
68// r4 = source ptr (unalaigned)
69// r6 = 0xFEFEFEFF
70// r7 = 0x80808080
71// r9 = dest ptr (unaligned)
72// ctr = byte count
73
74Lbyte0loop:
75 lbz r8,0(r9) // r8 <- next dest byte
76 addi r9,r9,1
77 cmpwi r8,0 // test for 0
78 bdnzf eq,Lbyte0loop // loop until (ctr==0) | (r8==0)
79
80 bne Lword0loop // enter word loop if we haven't found the 0-byte
81 subi r9,r9,1 // point to 0-byte
82 b L0found // start to append the source
83
84// Loop over words looking for 0-byte marking end of dest.
85// r4 = source ptr (unalaigned)
86// r6 = 0xFEFEFEFF
87// r7 = 0x80808080
88// r9 = dest ptr (word aligned)
89
90 .align 5 // align inner loops for speed
91Lword0loop:
92 lwz r8,0(r9) // r8 <- next dest word
93 addi r9,r9,4
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 slwi r0,r8,7 // move 0x01 bits (false hits) into 0x80 position
100 subi r9,r9,4 // back r9 up to beginning of word
101 andc r11,r11,r0 // mask out false hits
102 cntlzw r0,r11 // find 0 byte (r0 = 0, 8, 16, or 24)
103 srwi r0,r0,3 // now r0 = 0, 1, 2, or 3
104 add r9,r9,r0 // now r9 points to the 0-byte in dest
105
106// End of dest found, so we can start appending source.
107// We align the _source_, which allows us to avoid all worries about
108// spurious page faults. Doing so is faster than aligning the dest.
109// r4 = source ptr (unaligned)
110// r6 = 0xFEFEFEFF
111// r7 = 0x80808080
112// r9 = ptr to 0-byte (unaligned)
113
114L0found:
115 andi. r0,r4,3 // is source aligned?
116 beq LwordloopEnter // skip if so
117 subfic r0,r0,4 // not aligned, get #bytes to align r4
118 mtctr r0 // set up loop
119
120// Loop over bytes.
121// r4 = source ptr (unaligned)
122// r6 = 0xFEFEFEFF
123// r7 = 0x80808080
124// r9 = dest ptr (unaligned)
125// ctr = byte count
126
127Lbyteloop:
128 lbz r8,0(r4) // r8 <- next source byte
129 addi r4,r4,1
130 cmpwi r8,0 // 0 ?
131 stb r8,0(r9) // pack into dest
132 addi r9,r9,1
133 bdnzf eq,Lbyteloop // loop until (ctr==0) | (r8==0)
134
135 bne LwordloopEnter // 0-byte not found, so enter word loop
136 blr // 0-byte found, done
137
138// Word loop: move a word at a time until 0-byte found.
139// r4 = source ptr (word aligned)
140// r6 = 0xFEFEFEFF
141// r7 = 0x80808080
142// r9 = dest ptr (unaligned)
143
144 .align 5 // align inner loop, which is 8 words ling
145Lwordloop:
146 stw r8,0(r9) // pack word into destination
147 addi r9,r9,4
148LwordloopEnter:
149 lwz r8,0(r4) // r8 <- next 4 source bytes
150 addi r4,r4,4
151 add r10,r8,r6 // r10 <- word + 0xFEFEFEFF
152 andc r12,r7,r8 // r12 <- ~word & 0x80808080
153 and. r0,r10,r12 // r0 <- nonzero iff word has a 0-byte
154 beq Lwordloop // loop if ctr!=0 and cr0_eq
155
156// Found a 0-byte. Store last word up to and including the 0, a byte at a time.
157// r8 = last word, known to have a 0-byte
158// r9 = dest ptr
159
160Lstorelastbytes:
161 srwi. r0,r8,24 // right justify next byte and test for 0
162 slwi r8,r8,8 // shift next byte into position
163 stb r0,0(r9) // pack into dest
164 addi r9,r9,1
165 bne Lstorelastbytes // loop until 0 stored
166
167 blr
168