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