xref: /freebsd/contrib/cortex-strings/src/thumb-2/strcpy.c (revision 8c4282b370bd66908b45b6a223226a9fc2b69d57)
1*09a53ad8SAndrew Turner /*
2*09a53ad8SAndrew Turner  * Copyright (c) 2008 ARM Ltd
3*09a53ad8SAndrew Turner  * All rights reserved.
4*09a53ad8SAndrew Turner  *
5*09a53ad8SAndrew Turner  * Redistribution and use in source and binary forms, with or without
6*09a53ad8SAndrew Turner  * modification, are permitted provided that the following conditions
7*09a53ad8SAndrew Turner  * are met:
8*09a53ad8SAndrew Turner  * 1. Redistributions of source code must retain the above copyright
9*09a53ad8SAndrew Turner  *    notice, this list of conditions and the following disclaimer.
10*09a53ad8SAndrew Turner  * 2. Redistributions in binary form must reproduce the above copyright
11*09a53ad8SAndrew Turner  *    notice, this list of conditions and the following disclaimer in the
12*09a53ad8SAndrew Turner  *    documentation and/or other materials provided with the distribution.
13*09a53ad8SAndrew Turner  * 3. The name of the company may not be used to endorse or promote
14*09a53ad8SAndrew Turner  *    products derived from this software without specific prior written
15*09a53ad8SAndrew Turner  *    permission.
16*09a53ad8SAndrew Turner  *
17*09a53ad8SAndrew Turner  * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
18*09a53ad8SAndrew Turner  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*09a53ad8SAndrew Turner  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*09a53ad8SAndrew Turner  * IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21*09a53ad8SAndrew Turner  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22*09a53ad8SAndrew Turner  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23*09a53ad8SAndrew Turner  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24*09a53ad8SAndrew Turner  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25*09a53ad8SAndrew Turner  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26*09a53ad8SAndrew Turner  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*09a53ad8SAndrew Turner  */
28*09a53ad8SAndrew Turner 
29*09a53ad8SAndrew Turner /* For GLIBC:
30*09a53ad8SAndrew Turner #include <string.h>
31*09a53ad8SAndrew Turner #include <memcopy.h>
32*09a53ad8SAndrew Turner 
33*09a53ad8SAndrew Turner #undef strcmp
34*09a53ad8SAndrew Turner */
35*09a53ad8SAndrew Turner 
36*09a53ad8SAndrew Turner #ifdef __thumb2__
37*09a53ad8SAndrew Turner #define magic1(REG) "#0x01010101"
38*09a53ad8SAndrew Turner #define magic2(REG) "#0x80808080"
39*09a53ad8SAndrew Turner #else
40*09a53ad8SAndrew Turner #define magic1(REG) #REG
41*09a53ad8SAndrew Turner #define magic2(REG) #REG ", lsl #7"
42*09a53ad8SAndrew Turner #endif
43*09a53ad8SAndrew Turner 
44*09a53ad8SAndrew Turner char* __attribute__((naked))
strcpy(char * dst,const char * src)45*09a53ad8SAndrew Turner strcpy (char* dst, const char* src)
46*09a53ad8SAndrew Turner {
47*09a53ad8SAndrew Turner   asm (
48*09a53ad8SAndrew Turner #if !(defined(__OPTIMIZE_SIZE__) || defined (PREFER_SIZE_OVER_SPEED) || \
49*09a53ad8SAndrew Turner       (defined (__thumb__) && !defined (__thumb2__)))
50*09a53ad8SAndrew Turner        "pld	[r1, #0]\n\t"
51*09a53ad8SAndrew Turner        "eor	r2, r0, r1\n\t"
52*09a53ad8SAndrew Turner        "mov	ip, r0\n\t"
53*09a53ad8SAndrew Turner        "tst	r2, #3\n\t"
54*09a53ad8SAndrew Turner        "bne	4f\n\t"
55*09a53ad8SAndrew Turner        "tst	r1, #3\n\t"
56*09a53ad8SAndrew Turner        "bne	3f\n"
57*09a53ad8SAndrew Turner   "5:\n\t"
58*09a53ad8SAndrew Turner #ifndef __thumb2__
59*09a53ad8SAndrew Turner        "str	r5, [sp, #-4]!\n\t"
60*09a53ad8SAndrew Turner        "mov	r5, #0x01\n\t"
61*09a53ad8SAndrew Turner        "orr	r5, r5, r5, lsl #8\n\t"
62*09a53ad8SAndrew Turner        "orr	r5, r5, r5, lsl #16\n\t"
63*09a53ad8SAndrew Turner #endif
64*09a53ad8SAndrew Turner 
65*09a53ad8SAndrew Turner        "str	r4, [sp, #-4]!\n\t"
66*09a53ad8SAndrew Turner        "tst	r1, #4\n\t"
67*09a53ad8SAndrew Turner        "ldr	r3, [r1], #4\n\t"
68*09a53ad8SAndrew Turner        "beq	2f\n\t"
69*09a53ad8SAndrew Turner        "sub	r2, r3, "magic1(r5)"\n\t"
70*09a53ad8SAndrew Turner        "bics	r2, r2, r3\n\t"
71*09a53ad8SAndrew Turner        "tst	r2, "magic2(r5)"\n\t"
72*09a53ad8SAndrew Turner        "itt	eq\n\t"
73*09a53ad8SAndrew Turner        "streq	r3, [ip], #4\n\t"
74*09a53ad8SAndrew Turner        "ldreq	r3, [r1], #4\n"
75*09a53ad8SAndrew Turner        "bne	1f\n\t"
76*09a53ad8SAndrew Turner        /* Inner loop.  We now know that r1 is 64-bit aligned, so we
77*09a53ad8SAndrew Turner 	  can safely fetch up to two words.  This allows us to avoid
78*09a53ad8SAndrew Turner 	  load stalls.  */
79*09a53ad8SAndrew Turner        ".p2align 2\n"
80*09a53ad8SAndrew Turner   "2:\n\t"
81*09a53ad8SAndrew Turner        "pld	[r1, #8]\n\t"
82*09a53ad8SAndrew Turner        "ldr	r4, [r1], #4\n\t"
83*09a53ad8SAndrew Turner        "sub	r2, r3, "magic1(r5)"\n\t"
84*09a53ad8SAndrew Turner        "bics	r2, r2, r3\n\t"
85*09a53ad8SAndrew Turner        "tst	r2, "magic2(r5)"\n\t"
86*09a53ad8SAndrew Turner        "sub	r2, r4, "magic1(r5)"\n\t"
87*09a53ad8SAndrew Turner        "bne	1f\n\t"
88*09a53ad8SAndrew Turner        "str	r3, [ip], #4\n\t"
89*09a53ad8SAndrew Turner        "bics	r2, r2, r4\n\t"
90*09a53ad8SAndrew Turner        "tst	r2, "magic2(r5)"\n\t"
91*09a53ad8SAndrew Turner        "itt	eq\n\t"
92*09a53ad8SAndrew Turner        "ldreq	r3, [r1], #4\n\t"
93*09a53ad8SAndrew Turner        "streq	r4, [ip], #4\n\t"
94*09a53ad8SAndrew Turner        "beq	2b\n\t"
95*09a53ad8SAndrew Turner        "mov	r3, r4\n"
96*09a53ad8SAndrew Turner   "1:\n\t"
97*09a53ad8SAndrew Turner #ifdef __ARMEB__
98*09a53ad8SAndrew Turner        "rors	r3, r3, #24\n\t"
99*09a53ad8SAndrew Turner #endif
100*09a53ad8SAndrew Turner        "strb	r3, [ip], #1\n\t"
101*09a53ad8SAndrew Turner        "tst	r3, #0xff\n\t"
102*09a53ad8SAndrew Turner #ifdef __ARMEL__
103*09a53ad8SAndrew Turner        "ror	r3, r3, #8\n\t"
104*09a53ad8SAndrew Turner #endif
105*09a53ad8SAndrew Turner        "bne	1b\n\t"
106*09a53ad8SAndrew Turner        "ldr	r4, [sp], #4\n\t"
107*09a53ad8SAndrew Turner #ifndef __thumb2__
108*09a53ad8SAndrew Turner        "ldr	r5, [sp], #4\n\t"
109*09a53ad8SAndrew Turner #endif
110*09a53ad8SAndrew Turner        "BX LR\n"
111*09a53ad8SAndrew Turner 
112*09a53ad8SAndrew Turner        /* Strings have the same offset from word alignment, but it's
113*09a53ad8SAndrew Turner 	  not zero.  */
114*09a53ad8SAndrew Turner   "3:\n\t"
115*09a53ad8SAndrew Turner        "tst	r1, #1\n\t"
116*09a53ad8SAndrew Turner        "beq	1f\n\t"
117*09a53ad8SAndrew Turner        "ldrb	r2, [r1], #1\n\t"
118*09a53ad8SAndrew Turner        "strb	r2, [ip], #1\n\t"
119*09a53ad8SAndrew Turner        "cmp	r2, #0\n\t"
120*09a53ad8SAndrew Turner        "it	eq\n"
121*09a53ad8SAndrew Turner        "BXEQ LR\n"
122*09a53ad8SAndrew Turner   "1:\n\t"
123*09a53ad8SAndrew Turner        "tst	r1, #2\n\t"
124*09a53ad8SAndrew Turner        "beq	5b\n\t"
125*09a53ad8SAndrew Turner        "ldrh	r2, [r1], #2\n\t"
126*09a53ad8SAndrew Turner #ifdef __ARMEB__
127*09a53ad8SAndrew Turner        "tst	r2, #0xff00\n\t"
128*09a53ad8SAndrew Turner        "iteet	ne\n\t"
129*09a53ad8SAndrew Turner        "strneh	r2, [ip], #2\n\t"
130*09a53ad8SAndrew Turner        "lsreq	r2, r2, #8\n\t"
131*09a53ad8SAndrew Turner        "streqb	r2, [ip]\n\t"
132*09a53ad8SAndrew Turner        "tstne	r2, #0xff\n\t"
133*09a53ad8SAndrew Turner #else
134*09a53ad8SAndrew Turner        "tst	r2, #0xff\n\t"
135*09a53ad8SAndrew Turner        "itet	ne\n\t"
136*09a53ad8SAndrew Turner        "strneh	r2, [ip], #2\n\t"
137*09a53ad8SAndrew Turner        "streqb	r2, [ip]\n\t"
138*09a53ad8SAndrew Turner        "tstne	r2, #0xff00\n\t"
139*09a53ad8SAndrew Turner #endif
140*09a53ad8SAndrew Turner        "bne	5b\n\t"
141*09a53ad8SAndrew Turner        "BX LR\n"
142*09a53ad8SAndrew Turner 
143*09a53ad8SAndrew Turner        /* src and dst do not have a common word-alignement.  Fall back to
144*09a53ad8SAndrew Turner 	  byte copying.  */
145*09a53ad8SAndrew Turner   "4:\n\t"
146*09a53ad8SAndrew Turner        "ldrb	r2, [r1], #1\n\t"
147*09a53ad8SAndrew Turner        "strb	r2, [ip], #1\n\t"
148*09a53ad8SAndrew Turner        "cmp	r2, #0\n\t"
149*09a53ad8SAndrew Turner        "bne	4b\n\t"
150*09a53ad8SAndrew Turner        "BX LR"
151*09a53ad8SAndrew Turner 
152*09a53ad8SAndrew Turner #elif !defined (__thumb__) || defined (__thumb2__)
153*09a53ad8SAndrew Turner        "mov	r3, r0\n\t"
154*09a53ad8SAndrew Turner   "1:\n\t"
155*09a53ad8SAndrew Turner        "ldrb	r2, [r1], #1\n\t"
156*09a53ad8SAndrew Turner        "strb	r2, [r3], #1\n\t"
157*09a53ad8SAndrew Turner        "cmp	r2, #0\n\t"
158*09a53ad8SAndrew Turner        "bne	1b\n\t"
159*09a53ad8SAndrew Turner        "BX LR"
160*09a53ad8SAndrew Turner #else
161*09a53ad8SAndrew Turner        "mov	r3, r0\n\t"
162*09a53ad8SAndrew Turner   "1:\n\t"
163*09a53ad8SAndrew Turner        "ldrb	r2, [r1]\n\t"
164*09a53ad8SAndrew Turner        "add	r1, r1, #1\n\t"
165*09a53ad8SAndrew Turner        "strb	r2, [r3]\n\t"
166*09a53ad8SAndrew Turner        "add	r3, r3, #1\n\t"
167*09a53ad8SAndrew Turner        "cmp	r2, #0\n\t"
168*09a53ad8SAndrew Turner        "bne	1b\n\t"
169*09a53ad8SAndrew Turner        "BX LR"
170*09a53ad8SAndrew Turner #endif
171*09a53ad8SAndrew Turner        );
172*09a53ad8SAndrew Turner }
173*09a53ad8SAndrew Turner /* For GLIBC: libc_hidden_builtin_def (strcpy) */
174