1 /* Optimized strcmp implementation for PowerPC64/POWER8.
2 Copyright (C) 2015-2019 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 <https://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
35 ENTRY_TOCLESS (STRCMP, 4)
38 /* Check if [s1]+16 or [s2]+16 will cross a 4K page boundary using
41 (((size_t) s1) % PAGE_SIZE > (PAGE_SIZE - ITER_SIZE))
43 with PAGE_SIZE being 4096 and ITER_SIZE begin 16. */
48 bgt cr7,L(pagecross_check)
50 bgt cr5,L(pagecross_check)
52 /* For short string up to 16 bytes, load both s1 and s2 using
53 unaligned dwords and compare. */
59 bne cr0,L(different_nocmpb)
66 bne cr0,L(different_nocmpb)
72 /* Now it has checked for first 16 bytes, align source1 to doubleword
73 and adjust source2 address. */
74 rldicl r9,r7,0,61 /* source1 alignment to doubleword */
75 subf r4,r9,r4 /* Adjust source2 address based on source1
77 rldicr r7,r7,0,60 /* Align source1 to doubleword. */
79 /* At this point, source1 alignment is 0 and source2 alignment is
80 between 0 and 7. Check is source2 alignment is 0, meaning both
81 sources have the same alignment. */
83 bne cr0,L(loop_diff_align)
85 /* If both source1 and source2 are doubleword aligned, there is no
86 need for page boundary cross checks. */
93 bne cr0,L(different_nocmpb)
102 bne cr0,L(different_nocmpb)
109 bne cr0,L(different_nocmpb)
116 bne cr0,L(different_nocmpb)
118 b L(loop_equal_align)
120 /* A zero byte was found in r8 (s1 dword), r9 contains the cmpb
121 result and r10 the dword from s2. To code isolate the byte
122 up to end (including the '\0'), masking with 0xFF the remaining
125 #if __LITTLE_ENDIAN__
126 (__builtin_ffsl (x) - 1) = counting trailing zero bits
127 r9 = (__builtin_ffsl (r9) - 1) + 8;
130 r9 = __builtin_clzl (r9) + 8;
136 #ifdef __LITTLE_ENDIAN__
165 /* If unaligned 16 bytes reads across a 4K page boundary, it uses
166 a simple byte a byte comparison until the page alignment for s1
175 /* Loads a byte from s1 and s2, compare if *s1 is equal to *s2
176 and if *s1 is '\0'. */
183 bne cr7,L(pagecross_ne)
184 beq cr5,L(pagecross_nullfound)
185 bdnz L(pagecross_loop)
189 /* The unaligned read of source2 will cross a 4K page boundary,
190 and the different byte or NULL maybe be in the remaining page
191 bytes. Since it can not use the unaligned load, the algorithm
192 reads and compares 8 bytes to keep source1 doubleword aligned. */
193 L(check_source2_byte):
198 L(check_source2_byte_loop):
205 bne cr7,L(pagecross_ne)
206 beq cr5,L(pagecross_nullfound)
207 bdnz L(check_source2_byte_loop)
209 /* If source2 is unaligned to doubleword, the code needs to check
210 on each interation if the unaligned doubleword access will cross
211 a 4k page boundary. */
219 bne cr0,L(different_nocmpb)
224 /* Check if [src2]+8 cross a 4k page boundary:
226 srcin2 % PAGE_SIZE > (PAGE_SIZE - 8)
228 with PAGE_SIZE being 4096. */
231 ble cr7,L(loop_unaligned)
232 b L(check_source2_byte)
238 L(pagecross_retdiff):
244 L(pagecross_nullfound):
246 b L(pagecross_retdiff)
248 libc_hidden_builtin_def (strcmp)