1 /* Optimized strnlen implementation for PowerPC64/POWER7 using cmpb insn.
2 Copyright (C) 2010-2023 Free Software Foundation, Inc.
3 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.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
22 # define STRNLEN __strnlen
25 /* int [r3] strnlen (char *s [r3], int size [r4]) */
27 ENTRY_TOCLESS (STRNLEN)
31 add r7,r3,r4 /* Calculate the last acceptable address. */
33 li r0,0 /* Doubleword with null chars. */
36 /* If we have less than 33 bytes to search, skip to a faster code. */
39 rlwinm r6,r3,3,26,28 /* Calculate padding. */
40 ld r12,0(r8) /* Load doubleword from memory. */
41 cmpb r10,r12,r0 /* Check for null bytes in DWORD1. */
42 #ifdef __LITTLE_ENDIAN__
49 cmpldi cr7,r10,0 /* If r10 == 0, no null's have been found. */
52 clrrdi r7,r7,3 /* Address of last doubleword. */
54 /* Are we now aligned to a quadword boundary? If so, skip to
55 the main loop. Otherwise, go through the alignment code. */
59 /* Handle DWORD2 of pair. */
66 /* The last dword we want to read in the loop below is the one
67 containing the last byte of the string, ie. the dword at
68 (s + size - 1) & ~7, or r7. The first dword read is at
69 r8 + 8, we read 2 * cnt dwords, so the last dword read will
70 be at r8 + 8 + 16 * cnt - 8. Solving for cnt gives
71 cnt = (r7 - r8) / 16 */
73 srdi r6,r5,4 /* Number of loop iterations. */
74 mtctr r6 /* Setup the counter. */
76 /* Main loop to look for the null byte in the string. Since
77 it's a small loop (< 8 instructions), align it to 32-bytes. */
80 /* Load two doublewords, compare and merge in a
81 single register for speed. This is an attempt
82 to speed up the null-checking process for bigger strings. */
88 or r5,r9,r10 /* Merge everything in one doubleword. */
93 /* We may have one more dword to read. */
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. */
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
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 end of the
128 #ifdef __LITTLE_ENDIAN__
133 cntlzd r0,r10 /* Count leading zeros before the match. */
136 srdi r0,r0,3 /* Convert leading/trailing zeros to bytes. */
137 add r3,r3,r0 /* Length until the match. */
143 /* Deals with size <= 32. */
149 clrrdi r7,r7,3 /* Address of last doubleword. */
151 rlwinm r6,r3,3,26,28 /* Calculate padding. */
152 ld r12,0(r8) /* Load doubleword from memory. */
153 cmpb r10,r12,r0 /* Check for null bytes in DWORD1. */
154 #ifdef __LITTLE_ENDIAN__
179 libc_hidden_def (__strnlen)
180 weak_alias (__strnlen, strnlen)
181 libc_hidden_def (strnlen)