1 /*#************************************************************************#*/
2 /*#-------------------------------------------------------------------------*/
4 /*# FUNCTION NAME: memcpy() */
6 /*# PARAMETERS: void* dst; Destination address. */
7 /*# void* src; Source address. */
8 /*# int len; Number of bytes to copy. */
12 /*# DESCRIPTION: Copies len bytes of memory from src to dst. No guarantees */
13 /*# about copying of overlapping memory areas. This routine is */
14 /*# very sensitive to compiler changes in register allocation. */
15 /*# Should really be rewritten to avoid this problem. */
17 /*#-------------------------------------------------------------------------*/
21 /*# DATE NAME CHANGES */
22 /*# ---- ---- ------- */
23 /*# 941007 Kenny R Creation */
24 /*# 941011 Kenny R Lots of optimizations and inlining. */
25 /*# 941129 Ulf A Adapted for use in libc. */
26 /*# 950216 HP N==0 forgotten if non-aligned src/dst. */
27 /*# Added some optimizations. */
28 /*# 001025 HP Make src and dst char *. Align dst to */
29 /*# dword, not just word-if-both-src-and-dst- */
30 /*# are-misaligned. */
32 /*#-------------------------------------------------------------------------*/
34 #include <linux/types.h>
36 void *memcpy(void *pdst
,
40 /* Ok. Now we want the parameters put in special registers.
41 Make sure the compiler is able to make something useful of this.
42 As it is now: r10 -> r13; r11 -> r11 (nop); r12 -> r12 (nop).
44 If gcc was alright, it really would need no temporaries, and no
45 stack space to save stuff on. */
47 register void *return_dst
__asm__ ("r10") = pdst
;
48 register char *dst
__asm__ ("r13") = pdst
;
49 register const char *src
__asm__ ("r11") = psrc
;
50 register int n
__asm__ ("r12") = pn
;
53 /* When src is aligned but not dst, this makes a few extra needless
54 cycles. I believe it would take as many to check that the
55 re-alignment was unnecessary. */
56 if (((unsigned long) dst
& 3) != 0
57 /* Don't align if we wouldn't copy more than a few bytes; so we
58 don't have to check further for overflows. */
61 if ((unsigned long) dst
& 1)
64 *(char*)dst
= *(char*)src
;
69 if ((unsigned long) dst
& 2)
72 *(short*)dst
= *(short*)src
;
78 /* Decide which copying method to use. Movem is dirt cheap, so the
79 overheap is low enough to always use the minimum block size as the
83 /* For large copies we use 'movem' */
85 /* It is not optimal to tell the compiler about clobbering any
86 registers; that will move the saving/restoring of those registers
87 to the function prologue/epilogue, and make non-movem sizes
89 __asm__
volatile (" \n\
90 ;; Check that the register asm declaration got right. \n\
91 ;; The GCC manual explicitly says TRT will happen. \n\
92 .ifnc %0-%1-%2,$r13-$r11-$r12 \n\
96 ;; Save the registers we'll use in the movem process \n\
102 ;; Now we've got this: \n\
107 ;; Update n for the first loop \n\
110 movem [$r11+],$r10 \n\
113 movem $r10,[$r13+] \n\
115 addq 44,$r12 ;; compensate for last loop underflowing n \n\
117 ;; Restore registers from stack \n\
120 /* Outputs */ : "=r" (dst
), "=r" (src
), "=r" (n
)
121 /* Inputs */ : "0" (dst
), "1" (src
), "2" (n
));
125 /* Either we directly starts copying, using dword copying
126 in a loop, or we copy as much as possible with 'movem'
127 and then the last block (<44 bytes) is copied here.
128 This will work since 'movem' will have updated src,dst,n. */
132 *((long*)dst
)++ = *((long*)src
)++;
133 *((long*)dst
)++ = *((long*)src
)++;
134 *((long*)dst
)++ = *((long*)src
)++;
135 *((long*)dst
)++ = *((long*)src
)++;
139 /* A switch() is definitely the fastest although it takes a LOT of code.
140 * Particularly if you inline code this.
147 *(char*)dst
= *(char*)src
;
150 *(short*)dst
= *(short*)src
;
153 *((short*)dst
)++ = *((short*)src
)++;
154 *(char*)dst
= *(char*)src
;
157 *((long*)dst
)++ = *((long*)src
)++;
160 *((long*)dst
)++ = *((long*)src
)++;
161 *(char*)dst
= *(char*)src
;
164 *((long*)dst
)++ = *((long*)src
)++;
165 *(short*)dst
= *(short*)src
;
168 *((long*)dst
)++ = *((long*)src
)++;
169 *((short*)dst
)++ = *((short*)src
)++;
170 *(char*)dst
= *(char*)src
;
173 *((long*)dst
)++ = *((long*)src
)++;
174 *((long*)dst
)++ = *((long*)src
)++;
177 *((long*)dst
)++ = *((long*)src
)++;
178 *((long*)dst
)++ = *((long*)src
)++;
179 *(char*)dst
= *(char*)src
;
182 *((long*)dst
)++ = *((long*)src
)++;
183 *((long*)dst
)++ = *((long*)src
)++;
184 *(short*)dst
= *(short*)src
;
187 *((long*)dst
)++ = *((long*)src
)++;
188 *((long*)dst
)++ = *((long*)src
)++;
189 *((short*)dst
)++ = *((short*)src
)++;
190 *(char*)dst
= *(char*)src
;
193 *((long*)dst
)++ = *((long*)src
)++;
194 *((long*)dst
)++ = *((long*)src
)++;
195 *((long*)dst
)++ = *((long*)src
)++;
198 *((long*)dst
)++ = *((long*)src
)++;
199 *((long*)dst
)++ = *((long*)src
)++;
200 *((long*)dst
)++ = *((long*)src
)++;
201 *(char*)dst
= *(char*)src
;
204 *((long*)dst
)++ = *((long*)src
)++;
205 *((long*)dst
)++ = *((long*)src
)++;
206 *((long*)dst
)++ = *((long*)src
)++;
207 *(short*)dst
= *(short*)src
;
210 *((long*)dst
)++ = *((long*)src
)++;
211 *((long*)dst
)++ = *((long*)src
)++;
212 *((long*)dst
)++ = *((long*)src
)++;
213 *((short*)dst
)++ = *((short*)src
)++;
214 *(char*)dst
= *(char*)src
;
218 return return_dst
; /* destination pointer. */