1 /* Extracted from GLIBC memcpy.c and memcopy.h, which is:
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).
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.
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.
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/>. */
20 #include <linux/types.h>
22 /* Type to use for aligned memory operations.
23 This should normally be the biggest type supported by a single load
25 #define op_t unsigned long int
26 #define OPSIZ (sizeof(op_t))
28 /* Optimal type for storing bytes in registers. */
31 #define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
33 /* Copy exactly NBYTES bytes from SRC_BP to DST_BP,
34 without any assumptions about alignment of the pointers. */
35 #define BYTE_COPY_FWD(dst_bp, src_bp, nbytes) \
37 size_t __nbytes = (nbytes); \
38 while (__nbytes > 0) { \
39 unsigned char __x = ((unsigned char *) src_bp)[0]; \
42 ((unsigned char *) dst_bp)[0] = __x; \
47 /* Copy *up to* NBYTES bytes from SRC_BP to DST_BP, with
48 the assumption that DST_BP is aligned on an OPSIZ multiple. If
49 not all bytes could be easily copied, store remaining number of bytes
50 in NBYTES_LEFT, otherwise store 0. */
51 /* extern void _wordcopy_fwd_aligned __P ((long int, long int, size_t)); */
52 /* extern void _wordcopy_fwd_dest_aligned __P ((long int, long int, size_t)); */
53 #define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes) \
55 if (src_bp % OPSIZ == 0) \
56 _wordcopy_fwd_aligned(dst_bp, src_bp, (nbytes) / OPSIZ);\
58 _wordcopy_fwd_dest_aligned(dst_bp, src_bp, (nbytes) / OPSIZ);\
59 src_bp += (nbytes) & -OPSIZ; \
60 dst_bp += (nbytes) & -OPSIZ; \
61 (nbytes_left) = (nbytes) % OPSIZ; \
65 /* Threshold value for when to enter the unrolled loops. */
68 /* _wordcopy_fwd_aligned -- Copy block beginning at SRCP to
69 block beginning at DSTP with LEN `op_t' words (not LEN bytes!).
70 Both SRCP and DSTP should be aligned for memory operations on `op_t's. */
71 /* stream-lined (read x8 + write x8) */
72 static void _wordcopy_fwd_aligned(long int dstp
, long int srcp
, size_t len
)
75 register op_t a0
, a1
, a2
, a3
, a4
, a5
, a6
, a7
;
77 a0
= ((op_t
*) srcp
)[0];
78 a1
= ((op_t
*) srcp
)[1];
79 a2
= ((op_t
*) srcp
)[2];
80 a3
= ((op_t
*) srcp
)[3];
81 a4
= ((op_t
*) srcp
)[4];
82 a5
= ((op_t
*) srcp
)[5];
83 a6
= ((op_t
*) srcp
)[6];
84 a7
= ((op_t
*) srcp
)[7];
85 ((op_t
*) dstp
)[0] = a0
;
86 ((op_t
*) dstp
)[1] = a1
;
87 ((op_t
*) dstp
)[2] = a2
;
88 ((op_t
*) dstp
)[3] = a3
;
89 ((op_t
*) dstp
)[4] = a4
;
90 ((op_t
*) dstp
)[5] = a5
;
91 ((op_t
*) dstp
)[6] = a6
;
92 ((op_t
*) dstp
)[7] = a7
;
99 *(op_t
*)dstp
= *(op_t
*)srcp
;
107 /* _wordcopy_fwd_dest_aligned -- Copy block beginning at SRCP to
108 block beginning at DSTP with LEN `op_t' words (not LEN bytes!).
109 DSTP should be aligned for memory operations on `op_t's, but SRCP must
111 /* stream-lined (read x4 + write x4) */
112 static void _wordcopy_fwd_dest_aligned(long int dstp
, long int srcp
,
118 /* Calculate how to shift a word read at the memory operation
119 aligned srcp to make it aligned for copy. */
121 sh_1
= 8 * (srcp
% OPSIZ
);
122 sh_2
= 8 * OPSIZ
- sh_1
;
124 /* Make SRCP aligned by rounding it down to the beginning of the `op_t'
125 it points in the middle of. */
127 ap
= ((op_t
*) srcp
)[0];
133 a0
= ((op_t
*) srcp
)[0];
134 a1
= ((op_t
*) srcp
)[1];
135 a2
= ((op_t
*) srcp
)[2];
136 a3
= ((op_t
*) srcp
)[3];
137 ((op_t
*) dstp
)[0] = MERGE(ap
, sh_1
, a0
, sh_2
);
138 ((op_t
*) dstp
)[1] = MERGE(a0
, sh_1
, a1
, sh_2
);
139 ((op_t
*) dstp
)[2] = MERGE(a1
, sh_1
, a2
, sh_2
);
140 ((op_t
*) dstp
)[3] = MERGE(a2
, sh_1
, a3
, sh_2
);
150 a0
= ((op_t
*) srcp
)[0];
151 ((op_t
*) dstp
)[0] = MERGE(ap
, sh_1
, a0
, sh_2
);
160 void *memcpy(void *dstpp
, const void *srcpp
, size_t len
)
162 unsigned long int dstp
= (long int) dstpp
;
163 unsigned long int srcp
= (long int) srcpp
;
165 /* Copy from the beginning to the end. */
167 /* If there not too few bytes to copy, use word copy. */
168 if (len
>= OP_T_THRES
) {
169 /* Copy just a few bytes to make DSTP aligned. */
170 len
-= (-dstp
) % OPSIZ
;
171 BYTE_COPY_FWD(dstp
, srcp
, (-dstp
) % OPSIZ
);
173 /* Copy whole pages from SRCP to DSTP by virtual address
174 manipulation, as much as possible. */
176 /* PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len); */
178 /* Copy from SRCP to DSTP taking advantage of the known
179 alignment of DSTP. Number of bytes remaining is put in the
180 third argument, i.e. in LEN. This number may vary from
181 machine to machine. */
183 WORD_COPY_FWD(dstp
, srcp
, len
, len
);
185 /* Fall out and copy the tail. */
188 /* There are just a few bytes to copy. Use byte memory operations. */
189 BYTE_COPY_FWD(dstp
, srcp
, len
);
194 void *memcpyb(void *dstpp
, const void *srcpp
, unsigned len
)
196 unsigned long int dstp
= (long int) dstpp
;
197 unsigned long int srcp
= (long int) srcpp
;
199 BYTE_COPY_FWD(dstp
, srcp
, len
);