1 /* A memmove for CRIS.
2    Copyright (C) 2000-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    Even worse, we base it on memcpy, on the assumption that overlapping
34    moves are rare, and we will do no worse than the generic memmove.  */
35 
36 #include <picolibc.h>
37 
38 #include <stddef.h>
39 
40 /* Break even between movem and move16 is really at 38.7 * 2, but
41    modulo 44, so up to the next multiple of 44, we use ordinary code.  */
42 #define MEMMOVE_BY_BLOCK_THRESHOLD (44 * 2)
43 
44 /* No name ambiguities in this file.  */
45 __asm__ (".syntax no_register_prefix");
46 
47 void *
memmove(void * pdst,const void * psrc,size_t pn)48 memmove(void *pdst, const void *psrc, size_t pn)
49 {
50   /* Now we want the parameters put in special registers.
51      Make sure the compiler is able to make something useful of this.
52      As it is now: r10 -> r13; r11 -> r11 (nop); r12 -> r12 (nop).
53 
54      If gcc was allright, it really would need no temporaries, and no
55      stack space to save stuff on.  */
56 
57   register void *return_dst __asm__ ("r10") = pdst;
58   register unsigned char *dst __asm__ ("r13") = pdst;
59   register unsigned const char *src __asm__ ("r11") = psrc;
60   register int n __asm__ ("r12") = pn;
61 
62   /* Check and handle overlap.  */
63   if (src < dst && dst < src + n)
64     {
65       /* Destructive overlap.  We could optimize this, but we don't (for
66 	 the moment).  */
67       src += n;
68       dst += n;
69       while (n--)
70 	{
71 	  *--dst = *--src;
72 	}
73 
74       return return_dst;
75     }
76   /* Whew, no overlap.  Proceed as with memcpy.  We could call it instead
77      of having a copy here.  That would spoil some of the optimization, so
78      we take the trouble with having two copies.  */
79 
80   /* When src is aligned but not dst, this makes a few extra needless
81      cycles.  I believe it would take as many to check that the
82      re-alignment was unnecessary.  */
83   if (((unsigned long) dst & 3) != 0
84       /* Don't align if we wouldn't copy more than a few bytes; so we
85 	 don't have to check further for overflows.  */
86       && n >= 3)
87   {
88     if ((unsigned long) dst & 1)
89       {
90 	n--;
91 	*dst = *src;
92 	src++;
93 	dst++;
94       }
95 
96     if ((unsigned long) dst & 2)
97       {
98 	n -= 2;
99 	*(short *) dst = *(short *) src;
100 	src += 2;
101 	dst += 2;
102       }
103   }
104 
105   /* Decide which copying method to use.  */
106   if (n >= MEMMOVE_BY_BLOCK_THRESHOLD)
107     {
108       /* It is not optimal to tell the compiler about clobbering any
109 	 registers; that will move the saving/restoring of those registers
110 	 to the function prologue/epilogue, and make non-movem sizes
111 	 suboptimal.  */
112       __asm__ volatile
113 	("\
114 	 ;; GCC does promise correct register allocations, but let's	\n\
115 	 ;; make sure it keeps its promises.				\n\
116 	 .ifnc %0-%1-%2,$r13-$r11-$r12					\n\
117 	 .error \"GCC reg alloc bug: %0-%1-%4 != $r13-$r12-$r11\"	\n\
118 	 .endif								\n\
119 									\n\
120 	 ;; Save the registers we'll use in the movem process		\n\
121 	 ;; on the stack.						\n\
122 	 subq	11*4,sp							\n\
123 	 movem	r10,[sp]						\n\
124 									\n\
125 	 ;; Now we've got this:						\n\
126 	 ;; r11 - src							\n\
127 	 ;; r13 - dst							\n\
128 	 ;; r12 - n							\n\
129 									\n\
130 	 ;; Update n for the first loop.				\n\
131 	 subq	 44,r12							\n\
132 0:									\n\
133 "
134 #ifdef __arch_common_v10_v32
135 	 /* Cater to branch offset difference between v32 and v10.  We
136 	    assume the branch below has an 8-bit offset.  */
137 "	 setf\n"
138 #endif
139 "	 movem	[r11+],r10						\n\
140 	 subq	44,r12							\n\
141 	 bge	 0b							\n\
142 	 movem	r10,[r13+]						\n\
143 									\n\
144 	 ;; Compensate for last loop underflowing n.			\n\
145 	 addq	44,r12							\n\
146 									\n\
147 	 ;; Restore registers from stack.				\n\
148 	 movem [sp+],r10"
149 
150 	 /* Outputs.  */
151 	 : "=r" (dst), "=r" (src), "=r" (n)
152 
153 	 /* Inputs.  */
154 	 : "0" (dst), "1" (src), "2" (n));
155     }
156 
157   while (n >= 16)
158     {
159       *(long *) dst = *(long *) src; dst += 4; src += 4;
160       *(long *) dst = *(long *) src; dst += 4; src += 4;
161       *(long *) dst = *(long *) src; dst += 4; src += 4;
162       *(long *) dst = *(long *) src; dst += 4; src += 4;
163 
164       n -= 16;
165     }
166 
167   switch (n)
168     {
169     case 0:
170       break;
171 
172     case 1:
173       *dst = *src;
174       break;
175 
176     case 2:
177       *(short *) dst = *(short *) src;
178       break;
179 
180     case 3:
181       *(short *) dst = *(short *) src; dst += 2; src += 2;
182       *dst = *src;
183       break;
184 
185     case 4:
186       *(long *) dst = *(long *) src;
187       break;
188 
189     case 5:
190       *(long *) dst = *(long *) src; dst += 4; src += 4;
191       *dst = *src;
192       break;
193 
194     case 6:
195       *(long *) dst = *(long *) src; dst += 4; src += 4;
196       *(short *) dst = *(short *) src;
197       break;
198 
199     case 7:
200       *(long *) dst = *(long *) src; dst += 4; src += 4;
201       *(short *) dst = *(short *) src; dst += 2; src += 2;
202       *dst = *src;
203       break;
204 
205     case 8:
206       *(long *) dst = *(long *) src; dst += 4; src += 4;
207       *(long *) dst = *(long *) src;
208       break;
209 
210     case 9:
211       *(long *) dst = *(long *) src; dst += 4; src += 4;
212       *(long *) dst = *(long *) src; dst += 4; src += 4;
213       *dst = *src;
214       break;
215 
216     case 10:
217       *(long *) dst = *(long *) src; dst += 4; src += 4;
218       *(long *) dst = *(long *) src; dst += 4; src += 4;
219       *(short *) dst = *(short *) src;
220       break;
221 
222     case 11:
223       *(long *) dst = *(long *) src; dst += 4; src += 4;
224       *(long *) dst = *(long *) src; dst += 4; src += 4;
225       *(short *) dst = *(short *) src; dst += 2; src += 2;
226       *dst = *src;
227       break;
228 
229     case 12:
230       *(long *) dst = *(long *) src; dst += 4; src += 4;
231       *(long *) dst = *(long *) src; dst += 4; src += 4;
232       *(long *) dst = *(long *) src;
233       break;
234 
235     case 13:
236       *(long *) dst = *(long *) src; dst += 4; src += 4;
237       *(long *) dst = *(long *) src; dst += 4; src += 4;
238       *(long *) dst = *(long *) src; dst += 4; src += 4;
239       *dst = *src;
240       break;
241 
242     case 14:
243       *(long *) dst = *(long *) src; dst += 4; src += 4;
244       *(long *) dst = *(long *) src; dst += 4; src += 4;
245       *(long *) dst = *(long *) src; dst += 4; src += 4;
246       *(short *) dst = *(short *) src;
247       break;
248 
249     case 15:
250       *(long *) dst = *(long *) src; dst += 4; src += 4;
251       *(long *) dst = *(long *) src; dst += 4; src += 4;
252       *(long *) dst = *(long *) src; dst += 4; src += 4;
253       *(short *) dst = *(short *) src; dst += 2; src += 2;
254       *dst = *src;
255       break;
256     }
257 
258   return return_dst;
259 }
260