]> git.saurik.com Git - apple/libc.git/blob - ppc/string/memcmp.s
Libc-339.tar.gz
[apple/libc.git] / ppc / string / memcmp.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 // we need the defs for cr7_eq etc
26 #include <mach/ppc/asm.h>
27 #undef ASSEMBLER
28
29 /* We use mode-independent "g" opcodes such as "srgi". These expand
30 * into word operations when targeting __ppc__, and into doubleword
31 * operations when targeting __ppc64__.
32 */
33 #include <architecture/ppc/mode_independent_asm.h>
34
35
36 // *************** ***********
37 // * M E M C M P * and * B C M P *
38 // *************** ***********
39 //
40 // int memcmp(const char *s1, const char *s2, size_t len);
41 // int bcmp(const char *s1, const char *s2, size_t len);
42 //
43 // Bcmp returns (+,0,-), whereas memcmp returns the true difference
44 // between the first differing bytes, but we treat them identically.
45 //
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, 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.
51 //
52 // In 64-bit mode, this routine is doubleword parallel.
53
54 .text
55 .globl EXT(memcmp)
56 .globl EXT(bcmp)
57
58 .align 5
59 LEXT(memcmp) // int memcmp(const char *s1,const char *s2,size_t len);
60 LEXT(bcmp) // int bcmp(const char *s1,const char *s2,size_t len);
61 cmplgi cr1,r5,2*GPR_BYTES // is buffer too short to bother with parallel compares?
62 andi. r0,r3,GPR_BYTES-1 // is LHS aligned?
63 blt cr1,Lshort // short buffer, so just compare byte-by-byte
64 beq Laligned // skip if aligned
65 subfic r0,r0,GPR_BYTES // r0 <- #bytes to align LHS
66 mtctr r0 // set up for byte loop
67 b Lbyteloop
68
69 // Handle short buffer or end-of-buffer.
70 // r3 = LHS ptr (unaligned)
71 // r4 = RHS ptr (unaligned)
72 // r5 = length remaining in buffer (0..2*GPR_BYTES-1)
73
74 Lshort:
75 cmpgi r5,0 // null buffer?
76 mtctr r5 // assume not null, and set up for loop
77 bne Lshortloop // buffer not null
78 li r3,0 // say "equal"
79 blr
80
81 .align 5
82 Lshortloop:
83 lbz r7,0(r3) // next LHS byte
84 addi r3,r3,1
85 lbz r8,0(r4) // next RHS byte
86 addi r4,r4,1
87 cmpw r7,r8 // compare the bytes
88 bdnzt eq,Lshortloop // loop if more to go and bytes are equal
89
90 sub r3,r7,r8 // generate return value
91 blr
92
93 // We're at a RHS page boundary. Compare GPR_BYTES bytes in order to cross the
94 // page but still keep the LHS ptr aligned.
95
96 Lcrosspage:
97 cmplgi r5,2*GPR_BYTES // enough bytes left to use parallel compares?
98 li r0,GPR_BYTES // get #bytes to cross RHS page
99 blt Lshort // buffer is about to end
100 mtctr r0
101 b Lbyteloop
102
103 // Compare byte-by-byte.
104 // r3 = LHS ptr (unaligned)
105 // r4 = RHS ptr (unaligned)
106 // r5 = length remaining in buffer (must be >0)
107 // ctr = bytes to compare
108
109 .align 5
110 Lbyteloop:
111 lbz r7,0(r3) // next LHS byte
112 addi r3,r3,1
113 lbz r8,0(r4) // next RHS byte
114 addi r4,r4,1
115 subi r5,r5,1 // decrement bytes remaining in buffer
116 cmpw r7,r8 // compare the bytes
117 bdnzt eq,Lbyteloop // loop if more to go and bytes are equal
118
119 bne Ldifferent // done if we found differing bytes
120
121 // LHS is now aligned. Loop over words/doublewords until end of RHS page or buffer.
122 // When we get to the end of the page, we compare 4/8 bytes, so that we keep
123 // the LHS aligned.
124 // r3 = LHS ptr (aligned)
125 // r4 = RHS ptr (unaligned)
126 // r5 = length remaining in buffer (>= GPR_BYTES bytes)
127
128 Laligned:
129 rlwinm r9,r4,0,0xFFF // get RHS offset in page
130 subfic r0,r9,4096 // get #bytes left in RHS page
131 subfc r7,r0,r5 // ***
132 subfe r8,r5,r5 // * r9 <- min(r0,r5),
133 and r7,r7,r8 // * using algorithm in Compiler Writer's Guide
134 add r9,r0,r7 // ***
135 srgi. r8,r9,LOG2_GPR_BYTES// get #words/doublewords we can compare
136 clrrgi r9,r9,LOG2_GPR_BYTES// get #bytes we will compare word-parallel
137 beq-- Lcrosspage // we're at a RHS page boundary
138 mtctr r8 // set up loop count
139 sub r5,r5,r9 // decrement length remaining
140 b Lwordloop
141
142 // Compare a word or doubleword at a time, until one of two conditions:
143 // - a difference is found
144 // - end of count (ie, end of buffer or RHS page, whichever is first)
145 // At this point, registers are as follows:
146 // r3 = LHS ptr (aligned)
147 // r4 = RHS ptr (unaligned)
148 // r5 = length remaining in buffer (may be 0)
149 // ctr = count of word/doublewords until end of buffer or RHS page
150
151 .align 5 // align inner loop
152 Lwordloop:
153 lg r7,0(r3) // r7 <- next aligned LHS word or doubleword
154 addi r3,r3,GPR_BYTES
155 lg r8,0(r4) // r8 <- next unaligned RHS word or doubleword
156 addi r4,r4,GPR_BYTES
157 xor. r11,r7,r8 // compare them
158 bdnzt eq,Lwordloop // loop if ctr!=0 and cr0_eq
159
160 beq Lcrosspage // skip if buffer or page end reached wo difference
161
162 // Found differing bytes.
163
164 cntlzg r0,r11 // find 1st difference (r0 = 0..31 or 63)
165 rlwinm r9,r0,0,0x38 // byte align bit offset (r9 = 0,8,16, or 24 etc)
166 addi r0,r9,8 // now, r0 = 8, 16, 24, or 32 etc
167 #if defined(__ppc__)
168 rlwnm r7,r7,r0,24,31 // right justify differing bytes and mask off rest
169 rlwnm r8,r8,r0,24,31
170 #else
171 rldcl r7,r7,r0,56 // right justify differing bytes and mask off rest
172 rldcl r8,r8,r0,56
173 #endif
174
175 Ldifferent: // bytes in r7 and r8 differ
176 sub r3,r7,r8 // compute return value
177 blr
178