(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / powerpc / powerpc32 / strncmp.S
blob3b33bb921f105ec5a9dc8089dcbdf7a061f56454
1 /* Optimized strcmp implementation for PowerPC32.
2    Copyright (C) 2003 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, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
20 #include <sysdep.h>
21 #include <bp-sym.h>
22 #include <bp-asm.h>
24 /* See strlen.s for comments on how the end-of-string testing works.  */
26 /* int [r3] strncmp (const char *s1 [r3], const char *s2 [r4], size_t size [r5])  */
28 EALIGN (BP_SYM(strncmp), 4, 0)
30 #define rTMP    r0
31 #define rRTN    r3
32 #define rSTR1   r3      /* first string arg */
33 #define rSTR2   r4      /* second string arg */
34 #define rN      r5      /* max string length */
35 /* Note:  The Bounded pointer support in this code is broken.  This code
36    was inherited from PPC32 and and that support was never completed.  
37    Current PPC gcc does not support -fbounds-check or -fbounded-pointers.  */
38 #define rWORD1  r6      /* current word in s1 */
39 #define rWORD2  r7      /* current word in s2 */
40 #define rFEFE   r8      /* constant 0xfefefeff (-0x01010101) */
41 #define r7F7F   r9      /* constant 0x7f7f7f7f */
42 #define rNEG    r10     /* ~(word in s1 | 0x7f7f7f7f) */
43 #define rBITDIF r11     /* bits that differ in s1 & s2 words */
45         dcbt    0,rSTR1
46         or      rTMP, rSTR2, rSTR1
47         lis     r7F7F, 0x7f7f
48         dcbt    0,rSTR2
49         clrlwi. rTMP, rTMP, 30
50         lis     rFEFE, -0x101
51         bne     L(unaligned)
52 /* We are word alligned so set up for two loops.  first a word
53    loop, then fall into the byte loop if any residual.  */
54         srwi.   rTMP, rN, 2
55         clrlwi  rN, rN, 30
56         addi    rFEFE, rFEFE, -0x101
57         addi    r7F7F, r7F7F, 0x7f7f    
58         beq     L(unaligned)
60         mtctr   rTMP    /* Power4 wants mtctr 1st in dispatch group.  */
61         lwz     rWORD1, 0(rSTR1)
62         lwz     rWORD2, 0(rSTR2)
63         b       L(g1)
65 L(g0):  
66         lwzu    rWORD1, 4(rSTR1)
67         bne-    cr1, L(different)
68         lwzu    rWORD2, 4(rSTR2)
69 L(g1):  add     rTMP, rFEFE, rWORD1
70         nor     rNEG, r7F7F, rWORD1
71         bdz     L(tail)
72         and.    rTMP, rTMP, rNEG
73         cmpw    cr1, rWORD1, rWORD2
74         beq+    L(g0)
75         
76 /* OK. We've hit the end of the string. We need to be careful that
77    we don't compare two strings as different because of gunk beyond
78    the end of the strings...  */
79         
80 L(endstring):
81         and     rTMP, r7F7F, rWORD1
82         beq     cr1, L(equal)
83         add     rTMP, rTMP, r7F7F
84         xor.    rBITDIF, rWORD1, rWORD2
86         andc    rNEG, rNEG, rTMP
87         blt-    L(highbit)
88         cntlzw  rBITDIF, rBITDIF
89         cntlzw  rNEG, rNEG
90         addi    rNEG, rNEG, 7
91         cmpw    cr1, rNEG, rBITDIF
92         sub     rRTN, rWORD1, rWORD2
93         blt-    cr1, L(equal)
94         srawi   rRTN, rRTN, 31
95         ori     rRTN, rRTN, 1
96         blr
97 L(equal):
98         li      rRTN, 0
99         blr
101 L(different):
102         lwzu    rWORD1, -4(rSTR1)
103         xor.    rBITDIF, rWORD1, rWORD2
104         sub     rRTN, rWORD1, rWORD2
105         blt-    L(highbit)
106         srawi   rRTN, rRTN, 31
107         ori     rRTN, rRTN, 1
108         blr
109 L(highbit):
110         srwi    rWORD2, rWORD2, 24
111         srwi    rWORD1, rWORD1, 24
112         sub     rRTN, rWORD1, rWORD2
113         blr
116 /* Oh well.  In this case, we just do a byte-by-byte comparison.  */
117         .align 4
118 L(tail):
119         and.    rTMP, rTMP, rNEG
120         cmpw    cr1, rWORD1, rWORD2
121         bne-    L(endstring)
122         addi    rSTR1, rSTR1, 4
123         bne-    cr1, L(different)
124         addi    rSTR2, rSTR2, 4
125 L(unaligned):
126         mtctr   rN      /* Power4 wants mtctr 1st in dispatch group */
127         cmpwi   rN,0
128         lbz     rWORD1, 0(rSTR1)
129         lbz     rWORD2, 0(rSTR2)
130         bgt     L(u1)
131 L(ux):
132         li      rRTN, 0
133         blr
135 L(u0):
136         lbzu    rWORD2, 1(rSTR2)
137 L(u1):
138         bdz     L(u3)
139         cmpwi   cr1, rWORD1, 0
140         cmpw    rWORD1, rWORD2
141         beq-    cr1, L(u3)
142         lbzu    rWORD1, 1(rSTR1)
143         bne-    L(u2)
144         lbzu    rWORD2, 1(rSTR2)
145         bdz     L(u3)
146         cmpwi   cr1, rWORD1, 0
147         cmpw    rWORD1, rWORD2
148         bne-    L(u3)
149         lbzu    rWORD1, 1(rSTR1)
150         bne+    cr1, L(u0)
152 L(u2):  lbzu    rWORD1, -1(rSTR1)       
153 L(u3):  sub     rRTN, rWORD1, rWORD2
154         blr
155 END (BP_SYM (strncmp))
156 libc_hidden_builtin_def (strncmp)