]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/powerpc/powerpc32/power7/memchr.S
powerpc: Various P7-optimized string functions
[thirdparty/glibc.git] / sysdeps / powerpc / powerpc32 / power7 / memchr.S
1 /* Optimized memchr implementation for PowerPC32/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] memchr (char *s [r3], int byte [r4], int size [r5]) */
26 .machine power7
27 ENTRY (BP_SYM (__memchr))
28 CALL_MCOUNT
29 dcbt 0,r3
30 clrrwi r8,r3,2
31 rlwimi r4,r4,8,16,23
32 rlwimi r4,r4,16,0,15
33 add r7,r3,r5 /* Calculate the last acceptable address. */
34 cmplwi r5,16
35 ble L(small_range)
36
37 cmplw cr7,r3,r7 /* Is the address equal or less than r3? If
38 it's equal or less, it means size is either 0
39 or a negative number. */
40 ble cr7,L(proceed)
41
42 li r7,-1 /* Make r11 the biggest if r4 <= 0. */
43 L(proceed):
44 rlwinm r6,r3,3,27,28 /* Calculate padding. */
45 lwz r12,0(r8) /* Load word from memory. */
46 cmpb r10,r12,r4 /* Check for BYTE's in WORD1. */
47 slw r10,r10,r6
48 srw r10,r10,r6
49 cmplwi cr7,r10,0 /* If r10 == 0, no BYTE's have been found. */
50 bne cr7,L(done)
51
52 /* Are we done already? */
53 addi r9,r8,4
54 cmplw cr6,r9,r7
55 bge cr6,L(null)
56
57 mtcrf 0x01,r8
58 /* Are we now aligned to a doubleword boundary? If so, skip to
59 the main loop. Otherwise, go through the alignment code. */
60
61 bt 29,L(loop_setup)
62
63 /* Handle WORD2 of pair. */
64 lwzu r12,4(r8)
65 cmpb r10,r12,r4
66 cmplwi cr7,r10,0
67 bne cr7,L(done)
68
69 /* Are we done already? */
70 addi r9,r8,4
71 cmplw cr6,r9,r7
72 bge cr6,L(null)
73
74 L(loop_setup):
75 sub r5,r7,r9
76 srwi r6,r5,3 /* Number of loop iterations. */
77 mtctr r6 /* Setup the counter. */
78 b L(loop)
79 /* Main loop to look for BYTE backwards in the string. Since
80 it's a small loop (< 8 instructions), align it to 32-bytes. */
81 .p2align 5
82 L(loop):
83 /* Load two words, compare and merge in a
84 single register for speed. This is an attempt
85 to speed up the byte-checking process for bigger strings. */
86
87 lwz r12,4(r8)
88 lwzu r11,8(r8)
89 cmpb r10,r12,r4
90 cmpb r9,r11,r4
91 or r5,r9,r10 /* Merge everything in one word. */
92 cmplwi cr7,r5,0
93 bne cr7,L(found)
94 bdnz L(loop)
95 /* We're here because the counter reached 0, and that means we
96 didn't have any matches for BYTE in the whole range. Just
97 return the original range. */
98 addi r9,r8,4
99 cmplw cr6,r9,r7
100 blt cr6,L(loop_small)
101 b L(null)
102
103 /* OK, one (or both) of the words contains BYTE. Check
104 the first word and decrement the address in case the first
105 word really contains BYTE. */
106 .align 4
107 L(found):
108 cmplwi cr6,r10,0
109 addi r8,r8,-4
110 bne cr6,L(done)
111
112 /* BYTE must be in the second word. Adjust the address
113 again and move the result of cmpb to r10 so we can calculate the
114 pointer. */
115
116 mr r10,r9
117 addi r8,r8,4
118
119 /* r10 has the output of the cmpb instruction, that is, it contains
120 0xff in the same position as BYTE in the original
121 word from the string. Use that to calculate the pointer.
122 We need to make sure BYTE is *before* the end of the
123 range. */
124 L(done):
125 cntlzw r0,r10 /* Count leading zeroes before the match. */
126 srwi r0,r0,3 /* Convert leading zeroes to bytes. */
127 add r3,r8,r0
128 cmplw r3,r7
129 bge L(null)
130 blr
131
132 .align 4
133 L(null):
134 li r3,0
135 blr
136
137 /* Deals with size <= 16. */
138 .align 4
139 L(small_range):
140 cmplwi r5,0
141 beq L(null)
142
143 rlwinm r6,r3,3,27,28 /* Calculate padding. */
144 lwz r12,0(r8) /* Load word from memory. */
145 cmpb r10,r12,r4 /* Check for BYTE in DWORD1. */
146 slw r10,r10,r6
147 srw r10,r10,r6
148 cmplwi cr7,r10,0
149 bne cr7,L(done)
150
151 /* Are we done already? */
152 addi r9,r8,4
153 cmplw r9,r7
154 bge L(null)
155 b L(loop_small)
156
157 .p2align 5
158 L(loop_small):
159 lwzu r12,4(r8)
160 cmpb r10,r12,r4
161 addi r9,r8,4
162 cmplwi cr6,r10,0
163 bne cr6,L(done)
164 cmplw r9,r7
165 bge L(null)
166 b L(loop_small)
167
168 END (BP_SYM (__memchr))
169 weak_alias (BP_SYM (__memchr), BP_SYM(memchr))
170 libc_hidden_builtin_def (memchr)