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