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 <stddef.h>
49 #include <limits.h>
50 #include <stdint.h>
51 #include "local.h"
52
53 /* Nonzero if either X or Y is not aligned on a "long" boundary. */
54 #define UNALIGNED(X, Y) \
55 (((uintptr_t)X & (sizeof (long) - 1)) | ((uintptr_t)Y & (sizeof (long) - 1)))
56
57 /* How many bytes are copied each iteration of the 4X unrolled loop. */
58 #define BIGBLOCKSIZE (sizeof (long) << 2)
59
60 /* How many bytes are copied each iteration of the word copy loop. */
61 #define LITTLEBLOCKSIZE (sizeof (long))
62
63 /* Threshhold for punting to the byte copier. */
64 #define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE)
65
66 #undef memmove
67
68 /*SUPPRESS 20*/
69 void *
70 __inhibit_loop_to_libcall
memmove(void * dst_void,const void * src_void,size_t length)71 memmove (void *dst_void,
72 const void *src_void,
73 size_t length)
74 {
75 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
76 char *dst = dst_void;
77 const char *src = src_void;
78
79 if (src < dst && dst < src + length)
80 {
81 /* Have to copy backwards */
82 src += length;
83 dst += length;
84 while (length--)
85 {
86 *--dst = *--src;
87 }
88 }
89 else
90 {
91 while (length--)
92 {
93 *dst++ = *src++;
94 }
95 }
96
97 return dst_void;
98 #else
99 char *dst = dst_void;
100 const char *src = src_void;
101 long *aligned_dst;
102 const long *aligned_src;
103
104 if (src < dst && dst < src + length)
105 {
106 /* Destructive overlap...have to copy backwards */
107 src += length;
108 dst += length;
109 while (length--)
110 {
111 *--dst = *--src;
112 }
113 }
114 else
115 {
116 /* Use optimizing algorithm for a non-destructive copy to closely
117 match memcpy. If the size is small or either SRC or DST is unaligned,
118 then punt into the byte copy loop. This should be rare. */
119 if (!TOO_SMALL(length) && !UNALIGNED (src, dst))
120 {
121 aligned_dst = (long*)dst;
122 aligned_src = (long*)src;
123
124 /* Copy 4X long words at a time if possible. */
125 while (length >= BIGBLOCKSIZE)
126 {
127 *aligned_dst++ = *aligned_src++;
128 *aligned_dst++ = *aligned_src++;
129 *aligned_dst++ = *aligned_src++;
130 *aligned_dst++ = *aligned_src++;
131 length -= BIGBLOCKSIZE;
132 }
133
134 /* Copy one long word at a time if possible. */
135 while (length >= LITTLEBLOCKSIZE)
136 {
137 *aligned_dst++ = *aligned_src++;
138 length -= LITTLEBLOCKSIZE;
139 }
140
141 /* Pick up any residual with a byte copier. */
142 dst = (char*)aligned_dst;
143 src = (char*)aligned_src;
144 }
145
146 while (length--)
147 {
148 *dst++ = *src++;
149 }
150 }
151
152 return dst_void;
153 #endif /* not PREFER_SIZE_OVER_SPEED */
154 }
155