1 /* Optimized strcmp implementation for Power7 using 'cmpb' instruction
2 Copyright (C) 2014-2021 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/>. */
19 /* The optimization is achieved here through cmpb instruction.
20 8byte aligned strings are processed with double word comparision
21 and unaligned strings are handled effectively with loop unrolling
27 # define STRCMP strcmp
30 /* int [r3] strcmp (const char *s1 [r3], const char *s2 [r4]) */
33 ENTRY_TOCLESS (STRCMP, 4)
37 rldicl. r10, r9, 0, 61 /* are s1 and s2 8 byte aligned..? */
38 bne cr0, L(process_unaligned_bytes)
42 /* process input parameters on double word aligned boundary */
82 beq cr7, L(unrollDword)
86 #ifdef __LITTLE_ENDIAN__
104 #ifdef __LITTLE_ENDIAN__
122 L(process_unaligned_bytes):
123 lbz r9, 0(r3) /* load byte from s1 */
124 lbz r10, 0(r4) /* load byte from s2 */
125 cmpdi cr7, r9, 0 /* compare *s1 with NULL */
126 beq cr7, L(diffOfNULL) /* if *s1 is NULL , return *s1 - *s2 */
127 cmplw cr7, r9, r10 /* compare *s1 and *s2 */
128 bne cr7, L(ComputeDiff) /* branch to compute difference and return */
130 lbz r9, 1(r3) /* load next byte from s1 */
131 lbz r10, 1(r4) /* load next byte from s2 */
132 cmpdi cr7, r9, 0 /* compare *s1 with NULL */
133 beq cr7, L(diffOfNULL) /* if *s1 is NULL , return *s1 - *s2 */
134 cmplw cr7, r9, r10 /* compare *s1 and *s2 */
135 bne cr7, L(ComputeDiff) /* branch to compute difference and return */
137 lbz r9, 2(r3) /* unroll 3rd byte here */
140 beq cr7, L(diffOfNULL)
142 bne 7, L(ComputeDiff)
144 lbz r9, 3(r3) /* unroll 4th byte now */
146 addi r3, r3, 4 /* increment s1 by unroll factor */
149 beq cr7, L(diffOfNULL)
150 addi r4, r4, 4 /* increment s2 by unroll factor */
151 beq cr6, L(process_unaligned_bytes) /* unroll byte processing */
156 subf r10, r10, r9 /* compute s1 - s2 */
163 subf r10, r10, r9 /* compute s1 - s2 */
164 extsw r3, r10 /* sign extend result */
168 libc_hidden_builtin_def (strcmp)