1 /* Copyright (c) 2009 Xilinx, Inc. All rights reserved.
2
3 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions are
5 met:
6
7 1. Redistributions source code must retain the above copyright notice,
8 this list of conditions and the following disclaimer.
9
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.
13
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.
17
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.
29
30
31 FUNCTION
32 <<strlen>>---character string length
33
34 INDEX
35 strlen
36
37 SYNOPSIS
38 #include <string.h>
39 size_t strlen(const char *<[str]>);
40
41 DESCRIPTION
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.
45
46 RETURNS
47 <<strlen>> returns the character count.
48
49 PORTABILITY
50 <<strlen>> is ANSI C.
51
52 <<strlen>> requires no supporting OS subroutines.
53
54 QUICKREF
55 strlen ansi pure
56 */
57
58 #include <_ansi.h>
59 #include <string.h>
60 #include <limits.h>
61
62 #define LBLOCKSIZE (sizeof (long))
63 #define UNALIGNED(X) ((long)X & (LBLOCKSIZE - 1))
64
65 #if LONG_MAX == 2147483647L
66 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
67 #else
68 #if LONG_MAX == 9223372036854775807L
69 /* Nonzero if X (a long int) contains a NULL byte. */
70 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
71 #else
72 #error long int is not a 32bit or 64bit type.
73 #endif
74 #endif
75
76 #ifndef DETECTNULL
77 #error long int is not a 32bit or 64bit byte
78 #endif
79
80 size_t
strlen(const char * str)81 strlen (const char *str)
82 {
83
84 #ifndef HAVE_HW_PCMP
85
86 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
87 const char *start = str;
88
89 while (*str)
90 str++;
91
92 return str - start;
93 #else
94 const char *start = str;
95 unsigned long *aligned_addr;
96
97 if (!UNALIGNED (str))
98 {
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))
103 aligned_addr++;
104
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;
108 }
109
110 while (*str)
111 str++;
112 return str - start;
113 #endif /* not PREFER_SIZE_OVER_SPEED */
114
115 #else
116
117 #include "mb_endian.h"
118
119 __asm__ volatile (" \n\
120 or r9, r0, r0 /* Index register */ \n\
121 check_alignment: \n\
122 andi r3, r5, 3 \n\
123 bnei r3, align_arg \n\
124 len_loop: \n"
125 LOAD4BYTES("r3", "r5", "r9")
126 " \n\
127 pcmpbf r4, r3, r0 \n\
128 bnei r4, end_len \n\
129 brid len_loop \n\
130 addik r9, r9, 4 \n\
131 end_len: \n\
132 lbu r3, r5, r9 \n\
133 beqi r3, done_len \n\
134 brid end_len \n\
135 addik r9, r9, 1 \n\
136 done_len: \n\
137 rtsd r15, 8 \n\
138 or r3, r0, r9 /* Return len */ \n\
139 align_arg: \n\
140 rsubik r10, r3, 4 \n\
141 align_loop: \n\
142 lbu r3, r5, r9 \n\
143 beqid r3, done_len \n\
144 addik r10, r10, -1 \n\
145 bneid r10, align_loop \n\
146 addik r9, r9, 1 \n\
147 bri len_loop");
148
149 #endif /* ! HAVE_HW_PCMP */
150 }
151