1 /*
2 Copyright (c) 2002 Jeff Johnston <jjohnstn@redhat.com>
3  */
4 /*
5 FUNCTION
6         <<mempcpy>>---copy memory regions and return end pointer
7 
8 SYNOPSIS
9         #include <string.h>
10         void* mempcpy(void *<[out]>, const void *<[in]>, size_t <[n]>);
11 
12 DESCRIPTION
13         This function copies <[n]> bytes from the memory region
14         pointed to by <[in]> to the memory region pointed to by
15         <[out]>.
16 
17         If the regions overlap, the behavior is undefined.
18 
19 RETURNS
20         <<mempcpy>> returns a pointer to the byte following the
21         last byte copied to the <[out]> region.
22 
23 PORTABILITY
24 <<mempcpy>> is a GNU extension.
25 
26 <<mempcpy>> requires no supporting OS subroutines.
27 
28 	*/
29 
30 #include <_ansi.h>
31 #include <stddef.h>
32 #include <limits.h>
33 #include <string.h>
34 #include <stdint.h>
35 
36 /* Nonzero if either X or Y is not aligned on a "long" boundary.  */
37 #define UNALIGNED(X, Y) \
38   (((uintptr_t)X & (sizeof (long) - 1)) | ((uintptr_t)Y & (sizeof (long) - 1)))
39 
40 /* How many bytes are copied each iteration of the 4X unrolled loop.  */
41 #define BIGBLOCKSIZE    (sizeof (long) << 2)
42 
43 /* How many bytes are copied each iteration of the word copy loop.  */
44 #define LITTLEBLOCKSIZE (sizeof (long))
45 
46 /* Threshhold for punting to the byte copier.  */
47 #define TOO_SMALL(LEN)  ((LEN) < BIGBLOCKSIZE)
48 
49 void *
mempcpy(void * dst0,const void * src0,size_t len0)50 mempcpy (void *dst0,
51 	const void *src0,
52 	size_t len0)
53 {
54 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
55   char *dst = (char *) dst0;
56   char *src = (char *) src0;
57 
58   while (len0--)
59     {
60       *dst++ = *src++;
61     }
62 
63   return dst;
64 #else
65   char *dst = dst0;
66   const char *src = src0;
67   long *aligned_dst;
68   const long *aligned_src;
69 
70   /* If the size is small, or either SRC or DST is unaligned,
71      then punt into the byte copy loop.  This should be rare.  */
72   if (!TOO_SMALL(len0) && !UNALIGNED (src, dst))
73     {
74       aligned_dst = (long*)dst;
75       aligned_src = (long*)src;
76 
77       /* Copy 4X long words at a time if possible.  */
78       while (len0 >= BIGBLOCKSIZE)
79         {
80           *aligned_dst++ = *aligned_src++;
81           *aligned_dst++ = *aligned_src++;
82           *aligned_dst++ = *aligned_src++;
83           *aligned_dst++ = *aligned_src++;
84           len0 -= BIGBLOCKSIZE;
85         }
86 
87       /* Copy one long word at a time if possible.  */
88       while (len0 >= LITTLEBLOCKSIZE)
89         {
90           *aligned_dst++ = *aligned_src++;
91           len0 -= LITTLEBLOCKSIZE;
92         }
93 
94        /* Pick up any residual with a byte copier.  */
95       dst = (char*)aligned_dst;
96       src = (char*)aligned_src;
97     }
98 
99   while (len0--)
100     *dst++ = *src++;
101 
102   return dst;
103 #endif /* not PREFER_SIZE_OVER_SPEED */
104 }
105