1 /* Optimized strncmp implementation for PowerPC64/POWER8.
2 Copyright (C) 2015-2024 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 STRNCMP strncmp
25 /* Implements the function
27 int [r3] strncmp (const char *s1 [r3], const char *s2 [r4], size_t [r5] n)
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 (STRNCMP, 4)
36 /* Check if size is 0. */
40 /* Check if [s1]+16 or [s2]+16 will cross a 4K page boundary using
43 (((size_t) s1) % PAGE_SIZE > (PAGE_SIZE - ITER_SIZE))
45 with PAGE_SIZE being 4096 and ITER_SIZE begin 16. */
53 /* For short string up to 16 bytes, load both s1 and s2 using
54 unaligned dwords and compare. */
63 /* If the string compared are equal, but size is less or equal
81 /* Update pointers and size. */
86 /* Now it has checked for first 16 bytes, align source1 to doubleword
87 and adjust source2 address. */
94 /* At this point, source1 alignment is 0 and source2 alignment is
95 between 0 and 7. Check is source2 alignment is 0, meaning both
96 sources have the same alignment. */
98 beq cr0,L(loop_eq_align_0)
103 /* If source2 is unaligned to doubleword, the code needs to check
104 on each iteration if the unaligned doubleword access will cross
105 a 4k page boundary. */
113 bne cr0,L(different1)
123 ble cr7,L(loop_ne_align_0)
132 beq cr7,L(size_reached_0)
141 /* The unaligned read of source2 will cross a 4K page boundary,
142 and the different byte or NULL maybe be in the remaining page
143 bytes. Since it can not use the unaligned load the algorithm
144 reads and compares 8 bytes to keep source1 doubleword aligned. */
146 L(loop_ne_align_byte):
156 bne cr7,L(size_reached_2)
157 beq cr5,L(size_reached_0)
158 bdnz L(loop_ne_align_byte)
161 bne+ cr7,L(loop_ne_align_0)
170 /* The code now check if r8 and r10 are different by issuing a
171 cmpb and shift the result based on its output:
173 #ifdef __LITTLE_ENDIAN__
174 leadzero = (__builtin_ffsl (z1) - 1);
175 leadzero = leadzero > (n-1)*8 ? (n-1)*8 : leadzero;
176 r1 = (r1 >> leadzero) & 0xFFUL;
177 r2 = (r2 >> leadzero) & 0xFFUL;
179 leadzero = __builtin_clzl (z1);
180 leadzero = leadzero > (n-1)*8 ? (n-1)*8 : leadzero;
181 r1 = (r1 >> (56 - leadzero)) & 0xFFUL;
182 r2 = (r2 >> (56 - leadzero)) & 0xFFUL;
189 #ifdef __LITTLE_ENDIAN__
199 ble cr7,L(different2)
209 blt cr7,L(different2)
223 /* If unaligned 16 bytes reads across a 4K page boundary, it uses
224 a simple byte a byte comparison until the page alignment for s1
256 bdnz L(pagecross_loop0)
262 /* If both source1 and source2 are doubleword aligned, there is no
263 need for page boundary cross checks. */
271 bne cr0,L(different1)
293 beq cr0,L(loop_eq_align_1)
327 libc_hidden_builtin_def(strncmp)