xref: /OK3568_Linux_fs/kernel/arch/nios2/lib/memcpy.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* Extracted from GLIBC memcpy.c and memcopy.h, which is:
2*4882a593Smuzhiyun    Copyright (C) 1991, 1992, 1993, 1997, 2004 Free Software Foundation, Inc.
3*4882a593Smuzhiyun    This file is part of the GNU C Library.
4*4882a593Smuzhiyun    Contributed by Torbjorn Granlund (tege@sics.se).
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun    The GNU C Library is free software; you can redistribute it and/or
7*4882a593Smuzhiyun    modify it under the terms of the GNU Lesser General Public
8*4882a593Smuzhiyun    License as published by the Free Software Foundation; either
9*4882a593Smuzhiyun    version 2.1 of the License, or (at your option) any later version.
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun    The GNU C Library is distributed in the hope that it will be useful,
12*4882a593Smuzhiyun    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*4882a593Smuzhiyun    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14*4882a593Smuzhiyun    Lesser General Public License for more details.
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun    You should have received a copy of the GNU Lesser General Public
17*4882a593Smuzhiyun    License along with the GNU C Library; if not, see
18*4882a593Smuzhiyun    <http://www.gnu.org/licenses/>.  */
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <linux/types.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun /* Type to use for aligned memory operations.
23*4882a593Smuzhiyun    This should normally be the biggest type supported by a single load
24*4882a593Smuzhiyun    and store.  */
25*4882a593Smuzhiyun #define	op_t	unsigned long int
26*4882a593Smuzhiyun #define OPSIZ	(sizeof(op_t))
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /* Optimal type for storing bytes in registers.  */
29*4882a593Smuzhiyun #define	reg_char	char
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /* Copy exactly NBYTES bytes from SRC_BP to DST_BP,
34*4882a593Smuzhiyun    without any assumptions about alignment of the pointers.  */
35*4882a593Smuzhiyun #define BYTE_COPY_FWD(dst_bp, src_bp, nbytes)				\
36*4882a593Smuzhiyun do {									\
37*4882a593Smuzhiyun 	size_t __nbytes = (nbytes);					\
38*4882a593Smuzhiyun 	while (__nbytes > 0) {						\
39*4882a593Smuzhiyun 		unsigned char __x = ((unsigned char *) src_bp)[0];	\
40*4882a593Smuzhiyun 		src_bp += 1;						\
41*4882a593Smuzhiyun 		__nbytes -= 1;						\
42*4882a593Smuzhiyun 		((unsigned char *) dst_bp)[0] = __x;			\
43*4882a593Smuzhiyun 		dst_bp += 1;						\
44*4882a593Smuzhiyun 	}								\
45*4882a593Smuzhiyun } while (0)
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /* Copy *up to* NBYTES bytes from SRC_BP to DST_BP, with
48*4882a593Smuzhiyun    the assumption that DST_BP is aligned on an OPSIZ multiple.  If
49*4882a593Smuzhiyun    not all bytes could be easily copied, store remaining number of bytes
50*4882a593Smuzhiyun    in NBYTES_LEFT, otherwise store 0.  */
51*4882a593Smuzhiyun /* extern void _wordcopy_fwd_aligned __P ((long int, long int, size_t)); */
52*4882a593Smuzhiyun /* extern void _wordcopy_fwd_dest_aligned __P ((long int, long int, size_t)); */
53*4882a593Smuzhiyun #define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes)		\
54*4882a593Smuzhiyun do {									\
55*4882a593Smuzhiyun 	if (src_bp % OPSIZ == 0)					\
56*4882a593Smuzhiyun 		_wordcopy_fwd_aligned(dst_bp, src_bp, (nbytes) / OPSIZ);\
57*4882a593Smuzhiyun 	else								\
58*4882a593Smuzhiyun 		_wordcopy_fwd_dest_aligned(dst_bp, src_bp, (nbytes) / OPSIZ);\
59*4882a593Smuzhiyun 	src_bp += (nbytes) & -OPSIZ;					\
60*4882a593Smuzhiyun 	dst_bp += (nbytes) & -OPSIZ;					\
61*4882a593Smuzhiyun 	(nbytes_left) = (nbytes) % OPSIZ;				\
62*4882a593Smuzhiyun } while (0)
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun /* Threshold value for when to enter the unrolled loops.  */
66*4882a593Smuzhiyun #define	OP_T_THRES	16
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun /* _wordcopy_fwd_aligned -- Copy block beginning at SRCP to
69*4882a593Smuzhiyun    block beginning at DSTP with LEN `op_t' words (not LEN bytes!).
70*4882a593Smuzhiyun    Both SRCP and DSTP should be aligned for memory operations on `op_t's.  */
71*4882a593Smuzhiyun /* stream-lined (read x8 + write x8) */
_wordcopy_fwd_aligned(long int dstp,long int srcp,size_t len)72*4882a593Smuzhiyun static void _wordcopy_fwd_aligned(long int dstp, long int srcp, size_t len)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	while (len > 7) {
75*4882a593Smuzhiyun 		register op_t a0, a1, a2, a3, a4, a5, a6, a7;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 		a0 = ((op_t *) srcp)[0];
78*4882a593Smuzhiyun 		a1 = ((op_t *) srcp)[1];
79*4882a593Smuzhiyun 		a2 = ((op_t *) srcp)[2];
80*4882a593Smuzhiyun 		a3 = ((op_t *) srcp)[3];
81*4882a593Smuzhiyun 		a4 = ((op_t *) srcp)[4];
82*4882a593Smuzhiyun 		a5 = ((op_t *) srcp)[5];
83*4882a593Smuzhiyun 		a6 = ((op_t *) srcp)[6];
84*4882a593Smuzhiyun 		a7 = ((op_t *) srcp)[7];
85*4882a593Smuzhiyun 		((op_t *) dstp)[0] = a0;
86*4882a593Smuzhiyun 		((op_t *) dstp)[1] = a1;
87*4882a593Smuzhiyun 		((op_t *) dstp)[2] = a2;
88*4882a593Smuzhiyun 		((op_t *) dstp)[3] = a3;
89*4882a593Smuzhiyun 		((op_t *) dstp)[4] = a4;
90*4882a593Smuzhiyun 		((op_t *) dstp)[5] = a5;
91*4882a593Smuzhiyun 		((op_t *) dstp)[6] = a6;
92*4882a593Smuzhiyun 		((op_t *) dstp)[7] = a7;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 		srcp += 8 * OPSIZ;
95*4882a593Smuzhiyun 		dstp += 8 * OPSIZ;
96*4882a593Smuzhiyun 		len -= 8;
97*4882a593Smuzhiyun 	}
98*4882a593Smuzhiyun 	while (len > 0) {
99*4882a593Smuzhiyun 		*(op_t *)dstp = *(op_t *)srcp;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 		srcp += OPSIZ;
102*4882a593Smuzhiyun 		dstp += OPSIZ;
103*4882a593Smuzhiyun 		len -= 1;
104*4882a593Smuzhiyun 	}
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun /* _wordcopy_fwd_dest_aligned -- Copy block beginning at SRCP to
108*4882a593Smuzhiyun    block beginning at DSTP with LEN `op_t' words (not LEN bytes!).
109*4882a593Smuzhiyun    DSTP should be aligned for memory operations on `op_t's, but SRCP must
110*4882a593Smuzhiyun    *not* be aligned.  */
111*4882a593Smuzhiyun /* stream-lined (read x4 + write x4) */
_wordcopy_fwd_dest_aligned(long int dstp,long int srcp,size_t len)112*4882a593Smuzhiyun static void _wordcopy_fwd_dest_aligned(long int dstp, long int srcp,
113*4882a593Smuzhiyun 					size_t len)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun 	op_t ap;
116*4882a593Smuzhiyun 	int sh_1, sh_2;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	/* Calculate how to shift a word read at the memory operation
119*4882a593Smuzhiyun 	aligned srcp to make it aligned for copy. */
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	sh_1 = 8 * (srcp % OPSIZ);
122*4882a593Smuzhiyun 	sh_2 = 8 * OPSIZ - sh_1;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	/* Make SRCP aligned by rounding it down to the beginning of the `op_t'
125*4882a593Smuzhiyun 	it points in the middle of. */
126*4882a593Smuzhiyun 	srcp &= -OPSIZ;
127*4882a593Smuzhiyun 	ap = ((op_t *) srcp)[0];
128*4882a593Smuzhiyun 	srcp += OPSIZ;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	while (len > 3) {
131*4882a593Smuzhiyun 		op_t a0, a1, a2, a3;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 		a0 = ((op_t *) srcp)[0];
134*4882a593Smuzhiyun 		a1 = ((op_t *) srcp)[1];
135*4882a593Smuzhiyun 		a2 = ((op_t *) srcp)[2];
136*4882a593Smuzhiyun 		a3 = ((op_t *) srcp)[3];
137*4882a593Smuzhiyun 		((op_t *) dstp)[0] = MERGE(ap, sh_1, a0, sh_2);
138*4882a593Smuzhiyun 		((op_t *) dstp)[1] = MERGE(a0, sh_1, a1, sh_2);
139*4882a593Smuzhiyun 		((op_t *) dstp)[2] = MERGE(a1, sh_1, a2, sh_2);
140*4882a593Smuzhiyun 		((op_t *) dstp)[3] = MERGE(a2, sh_1, a3, sh_2);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 		ap = a3;
143*4882a593Smuzhiyun 		srcp += 4 * OPSIZ;
144*4882a593Smuzhiyun 		dstp += 4 * OPSIZ;
145*4882a593Smuzhiyun 		len -= 4;
146*4882a593Smuzhiyun 	}
147*4882a593Smuzhiyun 	while (len > 0) {
148*4882a593Smuzhiyun 		register op_t a0;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 		a0 = ((op_t *) srcp)[0];
151*4882a593Smuzhiyun 		((op_t *) dstp)[0] = MERGE(ap, sh_1, a0, sh_2);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 		ap = a0;
154*4882a593Smuzhiyun 		srcp += OPSIZ;
155*4882a593Smuzhiyun 		dstp += OPSIZ;
156*4882a593Smuzhiyun 		len -= 1;
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
memcpy(void * dstpp,const void * srcpp,size_t len)160*4882a593Smuzhiyun void *memcpy(void *dstpp, const void *srcpp, size_t len)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	unsigned long int dstp = (long int) dstpp;
163*4882a593Smuzhiyun 	unsigned long int srcp = (long int) srcpp;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	/* Copy from the beginning to the end.  */
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	/* If there not too few bytes to copy, use word copy.  */
168*4882a593Smuzhiyun 	if (len >= OP_T_THRES) {
169*4882a593Smuzhiyun 		/* Copy just a few bytes to make DSTP aligned.  */
170*4882a593Smuzhiyun 		len -= (-dstp) % OPSIZ;
171*4882a593Smuzhiyun 		BYTE_COPY_FWD(dstp, srcp, (-dstp) % OPSIZ);
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 		/* Copy whole pages from SRCP to DSTP by virtual address
174*4882a593Smuzhiyun 		   manipulation, as much as possible.  */
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 		/* PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len); */
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 		/* Copy from SRCP to DSTP taking advantage of the known
179*4882a593Smuzhiyun 		   alignment of DSTP. Number of bytes remaining is put in the
180*4882a593Smuzhiyun 		   third argument, i.e. in LEN.  This number may vary from
181*4882a593Smuzhiyun 		   machine to machine. */
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 		WORD_COPY_FWD(dstp, srcp, len, len);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 		/* Fall out and copy the tail. */
186*4882a593Smuzhiyun 	}
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	/* There are just a few bytes to copy.  Use byte memory operations. */
189*4882a593Smuzhiyun 	BYTE_COPY_FWD(dstp, srcp, len);
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	return dstpp;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun 
memcpyb(void * dstpp,const void * srcpp,unsigned len)194*4882a593Smuzhiyun void *memcpyb(void *dstpp, const void *srcpp, unsigned len)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun 	unsigned long int dstp = (long int) dstpp;
197*4882a593Smuzhiyun 	unsigned long int srcp = (long int) srcpp;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	BYTE_COPY_FWD(dstp, srcp, len);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	return dstpp;
202*4882a593Smuzhiyun }
203