1 /* Optimized strlen implementation for PowerPC32/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/>. */
21 /* int [r3] strlen (char *s [r3]) */
26 clrrwi r4,r3,2 /* Align the address to word boundary. */
27 rlwinm r6,r3,3,27,28 /* Calculate padding. */
28 li r0,0 /* Word with null chars to use with cmpb. */
29 li r5,-1 /* MASK = 0xffffffffffffffff. */
30 lwz r12,0(r4) /* Load word from memory. */
31 #ifdef __LITTLE_ENDIAN__
34 srw r5,r5,r6 /* MASK = MASK >> padding. */
36 orc r9,r12,r5 /* Mask bits that are not part of the string. */
37 cmpb r10,r9,r0 /* Check for null bytes in WORD1. */
38 cmpwi cr7,r10,0 /* If r10 == 0, no null's have been found. */
43 /* Are we now aligned to a doubleword boundary? If so, skip to
44 the main loop. Otherwise, go through the alignment code. */
48 /* Handle WORD2 of pair. */
54 /* Main loop to look for the end of the string. Since it's a
55 small loop (< 8 instructions), align it to 32-bytes. */
58 /* Load two words, compare and merge in a
59 single register for speed. This is an attempt
60 to speed up the null-checking process for bigger strings. */
66 or r8,r9,r10 /* Merge everything in one word. */
70 /* OK, one (or both) of the words contains a null byte. Check
71 the first word and decrement the address in case the first
72 word really contains a null byte. */
78 /* The null byte must be in the second word. Adjust the address
79 again and move the result of cmpb to r10 so we can calculate the
85 /* r10 has the output of the cmpb instruction, that is, it contains
86 0xff in the same position as the null byte in the original
87 word from the string. Use that to calculate the length. */
89 #ifdef __LITTLE_ENDIAN__
90 addi r9, r10, -1 /* Form a mask from trailing zeros. */
92 popcntw r0, r9 /* Count the bits in the mask. */
94 cntlzw r0,r10 /* Count leading zeros before the match. */
97 srwi r0,r0,3 /* Convert leading zeros to bytes. */
98 add r3,r5,r0 /* Compute final length. */
101 libc_hidden_builtin_def (strlen)