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.
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.
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.
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/>. */
22 # define STRCMP strcmp
25 /* Implements the function
27 size_t [r3] strcmp (const char *s1 [r3], const char *s2 [r4])
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
34 ENTRY_TOCLESS (STRCMP, 4)
37 /* Check if [s1]+16 or [s2]+16 will cross a 4K page boundary using
40 (((size_t) s1) % PAGE_SIZE > (PAGE_SIZE - ITER_SIZE))
42 with PAGE_SIZE being 4096 and ITER_SIZE begin 16. */
47 bgt cr7,L(pagecross_check)
49 bgt cr5,L(pagecross_check)
51 /* For short string up to 16 bytes, load both s1 and s2 using
52 unaligned dwords and compare. */
58 bne cr0,L(different_nocmpb)
65 bne cr0,L(different_nocmpb)
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
76 rldicr r7,r7,0,60 /* Align source1 to doubleword. */
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. */
82 bne cr0,L(loop_diff_align)
84 /* If both source1 and source2 are doubleword aligned, there is no
85 need for page boundary cross checks. */
92 bne cr0,L(different_nocmpb)
101 bne cr0,L(different_nocmpb)
108 bne cr0,L(different_nocmpb)
115 bne cr0,L(different_nocmpb)
117 b L(loop_equal_align)
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
124 #if __LITTLE_ENDIAN__
125 (__builtin_ffsl (x) - 1) = counting trailing zero bits
126 r9 = (__builtin_ffsl (r9) - 1) + 8;
129 r9 = __builtin_clzl (r9) + 8;
135 #ifdef __LITTLE_ENDIAN__
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
174 /* Loads a byte from s1 and s2, compare if *s1 is equal to *s2
175 and if *s1 is '\0'. */
182 bne cr7,L(pagecross_ne)
183 beq cr5,L(pagecross_nullfound)
184 bdnz L(pagecross_loop)
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):
197 L(check_source2_byte_loop):
204 bne cr7,L(pagecross_ne)
205 beq cr5,L(pagecross_nullfound)
206 bdnz L(check_source2_byte_loop)
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. */
218 bne cr0,L(different_nocmpb)
223 /* Check if [src2]+8 cross a 4k page boundary:
225 srcin2 % PAGE_SIZE > (PAGE_SIZE - 8)
227 with PAGE_SIZE being 4096. */
230 ble cr7,L(loop_unaligned)
231 b L(check_source2_byte)
237 L(pagecross_retdiff):
243 L(pagecross_nullfound):
245 b L(pagecross_retdiff)
247 libc_hidden_builtin_def (strcmp)