~vorlon/ubuntu/natty/eglibc/multiarch

« back to all changes in this revision

Viewing changes to sysdeps/powerpc/powerpc64/power7/strchr.S

  • Committer: Steve Langasek
  • Date: 2011-02-18 21:18:44 UTC
  • mfrom: (103.1.7 eglibc)
  • Revision ID: steve.langasek@linaro.org-20110218211844-lodmi8b1qhyq3f3x
Tags: 2.13~pre1-0ubuntu1+multiarch.1
merge from natty

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Optimized strchr implementation for PowerPC64/POWER7 using cmpb insn.
 
2
   Copyright (C) 2010 Free Software Foundation, Inc.
 
3
   Contributed by Luis Machado <luisgpm@br.ibm.com>.
 
4
   This file is part of the GNU C Library.
 
5
 
 
6
   The GNU C Library is free software; you can redistribute it and/or
 
7
   modify it under the terms of the GNU Lesser General Public
 
8
   License as published by the Free Software Foundation; either
 
9
   version 2.1 of the License, or (at your option) any later version.
 
10
 
 
11
   The GNU C Library is distributed in the hope that it will be useful,
 
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
   Lesser General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU Lesser General Public
 
17
   License along with the GNU C Library; if not, write to the Free
 
18
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA
 
19
   02110-1301 USA.  */
 
20
 
 
21
#include <sysdep.h>
 
22
#include <bp-sym.h>
 
23
#include <bp-asm.h>
 
24
 
 
25
/* int [r3] strchr (char *s [r3], int c [r4])  */
 
26
        .machine  power7
 
27
ENTRY (BP_SYM(strchr))
 
28
        CALL_MCOUNT 2
 
29
        dcbt    0,r3
 
30
        clrrdi  r8,r3,3       /* Align the address to doubleword boundary.  */
 
31
        cmpdi   cr7,r4,0
 
32
        ld      r12,0(r8)     /* Load doubleword from memory.  */
 
33
        li      r0,0          /* Doubleword with null chars to use
 
34
                                 with cmpb.  */
 
35
 
 
36
        rlwinm  r6,r3,3,26,28 /* Calculate padding.  */
 
37
 
 
38
        beq     cr7,L(null_match)
 
39
 
 
40
        /* Replicate byte to doubleword.  */
 
41
        rlwimi  r4,r4,8,16,23
 
42
        rlwimi  r4,r4,16,0,15
 
43
        insrdi  r4,r4,32,0
 
44
 
 
45
        /* Now r4 has a doubleword of c bytes and r0 has
 
46
           a doubleword of null bytes.  */
 
47
 
 
48
        cmpb    r10,r12,r4     /* Compare each byte against c byte.  */
 
49
        cmpb    r11,r12,r0     /* Compare each byte against null byte.  */
 
50
 
 
51
        /* Move the doublewords left and right to discard the bits that are
 
52
           not part of the string and bring them back as zeros.  */
 
53
 
 
54
        sld     r10,r10,r6
 
55
        sld     r11,r11,r6
 
56
        srd     r10,r10,r6
 
57
        srd     r11,r11,r6
 
58
        or      r5,r10,r11    /* OR the results to speed things up.  */
 
59
        cmpdi   cr7,r5,0      /* If r5 == 0, no c or null bytes
 
60
                                 have been found.  */
 
61
        bne     cr7,L(done)
 
62
 
 
63
        mtcrf   0x01,r8
 
64
 
 
65
        /* Are we now aligned to a doubleword boundary?  If so, skip to
 
66
           the main loop.  Otherwise, go through the alignment code.  */
 
67
 
 
68
        bt      28,L(loop)
 
69
 
 
70
        /* Handle WORD2 of pair.  */
 
71
        ldu     r12,8(r8)
 
72
        cmpb    r10,r12,r4
 
73
        cmpb    r11,r12,r0
 
74
        or      r5,r10,r11
 
75
        cmpdi   cr7,r5,0
 
76
        bne     cr7,L(done)
 
77
        b       L(loop)       /* We branch here (rather than falling through)
 
78
                                 to skip the nops due to heavy alignment
 
79
                                 of the loop below.  */
 
80
 
 
81
        .p2align  5
 
82
L(loop):
 
83
        /* Load two doublewords, compare and merge in a
 
84
           single register for speed.  This is an attempt
 
85
           to speed up the null-checking process for bigger strings.  */
 
86
        ld      r12,8(r8)
 
87
        ldu     r9,16(r8)
 
88
        cmpb    r10,r12,r4
 
89
        cmpb    r11,r12,r0
 
90
        cmpb    r6,r9,r4
 
91
        cmpb    r7,r9,r0
 
92
        or      r12,r10,r11
 
93
        or      r9,r6,r7
 
94
        or      r5,r12,r9
 
95
        cmpdi   cr7,r5,0
 
96
        beq     cr7,L(loop)
 
97
 
 
98
        /* OK, one (or both) of the doublewords contains a c/null byte.  Check
 
99
           the first doubleword and decrement the address in case the first
 
100
           doubleword really contains a c/null byte.  */
 
101
 
 
102
        cmpdi   cr6,r12,0
 
103
        addi    r8,r8,-8
 
104
        bne     cr6,L(done)
 
105
 
 
106
        /* The c/null byte must be in the second doubleword.  Adjust the
 
107
           address again and move the result of cmpb to r10 so we can calculate
 
108
           the pointer.  */
 
109
 
 
110
        mr      r10,r6
 
111
        mr      r11,r7
 
112
        addi    r8,r8,8
 
113
 
 
114
        /* r5 has the output of the cmpb instruction, that is, it contains
 
115
           0xff in the same position as the c/null byte in the original
 
116
           doubleword from the string.  Use that to calculate the pointer.  */
 
117
L(done):
 
118
        cntlzd  r4,r10        /* Count leading zeroes before c matches.  */
 
119
        cntlzd  r0,r11        /* Count leading zeroes before null matches.  */
 
120
        cmpld   cr7,r4,r0
 
121
        bgt     cr7,L(no_match)
 
122
        srdi    r0,r4,3       /* Convert leading zeroes to bytes.  */
 
123
        add     r3,r8,r0      /* Return address of the matching c byte
 
124
                                 or null in case c was not found.  */
 
125
        blr
 
126
 
 
127
        .align  4
 
128
L(no_match):
 
129
        li      r3,0
 
130
        blr
 
131
 
 
132
/* We are here because strchr was called with a null byte.  */
 
133
        .align  4
 
134
L(null_match):
 
135
        /* r0 has a doubleword of null bytes.  */
 
136
 
 
137
        cmpb    r5,r12,r0     /* Compare each byte against null bytes.  */
 
138
 
 
139
        /* Move the doublewords left and right to discard the bits that are
 
140
           not part of the string and bring them back as zeros.  */
 
141
 
 
142
        sld     r5,r5,r6
 
143
        srd     r5,r5,r6
 
144
        cmpdi   cr7,r5,0      /* If r10 == 0, no c or null bytes
 
145
                                 have been found.  */
 
146
        bne     cr7,L(done_null)
 
147
 
 
148
        mtcrf   0x01,r8
 
149
 
 
150
        /* Are we now aligned to a quadword boundary?  If so, skip to
 
151
           the main loop.  Otherwise, go through the alignment code.  */
 
152
 
 
153
        bt      28,L(loop_null)
 
154
 
 
155
        /* Handle WORD2 of pair.  */
 
156
        ldu     r12,8(r8)
 
157
        cmpb    r5,r12,r0
 
158
        cmpdi   cr7,r5,0
 
159
        bne     cr7,L(done_null)
 
160
        b       L(loop_null)  /* We branch here (rather than falling through)
 
161
                                 to skip the nops due to heavy alignment
 
162
                                 of the loop below.  */
 
163
 
 
164
        /* Main loop to look for the end of the string.  Since it's a
 
165
           small loop (< 8 instructions), align it to 32-bytes.  */
 
166
        .p2align  5
 
167
L(loop_null):
 
168
        /* Load two doublewords, compare and merge in a
 
169
           single register for speed.  This is an attempt
 
170
           to speed up the null-checking process for bigger strings.  */
 
171
        ld      r12,8(r8)
 
172
        ldu     r11,16(r8)
 
173
        cmpb    r5,r12,r0
 
174
        cmpb    r10,r11,r0
 
175
        or      r6,r5,r10
 
176
        cmpdi   cr7,r6,0
 
177
        beq     cr7,L(loop_null)
 
178
 
 
179
        /* OK, one (or both) of the doublewords contains a null byte.  Check
 
180
           the first doubleword and decrement the address in case the first
 
181
           doubleword really contains a null byte.  */
 
182
 
 
183
        cmpdi   cr6,r5,0
 
184
        addi    r8,r8,-8
 
185
        bne     cr6,L(done_null)
 
186
 
 
187
        /* The null byte must be in the second doubleword.  Adjust the address
 
188
           again and move the result of cmpb to r10 so we can calculate the
 
189
           pointer.  */
 
190
 
 
191
        mr      r5,r10
 
192
        addi    r8,r8,8
 
193
 
 
194
        /* r5 has the output of the cmpb instruction, that is, it contains
 
195
           0xff in the same position as the null byte in the original
 
196
           doubleword from the string.  Use that to calculate the pointer.  */
 
197
L(done_null):
 
198
        cntlzd  r0,r5         /* Count leading zeros before the match.  */
 
199
        srdi    r0,r0,3       /* Convert leading zeros to bytes.  */
 
200
        add     r3,r8,r0      /* Return address of the matching null byte.  */
 
201
        blr
 
202
END (BP_SYM (strchr))
 
203
weak_alias (BP_SYM (strchr), BP_SYM (index))
 
204
libc_hidden_builtin_def (strchr)