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