70
by Michael Hope
Added the C only routines from GLIBC 2.16+20120607~git24a6dbe |
1 |
/* memcopy.h -- definitions for memory copy functions. Generic C version.
|
2 |
Copyright (C) 1991, 1992, 1993, 1997, 2004 Free Software Foundation, Inc.
|
|
3 |
This file is part of the GNU C Library.
|
|
4 |
Contributed by Torbjorn Granlund (tege@sics.se).
|
|
5 |
||
6 |
The GNU C Library is free software; you can redistribute it and/or
|
|
7 |
modify it under the terms of the GNU Lesser General Public
|
|
8 |
License as published by the Free Software Foundation; either
|
|
9 |
version 2.1 of the License, or (at your option) any later version.
|
|
10 |
||
11 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 |
Lesser General Public License for more details.
|
|
15 |
||
16 |
You should have received a copy of the GNU Lesser General Public
|
|
17 |
License along with the GNU C Library; if not, see
|
|
18 |
<http://www.gnu.org/licenses/>. */
|
|
19 |
||
20 |
/* The strategy of the memory functions is:
|
|
21 |
||
22 |
1. Copy bytes until the destination pointer is aligned.
|
|
23 |
||
24 |
2. Copy words in unrolled loops. If the source and destination
|
|
25 |
are not aligned in the same way, use word memory operations,
|
|
26 |
but shift and merge two read words before writing.
|
|
27 |
||
28 |
3. Copy the few remaining bytes.
|
|
29 |
||
30 |
This is fast on processors that have at least 10 registers for
|
|
31 |
allocation by GCC, and that can access memory at reg+const in one
|
|
32 |
instruction.
|
|
33 |
||
34 |
I made an "exhaustive" test of this memmove when I wrote it,
|
|
35 |
exhaustive in the sense that I tried all alignment and length
|
|
36 |
combinations, with and without overlap. */
|
|
37 |
||
38 |
#include <sys/cdefs.h> |
|
39 |
#include <endian.h> |
|
40 |
||
41 |
/* The macros defined in this file are:
|
|
42 |
||
43 |
BYTE_COPY_FWD(dst_beg_ptr, src_beg_ptr, nbytes_to_copy)
|
|
44 |
||
45 |
BYTE_COPY_BWD(dst_end_ptr, src_end_ptr, nbytes_to_copy)
|
|
46 |
||
47 |
WORD_COPY_FWD(dst_beg_ptr, src_beg_ptr, nbytes_remaining, nbytes_to_copy)
|
|
48 |
||
49 |
WORD_COPY_BWD(dst_end_ptr, src_end_ptr, nbytes_remaining, nbytes_to_copy)
|
|
50 |
||
51 |
MERGE(old_word, sh_1, new_word, sh_2)
|
|
52 |
[I fail to understand. I feel stupid. --roland]
|
|
53 |
*/
|
|
54 |
||
55 |
/* Type to use for aligned memory operations.
|
|
56 |
This should normally be the biggest type supported by a single load
|
|
57 |
and store. */
|
|
58 |
#define op_t unsigned long int
|
|
59 |
#define OPSIZ (sizeof(op_t))
|
|
60 |
||
61 |
/* Type to use for unaligned operations. */
|
|
62 |
typedef unsigned char byte; |
|
63 |
||
64 |
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
65 |
#define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
|
|
66 |
#endif
|
|
67 |
#if __BYTE_ORDER == __BIG_ENDIAN
|
|
68 |
#define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
|
|
69 |
#endif
|
|
70 |
||
71 |
/* Copy exactly NBYTES bytes from SRC_BP to DST_BP,
|
|
72 |
without any assumptions about alignment of the pointers. */
|
|
73 |
#define BYTE_COPY_FWD(dst_bp, src_bp, nbytes) \
|
|
74 |
do \
|
|
75 |
{ \
|
|
76 |
size_t __nbytes = (nbytes); \
|
|
77 |
while (__nbytes > 0) \
|
|
78 |
{ \
|
|
79 |
byte __x = ((byte *) src_bp)[0]; \
|
|
80 |
src_bp += 1; \
|
|
81 |
__nbytes -= 1; \
|
|
82 |
((byte *) dst_bp)[0] = __x; \
|
|
83 |
dst_bp += 1; \
|
|
84 |
} \
|
|
85 |
} while (0)
|
|
86 |
||
87 |
/* Copy exactly NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR,
|
|
88 |
beginning at the bytes right before the pointers and continuing towards
|
|
89 |
smaller addresses. Don't assume anything about alignment of the
|
|
90 |
pointers. */
|
|
91 |
#define BYTE_COPY_BWD(dst_ep, src_ep, nbytes) \
|
|
92 |
do \
|
|
93 |
{ \
|
|
94 |
size_t __nbytes = (nbytes); \
|
|
95 |
while (__nbytes > 0) \
|
|
96 |
{ \
|
|
97 |
byte __x; \
|
|
98 |
src_ep -= 1; \
|
|
99 |
__x = ((byte *) src_ep)[0]; \
|
|
100 |
dst_ep -= 1; \
|
|
101 |
__nbytes -= 1; \
|
|
102 |
((byte *) dst_ep)[0] = __x; \
|
|
103 |
} \
|
|
104 |
} while (0)
|
|
105 |
||
106 |
/* Copy *up to* NBYTES bytes from SRC_BP to DST_BP, with
|
|
107 |
the assumption that DST_BP is aligned on an OPSIZ multiple. If
|
|
108 |
not all bytes could be easily copied, store remaining number of bytes
|
|
109 |
in NBYTES_LEFT, otherwise store 0. */
|
|
110 |
extern void _wordcopy_fwd_aligned (long int, long int, size_t) __THROW; |
|
111 |
extern void _wordcopy_fwd_dest_aligned (long int, long int, size_t) __THROW; |
|
112 |
#define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes) \
|
|
113 |
do \
|
|
114 |
{ \
|
|
115 |
if (src_bp % OPSIZ == 0) \
|
|
116 |
_wordcopy_fwd_aligned (dst_bp, src_bp, (nbytes) / OPSIZ); \
|
|
117 |
else \
|
|
118 |
_wordcopy_fwd_dest_aligned (dst_bp, src_bp, (nbytes) / OPSIZ); \
|
|
119 |
src_bp += (nbytes) & -OPSIZ; \
|
|
120 |
dst_bp += (nbytes) & -OPSIZ; \
|
|
121 |
(nbytes_left) = (nbytes) % OPSIZ; \
|
|
122 |
} while (0)
|
|
123 |
||
124 |
/* Copy *up to* NBYTES_TO_COPY bytes from SRC_END_PTR to DST_END_PTR,
|
|
125 |
beginning at the words (of type op_t) right before the pointers and
|
|
126 |
continuing towards smaller addresses. May take advantage of that
|
|
127 |
DST_END_PTR is aligned on an OPSIZ multiple. If not all bytes could be
|
|
128 |
easily copied, store remaining number of bytes in NBYTES_REMAINING,
|
|
129 |
otherwise store 0. */
|
|
130 |
extern void _wordcopy_bwd_aligned (long int, long int, size_t) __THROW; |
|
131 |
extern void _wordcopy_bwd_dest_aligned (long int, long int, size_t) __THROW; |
|
132 |
#define WORD_COPY_BWD(dst_ep, src_ep, nbytes_left, nbytes) \
|
|
133 |
do \
|
|
134 |
{ \
|
|
135 |
if (src_ep % OPSIZ == 0) \
|
|
136 |
_wordcopy_bwd_aligned (dst_ep, src_ep, (nbytes) / OPSIZ); \
|
|
137 |
else \
|
|
138 |
_wordcopy_bwd_dest_aligned (dst_ep, src_ep, (nbytes) / OPSIZ); \
|
|
139 |
src_ep -= (nbytes) & -OPSIZ; \
|
|
140 |
dst_ep -= (nbytes) & -OPSIZ; \
|
|
141 |
(nbytes_left) = (nbytes) % OPSIZ; \
|
|
142 |
} while (0)
|
|
143 |
||
144 |
||
145 |
/* Threshold value for when to enter the unrolled loops. */
|
|
146 |
#define OP_T_THRES 16
|