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 <picolibc.h>
59
60 #include <string.h>
61 #include <limits.h>
62
63 #define LBLOCKSIZE (sizeof (long))
64 #define UNALIGNED(X) ((long)X & (LBLOCKSIZE - 1))
65
66 #if LONG_MAX == 2147483647L
67 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
68 #else
69 #if LONG_MAX == 9223372036854775807L
70 /* Nonzero if X (a long int) contains a NULL byte. */
71 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
72 #else
73 #error long int is not a 32bit or 64bit type.
74 #endif
75 #endif
76
77 #ifndef DETECTNULL
78 #error long int is not a 32bit or 64bit byte
79 #endif
80
81 size_t
strlen(const char * str)82 strlen (const char *str)
83 {
84
85 #ifndef HAVE_HW_PCMP
86
87 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
88 const char *start = str;
89
90 while (*str)
91 str++;
92
93 return str - start;
94 #else
95 const char *start = str;
96 unsigned long *aligned_addr;
97
98 if (!UNALIGNED (str))
99 {
100 /* If the string is word-aligned, we can check for the presence of
101 a null in each word-sized block. */
102 aligned_addr = (unsigned long*)str;
103 while (!DETECTNULL (*aligned_addr))
104 aligned_addr++;
105
106 /* Once a null is detected, we check each byte in that block for a
107 precise position of the null. */
108 str = (char*)aligned_addr;
109 }
110
111 while (*str)
112 str++;
113 return str - start;
114 #endif /* not PREFER_SIZE_OVER_SPEED */
115
116 #else
117
118 #include "mb_endian.h"
119
120 __asm__ volatile (" \n\
121 or r9, r0, r0 /* Index register */ \n\
122 check_alignment: \n\
123 andi r3, r5, 3 \n\
124 bnei r3, align_arg \n\
125 len_loop: \n"
126 LOAD4BYTES("r3", "r5", "r9")
127 " \n\
128 pcmpbf r4, r3, r0 \n\
129 bnei r4, end_len \n\
130 brid len_loop \n\
131 addik r9, r9, 4 \n\
132 end_len: \n\
133 lbu r3, r5, r9 \n\
134 beqi r3, done_len \n\
135 brid end_len \n\
136 addik r9, r9, 1 \n\
137 done_len: \n\
138 rtsd r15, 8 \n\
139 or r3, r0, r9 /* Return len */ \n\
140 align_arg: \n\
141 rsubik r10, r3, 4 \n\
142 align_loop: \n\
143 lbu r3, r5, r9 \n\
144 beqid r3, done_len \n\
145 addik r10, r10, -1 \n\
146 bneid r10, align_loop \n\
147 addik r9, r9, 1 \n\
148 bri len_loop");
149
150 #endif /* ! HAVE_HW_PCMP */
151 }
152