~linaro-toolchain-dev/cortex-strings/trunk

7 by Michael Hope
Pulled in the routines and packaged them up.
1
/*
2
 * Copyright (c) 2008 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 <limits.h>
30
#include <stddef.h>
31
11 by Michael Hope
Added GLIBC add-on support
32
/* For GLIBC:
33
#include <string.h>
34
#include <memcopy.h>
35
36
#undef strcmp
37
*/
38
7 by Michael Hope
Pulled in the routines and packaged them up.
39
#if defined (__OPTIMIZE_SIZE__) || defined (PREFER_SIZE_OVER_SPEED) || \
40
  (defined (__thumb__) && !defined (__thumb2__))
41
42
size_t
43
strlen (const char* str)
44
{
45
  int scratch;
46
#if defined (__thumb__) && !defined (__thumb2__)
47
  size_t len;
48
  asm ("mov	%0, #0\n"
49
       "1:\n\t"
50
       "ldrb	%1, [%2, %0]\n\t"
51
       "add 	%0, %0, #1\n\t"
52
       "cmp	%1, #0\n\t"
53
       "bne	1b"
54
       : "=&r" (len), "=&r" (scratch) : "r" (str) : "memory", "cc");
55
  return len - 1;
56
#else
57
  const char* end;
58
  asm ("1:\n\t"
59
       "ldrb	%1, [%0], #1\n\t"
60
       "cmp	%1, #0\n\t"
61
       "bne	1b"
62
       : "=&r" (end), "=&r" (scratch) : "0" (str) : "memory", "cc");
63
  return end - str - 1;
64
#endif
65
}
66
#else
67
68
size_t __attribute__((naked))
69
strlen (const char* str)
70
{
71
  asm ("len .req r0\n\t"
72
       "data .req r3\n\t"
73
       "addr .req r1\n\t"
74
75
       "pld [r0, #0]\n\t"
76
       /* Word-align address */
77
       "bic	addr, r0, #3\n\t"
78
       /* Get adjustment for start ... */
79
       "ands	len, r0, #3\n\t"
80
       "neg	len, len\n\t"
81
       /* First word of data */
82
       "ldr	data, [addr], #4\n\t"
83
       /* Ensure bytes preceeding start ... */
84
       "add	ip, len, #4\n\t"
85
       "mov	ip, ip, asl #3\n\t"
86
       "mvn	r2, #0\n\t"
87
       /* ... are masked out */
88
#ifdef __thumb__
89
       "itt	ne\n\t"
90
# ifdef __ARMEB__
91
       "lslne	r2, ip\n\t"
92
# else
93
       "lsrne	r2, ip\n\t"
94
# endif
95
       "orrne	data, data, r2\n\t"
96
#else
97
       "it	ne\n\t"
98
# ifdef __ARMEB__
99
       "orrne	data, data, r2, lsl ip\n\t"
100
# else
101
       "orrne	data, data, r2, lsr ip\n\t"
102
# endif
103
#endif
104
       /* Magic const 0x01010101 */
105
#ifdef _ISA_ARM_7
106
       "movw	ip, #0x101\n\t"
107
#else
108
       "mov	ip, #0x1\n\t"
109
       "orr	ip, ip, ip, lsl #8\n\t"
110
#endif
111
       "orr	ip, ip, ip, lsl #16\n"
112
113
	/* This is the main loop.  We subtract one from each byte in
114
	   the word: the sign bit changes iff the byte was zero or
115
	   0x80 -- we eliminate the latter case by anding the result
116
	   with the 1-s complement of the data.  */
117
       "1:\n\t"
118
       /* test (data - 0x01010101)  */
119
       "sub	r2, data, ip\n\t"
120
       /* ... & ~data */
121
       "bic	r2, r2, data\n\t"
122
       /* ... & 0x80808080 == 0? */
123
       "ands	r2, r2, ip, lsl #7\n\t"
124
#ifdef _ISA_ARM_7
125
       /* yes, get more data... */
126
       "itt	eq\n\t"
127
       "ldreq	data, [addr], #4\n\t"
128
       /* and 4 more bytes  */
129
       "addeq	len, len, #4\n\t"
130
	/* If we have PLD, then unroll the loop a bit.  */
131
       "pld [addr, #8]\n\t"
132
       /*  test (data - 0x01010101)  */
133
       "ittt	eq\n\t"
134
       "subeq	r2, data, ip\n\t"
135
       /* ... & ~data */
136
       "biceq	r2, r2, data\n\t"
137
       /* ... & 0x80808080 == 0? */
138
       "andeqs	r2, r2, ip, lsl #7\n\t"
139
#endif
140
       "itt	eq\n\t"
141
       /* yes, get more data... */
142
       "ldreq	data, [addr], #4\n\t"
143
       /* and 4 more bytes  */
144
       "addeq	len, len, #4\n\t"
145
       "beq	1b\n\t"
146
#ifdef __ARMEB__
147
       "tst	data, #0xff000000\n\t"
148
       "itttt	ne\n\t"
149
       "addne	len, len, #1\n\t"
150
       "tstne	data, #0xff0000\n\t"
151
       "addne	len, len, #1\n\t"
152
       "tstne	data, #0xff00\n\t"
153
       "it	ne\n\t"
154
       "addne	len, len, #1\n\t"
155
#else
156
# ifdef _ISA_ARM_5
157
	/* R2 is the residual sign bits from the above test.  All we
158
	need to do now is establish the position of the first zero
159
	byte... */
160
	/* Little-endian is harder, we need the number of trailing
161
	zeros / 8 */
162
#  ifdef _ISA_ARM_7
163
       "rbit	r2, r2\n\t"
164
       "clz	r2, r2\n\t"
165
#  else
166
       "rsb	r1, r2, #0\n\t"
167
       "and	r2, r2, r1\n\t"
168
       "clz	r2, r2\n\t"
169
       "rsb	r2, r2, #31\n\t"
170
#  endif
171
       "add	len, len, r2, lsr #3\n\t"
172
# else  /* No CLZ instruction */
173
       "tst	data, #0xff\n\t"
174
       "itttt	ne\n\t"
175
       "addne	len, len, #1\n\t"
176
       "tstne	data, #0xff00\n\t"
177
       "addne	len, len, #1\n\t"
178
       "tstne	data, #0xff0000\n\t"
179
       "it	ne\n\t"
180
       "addne	len, len, #1\n\t"
181
# endif
182
#endif
183
       "BX LR");
184
}
185
#endif
11 by Michael Hope
Added GLIBC add-on support
186
/* For GLIBC: libc_hidden_builtin_def (strlen) */