1 /* Optimized strnlen implementation for PowerPC64/POWER7 using cmpb insn.
2 Copyright (C) 2010-2016 Free Software Foundation, Inc.
3 Contributed by Luis Machado <luisgpm@br.ibm.com>.
4 This file is part of the GNU C Library.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
22 /* int [r3] strnlen (char *s [r3], int size [r4]) */
28 add r7,r3,r4 /* Calculate the last acceptable address. */
30 li r0,0 /* Doubleword with null chars. */
33 /* If we have less than 33 bytes to search, skip to a faster code. */
36 rlwinm r6,r3,3,26,28 /* Calculate padding. */
37 ld r12,0(r8) /* Load doubleword from memory. */
38 cmpb r10,r12,r0 /* Check for null bytes in DWORD1. */
39 #ifdef __LITTLE_ENDIAN__
46 cmpldi cr7,r10,0 /* If r10 == 0, no null's have been found. */
49 clrrdi r7,r7,3 /* Address of last doubleword. */
51 /* Are we now aligned to a quadword boundary? If so, skip to
52 the main loop. Otherwise, go through the alignment code. */
56 /* Handle DWORD2 of pair. */
63 /* The last dword we want to read in the loop below is the one
64 containing the last byte of the string, ie. the dword at
65 (s + size - 1) & ~7, or r7. The first dword read is at
66 r8 + 8, we read 2 * cnt dwords, so the last dword read will
67 be at r8 + 8 + 16 * cnt - 8. Solving for cnt gives
68 cnt = (r7 - r8) / 16 */
70 srdi r6,r5,4 /* Number of loop iterations. */
71 mtctr r6 /* Setup the counter. */
73 /* Main loop to look for the null byte in the string. Since
74 it's a small loop (< 8 instructions), align it to 32-bytes. */
77 /* Load two doublewords, compare and merge in a
78 single register for speed. This is an attempt
79 to speed up the null-checking process for bigger strings. */
85 or r5,r9,r10 /* Merge everything in one doubleword. */
90 /* We may have one more dword to read. */
103 /* OK, one (or both) of the doublewords contains a null byte. Check
104 the first doubleword and decrement the address in case the first
105 doubleword really contains a null byte. */
112 /* The null byte must be in the second doubleword. Adjust the address
113 again and move the result of cmpb to r10 so we can calculate the
119 /* r10 has the output of the cmpb instruction, that is, it contains
120 0xff in the same position as the null byte in the original
121 doubleword from the string. Use that to calculate the length.
122 We need to make sure the null char is *before* the end of the
125 #ifdef __LITTLE_ENDIAN__
130 cntlzd r0,r10 /* Count leading zeros before the match. */
133 srdi r0,r0,3 /* Convert leading/trailing zeros to bytes. */
134 add r3,r3,r0 /* Length until the match. */
140 /* Deals with size <= 32. */
146 clrrdi r7,r7,3 /* Address of last doubleword. */
148 rlwinm r6,r3,3,26,28 /* Calculate padding. */
149 ld r12,0(r8) /* Load doubleword from memory. */
150 cmpb r10,r12,r0 /* Check for null bytes in DWORD1. */
151 #ifdef __LITTLE_ENDIAN__
176 libc_hidden_def (__strnlen)
177 weak_alias (__strnlen, strnlen)
178 libc_hidden_def (strnlen)