]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/powerpc/powerpc32/power7/memchr.S
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / powerpc / powerpc32 / power7 / memchr.S
1 /* Optimized memchr implementation for PowerPC32/POWER7 using cmpb insn.
2 Copyright (C) 2010-2016 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, see
18 <http://www.gnu.org/licenses/>. */
19
20 #include <sysdep.h>
21
22 /* int [r3] memchr (char *s [r3], int byte [r4], int size [r5]) */
23 .machine power7
24 ENTRY (__memchr)
25 CALL_MCOUNT
26 dcbt 0,r3
27 clrrwi r8,r3,2
28 insrwi r4,r4,8,16 /* Replicate byte to word. */
29 add r7,r3,r5 /* Calculate the last acceptable address. */
30 insrwi r4,r4,16,0
31 cmplwi r5,16
32 li r9, -1
33 rlwinm r6,r3,3,27,28 /* Calculate padding. */
34 addi r7,r7,-1
35 #ifdef __LITTLE_ENDIAN__
36 slw r9,r9,r6
37 #else
38 srw r9,r9,r6
39 #endif
40 ble L(small_range)
41
42 lwz r12,0(r8) /* Load word from memory. */
43 cmpb r3,r12,r4 /* Check for BYTEs in WORD1. */
44 and r3,r3,r9
45 clrlwi r5,r7,30 /* Byte count - 1 in last word. */
46 clrrwi r7,r7,2 /* Address of last word. */
47 cmplwi cr7,r3,0 /* If r3 == 0, no BYTEs have been found. */
48 bne cr7,L(done)
49
50 mtcrf 0x01,r8
51 /* Are we now aligned to a doubleword boundary? If so, skip to
52 the main loop. Otherwise, go through the alignment code. */
53 bt 29,L(loop_setup)
54
55 /* Handle WORD2 of pair. */
56 lwzu r12,4(r8)
57 cmpb r3,r12,r4
58 cmplwi cr7,r3,0
59 bne cr7,L(done)
60
61 L(loop_setup):
62 /* The last word we want to read in the loop below is the one
63 containing the last byte of the string, ie. the word at
64 (s + size - 1) & ~3, or r7. The first word read is at
65 r8 + 4, we read 2 * cnt words, so the last word read will
66 be at r8 + 4 + 8 * cnt - 4. Solving for cnt gives
67 cnt = (r7 - r8) / 8 */
68 sub r6,r7,r8
69 srwi r6,r6,3 /* Number of loop iterations. */
70 mtctr r6 /* Setup the counter. */
71
72 /* Main loop to look for BYTE in the string. Since
73 it's a small loop (8 instructions), align it to 32-bytes. */
74 .align 5
75 L(loop):
76 /* Load two words, compare and merge in a
77 single register for speed. This is an attempt
78 to speed up the byte-checking process for bigger strings. */
79 lwz r12,4(r8)
80 lwzu r11,8(r8)
81 cmpb r3,r12,r4
82 cmpb r9,r11,r4
83 or r6,r9,r3 /* Merge everything in one word. */
84 cmplwi cr7,r6,0
85 bne cr7,L(found)
86 bdnz L(loop)
87
88 /* We may have one more dword to read. */
89 cmplw r8,r7
90 beqlr
91
92 lwzu r12,4(r8)
93 cmpb r3,r12,r4
94 cmplwi cr6,r3,0
95 bne cr6,L(done)
96 blr
97
98 .align 4
99 L(found):
100 /* OK, one (or both) of the words contains BYTE. Check
101 the first word and decrement the address in case the first
102 word really contains BYTE. */
103 cmplwi cr6,r3,0
104 addi r8,r8,-4
105 bne cr6,L(done)
106
107 /* BYTE must be in the second word. Adjust the address
108 again and move the result of cmpb to r3 so we can calculate the
109 pointer. */
110
111 mr r3,r9
112 addi r8,r8,4
113
114 /* r3 has the output of the cmpb instruction, that is, it contains
115 0xff in the same position as BYTE in the original
116 word from the string. Use that to calculate the pointer.
117 We need to make sure BYTE is *before* the end of the range. */
118 L(done):
119 #ifdef __LITTLE_ENDIAN__
120 addi r0,r3,-1
121 andc r0,r0,r3
122 popcntw r0,r0 /* Count trailing zeros. */
123 #else
124 cntlzw r0,r3 /* Count leading zeros before the match. */
125 #endif
126 cmplw r8,r7 /* Are we on the last word? */
127 srwi r0,r0,3 /* Convert leading/trailing zeros to bytes. */
128 add r3,r8,r0
129 cmplw cr7,r0,r5 /* If on the last dword, check byte offset. */
130 bnelr
131 blelr cr7
132 li r3,0
133 blr
134
135 .align 4
136 L(null):
137 li r3,0
138 blr
139
140 /* Deals with size <= 16. */
141 .align 4
142 L(small_range):
143 cmplwi r5,0
144 beq L(null)
145 lwz r12,0(r8) /* Load word from memory. */
146 cmpb r3,r12,r4 /* Check for BYTE in DWORD1. */
147 and r3,r3,r9
148 cmplwi cr7,r3,0
149 clrlwi r5,r7,30 /* Byte count - 1 in last word. */
150 clrrwi r7,r7,2 /* Address of last word. */
151 cmplw r8,r7 /* Are we done already? */
152 bne cr7,L(done)
153 beqlr
154
155 lwzu r12,4(r8)
156 cmpb r3,r12,r4
157 cmplwi cr6,r3,0
158 cmplw r8,r7
159 bne cr6,L(done)
160 beqlr
161
162 lwzu r12,4(r8)
163 cmpb r3,r12,r4
164 cmplwi cr6,r3,0
165 cmplw r8,r7
166 bne cr6,L(done)
167 beqlr
168
169 lwzu r12,4(r8)
170 cmpb r3,r12,r4
171 cmplwi cr6,r3,0
172 cmplw r8,r7
173 bne cr6,L(done)
174 beqlr
175
176 lwzu r12,4(r8)
177 cmpb r3,r12,r4
178 cmplwi cr6,r3,0
179 bne cr6,L(done)
180 blr
181
182 END (__memchr)
183 weak_alias (__memchr, memchr)
184 libc_hidden_builtin_def (memchr)