2 * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
26 #include <mach/ppc/asm.h>
29 #define __APPLE_API_PRIVATE
30 #include <machine/cpu_capabilities.h>
31 #undef __APPLE_API_PRIVATE
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__.
37 #include <architecture/ppc/mode_independent_asm.h>
44 // int strcmp(const char *s1, const char *s2);
46 // We optimize the compare by doing it word parallel. This introduces
47 // a complication: if we blindly did word loads from both sides until
48 // finding a difference (or 0), we might get a spurious page fault by
49 // reading bytes past the difference. To avoid this, we never do a "lwz"
50 // that crosses a page boundary.
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
61 // In 64-bit mode, the algorithm is doubleword parallel.
67 LEXT(strcmp) // int strcmp(const char *s1, const char *s2);
68 andi. r0,r3,GPR_BYTES-1 // is LHS aligned?
70 lis r5,hi16(0xFEFEFEFF) // start to generate 32-bit magic constants
71 lis r6,hi16(0x80808080)
72 ori r5,r5,lo16(0xFEFEFEFF)
73 ori r6,r6,lo16(0x80808080)
75 ld r5,_COMM_PAGE_MAGIC_FE(0) // get 0xFEFEFEFE FEFEFEFF from commpage
76 ld r6,_COMM_PAGE_MAGIC_80(0) // get 0x80808080 80808080 from commpage
78 subi r3,r3,GPR_BYTES // we use "lgu" in the inner loops
80 beq Laligned // LHS is aligned
81 subfic r0,r0,GPR_BYTES // r0 <- #bytes to align LHS
87 lbz r7,GPR_BYTES(r3) // r7 <- next LHS byte
89 lbz r8,GPR_BYTES(r4) // r8 <- next RHS byte
91 cntlzw r9,r7 // is r7 zero?
92 sub r0,r7,r8 // different?
93 srwi r9,r9,5 // r9 <- (r7==0) ? 1 : 0
94 or. r9,r9,r0 // r9 is nonzero if either different or 0
95 bdnzt eq,Lbyteloop // loop until different, 0, or buf end
97 bne Ldone // done if different or 0
99 // LHS is aligned. If RHS also is, we need not worry about page
100 // crossing. Otherwise, we must stop the loop before page is crossed.
103 andi. r0,r4,GPR_BYTES-1 // is RHS now aligned too?
104 addi r9,r4,GPR_BYTES // restore true address of next RHS byte
105 rlwinm r9,r9,0,0xFFF // get RHS offset in page
106 beq Lalignedloop // RHS also aligned, use simple loop
107 subfic r9,r9,4096 // get #bytes left in RHS page
108 srwi. r0,r9,LOG2_GPR_BYTES// get #words or doublewords left in RHS page
109 mtctr r0 // set up loop count
110 bne++ Lunalignedloop // at least one word left in RHS page
111 li r0,GPR_BYTES // must check GPR_BYTES, a byte at a time...
112 mtctr r0 // ...in order to keep LHS aligned
113 b Lbyteloop // go cross RHS page
115 // Unaligned inner loop: compare a word or doubleword at a time, until one of
117 // - a difference is found
118 // - a zero byte is found
119 // - end of RHS page (we dare not touch next page until we must)
120 // At this point, registers are as follows:
121 // r3 = LHS ptr - GPR_BYTES (aligned)
122 // r4 = RHS ptr - GPR_BYTES (not aligned)
125 // ctr = words or doublewords left in RHS page
127 .align 5 // align inner loop, which is 8 words long
129 lgu r7,GPR_BYTES(r3) // r7 <- next LHS bytes
130 lgu r8,GPR_BYTES(r4) // r8 <- next RHS bytes
131 add r10,r7,r5 // r10 <- LHS + 0xFEFEFEFF
132 andc r12,r6,r7 // r12 <- ~LHS & 0x80808080
133 xor r11,r7,r8 // r11 <- compare the words
134 and r0,r10,r12 // r0 <- nonzero iff LHS has a 0-byte
135 or. r12,r0,r11 // combine difference and 0-test vectors
136 bdnzt eq,Lunalignedloop // loop if ctr!=0 and cr0_eq
138 bne++ Ldifferent // done if we found a 0 or difference
139 li r0,GPR_BYTES // must check GPR_BYTES, a byte at a time...
140 mtctr r0 // ...in order to keep LHS word aligned
141 b Lbyteloop // cross RHS page, then resume word loop
143 // Aligned inner loop: compare a word at a time, until one of two conditions:
144 // - a difference is found
145 // - a zero byte is found
146 // At this point, registers are as follows:
147 // r3 = LHS ptr - 4 (word aligned)
148 // r4 = RHS ptr - 4 (word aligned)
152 .align 5 // align inner loop, which is 8 words ling
154 lgu r7,GPR_BYTES(r3) // r7 <- next LHS bytes
155 lgu r8,GPR_BYTES(r4) // r8 <- next RHS bytes
156 add r10,r7,r5 // r10 <- LHS + 0xFEFEFEFF
157 andc r12,r6,r7 // r12 <- ~LHS & 0x80808080
158 xor r11,r7,r8 // r11 <- compare the words
159 and r0,r10,r12 // r0 <- nonzero iff LHS has a 0-byte
160 or. r12,r0,r11 // combine difference and 0-test vectors
161 beq Lalignedloop // loop if neither found
163 // Found differing bytes and/or a 0-byte. Determine which comes first, and
164 // subtract the bytes to compute the return value. We also need to mask out the
165 // false hits in the 0-byte test, which consist of 0x01 bytes that preceed
168 Ldifferent: // r0 == 0-test vector (with 0x01 false hits)
169 slgi r9,r7,7 // move 0x01 bits in LHS into position 0x80
170 andc r0,r0,r9 // mask out the false 0-hits from 0x01 bytes
171 or r11,r11,r0 // recompute difference vector
172 cntlzg r9,r11 // find 1st difference (r9 = 0..31 or 63)
173 rlwinm r9,r9,0,0x38 // byte align bit offset (now, r9 = 0,8,16, or 24 etc)
174 addi r0,r9,8 // now, r0 = 8, 16, 24, or 32
176 rlwnm r7,r7,r0,24,31 // right justify differing bytes and mask off rest
179 rldcl r7,r7,r0,56 // right justify differing bytes and mask off rest
183 Ldone: // differing bytes are in r7 and r8
184 sub r3,r7,r8 // compute difference (0, +, or -)