fixes for host gcc 4.6.1
[zpugcc/jano.git] / toolchain / gcc / newlib / libc / string / mempcpy.c
blob284cbea79fdd6e87a7e7bddc53fc48c3526daa18
1 /*
2 FUNCTION
3 <<mempcpy>>---copy memory regions and return end pointer
5 ANSI_SYNOPSIS
6 #include <string.h>
7 void* mempcpy(void *<[out]>, const void *<[in]>, size_t <[n]>);
9 TRAD_SYNOPSIS
10 void *mempcpy(<[out]>, <[in]>, <[n]>
11 void *<[out]>;
12 void *<[in]>;
13 size_t <[n]>;
15 DESCRIPTION
16 This function copies <[n]> bytes from the memory region
17 pointed to by <[in]> to the memory region pointed to by
18 <[out]>.
20 If the regions overlap, the behavior is undefined.
22 RETURNS
23 <<mempcpy>> returns a pointer to the byte following the
24 last byte copied to the <[out]> region.
26 PORTABILITY
27 <<mempcpy>> is a GNU extension.
29 <<mempcpy>> requires no supporting OS subroutines.
33 #include <_ansi.h>
34 #include <stddef.h>
35 #include <limits.h>
36 #include <string.h>
38 /* Nonzero if either X or Y is not aligned on a "long" boundary. */
39 #define UNALIGNED(X, Y) \
40 (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
42 /* How many bytes are copied each iteration of the 4X unrolled loop. */
43 #define BIGBLOCKSIZE (sizeof (long) << 2)
45 /* How many bytes are copied each iteration of the word copy loop. */
46 #define LITTLEBLOCKSIZE (sizeof (long))
48 /* Threshhold for punting to the byte copier. */
49 #define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE)
51 _PTR
52 _DEFUN (mempcpy, (dst0, src0, len0),
53 _PTR dst0 _AND
54 _CONST _PTR src0 _AND
55 size_t len0)
57 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
58 char *dst = (char *) dst0;
59 char *src = (char *) src0;
61 while (len0--)
63 *dst++ = *src++;
66 return dst;
67 #else
68 char *dst = dst0;
69 _CONST char *src = src0;
70 long *aligned_dst;
71 _CONST long *aligned_src;
72 int len = len0;
74 /* If the size is small, or either SRC or DST is unaligned,
75 then punt into the byte copy loop. This should be rare. */
76 if (!TOO_SMALL(len) && !UNALIGNED (src, dst))
78 aligned_dst = (long*)dst;
79 aligned_src = (long*)src;
81 /* Copy 4X long words at a time if possible. */
82 while (len >= BIGBLOCKSIZE)
84 *aligned_dst++ = *aligned_src++;
85 *aligned_dst++ = *aligned_src++;
86 *aligned_dst++ = *aligned_src++;
87 *aligned_dst++ = *aligned_src++;
88 len -= BIGBLOCKSIZE;
91 /* Copy one long word at a time if possible. */
92 while (len >= LITTLEBLOCKSIZE)
94 *aligned_dst++ = *aligned_src++;
95 len -= LITTLEBLOCKSIZE;
98 /* Pick up any residual with a byte copier. */
99 dst = (char*)aligned_dst;
100 src = (char*)aligned_src;
103 while (len--)
104 *dst++ = *src++;
106 return dst;
107 #endif /* not PREFER_SIZE_OVER_SPEED */