1 /* Copy memory to memory until the specified number of bytes
2 has been copied. Overlap is handled correctly.
3 Copyright (C) 1991, 1995, 1996, 1997, 2003 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Torbjorn Granlund (tege@sics.se).
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 libc_hidden_proto(memcpy)
29 static void _wordcopy_bwd_aligned (long int dstp, long int srcp, size_t len)
38 a0 = ((op_t *) srcp)[1];
44 a1 = ((op_t *) srcp)[2];
50 a0 = ((op_t *) srcp)[3];
56 a1 = ((op_t *) srcp)[4];
62 a0 = ((op_t *) srcp)[5];
68 a1 = ((op_t *) srcp)[6];
73 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
77 a0 = ((op_t *) srcp)[7];
82 a1 = ((op_t *) srcp)[8];
84 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
86 goto do8; /* No-op. */
92 a0 = ((op_t *) srcp)[7];
93 ((op_t *) dstp)[7] = a1;
95 a1 = ((op_t *) srcp)[6];
96 ((op_t *) dstp)[6] = a0;
98 a0 = ((op_t *) srcp)[5];
99 ((op_t *) dstp)[5] = a1;
101 a1 = ((op_t *) srcp)[4];
102 ((op_t *) dstp)[4] = a0;
104 a0 = ((op_t *) srcp)[3];
105 ((op_t *) dstp)[3] = a1;
107 a1 = ((op_t *) srcp)[2];
108 ((op_t *) dstp)[2] = a0;
110 a0 = ((op_t *) srcp)[1];
111 ((op_t *) dstp)[1] = a1;
113 a1 = ((op_t *) srcp)[0];
114 ((op_t *) dstp)[0] = a0;
122 /* This is the right position for do0. Please don't move
125 ((op_t *) dstp)[7] = a1;
128 /* _wordcopy_bwd_dest_aligned -- Copy block finishing right
129 before SRCP to block finishing right before DSTP with LEN `op_t'
130 words (not LEN bytes!). DSTP should be aligned for memory
131 operations on `op_t', but SRCP must *not* be aligned. */
133 static void _wordcopy_bwd_dest_aligned (long int dstp, long int srcp, size_t len)
138 /* Calculate how to shift a word read at the memory operation
139 aligned srcp to make it aligned for copy. */
141 sh_1 = 8 * (srcp % OPSIZ);
142 sh_2 = 8 * OPSIZ - sh_1;
144 /* Make srcp aligned by rounding it down to the beginning of the op_t
145 it points in the middle of. */
154 a2 = ((op_t *) srcp)[2];
155 a1 = ((op_t *) srcp)[1];
161 a3 = ((op_t *) srcp)[3];
162 a2 = ((op_t *) srcp)[2];
166 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
170 a0 = ((op_t *) srcp)[4];
171 a3 = ((op_t *) srcp)[3];
176 a1 = ((op_t *) srcp)[5];
177 a0 = ((op_t *) srcp)[4];
179 if (OP_T_THRES <= 3 * OPSIZ && len == 0)
181 goto do4; /* No-op. */
187 a3 = ((op_t *) srcp)[3];
188 ((op_t *) dstp)[3] = MERGE (a0, sh_1, a1, sh_2);
190 a2 = ((op_t *) srcp)[2];
191 ((op_t *) dstp)[2] = MERGE (a3, sh_1, a0, sh_2);
193 a1 = ((op_t *) srcp)[1];
194 ((op_t *) dstp)[1] = MERGE (a2, sh_1, a3, sh_2);
196 a0 = ((op_t *) srcp)[0];
197 ((op_t *) dstp)[0] = MERGE (a1, sh_1, a2, sh_2);
205 /* This is the right position for do0. Please don't move
208 ((op_t *) dstp)[3] = MERGE (a0, sh_1, a1, sh_2);
211 void *memmove (void *dest, const void *src, size_t len)
213 unsigned long int dstp = (long int) dest;
214 unsigned long int srcp = (long int) src;
216 /* This test makes the forward copying code be used whenever possible.
217 Reduces the working set. */
218 if (dstp - srcp >= len) /* *Unsigned* compare! */
221 #warning REMINDER: generic-opt memmove assumes memcpy does forward copying!
222 memcpy(dest, src, len);
224 /* Copy from the beginning to the end. */
226 /* If there not too few bytes to copy, use word copy. */
227 if (len >= OP_T_THRES)
229 /* Copy just a few bytes to make DSTP aligned. */
230 len -= (-dstp) % OPSIZ;
231 BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
233 /* Copy whole pages from SRCP to DSTP by virtual address
234 manipulation, as much as possible. */
236 PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len);
238 /* Copy from SRCP to DSTP taking advantage of the known
239 alignment of DSTP. Number of bytes remaining is put
240 in the third argument, i.e. in LEN. This number may
241 vary from machine to machine. */
243 WORD_COPY_FWD (dstp, srcp, len, len);
245 /* Fall out and copy the tail. */
248 /* There are just a few bytes to copy. Use byte memory operations. */
249 BYTE_COPY_FWD (dstp, srcp, len);
254 /* Copy from the end to the beginning. */
258 /* If there not too few bytes to copy, use word copy. */
259 if (len >= OP_T_THRES)
261 /* Copy just a few bytes to make DSTP aligned. */
263 BYTE_COPY_BWD (dstp, srcp, dstp % OPSIZ);
265 /* Copy from SRCP to DSTP taking advantage of the known
266 alignment of DSTP. Number of bytes remaining is put
267 in the third argument, i.e. in LEN. This number may
268 vary from machine to machine. */
270 WORD_COPY_BWD (dstp, srcp, len, len);
272 /* Fall out and copy the tail. */
275 /* There are just a few bytes to copy. Use byte memory operations. */
276 BYTE_COPY_BWD (dstp, srcp, len);
281 libc_hidden_proto(memmove)
282 libc_hidden_def(memmove)