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/>. */
21 /* Implements the function
23 size_t [r3] strcmp (const char *s1 [r3], const char *s2 [r4])
25 The implementation uses unaligned doubleword access to avoid specialized
26 code paths depending of data alignment. Although recent powerpc64 uses
27 64K as default, the page cross handling assumes minimum page size of
33 /* Check if [s1]+16 or [s2]+16 will cross a 4K page boundary using
36 (((size_t) s1) % PAGE_SIZE > (PAGE_SIZE - ITER_SIZE))
38 with PAGE_SIZE being 4096 and ITER_SIZE begin 16. */
43 bgt cr7,L(pagecross_check)
45 bgt cr5,L(pagecross_check)
47 /* For short string up to 16 bytes, load both s1 and s2 using
48 unaligned dwords and compare. */
54 bne cr0,L(different_nocmpb)
61 bne cr0,L(different_nocmpb)
67 /* Now it has checked for first 16 bytes, align source1 to doubleword
68 and adjust source2 address. */
69 rldicl r9,r7,0,61 /* source1 alignment to doubleword */
70 subf r4,r9,r4 /* Adjust source2 address based on source1
72 rldicr r7,r7,0,60 /* Align source1 to doubleword. */
74 /* At this point, source1 alignment is 0 and source2 alignment is
75 between 0 and 7. Check is source2 alignment is 0, meaning both
76 sources have the same alignment. */
78 bne cr0,L(loop_diff_align)
80 /* If both source1 and source2 are doubleword aligned, there is no
81 need for page boundary cross checks. */
88 bne cr0,L(different_nocmpb)
97 bne cr0,L(different_nocmpb)
104 bne cr0,L(different_nocmpb)
111 bne cr0,L(different_nocmpb)
113 b L(loop_equal_align)
115 /* A zero byte was found in r8 (s1 dword), r9 contains the cmpb
116 result and r10 the dword from s2. To code isolate the byte
117 up to end (including the '\0'), masking with 0xFF the remaining
120 #if __LITTLE_ENDIAN__
121 (__builtin_ffsl (x) - 1) = counting trailing zero bits
122 r9 = (__builtin_ffsl (r9) - 1) + 8;
125 r9 = __builtin_clzl (r9) + 8;
131 #ifdef __LITTLE_ENDIAN__
160 /* If unaligned 16 bytes reads across a 4K page boundary, it uses
161 a simple byte a byte comparison until the page alignment for s1
170 /* Loads a byte from s1 and s2, compare if *s1 is equal to *s2
171 and if *s1 is '\0'. */
178 bne cr7,L(pagecross_ne)
179 beq cr5,L(pagecross_nullfound)
180 bdnz L(pagecross_loop)
184 /* The unaligned read of source2 will cross a 4K page boundary,
185 and the different byte or NULL maybe be in the remaining page
186 bytes. Since it can not use the unaligned load, the algorithm
187 reads and compares 8 bytes to keep source1 doubleword aligned. */
188 L(check_source2_byte):
193 L(check_source2_byte_loop):
200 bne cr7,L(pagecross_ne)
201 beq cr5,L(pagecross_nullfound)
202 bdnz L(check_source2_byte_loop)
204 /* If source2 is unaligned to doubleword, the code needs to check
205 on each interation if the unaligned doubleword access will cross
206 a 4k page boundary. */
214 bne cr0,L(different_nocmpb)
219 /* Check if [src2]+8 cross a 4k page boundary:
221 srcin2 % PAGE_SIZE > (PAGE_SIZE - 8)
223 with PAGE_SIZE being 4096. */
226 ble cr7,L(loop_unaligned)
227 b L(check_source2_byte)
233 L(pagecross_retdiff):
239 L(pagecross_nullfound):
241 b L(pagecross_retdiff)
243 libc_hidden_builtin_def (strcmp)