1 /*
2  * Copyright (c) 2008-2015 ARM Ltd
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, 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.
13  * 3. The name of the company may not be used to endorse or promote
14  *    products derived from this software without specific prior written
15  *    permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <picolibc.h>
30 
31 #include "arm_asm.h"
32 #include <string.h>
33 #include <limits.h>
34 
35 #if defined __OPTIMIZE_SIZE__ || defined PREFER_SIZE_OVER_SPEED
36 #if __ARM_ARCH_ISA_THUMB == 2
37 /* Implemented in strlen.S.  */
38 
39 #elif defined (__thumb__)
40 /* Implemented in strlen.S.  */
41 
42 #else
43 #include "../../string/strlen.c"
44 
45 #endif
46 
47 #else /* defined __OPTIMIZE_SIZE__ || defined PREFER_SIZE_OVER_SPEED */
48 #if defined __thumb__ && ! defined __thumb2__
49 #include "../../string/strlen.c"
50 
51 #elif __ARM_ARCH_ISA_THUMB >= 2 && defined __ARM_FEATURE_DSP
52   /* Implemented in strlen.S.  */
53 
54 #else
55 #pragma GCC diagnostic ignored "-Wunused-parameter"
56 size_t __attribute__((naked))
strlen(const char * str)57 strlen (const char* str)
58 {
59   __asm__("len .req r0\n\t"
60        "data .req r3\n\t"
61        "addr .req r1\n\t"
62 
63 #ifdef _ISA_ARM_7
64        "pld [r0]\n\t"
65 #endif
66        /* Word-align address */
67        "bic	addr, r0, #3\n\t"
68        /* Get adjustment for start ... */
69        "ands	len, r0, #3\n\t"
70        "neg	len, len\n\t"
71        /* First word of data */
72        "ldr	data, [addr], #4\n\t"
73        /* Ensure bytes preceeding start ... */
74        "add	ip, len, #4\n\t"
75        "mov	ip, ip, asl #3\n\t"
76        "mvn	r2, #0\n\t"
77        /* ... are masked out */
78 #ifdef __thumb__
79        "itt	ne\n\t"
80 # ifdef __ARMEB__
81        "lslne	r2, ip\n\t"
82 # else
83        "lsrne	r2, ip\n\t"
84 # endif
85        "orrne	data, data, r2\n\t"
86 #else
87        "it	ne\n\t"
88 # ifdef __ARMEB__
89        "orrne	data, data, r2, lsl ip\n\t"
90 # else
91        "orrne	data, data, r2, lsr ip\n\t"
92 # endif
93 #endif
94        /* Magic const 0x01010101 */
95 #ifdef _ISA_ARM_7
96        "movw	ip, #0x101\n\t"
97 #else
98        "mov	ip, #0x1\n\t"
99        "orr	ip, ip, ip, lsl #8\n\t"
100 #endif
101        "orr	ip, ip, ip, lsl #16\n"
102 
103 	/* This is the main loop.  We subtract one from each byte in
104 	   the word: the sign bit changes iff the byte was zero or
105 	   0x80 -- we eliminate the latter case by anding the result
106 	   with the 1-s complement of the data.  */
107        "1:\n\t"
108        /* test (data - 0x01010101)  */
109        "sub	r2, data, ip\n\t"
110        /* ... & ~data */
111        "bic	r2, r2, data\n\t"
112        /* ... & 0x80808080 == 0? */
113        "ands	r2, r2, ip, lsl #7\n\t"
114 #ifdef _ISA_ARM_7
115        /* yes, get more data... */
116        "itt	eq\n\t"
117        "ldreq	data, [addr], #4\n\t"
118        /* and 4 more bytes  */
119        "addeq	len, len, #4\n\t"
120 	/* Unroll the loop a bit.  */
121        "pld	[addr, #8]\n\t"
122        /*  test (data - 0x01010101)  */
123        "ittt	eq\n\t"
124        "subeq	r2, data, ip\n\t"
125        /* ... & ~data */
126        "biceq	r2, r2, data\n\t"
127        /* ... & 0x80808080 == 0? */
128        "andeqs	r2, r2, ip, lsl #7\n\t"
129 #endif
130        "itt	eq\n\t"
131        /* yes, get more data... */
132        "ldreq	data, [addr], #4\n\t"
133        /* and 4 more bytes  */
134        "addeq	len, len, #4\n\t"
135        "beq	1b\n\t"
136 #ifdef __ARMEB__
137        "tst	data, #0xff000000\n\t"
138        "itttt	ne\n\t"
139        "addne	len, len, #1\n\t"
140        "tstne	data, #0xff0000\n\t"
141        "addne	len, len, #1\n\t"
142        "tstne	data, #0xff00\n\t"
143        "it	ne\n\t"
144        "addne	len, len, #1\n\t"
145 #else
146 # ifdef _ISA_ARM_5
147 	/* R2 is the residual sign bits from the above test.  All we
148 	need to do now is establish the position of the first zero
149 	byte... */
150 	/* Little-endian is harder, we need the number of trailing
151 	zeros / 8 */
152 #  ifdef _ISA_ARM_7
153        "rbit	r2, r2\n\t"
154        "clz	r2, r2\n\t"
155 #  else
156        "rsb	r1, r2, #0\n\t"
157        "and	r2, r2, r1\n\t"
158        "clz	r2, r2\n\t"
159        "rsb	r2, r2, #31\n\t"
160 #  endif
161        "add	len, len, r2, lsr #3\n\t"
162 # else  /* No CLZ instruction */
163        "tst	data, #0xff\n\t"
164        "itttt	ne\n\t"
165        "addne	len, len, #1\n\t"
166        "tstne	data, #0xff00\n\t"
167        "addne	len, len, #1\n\t"
168        "tstne	data, #0xff0000\n\t"
169        "it	ne\n\t"
170        "addne	len, len, #1\n\t"
171 # endif
172 #endif
173        "bx	lr\n\t");
174 }
175 #endif
176 #endif
177