]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/powerpc/powerpc64/power8/strcmp.S
PowerPC64 ENTRY_TOCLESS
[thirdparty/glibc.git] / sysdeps / powerpc / powerpc64 / power8 / strcmp.S
1 /* Optimized strcmp implementation for PowerPC64/POWER8.
2 Copyright (C) 2015-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <sysdep.h>
20
21 #ifndef STRCMP
22 # define STRCMP strcmp
23 #endif
24
25 /* Implements the function
26
27 size_t [r3] strcmp (const char *s1 [r3], const char *s2 [r4])
28
29 The implementation uses unaligned doubleword access to avoid specialized
30 code paths depending of data alignment. Although recent powerpc64 uses
31 64K as default, the page cross handling assumes minimum page size of
32 4k. */
33
34 ENTRY_TOCLESS (STRCMP, 4)
35 li r0,0
36
37 /* Check if [s1]+16 or [s2]+16 will cross a 4K page boundary using
38 the code:
39
40 (((size_t) s1) % PAGE_SIZE > (PAGE_SIZE - ITER_SIZE))
41
42 with PAGE_SIZE being 4096 and ITER_SIZE begin 16. */
43
44 rldicl r7,r3,0,52
45 rldicl r9,r4,0,52
46 cmpldi cr7,r7,4096-16
47 bgt cr7,L(pagecross_check)
48 cmpldi cr5,r9,4096-16
49 bgt cr5,L(pagecross_check)
50
51 /* For short string up to 16 bytes, load both s1 and s2 using
52 unaligned dwords and compare. */
53 ld r8,0(r3)
54 ld r10,0(r4)
55 cmpb r12,r8,r0
56 cmpb r11,r8,r10
57 orc. r9,r12,r11
58 bne cr0,L(different_nocmpb)
59
60 ld r8,8(r3)
61 ld r10,8(r4)
62 cmpb r12,r8,r0
63 cmpb r11,r8,r10
64 orc. r9,r12,r11
65 bne cr0,L(different_nocmpb)
66
67 addi r7,r3,16
68 addi r4,r4,16
69
70 L(align_8b):
71 /* Now it has checked for first 16 bytes, align source1 to doubleword
72 and adjust source2 address. */
73 rldicl r9,r7,0,61 /* source1 alignment to doubleword */
74 subf r4,r9,r4 /* Adjust source2 address based on source1
75 alignment. */
76 rldicr r7,r7,0,60 /* Align source1 to doubleword. */
77
78 /* At this point, source1 alignment is 0 and source2 alignment is
79 between 0 and 7. Check is source2 alignment is 0, meaning both
80 sources have the same alignment. */
81 andi. r9,r4,0x7
82 bne cr0,L(loop_diff_align)
83
84 /* If both source1 and source2 are doubleword aligned, there is no
85 need for page boundary cross checks. */
86
87 ld r8,0(r7)
88 ld r10,0(r4)
89 cmpb r12,r8,r0
90 cmpb r11,r8,r10
91 orc. r9,r12,r11
92 bne cr0,L(different_nocmpb)
93
94 .align 4
95 L(loop_equal_align):
96 ld r8,8(r7)
97 ld r10,8(r4)
98 cmpb r12,r8,r0
99 cmpb r11,r8,r10
100 orc. r9,r12,r11
101 bne cr0,L(different_nocmpb)
102
103 ld r8,16(r7)
104 ld r10,16(r4)
105 cmpb r12,r8,r0
106 cmpb r11,r8,r10
107 orc. r9,r12,r11
108 bne cr0,L(different_nocmpb)
109
110 ldu r8,24(r7)
111 ldu r10,24(r4)
112 cmpb r12,r8,r0
113 cmpb r11,r8,r10
114 orc. r9,r12,r11
115 bne cr0,L(different_nocmpb)
116
117 b L(loop_equal_align)
118
119 /* A zero byte was found in r8 (s1 dword), r9 contains the cmpb
120 result and r10 the dword from s2. To code isolate the byte
121 up to end (including the '\0'), masking with 0xFF the remaining
122 ones:
123
124 #if __LITTLE_ENDIAN__
125 (__builtin_ffsl (x) - 1) = counting trailing zero bits
126 r9 = (__builtin_ffsl (r9) - 1) + 8;
127 r9 = -1UL << r9
128 #else
129 r9 = __builtin_clzl (r9) + 8;
130 r9 = -1UL >> r9
131 #endif
132 r8 = r8 | r9
133 r10 = r10 | r9 */
134
135 #ifdef __LITTLE_ENDIAN__
136 nor r9,r9,r9
137 L(different_nocmpb):
138 neg r3,r9
139 and r9,r9,r3
140 cntlzd r9,r9
141 subfic r9,r9,63
142 #else
143 not r9,r9
144 L(different_nocmpb):
145 cntlzd r9,r9
146 subfic r9,r9,56
147 #endif
148 srd r3,r8,r9
149 srd r10,r10,r9
150 rldicl r10,r10,0,56
151 rldicl r3,r3,0,56
152 subf r3,r10,r3
153 extsw r3,r3
154 blr
155
156 .align 4
157 L(pagecross_check):
158 subfic r9,r9,4096
159 subfic r7,r7,4096
160 cmpld cr7,r7,r9
161 bge cr7,L(pagecross)
162 mr r7,r9
163
164 /* If unaligned 16 bytes reads across a 4K page boundary, it uses
165 a simple byte a byte comparison until the page alignment for s1
166 is reached. */
167 L(pagecross):
168 add r7,r3,r7
169 subf r9,r3,r7
170 mtctr r9
171
172 .align 4
173 L(pagecross_loop):
174 /* Loads a byte from s1 and s2, compare if *s1 is equal to *s2
175 and if *s1 is '\0'. */
176 lbz r9,0(r3)
177 lbz r10,0(r4)
178 addi r3,r3,1
179 addi r4,r4,1
180 cmplw cr7,r9,r10
181 cmpdi cr5,r9,r0
182 bne cr7,L(pagecross_ne)
183 beq cr5,L(pagecross_nullfound)
184 bdnz L(pagecross_loop)
185 b L(align_8b)
186
187 .align 4
188 /* The unaligned read of source2 will cross a 4K page boundary,
189 and the different byte or NULL maybe be in the remaining page
190 bytes. Since it can not use the unaligned load, the algorithm
191 reads and compares 8 bytes to keep source1 doubleword aligned. */
192 L(check_source2_byte):
193 li r9,8
194 mtctr r9
195
196 .align 4
197 L(check_source2_byte_loop):
198 lbz r9,0(r7)
199 lbz r10,0(r4)
200 addi r7,r7,1
201 addi r4,r4,1
202 cmplw cr7,r9,10
203 cmpdi r5,r9,0
204 bne cr7,L(pagecross_ne)
205 beq cr5,L(pagecross_nullfound)
206 bdnz L(check_source2_byte_loop)
207
208 /* If source2 is unaligned to doubleword, the code needs to check
209 on each interation if the unaligned doubleword access will cross
210 a 4k page boundary. */
211 .align 5
212 L(loop_unaligned):
213 ld r8,0(r7)
214 ld r10,0(r4)
215 cmpb r12,r8,r0
216 cmpb r11,r8,r10
217 orc. r9,r12,r11
218 bne cr0,L(different_nocmpb)
219 addi r7,r7,8
220 addi r4,r4,8
221
222 L(loop_diff_align):
223 /* Check if [src2]+8 cross a 4k page boundary:
224
225 srcin2 % PAGE_SIZE > (PAGE_SIZE - 8)
226
227 with PAGE_SIZE being 4096. */
228 rldicl r9,r4,0,52
229 cmpldi cr7,r9,4088
230 ble cr7,L(loop_unaligned)
231 b L(check_source2_byte)
232
233 .align 4
234 L(pagecross_ne):
235 extsw r3,r9
236 mr r9,r10
237 L(pagecross_retdiff):
238 subf r9,r9,r3
239 extsw r3,r9
240 blr
241
242 .align 4
243 L(pagecross_nullfound):
244 li r3,0
245 b L(pagecross_retdiff)
246 END (STRCMP)
247 libc_hidden_builtin_def (strcmp)