1 /* Optimized strcmp implementation for PowerPC32.
2 Copyright (C) 2003-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 /* See strlen.s for comments on how the end-of-string testing works. */
23 /* int [r3] strncmp (const char *s1 [r3], const char *s2 [r4], size_t size [r5]) */
25 EALIGN (strncmp, 4, 0)
29 #define rSTR1 r3 /* first string arg */
30 #define rSTR2 r4 /* second string arg */
31 #define rN r5 /* max string length */
32 #define rWORD1 r6 /* current word in s1 */
33 #define rWORD2 r7 /* current word in s2 */
36 #define rFEFE r8 /* constant 0xfefefeff (-0x01010101) */
37 #define r7F7F r9 /* constant 0x7f7f7f7f */
38 #define rNEG r10 /* ~(word in s1 | 0x7f7f7f7f) */
39 #define rBITDIF r11 /* bits that differ in s1 & s2 words */
46 clrlwi. rTMP, rTMP, 30
50 /* We are word aligned so set up for two loops. first a word
51 loop, then fall into the byte loop if any residual. */
54 addi rFEFE, rFEFE, -0x101
55 addi r7F7F, r7F7F, 0x7f7f
59 mtctr rTMP /* Power4 wants mtctr 1st in dispatch group. */
66 bne- cr1, L(different)
68 L(g1): add rTMP, rFEFE, rWORD1
69 nor rNEG, r7F7F, rWORD1
72 cmpw cr1, rWORD1, rWORD2
75 /* OK. We've hit the end of the string. We need to be careful that
76 we don't compare two strings as different because of gunk beyond
77 the end of the strings... */
79 #ifdef __LITTLE_ENDIAN__
83 andc rTMP2, rTMP2, rTMP
84 and rWORD2, rWORD2, rTMP2 /* Mask off gunk. */
85 and rWORD1, rWORD1, rTMP2
86 rlwinm rTMP2, rWORD2, 8, 0xffffffff /* Byte reverse word. */
87 rlwinm rTMP, rWORD1, 8, 0xffffffff
88 rldimi rTMP2, rWORD2, 24, 32
89 rldimi rTMP, rWORD1, 24, 32
90 rlwimi rTMP2, rWORD2, 24, 16, 23
91 rlwimi rTMP, rWORD1, 24, 16, 23
92 xor. rBITDIF, rTMP, rTMP2
100 rlwinm rTMP2, rWORD2, 8, 0xffffffff /* Byte reverse word. */
101 rlwinm rTMP, rWORD1, 8, 0xffffffff
102 rldimi rTMP2, rWORD2, 24, 32
103 rldimi rTMP, rWORD1, 24, 32
104 rlwimi rTMP2, rWORD2, 24, 16, 23
105 rlwimi rTMP, rWORD1, 24, 16, 23
106 xor. rBITDIF, rTMP, rTMP2
107 sub rRTN, rTMP, rTMP2
114 and rTMP, r7F7F, rWORD1
116 add rTMP, rTMP, r7F7F
117 xor. rBITDIF, rWORD1, rWORD2
118 andc rNEG, rNEG, rTMP
120 cntlzw rBITDIF, rBITDIF
123 cmpw cr1, rNEG, rBITDIF
124 sub rRTN, rWORD1, rWORD2
131 lwz rWORD1, -4(rSTR1)
132 xor. rBITDIF, rWORD1, rWORD2
133 sub rRTN, rWORD1, rWORD2
140 /* Oh well. In this case, we just do a byte-by-byte comparison. */
143 and. rTMP, rTMP, rNEG
144 cmpw cr1, rWORD1, rWORD2
147 bne- cr1, L(different)
151 mtctr rN /* Power4 wants mtctr 1st in dispatch group */
163 lbzu rWORD3, 1(rSTR1)
164 lbzu rWORD4, 1(rSTR2)
170 lbzu rWORD1, 1(rSTR1)
171 lbzu rWORD2, 1(rSTR2)
177 lbzu rWORD3, 1(rSTR1)
178 lbzu rWORD4, 1(rSTR2)
184 lbzu rWORD1, 1(rSTR1)
185 lbzu rWORD2, 1(rSTR2)
188 L(u3): sub rRTN, rWORD3, rWORD4
190 L(u4): sub rRTN, rWORD1, rWORD2
196 libc_hidden_builtin_def (strncmp)