]> git.saurik.com Git - apple/libc.git/blob - ppc/string/strncpy.s
fda3976c79c7556e4aed3c8df97f7ee6107bf796
[apple/libc.git] / ppc / string / strncpy.s
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 #define __APPLE_API_PRIVATE
30 #include <machine/cpu_capabilities.h>
31 #undef __APPLE_API_PRIVATE
32
33 /* We use mode-independent "g" opcodes such as "srgi". These expand
34 * into word operations when targeting __ppc__, and into doubleword
35 * operations when targeting __ppc64__.
36 */
37 #include <architecture/ppc/mode_independent_asm.h>
38
39
40 // *****************
41 // * S T R N C P Y *
42 // *****************
43 //
44 // char* strncpy(const char *dst, const char *src, size_t len);
45 //
46 // We optimize the move by doing it word parallel. This introduces
47 // a complication: if we blindly did word load/stores until finding
48 // a 0, we might get a spurious page fault by touching bytes past it.
49 // To avoid this, we never do a "lwz" that crosses a page boundary,
50 // or store unnecessary bytes.
51 //
52 // The test for 0s relies on the following inobvious but very efficient
53 // word-parallel test:
54 // x = dataWord + 0xFEFEFEFF
55 // y = ~dataWord & 0x80808080
56 // if (x & y) == 0 then no zero found
57 // The test maps any non-zero byte to zero, and any zero byte to 0x80,
58 // with one exception: 0x01 bytes preceeding the first zero are also
59 // mapped to 0x80.
60 //
61 // This algorithm is doubleword parallel in 64-bit mode.
62
63 .text
64 .globl EXT(strncpy)
65
66 .align 5
67 LEXT(strncpy) // char* strncpy(const char *dst, const char *src, size_t len));
68 andi. r0,r4,GPR_BYTES-1 // is source aligned?
69 #if defined(__ppc__)
70 lis r6,hi16(0xFEFEFEFF) // start to generate 32-bit magic constants
71 lis r7,hi16(0x80808080)
72 ori r6,r6,lo16(0xFEFEFEFF)
73 ori r7,r7,lo16(0x80808080)
74 #else
75 ld r6,_COMM_PAGE_MAGIC_FE(0) // get 0xFEFEFEFE FEFEFEFF from commpage
76 ld r7,_COMM_PAGE_MAGIC_80(0) // get 0x80808080 80808080 from commpage
77 #endif
78 mr r9,r3 // use r9 for dest ptr (must return r3 intact)
79 add r2,r3,r5 // remember where end of buffer is
80 beq Laligned // source is aligned
81 subfic r0,r0,GPR_BYTES // r0 <- #bytes to align source
82
83 // Copy min(r0,r5) bytes, until 0-byte.
84 // r0 = #bytes we propose to copy (NOTE: must be >0)
85 // r2 = ptr to 1st byte not in buffer
86 // r4 = source ptr (unaligned)
87 // r5 = length remaining in buffer (may be 0)
88 // r6 = 0xFEFEFEFF
89 // r7 = 0x80808080
90 // r9 = dest ptr (unaligned)
91
92 Lbyteloop:
93 cmpgi r5,0 // buffer empty? (note: length is unsigned)
94 beqlr-- // buffer full but 0 not found
95 lbz r8,0(r4) // r8 <- next source byte
96 subic. r0,r0,1 // decrement count of bytes to move
97 addi r4,r4,1
98 subi r5,r5,1 // decrement buffer length remaining
99 cmpwi cr1,r8,0 // 0-byte?
100 stb r8,0(r9) // pack into dest
101 addi r9,r9,1
102 beq cr1,L0found // byte was 0
103 bne Lbyteloop // r0!=0, source not yet aligned
104
105 // Source is aligned. Loop over words or doublewords until end of buffer. Note that
106 // we have aligned the source, rather than the dest, in order to avoid spurious
107 // page faults.
108 // r2 = ptr to 1st byte not in buffer
109 // r4 = source ptr (aligned)
110 // r5 = length remaining in buffer
111 // r6 = 0xFEFEFEFF
112 // r7 = 0x80808080
113 // r9 = dest ptr (unaligned)
114
115 Laligned:
116 srgi. r8,r5,LOG2_GPR_BYTES// get #words or doublewords in buffer
117 addi r0,r5,1 // if none, compare rest of buffer
118 beq-- Lbyteloop // r8==0, no words
119 mtctr r8 // set up word loop count
120 rlwinm r5,r5,0,GPR_BYTES-1 // mask buffer length down to leftover bytes
121 b Lwordloop
122
123 // Move a word or a doubleword at a time, until one of two conditions:
124 // - a zero byte is found
125 // - end of buffer
126 // At this point, registers are as follows:
127 // r2 = ptr to 1st byte not in buffer
128 // r4 = source ptr (aligned)
129 // r5 = leftover bytes in buffer (0..GPR_BYTES-1)
130 // r6 = 0xFEFEFEFF
131 // r7 = 0x80808080
132 // r9 = dest ptr (unaligned)
133 // ctr = whole words or doublewords left in buffer
134
135 .align 5 // align inner loop, which is 8 words long
136 Lwordloop:
137 lg r8,0(r4) // r8 <- next 4 or 8 source bytes
138 addi r9,r9,GPR_BYTES // bump dest addr while we wait for data
139 addi r4,r4,GPR_BYTES
140 add r10,r8,r6 // r10 <- word + 0xFEFEFEFF
141 andc r12,r7,r8 // r12 <- ~word & 0x80808080
142 stg r8,-GPR_BYTES(r9) // pack word or doubleword into destination
143 and. r11,r10,r12 // r11 <- nonzero iff word has a 0-byte
144 bdnzt eq,Lwordloop // loop if ctr!=0 and cr0_eq
145
146 addi r0,r5,1 // if no 0-byte found...
147 beq-- Lbyteloop // ...fill rest of buffer a byte at a time
148
149 // Found a 0-byte, point to following byte with r9.
150
151 slgi r0,r8,7 // move 0x01 false hit bits to 0x80 position
152 andc r11,r11,r0 // mask out false hits
153 cntlzg r0,r11 // find the 0-byte (r0 = 0,8,16, or 24)
154 srwi r0,r0,3 // now r0 = 0, 1, 2, or 3 (0..7 if 64-bit)
155 subfic r0,r0,GPR_BYTES-1 // now r0 = 3, 2, 1, or 0
156 sub r9,r9,r0 // now r9 points one past the 0-byte
157
158 // Zero rest of buffer, if any. We use the commpage bzero() routine.
159 // r2 = ptr to 1st byte not in buffer
160 // r9 = ptr to 1st byte to zero
161 //
162 // NB: commpage bzero() preserves r10-r12 by contract.
163
164 L0found:
165 mflr r12 // save return
166 mr r11,r3 // save original dest ptr
167 sub r4,r2,r9 // #bytes to zero (ie, rest of buffer)
168 mr r3,r9 // point to 1st byte to zero
169 bla _COMM_PAGE_BZERO
170 mtlr r12 // restore our return
171 mr r3,r11 // restore ptr to original dest
172 blr