1 /* Copyright (C) 2001, 2003 Free Software Foundation, Inc.
2 Copyright (C) 1994, 1995, 2000 Axis Communications AB.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /*#************************************************************************#*/
22 /*#-------------------------------------------------------------------------*/
24 /*# FUNCTION NAME: memcpy() */
26 /*# PARAMETERS: void* dst; Destination address. */
27 /*# void* src; Source address. */
28 /*# int len; Number of bytes to copy. */
32 /*# DESCRIPTION: Copies len bytes of memory from src to dst. No guarantees */
33 /*# about copying of overlapping memory areas. This routine is */
34 /*# very sensitive to compiler changes in register allocation. */
35 /*# Should really be rewritten to avoid this problem. */
37 /*#-------------------------------------------------------------------------*/
41 /*# DATE NAME CHANGES */
42 /*# ---- ---- ------- */
43 /*# 941007 Kenny R Creation */
44 /*# 941011 Kenny R Lots of optimizations and inlining. */
45 /*# 941129 Ulf A Adapted for use in libc. */
46 /*# 950216 HP N==0 forgotten if non-aligned src/dst. */
47 /*# Added some optimizations. */
48 /*# 001025 HP Make src and dst char *. Align dst to */
49 /*# dword, not just word-if-both-src-and-dst- */
50 /*# are-misaligned. */
51 /*# 070806 RW Modified for uClibc */
52 /*# (__arch_v32 -> __CONFIG_CRISV32__, */
53 /*# include features.h to reach it.) */
55 /*#-------------------------------------------------------------------------*/
59 #ifdef __CONFIG_CRISV32__
60 /* For CRISv32, movem is very cheap. */
61 #define MEMCPY_BLOCK_THRESHOLD (44)
63 /* Break even between movem and move16 is at 38.7*2, but modulo 44. */
64 #define MEMCPY_BLOCK_THRESHOLD (44*2)
67 void *memcpy(void *, const void *, unsigned int);
69 libc_hidden_proto(memcpy)
70 void *memcpy(void *pdst,
74 /* Ok. Now we want the parameters put in special registers.
75 Make sure the compiler is able to make something useful of this.
76 As it is now: r10 -> r13; r11 -> r11 (nop); r12 -> r12 (nop).
78 If gcc was allright, it really would need no temporaries, and no
79 stack space to save stuff on. */
82 register void *return_dst __asm__ ("r10") = pdst;
84 /* FIXME: Use R10 for something. */
85 # define return_dst dst
88 register char *dst __asm__ ("r13") = pdst;
89 register char *src __asm__ ("r11") = (char *) psrc;
90 register int n __asm__ ("r12") = pn;
93 /* When src is aligned but not dst, this makes a few extra needless
94 cycles. I believe it would take as many to check that the
95 re-alignment was unnecessary. */
96 if (((unsigned long) dst & 3) != 0
97 /* Don't align if we wouldn't copy more than a few bytes; so we
98 don't have to check further for overflows. */
101 if ((unsigned long) dst & 1)
104 *(char*)dst = *(char*)src;
109 if ((unsigned long) dst & 2)
112 *(short*)dst = *(short*)src;
118 /* Decide which copying method to use. */
119 if (n >= MEMCPY_BLOCK_THRESHOLD)
121 /* For large copies we use 'movem' */
123 /* It is not optimal to tell the compiler about clobbering any
124 registers; that will move the saving/restoring of those registers
125 to the function prologue/epilogue, and make non-movem sizes
128 This method is not foolproof; it assumes that the "register asm"
129 declarations at the beginning of the function really are used
130 here (beware: they may be moved to temporary registers).
131 This way, we do not have to save/move the registers around into
132 temporaries; we can safely use them straight away. */
133 __asm__ __volatile__ ("\
134 .syntax no_register_prefix \n\
136 ;; Check that the register asm declaration got right. \n\
137 ;; The GCC manual explicitly says TRT will happen. \n\
138 .ifnc %0-%1-%2,$r13-$r11-$r12 \n\
142 ;; Save the registers we'll use in the movem process \n\
147 ;; Now we've got this: \n\
152 ;; Update n for the first loop \n\
160 addq 44,r12 ;; compensate for last loop underflowing n \n\
162 ;; Restore registers from stack \n\
165 /* Outputs */ : "=r" (dst), "=r" (src), "=r" (n)
166 /* Inputs */ : "0" (dst), "1" (src), "2" (n));
169 /* Either we directly starts copying, using dword copying
170 in a loop, or we copy as much as possible with 'movem'
171 and then the last block (<44 bytes) is copied here.
172 This will work since 'movem' will have updated src,dst,n. */
176 *((long*)dst)++ = *((long*)src)++;
177 *((long*)dst)++ = *((long*)src)++;
178 *((long*)dst)++ = *((long*)src)++;
179 *((long*)dst)++ = *((long*)src)++;
183 /* A switch() is definitely the fastest although it takes a LOT of code.
184 * Particularly if you inline code this.
191 *((char*)dst)++ = *((char*)src)++;
194 *((short*)dst)++ = *((short*)src)++;
197 *((short*)dst)++ = *((short*)src)++;
198 *((char*)dst)++ = *((char*)src)++;
201 *((long*)dst)++ = *((long*)src)++;
204 *((long*)dst)++ = *((long*)src)++;
205 *((char*)dst)++ = *((char*)src)++;
208 *((long*)dst)++ = *((long*)src)++;
209 *((short*)dst)++ = *((short*)src)++;
212 *((long*)dst)++ = *((long*)src)++;
213 *((short*)dst)++ = *((short*)src)++;
214 *((char*)dst)++ = *((char*)src)++;
217 *((long*)dst)++ = *((long*)src)++;
218 *((long*)dst)++ = *((long*)src)++;
221 *((long*)dst)++ = *((long*)src)++;
222 *((long*)dst)++ = *((long*)src)++;
223 *((char*)dst)++ = *((char*)src)++;
226 *((long*)dst)++ = *((long*)src)++;
227 *((long*)dst)++ = *((long*)src)++;
228 *((short*)dst)++ = *((short*)src)++;
231 *((long*)dst)++ = *((long*)src)++;
232 *((long*)dst)++ = *((long*)src)++;
233 *((short*)dst)++ = *((short*)src)++;
234 *((char*)dst)++ = *((char*)src)++;
237 *((long*)dst)++ = *((long*)src)++;
238 *((long*)dst)++ = *((long*)src)++;
239 *((long*)dst)++ = *((long*)src)++;
242 *((long*)dst)++ = *((long*)src)++;
243 *((long*)dst)++ = *((long*)src)++;
244 *((long*)dst)++ = *((long*)src)++;
245 *((char*)dst)++ = *((char*)src)++;
248 *((long*)dst)++ = *((long*)src)++;
249 *((long*)dst)++ = *((long*)src)++;
250 *((long*)dst)++ = *((long*)src)++;
251 *((short*)dst)++ = *((short*)src)++;
254 *((long*)dst)++ = *((long*)src)++;
255 *((long*)dst)++ = *((long*)src)++;
256 *((long*)dst)++ = *((long*)src)++;
257 *((short*)dst)++ = *((short*)src)++;
258 *((char*)dst)++ = *((char*)src)++;
262 return return_dst; /* destination pointer. */
264 libc_hidden_def(memcpy)