2.9
[glibc/nacl-glibc.git] / sysdeps / powerpc / powerpc64 / strcmp.S
blob4d7eb21bf721c996751158436274b5e4564d3bec
1 /* Optimized strcmp implementation for PowerPC64.
2    Copyright (C) 1997, 1999, 2000, 2002, 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] strcmp (const char *s1 [r3], const char *s2 [r4])  */
28 EALIGN (BP_SYM(strcmp), 4, 0)
29         CALL_MCOUNT 2
31 #define rTMP    r0
32 #define rRTN    r3
33 #define rSTR1   r3      /* first string arg */
34 #define rSTR2   r4      /* second string arg */
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    These artifacts are left in the code as a reminder in case we need
39    bounded pointer support in the future.  */
40 #if __BOUNDED_POINTERS__
41 # define rHIGH1 r11
42 # define rHIGH2 r12
43 #endif
44 #define rWORD1  r5      /* current word in s1 */
45 #define rWORD2  r6      /* current word in s2 */
46 #define rFEFE   r7      /* constant 0xfefefefefefefeff (-0x0101010101010101) */
47 #define r7F7F   r8      /* constant 0x7f7f7f7f7f7f7f7f */
48 #define rNEG    r9      /* ~(word in s1 | 0x7f7f7f7f7f7f7f7f) */
49 #define rBITDIF r10     /* bits that differ in s1 & s2 words */
51         CHECK_BOUNDS_LOW (rSTR1, rTMP, rHIGH1)
52         CHECK_BOUNDS_LOW (rSTR2, rTMP, rHIGH2)
54         dcbt    0,rSTR1
55         or      rTMP, rSTR2, rSTR1
56         dcbt    0,rSTR2
57         clrldi. rTMP, rTMP, 61
58         lis     rFEFE, -0x101
59         bne     L(unaligned)
61         ld      rWORD1, 0(rSTR1)
62         ld      rWORD2, 0(rSTR2)
63         lis     r7F7F, 0x7f7f
64         addi    rFEFE, rFEFE, -0x101
65         addi    r7F7F, r7F7F, 0x7f7f
66         sldi    rTMP, rFEFE, 32
67         insrdi  r7F7F, r7F7F, 32, 0
68         add     rFEFE, rFEFE, rTMP
69         b       L(g1)
71 L(g0):  ldu     rWORD1, 8(rSTR1)
72         bne     cr1, L(different)
73         ldu     rWORD2, 8(rSTR2)
74 L(g1):  add     rTMP, rFEFE, rWORD1
75         nor     rNEG, r7F7F, rWORD1
77         and.    rTMP, rTMP, rNEG
78         cmpd    cr1, rWORD1, rWORD2
79         beq+    L(g0)
80 L(endstring):
81 /* OK. We've hit the end of the string. We need to be careful that
82    we don't compare two strings as different because of gunk beyond
83    the end of the strings...  */
84         and     rTMP, r7F7F, rWORD1
85         beq     cr1, L(equal)
86         add     rTMP, rTMP, r7F7F
87         xor.    rBITDIF, rWORD1, rWORD2
89         andc    rNEG, rNEG, rTMP
90         blt-    L(highbit)
91         cntlzd  rBITDIF, rBITDIF
92         cntlzd  rNEG, rNEG
93         addi    rNEG, rNEG, 7
94         cmpd    cr1, rNEG, rBITDIF
95         sub     rRTN, rWORD1, rWORD2
96         blt-    cr1, L(equal)
97         sradi   rRTN, rRTN, 63
98         ori     rRTN, rRTN, 1
99         blr
100 L(equal):
101         li      rRTN, 0
102         /* GKM FIXME: check high bounds.  */
103         blr
105 L(different):
106         ld      rWORD1, -8(rSTR1)
107         xor.    rBITDIF, rWORD1, rWORD2
108         sub     rRTN, rWORD1, rWORD2
109         blt-    L(highbit)
110         sradi   rRTN, rRTN, 63
111         ori     rRTN, rRTN, 1
112         blr
113 L(highbit):
114         srdi    rWORD2, rWORD2, 56
115         srdi    rWORD1, rWORD1, 56
116         sub     rRTN, rWORD1, rWORD2
117         /* GKM FIXME: check high bounds.  */
118         blr
121 /* Oh well.  In this case, we just do a byte-by-byte comparison.  */
122         .align 4
123 L(unaligned):
124         lbz     rWORD1, 0(rSTR1)
125         lbz     rWORD2, 0(rSTR2)
126         b       L(u1)
128 L(u0):  lbzu    rWORD1, 1(rSTR1)
129         bne-    L(u4)
130         lbzu    rWORD2, 1(rSTR2)
131 L(u1):  cmpwi   cr1, rWORD1, 0
132         beq-    cr1, L(u3)
133         cmpd    rWORD1, rWORD2
134         bne-    L(u3)
135         lbzu    rWORD1, 1(rSTR1)
136         lbzu    rWORD2, 1(rSTR2)
137         cmpdi   cr1, rWORD1, 0
138         cmpd    rWORD1, rWORD2
139         bne+    cr1, L(u0)
140 L(u3):  sub     rRTN, rWORD1, rWORD2
141         /* GKM FIXME: check high bounds.  */
142         blr
143 L(u4):  lbz     rWORD1, -1(rSTR1)
144         sub     rRTN, rWORD1, rWORD2
145         /* GKM FIXME: check high bounds.  */
146         blr
147 END (BP_SYM (strcmp))
148 libc_hidden_builtin_def (strcmp)