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

« back to all changes in this revision

Viewing changes to src/thumb-2/strlen.c

  • Committer: Michael Hope
  • Date: 2010-09-02 00:46:57 UTC
  • Revision ID: michael.hope@linaro.org-20100902004657-4q6rn5888130zr3o
Pulled in the routines and packaged them up.

Show diffs side-by-side

added added

removed removed

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