1 /* Optimized strcmp implementation for POWER7/PowerPC32.
2 Copyright (C) 2010-2015 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],
31 #define rSTR1 r3 /* first string arg */
32 #define rSTR2 r4 /* second string arg */
33 #define rN r5 /* max string length */
34 #define rWORD1 r6 /* current word in s1 */
35 #define rWORD2 r7 /* current word in s2 */
38 #define rFEFE r8 /* constant 0xfefefeff (-0x01010101) */
39 #define r7F7F r9 /* constant 0x7f7f7f7f */
40 #define rNEG r10 /* ~(word in s1 | 0x7f7f7f7f) */
41 #define rBITDIF r11 /* bits that differ in s1 & s2 words */
54 /* We are word aligned so set up for two loops. first a word
55 loop, then fall into the byte loop if any residual. */
58 addi rFEFE,rFEFE,-0x101
59 addi r7F7F,r7F7F,0x7f7f
72 L(g1): add rTMP,rFEFE,rWORD1
76 cmpw cr1,rWORD1,rWORD2
79 /* OK. We've hit the end of the string. We need to be careful that
80 we don't compare two strings as different because of gunk beyond
81 the end of the strings... */
82 #ifdef __LITTLE_ENDIAN__
86 andc rTMP2, rTMP2, rTMP
87 and rWORD2, rWORD2, rTMP2 /* Mask off gunk. */
88 and rWORD1, rWORD1, rTMP2
89 rlwinm rTMP2, rWORD2, 8, 0xffffffff /* Byte reverse word. */
90 rlwinm rTMP, rWORD1, 8, 0xffffffff
91 rldimi rTMP2, rWORD2, 24, 32
92 rldimi rTMP, rWORD1, 24, 32
93 rlwimi rTMP2, rWORD2, 24, 16, 23
94 rlwimi rTMP, rWORD1, 24, 16, 23
95 xor. rBITDIF, rTMP, rTMP2
102 lwz rWORD1, -4(rSTR1)
103 rlwinm rTMP2, rWORD2, 8, 0xffffffff /* Byte reverse word. */
104 rlwinm rTMP, rWORD1, 8, 0xffffffff
105 rldimi rTMP2, rWORD2, 24, 32
106 rldimi rTMP, rWORD1, 24, 32
107 rlwimi rTMP2, rWORD2, 24, 16, 23
108 rlwimi rTMP, rWORD1, 24, 16, 23
109 xor. rBITDIF, rTMP, rTMP2
110 sub rRTN, rTMP, rTMP2
117 and rTMP,r7F7F,rWORD1
120 xor. rBITDIF,rWORD1,rWORD2
123 cntlzw rBITDIF,rBITDIF
126 cmpw cr1,rNEG,rBITDIF
127 sub rRTN,rWORD1,rWORD2
135 xor. rBITDIF,rWORD1,rWORD2
136 sub rRTN,rWORD1,rWORD2
143 /* Oh well. In this case, we just do a byte-by-byte comparison. */
147 cmpw cr1,rWORD1,rWORD2
191 L(u3): sub rRTN,rWORD3,rWORD4
193 L(u4): sub rRTN,rWORD1,rWORD2
199 libc_hidden_builtin_def (strncmp)