1 /* Optimized strnlen implementation for POWER8 using a vmx loop.
3 Copyright (C) 2017-2019 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13 You should have received a copy of the GNU Lesser General Public
14 License along with the GNU C Library; if not, see
15 <http://www.gnu.org/licenses/>. */
17 /* It is implemented the following heuristic:
18 1. Case maxlen <= 32: align the pointer to 8 bytes to loop through
19 reading doublewords. Uses the POWER7 algorithm.
20 2. Case maxlen > 32: check for null bytes in the first 16 bytes using
21 unaligned accesses. Return length if found. Otherwise:
22 2.1 Case maxlen < 64: deduct the bytes previously read, align
23 the pointer to 16 bytes and loop through reading quadwords
24 until find null bytes or reach maxlen.
25 2.2 Case maxlen > 64: deduct the bytes previously read, align
26 the pointer to 64 bytes and set up a counter to loop through
27 reading in strides of 64 bytes. In case it finished the loop
28 with null bytes not found, process the remainder bytes by
29 switching to the loop to heuristic in 2.1. */
33 /* Define default page size to 4KB. */
34 #define PAGE_SIZE 4096
37 /* int [r3] strnlen (char *s [r3], size_t maxlen [r4]) */
39 ENTRY_TOCLESS (__strnlen)
43 cmpldi r4,32 /* Check if maxlen <= 32. */
44 ble L(small_range) /* If maxlen <= 32. */
46 /* Upcoming 16 bytes unaligned accesses cannot cross the page boundary
47 otherwise the processor throws an memory access error.
48 Use following code to check there is room for such as accesses:
49 (((size_t) s) % PAGE_SIZE > (PAGE_SIZE - 16)
50 If it is disallowed then switch to the code that handles
51 the string when maxlen <= 32. */
53 cmpldi cr7,r10,PAGE_SIZE-16
54 bgt cr7,L(small_range) /* If less than 16B of page end. */
56 /* Compute our permute constant r8. */
58 /* Compute a bpermd constant to move bit 0 of each word into
59 a halfword value, and count trailing zeros. */
60 #ifdef __LITTLE_ENDIAN__
74 /* maxlen > 32. Optimistically check for null bytes in the first
75 16 bytes of the string using unaligned accesses. */
78 cmpb r10,r7,r5 /* Check for null bytes in DWORD1. */
79 cmpb r11,r7,r6 /* Check for null bytes in DWORD2. */
81 bne cr0, L(early_find) /* If found null bytes. */
83 /* At this point maxlen > 32 and null bytes were not found at first
84 16 bytes. Prepare for loop using VMX. */
86 /* r3 == s, r4 == maxlen. All other volatile regs are unused now. */
88 addi r5,r3,16 /* Align up, or just add the 16B we
91 and r7,r5,r0 /* Find offset into 16B alignment. */
92 andc r5,r5,r0 /* Quadword align up s to the next quadword. */
95 subf r4,r0,r4 /* Deduct unaligned bytes from maxlen. */
98 /* Compute offsets for vmx loads, and precompute the vbpermq
99 constants for both the 64B and 16B loops. */
106 cmpldi r4,64 /* Check maxlen < 64. */
107 blt L(smaller) /* If maxlen < 64 */
109 /* In order to begin the 64B loop, it needs to be 64
110 bytes aligned. So read quadwords until it is aligned or found null
111 bytes. At worst case it will be aligned after the fourth iteration,
112 so unroll the loop to avoid counter checking. */
113 andi. r7,r5,63 /* Check if is 64 bytes aligned. */
114 beq cr0,L(preloop_64B) /* If it is already 64B aligned. */
118 addi r4,r4,-16 /* Decrement maxlen in 16 bytes. */
119 bne cr6,L(found_aligning64B) /* If found null bytes. */
121 /* Unroll 2x above code block until aligned or find null bytes. */
123 beq cr0,L(preloop_64B)
128 bne cr6,L(found_aligning64B)
131 beq cr0,L(preloop_64B)
136 bne cr6,L(found_aligning64B)
138 /* At this point it should be 16 bytes aligned.
139 Prepare for the 64B loop. */
142 /* Check if maxlen became is less than 64, therefore disallowing the
143 64B loop. If it happened switch to the 16B loop code. */
144 cmpldi r4,64 /* Check if maxlen < 64. */
145 blt L(smaller) /* If maxlen < 64. */
146 /* Set some constant values. */
151 /* Compute the number of 64 bytes iterations needed. */
152 srdi r11,r4,6 /* Compute loop count (maxlen / 64). */
153 andi. r4,r4,63 /* Set maxlen the remainder (maxlen % 64). */
154 mtctr r11 /* Move loop count to counter register. */
156 /* Handle maxlen > 64. Loop over the bytes in strides of 64B. */
159 lvx v1,r5,r6 /* r5 is the pointer to s. */
163 /* Compare the four 16B vectors to obtain the least 16 values.
164 Null bytes should emerge into v7, then check for null bytes. */
168 vcmpequb. v7,v7,v0 /* Check for null bytes. */
169 addi r5,r5,64 /* Add pointer to next iteraction. */
170 bne cr6,L(found_64B) /* If found null bytes. */
171 bdnz L(loop_64B) /* Continue the loop if count > 0. */
173 /* Hit loop end without null match. So branch to handle the remainder. */
175 /* Prepare a 16B loop to handle two cases:
176 1. If 32 > maxlen < 64.
177 2. If maxlen >= 64, and reached end of the 64B loop with null
178 bytes not found. Thus handle the remainder bytes here. */
181 cmpldi r4,0 /* Check maxlen is zero. */
182 beq L(done) /* If maxlen is zero. */
184 /* Place rounded up number of qw's to check into a vmx
185 register, and use some vector tricks to minimize
187 mtvrd v7,r4 /* copy maxlen from gpr to vector register. */
193 #ifdef __LITTLE_ENDIAN__
194 vspltish v5,1 /* Compute 16 in each byte. */
197 /* Loop in 16B aligned incremements now. */
200 lvx v1,r5,r6 /* Load quadword into vector register. */
201 addi r5,r5,16 /* Increment address to next 16B block. */
202 vor v7,v2,v2 /* Save loop count (v2) into v7. */
203 vsububs v2,v2,v3 /* Subtract 16B from count, saturate at 0. */
205 vcmpequb. v4,v4,v0 /* Checking for null bytes. */
206 beq cr6,L(loop_16B) /* If null bytes not found. */
210 #ifdef __LITTLE_ENDIAN__
211 vsubuhm v2,v1,v5 /* Form a mask of trailing zeros. */
213 vpopcnth v1,v2 /* count of trailing zeros, 16 if none. */
215 vclzh v1,v1 /* count the leading zeros, 16 if none. */
217 /* Truncate to maximum allowable offset. */
218 vcmpgtub v2,v1,v7 /* Compare and truncate for matches beyond
220 vsel v1,v1,v7,v2 /* 0-16 is now in byte 7. */
223 addi r5,r5,-16 /* Undo speculative bump. */
224 extsb r0,r0 /* Clear whatever gunk is in the high 56b. */
225 add r5,r5,r0 /* Add the offset of whatever was found. */
227 subf r3,r3,r5 /* Length is equal to the offset of null byte
228 matched minus the pointer to s. */
231 /* Handle case of maxlen > 64 and found null bytes in last block
235 /* A zero was found. Reduce the result. */
241 /* Permute the first bit of each byte into bits 48-63. */
247 /* Shift each component into its correct position for merging. */
248 #ifdef __LITTLE_ENDIAN__
258 /* Merge the results and move to a GPR. */
263 /* Adjust address to the start of the current 64B block. */
267 #ifdef __LITTLE_ENDIAN__
268 addi r9,r10,-1 /* Form a mask from trailing zeros. */
270 popcntd r0,r9 /* Count the bits in the mask. */
272 cntlzd r0,r10 /* Count leading zeros before the match. */
275 add r3,r5,r0 /* Compute final length. */
278 /* Handle case where null bytes were found while aligning
279 as a preparation for the 64B loop. */
281 L(found_aligning64B):
283 #ifdef __LITTLE_ENDIAN__
285 addi r9,r10,-1 /* Form a mask from trailing zeros. */
287 popcntd r0,r9 /* Count the bits in the mask. */
291 cntlzd r0,r10 /* Count leading zeros before the match. */
293 addi r5,r5,-16 /* Adjust address to offset of last 16 bytes
295 /* Calculate length as subtracted the pointer to s of last 16 bytes
296 offset, added with the bytes before the match. */
301 /* Handle case of maxlen > 32 and found a null bytes within the first
305 bpermd r5,r8,r10 /* r8 contains the bit permute constants. */
308 or r5,r5,r6 /* r5 should hold a 16B mask of
310 cntlzd r5,r5 /* Count leading zeros. */
311 addi r3,r5,-48 /* Deduct the 48 leading zeros always
315 /* Handle case of maxlen <= 32. Use the POWER7 algorithm. */
318 clrrdi r8,r3,3 /* Align the pointer to 8B. */
320 /* Register's content at this point:
321 r3 == pointer to s, r4 == maxlen, r8 == pointer to s aligned to 8B,
322 r7 == last acceptable address. */
323 cmpldi r4,0 /* Check if maxlen is zero. */
324 beq L(end_max) /* If maxlen is zero. */
326 /* Calculate the last acceptable address and check for possible
327 addition overflow by using satured math:
337 clrrdi r7,r7,3 /* Align to 8B address of last
338 acceptable address. */
340 rlwinm r6,r3,3,26,28 /* Calculate padding. */
341 ld r12,0(r8) /* Load aligned doubleword. */
342 cmpb r10,r12,r0 /* Check for null bytes. */
343 #ifdef __LITTLE_ENDIAN__
349 #endif /* __LITTLE_ENDIAN__ */
351 bne cr7,L(done_small) /* If found null byte. */
353 cmpld r8,r7 /* Check if reached maxlen. */
354 beq L(end_max) /* If reached maxlen. */
356 /* Still handling case of maxlen <= 32. Read doubleword aligned until
357 find null bytes or reach maxlen. */
360 ldu r12,8(r8) /* Load next doubleword and update r8. */
361 cmpb r10,r12,r0 /* Check for null bytes. */
363 bne cr6,L(done_small) /* If found null bytes. */
364 cmpld r8,r7 /* Check if reached maxlen. */
365 bne L(loop_small) /* If it has more bytes to read. */
366 mr r3,r4 /* Reached maxlen with null bytes not found.
367 Length is equal to maxlen. */
370 /* Still handling case of maxlen <= 32. Found null bytes.
371 Registers: r10 == match bits within doubleword, r8 == address of
372 last doubleword read, r3 == pointer to s, r4 == maxlen. */
375 #ifdef __LITTLE_ENDIAN__
376 /* Count trailing zeros. */
381 cntlzd r0,r10 /* Count leading zeros before the match. */
383 sub r3,r8,r3 /* Calculate total of bytes before the match. */
384 srdi r0,r0,3 /* Convert leading/trailing zeros to bytes. */
385 add r3,r3,r0 /* Length until the match. */
386 cmpld r3,r4 /* Check length is greater than maxlen. */
388 mr r3,r4 /* If length is greater than maxlen, return
392 /* Handle case of reached maxlen with null bytes not found. */
395 mr r3,r4 /* Length is equal to maxlen. */
400 libc_hidden_def (__strnlen)
401 weak_alias (__strnlen, strnlen)
402 libc_hidden_def (strnlen)