2 * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <mach/ppc/asm.h>
27 #define __APPLE_API_PRIVATE
28 #include <machine/cpu_capabilities.h>
29 #undef __APPLE_API_PRIVATE
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__.
35 #include <architecture/ppc/mode_independent_asm.h>
42 // int strcmp(const char *s1, const char *s2);
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.
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
59 // In 64-bit mode, the algorithm is doubleword parallel.
65 LEXT(strcmp) // int strcmp(const char *s1, const char *s2);
66 andi. r0,r3,GPR_BYTES-1 // is LHS aligned?
68 lis r5,hi16(0xFEFEFEFF) // start to generate 32-bit magic constants
69 lis r6,hi16(0x80808080)
70 ori r5,r5,lo16(0xFEFEFEFF)
71 ori r6,r6,lo16(0x80808080)
73 ld r5,_COMM_PAGE_MAGIC_FE(0) // get 0xFEFEFEFE FEFEFEFF from commpage
74 ld r6,_COMM_PAGE_MAGIC_80(0) // get 0x80808080 80808080 from commpage
76 subi r3,r3,GPR_BYTES // we use "lgu" in the inner loops
78 beq Laligned // LHS is aligned
79 subfic r0,r0,GPR_BYTES // r0 <- #bytes to align LHS
85 lbz r7,GPR_BYTES(r3) // r7 <- next LHS byte
87 lbz r8,GPR_BYTES(r4) // r8 <- next RHS byte
89 cntlzw r9,r7 // is r7 zero?
90 sub r0,r7,r8 // different?
91 srwi r9,r9,5 // r9 <- (r7==0) ? 1 : 0
92 or. r9,r9,r0 // r9 is nonzero if either different or 0
93 bdnzt eq,Lbyteloop // loop until different, 0, or buf end
95 bne Ldone // done if different or 0
97 // LHS is aligned. If RHS also is, we need not worry about page
98 // crossing. Otherwise, we must stop the loop before page is crossed.
101 andi. r0,r4,GPR_BYTES-1 // is RHS now aligned too?
102 addi r9,r4,GPR_BYTES // restore true address of next RHS byte
103 rlwinm r9,r9,0,0xFFF // get RHS offset in page
104 beq Lalignedloop // RHS also aligned, use simple loop
105 subfic r9,r9,4096 // get #bytes left in RHS page
106 srwi. r0,r9,LOG2_GPR_BYTES// get #words or doublewords left in RHS page
107 mtctr r0 // set up loop count
108 bne++ Lunalignedloop // at least one word left in RHS page
109 li r0,GPR_BYTES // must check GPR_BYTES, a byte at a time...
110 mtctr r0 // ...in order to keep LHS aligned
111 b Lbyteloop // go cross RHS page
113 // Unaligned inner loop: compare a word or doubleword at a time, until one of
115 // - a difference is found
116 // - a zero byte is found
117 // - end of RHS page (we dare not touch next page until we must)
118 // At this point, registers are as follows:
119 // r3 = LHS ptr - GPR_BYTES (aligned)
120 // r4 = RHS ptr - GPR_BYTES (not aligned)
123 // ctr = words or doublewords left in RHS page
125 .align 5 // align inner loop, which is 8 words long
127 lgu r7,GPR_BYTES(r3) // r7 <- next LHS bytes
128 lgu r8,GPR_BYTES(r4) // r8 <- next RHS bytes
129 add r10,r7,r5 // r10 <- LHS + 0xFEFEFEFF
130 andc r12,r6,r7 // r12 <- ~LHS & 0x80808080
131 xor r11,r7,r8 // r11 <- compare the words
132 and r0,r10,r12 // r0 <- nonzero iff LHS has a 0-byte
133 or. r12,r0,r11 // combine difference and 0-test vectors
134 bdnzt eq,Lunalignedloop // loop if ctr!=0 and cr0_eq
136 bne++ Ldifferent // done if we found a 0 or difference
137 li r0,GPR_BYTES // must check GPR_BYTES, a byte at a time...
138 mtctr r0 // ...in order to keep LHS word aligned
139 b Lbyteloop // cross RHS page, then resume word loop
141 // Aligned inner loop: compare a word at a time, until one of two conditions:
142 // - a difference is found
143 // - a zero byte is found
144 // At this point, registers are as follows:
145 // r3 = LHS ptr - 4 (word aligned)
146 // r4 = RHS ptr - 4 (word aligned)
150 .align 5 // align inner loop, which is 8 words ling
152 lgu r7,GPR_BYTES(r3) // r7 <- next LHS bytes
153 lgu r8,GPR_BYTES(r4) // r8 <- next RHS bytes
154 add r10,r7,r5 // r10 <- LHS + 0xFEFEFEFF
155 andc r12,r6,r7 // r12 <- ~LHS & 0x80808080
156 xor r11,r7,r8 // r11 <- compare the words
157 and r0,r10,r12 // r0 <- nonzero iff LHS has a 0-byte
158 or. r12,r0,r11 // combine difference and 0-test vectors
159 beq Lalignedloop // loop if neither found
161 // Found differing bytes and/or a 0-byte. Determine which comes first, and
162 // subtract the bytes to compute the return value. We also need to mask out the
163 // false hits in the 0-byte test, which consist of 0x01 bytes that preceed
166 Ldifferent: // r0 == 0-test vector (with 0x01 false hits)
167 slgi r9,r7,7 // move 0x01 bits in LHS into position 0x80
168 andc r0,r0,r9 // mask out the false 0-hits from 0x01 bytes
169 or r11,r11,r0 // recompute difference vector
170 cntlzg r9,r11 // find 1st difference (r9 = 0..31 or 63)
171 rlwinm r9,r9,0,0x38 // byte align bit offset (now, r9 = 0,8,16, or 24 etc)
172 addi r0,r9,8 // now, r0 = 8, 16, 24, or 32
174 rlwnm r7,r7,r0,24,31 // right justify differing bytes and mask off rest
177 rldcl r7,r7,r0,56 // right justify differing bytes and mask off rest
181 Ldone: // differing bytes are in r7 and r8
182 sub r3,r7,r8 // compute difference (0, +, or -)