]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/powerpc/powerpc64/power7/strnlen.S
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / powerpc / powerpc64 / power7 / strnlen.S
CommitLineData
fe2f79db 1/* Optimized strnlen implementation for PowerPC64/POWER7 using cmpb insn.
2b778ceb 2 Copyright (C) 2010-2021 Free Software Foundation, Inc.
fe2f79db
LM
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
59ba27a6 17 License along with the GNU C Library; if not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
fe2f79db
LM
19
20#include <sysdep.h>
fe2f79db 21
001649fd
WSM
22#ifndef STRNLEN
23# define STRNLEN __strnlen
24#endif
25
fe2f79db
LM
26/* int [r3] strnlen (char *s [r3], int size [r4]) */
27 .machine power7
d5b41185 28ENTRY_TOCLESS (STRNLEN)
fe2f79db
LM
29 CALL_MCOUNT 2
30 dcbt 0,r3
33ee81de 31 clrrdi r8,r3,3
fe2f79db
LM
32 add r7,r3,r4 /* Calculate the last acceptable address. */
33 cmpldi r4,32
34 li r0,0 /* Doubleword with null chars. */
33ee81de
AM
35 addi r7,r7,-1
36
fe2f79db
LM
37 /* If we have less than 33 bytes to search, skip to a faster code. */
38 ble L(small_range)
39
fe2f79db
LM
40 rlwinm r6,r3,3,26,28 /* Calculate padding. */
41 ld r12,0(r8) /* Load doubleword from memory. */
42 cmpb r10,r12,r0 /* Check for null bytes in DWORD1. */
33ee81de
AM
43#ifdef __LITTLE_ENDIAN__
44 srd r10,r10,r6
45 sld r10,r10,r6
46#else
fe2f79db
LM
47 sld r10,r10,r6
48 srd r10,r10,r6
33ee81de 49#endif
fe2f79db
LM
50 cmpldi cr7,r10,0 /* If r10 == 0, no null's have been found. */
51 bne cr7,L(done)
52
33ee81de 53 clrrdi r7,r7,3 /* Address of last doubleword. */
fe2f79db
LM
54 mtcrf 0x01,r8
55 /* Are we now aligned to a quadword boundary? If so, skip to
56 the main loop. Otherwise, go through the alignment code. */
57
58 bt 28,L(loop_setup)
59
60 /* Handle DWORD2 of pair. */
61 ldu r12,8(r8)
62 cmpb r10,r12,r0
63 cmpldi cr7,r10,0
64 bne cr7,L(done)
65
fe2f79db 66L(loop_setup):
33ee81de
AM
67 /* The last dword we want to read in the loop below is the one
68 containing the last byte of the string, ie. the dword at
69 (s + size - 1) & ~7, or r7. The first dword read is at
70 r8 + 8, we read 2 * cnt dwords, so the last dword read will
71 be at r8 + 8 + 16 * cnt - 8. Solving for cnt gives
72 cnt = (r7 - r8) / 16 */
73 sub r5,r7,r8
fe2f79db
LM
74 srdi r6,r5,4 /* Number of loop iterations. */
75 mtctr r6 /* Setup the counter. */
33ee81de
AM
76
77 /* Main loop to look for the null byte in the string. Since
fe2f79db
LM
78 it's a small loop (< 8 instructions), align it to 32-bytes. */
79 .p2align 5
80L(loop):
81 /* Load two doublewords, compare and merge in a
82 single register for speed. This is an attempt
83 to speed up the null-checking process for bigger strings. */
84
85 ld r12,8(r8)
86 ldu r11,16(r8)
87 cmpb r10,r12,r0
88 cmpb r9,r11,r0
89 or r5,r9,r10 /* Merge everything in one doubleword. */
90 cmpldi cr7,r5,0
91 bne cr7,L(found)
92 bdnz L(loop)
33ee81de
AM
93
94 /* We may have one more dword to read. */
95 cmpld cr6,r8,r7
96 beq cr6,L(end_max)
97
98 ldu r12,8(r8)
99 cmpb r10,r12,r0
100 cmpldi cr6,r10,0
101 bne cr6,L(done)
fe2f79db
LM
102
103L(end_max):
33ee81de 104 mr r3,r4
fe2f79db
LM
105 blr
106
107 /* OK, one (or both) of the doublewords contains a null byte. Check
108 the first doubleword and decrement the address in case the first
109 doubleword really contains a null byte. */
110 .align 4
111L(found):
112 cmpldi cr6,r10,0
113 addi r8,r8,-8
114 bne cr6,L(done)
115
116 /* The null byte must be in the second doubleword. Adjust the address
117 again and move the result of cmpb to r10 so we can calculate the
118 length. */
119
120 mr r10,r9
121 addi r8,r8,8
122
123 /* r10 has the output of the cmpb instruction, that is, it contains
124 0xff in the same position as the null byte in the original
125 doubleword from the string. Use that to calculate the length.
33ee81de
AM
126 We need to make sure the null char is *before* the end of the
127 range. */
fe2f79db 128L(done):
33ee81de
AM
129#ifdef __LITTLE_ENDIAN__
130 addi r0,r10,-1
131 andc r0,r0,r10
132 popcntd r0,r0
133#else
134 cntlzd r0,r10 /* Count leading zeros before the match. */
135#endif
136 sub r3,r8,r3
137 srdi r0,r0,3 /* Convert leading/trailing zeros to bytes. */
138 add r3,r3,r0 /* Length until the match. */
139 cmpld r3,r4
140 blelr
141 mr r3,r4
fe2f79db
LM
142 blr
143
144/* Deals with size <= 32. */
145 .align 4
146L(small_range):
147 cmpldi r4,0
33ee81de
AM
148 beq L(end_max)
149
150 clrrdi r7,r7,3 /* Address of last doubleword. */
fe2f79db
LM
151
152 rlwinm r6,r3,3,26,28 /* Calculate padding. */
33ee81de 153 ld r12,0(r8) /* Load doubleword from memory. */
fe2f79db 154 cmpb r10,r12,r0 /* Check for null bytes in DWORD1. */
33ee81de
AM
155#ifdef __LITTLE_ENDIAN__
156 srd r10,r10,r6
157 sld r10,r10,r6
158#else
fe2f79db
LM
159 sld r10,r10,r6
160 srd r10,r10,r6
33ee81de 161#endif
fe2f79db
LM
162 cmpldi cr7,r10,0
163 bne cr7,L(done)
164
33ee81de
AM
165 cmpld r8,r7
166 beq L(end_max)
fe2f79db
LM
167
168 .p2align 5
169L(loop_small):
170 ldu r12,8(r8)
171 cmpb r10,r12,r0
fe2f79db
LM
172 cmpldi cr6,r10,0
173 bne cr6,L(done)
33ee81de
AM
174 cmpld r8,r7
175 bne L(loop_small)
176 mr r3,r4
177 blr
178
001649fd 179END (STRNLEN)
14362ef1 180libc_hidden_def (__strnlen)
2d67d91a 181weak_alias (__strnlen, strnlen)
14362ef1 182libc_hidden_def (strnlen)