initial import
[glibc.git] / sysdeps / generic / memmove.c
blobe3016819d9fc1689f2a17f1842f0a81f9cfd860e
1 /* memmove -- copy memory to memory until the specified number of bytes
2 has been copied. Overlap is handled correctly.
3 Copyright (C) 1991 Free Software Foundation, Inc.
4 Contributed by Torbjorn Granlund (tege@sics.se).
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
18 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
19 Cambridge, MA 02139, USA. */
21 #include <ansidecl.h>
22 #include <string.h>
23 #include <memcopy.h>
25 /* All this is so that bcopy.c can #include
26 this file after defining some things. */
27 #ifndef a1
28 #define a1 dest /* First arg is DEST. */
29 #define a1const
30 #define a2 src /* Second arg is SRC. */
31 #define a2const CONST
32 #endif
33 #if !defined(RETURN) || !defined(rettype)
34 #define RETURN(s) return (s) /* Return DEST. */
35 #define rettype PTR
36 #endif
38 rettype
39 DEFUN(memmove, (a1, a2, len),
40 a1const PTR a1 AND a2const PTR a2 AND size_t len)
42 unsigned long int dstp = (long int) dest;
43 unsigned long int srcp = (long int) src;
45 /* This test makes the forward copying code be used whenever possible.
46 Reduces the working set. */
47 if (dstp - srcp >= len) /* *Unsigned* compare! */
49 /* Copy from the beginning to the end. */
51 /* If there not too few bytes to copy, use word copy. */
52 if (len >= OP_T_THRES)
54 /* Copy just a few bytes to make DSTP aligned. */
55 len -= (-dstp) % OPSIZ;
56 BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
58 /* Copy from SRCP to DSTP taking advantage of the known
59 alignment of DSTP. Number of bytes remaining is put
60 in the third argumnet, i.e. in LEN. This number may
61 vary from machine to machine. */
63 WORD_COPY_FWD (dstp, srcp, len, len);
65 /* Fall out and copy the tail. */
68 /* There are just a few bytes to copy. Use byte memory operations. */
69 BYTE_COPY_FWD (dstp, srcp, len);
71 else
73 /* Copy from the end to the beginning. */
74 srcp += len;
75 dstp += len;
77 /* If there not too few bytes to copy, use word copy. */
78 if (len >= OP_T_THRES)
80 /* Copy just a few bytes to make DSTP aligned. */
81 len -= dstp % OPSIZ;
82 BYTE_COPY_BWD (dstp, srcp, dstp % OPSIZ);
84 /* Copy from SRCP to DSTP taking advantage of the known
85 alignment of DSTP. Number of bytes remaining is put
86 in the third argumnet, i.e. in LEN. This number may
87 vary from machine to machine. */
89 WORD_COPY_BWD (dstp, srcp, len, len);
91 /* Fall out and copy the tail. */
94 /* There are just a few bytes to copy. Use byte memory operations. */
95 BYTE_COPY_BWD (dstp, srcp, len);
98 RETURN(dest);