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 <<memcpy>>---copy memory regions
20
21 SYNOPSIS
22 #include <string.h>
23 void* memcpy(void *restrict <[out]>, const void *restrict <[in]>,
24 size_t <[n]>);
25
26 DESCRIPTION
27 This function copies <[n]> bytes from the memory region
28 pointed to by <[in]> to the memory region pointed to by
29 <[out]>.
30
31 If the regions overlap, the behavior is undefined.
32
33 RETURNS
34 <<memcpy>> returns a pointer to the first byte of the <[out]>
35 region.
36
37 PORTABILITY
38 <<memcpy>> is ANSI C.
39
40 <<memcpy>> requires no supporting OS subroutines.
41
42 QUICKREF
43 memcpy ansi pure
44 */
45
46 #include <_ansi.h>
47 #include <string.h>
48 #include "local.h"
49 #include <stdint.h>
50
51 /* Nonzero if either X or Y is not aligned on a "long" boundary. */
52 #define UNALIGNED(X, Y) \
53 (((uintptr_t)X & (sizeof (long) - 1)) | ((uintptr_t)Y & (sizeof (long) - 1)))
54
55 /* How many bytes are copied each iteration of the 4X unrolled loop. */
56 #define BIGBLOCKSIZE (sizeof (long) << 2)
57
58 /* How many bytes are copied each iteration of the word copy loop. */
59 #define LITTLEBLOCKSIZE (sizeof (long))
60
61 /* Threshhold for punting to the byte copier. */
62 #define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE)
63
64 #undef memcpy
65
66 void *
67 __inhibit_loop_to_libcall
memcpy(void * __restrict dst0,const void * __restrict src0,size_t len0)68 memcpy (void *__restrict dst0,
69 const void *__restrict src0,
70 size_t len0)
71 {
72 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
73 char *dst = (char *) dst0;
74 char *src = (char *) src0;
75
76 void *save = dst0;
77
78 while (len0--)
79 {
80 *dst++ = *src++;
81 }
82
83 return save;
84 #else
85 char *dst = dst0;
86 const char *src = src0;
87 long *aligned_dst;
88 const long *aligned_src;
89
90 /* If the size is small, or either SRC or DST is unaligned,
91 then punt into the byte copy loop. This should be rare. */
92 if (!TOO_SMALL(len0) && !UNALIGNED (src, dst))
93 {
94 aligned_dst = (long*)dst;
95 aligned_src = (long*)src;
96
97 /* Copy 4X long words at a time if possible. */
98 while (len0 >= BIGBLOCKSIZE)
99 {
100 *aligned_dst++ = *aligned_src++;
101 *aligned_dst++ = *aligned_src++;
102 *aligned_dst++ = *aligned_src++;
103 *aligned_dst++ = *aligned_src++;
104 len0 -= BIGBLOCKSIZE;
105 }
106
107 /* Copy one long word at a time if possible. */
108 while (len0 >= LITTLEBLOCKSIZE)
109 {
110 *aligned_dst++ = *aligned_src++;
111 len0 -= LITTLEBLOCKSIZE;
112 }
113
114 /* Pick up any residual with a byte copier. */
115 dst = (char*)aligned_dst;
116 src = (char*)aligned_src;
117 }
118
119 while (len0--)
120 *dst++ = *src++;
121
122 return dst0;
123 #endif /* not PREFER_SIZE_OVER_SPEED */
124 }
125