Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / powerpc / powerpc64 / strcmp.S
blob6cd587cd99fc6d7be08f7e1d94554c702f58e4ae
1 /* Optimized strcmp implementation for PowerPC64.
2    Copyright (C) 1997-2014 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/>.  */
19 #include <sysdep.h>
21 /* See strlen.s for comments on how the end-of-string testing works.  */
23 /* int [r3] strcmp (const char *s1 [r3], const char *s2 [r4])  */
25 EALIGN (strcmp, 4, 0)
26         CALL_MCOUNT 2
28 #define rTMP2   r0
29 #define rRTN    r3
30 #define rSTR1   r3      /* first string arg */
31 #define rSTR2   r4      /* second string arg */
32 #define rWORD1  r5      /* current word in s1 */
33 #define rWORD2  r6      /* current word in s2 */
34 #define rFEFE   r7      /* constant 0xfefefefefefefeff (-0x0101010101010101) */
35 #define r7F7F   r8      /* constant 0x7f7f7f7f7f7f7f7f */
36 #define rNEG    r9      /* ~(word in s1 | 0x7f7f7f7f7f7f7f7f) */
37 #define rBITDIF r10     /* bits that differ in s1 & s2 words */
38 #define rTMP    r11
40         dcbt    0,rSTR1
41         or      rTMP, rSTR2, rSTR1
42         dcbt    0,rSTR2
43         clrldi. rTMP, rTMP, 61
44         lis     rFEFE, -0x101
45         bne     L(unaligned)
47         ld      rWORD1, 0(rSTR1)
48         ld      rWORD2, 0(rSTR2)
49         lis     r7F7F, 0x7f7f
50         addi    rFEFE, rFEFE, -0x101
51         addi    r7F7F, r7F7F, 0x7f7f
52         sldi    rTMP, rFEFE, 32
53         insrdi  r7F7F, r7F7F, 32, 0
54         add     rFEFE, rFEFE, rTMP
55         b       L(g1)
57 L(g0):  ldu     rWORD1, 8(rSTR1)
58         bne     cr1, L(different)
59         ldu     rWORD2, 8(rSTR2)
60 L(g1):  add     rTMP, rFEFE, rWORD1
61         nor     rNEG, r7F7F, rWORD1
62         and.    rTMP, rTMP, rNEG
63         cmpd    cr1, rWORD1, rWORD2
64         beq+    L(g0)
66 /* OK. We've hit the end of the string. We need to be careful that
67    we don't compare two strings as different because of gunk beyond
68    the end of the strings...  */
69 #ifdef __LITTLE_ENDIAN__
70 L(endstring):
71         addi    rTMP2, rTMP, -1
72         beq     cr1, L(equal)
73         andc    rTMP2, rTMP2, rTMP
74         rldimi  rTMP2, rTMP2, 1, 0
75         and     rWORD2, rWORD2, rTMP2   /* Mask off gunk.  */
76         and     rWORD1, rWORD1, rTMP2
77         cmpd    cr1, rWORD1, rWORD2
78         beq     cr1, L(equal)
79         xor     rBITDIF, rWORD1, rWORD2 /* rBITDIF has bits that differ.  */
80         neg     rNEG, rBITDIF
81         and     rNEG, rNEG, rBITDIF     /* rNEG has LS bit that differs.  */
82         cntlzd  rNEG, rNEG              /* bitcount of the bit.  */
83         andi.   rNEG, rNEG, 56          /* bitcount to LS byte that differs. */
84         sld     rWORD1, rWORD1, rNEG    /* shift left to clear MS bytes.  */
85         sld     rWORD2, rWORD2, rNEG
86         xor.    rBITDIF, rWORD1, rWORD2
87         sub     rRTN, rWORD1, rWORD2
88         blt-    L(highbit)
89         sradi   rRTN, rRTN, 63          /* must return an int.  */
90         ori     rRTN, rRTN, 1
91         blr
92 L(equal):
93         li      rRTN, 0
94         blr
96 L(different):
97         ld      rWORD1, -8(rSTR1)
98         xor     rBITDIF, rWORD1, rWORD2 /* rBITDIF has bits that differ.  */
99         neg     rNEG, rBITDIF
100         and     rNEG, rNEG, rBITDIF     /* rNEG has LS bit that differs.  */
101         cntlzd  rNEG, rNEG              /* bitcount of the bit.  */
102         andi.   rNEG, rNEG, 56          /* bitcount to LS byte that differs. */
103         sld     rWORD1, rWORD1, rNEG    /* shift left to clear MS bytes.  */
104         sld     rWORD2, rWORD2, rNEG
105         xor.    rBITDIF, rWORD1, rWORD2
106         sub     rRTN, rWORD1, rWORD2
107         blt-    L(highbit)
108         sradi   rRTN, rRTN, 63
109         ori     rRTN, rRTN, 1
110         blr
111 L(highbit):
112         sradi   rRTN, rWORD2, 63
113         ori     rRTN, rRTN, 1
114         blr
116 #else
117 L(endstring):
118         and     rTMP, r7F7F, rWORD1
119         beq     cr1, L(equal)
120         add     rTMP, rTMP, r7F7F
121         xor.    rBITDIF, rWORD1, rWORD2
122         andc    rNEG, rNEG, rTMP
123         blt-    L(highbit)
124         cntlzd  rBITDIF, rBITDIF
125         cntlzd  rNEG, rNEG
126         addi    rNEG, rNEG, 7
127         cmpd    cr1, rNEG, rBITDIF
128         sub     rRTN, rWORD1, rWORD2
129         blt-    cr1, L(equal)
130         sradi   rRTN, rRTN, 63          /* must return an int.  */
131         ori     rRTN, rRTN, 1
132         blr
133 L(equal):
134         li      rRTN, 0
135         blr
137 L(different):
138         ld      rWORD1, -8(rSTR1)
139         xor.    rBITDIF, rWORD1, rWORD2
140         sub     rRTN, rWORD1, rWORD2
141         blt-    L(highbit)
142         sradi   rRTN, rRTN, 63
143         ori     rRTN, rRTN, 1
144         blr
145 L(highbit):
146         sradi   rRTN, rWORD2, 63
147         ori     rRTN, rRTN, 1
148         blr
149 #endif
151 /* Oh well.  In this case, we just do a byte-by-byte comparison.  */
152         .align 4
153 L(unaligned):
154         lbz     rWORD1, 0(rSTR1)
155         lbz     rWORD2, 0(rSTR2)
156         b       L(u1)
158 L(u0):  lbzu    rWORD1, 1(rSTR1)
159         bne-    L(u4)
160         lbzu    rWORD2, 1(rSTR2)
161 L(u1):  cmpwi   cr1, rWORD1, 0
162         beq-    cr1, L(u3)
163         cmpd    rWORD1, rWORD2
164         bne-    L(u3)
165         lbzu    rWORD1, 1(rSTR1)
166         lbzu    rWORD2, 1(rSTR2)
167         cmpdi   cr1, rWORD1, 0
168         cmpd    rWORD1, rWORD2
169         bne+    cr1, L(u0)
170 L(u3):  sub     rRTN, rWORD1, rWORD2
171         blr
172 L(u4):  lbz     rWORD1, -1(rSTR1)
173         sub     rRTN, rWORD1, rWORD2
174         blr
175 END (strcmp)
176 libc_hidden_builtin_def (strcmp)