3 <<memcpy>>---copy memory regions
7 void* memcpy(void *<[out]>, const void *<[in]>, size_t <[n]>);
10 void *memcpy(<[out]>, <[in]>, <[n]>
16 This function copies <[n]> bytes from the memory region
17 pointed to by <[in]> to the memory region pointed to by
20 If the regions overlap, the behavior is undefined.
23 <<memcpy>> returns a pointer to the first byte of the <[out]>
29 <<memcpy>> requires no supporting OS subroutines.
36 #include "_ansi.h" /* for _DEFUN */
39 /* Nonzero if either X or Y is not aligned on a "long" boundary. */
40 #define UNALIGNED(X, Y) \
41 (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
43 /* How many bytes are copied each iteration of the 4X unrolled loop. */
44 #define BIGBLOCKSIZE (sizeof (long) << 2)
46 /* How many bytes are copied each iteration of the word copy loop. */
47 #define LITTLEBLOCKSIZE (sizeof (long))
49 /* Threshold for punting to the byte copier. */
50 #define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE)
53 _DEFUN (memcpy
, (dst0
, src0
, len0
),
56 size_t len0
) ICODE_ATTR
;
59 _DEFUN (memcpy
, (dst0
, src0
, len0
),
64 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
65 char *dst
= (char *) dst0
;
66 char *src
= (char *) src0
;
78 _CONST
char *src
= src0
;
80 _CONST
long *aligned_src
;
81 unsigned int len
= len0
;
83 /* If the size is small, or either SRC or DST is unaligned,
84 then punt into the byte copy loop. This should be rare. */
85 if (!TOO_SMALL(len
) && !UNALIGNED (src
, dst
))
87 aligned_dst
= (long*)dst
;
88 aligned_src
= (long*)src
;
90 /* Copy 4X long words at a time if possible. */
91 while (len
>= BIGBLOCKSIZE
)
93 *aligned_dst
++ = *aligned_src
++;
94 *aligned_dst
++ = *aligned_src
++;
95 *aligned_dst
++ = *aligned_src
++;
96 *aligned_dst
++ = *aligned_src
++;
97 len
-= (unsigned int)BIGBLOCKSIZE
;
100 /* Copy one long word at a time if possible. */
101 while (len
>= LITTLEBLOCKSIZE
)
103 *aligned_dst
++ = *aligned_src
++;
104 len
-= LITTLEBLOCKSIZE
;
107 /* Pick up any residual with a byte copier. */
108 dst
= (char*)aligned_dst
;
109 src
= (char*)aligned_src
;
116 #endif /* not PREFER_SIZE_OVER_SPEED */