1 /*
2 Copyright (c) 1994 Cygnus Support.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms are permitted
6 provided that the above copyright notice and this paragraph are
7 duplicated in all such forms and that any documentation,
8 and/or other materials related to such
9 distribution and use acknowledge that the software was developed
10 at Cygnus Support, Inc. Cygnus Support, Inc. may not be used to
11 endorse or promote products derived from this software without
12 specific prior written permission.
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17 /*
18 FUNCTION
19 <<memmove>>---move possibly overlapping memory
20
21 INDEX
22 memmove
23
24 SYNOPSIS
25 #include <string.h>
26 void *memmove(void *<[dst]>, const void *<[src]>, size_t <[length]>);
27
28 DESCRIPTION
29 This function moves <[length]> characters from the block of
30 memory starting at <<*<[src]>>> to the memory starting at
31 <<*<[dst]>>>. <<memmove>> reproduces the characters correctly
32 at <<*<[dst]>>> even if the two areas overlap.
33
34
35 RETURNS
36 The function returns <[dst]> as passed.
37
38 PORTABILITY
39 <<memmove>> is ANSI C.
40
41 <<memmove>> requires no supporting OS subroutines.
42
43 QUICKREF
44 memmove ansi pure
45 */
46
47 #include <string.h>
48 #include <_ansi.h>
49 #include <stddef.h>
50 #include <limits.h>
51 #include <stdint.h>
52 #include "local.h"
53
54 /* Nonzero if either X or Y is not aligned on a "long" boundary. */
55 #define UNALIGNED(X, Y) \
56 (((uintptr_t)X & (sizeof (long) - 1)) | ((uintptr_t)Y & (sizeof (long) - 1)))
57
58 /* How many bytes are copied each iteration of the 4X unrolled loop. */
59 #define BIGBLOCKSIZE (sizeof (long) << 2)
60
61 /* How many bytes are copied each iteration of the word copy loop. */
62 #define LITTLEBLOCKSIZE (sizeof (long))
63
64 /* Threshhold for punting to the byte copier. */
65 #define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE)
66
67 #undef memmove
68
69 /*SUPPRESS 20*/
70 void *
71 __inhibit_loop_to_libcall
memmove(void * dst_void,const void * src_void,size_t length)72 memmove (void *dst_void,
73 const void *src_void,
74 size_t length)
75 {
76 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
77 char *dst = dst_void;
78 const char *src = src_void;
79
80 if (src < dst && dst < src + length)
81 {
82 /* Have to copy backwards */
83 src += length;
84 dst += length;
85 while (length--)
86 {
87 *--dst = *--src;
88 }
89 }
90 else
91 {
92 while (length--)
93 {
94 *dst++ = *src++;
95 }
96 }
97
98 return dst_void;
99 #else
100 char *dst = dst_void;
101 const char *src = src_void;
102 long *aligned_dst;
103 const long *aligned_src;
104
105 if (src < dst && dst < src + length)
106 {
107 /* Destructive overlap...have to copy backwards */
108 src += length;
109 dst += length;
110 while (length--)
111 {
112 *--dst = *--src;
113 }
114 }
115 else
116 {
117 /* Use optimizing algorithm for a non-destructive copy to closely
118 match memcpy. If the size is small or either SRC or DST is unaligned,
119 then punt into the byte copy loop. This should be rare. */
120 if (!TOO_SMALL(length) && !UNALIGNED (src, dst))
121 {
122 aligned_dst = (long*)dst;
123 aligned_src = (long*)src;
124
125 /* Copy 4X long words at a time if possible. */
126 while (length >= BIGBLOCKSIZE)
127 {
128 *aligned_dst++ = *aligned_src++;
129 *aligned_dst++ = *aligned_src++;
130 *aligned_dst++ = *aligned_src++;
131 *aligned_dst++ = *aligned_src++;
132 length -= BIGBLOCKSIZE;
133 }
134
135 /* Copy one long word at a time if possible. */
136 while (length >= LITTLEBLOCKSIZE)
137 {
138 *aligned_dst++ = *aligned_src++;
139 length -= LITTLEBLOCKSIZE;
140 }
141
142 /* Pick up any residual with a byte copier. */
143 dst = (char*)aligned_dst;
144 src = (char*)aligned_src;
145 }
146
147 while (length--)
148 {
149 *dst++ = *src++;
150 }
151 }
152
153 return dst_void;
154 #endif /* not PREFER_SIZE_OVER_SPEED */
155 }
156