Fix spelling errors in sysdeps/powerpc files.
[glibc.git] / sysdeps / powerpc / powerpc64 / power7 / strncmp.S
blob25a6baf479230e481e2ef3ca4d746ac048f3a546
1 /* Optimized strcmp implementation for POWER7/PowerPC64.
2    Copyright (C) 2010-2013 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>
20 #include <bp-sym.h>
21 #include <bp-asm.h>
23 /* See strlen.s for comments on how the end-of-string testing works.  */
25 /* int [r3] strncmp (const char *s1 [r3],
26                      const char *s2 [r4],
27                      size_t size [r5])  */
29 EALIGN (BP_SYM(strncmp),5,0)
30         CALL_MCOUNT 3
32 #define rTMP    r0
33 #define rRTN    r3
34 #define rSTR1   r3      /* first string arg */
35 #define rSTR2   r4      /* second string arg */
36 #define rN      r5      /* max string length */
37 /* Note:  The Bounded pointer support in this code is broken.  This code
38    was inherited from PPC32 and that support was never completed.
39    Current PPC gcc does not support -fbounds-check or -fbounded-pointers.  */
40 #define rWORD1  r6      /* current word in s1 */
41 #define rWORD2  r7      /* current word in s2 */
42 #define rWORD3  r10
43 #define rWORD4  r11
44 #define rFEFE   r8      /* constant 0xfefefefefefefeff (-0x0101010101010101) */
45 #define r7F7F   r9      /* constant 0x7f7f7f7f7f7f7f7f */
46 #define rNEG    r10     /* ~(word in s1 | 0x7f7f7f7f7f7f7f7f) */
47 #define rBITDIF r11     /* bits that differ in s1 & s2 words */
49         dcbt    0,rSTR1
50         nop
51         or      rTMP,rSTR2,rSTR1
52         lis     r7F7F,0x7f7f
53         dcbt    0,rSTR2
54         nop
55         clrldi. rTMP,rTMP,61
56         cmpldi  cr1,rN,0
57         lis     rFEFE,-0x101
58         bne     L(unaligned)
59 /* We are doubleword aligned so set up for two loops.  first a double word
60    loop, then fall into the byte loop if any residual.  */
61         srdi.   rTMP,rN,3
62         clrldi  rN,rN,61
63         addi    rFEFE,rFEFE,-0x101
64         addi    r7F7F,r7F7F,0x7f7f
65         cmpldi  cr1,rN,0
66         beq     L(unaligned)
68         mtctr   rTMP
69         ld      rWORD1,0(rSTR1)
70         ld      rWORD2,0(rSTR2)
71         sldi    rTMP,rFEFE,32
72         insrdi  r7F7F,r7F7F,32,0
73         add     rFEFE,rFEFE,rTMP
74         b       L(g1)
76 L(g0):
77         ldu     rWORD1,8(rSTR1)
78         bne     cr1,L(different)
79         ldu     rWORD2,8(rSTR2)
80 L(g1):  add     rTMP,rFEFE,rWORD1
81         nor     rNEG,r7F7F,rWORD1
82         bdz     L(tail)
83         and.    rTMP,rTMP,rNEG
84         cmpd    cr1,rWORD1,rWORD2
85         beq     L(g0)
87 /* OK. We've hit the end of the string. We need to be careful that
88    we don't compare two strings as different because of gunk beyond
89    the end of the strings...  */
91 L(endstring):
92         and     rTMP,r7F7F,rWORD1
93         beq     cr1,L(equal)
94         add     rTMP,rTMP,r7F7F
95         xor.    rBITDIF,rWORD1,rWORD2
97         andc    rNEG,rNEG,rTMP
98         blt     L(highbit)
99         cntlzd  rBITDIF,rBITDIF
100         cntlzd  rNEG,rNEG
101         addi    rNEG,rNEG,7
102         cmpd    cr1,rNEG,rBITDIF
103         sub     rRTN,rWORD1,rWORD2
104         blt     cr1,L(equal)
105         sradi   rRTN,rRTN,63
106         ori     rRTN,rRTN,1
107         blr
108 L(equal):
109         li      rRTN,0
110         blr
112 L(different):
113         ldu     rWORD1,-8(rSTR1)
114         xor.    rBITDIF,rWORD1,rWORD2
115         sub     rRTN,rWORD1,rWORD2
116         blt     L(highbit)
117         sradi   rRTN,rRTN,63
118         ori     rRTN,rRTN,1
119         blr
120 L(highbit):
121         srdi    rWORD2,rWORD2,56
122         srdi    rWORD1,rWORD1,56
123         sub     rRTN,rWORD1,rWORD2
124         blr
127 /* Oh well.  In this case, we just do a byte-by-byte comparison.  */
128         .align  4
129 L(tail):
130         and.    rTMP,rTMP,rNEG
131         cmpd    cr1,rWORD1,rWORD2
132         bne     L(endstring)
133         addi    rSTR1,rSTR1,8
134         bne     cr1,L(different)
135         addi    rSTR2,rSTR2,8
136         cmpldi  cr1,rN,0
137 L(unaligned):
138         mtctr   rN
139         ble     cr1,L(ux)
140 L(uz):
141         lbz     rWORD1,0(rSTR1)
142         lbz     rWORD2,0(rSTR2)
143         .align  4
144 L(u1):
145         cmpdi   cr1,rWORD1,0
146         bdz     L(u4)
147         cmpd    rWORD1,rWORD2
148         beq     cr1,L(u4)
149         bne     L(u4)
150         lbzu    rWORD3,1(rSTR1)
151         lbzu    rWORD4,1(rSTR2)
152         cmpdi   cr1,rWORD3,0
153         bdz     L(u3)
154         cmpd    rWORD3,rWORD4
155         beq     cr1,L(u3)
156         bne     L(u3)
157         lbzu    rWORD1,1(rSTR1)
158         lbzu    rWORD2,1(rSTR2)
159         cmpdi   cr1,rWORD1,0
160         bdz     L(u4)
161         cmpd    rWORD1,rWORD2
162         beq     cr1,L(u4)
163         bne     L(u4)
164         lbzu    rWORD3,1(rSTR1)
165         lbzu    rWORD4,1(rSTR2)
166         cmpdi   cr1,rWORD3,0
167         bdz     L(u3)
168         cmpd    rWORD3,rWORD4
169         beq     cr1,L(u3)
170         bne     L(u3)
171         lbzu    rWORD1,1(rSTR1)
172         lbzu    rWORD2,1(rSTR2)
173         b       L(u1)
175 L(u3):  sub     rRTN,rWORD3,rWORD4
176         blr
177 L(u4):  sub     rRTN,rWORD1,rWORD2
178         blr
179 L(ux):
180         li      rRTN,0
181         blr
182 END (BP_SYM (strncmp))
183 libc_hidden_builtin_def (strncmp)