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