]> git.saurik.com Git - apple/libc.git/blob - ppc/string/strncmp.s
Libc-391.4.1.tar.gz
[apple/libc.git] / ppc / string / strncmp.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 // we need the defs for cr7_eq etc
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 M P *
40 // *****************
41 //
42 // int strncmp(const char *s1, const char *s2, size_t len);
43 //
44 // We optimize the compare by doing it word parallel. This introduces
45 // a complication: if we blindly did word loads from both sides until
46 // finding a difference (or 0), we might get a spurious page fault by
47 // reading bytes past the difference. To avoid this, we never do a "lwz"
48 // that crosses a page boundary.
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 routine is doubleword parallel in 64-bit mode.
60
61 .text
62 .globl EXT(strncmp)
63
64 .align 5
65 LEXT(strncmp) // int strncmp(const char *s1,const char *s2,size_t len);
66 cmplgi cr1,r5,GPR_BYTES*2 // buffer too short to bother? (WARNING: cr1 is used later)
67 andi. r0,r3,GPR_BYTES-1 // is LHS aligned?
68 subi r3,r3,GPR_BYTES // we use "lgu" in the inner loops
69 subi r4,r4,GPR_BYTES
70 blt cr1,Lshort // short buffer, just compare a byte at a time
71 #if defined(__ppc__)
72 lis r2,hi16(0xFEFEFEFF) // start to generate 32-bit magic constants
73 lis r6,hi16(0x80808080)
74 ori r2,r2,lo16(0xFEFEFEFF)
75 ori r6,r6,lo16(0x80808080)
76 #else
77 ld r2,_COMM_PAGE_MAGIC_FE(0) // get 0xFEFEFEFE FEFEFEFF from commpage
78 ld r6,_COMM_PAGE_MAGIC_80(0) // get 0x80808080 80808080 from commpage
79 #endif
80 beq Laligned // LHS is aligned
81 subfic r0,r0,GPR_BYTES // r0 <- #bytes to align LHS
82 mtctr r0
83 sub r5,r5,r0 // adjust length
84 b Lbyteloop
85
86 // Handle short operands or end-of-buffer.
87 // r3 = LHS ptr - GPR_BYTES (unaligned)
88 // r4 = RHS ptr - GPR_BYTES (unaligned)
89 // r5 = length remaining in buffer (0..GPR_BYTES*2-1)
90 // cr1 = blt set
91
92 Lshort:
93 cmpgi r5,0 // buffer null?
94 mtctr r5 // assume not null, set up for loop
95 bne Lbyteloop // buffer not null
96 li r3,0 // if buffer null, say "equal"
97 blr
98
99 // We're at a RHS page boundary. Compare GPR_BYTES bytes in order to cross the page
100 // but still keep the LHS ptr word-aligned.
101 // r2 = 0xFEFEFEFF
102 // r3 = LHS ptr - GPR_BYTES (aligned)
103 // r4 = RHS ptr - GPR_BYTES (unaligned)
104 // r5 = length remaining in buffer (may be 0)
105 // r6 = 0x80808080
106
107 Lcrosspage:
108 cmplgi cr1,r5,GPR_BYTES*2 // not enough left in buffer for word compares?
109 li r0,GPR_BYTES // get #bytes to cross RHS page
110 blt cr1,Lshort // buffer is about to end
111 mtctr r0 // set up to compare GPR_BYTES bytes
112 sub r5,r5,r0 // adjust length
113 b Lbyteloop
114
115 // Compare bytes, until 0-byte or difference found.
116 // r2 = 0xFEFEFEFF (if cr1 bge)
117 // r3 = LHS ptr - GPR_BYTES (unaligned)
118 // r4 = RHS ptr - GPR_BYTES (unaligned)
119 // r5 = length remaining in buffer (may be 0)
120 // r6 = 0x80808080 (if cr1 bge)
121 // cr1 = blt if this is end of buffer
122
123 .align 5 // align inner loop, which is 8 words long
124 Lbyteloop:
125 lbz r7,GPR_BYTES(r3) // next LHS byte
126 addi r3,r3,1
127 lbz r8,GPR_BYTES(r4) // next RHS byte
128 addi r4,r4,1
129 cmpwi cr0,r7,0 // zero?
130 cmpw cr7,r7,r8 // equal?
131 crandc cr0_eq,cr7_eq,cr0_eq// set cr0_eq if equal and not 0
132 bdnzt eq,Lbyteloop // loop until different, 0, or (ctr==0)
133
134 bne Ldifferent // done if bytes differ or are 0
135 blt cr1,Ldifferent // done if buffer end (ie, if r5==0)
136
137 // LHS is now aligned. Loop over words or doublewords until end of RHS page
138 // or buffer. When we get to the end of the page, we compare GPR_BYTES bytes,
139 // so that we keep the LHS aligned.
140 // r2 = 0xFEFEFEFF
141 // r3 = LHS ptr - GPR_BYTES (aligned)
142 // r4 = RHS ptr - GPR_BYTES (unaligned)
143 // r5 = length remaining in buffer (may be 0)
144 // r6 = 0x80808080
145
146 Laligned:
147 addi r9,r4,GPR_BYTES // restore true address of next RHS byte
148 rlwinm r9,r9,0,0xFFF // get RHS offset in page
149 subfic r0,r9,4096 // get #bytes left in RHS page
150 subfc r7,r0,r5 // ***
151 subfe r8,r5,r5 // * r9 <- min(r0,r5),
152 and r7,r7,r8 // * using algorithm in Compiler Writer's Guide
153 add r9,r0,r7 // ***
154 srgi. r8,r9,LOG2_GPR_BYTES// get #words or doublewords we can compare
155 beq-- Lcrosspage // none so advance to next RHS page
156 slgi r9,r8,LOG2_GPR_BYTES// get #bytes we will be comparing in parallel loop
157 mtctr r8 // set up loop count
158 sub r5,r5,r9 // decrement length remaining
159 b Lwordloop
160
161 // Inner loop: compare a word or doubleword at a time, until one of three conditions:
162 // - a difference is found
163 // - a zero byte is found
164 // - end of count (ie, end of buffer or RHS page, whichever is first)
165 // At this point, registers are as follows:
166 // r2 = 0xFEFEFEFF
167 // r3 = LHS ptr - GPR_BYTES (aligned)
168 // r4 = RHS ptr - GPR_BYTES (unaligned)
169 // r5 = length remaining in buffer (may be 0)
170 // r6 = 0x80808080
171 // ctr = count of words or doublewords until end of buffer or RHS page
172
173 .align 5 // align inner loop, which is 8 words long
174 Lwordloop:
175 lgu r7,GPR_BYTES(r3) // r7 <- next 4 or 8 LHS bytes
176 lgu r8,GPR_BYTES(r4) // r8 <- next 4 or 8 RHS bytes
177 add r10,r7,r2 // r10 <- LHS + 0xFEFEFEFF
178 andc r12,r6,r7 // r12 <- ~LHS & 0x80808080
179 xor r11,r7,r8 // r11 <- compare the words
180 and r9,r10,r12 // r9 <- nonzero iff LHS has a 0-byte
181 or. r12,r9,r11 // combine difference and 0-test vectors
182 bdnzt eq,Lwordloop // loop if ctr!=0 and cr0_eq
183
184 beq-- Lcrosspage // skip if buffer or page end reached
185
186 // Found differing bytes and/or a 0-byte. Determine which comes first, and
187 // subtract the bytes to compute the return value. We also need to mask out the
188 // false hits in the 0-byte test, which consist of 0x01 bytes that preceed
189 // the 0-byte.
190
191 slgi r0,r7,7 // move 0x01 bits in LHS into position 0x80
192 andc r9,r9,r0 // mask out the false 0-hits from 0x01 bytes
193 or r11,r11,r9 // recompute difference vector
194 cntlzg r0,r11 // find 1st difference (r0 = 0..31 or 63)
195 rlwinm r9,r0,0,0x38 // byte align bit offset (r9 = 0,8,16, or 24)
196 addi r0,r9,8 // now, r0 = 8, 16, 24, or 32
197 #if defined(__ppc__)
198 rlwnm r7,r7,r0,24,31 // right justify differing bytes and mask off rest
199 rlwnm r8,r8,r0,24,31
200 #else
201 rldcl r7,r7,r0,56 // right justify differing bytes and mask off rest
202 rldcl r8,r8,r0,56
203 #endif
204
205 Ldifferent: // bytes in r7 and r8 differ or are 0
206 sub r3,r7,r8 // compute return value
207 blr
208