1 /* A memcpy for CRIS.
2 Copyright (C) 1994-2005 Axis Communications.
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
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 2. Neither the name of Axis Communications nor the names of its
13 contributors may be used to endorse or promote products derived
14 from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY AXIS COMMUNICATIONS AND ITS CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AXIS
20 COMMUNICATIONS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE. */
28
29 /* FIXME: This file should really only be used for reference, as the
30 result is somewhat depending on gcc generating what we expect rather
31 than what we describe. An assembly file should be used instead. */
32
33 #include <picolibc.h>
34
35 #include <stddef.h>
36 #include "../../string/local.h"
37
38 /* Break even between movem and move16 is really at 38.7 * 2, but
39 modulo 44, so up to the next multiple of 44, we use ordinary code. */
40 #define MEMCPY_BY_BLOCK_THRESHOLD (44 * 2)
41
42 /* No name ambiguities in this file. */
43 __asm__ (".syntax no_register_prefix");
44
45 void *
46 __inhibit_loop_to_libcall
memcpy(void * __restrict pdst,const void * __restrict psrc,size_t pn)47 memcpy(void *__restrict pdst, const void *__restrict psrc, size_t pn)
48 {
49 /* Now we want the parameters put in special registers.
50 Make sure the compiler is able to make something useful of this.
51 As it is now: r10 -> r13; r11 -> r11 (nop); r12 -> r12 (nop).
52
53 If gcc was allright, it really would need no temporaries, and no
54 stack space to save stuff on. */
55
56 register void *return_dst __asm__ ("r10") = pdst;
57 register unsigned char *dst __asm__ ("r13") = pdst;
58 register unsigned const char *src __asm__ ("r11") = psrc;
59 register int n __asm__ ("r12") = pn;
60
61 /* When src is aligned but not dst, this makes a few extra needless
62 cycles. I believe it would take as many to check that the
63 re-alignment was unnecessary. */
64 if (((unsigned long) dst & 3) != 0
65 /* Don't align if we wouldn't copy more than a few bytes; so we
66 don't have to check further for overflows. */
67 && n >= 3)
68 {
69 if ((unsigned long) dst & 1)
70 {
71 n--;
72 *dst = *src;
73 src++;
74 dst++;
75 }
76
77 if ((unsigned long) dst & 2)
78 {
79 n -= 2;
80 *(short *) dst = *(short *) src;
81 src += 2;
82 dst += 2;
83 }
84 }
85
86 /* Decide which copying method to use. */
87 if (n >= MEMCPY_BY_BLOCK_THRESHOLD)
88 {
89 /* It is not optimal to tell the compiler about clobbering any
90 registers; that will move the saving/restoring of those registers
91 to the function prologue/epilogue, and make non-movem sizes
92 suboptimal. */
93 __asm__ volatile
94 ("\
95 ;; GCC does promise correct register allocations, but let's \n\
96 ;; make sure it keeps its promises. \n\
97 .ifnc %0-%1-%2,$r13-$r11-$r12 \n\
98 .error \"GCC reg alloc bug: %0-%1-%4 != $r13-$r12-$r11\" \n\
99 .endif \n\
100 \n\
101 ;; Save the registers we'll use in the movem process \n\
102 ;; on the stack. \n\
103 subq 11*4,sp \n\
104 movem r10,[sp] \n\
105 \n\
106 ;; Now we've got this: \n\
107 ;; r11 - src \n\
108 ;; r13 - dst \n\
109 ;; r12 - n \n\
110 \n\
111 ;; Update n for the first loop. \n\
112 subq 44,r12 \n\
113 0: \n\
114 "
115 #ifdef __arch_common_v10_v32
116 /* Cater to branch offset difference between v32 and v10. We
117 assume the branch below has an 8-bit offset. */
118 " setf\n"
119 #endif
120 " movem [r11+],r10 \n\
121 subq 44,r12 \n\
122 bge 0b \n\
123 movem r10,[r13+] \n\
124 \n\
125 ;; Compensate for last loop underflowing n. \n\
126 addq 44,r12 \n\
127 \n\
128 ;; Restore registers from stack. \n\
129 movem [sp+],r10"
130
131 /* Outputs. */
132 : "=r" (dst), "=r" (src), "=r" (n)
133
134 /* Inputs. */
135 : "0" (dst), "1" (src), "2" (n));
136 }
137
138 while (n >= 16)
139 {
140 *(long *) dst = *(long *) src; dst += 4; src += 4;
141 *(long *) dst = *(long *) src; dst += 4; src += 4;
142 *(long *) dst = *(long *) src; dst += 4; src += 4;
143 *(long *) dst = *(long *) src; dst += 4; src += 4;
144
145 n -= 16;
146 }
147
148 switch (n)
149 {
150 case 0:
151 break;
152
153 case 1:
154 *dst = *src;
155 break;
156
157 case 2:
158 *(short *) dst = *(short *) src;
159 break;
160
161 case 3:
162 *(short *) dst = *(short *) src; dst += 2; src += 2;
163 *dst = *src;
164 break;
165
166 case 4:
167 *(long *) dst = *(long *) src;
168 break;
169
170 case 5:
171 *(long *) dst = *(long *) src; dst += 4; src += 4;
172 *dst = *src;
173 break;
174
175 case 6:
176 *(long *) dst = *(long *) src; dst += 4; src += 4;
177 *(short *) dst = *(short *) src;
178 break;
179
180 case 7:
181 *(long *) dst = *(long *) src; dst += 4; src += 4;
182 *(short *) dst = *(short *) src; dst += 2; src += 2;
183 *dst = *src;
184 break;
185
186 case 8:
187 *(long *) dst = *(long *) src; dst += 4; src += 4;
188 *(long *) dst = *(long *) src;
189 break;
190
191 case 9:
192 *(long *) dst = *(long *) src; dst += 4; src += 4;
193 *(long *) dst = *(long *) src; dst += 4; src += 4;
194 *dst = *src;
195 break;
196
197 case 10:
198 *(long *) dst = *(long *) src; dst += 4; src += 4;
199 *(long *) dst = *(long *) src; dst += 4; src += 4;
200 *(short *) dst = *(short *) src;
201 break;
202
203 case 11:
204 *(long *) dst = *(long *) src; dst += 4; src += 4;
205 *(long *) dst = *(long *) src; dst += 4; src += 4;
206 *(short *) dst = *(short *) src; dst += 2; src += 2;
207 *dst = *src;
208 break;
209
210 case 12:
211 *(long *) dst = *(long *) src; dst += 4; src += 4;
212 *(long *) dst = *(long *) src; dst += 4; src += 4;
213 *(long *) dst = *(long *) src;
214 break;
215
216 case 13:
217 *(long *) dst = *(long *) src; dst += 4; src += 4;
218 *(long *) dst = *(long *) src; dst += 4; src += 4;
219 *(long *) dst = *(long *) src; dst += 4; src += 4;
220 *dst = *src;
221 break;
222
223 case 14:
224 *(long *) dst = *(long *) src; dst += 4; src += 4;
225 *(long *) dst = *(long *) src; dst += 4; src += 4;
226 *(long *) dst = *(long *) src; dst += 4; src += 4;
227 *(short *) dst = *(short *) src;
228 break;
229
230 case 15:
231 *(long *) dst = *(long *) src; dst += 4; src += 4;
232 *(long *) dst = *(long *) src; dst += 4; src += 4;
233 *(long *) dst = *(long *) src; dst += 4; src += 4;
234 *(short *) dst = *(short *) src; dst += 2; src += 2;
235 *dst = *src;
236 break;
237 }
238
239 return return_dst;
240 }
241