Commit part of FS#11748 by Michael Hohmuth. Adds support for automatically resuming...
[kugel-rb.git] / firmware / libc / memmove.c
blob5f423964bb1a819c131a1d11f91156bcb36e0374
1 /*
2 FUNCTION
3 <<memmove>>---move possibly overlapping memory
5 INDEX
6 memmove
8 ANSI_SYNOPSIS
9 #include <string.h>
10 void *memmove(void *<[dst]>, const void *<[src]>, size_t <[length]>);
12 TRAD_SYNOPSIS
13 #include <string.h>
14 void *memmove(<[dst]>, <[src]>, <[length]>)
15 void *<[dst]>;
16 void *<[src]>;
17 size_t <[length]>;
19 DESCRIPTION
20 This function moves <[length]> characters from the block of
21 memory starting at <<*<[src]>>> to the memory starting at
22 <<*<[dst]>>>. <<memmove>> reproduces the characters correctly
23 at <<*<[dst]>>> even if the two areas overlap.
26 RETURNS
27 The function returns <[dst]> as passed.
29 PORTABILITY
30 <<memmove>> is ANSI C.
32 <<memmove>> requires no supporting OS subroutines.
34 QUICKREF
35 memmove ansi pure
38 #include "config.h"
39 #include <_ansi.h>
40 #include <string.h>
42 /* Nonzero if either X or Y is not aligned on a "long" boundary. */
43 #define UNALIGNED(X, Y) \
44 (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
46 /* How many bytes are copied each iteration of the 4X unrolled loop. */
47 #define BIGBLOCKSIZE (sizeof (long) << 2)
49 /* How many bytes are copied each iteration of the word copy loop. */
50 #define LITTLEBLOCKSIZE (sizeof (long))
52 /* Threshhold for punting to the byte copier. */
53 #define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE)
55 _PTR
56 _DEFUN (memmove, (dst_void, src_void, length),
57 _PTR dst_void _AND
58 _CONST _PTR src_void _AND
59 size_t length) ICODE_ATTR;
61 _PTR
62 _DEFUN (memmove, (dst_void, src_void, length),
63 _PTR dst_void _AND
64 _CONST _PTR src_void _AND
65 size_t length)
67 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
68 char *dst = dst_void;
69 _CONST char *src = src_void;
71 if (src < dst && dst < src + length)
73 /* Have to copy backwards */
74 src += length;
75 dst += length;
76 while (length--)
78 *--dst = *--src;
81 else
83 while (length--)
85 *dst++ = *src++;
89 return dst_void;
90 #else
91 char *dst = dst_void;
92 _CONST char *src = src_void;
93 long *aligned_dst;
94 _CONST long *aligned_src;
95 unsigned int len = length;
97 if (src < dst && dst < src + len)
99 /* Destructive overlap...have to copy backwards */
100 src += len;
101 dst += len;
102 while (len--)
104 *--dst = *--src;
107 else
109 /* Use optimizing algorithm for a non-destructive copy to closely
110 match memcpy. If the size is small or either SRC or DST is unaligned,
111 then punt into the byte copy loop. This should be rare. */
112 if (!TOO_SMALL(len) && !UNALIGNED (src, dst))
114 aligned_dst = (long*)dst;
115 aligned_src = (long*)src;
117 /* Copy 4X long words at a time if possible. */
118 while (len >= BIGBLOCKSIZE)
120 *aligned_dst++ = *aligned_src++;
121 *aligned_dst++ = *aligned_src++;
122 *aligned_dst++ = *aligned_src++;
123 *aligned_dst++ = *aligned_src++;
124 len -= BIGBLOCKSIZE;
127 /* Copy one long word at a time if possible. */
128 while (len >= LITTLEBLOCKSIZE)
130 *aligned_dst++ = *aligned_src++;
131 len -= LITTLEBLOCKSIZE;
134 /* Pick up any residual with a byte copier. */
135 dst = (char*)aligned_dst;
136 src = (char*)aligned_src;
139 while (len--)
141 *dst++ = *src++;
145 return dst_void;
146 #endif /* not PREFER_SIZE_OVER_SPEED */