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

« back to all changes in this revision

Viewing changes to src/linaro-a9/memcpy.S

  • Committer: Dr. David Alan Gilbert
  • Date: 2011-09-08 17:20:49 UTC
  • Revision ID: david.gilbert@linaro.org-20110908172049-sykgo503kal106w0
spaces->tabs, use C style comments for the big top comment

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
        .syntax unified
2
 
        .arch armv7-a
3
 
 
4
 
@ Copyright (c) 2010-2011, Linaro Limited
5
 
@ All rights reserved.
6
 
 
7
 
@ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8
 
 
9
 
@    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
10
 
@    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11
 
@    * Neither the name of Linaro Limited nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
12
 
 
13
 
@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 
 
15
 
@ Written by Dave Gilbert <david.gilbert@linaro.org>
16
 
 
17
 
@ This memcpy routine is optimised on a Cortex-A9 and should work on all ARMv7
18
 
@ processors.
 
1
/* Copyright (c) 2010-2011, Linaro Limited
 
2
   All rights reserved.
 
3
 
 
4
   Redistribution and use in source and binary forms, with or without
 
5
   modification, are permitted provided that the following conditions
 
6
   are met:
 
7
 
 
8
      * Redistributions of source code must retain the above copyright
 
9
      notice, this list of conditions and the following disclaimer.
 
10
 
 
11
      * Redistributions in binary form must reproduce the above copyright
 
12
      notice, this list of conditions and the following disclaimer in the
 
13
      documentation and/or other materials provided with the distribution.
 
14
 
 
15
      * Neither the name of Linaro Limited nor the names of its
 
16
      contributors may be used to endorse or promote products derived
 
17
      from this software without specific prior written permission.
 
18
 
 
19
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
20
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
21
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
22
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
23
   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
24
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
25
   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
26
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
27
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
28
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
29
   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
30
 
 
31
   Written by Dave Gilbert <david.gilbert@linaro.org>
 
32
 
 
33
   This memcpy routine is optimised on a Cortex-A9 and should work on
 
34
   all ARMv7 processors. */
19
35
 
20
36
@ 2011-09-01 david.gilbert@linaro.org
21
37
@    Extracted from local git 2f11b436
22
38
 
 
39
        .syntax unified
 
40
        .arch armv7-a
 
41
 
23
42
@ this lets us check a flag in a 00/ff byte easily in either endianness
24
43
#ifdef __ARMEB__
25
44
#define CHARTSTMASK(c) 1<<(31-(c*8))
26
45
#else
27
46
#define CHARTSTMASK(c) 1<<(c*8)
28
47
#endif
29
 
        .text
30
 
        .thumb
 
48
        .text
 
49
        .thumb
31
50
 
32
51
@ ---------------------------------------------------------------------------
33
52
        .thumb_func
36
55
        .global memcpy
37
56
        .type memcpy,%function
38
57
memcpy:
39
 
  @ r0 = dest
40
 
  @ r1 = source
41
 
  @ r2 = count
42
 
  @ returns dest in r0
43
 
  @ Overlaps of source/dest not allowed according to spec
44
 
  @ Note this routine relies on v7 misaligned loads/stores
45
 
  pld     [r1]
46
 
  mov     r12, r0       @ stash original r0
47
 
  cmp     r2,#32
48
 
  blt     10f           @ take the small copy case separately
 
58
        @ r0 = dest
 
59
        @ r1 = source
 
60
        @ r2 = count
 
61
        @ returns dest in r0
 
62
        @ Overlaps of source/dest not allowed according to spec
 
63
        @ Note this routine relies on v7 misaligned loads/stores
 
64
        pld     [r1]
 
65
        mov     r12, r0         @ stash original r0
 
66
        cmp     r2,#32
 
67
        blt     10f             @ take the small copy case separately
49
68
 
50
 
  @ test for either source or destination being misaligned
51
 
  @ (We only rely on word align)
52
 
  @ TODO: Test for co-misalignment
53
 
  tst     r0,#3
54
 
  it      eq
55
 
  tsteq   r1,#3
56
 
  bne     30f           @ misaligned case
 
69
        @ test for either source or destination being misaligned
 
70
        @ (We only rely on word align)
 
71
        @ TODO: Test for co-misalignment
 
72
        tst     r0,#3
 
73
        it      eq
 
74
        tsteq   r1,#3
 
75
        bne     30f             @ misaligned case
57
76
 
58
77
4:
59
 
  @ at this point we are word (or better) aligned and have at least
60
 
  @ 32 bytes to play with
61
 
  push    {r3,r4,r5,r6,r7,r8,r10,r11}
 
78
        @ at this point we are word (or better) aligned and have at least
 
79
        @ 32 bytes to play with
 
80
        push    {r3,r4,r5,r6,r7,r8,r10,r11}
62
81
5:
63
 
  ldmia   r1!,{r3,r4,r5,r6,r7,r8,r10,r11}
64
 
  pld     [r1,#96]
65
 
  sub     r2,r2,#32
66
 
  cmp     r2,#32
67
 
  stmia   r0!,{r3,r4,r5,r6,r7,r8,r10,r11}
68
 
  bge     5b
 
82
        ldmia   r1!,{r3,r4,r5,r6,r7,r8,r10,r11}
 
83
        pld     [r1,#96]
 
84
        sub     r2,r2,#32
 
85
        cmp     r2,#32
 
86
        stmia   r0!,{r3,r4,r5,r6,r7,r8,r10,r11}
 
87
        bge     5b
69
88
 
70
 
  pop     {r3,r4,r5,r6,r7,r8,r10,r11}
71
 
  @ We are now down to less than 32 bytes
72
 
  cbz     r2,15f        @ quick exit for the case where we copied a multiple of 32
 
89
        pop     {r3,r4,r5,r6,r7,r8,r10,r11}
 
90
        @ We are now down to less than 32 bytes
 
91
        cbz     r2,15f          @ quick exit for the case where we copied a multiple of 32
73
92
 
74
93
10:  @ small copies (not necessarily aligned - note might be slightly more than 32bytes)
75
 
  cmp     r2,#4
76
 
  blt     12f
 
94
        cmp     r2,#4
 
95
        blt     12f
77
96
11:
78
 
  sub     r2,r2,#4
79
 
  cmp     r2,#4
80
 
  ldr     r3, [r1],#4
81
 
  str     r3, [r0],#4
82
 
  bge     11b
 
97
        sub     r2,r2,#4
 
98
        cmp     r2,#4
 
99
        ldr     r3, [r1],#4
 
100
        str     r3, [r0],#4
 
101
        bge     11b
83
102
12:
84
 
  tst     r2,#2
85
 
  itt     ne
86
 
  ldrhne  r3, [r1],#2
87
 
  strhne  r3, [r0],#2
88
 
  
89
 
  tst     r2,#1
90
 
  itt     ne
91
 
  ldrbne  r3, [r1],#1
92
 
  strbne  r3, [r0],#1
93
 
  
 
103
        tst     r2,#2
 
104
        itt     ne
 
105
        ldrhne  r3, [r1],#2
 
106
        strhne  r3, [r0],#2
 
107
        
 
108
        tst     r2,#1
 
109
        itt     ne
 
110
        ldrbne  r3, [r1],#1
 
111
        strbne  r3, [r0],#1
 
112
        
94
113
15:  @ exit
95
 
  mov     r0,r12        @ restore r0
96
 
  bx      lr
 
114
        mov     r0,r12          @ restore r0
 
115
        bx      lr
97
116
 
98
117
30:  @ non-aligned - at least 32 bytes to play with
99
 
  @ On v7 we're allowed to do ldr's and str's from arbitrary alignments
100
 
  @ but not ldrd/strd or ldm/stm
101
 
  @ Note Neon is often a better choice misaligned using vld1
 
118
        @ On v7 we're allowed to do ldr's and str's from arbitrary alignments
 
119
        @ but not ldrd/strd or ldm/stm
 
120
        @ Note Neon is often a better choice misaligned using vld1
102
121
 
103
 
  @ copy a byte at a time until the point where we have an aligned destination
104
 
  @ we know we have enough bytes to go to know we won't run out in this phase
105
 
  tst     r0,#7
106
 
  beq     35f
 
122
        @ copy a byte at a time until the point where we have an aligned destination
 
123
        @ we know we have enough bytes to go to know we won't run out in this phase
 
124
        tst     r0,#7
 
125
        beq     35f
107
126
 
108
127
31:
109
 
  ldrb    r3,[r1],#1
110
 
  sub     r2,r2,#1
111
 
  strb    r3,[r0],#1
112
 
  tst     r0,#7
113
 
  bne     31b
114
 
 
115
 
  cmp     r2,#32       @ Lets get back to knowing we have 32 bytes to play with
116
 
  blt     11b
117
 
 
118
 
  @ Now the store address is aligned
 
128
        ldrb    r3,[r1],#1
 
129
        sub     r2,r2,#1
 
130
        strb    r3,[r0],#1
 
131
        tst     r0,#7
 
132
        bne     31b
 
133
 
 
134
        cmp     r2,#32          @ Lets get back to knowing we have 32 bytes to play with
 
135
        blt     11b
 
136
 
 
137
        @ Now the store address is aligned
119
138
35:
120
 
  push    {r3,r4,r5,r6,r7,r8,r10,r11,r12,r14}
121
 
  and     r6,r1,#3     @ how misaligned we are
122
 
  cmp     r6,#2
123
 
  cbz     r6, 100f     @ Go there if we're actually aligned
124
 
  bge     120f         @ And here if it's aligned on 2 or 3 byte
125
 
                       @ Note might be worth splitting to bgt and a separate beq
126
 
                       @ if the branches are well separated
 
139
        push    {r3,r4,r5,r6,r7,r8,r10,r11,r12,r14}
 
140
        and     r6,r1,#3        @ how misaligned we are
 
141
        cmp     r6,#2
 
142
        cbz     r6, 100f        @ Go there if we're actually aligned
 
143
        bge     120f            @ And here if it's aligned on 2 or 3 byte
 
144
                @ Note might be worth splitting to bgt and a separate beq
 
145
                @ if the branches are well separated
127
146
 
128
 
  @ At this point dest is aligned, source is 1 byte forward
 
147
        @ At this point dest is aligned, source is 1 byte forward
129
148
110:
130
 
  ldr     r3,[r1]      @ Misaligned load - but it gives the first 4 bytes to store
131
 
  sub     r2,r2,#3     @ Number of bytes left in whole words we can load
132
 
  add     r1,r1,#3     @ To aligned load address
133
 
  bic     r3,r3,#0xff000000
 
149
        ldr     r3,[r1]         @ Misaligned load - but it gives the first 4 bytes to store
 
150
        sub     r2,r2,#3        @ Number of bytes left in whole words we can load
 
151
        add     r1,r1,#3        @ To aligned load address
 
152
        bic     r3,r3,#0xff000000
134
153
 
135
154
112:
136
 
  ldmia   r1!,{r5,r6,r7,r8}
137
 
  sub     r2,r2,#32
138
 
  cmp     r2,#32
139
 
  pld     [r1,#96]
140
 
 
141
 
  orr     r3,r3,r5,lsl#24
142
 
  mov     r4,r5,lsr#8
143
 
  mov     r5,r6,lsr#8
144
 
  orr     r4,r4,r6,lsl#24
145
 
  mov     r6,r7,lsr#8
146
 
  ldmia   r1!,{r10,r11,r12,r14}
147
 
  orr     r5,r5,r7,lsl#24
148
 
  mov     r7,r8,lsr#8
149
 
  orr     r6,r6,r8,lsl#24
150
 
  mov     r8,r10,lsr#8
151
 
  orr     r7,r7,r10,lsl#24
152
 
  mov     r10,r11,lsr#8
153
 
  orr     r8,r8,r11,lsl#24
154
 
  orr     r10,r10,r12,lsl#24
155
 
  mov     r11,r12,lsr#8
156
 
  orr     r11,r11,r14,lsl#24
157
 
  stmia   r0!,{r3,r4,r5,r6,r7,r8,r10,r11}
158
 
  mov     r3,r14,lsr#8
159
 
  
160
 
  bge     112b
161
 
 
162
 
  @ Deal with the stragglers
163
 
  add     r2,r2,#3
164
 
  sub     r1,r1,#3
165
 
  pop     {r3,r4,r5,r6,r7,r8,r10,r11,r12,r14}
166
 
  b       10b
 
155
        ldmia   r1!,{r5,r6,r7,r8}
 
156
        sub     r2,r2,#32
 
157
        cmp     r2,#32
 
158
        pld     [r1,#96]
 
159
 
 
160
        orr     r3,r3,r5,lsl#24
 
161
        mov     r4,r5,lsr#8
 
162
        mov     r5,r6,lsr#8
 
163
        orr     r4,r4,r6,lsl#24
 
164
        mov     r6,r7,lsr#8
 
165
        ldmia   r1!,{r10,r11,r12,r14}
 
166
        orr     r5,r5,r7,lsl#24
 
167
        mov     r7,r8,lsr#8
 
168
        orr     r6,r6,r8,lsl#24
 
169
        mov     r8,r10,lsr#8
 
170
        orr     r7,r7,r10,lsl#24
 
171
        mov     r10,r11,lsr#8
 
172
        orr     r8,r8,r11,lsl#24
 
173
        orr     r10,r10,r12,lsl#24
 
174
        mov     r11,r12,lsr#8
 
175
        orr     r11,r11,r14,lsl#24
 
176
        stmia   r0!,{r3,r4,r5,r6,r7,r8,r10,r11}
 
177
        mov     r3,r14,lsr#8
 
178
        
 
179
        bge     112b
 
180
 
 
181
        @ Deal with the stragglers
 
182
        add     r2,r2,#3
 
183
        sub     r1,r1,#3
 
184
        pop     {r3,r4,r5,r6,r7,r8,r10,r11,r12,r14}
 
185
        b       10b
167
186
 
168
187
100:  @ Dest and source aligned - must have been originally co-misaligned
169
 
      @ Fallback to main aligned case if still big enough
170
 
  pop     {r3,r4,r5,r6,r7,r8,r10,r11,r12,r14}
171
 
  b       4b           @ Big copies (32 bytes or more)
 
188
        @ Fallback to main aligned case if still big enough
 
189
        pop     {r3,r4,r5,r6,r7,r8,r10,r11,r12,r14}
 
190
        b       4b              @ Big copies (32 bytes or more)
172
191
 
173
192
120:  @ Dest is aligned, source is align+2 or 3
174
 
  bgt     130f         @ Now split off for 3 byte offset
 
193
        bgt     130f            @ Now split off for 3 byte offset
175
194
 
176
 
  ldrh    r3,[r1]      
177
 
  sub     r2,r2,#2     @ Number of bytes left in whole words we can load
178
 
  add     r1,r1,#2     @ To aligned load address
 
195
        ldrh    r3,[r1]
 
196
        sub     r2,r2,#2        @ Number of bytes left in whole words we can load
 
197
        add     r1,r1,#2        @ To aligned load address
179
198
 
180
199
122:
181
 
  ldmia   r1!,{r5,r6,r7,r8}
182
 
  sub     r2,r2,#32
183
 
  cmp     r2,#32
184
 
  pld     [r1,#96]
185
 
 
186
 
  orr     r3,r3,r5,lsl#16
187
 
  mov     r4,r5,lsr#16
188
 
  mov     r5,r6,lsr#16
189
 
  orr     r4,r4,r6,lsl#16
190
 
  mov     r6,r7,lsr#16
191
 
  ldmia   r1!,{r10,r11,r12,r14}
192
 
  orr     r5,r5,r7,lsl#16
193
 
  orr     r6,r6,r8,lsl#16
194
 
  mov     r7,r8,lsr#16
195
 
  orr     r7,r7,r10,lsl#16
196
 
  mov     r8,r10,lsr#16
197
 
  orr     r8,r8,r11,lsl#16
198
 
  mov     r10,r11,lsr#16
199
 
  orr     r10,r10,r12,lsl#16
200
 
  mov     r11,r12,lsr#16
201
 
  orr     r11,r11,r14,lsl#16
202
 
  stmia   r0!,{r3,r4,r5,r6,r7,r8,r10,r11}
203
 
  mov     r3,r14,lsr#16
204
 
  
205
 
  bge     122b
206
 
 
207
 
  @ Deal with the stragglers
208
 
  add     r2,r2,#2
209
 
  sub     r1,r1,#2
210
 
  pop     {r3,r4,r5,r6,r7,r8,r10,r11,r12,r14}
211
 
  b       10b
 
200
        ldmia   r1!,{r5,r6,r7,r8}
 
201
        sub     r2,r2,#32
 
202
        cmp     r2,#32
 
203
        pld     [r1,#96]
 
204
 
 
205
        orr     r3,r3,r5,lsl#16
 
206
        mov     r4,r5,lsr#16
 
207
        mov     r5,r6,lsr#16
 
208
        orr     r4,r4,r6,lsl#16
 
209
        mov     r6,r7,lsr#16
 
210
        ldmia   r1!,{r10,r11,r12,r14}
 
211
        orr     r5,r5,r7,lsl#16
 
212
        orr     r6,r6,r8,lsl#16
 
213
        mov     r7,r8,lsr#16
 
214
        orr     r7,r7,r10,lsl#16
 
215
        mov     r8,r10,lsr#16
 
216
        orr     r8,r8,r11,lsl#16
 
217
        mov     r10,r11,lsr#16
 
218
        orr     r10,r10,r12,lsl#16
 
219
        mov     r11,r12,lsr#16
 
220
        orr     r11,r11,r14,lsl#16
 
221
        stmia   r0!,{r3,r4,r5,r6,r7,r8,r10,r11}
 
222
        mov     r3,r14,lsr#16
 
223
        
 
224
        bge     122b
 
225
 
 
226
        @ Deal with the stragglers
 
227
        add     r2,r2,#2
 
228
        sub     r1,r1,#2
 
229
        pop     {r3,r4,r5,r6,r7,r8,r10,r11,r12,r14}
 
230
        b       10b
212
231
 
213
232
130:  @ Dest is aligned, source is align+3
214
 
  ldrb    r3,[r1]      
215
 
  sub     r2,r2,#1     @ Number of bytes left in whole words we can load
216
 
  add     r1,r1,#1     @ To aligned load address
 
233
        ldrb    r3,[r1]
 
234
        sub     r2,r2,#1        @ Number of bytes left in whole words we can load
 
235
        add     r1,r1,#1        @ To aligned load address
217
236
 
218
237
132:
219
 
  ldmia   r1!,{r5,r6,r7,r8}
220
 
  sub     r2,r2,#32
221
 
  cmp     r2,#32
222
 
  pld     [r1,#96]
223
 
 
224
 
  orr     r3,r3,r5,lsl#8
225
 
  mov     r4,r5,lsr#24
226
 
  mov     r5,r6,lsr#24
227
 
  orr     r4,r4,r6,lsl#8
228
 
  mov     r6,r7,lsr#24
229
 
  ldmia   r1!,{r10,r11,r12,r14}
230
 
  orr     r5,r5,r7,lsl#8
231
 
  mov     r7,r8,lsr#24
232
 
  orr     r6,r6,r8,lsl#8
233
 
  mov     r8,r10,lsr#24
234
 
  orr     r7,r7,r10,lsl#8
235
 
  orr     r8,r8,r11,lsl#8
236
 
  mov     r10,r11,lsr#24
237
 
  orr     r10,r10,r12,lsl#8
238
 
  mov     r11,r12,lsr#24
239
 
  orr     r11,r11,r14,lsl#8
240
 
  stmia   r0!,{r3,r4,r5,r6,r7,r8,r10,r11}
241
 
  mov     r3,r14,lsr#24
242
 
  
243
 
  bge     132b
244
 
 
245
 
  @ Deal with the stragglers
246
 
  add     r2,r2,#1
247
 
  sub     r1,r1,#1
248
 
  pop     {r3,r4,r5,r6,r7,r8,r10,r11,r12,r14}
249
 
  b       10b
 
238
        ldmia   r1!,{r5,r6,r7,r8}
 
239
        sub     r2,r2,#32
 
240
        cmp     r2,#32
 
241
        pld     [r1,#96]
 
242
 
 
243
        orr     r3,r3,r5,lsl#8
 
244
        mov     r4,r5,lsr#24
 
245
        mov     r5,r6,lsr#24
 
246
        orr     r4,r4,r6,lsl#8
 
247
        mov     r6,r7,lsr#24
 
248
        ldmia   r1!,{r10,r11,r12,r14}
 
249
        orr     r5,r5,r7,lsl#8
 
250
        mov     r7,r8,lsr#24
 
251
        orr     r6,r6,r8,lsl#8
 
252
        mov     r8,r10,lsr#24
 
253
        orr     r7,r7,r10,lsl#8
 
254
        orr     r8,r8,r11,lsl#8
 
255
        mov     r10,r11,lsr#24
 
256
        orr     r10,r10,r12,lsl#8
 
257
        mov     r11,r12,lsr#24
 
258
        orr     r11,r11,r14,lsl#8
 
259
        stmia   r0!,{r3,r4,r5,r6,r7,r8,r10,r11}
 
260
        mov     r3,r14,lsr#24
 
261
        
 
262
        bge     132b
 
263
 
 
264
        @ Deal with the stragglers
 
265
        add     r2,r2,#1
 
266
        sub     r1,r1,#1
 
267
        pop     {r3,r4,r5,r6,r7,r8,r10,r11,r12,r14}
 
268
        b       10b
250
269