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

« back to all changes in this revision

Viewing changes to src/thumb-2/strcpy.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
#ifdef __thumb2__
 
30
#define magic1(REG) "#0x01010101"
 
31
#define magic2(REG) "#0x80808080"
 
32
#else
 
33
#define magic1(REG) #REG
 
34
#define magic2(REG) #REG ", lsl #7"
 
35
#endif
 
36
 
 
37
char* __attribute__((naked))
 
38
strcpy (char* dst, const char* src)
 
39
{
 
40
  asm (
 
41
#if !(defined(__OPTIMIZE_SIZE__) || defined (PREFER_SIZE_OVER_SPEED) || \
 
42
      (defined (__thumb__) && !defined (__thumb2__)))
 
43
       "pld     [r1, #0]\n\t"
 
44
       "eor     r2, r0, r1\n\t"
 
45
       "mov     ip, r0\n\t"
 
46
       "tst     r2, #3\n\t"
 
47
       "bne     4f\n\t"
 
48
       "tst     r1, #3\n\t"
 
49
       "bne     3f\n"
 
50
  "5:\n\t"
 
51
#ifndef __thumb2__
 
52
       "str     r5, [sp, #-4]!\n\t"
 
53
       "mov     r5, #0x01\n\t"
 
54
       "orr     r5, r5, r5, lsl #8\n\t"
 
55
       "orr     r5, r5, r5, lsl #16\n\t"
 
56
#endif
 
57
 
 
58
       "str     r4, [sp, #-4]!\n\t"
 
59
       "tst     r1, #4\n\t"
 
60
       "ldr     r3, [r1], #4\n\t"
 
61
       "beq     2f\n\t"
 
62
       "sub     r2, r3, "magic1(r5)"\n\t"
 
63
       "bics    r2, r2, r3\n\t"
 
64
       "tst     r2, "magic2(r5)"\n\t"
 
65
       "itt     eq\n\t"
 
66
       "streq   r3, [ip], #4\n\t"
 
67
       "ldreq   r3, [r1], #4\n"
 
68
       "bne     1f\n\t"
 
69
       /* Inner loop.  We now know that r1 is 64-bit aligned, so we
 
70
          can safely fetch up to two words.  This allows us to avoid
 
71
          load stalls.  */
 
72
       ".p2align 2\n"
 
73
  "2:\n\t"
 
74
       "pld     [r1, #8]\n\t"
 
75
       "ldr     r4, [r1], #4\n\t"
 
76
       "sub     r2, r3, "magic1(r5)"\n\t"
 
77
       "bics    r2, r2, r3\n\t"
 
78
       "tst     r2, "magic2(r5)"\n\t"
 
79
       "sub     r2, r4, "magic1(r5)"\n\t"
 
80
       "bne     1f\n\t"
 
81
       "str     r3, [ip], #4\n\t"
 
82
       "bics    r2, r2, r4\n\t"
 
83
       "tst     r2, "magic2(r5)"\n\t"
 
84
       "itt     eq\n\t"
 
85
       "ldreq   r3, [r1], #4\n\t"
 
86
       "streq   r4, [ip], #4\n\t"
 
87
       "beq     2b\n\t"
 
88
       "mov     r3, r4\n"
 
89
  "1:\n\t"
 
90
#ifdef __ARMEB__
 
91
       "rors    r3, r3, #24\n\t"
 
92
#endif
 
93
       "strb    r3, [ip], #1\n\t"
 
94
       "tst     r3, #0xff\n\t"
 
95
#ifdef __ARMEL__
 
96
       "ror     r3, r3, #8\n\t"
 
97
#endif
 
98
       "bne     1b\n\t"
 
99
       "ldr     r4, [sp], #4\n\t"
 
100
#ifndef __thumb2__
 
101
       "ldr     r5, [sp], #4\n\t"
 
102
#endif
 
103
       "BX LR\n"
 
104
 
 
105
       /* Strings have the same offset from word alignment, but it's
 
106
          not zero.  */
 
107
  "3:\n\t"
 
108
       "tst     r1, #1\n\t"
 
109
       "beq     1f\n\t"
 
110
       "ldrb    r2, [r1], #1\n\t"
 
111
       "strb    r2, [ip], #1\n\t"
 
112
       "cmp     r2, #0\n\t"
 
113
       "it      eq\n"
 
114
       "BXEQ LR\n"
 
115
  "1:\n\t"
 
116
       "tst     r1, #2\n\t"
 
117
       "beq     5b\n\t"
 
118
       "ldrh    r2, [r1], #2\n\t"
 
119
#ifdef __ARMEB__
 
120
       "tst     r2, #0xff00\n\t"
 
121
       "iteet   ne\n\t"
 
122
       "strneh  r2, [ip], #2\n\t"
 
123
       "lsreq   r2, r2, #8\n\t"
 
124
       "streqb  r2, [ip]\n\t"
 
125
       "tstne   r2, #0xff\n\t"
 
126
#else
 
127
       "tst     r2, #0xff\n\t"
 
128
       "itet    ne\n\t"
 
129
       "strneh  r2, [ip], #2\n\t"
 
130
       "streqb  r2, [ip]\n\t"
 
131
       "tstne   r2, #0xff00\n\t"
 
132
#endif
 
133
       "bne     5b\n\t"
 
134
       "BX LR\n"
 
135
 
 
136
       /* src and dst do not have a common word-alignement.  Fall back to
 
137
          byte copying.  */
 
138
  "4:\n\t"
 
139
       "ldrb    r2, [r1], #1\n\t"
 
140
       "strb    r2, [ip], #1\n\t"
 
141
       "cmp     r2, #0\n\t"
 
142
       "bne     4b\n\t"
 
143
       "BX LR"
 
144
 
 
145
#elif !defined (__thumb__) || defined (__thumb2__)
 
146
       "mov     r3, r0\n\t"
 
147
  "1:\n\t"
 
148
       "ldrb    r2, [r1], #1\n\t"
 
149
       "strb    r2, [r3], #1\n\t"
 
150
       "cmp     r2, #0\n\t"
 
151
       "bne     1b\n\t"
 
152
       "BX LR"
 
153
#else
 
154
       "mov     r3, r0\n\t"
 
155
  "1:\n\t"
 
156
       "ldrb    r2, [r1]\n\t"
 
157
       "add     r1, r1, #1\n\t"
 
158
       "strb    r2, [r3]\n\t"
 
159
       "add     r3, r3, #1\n\t"
 
160
       "cmp     r2, #0\n\t"
 
161
       "bne     1b\n\t"
 
162
       "BX LR"
 
163
#endif
 
164
       );
 
165
}