1 /* Optimized strnlen implementation for PowerPC64/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))
30 add r7,r3,r4 /* Calculate the last acceptable address. */
32 li r0,0 /* Doubleword with null chars. */
33 /* If we have less than 33 bytes to search, skip to a faster code. */
36 cmpld cr7,r3,r7 /* Is the address equal or less than r3? If
37 it's equal or less, it means size is either 0
38 or a negative number. */
41 li r7,-1 /* Make r11 the biggest if r4 <= 0. */
43 rlwinm r6,r3,3,26,28 /* Calculate padding. */
44 ld r12,0(r8) /* Load doubleword from memory. */
45 cmpb r10,r12,r0 /* Check for null bytes in DWORD1. */
48 cmpldi cr7,r10,0 /* If r10 == 0, no null's have been found. */
51 /* Are we done already? */
57 /* Are we now aligned to a quadword boundary? If so, skip to
58 the main loop. Otherwise, go through the alignment code. */
62 /* Handle DWORD2 of pair. */
68 /* Are we done already? */
75 srdi r6,r5,4 /* Number of loop iterations. */
76 mtctr r6 /* Setup the counter. */
78 /* Main loop to look for the null byte backwards in the string. Since
79 it's a small loop (< 8 instructions), align it to 32-bytes. */
82 /* Load two doublewords, compare and merge in a
83 single register for speed. This is an attempt
84 to speed up the null-checking process for bigger strings. */
90 or r5,r9,r10 /* Merge everything in one doubleword. */
94 /* We're here because the counter reached 0, and that means we
95 didn't have any matches for null in the whole range. Just return
105 /* OK, one (or both) of the doublewords contains a null byte. Check
106 the first doubleword and decrement the address in case the first
107 doubleword really contains a null byte. */
114 /* The null byte must be in the second doubleword. Adjust the address
115 again and move the result of cmpb to r10 so we can calculate the
121 /* r10 has the output of the cmpb instruction, that is, it contains
122 0xff in the same position as the null byte in the original
123 doubleword from the string. Use that to calculate the length.
124 We need to make sure the null char is *before* the start of the
125 range (since we're going backwards). */
127 cntlzd r0,r10 /* Count leading zeroes before the match. */
128 srdi r0,r0,3 /* Convert leading zeroes to bytes. */
130 sub r6,r9,r3 /* Length until the match. */
141 /* Deals with size <= 32. */
147 rlwinm r6,r3,3,26,28 /* Calculate padding. */
148 ld r12,0(r8) /* Load word from memory. */
149 cmpb r10,r12,r0 /* Check for null bytes in DWORD1. */
170 END (BP_SYM (__strnlen))
171 weak_alias (BP_SYM (__strnlen), BP_SYM(strnlen))
172 libc_hidden_builtin_def (strnlen)