1 /* Optimized strnlen implementation for PowerPC32/POWER7 using cmpb insn.
2 Copyright (C) 2010 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/>. */
24 /* int [r3] strnlen (char *s [r3], int size [r4]) */
26 ENTRY (BP_SYM (__strnlen))
29 clrrwi r8,r3,2 /* Align the address to word boundary. */
30 add r7,r3,r4 /* Calculate the last acceptable address. */
32 li r0,0 /* Word with null chars. */
35 cmplw cr7,r3,r7 /* Is the address equal or less than r3? If
36 it's equal or less, it means size is either 0
37 or a negative number. */
40 li r7,-1 /* Make r11 the biggest if r4 <= 0. */
42 rlwinm r6,r3,3,27,28 /* Calculate padding. */
43 lwz r12,0(r8) /* Load word from memory. */
44 cmpb r10,r12,r0 /* Check for null bytes in DWORD1. */
47 cmplwi cr7,r10,0 /* If r10 == 0, no null's have been found. */
50 /* Are we done already? */
56 /* Are we now aligned to a doubleword boundary? If so, skip to
57 the main loop. Otherwise, go through the alignment code. */
61 /* Handle DWORD2 of pair. */
67 /* Are we done already? */
74 srwi r6,r5,3 /* Number of loop iterations. */
75 mtctr r6 /* Setup the counter. */
77 /* Main loop to look for the null byte backwards in the string. Since
78 it's a small loop (< 8 instructions), align it to 32-bytes. */
81 /* Load two words, 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. */
89 or r5,r9,r10 /* Merge everything in one word. */
93 /* We're here because the counter reached 0, and that means we
94 didn't have any matches for null in the whole range. Just return
104 /* OK, one (or both) of the words contains a null byte. Check
105 the first word and decrement the address in case the first
106 word really contains a null byte. */
113 /* The null byte must be in the second word. Adjust the address
114 again and move the result of cmpb to r10 so we can calculate the
120 /* r10 has the output of the cmpb instruction, that is, it contains
121 0xff in the same position as the null byte in the original
122 word from the string. Use that to calculate the length.
123 We need to make sure the null char is *before* the end of the
126 cntlzw r0,r10 /* Count leading zeroes before the match. */
127 srwi r0,r0,3 /* Convert leading zeroes to bytes. */
129 sub r6,r9,r3 /* Length until the match. */
140 /* Deals with size <= 32. */
146 rlwinm r6,r3,3,27,28 /* Calculate padding. */
147 lwz r12,0(r8) /* Load word from memory. */
148 cmpb r10,r12,r0 /* Check for null bytes in WORD1. */
169 END (BP_SYM (__strnlen))
170 weak_alias (BP_SYM (__strnlen), BP_SYM(strnlen))
171 libc_hidden_builtin_def (strnlen)