1 /* Copyright (c) 2009 Xilinx, Inc. All rights reserved.
3 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions are
7 1. Redistributions source code must retain the above copyright notice,
8 this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
14 3. Neither the name of Xilinx nor the names of its contributors may be
15 used to endorse or promote products derived from this software without
16 specific prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS
19 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 <<strlen>>---character string length
39 size_t strlen(const char *<[str]>);
42 The <<strlen>> function works out the length of the string
43 starting at <<*<[str]>>> by counting chararacters until it
44 reaches a <<NULL>> character.
47 <<strlen>> returns the character count.
52 <<strlen>> requires no supporting OS subroutines.
62 #define LBLOCKSIZE (sizeof (long))
63 #define UNALIGNED(X) ((long)X & (LBLOCKSIZE - 1))
65 #if LONG_MAX == 2147483647L
66 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
68 #if LONG_MAX == 9223372036854775807L
69 /* Nonzero if X (a long int) contains a NULL byte. */
70 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
72 #error long int is not a 32bit or 64bit type.
77 #error long int is not a 32bit or 64bit byte
81 strlen (const char *str
)
86 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
87 const char *start
= str
;
94 const char *start
= str
;
95 unsigned long *aligned_addr
;
99 /* If the string is word-aligned, we can check for the presence of
100 a null in each word-sized block. */
101 aligned_addr
= (unsigned long*)str
;
102 while (!DETECTNULL (*aligned_addr
))
105 /* Once a null is detected, we check each byte in that block for a
106 precise position of the null. */
107 str
= (char*)aligned_addr
;
113 #endif /* not PREFER_SIZE_OVER_SPEED */
117 #include "mb_endian.h"
120 or r9, r0, r0 /* Index register */ \n\
123 bnei r3, align_arg \n\
125 LOAD4BYTES("r3", "r5", "r9")
127 pcmpbf r4, r3, r0 \n\
133 beqi r3, done_len \n\
138 or r3, r0, r9 /* Return len */ \n\
140 rsubik r10, r3, 4 \n\
143 beqid r3, done_len \n\
144 addik r10, r10, -1 \n\
145 bneid r10, align_loop \n\
149 #endif /* ! HAVE_HW_PCMP */